GTK+ Forums

Discussion forum for GTK+ and Programming. Ask questions, troubleshoot problems, view and post example code, or express your opinions.
It is currently Thu Nov 27, 2014 12:09 am

All times are UTC




Post new topic Reply to topic  [ 5 posts ] 

Is GTK+ suitable for game programming in C?
Sure 25%  25%  [ 1 ]
May be 50%  50%  [ 2 ]
No 25%  25%  [ 1 ]
No, use Qt 0%  0%  [ 0 ]
No, use SDL 0%  0%  [ 0 ]
No, use OpenGL 0%  0%  [ 0 ]
Total votes : 4
Author Message
 Post subject: [Space+Up] keys blocking [Left] key
PostPosted: Fri Mar 09, 2012 3:55 am 
Offline
Familiar Face

Joined: Fri Mar 09, 2012 3:08 am
Posts: 5
I'm writing a game in C with GTK+ 2.0. It creates a full-screen window with a drawing area and uses it.
The game logics runs in a timeout callback and needs to know if a key is pressed at the moment or not.
5 keys are being tracked: [Up]. [Right], [Down], [Left] and [Space].
To do this, I have an array of 5 gboolean values.
Code:
gboolean keys[5];
I also connect a callback function that saves the current key (up/down) state:
Code:
guint on_key_press(GtkWidget *widget,
    GdkEventKey *event, Data *data) {
  // ...
  data->keys[key_index] = event->type == 8;
  // ...
  return FALSE;
}

// ...
gtk_key_snooper_install(on_key_press, (gpointer)data);
This basically assigns TRUE or FALSE to the [0 .. 4] elements of the array.
Everything is working, except one thing:
If in the game you hold [Up] and [Space] in the same time and then press [Left]. (i.e. three keys simultaniously), the application will not notice anything.
If you do the same but press the [Right] key, it will work with no problem.
It works perfectly if I change [Space] to [A] key.

It looks like [Space] key is blocking [Left] key if [Up] is also pressed.
I first thought it is somehow connected to the fact that if a key is held down, key press events keep coming repeatedly. (like when you hold "a" you get "aaaaaaaaa"), but that doesn't make sense, and it is not possible in GTK+ to disable repeating keys in a cross-platform way.

I've also tried:
Code:
g_signal_connect(G_OBJECT(window), "key_press_event", ... );
instead of:
Code:
gtk_key_snooper_install(on_key_press, ... );

but it doesn't make any difference.

I'm using Debian.

P.S.: While I was writing this message, I noticed that the same is happening with the [G] key. [G] is blocking [Left] in the same way, and doesn't affect the [Right] key.
In the same time [C] works perfectly (like [A]). May be these are some special hotkeys?

P.S.#2: Now I noticed that if you hold down [T+G], keys [Up] and [Right] are blocked, whereas [Left] and [Down] work! What is that???


Attachments:
File comment: Contains the C source file and 3 PNG images.
Compile with gtk+-2.0 support.

game.tar.bz2 [14.08 KiB]
Downloaded 91 times
Top
 Profile  
 
 Post subject: Re: [Space+Up] keys blocking [Left] key
PostPosted: Fri Mar 09, 2012 7:34 am 
Offline
Never Seen the Sunlight

Joined: Mon Apr 28, 2008 5:52 am
Posts: 780
Location: UK
Hi,

Have you tried this with other keyboards. What I think you have come across is a limitation in the hardware of the keyboard itself. The keyboard is normally made up of an X-Y matrix and not individual switches which is the scanned for key presses or releases. This matrix may mask some keys if others are already pressed. Also the processor in the keyboard may limit the number of keys it can keep track of to 3. But some times reserving one of those for processing the meta keys (shift L, shift R, Alt etc...)

I would advise against asking for the user to hold down more than two keys at a time, as keyboards can vary.

As a note your application worked on my system Debian Wheezy(testing) using GTK+ 2.24. I am using a logitech USB keyboard.

_________________
E.


Top
 Profile  
 
 Post subject: Re: [Space+Up] keys blocking [Left] key
PostPosted: Sat Mar 10, 2012 2:02 pm 
Offline
Familiar Face

Joined: Fri Mar 09, 2012 3:08 am
Posts: 5
Yes, seems like it is because of the keyboard. I tried it on two other notebooks and it worked the same.
I just changed the "Fire" key from [Space] to [Ctrl].

I've attached a newer version of the game if anyone wants to see.
Do you have an idea where the gray border around the screen comes from?


Attachments:
File comment: Newer version of the game, with the "Fire" key changed to [Ctrl] and some graphics changed
game.tar.bz2 [131.88 KiB]
Downloaded 78 times
Top
 Profile  
 
 Post subject: Re: [Space+Up] keys blocking [Left] key
PostPosted: Sat Mar 24, 2012 9:37 am 
Offline

Joined: Sat Mar 24, 2012 9:23 am
Posts: 2
Hi,

I'm using Gtk# to do game programming and I have similar issue.

IMHO, event model used in GTK is not suitable for game programming. Users would like to hold the "FIre" key. Therefore, the related event handler is continually called and any other things are blocked.

The alternative way is to check the keyboard regularly, say 30 times a second. This is sufficient since humans actually cannot press (down and up) keyboard very fast.

GetAsyncKeyState()
http://msdn.microsoft.com/en-us/library ... 46293.aspx
SDL_GetKeyState()
http://www.libsdl.org/cgi/docwiki.cgi/SDL_GetKeyState

These two functions are suitable for regularly checking. I'd like to know whether there is any similar function in GLib/GDK/GTK.


Last edited by damage3025 on Sat Mar 24, 2012 12:28 pm, edited 1 time in total.

Top
 Profile  
 
 Post subject: Re: [Space+Up] keys blocking [Left] key
PostPosted: Sat Mar 24, 2012 12:27 pm 
Offline

Joined: Sat Mar 24, 2012 9:23 am
Posts: 2
It seems that SDL_GetKeyState() is not a cross platform version of GetAsyncKeyState() .

The source of SDL_GetKeyState() in SDL-1.2.15
Code:
Uint8 * SDL_GetKeyState (int *numkeys)
{
   if ( numkeys != (int *)0 )
      *numkeys = SDLK_LAST;
   return(SDL_KeyState);
}

As you can see, no active update at all. So we still needs to deal with SDL's event loop, which causes "double event loop".
http://sdl.beuc.net/sdl.wiki/FAQ_GUI


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 5 posts ] 

All times are UTC


Who is online

Users browsing this forum: Google [Bot], Yahoo [Bot] and 3 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group