• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.
Sign in to follow this  
Followers 0
fir

can I go without events?

10 posts in this topic

i wonder if in winapi win32 game programming I can go without events

for keyboard and for mouse - If i would like to ask for keyboard and mouse state in each frame and totally ignore events?

 

- are there easy ways to read mouse position in other way than by event?

- are some things worse when i would use each frame checking than events servicing?

 

0

Share this post


Link to post
Share on other sites

In Khan Academy's Javascript programs there are native "mouseX" and "mouseY" variables that, when used, already give you the actual mouse position. I don't know the API you've mentioned, but It's not common to have these variables by default. Events are the main way to deal with it. Nevertheless, take a good look at the API and search for it.

Edited by EricsonWillians
0

Share this post


Link to post
Share on other sites
I use GetAsyncKeyState on my update loop directly. If you implement this with events you normally end up just reimplementing an array of bools for each key state anyway so seems pointless.

The problems arise when it comes to abstracting input away from being directly tied to a given device. Event based approach can be easier to decouple.
0

Share this post


Link to post
Share on other sites

You will at least have to handle the events for the window, or you will be unable to move or close it.

 

Other than that, you can do completely without events, but it is a really, really, really bad idea. User uses a different keyboard (different, as in language) and you're screwed. User clicks very fast, and you miss the click. User does <insert one of 1000 other things> and you're screwed.

 

Events work, and they work well. Plus, they work with different languages, with different devices, mappings, resolutions, different anything. And they don't burn CPU time polling.

 

One good example where polling keyboard state results in a total fuckup is when e.g. US American programmers assume that "/" is a proper key and uses it for some interactive control, and your end user has a German keyboard. With events, it is a nuisance since you must hold down shift before pressing the key (holding it works, at least), but if the program samples the keyboard state, the program is totally unusable (there simply isn't a way to press any combination of keys that works). Similar for []{}\.

Edited by samoth
1

Share this post


Link to post
Share on other sites

It is certainly much easier to handle input with events. Events are built into the library and will make your programming journey slightly easier. The only thing you need to handle yourself is how to apply the correct logic given the built-in event functionality.

Edited by warnexus
0

Share this post


Link to post
Share on other sites

You will at least have to handle the events for the window, or you will be unable to move or close it.

 

Other than that, you can do completely without events, but it is a really, really, really bad idea. User uses a different keyboard (different, as in language) and you're screwed. User clicks very fast, and you miss the click. User does <insert one of 1000 other things> and you're screwed.

 

Events work, and they work well. Plus, they work with different languages, with different devices, mappings, resolutions, different anything. And they don't burn CPU time polling.

 

especially interesting is this 'user clicks to fast' - keyboard and mouse, Do mouse programmed by polling can fail in some cases? Or in general it would work ok?

0

Share this post


Link to post
Share on other sites

Do mouse programmed by polling can fail in some cases? Or in general it would work ok?
If you have a reasonable frame rate, it will in general (most of the time) work. But what if your frame rate isn't so great or you have a bit of lag for some reason? If the state that you are looking for (say, button down) isn't there the very moment you look, you will miss it.

 

The nature of polling is "right now, check what the state is" whereas the nature of events is "whenever user clicks, you get exactly 1 event, and you can consume it any time you want". With polling, it is in principle always possible to miss something. With events, it just isn't possible (unless the operating system is FUBAR).

0

Share this post


Link to post
Share on other sites

 

Do mouse programmed by polling can fail in some cases? Or in general it would work ok?
If you have a reasonable frame rate, it will in general (most of the time) work. But what if your frame rate isn't so great or you have a bit of lag for some reason? If the state that you are looking for (say, button down) isn't there the very moment you look, you will miss it.

 

The nature of polling is "right now, check what the state is" whereas the nature of events is "whenever user clicks, you get exactly 1 event, and you can consume it any time you want". With polling, it is in principle always possible to miss something. With events, it just isn't possible (unless the operating system is FUBAR).

 

 

so say i got a 5 seconds lag in my app and user types the tekst

(say 30 keystrokes) will it be queued in windows and then flushed as a set of 30 keydowns ?

 

it was always unclear to me 

0

Share this post


Link to post
Share on other sites

 


i wonder if in winapi win32 game programming I can go without events

May I ask: Why would you do that?

 

Im not sure if i would or not, just wonder - in some cases polling

could be easier than event servicing (when you poll all you polling routines are independant each another, when you service an event you must wrote some call trees or lists to service them)

0

Share this post


Link to post
Share on other sites

The problem with polling is you can miss something.  If the user presses and releases a key before you get around to polling again, you will completely miss it.  Mouse clicks are fast, and if your frame rate stalls for whatever reason (not even your program's fault; maybe the dreaded McAfee just started to do an update in the background...) and you're out of luck.

 

I get that you want to just poll to check if a key is pressed in your code.  I like to do that too.  But it's easy to do with events:  (Rough pseudocode; haven't done win32 events in a while:

bool keystate[MAX_KEY];
bool mousebutton[MAX_MOUSEBUTTON];
int  mousex, mousey;

#define MOUSE_LEFT 0
#define MOUSE_RIGHT 1


eventhandler(...) 
{


  while (event = getnextevent(...)) 
  {

    if(event==KEY_DOWN && keycode < MAX_KEY)
        keystate[keycode] = 1;

    if(event==KEY_UP && keycode < MAX_KEY)
        keystate[keycode] = 0;

    if (event==L_MOUSE_DOWN)
         mousebutton[MOUSE_LEFT] = 1;
    
    if (event==L_MOUSE_UP)
         mousebutton[MOUSE_LEFT] = 0;

    if (event==MOUSE_MOVE)
    {  
        mousex = event_mouse_x;
        mousey = event_mouse_y;
    }
  }
}

Then, in your code you can do  if(keystate[KEY_L_CTRL]) all you want.  Or sprite.draw(mousex, mousey).

 

The above could still suffer from missing presses.  If the system slows down, and you call the event loop once per frame, if a keydown AND a keyup press bunch up against each other, then in one frame's processing of events you would set, and unset a single key in the same call to the loop.  One way to do that is handle the player's inputs in the event handler itself:

   if(event==KEY_DOWN && keycode < MAX_KEY)
   {
        keystate[keycode] = 1;
  
        if (keycode == playerconfig.firekey)
            player.fire=1;

        if (keycode == playerconfig.jump)
            player.jump= 1;
   }

This way the event loop is looking for particular events, and sets those flags in the player object.  Each frame, after the event handler is run, when the player's control code is run, it checks it's action flags and does the appropriate action.  This way you can't miss a jump or fire event, even if the next event is the queue is releasing the button.

 

You can abstract that completely and do this:


while (event = getnextevent(...))
{
    memset(keypressed, 0, sizeof(keypressed)); //clear pressed events

    if(event==KEY_DOWN && keycode < MAX_KEY)
    {
        keystate[keycode] = 1;
        keypressed[keycode] = 1;
    }

    if(event==KEY_UP && keycode < MAX_KEY)
        keystate[keycode] = 0;

}

Now, you can't miss an event.  If the key was pressed at all since the last frame, keypressed will be '1'.  And if its still pressed keystate will be '1'.  So now anywhere in your game loop you can do this:

if (keypressed[ player_config.fire_key] || ( keystate[player_config.fire_key] && (this_frame - last_fire_frame >= 20)) 
{
    last_fire_frame = this_frame;
    fire_a_bullet();
}

What this does is fire a bullet every time the player presses the fire key OR if the player holds down the fire key, one bullet every 20 frames.

 

Also this is all assume a single main loop thread, that looks something like this:

while(not_quitting)
{
   handle_window_events();
   increment_game_logic();
   draw();
}
1

Share this post


Link to post
Share on other sites




so say i got a 5 seconds lag in my app and user types the tekst

(say 30 keystrokes) will it be queued in windows and then flushed as a set of 30 keydowns ?
Yes, even a bit more than that. You will get roughly 30 key down and key up events, plus roughly 30 char events.

 

By counting down and up events, you know which keys are still down (for things like game controls). By handling character events, you know what the user has typed (in terms of "text").

 

I used the wording "roughly" because one key press does not necessarily correspond to a virtual key (usually it does, but the weird AltGr key to the right of the space key for example sends two key-downs and two key-ups for every press). Further, keys and characters are different things. Several keys pressed can result in a single character, for example Shift+a will result in A, and ´+e will result in é whereas Shift+´+e will result in è, but Shift+´+Shift+e will give È.

 

This is extremely complicated to a point where it gets annoying and unmanageable (especially in some non-roman languages where ligatures are mandatory and completely different glyphs). The nice thing is, you don't need to care because what comes out on the WM_CHAR end is already correct, human-readable characters in whatever language the user has configured (which you maybe don't even can't pronounce).

1

Share this post


Link to post
Share on other sites

Using WM_CHAR will save you a lot of trouble.  If you are going to interpret the keystrokes as text (player is entering their name), you should use WM_CHAR.  For player actions, use WM_KEYDOWN/UP.  Pressing, holding and releasing a key will generate many events.  If you press and hold shift-A you will get this:

 

shift keydown

a keydown

capital A char (repeated several times)

capital A char (repeated several times)

capital A char (repeated several times)

a key up

shift key up

0

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0