🎉 Celebrating 25 Years of GameDev.net! 🎉

Not many can claim 25 years on the Internet! Join us in celebrating this milestone. Learn more about our history, and thank you for being a part of our community!

EVENT HANDLING IN PONG

Started by
7 comments, last by ChocaPaluza123 22 years, 11 months ago
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.
C++
Advertisement
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
My HomepageSome shoot to kill, others shoot to mame. I say clear the chamber and let the lord decide. - Reno 911
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.
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?
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 =\

The nightmare travels across the cosmos with his burning mane. The trail of ash that is produced.

?Have a nice day!?

Or just call Invalidate(FALSE);
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,▭,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;
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.
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,▭,brush);

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

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

This topic is closed to new replies.

Advertisement