Thread: Need some help with this code because I can't see what's wrong with it

  1. #1
    Registered User Finchie_88's Avatar
    Join Date
    Aug 2004
    Posts
    154

    Question Need some help with this code because I can't see what's wrong with it

    Here is some code that I've written, the idea is that i'm going to make a simple 2 player pong game. The problem is that the circle isn't drawn on the screen and it doesn't move. Thanks in advance...
    Code:
    while (GetMessage (&messages, NULL, 0, 0))
        {
            /* Translate virtual-key messages into character messages */
            TranslateMessage(&messages);
            /* Send message to WindowProcedure */
            DispatchMessage(&messages);
        }
    
        // MAIN PROGRAMMING ////////////////////////////////////////////////////////
    
        hdc = GetDC(hwnd);
        
        rect.left  = ball_x - BALL_RADIUS;
        rect.right = ball_x + BALL_RADIUS;
        rect.top   = ball_y - BALL_RADIUS;
        rect.bottom= ball_y + BALL_RADIUS;
            // erase the ball /////////////////////////////////////
        SelectObject(hdc, GetStockObject(BLACK_BRUSH));
        SelectObject(hdc, black_pen);
        Ellipse(hdc, rect.left, rect.top, rect.right, rect.bottom);
    
        // move the ball //////////////////////////////////////
        ball_x += x;
        ball_y += y;
                        // fill in rect structure with new position
        rect.left  = ball_x - BALL_RADIUS;
        rect.right = ball_x + BALL_RADIUS;
        rect.top   = ball_y - BALL_RADIUS;
        rect.bottom= ball_y + BALL_RADIUS;
            Ellipse(hdc, rect.left, rect.top, rect.right, rect.bottom);
        
        // draw the ball
        SelectObject(hdc, red_pen);
        Ellipse(hdc, rect.left, rect.top, rect.right, rect.bottom);
        
        // TEST TO SEE IF AT THE EDGE OF THE WINDOW ////
        if(x == a||b){
            x = -x;
        }
        else if(x == a||b){
            y = -y;
        }        
        
        // release the device context
        ReleaseDC(hwnd,hdc);
                Sleep(40);
    
        /* The program return-value is 0 - The value that PostQuitMessage() gave */
        return messages.wParam;
    }


  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    1. your while loop loops until the program exits - by then it's all too late.
    Study how normal windows programs are structured.

    2. if(x == a||b){
    This isn't what you meant, I'm pretty sure.
    Perhaps if ( x == a || x == b )

  3. #3
    Registered User
    Join Date
    Nov 2004
    Posts
    17
    yes, if you just expanded the while block to include the graphics code then at least something would happen. However I would also suggest a study of windows main loops. The structure you have will not be set up for a nicely timed drawing scheme since it will be strictly controlled by the message pump there. You could dig into the ON_PAINT message perhaps...

    here's a few out of context mainloops though:

    Code:
    	//do this for a while
    	while(GetMessage(&msg, NULL, 0, 0))
    	{
    		if(!TranslateAccelerator(hwndApp, hacc, &msg))
    		{
    			TranslateMessage(&msg);
    			DispatchMessage(&msg);
    		}
    
    		PlaySound(snd, hinst, SND_ASYNC | SND_NODEFAULT | SND_RESOURCE);
    	}
    or...

    Code:
    while(1)
    {
    	startime = GetTickCount();
    
    	if (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
    	{ 
    		TranslateMessage(&msg);
    		DispatchMessage(&msg);
    	}
    	else
    	{
    		// do something here
    		anothertime = GetTickCount();
    	}
    }
    and..

    Code:
    while(1)
    {
    	if (PeekMessage(&msg, NULL, 0, 0, PM_NOREMOVE))
    	{
    		if (!GetMessage(&msg, NULL, 0, 0))
    			return msg.wParam;
    		TranslateMessage(&msg);
    		DispatchMessage(&msg);
    	}
    	else if(!paused)
    	{
    		//go to town
    	}
    	else WaitMessage();
    }
    So if you did an index search in the MS SDK for 'WaitMessage', for example, then you would be all around that mainloop functionality. Also, if I'm not mistaken there is some good example code for some GDI bouncing ball thing as well.

    keep me posted on your progress I'd be interested to see this.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. what is wrong in this simple code
    By vikingcarioca in forum C Programming
    Replies: 4
    Last Post: 04-23-2009, 07:10 AM
  2. what is wrong with this code please
    By korbitz in forum Windows Programming
    Replies: 3
    Last Post: 03-05-2004, 10:11 AM
  3. I cant find what is wrong with this code
    By senegene in forum C Programming
    Replies: 1
    Last Post: 11-12-2002, 06:32 PM
  4. Anyone see what is wrong with this code?
    By Wise1 in forum C Programming
    Replies: 2
    Last Post: 02-13-2002, 02:01 PM
  5. very simple code, please check to see whats wrong
    By Unregistered in forum C Programming
    Replies: 3
    Last Post: 10-10-2001, 12:51 AM