troubleshooting Question

How do I change the direction of a moving bitmap image when it hits a window wall?

Avatar of CollegeKid101
CollegeKid101 asked on
Editors IDEs
2 Comments1 Solution359 ViewsLast Modified:
I am trying to get a bitmap image to bounce around a window. I can get it to bounce around, but there is one small issue. When the bitmap image reaches the right side of the window, or the bottom for that matter, it disappears completely before reappearing and moving in the opposite direction. My instructor said this:

"Rather than changing direction in the x-direction when the bitmaps left value = the right edge, change direction when the bitmaps left value = the right edge - bitmaps' width.

The same applies for the bottom value with"

This makes perfect sense in my brain, but I cannot seem to get this to come out in code. I am just wondering if someone could point me into the right direction as to how and where this code would go!

Also, I read over some code from another thread, and used some of it in my code, mainly in the detection of a wall.

// Beginning Game Programming
// Chapter 4
// GameLoop project
 
#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;
 
//global variables 
static int curX = 100;
static int curY = 50; 
static int nextX = 2;
static int nextY = 1;
 
//the window event 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:
            Game_End();
			PostQuitMessage(0);
			break;
    }
	return DefWindowProc(hWnd, message, wParam, lParam);
}
 
//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);
 
}
 
//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;
}
 
 
//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);
        }
        
        //process game loop
        Game_Run();
	}
 
	return msg.wParam;
}
 
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)
    BitBlt( 
        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
    DeleteDC(hdcMem);
    DeleteObject((HBITMAP)image);
}
 
 
void Game_Init()
{
    //initialize the game...
    //load bitmaps, meshes, textures, sounds, etc.
 
    srand(time(NULL));
}
 
void Game_Run()
{
	Sleep(25); 
 
    RECT rect; 
 
	GetClientRect(global_hwnd, &rect); 
 
	curX += nextX; 
	curY += nextY; 
	
	if (curX >= rect.right)
	{	 
		nextX *= -1;
		curX = rect.right; 
	}
 
	if (curX < rect.left)
	{
		nextX *= -1;
		curX = rect.left;
	}
 
	if (curY > rect.bottom)
	{
		nextY *= -1;
		curY = rect.bottom ;
	}
 
	if (curY < rect.top)
	{
		nextY *= -1;
		curY = rect.top;
	}
	
	DrawBitmap(global_hdc, "asteroid.bmp", curX, curY); 
}
 
void Game_End()
{
 
}
ASKER CERTIFIED SOLUTION
mikluhaf

Our community of experts have been thoroughly vetted for their expertise and industry experience.

Join our community to see this answer!
Unlock 1 Answer and 2 Comments.
Start Free Trial
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 2 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros