Jump to content
  • Advertisement
Sign in to follow this  
Arjan B

SDL Input Only

This topic is 3350 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hey all, I would like to separate handling input and rendering in my code, but I'd like to use SDL for both. I can do SDL_Init( SDL_INIT_VIDEO ), but there's nothing like SDL_Init( SDL_INIT_EVENTS ). Does anyone know of a way to initialize only the event system of SDL, so my code handling the input is not dependent on some other subsystem being initialized by other code? Or does anyone have some other suggestion on how to separate my code? Thank you in advance! Arjan

Share this post


Link to post
Share on other sites
Advertisement
Quote:
I would like to separate handling input and rendering in my code, but I'd like to use SDL for both. I can do SDL_Init( SDL_INIT_VIDEO ), but there's nothing like SDL_Init( SDL_INIT_EVENTS ).
Does anyone know of a way to initialize only the event system of SDL, so my code handling the input is not dependent on some other subsystem being initialized by other code?
Or does anyone have some other suggestion on how to separate my code?
Someone else may know differently, but it's my understanding that input is tied to the windowing system in SDL, so I don't know that you can separate input and windowing as you describe.

(This is true in SDL 1.2.x, at least. I'm assuming that it holds in SDL 1.3 as well, although I don't know this for sure.)

Share this post


Link to post
Share on other sites
Well, there is SDL_INIT_EVENTTHREAD which should work at least on anything running X11 and on windows with newer versions of libsdl. I'm in the midst of testing it myself, so I'm only aware of it's existence, but unsure if it actually does anything beneficial.

Share this post


Link to post
Share on other sites
Thanks for your replies!

I've done the following and it seems to be working (compiles without warnings and no runtime errors):

#include <SDL.h>
#include <windows.h>

inline void Message( const char* msg ) {
MessageBox( NULL, msg, "Message", MB_OK );
}

int main( int argc, char* argv[] ) {
SDL_Init( SDL_INIT_EVENTTHREAD );

Message( "About to do event stuff..." );
SDL_Event e;
SDL_PollEvent( &e );
Message( "Done with event stuff :D!" );

SDL_Quit();
return 0;
}






So I assume this is going to work ^^. But I would like it if anyone could tell me whether this has any caveats.



EDIT:


Okay, so that wasn't a safe assumption I guess. I tried compiling it with the SDL_Init() line commented out and it still gave no sort of error.



EDIT #2:


So now I tested it in a more decent way:

#include <SDL.h>
#include <windows.h>

inline void Message( const char* msg ) {
MessageBox( NULL, msg, "Message", MB_OK );
}

int main( int argc, char* argv[] ) {
SDL_Init( SDL_INIT_EVENTTHREAD );

bool done = false;
SDL_Event e;
while( !done ) {
while( SDL_PollEvent( &e ) ) {
if( e.type == SDL_KEYDOWN ) {
Message( "You pressed a key!" );
if( e.key.keysym.sym == SDLK_ESCAPE ) done = true;
}
}
}

SDL_Quit();
return 0;
}



But since there is no window popping up, I'm guessing the process doesn't get focus, which would be the reason (/reason that?) there is no response to my happy key pressing.

I think I'll drop the idea of separating input and visuals this way. So I'm still open to any suggestions of some other way ^^.

[Edited by - Arjan B on October 12, 2009 1:06:56 PM]

Share this post


Link to post
Share on other sites
It seems you are not initializing the graphics at all. My understanding is that sdl doesn't receive events if it doesn't have graphics initialized and a window created.

Here's a snippet of my code: (I might be initializing video a bit prematurely, but as I said, I'm still just doing some testing)

int main (int argc, char *argv[])
{
/* Initializes Audio and Video. */
if (SDL_Init(SDL_INIT_VIDEO |SDL_INIT_AUDIO | SDL_INIT_EVENTTHREAD)< 0)
{
std::cerr << "Could not initialize SDL:" << SDL_GetError() << std::endl;
SDL_Quit();

return 0;
}


theMain->start();
renderer.start(NULL);

SDL_Event event;
while(theMain->isRunning())
{
while(SDL_PollEvent(&event))
{
if(event.type == SDL_QUIT)
{
quit();
}
SDL_Delay(100);
}
}
return(0);
}



renderer (of class Renderer) is a subclass of Thread and renderer.start() starts the renderer in a new thread(SDL_CreateThread), so the graphics are handled in a separate thread.

With this code polling for SDL_QUIT works. Still have to test the polling of actual input.

Share this post


Link to post
Share on other sites
Maybe this helps:

Quote:

If you need an event queue but don't want a real window, try putenv("SDL_VIDEODRIVER=dummy") before you call SDL_SetVideoMode. I find this useful in apps that use SDL facilities, but don't need a real video output device. Oh, and apparently you can use a 0x0 resolution to avoid to much memory from making the dummy window. Of course, the dummy driver can also be used for offscreen rendering like for an image converter and such.


From here.

Share this post


Link to post
Share on other sites
Well I did want to create a window, but I wanted the class to handle all the visual stuff to be completely independent of the class to handle all the events/input and vice versa. I guess this simply isn't possible with SDL ^^.
I think I will just make a wrapper around SDL and not worry too much about separating.

Thank you all for your replies!

Share this post


Link to post
Share on other sites
Think about why the input events are linked to the output window in SDL - if there is no window with input focus where do your input events come from?

That being said your games input handler will always be linked in some way to the "main" or "parent" window so order of operations is key here. In every GUI system I have ever used input events are received first by the window with focus. Window can mean button, toolbar, frame etc. since they are all parent child related. In the end how else would you want input work?

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!