Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon.

Pages: 1-4041-8081-

Question about event handling

Name: Anonymous 2014-06-21 11:42

Let's say I use SDL (1.2 or 2.0) in my program to handle events, should I use callbacks or the good old giant switch statement?

Name: Anonymous 2014-06-21 12:10

Callbacks are much more maintainable and scalable. Is this really a question?

Name: Anonymous 2014-06-21 12:14

>>2
Yes because everyone I have seen uses a switch
Also using a for/while that calls every callback that may not even be needed would be slower instead of using a nice O(1) switch

Name: Anonymous 2014-06-21 12:23

>>3
Also using a for/while that calls every callback that may not even be needed
Why would you even do that? Do you even know what a callback is?

And why are you microoptimizing before profiling? I bet you're going to use dynamic linking, killing any LTO you might have gotten.

Name: Anonymous 2014-06-21 12:30

>>4
Why would you even do that?
any better idea

Do you even know what a callback is?
using ``callback'' in that was a mistake, but I think it was very clear what I meant

And why are you microoptimizing before profiling?
It is not microoptimisation, it is very clear that it is going to be slower that way

I bet you're going to use dynamic linking, killing any LTO you might have gotten.
You do not know me

Name: Anonymous 2014-06-21 12:39

>>4
Are you from /g/? The way you talk makes me think so.

Name: Anonymous 2014-06-21 13:18

>>5
any better idea

Yes. Use callbacks properly.

but I think it was very clear what I meant

Callbacks are real things used for event handling. Whatever you are pretnding to be thinking of probably isn't.

It is not microoptimisation, it is very clear that it is going to be slower that way

It is, and callbacks aren't even slower. Even if you get the compiler to emit an O(1) switch (good luck with that), it's O(n) in terms of events received. Using callbacks is O(n) in terms of events handled.

Name: Anonymous 2014-06-21 13:22

>>7
Yes. Use callbacks properly.
explain

pretnding
what does this even mean?

It is, and callbacks aren't even slower. Even if you get the compiler to emit an O(1) switch (good luck with that), it's O(n) in terms of events received. Using callbacks is O(n) in terms of events handled.
no idea how it would work

Name: Anonymous 2014-06-21 14:30

>>8
Seriously, what do you think a callbacks is?

Name: Anonymous 2014-06-21 14:33

>>9
Why that question?

Name: Anonymous 2014-06-21 14:39

>>8
Who are you quoting?

Name: Anonymous 2014-06-21 14:39

>>11
del

Name: Anonymous 2014-06-21 15:32

>>10
Nevermind then. Hope you got what you needed.

Name: Anonymous 2014-06-21 16:01

>>13
I did not but anyway

Name: Anonymous 2014-06-21 16:31

>>14
Use callbacks.

Name: Anonymous 2014-06-21 16:39

>>2
Callbacks are much more maintainable and scalable
Fuck off, Javashit loving fagstorm. Die in a fire.

Name: Anonymous 2014-06-21 16:40

>>7
Even if you get the compiler to emit an O(1) switch (good luck with that)
What? Ever heard of branch tables?

Name: Anonymous 2014-06-21 16:53

>>17
We've been over this already. For non-trivial case analysis you either end up with a very large table or a forest of conditional guards. So yeah, if you're lucky you'll get O(1), if not you'll get something else. If you're unlucky you'll get O(1) on paper and O(stall) in real life.

Name: Anonymous 2014-06-21 17:14

>>15
I made this thread not because I wannted kikes to say to me ``use callbacks'' but because I wannted to find a good way to handle events without being hell slow
The only way I can think of handling the events it to travel a list of function pointers and call all of them with a pointer to the current event

Name: Anonymous 2014-06-21 17:43

>>1

C/C++ doesn't support lambdas, so callback would have limited value.

Name: Anonymous 2014-06-21 17:46

>>20
what's wrong with event_register (onMove, &player);?

Name: Anonymous 2014-06-21 17:54

>>21

you have to explicitly specify closure (&player in this case)

Name: Anonymous 2014-06-21 18:02

>>22
It's true that in a language like scheme it would look better but I have no problem with that
it's C after all

Name: /g/reat /g/uy 2014-06-21 18:30

>>20
[](){}
That's C++11, in old C++ you may use functors, and in C you can have something like a lambda expression with macros and function pointers. LISP is not that special m8!

Name: Anonymous 2014-06-21 18:37

>>24
in C you can have something like a lambda expression with macros and function pointers
I want a example (except from something like >>21)

Name: Anonymous 2014-06-21 19:06

>>16
I write C++. I never had to learn Java.
>>19
Provide a testcase -- a small snippet of code that is running slower than you'd like.
>>20
See >>2. Also, with embedded languages like Lua, your post is even more out of touch with reality.
>>24
In C (C89/C99/C11) you don't have lambda expressions. Lisp is special especially for its AST.

Name: Anonymous 2014-06-21 19:12

cork my anus

Name: Anonymous 2014-06-21 19:14

Provide a testcase -- a small snippet of code that is running slower than you'd like.
while (SDL_WaitEvent (&e))
for (size_t i = 0; i < events_p; i++)
events[i].fun (e, events[i].arg);

Name: Anonymous 2014-06-21 19:17

Callbacks are much more maintainable and scalable [than big switch statements]
Really?

Name: Anonymous 2014-06-21 19:29

Well, there must be a reason why the nodejs developers decided to use callbacks.

Name: Anonymous 2014-06-21 19:33

>>30
Because they suck!

Name: Anonymous 2014-06-21 19:52

>>28
You are passing the struct by value to your callbacks. Why does this run slower than you'd expect?
>>29
Yes.

Name: Anonymous 2014-06-21 19:54

>>32
You are passing the struct by value to your callbacks. Why does this run slower than you'd expect?
it runs slower even if I use a pointer

Why does this run slower than you'd expect?
because turds

Name: Anonymous 2014-06-21 19:58

Name: Anonymous 2014-06-21 20:04

>>34
Their is* macros are invoking undefined behavior on two different grounds. Mind you, those were the first lines after the #include's. Ergo, the code is shit.

Name: Anonymous 2014-06-21 20:06

>>34
That code is god damn awful. Memory leak at lambda_vector_resize. They don't even know how to properly use realloc. What shitheads.

Name: Anonymous 2014-06-21 20:15

the way they redefine is* incorectly by violotating te standard makes me upset
this was written by my one hannd because I was mastrubuting

Name: Anonymous 2014-06-21 20:25

char *temp = (char*)realloc(source->data, source->length + bs);
char *temp = realloc(vec->chars, vec->length * vec->size);
*vomits*
*farts all over this thread*

Name: Anonymous 2014-06-21 20:43

Why use C/C++ when there's ATS?

Name: Anonymous 2014-06-21 20:46

>>30
imperative programming could be awful for concurrency, so the idea is to do every in one single thread and think asynchronously, i.e., your program will follow an event driven paradigm (not sure if that makes sense in english, sorry)

>>1
http://gameprogrammingpatterns.com/

Name: Anonymous 2014-06-21 20:51

>>1
*I* shall answer you question, good sir, without being rude and without belittling your work.

Try one, see how it goes, then try th' other. Once you have experimented to your satisfaction, you may move on with your life.

Regards,
Invenietis

Name: Anonymous 2014-06-21 20:55

>>40
bookmarked

Name: Anonymous 2014-06-22 5:17

>>6
sup, /g/ s'goin' down /g/ top of the morning /g/ feelin good /g/ shooting niggas up /g/ crackalackin /g/ smokin grass /g/ doin taxes /g/ crackin homos /g/ sackin bonos /g/ cmon /g/ give us a glance /g/ show us your stuff /g/

Name: Anonymous 2014-06-22 5:18

>>6
sup, /g/ s'goin' down /g/ top of the morning /g/ feelin good /g/ shooting niggas up /g/ crackalackin /g/ smokin grass /g/ doin taxes /g/ crackin homos /g/ sackin bonos /g/ cmon /g/ give us a glance /g/ show us your stuff /g/

Name: Anonymous 2014-06-22 12:27

>>40

How is game programming different from any other kind of programming?

Name: Anonymous 2014-06-22 14:13

>>45
Everything is different... as programming an e-mail server is not the same as programming a word processor. Every ``kind of programming'' is different from each other. Even between different game genres.
Check out the design patterns of that e-book and you will understand.

Name: Anonymous 2014-06-22 14:26

Can someone please actually help with the question?

Name: Anonymous 2014-06-22 14:37

>>47
SDL a shit

Name: Anonymous 2014-06-22 14:42

>>48
it does not matter if it is about SDL, it is about any kind of event system

Name: Anonymous 2014-06-22 15:02

>>47
Could you please stop assuming it's the board's obligation to answer every single question you make?

Name: Anonymous 2014-06-22 15:06

>>46

Can you provide an example of fundamental differences between writing a game engine and say E. Coli simulation?

Name: Anonymous 2014-06-22 15:07

>>50
I made only one question here about programming and it is the only programming quetion in the front page
I also used the "Don't bump" functionality to post this
So, what is your problem? I would also like to ask if you are >>2,4,7

Name: Anonymous 2014-06-22 15:35

>>52
I am not.

How are these threads
http://progrider.org/prog/read/1403254313
http://progrider.org/prog/read/1403411006
http://progrider.org/prog/read/1403166961
http://progrider.org/prog/read/1399040031
http://progrider.org/prog/read/1403330382
http://progrider.org/prog/read/1403205144
not programming related? They're all on the front page.

Could you please stop calling everything you don't like ``off-topic''? The aforementioned threads may not have a completely serious mood but that doesn't mean they shouldn't be here. /prog/ likes to fuck around, is that fact that surprising to you?

Name: Anonymous 2014-06-22 15:39

>>53
fuck you

Name: Anonymous 2014-06-22 15:40

>>47
You got answers right away then objected to them on fictional grounds. It seems like what you're really asking for is an argument, which you also got. Enjoy.

Name: Anonymous 2014-06-22 15:48

>>55
The only answers I got were >>2,4,7 that would be also called as ``shitposting''.
what else?

Name: Anonymous 2014-06-22 15:48

hax my anus

Name: Anonymous 2014-06-22 15:51

>>57
hax mine first

Name: Anonymous 2014-06-22 16:02

>>56
If that's shitposting, what do you call >>1?

Name: Anonymous 2014-06-22 16:03

>>59
I do not know how I would call >>1 but I am sure that I would call you a big fat troll

Name: Anonymous 2014-06-22 16:13

I browse /prog/ since 2006 and this is the first! time this happened to me. Every programming question I ever made got high quality replies except this. Looks like from now on I will make my programming questions on the ni/g/g/erland because no programmers are left here

Name: Anonymous 2014-06-22 16:13

>>60
How does you would call you a big fat troll make you feel?

Name: Anonymous 2014-06-22 16:18

>>62
Does >>63 have a nose?

Name: Anonymous 2014-06-22 16:21

>>61
Go ahead.

Name: Anonymous 2014-06-22 16:30

>>64
No thanks

Name: Anonymous 2014-06-22 16:30

>>47
What is the question?

Name: Anonymous 2014-06-22 16:33

>>66
``should I use callbacks or the good old giant switch statement?'' [and why (implied)]

Name: Anonymous 2014-06-22 16:34

>>67
Callbacks, they are more scalable and maintainable.

Name: Anonymous 2014-06-22 16:36

>>68
Using a for/while that calls every callback that may not even be needed would be slower instead of using a nice O(1) switch

Name: Anonymous 2014-06-22 16:37

>>67
Anyway, in case you are not a troll, your confusion stems from the fact you think callbacks are supposed to check the event and decide whether to act or not. Callbacks always act when they are called. You register callbacks using some mechanism provided by the event library. Thus, whether it is O(n) or O(1) to decide which callback to use, is up to the event library implementation.

Name: Anonymous 2014-06-22 16:38

>>69
Well, see >>70.

Name: Anonymous 2014-06-22 16:40

hax my anus

Name: Anonymous 2014-06-22 16:43

>>70
The thing is that I am not using a event library that gives me the possibility of callbacks but I do it myself on top of SDL
I will implement something like this looks like http://www.gamedev.net/topic/646358-event-handlers-in-c/ that gives me O(1)

Name: Anonymous 2014-06-22 17:21

>>40
http://gameprogrammingpatterns.com/observer.html
That guy is surely a retrograde moron. "Observer pattern" and all other "design patterns" are just a boilerplate, completely useless in Scheme and Common Lisp, which you should be using today. I.e. observer pattern was invented to emulates closures.

Name: Anonymous 2014-06-22 17:41

>>74
How would you replicate that behavior with closures? I'm not encouraging the use of OBSERVERS, I'm legitimately curious.

Name: Anonymous 2014-06-22 18:43

>>74
Scheme and Common Lisp, which you should be using today
Why would I use an outdated, obsolete and also unpopular language?

Name: Anonymous 2014-06-22 18:50

>>73
Why don't you use SDL_AddEventWatch? It's O(1)!

http://wiki.libsdl.org/SDL_AddEventWatch
Use this function to add a callback to be triggered when an event is added to the event queue.

>>75
He's retarded. What he is saying is the same with 'lambda calculus is turing complete!'. He won't even show how to implement an observer with a closure -- I guarantee this.

Name: Anonymous 2014-06-22 18:54

>>77
SDL_AddEventWatch
doesn't return and error and /* uh oh */ https://hg.libsdl.org/SDL/file/60edb019f0fe/src/events/SDL_events.c#l496
it also calls every function even if it is not about that event so it has the same problems as using >>28 (something like signal() that has the event as first argument would be better)

Name: Anonymous 2014-06-22 20:29

>>76

How do these people find their way on here?

Name: Anonymous 2014-06-22 20:35

>>79
saying that everyone is from /g/ is rude, please stop

Name: Anonymous 2014-06-22 20:39

>>80
He didn't say that. Putting words in other people's mouths is even ruder.

Name: Anonymous 2014-06-22 20:41

>>81
It's clear what he meant, I am sure that he is the ``back to /g/'' guy

Name: Anonymous 2014-06-22 22:16

>>73
Why are you even asking if you have already made up your mind?

>>75
You can put the closure on the observer list and call it directly instead of calling a notify method on it (because there's no need & closures don't usually have methods.)

Or you can skip this nonsense and embed the closure in the event classifier with a setter. Default lamdbas (or checked None instances) exist in the classifer as no-ops. Finally, instead of internal event classification, direct handling of events should be used whenever possible.

Name: Anonymous 2014-06-22 22:49

>>78
Thanks, I didn't know SDL was so shitty.

Name: Anonymous 2014-06-23 4:21

>>76

Because it is still better than any other language in existence.

Name: Anonymous 2014-06-23 4:26

>>75

(register-callback event closure)

Name: Anonymous 2014-06-23 6:39

>>83
Why are you even asking if you have already made up your mind?
Firstly I thinked of something like this after I made this thread
Second I am asking for a better solution since it feels ``shitty''

>>84
If this was the only problem in SDL it would be fine

Name: Anonymous 2014-06-23 7:06

>>87
Second I am asking for a better solution since it feels ``shitty''

If you think this is shitty, just do the easiest thing you can think of, but keep in mind you might have to change your strategy. Event handling is going to need a lot of your attention to get right.

You've got about 16ms each frame to do all the logic and render the frame. That's about 64 million instructions on a modern machine. You could do events in O(n²) and it's not going to amount to much of that. So do what is easiest to debug and forget about how fast it is. If you start dropping frames profile before you optimize or you will end up optimizing the wrong thing.

Name: Anonymous 2014-06-23 7:13

>>88
O-okay, thanks
I wonder how modern multi-purpose engines do these things

Name: Anonymous 2014-06-23 9:03

>>89 it's an implementation detail. If you're really curious, read the source.

Name: Anonymous 2014-06-23 10:41

Name: Anonymous 2014-06-23 10:47

>>91
This music is fucking epic
5/5 breddy good

Name: Anonymous 2014-06-23 11:03

Name: Anonymous 2014-06-23 15:09

This thread did not deserve so much shitposting

Name: Anonymous 2014-06-23 15:41

>>94
this is /prog/
every thread shall contain shitposting
NO EXCEPTIONS

Name: Anonymous 2014-06-23 15:49

>>95
This is *grabs dick*
every thread shall contain *grabs dick*
NO *grabs dick*

Don't change these.
Name: Email:
Entire Thread Thread List