Created
December 30, 2019 16:22
-
-
Save lockie/c4dbfe893315f307cfc05ee84b3b3774 to your computer and use it in GitHub Desktop.
racket-sdl2 prettified events example
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#lang racket/base | |
(require | |
(prefix-in sdl2: sdl2/pretty) | |
ffi/unsafe) | |
(define (main) | |
(define quit #f) | |
(define event-ptr | |
(cast (malloc (ctype-sizeof sdl2:_event)) _pointer sdl2:_event*)) | |
(define x 288) | |
(define y 208) | |
(sdl2:init! '(video)) | |
(define window (sdl2:create-window! "SDL2 Keyboard/Mouse events" | |
sdl2:window-pos-undefined | |
sdl2:window-pos-undefined | |
640 480 '())) | |
(define renderer (sdl2:create-renderer! window -1 '())) | |
(define image (sdl2:load-bmp "spaceship.bmp")) | |
(define texture (sdl2:create-texture-from-surface renderer image)) | |
(sdl2:free-surface! image) | |
(sdl2:set-render-draw-color! renderer 255 255 255 255) | |
(for ([dummy (in-naturals)] | |
#:break quit) | |
(sdl2:delay! 20) | |
(sdl2:poll-event! event-ptr) | |
(define event (ptr-ref event-ptr sdl2:_event)) | |
(case (union-ref event 0) | |
[(quit) | |
(set! quit #t)] | |
[(key-down) | |
(case (sdl2:keysym-sym | |
(sdl2:keyboard-event-keysym | |
(union-ref event 4))) | |
[(left) (set! x (sub1 x))] | |
[(right) (set! x (add1 x))] | |
[(up) (set! y (sub1 y))] | |
[(down) (set! y (add1 y))])] | |
[(mouse-button-down) | |
(define (show-msg msg) | |
(sdl2:show-simple-message-box | |
'information "Mouse" msg window)) | |
(let ([x (sdl2:mouse-button-event-button (union-ref event 8))]) | |
(cond | |
[(= x sdl2:button-left) | |
(show-msg "Left button was pressed!")] | |
[(= x sdl2:button-right) | |
(show-msg "Right button was pressed!")] | |
[else | |
(show-msg "Some other button was pressed!")]))] | |
[(mouse-motion) | |
(define motion (union-ref event 7)) | |
(sdl2:set-window-title! | |
window | |
(format "X: ~a Y: ~a" | |
(sdl2:mouse-motion-event-x motion) | |
(sdl2:mouse-motion-event-y motion)))]) | |
(sdl2:render-clear! renderer) | |
(sdl2:render-copy! renderer texture #f (sdl2:make-rect x y 64 64)) | |
(sdl2:render-present! renderer)) | |
(sdl2:destroy-texture! texture) | |
(sdl2:destroy-renderer! renderer) | |
(sdl2:destroy-window! window) | |
(sdl2:quit!)) | |
(main) |
Yeah, racket-sdl2 is just a thin wrapper around low-level library; I guess for nicer Scheme'y interface you'll need to write some sort of wrapper (I reckon Racket has some kind of iterator abstraction, probably this would be a good start).
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thanks! Maybe this should be added to the example section as well... :)
I am trying to make the event handling more "Scheme-y", or "Racket-y"... so that at some point the pointers are abstracted away. For comparison, in PyGame you would do something like
...where
event
is just an object that you can inspect, etc. Racket is not Python, but it should still be possible to just get a list of the current events, as some kind of struct or object, and inspect them, without having to expose lower-level constructs like pointers orunion-ref
s, etc. So I want to look into that, but I'll probably need to learn some more Racket first...