473,761 Members | 4,421 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Problem with moving image

71 New Member
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(HI NSTANCE, 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(h Wnd, 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(WNDCLASS EX);

//Fill the struct with info
wc.style = CS_HREDRAW | CS_VREDRAW;
wc.lpfnWndProc = (WNDPROC)WinPro c;
wc.cbClsExtra = 0;
wc.cbWndExtra = 0;
wc.hInstance = hInstance;
wc.hIcon = NULL;
wc.hCursor = LoadCursor(NULL , IDC_ARROW);
wc.hbrBackgroun d = (HBRUSH)GetStoc kObject(BLACK_B RUSH);
wc.lpszMenuName = NULL;
wc.lpszClassNam e = 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(HI NSTANCE hInstance, int nCmdShow)
{
HWND hWnd;

//Create a new window
hWnd = CreateWindow(
APPTITLE, //Window class
APPTITLE, //Title bar
WS_OVERLAPPEDWI NDOW, //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(hW nd);

return TRUE;

} //End of InitInstance()

//Entry point for a Windows program
int WINAPI WinMain(HINSTAN CE 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(&m sg, NULL, 0, 0, PM_REMOVE))
{
//Look for quit message
if (msg.message == WM_QUIT)
done = 1;

//Decode and pass messages on to WndProc
TranslateMessag e(&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(g lobal_hwnd, &rect);
HBITMAP image;
BITMAP bm;

//Load the bitmap image
image = (HBITMAP)LoadIm age(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(glob al_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(g lobal_hwnd, &rect);
HBITMAP image;
BITMAP bm;

//Load the bitmap image
image = (HBITMAP)LoadIm age(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(glob al_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)LoadIm age(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 = CreateCompatibl eDC(global_hdc) ;
SelectObject(hd cMem, 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((H BITMAP)image);

} //End of DrawBitmap()
Oct 12 '07 #1
1 3796
rsteph
71 New Member
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
2895
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) Adding onmousemove="return false" onmousedown="return false" allows this to happen but I want to add events with addEventListener/attachEvent instead. It works in IE6 but not firefox1.0.6
2
290
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 problem comes when I am trying to "touchup" the background form. I could redraw the entire background, but that would make moving an individual image a piticularly processor consuming
2
3165
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 image in the picture box. This works fine on the screen but when I then print the image has moved diagonally towards the top left corner of the picturebox. I am sure the problem is with the line drawing code but can not work out why it is moving...
4
10102
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 both browsers. In one situation and frame I have a long scrollable list of cars offered for sale. Some list lines are linked to a separate page with images, to be loaded into the same frame as the listing. On closing this image page with <a...
10
6778
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 from Netscape. IE is ok but the Netscape browser including Firefox and Safari are not working now. Not sure why. The idea is that when you mouseover the arrows the gallery div moves to left and right. I have changed the script and now the image...
0
1656
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 http://www.movingcompanies.co.il/international/ http://www.movingcompanies.co.il/local-movers/ http://www.movingcompanies.co.il/company/coupons-and-discounts.html http://www.movingcompanies.co.il/commercial/heavy-equipment.html...
0
1852
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 the country. allows you to easily obtain no obligation moving quotes from local movers, long distance movers, international movers, auto transport, storage rentals and specialty movers. office movers, commercial moving, residential moving, movers...
0
1783
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 the country. allows you to easily obtain no obligation moving quotes from local movers, long distance movers, international movers, auto transport, storage rentals and specialty movers. office movers, commercial moving, residential moving, movers...
2
2430
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 top of the screen doesn't stay flush with the top of the screen. I've tried changing the alignment, but have had no luck. I'm not using a stylesheet and am wondering if maybe that's the problem. Also, below the head, I have another table that...
0
9538
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9353
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9975
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9909
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8794
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6623
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5241
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5384
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3889
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.