Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 SDL KEYUP event
Index -> Programming, C++ -> C++ Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
Insectoid




PostPosted: Mon Jun 20, 2011 8:33 pm   Post subject: SDL KEYUP event

I've been messing around in SDL to get familiar with it (and also to get better at C++). Anyway, the issue I'm having is, the SDL_KEYUP event does not seem to work right. Or maybe I don't understand how event queues are handled. Either way, something's up.

c++:
        while (!quit){
                while (SDL_PollEvent (&eventQueue)) {
                        if (eventQueue.type == SDL_QUIT) quit = true;
                        else if (eventQueue.type = SDL_KEYDOWN){
                                switch (eventQueue.key.keysym.sym){
                                        case SDLK_UP: p1.setDirectionUp();break;
                                        case SDLK_DOWN: p1.setDirectionDown();break;
                                        case SDLK_ESCAPE: quit = true;break;
                                }
                        }
                        else if (eventQueue.type = SDL_KEYUP){
                                switch (eventQueue.key.keysym.sym){
                                        case SDLK_UP: p1.setDirectionStop();break;
                                        case SDLK_DOWN: p1.setDirectionStop();break;
                                }
                        }
                }
//There's more code here, but I left it out 'cause I don't think it's the issue



The functions setDirectionUp(), setDirectionDown(), and setDirectionStop() all work perfectly (I've tested them rigorously). The SDL_KEYDOWN case works perfectly fine. I press 'up', my character moves up. If I press 'down', the character moves down. Note that these functions just change a direction flag. There is a move() function that actually moves the character. The SDL_KEYUP case calls SetDirectionStop() which sets the direction flag to 0 (ie no movement). This is not happening.

When I remove the SDL_KEYDOWN case completely (comment that section out), SDL_KEYUP works sporadically. The event seems to trigger whenever there is a KEYDOWN, OR a KEYUP event. So, KEYUP *sort of* works. I think part of the issue is that maybe checking for SDL_KEYDOWN is bumping SDL_KEYUP off the event queue?

I admit, I have very little understanding of how events work, and my implementation may well be the issue.

~Insectoid
Sponsor
Sponsor
Sponsor
sponsor
DemonWasp




PostPosted: Tue Jun 21, 2011 11:03 am   Post subject: RE:SDL KEYUP event

c++:
eventQueue.type = SDL_KEYDOWN


Look very closely at that condition and tell me what you see.
Insectoid




PostPosted: Tue Jun 21, 2011 11:08 am   Post subject: RE:SDL KEYUP event

Ah, haha, my bad. Major logical flaws I usually catch myself, but tiny errors like these slip past me all too often.
DemonWasp




PostPosted: Tue Jun 21, 2011 11:18 am   Post subject: RE:SDL KEYUP event

Depending on which compiler you're using, there's usually a setting to turn on errors for things like this. For gcc or g++ it's "-Wall".

I strongly recommend using "-Wall -Werror -Wextra", and I like to use "-pedantic" whenever I write C/C++/uC++. These will help you find minor syntax slip-ups before they get compiled and produce odd logic errors. If you're working with templates, it can be helpful to use "-Wfatal-errors", so that the very first problem halts compilation, preventing one error from visually merging with the next.

The price you pay for these settings is correcting your programs up-front, at compile time. The price you pay for NOT using them is difficult-to-debug logic errors at run-time.
Insectoid




PostPosted: Tue Jun 21, 2011 12:06 pm   Post subject: RE:SDL KEYUP event

Hmm, I'm using Xcode on OS X at the moment, since I've not figured out how to link SDL manually, but I'm sure there's an option somewhere to turn on those flags (Xcode compiles with gcc I believe).
DemonWasp




PostPosted: Tue Jun 21, 2011 12:33 pm   Post subject: RE:SDL KEYUP event

http://developer.apple.com/tools/xcode/xcodebuildsettings.html
Insectoid




PostPosted: Tue Jun 21, 2011 12:57 pm   Post subject: RE:SDL KEYUP event

Oh, thanks! Spares me some time on Google (and Apple's awful (imo) website).
Display posts from previous:   
   Index -> Programming, C++ -> C++ Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 7 Posts ]
Jump to:   


Style:  
Search: