Comments (13)
Looks great!
from go-sdl2.
Certainly!
from go-sdl2.
Hi @kv-o, you can add sleep using sdl.Delay()
between the loop to reduce CPU usage.
from go-sdl2.
sdl.Delay
seems somewhat like a hacky solution... Would sdl.WaitEvent
be a better approach? I am unfamiliar with SDL2 and have little experience with graphics, so I apologise if I have overlooked/misunderstood something obvious.
from go-sdl2.
Hi @kv-o, could you elaborate on why you find it to be hacky? My impression is that most games use similar approach, unless it updates only on events.
from go-sdl2.
If I understand correctly, specifying a hardcoded delay could introduce latencies between event occurrence and handling. sdl.WaitEvent
seems (at least on initial impression) to block until an event actually occurs, thus reducing CPU time without introducing additional latencies. My interpretation might be wrong though; this is my first time interacting with SDL.
from go-sdl2.
Hi @kv-o, you can calculate the time it takes to process one loop and pass appropriate value to sdl.Delay()
. It doesn't need to be hard-coded. I suppose this approach makes sense when you have a target framerate but otherwise, feel free to use sdl.WaitEvent()
if it's the better approach for your application.
from go-sdl2.
Hi @kv-o, I have updated the README with two more examples that demonstrate the use of both WaitEvent()
and PollEvent()
. Let me know if that addresses your concern!
from go-sdl2.
The sdl.WaitEvent
example only runs WaitEvent
on the for loop's init statement, not the post statement. (I haven't tested it but this seems to be a bug)
I've found the following to work decently for my use case:
running := true
for event := sdl.WaitEvent();; event = sdl.WaitEvent() {
if event == nil {
continue
}
switch event := event.(type) {
case *sdl.QuitEvent:
running = false
}
if !running {
break
}
}
I've had a look at the updated readme and I think there is a lot of code now (that newcomers might get lost in). Perhaps the WaitEvent
and PollEvent
examples could be reduced to two minimal code snippets that illustrate the difference?
Thanks for explaining the difference between using WaitEvent
and Delay
though — it made things a lot clearer!
from go-sdl2.
Hi @kv-o, thanks for noticing the typo! I have fixed it in the latest commit. What do you think can be done to make it simpler? I wanted to illustrate by drawing moving rectangle that reacts to user input for sdl.WaitEvent and another that reacts to user input while continuing to move for sdl.PollEvent.
from go-sdl2.
I think it's more about keeping the readme itself concise so that newcomers don't get overwhelmed by having to digest a lot of new information upfront. It might be too confusing for someone who is reading the readme for the first time.
Perhaps reduce the explanation of the difference between WaitEvent
and Delay
to just a couple of lines? I think the difference between the two is easy to understand, so maybe a short explanation with a minimal code snippet should suffice.
In my opinion it's probably better to have only one complete example in the readme, supplemented by a few guiding words of advice. Other standalone examples could probably be moved into the examples folder.
from go-sdl2.
Hi @kv-o, I have moved the two examples into the examples repository which is referred to in the README. Do you think that looks alright now?
from go-sdl2.
Thank you! Would it be okay if I close the issue then?
from go-sdl2.
Related Issues (20)
- WASI Support
- Not compatible with Go 1.21.0 HOT 7
- SDL/SDL.h: No such file or directory HOT 2
- How to ship release version HOT 4
- Surface panics on color conversions due to nonidiomatic color models HOT 4
- Windows release crashing likely because C.CString deallocated with C.SDL_free HOT 3
- Getting an errror when compiling to wasm HOT 2
- impossible type switch case: sdl.QuitEvent HOT 4
- Cannot find -lsdl2 HOT 10
- SDL_AudioStreamAvailable not correctly wrapped HOT 2
- Missing functions in gamecontroller.go HOT 6
- Readme Example Fix typo HOT 2
- Fails to link with musl libc HOT 1
- feature req: img.LoadSizedSVG HOT 2
- SdlSetTextureScaleMode HOT 7
- Window Doesn't Show HOT 9
- How to SDL on 64bit Windows Instructions Unclear HOT 1
- (suggestion) Convert sdl.Rect Fields from int32 to int HOT 4
- pkg-config issue with sdl2_gfx 0.4.35->0.4.39 HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from go-sdl2.