Solved

directx animate bitmap

Posted on 2010-11-19
8
885 Views
Last Modified: 2013-12-14
Hello,
I have a directx 9 application, I have some bitmaps in png and want to animate them like falling down (like tetris free fall down), any help I am a niewbie?

Thank you
Kyriakos
(I will provide code if necessary)
0
Comment
Question by:kyriakos70
  • 4
  • 2
8 Comments
 
LVL 12

Expert Comment

by:satsumo
ID: 34205100
The question isn't getting many answers because its not badly defined.  Its almost like your asking us to explain how to write a program.  If an Expert tells you what code to write then you haven't written the program, the Expert did.  If you explain what is working so far, and show some code, you're much more likely to get some answers.
0
 

Author Comment

by:kyriakos70
ID: 34206639
OK,
Problem 1) I have the first code and create a window 1024x768, the bitmap is also 1024x768 but it is not showing all of the bitmap, the bottom is cut doesn't fit to the window, is there any code to make it fit to the window the dimensions are the same even if I insert a rect with other dimensions and enter it like this d3dspt->Draw(sprite, &rct, NULL, NULL, D3DCOLOR_XRGB(255, 255, 255));
it is not showing correct?
problem 2) I need to make a n animation that change the position x ok I done it through satsumo's answer, I have to do is to stop the falling bitmap to a given location or if under it there is another bitmap. I have some code and provide it for this question too.
Sorry for the 2 questions if need I will create a new one to answer it and give the points.

Kyriakos
#define SCREEN_WIDTH  1024

#define SCREEN_HEIGHT 768

#define KEY_DOWN(vk_code) ((GetAsyncKeyState(vk_code) & 0x8000) ? 1 : 0)

#define KEY_UP(vk_code) ((GetAsyncKeyState(vk_code) & 0x8000) ? 0 : 1)

// include the Direct3D Library file

#pragma comment (lib, "d3d9.lib")

#pragma comment (lib, "d3dx9.lib")

LPDIRECT3D9 d3d;    

LPDIRECT3DDEVICE9 d3ddev;    // the pointer to the device class

LPD3DXSPRITE d3dspt;    // the pointer to our Direct3D Sprite interface

LPD3DXSPRITE d3dspt1;

LPDIRECT3DDEVICE9 d3ddev1;



// sprite declarations

LPDIRECT3DTEXTURE9 sprite;    // O pointer GIA TO sprite

LPDIRECT3DTEXTURE9 sprite1;

// function prototypes

void initD3D(HWND hWnd); // sets up and initializes Direct3D

void render_frame(void); // renders a single frame

void cleanD3D(void); // closes Direct3D and releases memory

LRESULT CALLBACK WindowProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam);

// the entry point for any Windows program

int WINAPI WinMain(HINSTANCE hInstance,

                   HINSTANCE hPrevInstance,

                   LPSTR lpCmdLine,

                   int nCmdShow)

{

    HWND hWnd;

    WNDCLASSEX wc;

    ZeroMemory(&wc, sizeof(WNDCLASSEX));

    wc.cbSize = sizeof(WNDCLASSEX);

    wc.style = CS_HREDRAW | CS_VREDRAW;

    wc.lpfnWndProc = (WNDPROC)WindowProc;

    wc.hInstance = hInstance;

    wc.hCursor = LoadCursor(NULL, IDC_ARROW);

    wc.lpszClassName = "WindowClass1";

    RegisterClassEx(&wc);



    hWnd = CreateWindowEx(NULL,

                          "WindowClass1",

                          "Our Direct3D Program",

                          WS_EX_TOPMOST | WS_POPUP,

                          0, 0,

                          SCREEN_WIDTH, SCREEN_HEIGHT,

                          NULL,

                          NULL,

                          hInstance,

                          NULL);

    ShowWindow(hWnd, nCmdShow);

    // set up and initialize Direct3D

    initD3D(hWnd);

    // enter the main loop:

    MSG msg;

    while(TRUE)

    {

        DWORD starting_point = GetTickCount();

        if (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))

        {

            if (msg.message == WM_QUIT)

                break;

            TranslateMessage(&msg);

            DispatchMessage(&msg);

        }

        render_frame();

       // check the 'escape' key

        if(KEY_DOWN(VK_ESCAPE))

            PostMessage(hWnd, WM_DESTROY, 0, 0);

        while ((GetTickCount() - starting_point) < 25);

    }

    // clean up DirectX and COM

    cleanD3D();

    return msg.wParam;

}

LRESULT CALLBACK WindowProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)

{

    switch(message)

    {

        case WM_DESTROY:

            {

                PostQuitMessage(0);

                return 0;

            } break;

    }

    return DefWindowProc (hWnd, message, wParam, lParam);

}

void initD3D(HWND hWnd)

{

    d3d = Direct3DCreate9(D3D_SDK_VERSION);



    D3DPRESENT_PARAMETERS d3dpp;



    ZeroMemory(&d3dpp, sizeof(d3dpp));

    d3dpp.Windowed = TRUE;

    d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD;

    d3dpp.hDeviceWindow = hWnd;

    d3dpp.BackBufferFormat = D3DFMT_X8R8G8B8;

    d3dpp.BackBufferWidth = SCREEN_WIDTH;    d3dpp.BackBufferHeight = SCREEN_HEIGHT;

//edo na valo kai kodika gia 1024x768

 // create a device class using this information and the info from the d3dpp stuct

    d3d->CreateDevice(D3DADAPTER_DEFAULT,

                      D3DDEVTYPE_HAL,

                      hWnd,

                      D3DCREATE_SOFTWARE_VERTEXPROCESSING,

                      &d3dpp,

                      &d3ddev);

    D3DXCreateSprite(d3ddev, &d3dspt);

 D3DXCreateTextureFromFile(d3ddev, "back_temp2.png", &sprite);

    return;

}

void render_frame(void)

{

D3DXVECTOR3 center(0.0f, 0.0f, 0.0f);    // center at the upper-left corner

D3DXVECTOR3 position(0.0f, 0.0f, 0.0f);

3ddev->BeginScene();    // begins the 3D scene

 d3dspt->Begin(NULL);    // begin sprite drawing	

d3dspt->Draw(sprite, NULL, &center, &position, D3DCOLOR_XRGB(255, 255, 255));

 d3dspt->End();    // end sprite drawing



    d3ddev->EndScene();    // ends the 3D scene



    d3ddev->Present(NULL, NULL, NULL, NULL);



    return;

}

Open in new window

0
 
LVL 12

Accepted Solution

by:
satsumo earned 500 total points
ID: 34207776
Problem 1: This function, d3ddev->Present(NULL, NULL, NULL, NULL), Direct X should draw the entire BackBuffer (which is 1024 x 768) inside  the window.  It dosen't matter what size the window is, DirectX should be stretching or shrinking the image to fit.

When you create the window at 1024 x 768, that is the size of the whole window, including the frame and the title bar.  The part of the window that can be rendered in (the client area), is smaller than 1024 x 768.  However Direct X should still scale the image to fit.

I can see no reason why this shouldn't work, the code draws the background in the right place.  You could try setting the D3DTS_WORLD matrix to identity, just to be sure.

Problem 2: Assuming you know how to make the object fall, to stop it falling at a specific height you do this -

#define GROUND_HEIGHT 640

position.y += 2; // this makes the sprite fall
if (position.y > GROUND_HEIGHT) position.y = GROUND_HEIGHT;

Before I can answer the part about landing on other bitmaps, your code needs another bitmap to test against.  To make a game of any complexity, you need a list of things.  The player, the enemies, bullets, pickups, explosions, moving platforms, falling blocks, whatever the game needs.  If the player shoots, you add a bullet thing to the list, if the bullet goes off the screen you remove it.  You update all of the things in the list and then you render them.

I suggest you separate your update and render code too.  The game loop should be more like this -

while (game_is_running)
{
   get_input () // check keys or mouse
   update_game (); // move things, check for collisions
   render_frame (); // disply the things in the game
}
0
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 

Author Comment

by:kyriakos70
ID: 34213254
Hello satsumo,
Here is what movement I am trying to achieve, in the ling you will find a slot machine, I want to do the spinning of the reels, I think I must create a rectangle and import ramndom bitmaps of the figures, eg for the first figure must appear 3 times the second must 2 times, but from a list of figures like
code below and with a random() function to show the bitmaps?
https://flashcasino.ladbrokes.com/instant-play-en/1clickStart.asp?sEXT1=demo&sEXT2=demo&gameID=thunderstruck2.

Thank you again satrumo
IDirect3DTexture9 *sprite[2];

D3DXCreateTextureFromFile(d3ddev, "Panel1.png", &sprite[0]);

D3DXCreateTextureFromFile(d3ddev, "Panel1.png", &sprite[1]);

Open in new window

0
 
LVL 12

Expert Comment

by:satsumo
ID: 34223449
Kyriakos, I do not feel I can continue to answer until you show that you have some understanding of programming.  None of the questions you have asked or code you have posted demonstrate any kind of development on your part.  The code you show appears to be from other sources.  

If you can program, then I would be willing to help, if you make an attempt to program the game.  The answers you already have contain enough information.  Another expert may be willing to write the program as an answer to this question.  Writing games is my job and I don't work for EE.
0
 
LVL 12

Expert Comment

by:satsumo
ID: 34327186
kyriakos70, thank you for accepting the solution.  I hope your game development is a success.
0

Featured Post

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

Suggested Solutions

Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.

757 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now