From 79cc5160a4342cc49e0a3ecf1843d87b609dc778 Mon Sep 17 00:00:00 2001 From: Vadim Grigoruk Date: Sat, 1 Dec 2018 14:49:47 +0300 Subject: [PATCH] #780 added mouse --- CMakeLists.txt | 14 +++++++++++-- src/system.h | 3 +++ src/system/sdlgpu.c | 16 +++++++-------- src/system/sokol.c | 46 ++++++++++++++++++++++++++++++++---------- src/system/sokol.h | 1 + src/system/sokol_gfx.c | 15 +++++++++++++- 6 files changed, 72 insertions(+), 23 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index beb9ed9..0417d15 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -496,7 +496,12 @@ foreach(TIC80_OUTPUT ${TIC80_OUTPUTS}) if(WIN32) set(TIC80_SRC ${TIC80_SRC} build/windows/tic80.rc) - add_executable(${TIC80_OUTPUT} WIN32 ${TIC80_SRC}) + if(CMAKE_BUILD_TYPE STREQUAL "Debug") + else() + set(SYSTEM_TYPE WIN32) + endif() + + add_executable(${TIC80_OUTPUT} ${SYSTEM_TYPE} ${TIC80_SRC}) elseif(APPLE) add_executable(${TIC80_OUTPUT} MACOSX_BUNDLE ${TIC80_SRC} build/macosx/tic80.icns) set_source_files_properties(build/macosx/tic80.icns PROPERTIES MACOSX_PACKAGE_LOCATION RESOURCES) @@ -555,7 +560,12 @@ foreach(TIC80_OUTPUT ${TIC80_OUTPUTS}) if(WIN32) set(TIC80_SRC ${TIC80_SRC} build/windows/tic80.rc) - add_executable(${TIC80_OUTPUT}-sokol WIN32 ${TIC80_SRC}) + if(CMAKE_BUILD_TYPE STREQUAL "Debug") + else() + set(SYSTEM_TYPE WIN32) + endif() + + add_executable(${TIC80_OUTPUT}-sokol ${SYSTEM_TYPE} ${TIC80_SRC}) elseif(APPLE) add_executable(${TIC80_OUTPUT}-sokol MACOSX_BUNDLE ${TIC80_SRC} build/macosx/tic80.icns) set_source_files_properties(build/macosx/tic80.icns PROPERTIES MACOSX_PACKAGE_LOCATION RESOURCES) diff --git a/src/system.h b/src/system.h index 7473064..ad718ba 100644 --- a/src/system.h +++ b/src/system.h @@ -3,6 +3,9 @@ #include "ticapi.h" #include "ext/file_dialog.h" +#define TIC80_OFFSET_LEFT ((TIC80_FULLWIDTH-TIC80_WIDTH)/2) +#define TIC80_OFFSET_TOP ((TIC80_FULLHEIGHT-TIC80_HEIGHT)/2) + typedef struct { void (*setClipboardText)(const char* text); diff --git a/src/system/sdlgpu.c b/src/system/sdlgpu.c index 7b5e096..9d5fbaf 100644 --- a/src/system/sdlgpu.c +++ b/src/system/sdlgpu.c @@ -14,8 +14,6 @@ #define STUDIO_PIXEL_FORMAT GPU_FORMAT_RGBA #define TEXTURE_SIZE (TIC80_FULLWIDTH) -#define OFFSET_LEFT ((TIC80_FULLWIDTH-TIC80_WIDTH)/2) -#define OFFSET_TOP ((TIC80_FULLHEIGHT-TIC80_HEIGHT)/2) #define KBD_COLS 22 #define KBD_ROWS 17 @@ -345,7 +343,7 @@ static void calcTextureRect(SDL_Rect* rect) rect->y = rect->w > rect->h ? (rect->h - discreteHeight) / 2 - : OFFSET_TOP*discreteWidth/Width; + : TIC80_OFFSET_TOP*discreteWidth/Width; rect->w = discreteWidth; rect->h = discreteHeight; @@ -381,8 +379,8 @@ static void processMouse() s32 x = -1, y = -1; if(crtMonitorEnabled()) { - if(rect.w) x = (mx - rect.x) * TIC80_FULLWIDTH / rect.w - OFFSET_LEFT; - if(rect.h) y = (my - rect.y) * TIC80_FULLHEIGHT / rect.h - OFFSET_TOP; + if(rect.w) x = (mx - rect.x) * TIC80_FULLWIDTH / rect.w - TIC80_OFFSET_LEFT; + if(rect.h) y = (my - rect.y) * TIC80_FULLHEIGHT / rect.h - TIC80_OFFSET_TOP; } else { @@ -833,7 +831,7 @@ static void blitGpuTexture(GPU_Target* screen, GPU_Image* texture) SDL_Rect rect = {0, 0, 0, 0}; calcTextureRect(&rect); - enum {Header = OFFSET_TOP, Top = OFFSET_TOP, Left = OFFSET_LEFT}; + enum {Header = TIC80_OFFSET_TOP, Top = TIC80_OFFSET_TOP, Left = TIC80_OFFSET_LEFT}; s32 width = 0; SDL_GetWindowSize(platform.window, &width, NULL); @@ -887,7 +885,7 @@ static void renderKeyboard() SDL_Rect rect; SDL_GetWindowSize(platform.window, &rect.w, &rect.h); - GPU_Rect src = {OFFSET_LEFT, OFFSET_TOP, KBD_COLS*TIC_SPRITESIZE, KBD_ROWS*TIC_SPRITESIZE}; + GPU_Rect src = {TIC80_OFFSET_LEFT, TIC80_OFFSET_TOP, KBD_COLS*TIC_SPRITESIZE, KBD_ROWS*TIC_SPRITESIZE}; float scale = rect.w/src.w; GPU_Rect dst = (GPU_Rect){0, rect.h - KBD_ROWS*TIC_SPRITESIZE*scale, scale, scale}; @@ -911,11 +909,11 @@ static void renderKeyboard() { if(key == KbdLayout[k]) { - GPU_Rect src = {(k % Cols)*TIC_SPRITESIZE + OFFSET_LEFT, (k / Cols)*TIC_SPRITESIZE + OFFSET_TOP, + GPU_Rect src = {(k % Cols)*TIC_SPRITESIZE + TIC80_OFFSET_LEFT, (k / Cols)*TIC_SPRITESIZE + TIC80_OFFSET_TOP, TIC_SPRITESIZE, TIC_SPRITESIZE}; GPU_BlitScale(platform.keyboard.texture.down, &src, platform.gpu.screen, - (src.x - OFFSET_LEFT) * dst.w, (src.y - OFFSET_TOP) * dst.h + dst.y, dst.w, dst.h); + (src.x - TIC80_OFFSET_LEFT) * dst.w, (src.y - TIC80_OFFSET_TOP) * dst.h + dst.y, dst.w, dst.h); } } } diff --git a/src/system/sokol.c b/src/system/sokol.c index d5ff6f2..26a6713 100644 --- a/src/system/sokol.c +++ b/src/system/sokol.c @@ -159,7 +159,9 @@ static void app_frame(void) return; } - tic->ram.input.gamepads.data = 0; + tic80_input* input = &tic->ram.input; + + input->gamepads.data = 0; handleKeyboard(); platform.studio->tick(); @@ -170,6 +172,8 @@ static void app_frame(void) platform.audio.samples[i] = (float)tic->samples.buffer[i] / SHRT_MAX; saudio_push(platform.audio.samples, count / 2); + + input->mouse.scrollx = input->mouse.scrolly = 0; } static void handleKeydown(sapp_keycode keycode, bool down) @@ -289,11 +293,11 @@ static void handleKeydown(sapp_keycode keycode, bool down) [SAPP_KEYCODE_KP_ENTER] = tic_key_return, [SAPP_KEYCODE_KP_EQUAL] = tic_key_equals, [SAPP_KEYCODE_LEFT_SHIFT] = tic_key_shift, - [SAPP_KEYCODE_LEFT_CONTROL] = tic_key_unknown, + [SAPP_KEYCODE_LEFT_CONTROL] = tic_key_ctrl, [SAPP_KEYCODE_LEFT_ALT] = tic_key_alt, [SAPP_KEYCODE_LEFT_SUPER] = tic_key_unknown, [SAPP_KEYCODE_RIGHT_SHIFT] = tic_key_shift, - [SAPP_KEYCODE_RIGHT_CONTROL] = tic_key_unknown, + [SAPP_KEYCODE_RIGHT_CONTROL] = tic_key_ctrl, [SAPP_KEYCODE_RIGHT_ALT] = tic_key_alt, [SAPP_KEYCODE_RIGHT_SUPER] = tic_key_unknown, [SAPP_KEYCODE_MENU] = tic_key_unknown, @@ -306,6 +310,18 @@ static void handleKeydown(sapp_keycode keycode, bool down) } } +static void processMouse(sapp_mousebutton btn, s32 down) +{ + tic80_input* input = &platform.studio->tic->ram.input; + + switch(btn) + { + case SAPP_MOUSEBUTTON_LEFT: input->mouse.left = down; break; + case SAPP_MOUSEBUTTON_MIDDLE: input->mouse.middle = down; break; + case SAPP_MOUSEBUTTON_RIGHT: input->mouse.right = down; break; + } +} + static void app_input(const sapp_event* event) { tic80_input* input = &platform.studio->tic->ram.input; @@ -319,19 +335,27 @@ static void app_input(const sapp_event* event) handleKeydown(event->key_code, false); break; case SAPP_EVENTTYPE_MOUSE_MOVE: - { - - } - // if(rect.w) x = (mx - rect.x) * TIC80_WIDTH / rect.w; - // if(rect.h) y = (my - rect.y) * TIC80_HEIGHT / rect.h; + struct {s32 x, y, w, h;}rect; + sokol_calc_viewport(&rect.x, &rect.y, &rect.w, &rect.h); - input->mouse.x = event->mouse_x >= 0 && event->mouse_x < 0xff ? event->mouse_x : 0xff; - input->mouse.y = event->mouse_y >= 0 && event->mouse_y < 0xff ? event->mouse_y : 0xff; + s32 mx = -1, my = -1; + if(rect.w) mx = ((s32)event->mouse_x - rect.x) * TIC80_FULLWIDTH / rect.w - TIC80_OFFSET_LEFT; + if(rect.h) my = ((s32)event->mouse_y - rect.y) * TIC80_FULLHEIGHT / rect.h - TIC80_OFFSET_TOP; + + input->mouse.x = mx >= 0 && mx < 0xff ? mx : 0xff; + input->mouse.y = my >= 0 && my < 0xff ? my : 0xff; + } break; - case SAPP_EVENTTYPE_MOUSE_DOWN: + case SAPP_EVENTTYPE_MOUSE_DOWN: + processMouse(event->mouse_button, 1); break; break; case SAPP_EVENTTYPE_MOUSE_UP: + processMouse(event->mouse_button, 0); break; + break; + case SAPP_EVENTTYPE_MOUSE_SCROLL: + input->mouse.scrollx = event->scroll_x; + input->mouse.scrolly = event->scroll_y; break; default: break; diff --git a/src/system/sokol.h b/src/system/sokol.h index 9d1944f..87c65b1 100644 --- a/src/system/sokol.h +++ b/src/system/sokol.h @@ -7,5 +7,6 @@ #include +void sokol_calc_viewport(int* x, int* y, int* w, int* h); void sokol_gfx_init(int w, int h, int sx, int sy, bool integer_scale, bool portrait_top_align); void sokol_gfx_draw(const uint32_t* ptr); diff --git a/src/system/sokol_gfx.c b/src/system/sokol_gfx.c index 9811de2..3d90328 100644 --- a/src/system/sokol_gfx.c +++ b/src/system/sokol_gfx.c @@ -198,7 +198,8 @@ static const sg_pass_action gfx_draw_pass_action = { .colors[0] = { .action = SG_ACTION_CLEAR, .val = { 0.05f, 0.05f, 0.05f, 1.0f } } }; -static void apply_viewport(void) { +void sokol_calc_viewport(int* x, int* y, int* w, int* h) +{ int vp_x = 0, vp_y = 0, vp_w = sapp_width(), vp_h = sapp_height(); if (vp_w * sokol_gfx.fb_height < vp_h * sokol_gfx.fb_width) @@ -226,6 +227,18 @@ static void apply_viewport(void) { vp_h = discreteHeight; } + *x = vp_x; + *y = vp_y; + *w = vp_w; + *h = vp_h; +} + + +static void apply_viewport(void) { + + int vp_x, vp_y, vp_w, vp_h; + sokol_calc_viewport(&vp_x, &vp_y, &vp_w, &vp_h); + sg_apply_viewport(vp_x, vp_y, vp_w, vp_h, true); }