Jump to content
  • Advertisement
Sign in to follow this  
PGSCreativeDirector

Problem with events in SDL :(

This topic is 2584 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

[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

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites

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

Can you draw a state transition diagram?

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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.

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!