• Advertisement
Sign in to follow this  

Need Help Modifying Code

This topic is 2652 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi guys. I'm relatively new to programming and I'm currently going through "Beginning Game Programming" by John S Harbour. One of the "Own Your Own" assignments in Chapter 2 is to "Modify the below program so that it draws just a single bitmap that moves around in the winwdow. (Hint: You will need to make sure the bitmap doesn't "fly off" the boundaries of the window.)"

I can't think of an easy way to do this. Help anyone? Thanks.


#include <windows.h>
#include <iostream>
#include <time.h>

using namespace std;

const string APPTITLE = "Your FACE";
HWND window;
HDC device;
bool gameover = false;


//
//load bitmap
//

void DrawBitmap(char *filename, int x, int y)
{
//load bitmap
HBITMAP image = (HBITMAP)LoadImage(0,"1.bmp",IMAGE_BITMAP,0,0,LR_LOADFROMFILE);

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

//creat a device context for the bitmap
HDC hdcImage = CreateCompatibleDC(device);
SelectObject(hdcImage, image);

//draw the bitmap to the window (bit block transfer)

BitBlt(
device, //destination device context
x,y, //x,y location on destination
bm.bmWidth, bm.bmHeight, //width,height of source bitmap
hdcImage, //source bitmap device context
0, 0, //start xmy on source bitmap
SRCCOPY); //blit method

//delete the device context and bitmap
DeleteDC(hdcImage);
DeleteObject((HBITMAP)image);
}


//
//
//Startup and loading code
//
bool Game_Init()
{
//start up random number generator
srand(time(NULL));

return 1;
}


//
//Update function called from inside game loop
//
void Game_Run()
{
if(gameover==true)return;

//get the drawing surface
RECT rect;
GetClientRect(window, &rect);

//draw bitmap at random location
int x = rand() % (rect.right - rect.left);
int y = rand() % (rect.bottom - rect.top);
DrawBitmap("1 .bmp",x,y);
}

//
//Shutdown code
//
void Game_End()
{
//free device
ReleaseDC(window,device);
}


//
// Windows Callback Function
//
LRESULT CALLBACK WinProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
switch (message)
{
case WM_DESTROY:
gameover = true;
PostQuitMessage(0);
break;
}
return DefWindowProc(hWnd, message, wParam, lParam);
}

//
// MyRegisterClass function sets program window properties
//
ATOM MyRegisterClass(HINSTANCE hInstance)
{
//create the windows class structure
WNDCLASSEX wc;
wc.cbSize = sizeof(WNDCLASSEX);

//fill 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.c_str();
wc.hIconSm = NULL;

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


//
//Helper function to create the window and refresh it
//
BOOL InitInstance(HINSTANCE hInstance, int nCmdShow)
{
//create a new window
window = CreateWindow(
APPTITLE.c_str(), //window class
APPTITLE.c_str(), //title bar
WS_OVERLAPPEDWINDOW, //window style
CW_USEDEFAULT, //x position of window
CW_USEDEFAULT, //y position of window
640, //width of window
480, //height of the window
NULL, //parent window
NULL, //menu
hInstance, //application instance
NULL); //window paramenters

//was there an error creating the window?
if (window == 0) return 0;

//display the window
ShowWindow(window, nCmdShow);

UpdateWindow(window);

//get device context for drawing
device = GetDC(window);

return 1;
}


//
// Entry Point Function
//
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
{
MSG msg;

//create window
MyRegisterClass(hInstance);
if (!InitInstance(hInstance, nCmdShow)) return 0;

//intialize the game
if(!Game_Init()) return 0;

//main message loop
while (!gameover)
{
//process Windows Events
if(PeekMessage(&msg,NULL,0,0,PM_REMOVE))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}

//process game loop
Game_Run();
UpdateWindow(window);
}

//free game resources
Game_End();

return msg.wParam;
}



Share this post


Link to post
Share on other sites
Advertisement
You normally clear a window by calling InvalidateRect(hwnd,NULL,TRUE). That results in a WM_PAINT message being sent to the window procedure. The background is erased when the BeginPaint() function is called.

Try adding the WM_PAINT message to your window procedure, and draw the bitmap (with random position as desired) in the BeginPaint/EndPaint sequence.

Somewhere you'll want to loop through a timer of some sort, calling InvalidateRect, then sleeping for a while, until the user indicates he wants to quit.

Maybe something like:

while(!gameover)
{
while(PeekMessage(...)) // Use WHILE not IF to process all messages before continuing
{ ... } // WM_PAINT handles drawing the bitmap
if( !gameover )
{
Sleep(500); // 500 milliseconds = 1/2 second. Longer if you want
InvalidateRect(hwnd,NULL,TRUE);
}
}

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement