Ok I have a problem here. I'm in my game engine class and for some unknown forsaken reason they decided to teach console c++ then jump to c# for windows apps, then back to c++ to do directx apps, so my skills with c++ and windows apps is well krappy. So I'm trying to learn as quick as possible.

Anyway on to my question, this weeks assignment is we need to load up a bitmap (already did that) and have it float around the screen slowly. I'm only able to get it to constantly load the image so it remebles more of an asteroid belt than a single rock floating in the window.

I'm assuming that DrawBitmap is probably the wrong way to go with this, but not sure. What function do I need to call to acomplish getting the image to move around the screen?

8 Years
Discussion Span
Last Post by Murtan

You just need to manipulate the coordinates of the frame where your are displaying the image.


Here the code I currently have for the image. What it does right now is flashes new images on the screen instead of making the single image float around the screen.

void DrawBitmap(HDC hdcDest, char *filename, int x, int y)
    HBITMAP image;
    BITMAP bm;
	HDC hdcMem;

    //load the bitmap image
    image = (HBITMAP)LoadImage(0,"asteroid.bmp",IMAGE_BITMAP,0,0,LR_LOADFROMFILE);

    //read the bitmap's properties
    GetObject(image, sizeof(BITMAP), &bm);

    //create a device context for the bitmap
    hdcMem = CreateCompatibleDC(global_hdc);
	SelectObject(hdcMem, image);

    //draw the bitmap to the window (bit block transfer)
        global_hdc,              //destination device context
        x, y,                    //x,y location on destination
        bm.bmWidth, bm.bmHeight, //width,height of source bitmap
	    hdcMem,                  //source bitmap device context
        0, 0,                    //start x,y on source bitmap
        SRCCOPY);                //blit method

    //delete the device context and bitmap

void Game_Init()
    //initialize the game...
    //load bitmaps, meshes, textures, sounds, etc.


void Game_Run()
    //this is called once every frame
    //do not include your own loop here!
    int x = 0, y = 0;
    RECT rect;
    GetClientRect(global_hwnd, &rect);
    if (rect.right > 0)
        x = rand() % (rect.right - rect.left);
        y = rand() % (rect.bottom - rect.top);
        DrawBitmap(global_hdc, "asteroid.bmp", x, y);

I was going to recommend that you only load the bitmap once, but I noticed that your code already has a comment about it:

//initialize the game...
    //load bitmaps, meshes, textures, sounds, etc.

And your whole x = rand()..., y = rand() appears to be all about picking a random location on the screen to draw at.

From what you described the desired behavior to be, you want to start your image somewhere (maybe the center?). Then you select a random direction (as dx, dy) for it to start moving. Then on each pass of the loop, you calculate the 'new position' of the image from the old by applying dx, dy and doing edge testing. (Edge testing is where you determine if the new position would cause any portion of your image to fall 'outside' the screen -- an alternative edge test if you don't mind some of your image off-screen is to edge test for the center of your image rather than the outside edges) If you have hit a screen edge, invert the direction in relation to that edge. For example if dy was <0 and we 'hit' the Y 0 edge of the screen, you make dy=-dy (which makes it positive) and re-calculate the new position. Once you're "happy" with the new position, you use BitBlt to move the image from where it was to the new position. The whole 'float about the screen' thing happens as your image 'bounces' off the edges of the screen.

If any of that makes sense to you, apply it and see what it does. If it still doesn't work like you want, post your new code, and question in the form "I wanted it to do ____ but it is doing ____. I've tried ___ but it didn't work."

PS- When posting c++ code, please use c++ code tags
[code=c++] // Your code here


This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.