Computer Science Canada event driven programming in C++ |
Author: | Anon [ Sat Mar 24, 2007 10:20 pm ] | ||||
Post subject: | event driven programming in C++ | ||||
I was just wondering what the best way was to implement event driven programming in C++ with the SDL. Right now I'm using a while loop that continously checks a boolean flag, and that flag is set when the user X's out the window, signalling the program to terminate. However, in Java, all I had to do was to implement a listener and write the event handler. Is there anything like that in C++/SDL? If not, is there a better way to handle events then what I'm doing now? heres a snippet from my code that might better show what I mean. This is what I'm doing right now:
This is the sort of thing I'd do in Java(note: I believe the example code is deprecated, but I just want to show how I could streamline detection/handling of events in Java):
|
Author: | abcdefghijklmnopqrstuvwxy [ Sat Mar 24, 2007 10:47 pm ] |
Post subject: | RE:event driven programming in C++ |
I too am using c++ with sdl and was wondering if the top example you posted is actually an example of "event driven programming". Cause all it is is a big while loop with a bunch of conditional statements - seems kind of blande to me. About your java code, if all you want is a function handEvents() than why don't you just write a c++ function called handle events and sub it in your while loop. |
Author: | Anon [ Sat Mar 24, 2007 10:58 pm ] |
Post subject: | RE:event driven programming in C++ |
I agree, the C++ stuff I wrote doesnt really seem like 'proper' event driven stuff to me. The thing with the java code is, that in the java example above, the handleEvent Method was all that was needed. Whenever the user closed the window, handleEvent was automatically called, ie there was no need to continously check to see if an event had been fired. Thats what I'd like to do with C++ and the SDL. |
Author: | ericfourfour [ Sun Mar 25, 2007 1:12 am ] |
Post subject: | RE:event driven programming in C++ |
Some of the more experienced c++ users may be able to help you, but I'm going to suggest callback functions. Although, if you want to make it work like Java (I assume you were working with Swing), you are going to have to do some research into Swing. It doesn't quite "automatically" call the handleEvent method and it does consistently check if an event has occurred. If you want to get the same effect in your c++ program, you are going to have to do a lot of work. |
Author: | abcdefghijklmnopqrstuvwxy [ Sun Mar 25, 2007 6:03 am ] | ||
Post subject: | RE:event driven programming in C++ | ||
Anon I was also asking if it was "proper" cause I honestly don't know that's just how I'm doing it. So far I haven't come across a better way. About handleEvents() I don't get how it would "automatically" get called. Your code could look something like this though, incase you don't already know...
I guess it makes the main() smaller to make a handleEvent function, but other than that I don't see a benifit. |
Author: | md [ Sun Mar 25, 2007 10:35 am ] |
Post subject: | RE:event driven programming in C++ |
If SDL supports callback functions you can use those, but in the end you'll have a while loop somewhere that just just as you've already got. Java hides all the looping and polling from you so you don't have to worry about it. In C++ there is no such abstraction, so you need to either write an extensive class system to create it; or just get used to the idea of having a central while loop that controls everything. |
Author: | Mazer [ Sun Mar 25, 2007 12:51 pm ] |
Post subject: | RE:event driven programming in C++ |
It might also be worth knowing that none of that was actually C++. You can use it with C++, but SDL is written in C. |
Author: | Anon [ Mon Mar 26, 2007 10:07 am ] | ||
Post subject: | Re: RE:event driven programming in C++ | ||
Mazer @ Sun Mar 25, 2007 12:51 pm wrote: It might also be worth knowing that none of that was actually C++. You can use it with C++, but SDL is written in C.
really? thats interesting, I was compiling with g++, all this time, but after reading this I used gcc to compile my program, and it worked just fine, since I had no OOP stuff, and I stuck to c strings. Thanks. I have another question though. When I compile/run my program, (entire code below), it seems to use a lot of CPU power, according to the windows task manager. have I done something wrong? I apologise in advance for the lack of good commenting
First I thought the inefficiency was because I was repainting the screen each time a key was pressed, but even if I press no keys and let the program sit idle(ie pollEvent finds nothing, and so the screen is not repainted), it still takes up a lot of CPU power. Any suggestions? |
Author: | md [ Mon Mar 26, 2007 10:59 am ] |
Post subject: | RE:event driven programming in C++ |
you're busy polling, which means that if there are no events you go and ask again right away. This a fine for things like games where you want things to happen as fast as they can, but otherwise it's pretty inefficient. A solution would be to insert a sleep() statement somewhere (or the SDL equivalent). That way you give up some CPU time to other processes and the CPU usage will drop. |
Author: | OneOffDriveByPoster [ Mon Mar 26, 2007 12:11 pm ] | ||
Post subject: | Re: RE:event driven programming in C++ | ||
Anon @ Mon Mar 26, 2007 10:07 am wrote:
|
Author: | Anon [ Mon Mar 26, 2007 3:54 pm ] |
Post subject: | Re: RE:event driven programming in C++ |
I looked up SDL_WaitEvent, but it seems that all that does is wait for 10 milliseconds before rechecking the event queue. So, I think I'll just include an SDL_Delay(10); in the outer while loop. Hopefully, that should give the same effect. thanks everyone. |