Skip to content

Commit

Permalink
added window id in events, regroup finger/controller/joystick id
Browse files Browse the repository at this point in the history
  • Loading branch information
ncannasse committed Mar 7, 2024
1 parent 13eea15 commit d34db3b
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 21 deletions.
60 changes: 42 additions & 18 deletions libs/sdl/sdl.c
Original file line number Diff line number Diff line change
Expand Up @@ -91,10 +91,10 @@ typedef struct {
int keyCode;
int scanCode;
bool keyRepeat;
int controller;
int reference;
int value;
int fingerId;
int joystick;
int __unused;
int window;
vbyte* dropFile;
} event_data;

Expand Down Expand Up @@ -161,6 +161,10 @@ HL_PRIM bool HL_NAME(hint_value)( vbyte* name, vbyte* value) {
return SDL_SetHint((char*)name, (char*)value) == SDL_TRUE;
}

HL_PRIM int HL_NAME(event_poll)( SDL_Event *e ) {
return SDL_PollEvent(e);
}

HL_PRIM bool HL_NAME(event_loop)( event_data *event ) {
while (true) {
SDL_Event e;
Expand All @@ -171,56 +175,65 @@ HL_PRIM bool HL_NAME(event_loop)( event_data *event ) {
break;
case SDL_MOUSEMOTION:
event->type = MouseMove;
event->window = e.motion.windowID;
event->mouseX = e.motion.x;
event->mouseY = e.motion.y;
event->mouseXRel = e.motion.xrel;
event->mouseYRel = e.motion.yrel;
break;
case SDL_KEYDOWN:
event->type = KeyDown;
event->window = e.key.windowID;
event->keyCode = e.key.keysym.sym;
event->scanCode = e.key.keysym.scancode;
event->keyRepeat = e.key.repeat != 0;
break;
case SDL_KEYUP:
event->type = KeyUp;
event->window = e.key.windowID;
event->keyCode = e.key.keysym.sym;
event->scanCode = e.key.keysym.scancode;
break;
case SDL_SYSWMEVENT:
continue;
case SDL_MOUSEBUTTONDOWN:
event->type = MouseDown;
event->window = e.button.windowID;
event->button = e.button.button;
event->mouseX = e.button.x;
event->mouseY = e.motion.y;
break;
case SDL_MOUSEBUTTONUP:
event->type = MouseUp;
event->window = e.button.windowID;
event->button = e.button.button;
event->mouseX = e.button.x;
event->mouseY = e.motion.y;
break;
case SDL_FINGERDOWN:
event->type = TouchDown;
event->window = e.tfinger.windowID;
event->mouseX = (int)(e.tfinger.x*10000);
event->mouseY = (int)(e.tfinger.y*10000);
event->fingerId = (int)e.tfinger.fingerId;
event->reference = (int)e.tfinger.fingerId;
break;
case SDL_FINGERMOTION:
event->type = TouchMove;
event->window = e.tfinger.windowID;
event->mouseX = (int)(e.tfinger.x*10000);
event->mouseY = (int)(e.tfinger.y*10000);
event->fingerId = (int)e.tfinger.fingerId;
event->reference = (int)e.tfinger.fingerId;
break;
case SDL_FINGERUP:
event->type = TouchUp;
event->window = e.tfinger.windowID;
event->mouseX = (int)(e.tfinger.x*10000);
event->mouseY = (int)(e.tfinger.y*10000);
event->fingerId = (int)e.tfinger.fingerId;
event->reference = (int)e.tfinger.fingerId;
break;
case SDL_MOUSEWHEEL:
event->type = MouseWheel;
event->window = e.wheel.windowID;
event->wheelDelta = e.wheel.y;
# if SDL_VERSION_ATLEAST(2,0,4)
if (e.wheel.direction == SDL_MOUSEWHEEL_FLIPPED) event->wheelDelta *= -1;
Expand All @@ -230,6 +243,7 @@ HL_PRIM bool HL_NAME(event_loop)( event_data *event ) {
break;
case SDL_WINDOWEVENT:
event->type = WindowState;
event->window = e.window.windowID;
switch (e.window.event) {
case SDL_WINDOWEVENT_SHOWN:
event->state = Show;
Expand Down Expand Up @@ -280,82 +294,86 @@ HL_PRIM bool HL_NAME(event_loop)( event_data *event ) {
continue;
case SDL_TEXTINPUT:
event->type = TextInput;
event->window = e.text.windowID;
event->keyCode = *(int*)e.text.text;
event->keyCode &= e.text.text[0] ? e.text.text[1] ? e.text.text[2] ? e.text.text[3] ? 0xFFFFFFFF : 0xFFFFFF : 0xFFFF : 0xFF : 0;
break;
case SDL_CONTROLLERDEVICEADDED:
event->type = GControllerAdded;
event->controller = e.jdevice.which;
event->reference = e.jdevice.which;
break;
case SDL_CONTROLLERDEVICEREMOVED:
event->type = GControllerRemoved;
event->controller = e.jdevice.which;
event->reference = e.jdevice.which;
break;
case SDL_CONTROLLERBUTTONDOWN:
event->type = GControllerDown;
event->controller = e.cbutton.which;
event->reference = e.cbutton.which;
event->button = e.cbutton.button;
break;
case SDL_CONTROLLERBUTTONUP:
event->type = GControllerUp;
event->controller = e.cbutton.which;
event->reference = e.cbutton.which;
event->button = e.cbutton.button;
break;
case SDL_CONTROLLERAXISMOTION:
event->type = GControllerAxis;
event->controller = e.caxis.which;
event->reference = e.caxis.which;
event->button = e.caxis.axis;
event->value = e.caxis.value;
break;
case SDL_JOYAXISMOTION:
event->type = JoystickAxisMotion;
event->joystick = e.jaxis.which;
event->reference = e.jaxis.which;
event->button = e.jaxis.axis;
event->value = e.jaxis.value;
break;
case SDL_JOYBALLMOTION:
event->type = JoystickBallMotion;
event->joystick = e.jball.which;
event->reference = e.jball.which;
event->button = e.jball.ball;
event->mouseXRel = e.jball.xrel;
event->mouseYRel = e.jball.yrel;
break;
case SDL_JOYHATMOTION:
event->type = JoystickHatMotion;
event->joystick = e.jhat.which;
event->reference = e.jhat.which;
event->button = e.jhat.hat;
event->value = e.jhat.value;
break;
case SDL_JOYBUTTONDOWN:
event->type = JoystickButtonDown;
event->joystick = e.jbutton.which;
event->reference = e.jbutton.which;
event->button = e.jbutton.button;
break;
case SDL_JOYBUTTONUP:
event->type = JoystickButtonUp;
event->joystick = e.jbutton.which;
event->reference = e.jbutton.which;
event->button = e.jbutton.button;
break;
case SDL_JOYDEVICEADDED:
event->type = JoystickAdded;
event->joystick = e.jdevice.which;
event->reference = e.jdevice.which;
break;
case SDL_JOYDEVICEREMOVED:
event->type = JoystickRemoved;
event->joystick = e.jdevice.which;
event->reference = e.jdevice.which;
break;
case SDL_DROPBEGIN:
event->type = DropStart;
event->window = e.drop.windowID;
break;
case SDL_DROPFILE: case SDL_DROPTEXT: {
vbyte* bytes = hl_copy_bytes(e.drop.file, (int)strlen(e.drop.file) + 1);
SDL_free(e.drop.file);
event->type = e.type == SDL_DROPFILE ? DropFile : DropText;
event->dropFile = bytes;
event->window = e.drop.windowID;
break;
}
case SDL_DROPCOMPLETE:
event->type = DropEnd;
event->window = e.drop.windowID;
break;
default:
//printf("Unknown event type 0x%X\\n", e.type);
Expand Down Expand Up @@ -475,6 +493,7 @@ HL_PRIM const char *HL_NAME(detect_keyboard_layout)() {
DEFINE_PRIM(_BOOL, init_once, _NO_ARG);
DEFINE_PRIM(_VOID, gl_options, _I32 _I32 _I32 _I32 _I32 _I32);
DEFINE_PRIM(_BOOL, event_loop, _DYN );
DEFINE_PRIM(_I32, event_poll, _STRUCT );
DEFINE_PRIM(_VOID, quit, _NO_ARG);
DEFINE_PRIM(_VOID, delay, _I32);
DEFINE_PRIM(_I32, get_screen_width, _NO_ARG);
Expand Down Expand Up @@ -676,6 +695,10 @@ HL_PRIM void HL_NAME(win_render_to)(SDL_Window *win, SDL_GLContext gl) {
SDL_GL_MakeCurrent(win, gl);
}

HL_PRIM int HL_NAME(win_get_id)(SDL_Window *window) {
return SDL_GetWindowID(window);
}

HL_PRIM void HL_NAME(win_destroy)(SDL_Window *win, SDL_GLContext gl) {
SDL_DestroyWindow(win);
SDL_GL_DeleteContext(gl);
Expand Down Expand Up @@ -703,6 +726,7 @@ DEFINE_PRIM(_BOOL, win_set_opacity, TWIN _F64);
DEFINE_PRIM(_VOID, win_swap_window, TWIN);
DEFINE_PRIM(_VOID, win_render_to, TWIN TGL);
DEFINE_PRIM(_VOID, win_destroy, TWIN TGL);
DEFINE_PRIM(_I32, win_get_id, TWIN);

// game controller

Expand Down
15 changes: 12 additions & 3 deletions libs/sdl/sdl/Event.hx
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,20 @@ package sdl;
public var keyCode : Int;
public var scanCode : Int;
public var keyRepeat : Bool;
public var controller : Int;
public var reference : Int;
public var value : Int;
public var fingerId : Int;
public var joystick : Int;
public var __unused : Int;
public var windowId : Int;
public var dropFile: hl.Bytes;

// for compile-time backward compatibility
public var controller(get,never) : Int;
public var joystick(get,never) : Int;
public var fingerId(get,never) : Int;
inline function get_controller() return reference;
inline function get_joystick() return reference;
inline function get_fingerId() return reference;

public function new() {
}
}
Expand Down
10 changes: 10 additions & 0 deletions libs/sdl/sdl/Window.hx
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ class Window {
var win : WinPtr;
var glctx : GLContext;
var lastFrame : Float;
public var id(get,never) : Int;
public var title(default, set) : String;
public var vsync(default, set) : Bool;
public var width(get, never) : Int;
Expand Down Expand Up @@ -256,6 +257,10 @@ class Window {
setWindowGrab(win, v);
return v;
}

function get_id() {
return winGetId(win);
}

/**
Set the current window you will render to (in case of multiple windows)
Expand Down Expand Up @@ -332,6 +337,11 @@ class Window {
return 0;
}

@:hlNative("?sdl", "win_get_id")
static function winGetId( win : WinPtr ) : Int {
return 0;
}

static function winSetSize( win : WinPtr, width : Int, height : Int ) {
}

Expand Down

0 comments on commit d34db3b

Please sign in to comment.