directx animate bitmap

Posted on 2010-11-19
Last Modified: 2013-12-14
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
(I will provide code if necessary)
Question by:kyriakos70
  • 4
  • 2
LVL 12

Expert Comment

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.

Author Comment

ID: 34206639
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.

#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

// sprite declarations
LPDIRECT3DTEXTURE9 sprite;    // O pointer GIA TO sprite
// 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;
    ZeroMemory(&wc, sizeof(WNDCLASSEX));
    wc.cbSize = sizeof(WNDCLASSEX); = CS_HREDRAW | CS_VREDRAW;
    wc.lpfnWndProc = (WNDPROC)WindowProc;
    wc.hInstance = hInstance;
    wc.hCursor = LoadCursor(NULL, IDC_ARROW);
    wc.lpszClassName = "WindowClass1";

    hWnd = CreateWindowEx(NULL,
                          "Our Direct3D Program",
                          WS_EX_TOPMOST | WS_POPUP,
                          0, 0,
                          SCREEN_WIDTH, SCREEN_HEIGHT,
    ShowWindow(hWnd, nCmdShow);
    // set up and initialize Direct3D
    // enter the main loop:
    MSG msg;
        DWORD starting_point = GetTickCount();
        if (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
            if (msg.message == WM_QUIT)
       // check the 'escape' key
            PostMessage(hWnd, WM_DESTROY, 0, 0);
        while ((GetTickCount() - starting_point) < 25);
    // clean up DirectX and COM
    return msg.wParam;
LRESULT CALLBACK WindowProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
        case WM_DESTROY:
                return 0;
            } break;
    return DefWindowProc (hWnd, message, wParam, lParam);
void initD3D(HWND hWnd)
    d3d = Direct3DCreate9(D3D_SDK_VERSION);


    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
    D3DXCreateSprite(d3ddev, &d3dspt);
 D3DXCreateTextureFromFile(d3ddev, "back_temp2.png", &sprite);
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);


Open in new window

LVL 12

Accepted Solution

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
Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.


Author Comment

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?

Thank you again satrumo
IDirect3DTexture9 *sprite[2];
D3DXCreateTextureFromFile(d3ddev, "Panel1.png", &sprite[0]);
D3DXCreateTextureFromFile(d3ddev, "Panel1.png", &sprite[1]);

Open in new window

LVL 12

Expert Comment

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.
LVL 12

Expert Comment

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

Featured Post

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
max float value 3 40
execute SQL Stored Procedure 1000 times from 3 34
Why does my excel data start at B1 instead of A1. 9 81
DCT of  2D array using fftw in c++ 9 38
Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
The viewer will learn how to use and create keystrokes in Netbeans IDE 8.0 for Windows.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

776 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