1
0
mirror of https://github.com/vlang/v.git synced 2023-08-10 21:13:21 +03:00

cleanup SDL event system

This commit is contained in:
prime31 2019-12-13 16:31:07 -08:00 committed by Alexander Medvednikov
parent 8581c11900
commit 75c01dec30
4 changed files with 301 additions and 68 deletions

284
vlib/sdl/events.v Normal file
View File

@ -0,0 +1,284 @@
module sdl
pub union C.SDL_Event {
pub:
@type u32
display C.SDL_DisplayEvent
window C.SDL_WindowEvent
key C.SDL_KeyboardEvent
edit C.SDL_TextEditingEvent
text C.SDL_TextInputEvent
motion C.SDL_MouseMotionEvent
button C.SDL_MouseButtonEvent
wheel C.SDL_MouseWheelEvent
jaxis C.SDL_JoyAxisEvent
jball C.SDL_JoyBallEvent
jhat C.SDL_JoyHatEvent
jbutton C.SDL_JoyButtonEvent
jdevice C.SDL_JoyDeviceEvent
caxis C.SDL_ControllerAxisEvent
cbutton C.SDL_ControllerButtonEvent
cdevice C.SDL_ControllerDeviceEvent
adevice C.SDL_AudioDeviceEvent
sensor C.SDL_SensorEvent
quit C.SDL_QuitEvent
user C.SDL_UserEvent
tfinger C.SDL_TouchFingerEvent
mgesture C.SDL_MultiGestureEvent
drop C.SDL_DropEvent
_pad56 [56]byte
}
// individual event declarations
pub struct C.SDL_DisplayEvent {
@type u32 /**< ::SDL_DISPLAYEVENT */
timestamp u32 /**< In milliseconds, populated using SDL_GetTicks() */
display u32 /**< The associated display index */
event byte /**< ::SDL_DisplayEventID */
padding1 byte
padding2 byte
padding3 byte
data1 int /**< event dependent data */
}
pub struct C.SDL_WindowEvent {
pub:
@type u32 /**< ::SDL_WINDOWEVENT */
timestamp u32 /**< In milliseconds, populated using SDL_GetTicks() */
windowID u32 /**< The associated window */
event byte /**< ::SDL_WindowEventID */
padding1 byte
padding2 byte
padding3 byte
data1 int
data2 int
}
pub struct C.SDL_KeyboardEvent {
pub:
@type u32 /**< ::SDL_KEYDOWN or ::SDL_KEYUP */
timestamp u32 /**< In milliseconds, populated using SDL_GetTicks() */
windowID u32 /**< The window with keyboard focus, if any */
state byte /**< ::SDL_PRESSED or ::SDL_RELEASED */
repeat byte /**< Non-zero if this is a key repeat */
padding2 byte
padding3 byte
keysym Keysym
}
pub struct C.SDL_TextEditingEvent {
pub:
@type u32 /**< ::SDL_TEXTEDITING */
timestamp u32 /**< In milliseconds, populated using SDL_GetTicks() */
windowID u32 /**< The window with keyboard focus, if any */
text [32]byte /**< The editing text */
start int /**< The start cursor of selected editing text */
length int /**< The length of selected editing text */
}
pub struct C.SDL_TextInputEvent {
pub:
@type u32 /**< ::SDL_TEXTINPUT */
timestamp u32 /**< In milliseconds, populated using SDL_GetTicks() */
windowID u32 /**< The window with keyboard focus, if any */
text [32]byte /**< The input text */
}
pub struct C.SDL_MouseMotionEvent {
pub:
@type u32 /**< ::SDL_MOUSEMOTION */
timestamp u32 /**< In milliseconds, populated using SDL_GetTicks() */
windowID u32 /**< The window with mouse focus, if any */
which u32 /**< The mouse instance id, or SDL_TOUCH_MOUSEID */
state u32 /**< The current button state */
x int /**< X coordinate, relative to window */
y int /**< Y coordinate, relative to window */
xrel int /**< The relative motion in the X direction */
yrel int /**< The relative motion in the Y direction */
}
pub struct C.SDL_MouseButtonEvent {
pub:
@type u32 /**< ::SDL_MOUSEBUTTONDOWN or ::SDL_MOUSEBUTTONUP */
timestamp u32 /**< In milliseconds, populated using SDL_GetTicks() */
windowID u32 /**< The window with mouse focus, if any */
which u32 /**< The mouse instance id, or SDL_TOUCH_MOUSEID */
button byte /**< The mouse button index */
state byte /**< ::SDL_PRESSED or ::SDL_RELEASED */
clicks byte /**< 1 for single-click, 2 for double-click, etc. */
padding1 byte
x int /**< X coordinate, relative to window */
y int /**< Y coordinate, relative to window */
}
pub struct C.SDL_MouseWheelEvent {
pub:
@type u32 /**< ::SDL_MOUSEWHEEL */
timestamp u32 /**< In milliseconds, populated using SDL_GetTicks() */
windowID u32 /**< The window with mouse focus, if any */
which u32 /**< The mouse instance id, or SDL_TOUCH_MOUSEID */
x int /**< The amount scrolled horizontally, positive to the right and negative to the left */
y int /**< The amount scrolled vertically, positive away from the user and negative toward the user */
direction u32 /**< Set to one of the SDL_MOUSEWHEEL_* defines. When FLIPPED the values in X and Y will be opposite. Multiply by -1 to change them back */
}
pub struct C.SDL_JoyAxisEvent {
pub:
@type u32 /**< ::SDL_JOYAXISMOTION */
timestamp u32 /**< In milliseconds, populated using SDL_GetTicks() */
which int /**< The joystick instance id */
axis byte /**< The joystick axis index */
padding1 byte
padding2 byte
padding3 byte
value i16 /**< The axis value (range: -32768 to 32767) */
padding4 u16
}
pub struct C.SDL_JoyBallEvent {
pub:
@type u32 /**< ::SDL_JOYBALLMOTION */
timestamp u32 /**< In milliseconds, populated using SDL_GetTicks() */
which int /**< The joystick instance id */
ball byte /**< The joystick trackball index */
padding1 byte
padding2 byte
padding3 byte
xrel i16 /**< The relative motion in the X direction */
yrel i16 /**< The relative motion in the Y direction */
}
pub struct C.SDL_JoyHatEvent {
pub:
@type u32 /**< SDL_JOYHATMOTION */
timestamp u32
which int /**< The joystick device index */
hat byte /**< The joystick hat index */
value byte /**< The hat position value:
* SDL_HAT_LEFTUP SDL_HAT_UP SDL_HAT_RIGHTUP
* SDL_HAT_LEFT SDL_HAT_CENTERED SDL_HAT_RIGHT
* SDL_HAT_LEFTDOWN SDL_HAT_DOWN SDL_HAT_RIGHTDOWN
* Note that zero means the POV is centered.
*/
padding1 byte
padding2 byte
}
pub struct C.SDL_JoyButtonEvent {
pub:
@type u32 /**< SDL_JOYBUTTONDOWN or SDL_JOYBUTTONUP */
timestamp u32
which int /**< The joystick device index */
button byte /**< The joystick button index */
state byte /**< SDL_PRESSED or SDL_RELEASED */
padding1 byte
padding2 byte
}
pub struct C.SDL_JoyDeviceEvent {
pub:
@type u32 /**< ::SDL_JOYDEVICEADDED or ::SDL_JOYDEVICEREMOVED */
timestamp u32 /**< In milliseconds, populated using SDL_GetTicks() */
which int /**< The joystick device index for the ADDED event, instance id for the REMOVED event */
}
pub struct C.SDL_ControllerAxisEvent {
pub:
@type u32 /**< ::SDL_CONTROLLERAXISMOTION */
timestamp u32 /**< In milliseconds, populated using SDL_GetTicks() */
which int /**< The joystick instance id */
axis byte /**< The controller axis (SDL_GameControllerAxis) */
padding1 byte
padding2 byte
padding3 byte
value i16 /**< The axis value (range: -32768 to 32767) */
padding4 u16
}
pub struct C.SDL_ControllerButtonEvent {
pub:
@type u32 /**< ::SDL_CONTROLLERBUTTONDOWN or ::SDL_CONTROLLERBUTTONUP */
timestamp u32 /**< In milliseconds, populated using SDL_GetTicks() */
which int /**< The joystick instance id */
button byte /**< The controller button (SDL_GameControllerButton) */
state byte /**< ::SDL_PRESSED or ::SDL_RELEASED */
padding1 byte
padding2 byte
}
pub struct C.SDL_ControllerDeviceEvent {
pub:
@type u32 /**< ::SDL_CONTROLLERDEVICEADDED, ::SDL_CONTROLLERDEVICEREMOVED, or ::SDL_CONTROLLERDEVICEREMAPPED */
timestamp u32 /**< In milliseconds, populated using SDL_GetTicks() */
which int /**< The joystick device index for the ADDED event, instance id for the REMOVED or REMAPPED event */
}
pub struct C.SDL_AudioDeviceEvent {
pub:
@type u32 /**< ::SDL_AUDIODEVICEADDED, or ::SDL_AUDIODEVICEREMOVED */
timestamp u32 /**< In milliseconds, populated using SDL_GetTicks() */
which u32 /**< The audio device index for the ADDED event (valid until next SDL_GetNumAudioDevices() call), SDL_AudioDeviceID for the REMOVED event */
iscapture byte /**< zero if an output device, non-zero if a capture device. */
padding1 byte
padding2 byte
padding3 byte
}
pub struct C.SDL_TouchFingerEvent {
pub:
@type u32 /**< ::SDL_FINGERMOTION or ::SDL_FINGERDOWN or ::SDL_FINGERUP */
timestamp u32 /**< In milliseconds, populated using SDL_GetTicks() */
touchId i64 /**< The touch device id */
fingerId i64
x f32 /**< Normalized in the range 0...1 */
y f32 /**< Normalized in the range 0...1 */
dx f32 /**< Normalized in the range -1...1 */
dy f32 /**< Normalized in the range -1...1 */
pressure f32 /**< Normalized in the range 0...1 */
}
pub struct C.SDL_MultiGestureEvent {
pub:
@type u32 /**< ::SDL_MULTIGESTURE */
timestamp u32 /**< In milliseconds, populated using SDL_GetTicks() */
touchId i64 /**< The touch device id */
dTheta f32
dDist f32
x f32
y f32
numFingers u16
padding u16
}
pub struct C.SDL_DropEvent {
pub:
@type u32 /**< ::SDL_DROPBEGIN or ::SDL_DROPFILE or ::SDL_DROPTEXT or ::SDL_DROPCOMPLETE */
timestamp u32 /**< In milliseconds, populated using SDL_GetTicks() */
file byteptr /**< The file name, which should be freed with SDL_free(), is NULL on begin/complete */
windowID u32 /**< The window that was dropped on, if any */
}
pub struct C.SDL_SensorEvent {
pub:
@type u32 /**< ::SDL_SENSORUPDATE */
timestamp u32 /**< In milliseconds, populated using SDL_GetTicks() */
which int /**< The instance ID of the sensor */
data [6]f32 /**< Up to 6 values from the sensor - additional values can be queried using SDL_SensorGetData() */
}
pub struct C.SDL_QuitEvent {
pub:
@type u32 /**< SDL_QUIT */
timestamp u32
}
pub struct C.SDL_UserEvent {
pub:
@type u32 /**< ::SDL_USEREVENT through ::SDL_LASTEVENT-1 */
timestamp u32 /**< In milliseconds, populated using SDL_GetTicks() */
windowID u32 /**< The associated window if any */
code int /**< User defined event code */
data1 voidptr /**< User defined data pointer */
data2 voidptr /**< User defined data pointer */
}

View File

@ -9,9 +9,9 @@ fn main() {
mut should_close := false
for {
ev := sdl.Event{}
for 0 < sdl.poll_event(&ev) {
match int(ev._type) {
evt := SDL_Event{}
for 0 < sdl.poll_event(&evt) {
match int(evt.@type) {
C.SDL_QUIT { should_close = true }
else {}
}

View File

@ -151,8 +151,7 @@ mut:
}
struct SdlContext {
pub:
mut:
pub mut:
// VIDEO
w int
h int
@ -374,12 +373,12 @@ fn main() {
g.draw_end()
// game.handle_events() // CRASHES if done in function ???
ev := sdl.Event{}
for 0 < sdl.poll_event(&ev) {
match int(ev._type) {
evt := SDL_Event{}
for 0 < sdl.poll_event(&evt) {
match int(evt.@type) {
C.SDL_QUIT { should_close = true }
C.SDL_KEYDOWN {
key := ev.key.keysym.sym
key := evt.key.keysym.sym
if key == C.SDLK_ESCAPE {
should_close = true
break
@ -388,16 +387,16 @@ fn main() {
game2.handle_key(key)
}
C.SDL_JOYBUTTONDOWN {
jb := int(ev.jbutton.button)
joyid := ev.jbutton.which
jb := int(evt.jbutton.button)
joyid := evt.jbutton.which
// println('JOY BUTTON $jb $joyid')
game.handle_jbutton(jb, joyid)
game2.handle_jbutton(jb, joyid)
}
C.SDL_JOYHATMOTION {
jh := int(ev.jhat.hat)
jv := int(ev.jhat.value)
joyid := ev.jhat.which
jh := int(evt.jhat.hat)
jv := int(evt.jhat.value)
joyid := evt.jhat.which
// println('JOY HAT $jh $jv $joyid')
game.handle_jhat(jh, jv, joyid)
game2.handle_jhat(jh, jv, joyid)

View File

@ -59,12 +59,6 @@ pub:
}
/////////////////////////////////////////////////////////
struct QuitEvent {
_type u32 /**< SDL_QUIT */
timestamp u32
}
struct Keysym {
pub:
scancode int /**< hardware specific scancode */
@ -72,49 +66,6 @@ pub:
mod u16 /**< current key modifiers */
unused u32 /**< translated character */
}
struct KeyboardEvent {
pub:
_type u32 /**< SDL_KEYDOWN or SDL_KEYUP */
timestamp u32
windowid u32
state byte /**< SDL_PRESSED or SDL_RELEASED */
repeat byte
padding2 byte
padding3 byte
keysym Keysym
}
struct JoyButtonEvent {
pub:
_type u32 /**< SDL_JOYBUTTONDOWN or SDL_JOYBUTTONUP */
timestamp u32
which int /**< The joystick device index */
button byte /**< The joystick button index */
state byte /**< SDL_PRESSED or SDL_RELEASED */
}
struct JoyHatEvent {
pub:
_type u32 /**< SDL_JOYHATMOTION */
timestamp u32
which int /**< The joystick device index */
hat byte /**< The joystick hat index */
value byte /**< The hat position value:
* SDL_HAT_LEFTUP SDL_HAT_UP SDL_HAT_RIGHTUP
* SDL_HAT_LEFT SDL_HAT_CENTERED SDL_HAT_RIGHT
* SDL_HAT_LEFTDOWN SDL_HAT_DOWN SDL_HAT_RIGHTDOWN
* Note that zero means the POV is centered.
*/
}
pub union Event {
pub:
_type u32
quit QuitEvent
key KeyboardEvent
jbutton JoyButtonEvent
jhat JoyHatEvent
_pad56 [56]byte
}
pub struct C.SDL_AudioSpec {
pub mut:
@ -140,13 +91,12 @@ pub mut:
// }
//type AudioSpec C.voidptrioSpec
type atexit_func_t fn ()
fn C.atexit(atexit_func_t)
fn C.atexit(func fn ())
///////////////////////////////////////////////////
fn C.SDL_MapRGB(fmt voidptr byte, g byte, b byte) u32
fn C.SDL_CreateRGBSurface(flags u32, width int, height int, depth int, Rmask u32, Gmask u32, Bmask u32, Amask u32) voidptr
fn C.SDL_PollEvent(&Event) int
fn C.SDL_PollEvent(&SDL_Event) int
fn C.SDL_NumJoysticks() int
fn C.SDL_JoystickNameForIndex(device_index int) voidptr
fn C.SDL_RenderCopy(renderer voidptr, texture voidptr, srcrect voidptr, dstrect voidptr) int
@ -231,8 +181,8 @@ pub fn render_copy(renderer voidptr, texture voidptr, srcrect &SDL_Rect, dstrect
return C.SDL_RenderCopy(renderer, texture, _srcrect, _dstrect)
}
pub fn poll_event(event &Event) int {
return C.SDL_PollEvent(voidptr(event))
pub fn poll_event(event &C.SDL_Event) int {
return C.SDL_PollEvent(event)
}
pub fn destroy_texture(text voidptr) {