Problem with events in SDL :(

Started by
20 comments, last by Bregma 12 years, 5 months ago
[font="Arial"]Hello using Simple direct media layer im creating a event like this



SDL_Event event1;
while(SDL_PollEvent(&event))
{

switch(event.type)

{

case SDL_MOUSEBUTTONDOWN:

int mouse_x, mouse_y;

SDL_GetMouseState(&mouse_x, &mouse_y);

if ((mouse_x <= 520) && (mouse_y <= 80))

{

SDL_Event event2;

while(SDL_WaitEvent(&event2))
{
switch(event2.type)
{

case SDL_MOUSEBUTTONDOWN:

int mouse_x2, mouse_y2;


SDL_GetMouseState(&mouse_x2, &mouse_y2);
if ((mouse_x2 <= 560) && (mouse_y2 <= 80))
{

dothis//

}


}


SDL_Event event3;
while(SDL_WaitEvent(&event3))
{
switch(event3.type)
{


case SDL_MOUSEBUTTONUP:



int mouse_x3, mouse_y3;

SDL_GetMouseState(&mouse_x3, &mouse_y3);

if ((mouse_x3 <= 600) && (mouse_y3 <= 80))
{

//do that

}


}
}
break; }





The problem is that it won't do the code when the application is executed[/font][font="Arial"] which would be like this eg.( when I click on a button then after that I click on another button it should act upon that but it does not). [/font]


It only does the first one because of the SDL_WaitEvent but i want it to wait for an event even after its found one the first time
[font="Arial"]
how do i do this? [/font]biggrin.gif
Advertisement
Now I'm not exactly sure what SDL_WaitEvent does... But why do you have more than one instance of SDL_Event?

Isn't it better to get the mouse coordinates from event.motion.* ?

I also don't see where you check what button is pressed, for example: SDL_BUTTON_LEFT.
because SDL_WaitEvent is weird and won't do what i ask so i assumed if i made another event directly it would get rid of the issue apart from that i have no idea lol

Even if I take it away it and just do one event it won't allow me to do cases without switching event types and won't change anything really.


bloody weird if you ask me
Can you please describe your problem again?
I still don't get what you're trying to do...
I want to do an wait event inside an event and then do another wait event inside the same event.

Here is a simplified version of what i just said


Say i have a spoon with food i want to wait to eat the food with the spoon and then wait to the eat food again.





If you don't understand that i don't know what else to say.
Why don't you have a class "Mouse" with some variables like x, y, left, right, left released, right released... And then some functions bool pressed(), bool released(), void update()...
The update function can go through pressed and released and check for the variables' values. You can also do x = event.motion.x and y = event.motion.y in the update function.
It makes it much easier to check what the mouse is up to.
(oh, if you do that, remember to set left_released and right_released to false in the end of a frame)

I also think you're approaching the solution in the wrong way. As what you've done seems a bit weird...
But I'm still confused about what you're trying to do so...

Anyway, I've tried to help you as good as I can, seems like I'm useless in this case.
SDL_PollEvent
SDL_WaitEvent

From the links:

PollEvent checks to see if there is an event waiting in the queue, and pops one event if there is. Otherwise, it does nothing.

WaitEvent will wait (block) until there is an event ready, then will return.

What you are doing here is just... just... wrong. This is not at all the way it is supposed to be done.

First, in a game you don't want to block. Ever. There is just too much other stuff you have to do (updating animations, etc....) that if you waste a whole bunch of time waiting for an event to happen, that other stuff won't get done. (Unless you do the busy-wait in a separate thread but from what I have seen, in no way are you ready to attempt that. So don't.)

Typically, what you want to use is PollEvent, then. That's it. Check to see if an event is waiting, handle it, then carry on with the loop.

Now, if you do it correctly, then by default you will be able to easily handle your stated test case of , ie handling any and all consecutive button clicks.

When the application is launched, SDL sets up a queue of events that is filled by the operating system infrastructure as operating system events happen. These events can be any number of things: mouse movements, window resizes, keystrokes, etc... The system will generate quite a lot of them. So what kind of events are you going to be looking at, in the case of subsequent button presses?

Well, you'll get MouseButtonDown events when a button is pressed. You'll get MouseButtonUp when it's released. If the mouse moves in between subsequent clicks, you'll also get a whole bunch of MouseMotion events. So in you your example, your first PollEvent call will catch a button click if it occurs before the call. Then WaitEvent will happen, at which point it will either catch the MouseButtonReleased event after the click, or it'll catch one of a long series of MouseMotion events if the mouse is moved. Or it might catch a keystroke, or a system event, or a user event, or any of a long list of events. (See here). It might even be another MouseButtonDown event, but it won't be one for the same button, since that was the event that was caught by the previous PollEvent call. Then once an event happens (an remember that since we were waiting, nothing else has happened during this time. No drawing, no enemy movement, no logic, just busy-waiting for that damned mouse event) you move on to yet another busy wait. This one might catch another MouseButtonDown event, it might catch another movement, another keystroke, whatever. Maybe it'll be what you want it to be, but probably it won't. After that event is caught, then you carry on with whatever you were doing.

There is just no reason to do it like this, none at all.


while (gameLoopIsRunning == true)
{
SDL_Event event;
if(SDL_PollEvent(&event))
{
// An event happened, do something with it
switch(event.type)
{
case SDL_MOUSEBUTTONDOWN: handleButtonEvent(); break;
// and so on
}
}

doGameLogic();
render();
}


This builds the kernel of your loop. If you find yourself straying from that basic pattern, you might be making an error.

If you don't understand that i don't know what else to say.

Can you draw a state transition diagram?

Stephen M. Webb
Professional Free Software Developer

So when i do SDL_PollEvent which i tried it will glitch my application and do weird things i have no idea why. don't think i havn't tried other solutions infact I spent the hole day yesterday tweaking and playing about


and of course what do you know nothing worked.


im going to try it again tho
PollEvent isn't glitchy, I assure you. SDL is stable and mature, and PollEvent is such a core library function that I promise you, any possible sources of glitchiness are ironed out. If there is a glitch, rest assured it comes as a result of your likely-incorrect assumptions about how things work. If you can attempt to explain more clearly what you are doing, maybe we can attempt to explain why you are incorrect.

This topic is closed to new replies.

Advertisement