Archived

This topic is now archived and is closed to further replies.

ChocaPaluza123

EVENT HANDLING IN PONG

Recommended Posts

hey all, im making a game (pong) and i am having trouble moving the paddle. if i press the up arrow, nothing happens. I have to press the up arrow or down arrow, and it won''t move unless i minimize it and open it again. What im saying is that you can see the result of the paddle moving only if you press the up or down arrow, minimize it and open it back up again. only then you can see the result. its kinda weird (i tested it with MessageBoxes, and it worked perfectly, but this doesn''t). If you know whats wrong, please post.

Share this post


Link to post
Share on other sites
well, unless you''re using vb, I would use a state driven machine, not an even driven one. this makes things more strait forward. if the key is pressed set a flag (a variable) to represent that the key has been pressed. then later before you render check what the variable is set to and move stuff accordingly.

HHSDrum@yahoo.com
Polarisoft Home Page

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
It sounds like you are using VB. What you should do is this:

1/ Put any code which draws your paddles in the Form_Paint event
2/ When a key is pressed, you update the paddle position and call the Form_Paint event to repaint the paddles in the new position.

The reason it worked when you minimized and restored your game before is because this calls the Form_Paint event for you.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
no, im not using vb, but Anonymous Poster: I think thats why its not happenin. it doesn''t update it until it know it needs painting and so you gotta tell it to repaint itself. but how do you call the WM_PAINT thing?
I tried to do sometin like this in the message loop thing:
if (msg.message == WM_KEYDOWN)
{
msg.message = WM_KEYDOWN;
msg.message = WM_PAINT;
}
but it still doesn''t work! Any ideas?

Share this post


Link to post
Share on other sites
You need to force a WM_PAINT message by
using the function SendMessage(hWnd,WM_PAINT,0,0)
I may have missed some stuff somewhere...
but hey.. it''s just an example

Start with Win32..until you feel confortable with it and then enter the realm of directX or look at the new GDI+ API Microsoft is coming out with. GDI+ is an integration of DirectX and GDI oooooo joy =\

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
I guess it still doesn''t work without me having to minimize it and opening it back up again. Here''s the code:

case WM_PAINT:
{
// simply validate the window
hdc = BeginPaint(hwnd,&ps);

FillRect(hdc,&rect,brush);

// end painting
EndPaint(hwnd,&ps);

// return success
return(0);
} break;

case WM_KEYDOWN:
{
int virtual_code = (int)wparam;
int key_bits = (int)lparam;

switch(virtual_code)
{
case VK_UP:
{
rect.top -= 10;
rect.bottom -= 10;
SendMessage(main_window_handle,WM_PAINT,wparam,lparam);
} break;
case VK_DOWN:
{
rect.top += 10;
rect.bottom += 10;
SendMessage(main_window_handle,WM_PAINT,wparam,lparam);
} break;
}

return (0);
} break;

Share this post


Link to post
Share on other sites
The problem is that the update region for the window is empty, and a DC retreived with BeginPaint can only be drawn on in the update region.

What I do is retrieve a permanent DC for the window after it''s created. I keep this DC as a global variable.

HDC DC;

DC = GetDC(Window);

Now, instead of using BeginPaint and whatnot, you can just draw on the permanent DC. In response to a WM_PAINT, you only need to validate your window (I think it''s ValidateRect(Window, NULL); ) - if you don''t validate it, the WM_PAINT will just stay in the message queue.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
I don''t really get what you are saying about DC''s, but here''s what I changed in my code:

case WM_PAINT:
{
// simply validate the window
hdc = BeginPaint(main_window_handle,&ps);
ValidateRect(main_window_handle,NULL);
FillRect(hdc,&rect,brush);

// end painting
EndPaint(main_window_handle,&ps);

// return success
return(0);
} break;

Share this post


Link to post
Share on other sites