veandco / go-sdl2-examples

This is where all go-sdl2 examples are stored
91 stars 34 forks source link

Why loop about event need twice #13

Closed kissdata closed 1 year ago

kissdata commented 1 year ago

When it comes to Event, most of code in examples look like this:

for event := sdl.PollEvent(); event != nil; event = sdl.PollEvent() {
    switch t := event.(type) {
    ...
    } 
}

Why the event (a interface) should be written = sdl.PollEvent() twice?

veeableful commented 1 year ago

Hi @kissdata, it is a standard for-loop in Go. You can see an explanation of it here.

We use it in the examples to extract all the Events in current frame until there's no more.

kissdata commented 1 year ago

I known it, for-loop with 3 expressions, so does in c/cpp. I now reading the article recommended by wiki.libsdl.org (ranked 6th) , he used a while-loop to process event:

while (SDL_PollEvent(&e)) {
    if (e.type == SDL_QUIT) {
        // ...
    }
    if (e.type == SDL_KEYDOWN) {
        // ...
    }
}

oh 👀 so short, if I use go recode it, this for-loop looks longer than original statement. I want see if exists a better way to implement it.