473,324 Members | 2,313 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,324 software developers and data experts.

Problem with moving image

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
1 3759
rsteph
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

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

Similar topics

1
by: philjhanna | last post by:
Hi I'm having a problem applying return false to a div via addEventListener. I'm adding this so that I can drag (click-hold-move) over an image. (Its so that I can add zooming to the image)...
2
by: jamie | last post by:
I have a few bitmaps drawn on a form and I wish to have the ability to move each of the individual bitmaps on that form. I am using the paint event to draw the images directly on the form. The...
2
by: davem | last post by:
Hi, I am writing a program in VB6 where an image is loaded into a picture box if I print this image it is exactly where I expect it to be. The problem is I need to be able to draw lines on the...
4
by: Heinrich Wolf | last post by:
Hi all I have a history.back() problem with FF(2). IE works as expected, while FF does not. The multi frame website setup as a whole with a lot of frame content switching works flawlessly in...
10
by: cjparis | last post by:
Hello everyone. If anyone can give me a hand I would be gratefull Am doing a site which requires a moving element and have used DHTML to do it. Have a simple Browser detect script to sort IE...
0
by: linkswanted | last post by:
http://www.movingcompanies.co.il/supplies/boxes.html http://www.movingcompanies.co.il/residental/ http://www.movingcompanies.co.il/commercial/corporate-moves.html...
0
by: linkswanted | last post by:
We are your trusted source. World Moving & Storage is bonded and licensed by the U.S. Department of Transportation and is one of the largest residential moving and corporate relocation company in...
0
by: linkswanted | last post by:
We are your trusted source. World Moving & Storage is bonded and licensed by the U.S. Department of Transportation and is one of the largest residential moving and corporate relocation company in...
2
beacon
by: beacon | last post by:
I'm having trouble with a personal web page that I'm designing for work. I'm not well versed in HTML and could use some assistance. Currently, when I open the browser, the table in the head at the...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.