By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,191 Members | 1,411 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,191 IT Pros & Developers. It's quick & easy.

Problem with moving image

P: 71
I bought a book to help me learn to use DirectX with windows programming. It's first trying to walk me through some basic windows programming and graphics before getting into DirectX. I'm trying to expand on one of the example programs in the book but I'm having some problems.

I can get a frame to appear, then an image gets randomly placed in the box. It is suppose to move right, then when it hits the edge move left; it should repeate this till the program is closed. Ultimately I want to it move up/down one notch everytime it hits a side, but right now I'm just trying to get the horizontal movement to work. So far it will make the image, and move it left and right like it's suppose to, the problem is after a dozen or so cycles it freezes. I can't seem to find anything in my code that would cause it to do that. Can anyone help me?

I'm guessing the problem has to be in the Game_Init() or Game_Run() function, but I'll include the whole thing so you can see what I'm trying to do:

//Beginning Game Programming, Second Edition
//Chapter 4
//GameLoop project 3.0
//Change the bitmap to a custom bitmap
/*Modify the program to draw just a single bitmap that moves around in the
window*/
//Hint: Make sure the bitmap doesn't 'fly off' the boundries of the window

#include <windows.h>
#include <winuser.h>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>

#define APPTITLE "Game Loop"

//Function Prototypes
LRESULT CALLBACK WinProc(HWND, UINT, WPARAM, LPARAM);
ATOM MyRegisterClass(HINSTANCE);
BOOL InitInstance(HINSTANCE, int);
void DrawBitmap(HDC, char*, int, int);
void Game_Init();
void Game_Run();
void Game_End();

//Local Variables
HWND global_hwnd;
HDC global_hdc;
int lastX, lastY;
bool moveRight;
bool moveDown;

//The window even callback function
LRESULT CALLBACK WinProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
global_hwnd = hWnd;
global_hdc = GetDC(hWnd);

switch (message)
{
case WM_DESTROY:
PostQuitMessage(0);
break;
} //End of switch statement

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

} //End of WinProc()

//Helper function to set up the window properties
ATOM MyRegisterClass(HINSTANCE hInstance)
{
//Create the window class structure
WNDCLASSEX wc;
wc.cbSize = sizeof(WNDCLASSEX);

//Fill the struct with info
wc.style = CS_HREDRAW | CS_VREDRAW;
wc.lpfnWndProc = (WNDPROC)WinProc;
wc.cbClsExtra = 0;
wc.cbWndExtra = 0;
wc.hInstance = hInstance;
wc.hIcon = NULL;
wc.hCursor = LoadCursor(NULL, IDC_ARROW);
wc.hbrBackground = (HBRUSH)GetStockObject(BLACK_BRUSH);
wc.lpszMenuName = NULL;
wc.lpszClassName = APPTITLE;
wc.hIconSm = NULL;

//Set up the window with the class info
return RegisterClassEx(&wc);

} //End of MyRegisterClass()

//Helper function to create the window and refresh it
BOOL InitInstance(HINSTANCE hInstance, int nCmdShow)
{
HWND hWnd;

//Create a new window
hWnd = CreateWindow(
APPTITLE, //Window class
APPTITLE, //Title bar
WS_OVERLAPPEDWINDOW, //Window Style
CW_USEDEFAULT, //x position of window
CW_USEDEFAULT, //y position of window
500, //Width of the window
400, //Height of the window
NULL, //Parent window
NULL, //Menu
hInstance, //Application instance
NULL); //Window parameters

//Was there an error creating the window?
if (!hWnd)
return FALSE;

//Display the window
ShowWindow(hWnd, nCmdShow);
UpdateWindow(hWnd);

return TRUE;

} //End of InitInstance()

//Entry point for a Windows program
int WINAPI WinMain(HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPSTR lpCmdLine,
int nCmdShow)
{
int done = 0;
MSG msg;

//Register the class
MyRegisterClass(hInstance);

//Initialize application
if (!InitInstance(hInstance, nCmdShow))
return FALSE;

//Initialize the game
Game_Init();

//Main message loop
while (!done)
{
if (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
{
//Look for quit message
if (msg.message == WM_QUIT)
done = 1;

//Decode and pass messages on to WndProc
TranslateMessage(&msg);
DispatchMessage(&msg);
} //End of if statement

//Process game loop
Game_Run();

} //End of while loop

//Do cleanup
Game_End();

return msg.wParam;

} //End of WinMain()

void Game_Init()
{
//Initialize the game...
//Load bitmaps, meshes, textures, sounds, etc.

//Initialize the random number generator
srand(time(NULL));

int x = 0, y = 0;
RECT rect;
GetClientRect(global_hwnd, &rect);
HBITMAP image;
BITMAP bm;

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

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

if (rect.right > 0)
{
x = rand() % (rect.right - rect.left);
y = rand() % (rect.bottom - rect.top);
lastX = x;
lastY = y;

if (x+bm.bmWidth < rect.right)
moveRight = TRUE;
else
moveRight = FALSE;

if (y+bm.bmHeight < rect.bottom)
moveDown = TRUE;
else
moveDown = FALSE;

DrawBitmap(global_hdc, "style.bmp", x, y);

} //End of if statement

} //End of Game_Init()

void Game_Run()
{
//This is called once every frame
//Do not include your own loop here!

int x = lastX, y = lastY;
RECT rect;
GetClientRect(global_hwnd, &rect);
HBITMAP image;
BITMAP bm;

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

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

if (rect.right > 0)
{
if (moveRight) //Moving right
{
if ((x+bm.bmWidth) < rect.right) //room to move right
{
x++;
lastX = x;
}
else //At or past right edge
{
moveRight = FALSE;
x--;
lastX = x;
}
} //End of if statement
else //Moving left
{
if (x > 0) //room to move left
{
x--;
lastX = x;
}
else //At or past left edge
{
moveRight = TRUE;
x++;
lastX = x;
}
} //End of else statement

DrawBitmap(global_hdc, "style.bmp", x, y);

} //End of if statement

} //End of Game_Run()

void Game_End()
{
} //End of Game_End()

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

//Load the bitmap image
image = (HBITMAP)LoadImage(0, filename, 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)
BitBlt(
global_hdc, //Destination device context
x, //x location on destination
y, //y location on destination
bm.bmWidth, //Width of source bitmap
bm.bmHeight, //Height of source bitmap
hdcMem, //Source bitmap device context
0, //Start x on source bitmap
0, //Start yon source bitmap
SRCCOPY); //Blit method

//Delete the device context and bitmap
DeleteDC(hdcMem);
DeleteObject((HBITMAP)image);

} //End of DrawBitmap()
Oct 12 '07 #1
Share this Question
Share on Google+
1 Reply


P: 71
Anyone have any thoughts on this for me? I've tried a few more things, but I still keep missing something. I'm guessing it's something easy that I'm just overlooking.
Oct 15 '07 #2

Post your reply

Sign in to post your reply or Sign up for a free account.