Thread: Trying to get a bmp. to move with in boundries in a windows ap....

  1. #1
    Registered User
    Join Date
    Jun 2005
    Posts
    131

    Trying to get a bmp. to move with in boundries in a windows ap....

    I have gotten all the way up to the point were the bitmap is drawn on the windows ap. Now I basically am trying to get the bitmap to float around never leaving the bounds of the the ap. So if it hits a side of the ap (width or height) then it is just going to bounce off and go another way. Kind of like that one game were there was a paddle at the bottom of the screen and you had to bounce a ball towards the top were you broke down bricks. Anyways, take a look at the last function...
    Code:
    #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;
    
    //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 = LoadImage(0,"w.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()
    {
        //this is called once every frame
        char buffer[80];
        static iCount = 0;
        int x = 0, y = 0;
        RECT rect;
        GetClientRect(global_hwnd, &rect);
    
    //    if (rect.right > 0)
    //    {
    //     x = rand() % (rect.right - rect.left);
    //     y = rand() % (rect.bottom - rect.top);
            DrawBitmap(global_hdc, "c.bmp", x, y);
    //    }
    
        iCount++;
        sprintf(buffer,"%d",iCount);
        TextOut(global_hdc,0,10, buffer,strlen(buffer));
    }
    If you take a look you will notice I am trying to get this accomplished in the Game_Run(). As of now the bitmap is drawn in the top left corner of the form (0,0) The stuff that is commented out is some things I was working on but failing at.

    How do you create an object of the actual instance of the bitmap I a trying to get to move?

    Thanks

    Chad
    Last edited by chadsxe; 01-06-2006 at 11:11 AM.

  2. #2
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Code:
    strlen(buffer)
    ->
    Code:
    #include <cstring>
    strlen(buffer)
    Do something like this:
    Code:
    static int x, y;  /* static variables are initialized to 0 by default */
    static int xdir, ydir;
    
    x += (xdir ? -1 : 1);
    y += (ydir ? -2 : 2);
    
    if(x == 0) xdir = 0;
    else if(x == MAX_X_POSITION) xdir = 1;
    if(y <= 1) ydir = 0;
    else if(y >= MAX_Y_POSITION-1) ydir = 1;
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  3. #3
    Registered User
    Join Date
    Jun 2005
    Posts
    131
    Quote Originally Posted by dwks
    Code:
    strlen(buffer)
    ->
    Code:
    #include <cstring>
    strlen(buffer)
    Do something like this:
    Code:
    static int x, y;  /* static variables are initialized to 0 by default */
    static int xdir, ydir;
    
    x += (xdir ? -1 : 1);
    y += (ydir ? -2 : 2);
    
    if(x == 0) xdir = 0;
    else if(x == MAX_X_POSITION) xdir = 1;
    if(y <= 1) ydir = 0;
    else if(y >= MAX_Y_POSITION-1) ydir = 1;
    So what I am confused about still is this......my call to drawbitmap happens everytime through my loop.....this means that the bitmap is being drawn in a new spot everytime.....This is bad correct?.....I want to store just one drawn bitmap in a variable and move that correct?......can you even do that?

  4. #4
    Registered User VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,607
    No you must redraw it every frame since it's position changes.

  5. #5
    Registered User
    Join Date
    Jun 2005
    Posts
    131
    Quote Originally Posted by Bubba
    No you must redraw it every frame since it's position changes.
    Yes I understand this....what I was saying is in my code I have my Draw() function being called everytime through the Game_Run() loop....I am guessing this is just stupid because everytime it is called it adding a new instance of the bitmap....1-1....2-2....3-3...

  6. #6
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Quote Originally Posted by chadsxe
    Yes I understand this....what I was saying is in my code I have my Draw() function being called everytime through the Game_Run() loop....I am guessing this is just stupid because everytime it is called it adding a new instance of the bitmap....1-1....2-2....3-3...
    Can you change the position of the bitmap instead of creating a new one?

    If not, then free the old ones.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Tic Tac Toe Comp Move help
    By swgh in forum C++ Programming
    Replies: 5
    Last Post: 09-24-2008, 11:05 AM
  2. Problem reading BMP
    By Mortissus in forum C Programming
    Replies: 4
    Last Post: 02-02-2006, 06:32 AM
  3. Manipulating the Windows Clipboard
    By Johno in forum Windows Programming
    Replies: 2
    Last Post: 10-01-2002, 09:37 AM
  4. OpenGL and Windows
    By sean345 in forum Game Programming
    Replies: 5
    Last Post: 06-24-2002, 10:14 PM
  5. shutting down windows
    By DavidP in forum A Brief History of Cprogramming.com
    Replies: 3
    Last Post: 01-02-2002, 12:28 PM