From 29ab09efd08c516ba885c632549356bc5c62c0a6 Mon Sep 17 00:00:00 2001 From: "BADIM-PC\\Vadim" Date: Wed, 24 Jan 2018 16:13:46 +0300 Subject: [PATCH] keyboard input works --- include/tic80.h | 4 ++-- src/console.c | 26 +++++++++++++++++++++++--- src/main.c | 41 +++++++++++++++++++++++++++++------------ src/studio.c | 26 +++++++++++++------------- src/tic.c | 23 ++++++++++++++++++++--- src/ticapi.h | 1 + 6 files changed, 88 insertions(+), 33 deletions(-) diff --git a/include/tic80.h b/include/tic80.h index 5054869..383e1f9 100644 --- a/include/tic80.h +++ b/include/tic80.h @@ -34,7 +34,7 @@ extern "C" { #define TIC80_FULLWIDTH_BITS 8 #define TIC80_FULLWIDTH (1 << TIC80_FULLWIDTH_BITS) #define TIC80_FULLHEIGHT (TIC80_FULLWIDTH*9/16) -#define TIC_KEY_BUFFER 4 +#define TIC80_KEY_BUFFER 4 typedef struct { @@ -109,7 +109,7 @@ typedef u8 tic_key; typedef union { - tic_key keys[TIC_KEY_BUFFER]; + tic_key keys[TIC80_KEY_BUFFER]; u32 data; } tic80_keyboard; diff --git a/src/console.c b/src/console.c index 0458bc5..bd5297f 100644 --- a/src/console.c +++ b/src/console.c @@ -2613,6 +2613,26 @@ static void tick(Console* console) // } // } + tic_mem* tic = console->tic; + + char sym = tic->api.keytext(tic); + + if(sym) + { + size_t size = strlen(console->inputBuffer); + + if(size < sizeof(console->inputBuffer)) + { + char* pos = console->inputBuffer + console->inputPosition; + memmove(pos + 1, pos, strlen(pos)); + + *(console->inputBuffer + console->inputPosition) = sym; + console->inputPosition++; + } + + console->cursor.delay = CONSOLE_CURSOR_DELAY; + } + processGesture(console); if(console->tickCounter == 0) @@ -2633,7 +2653,7 @@ static void tick(Console* console) else printBack(console, "\n loading cart..."); } - console->tic->api.clear(console->tic, TIC_COLOR_BG); + tic->api.clear(tic, TIC_COLOR_BG); drawConsoleText(console); if(console->embed.yes) @@ -2643,13 +2663,13 @@ static void tick(Console* console) if(!console->skipStart) console->showGameMenu = true; - memcpy(&console->tic->cart, console->embed.file, sizeof(tic_cartridge)); + memcpy(&tic->cart, console->embed.file, sizeof(tic_cartridge)); setStudioMode(TIC_RUN_MODE); console->embed.yes = false; console->skipStart = false; studioRomLoaded(); - console->tic->api.reset(console->tic); + tic->api.reset(tic); printLine(console); commandDone(console); diff --git a/src/main.c b/src/main.c index 331ab62..bb1924d 100644 --- a/src/main.c +++ b/src/main.c @@ -189,22 +189,22 @@ static void processMouse() s32 mx = 0, my = 0; s32 mb = SDL_GetMouseState(&mx, &my); - tic_mem* tic = platform.studio->tic; + tic80_input* input = &platform.studio->tic->ram.input; { - tic->ram.input.mouse.x = tic->ram.input.mouse.y = 0; + input->mouse.x = input->mouse.y = 0; SDL_Rect rect = {0, 0, 0, 0}; calcTextureRect(&rect); - if(rect.w) tic->ram.input.mouse.x = (mx - rect.x) * TIC80_WIDTH / rect.w; - if(rect.h) tic->ram.input.mouse.y = (my - rect.y) * TIC80_HEIGHT / rect.h; + if(rect.w) input->mouse.x = (mx - rect.x) * TIC80_WIDTH / rect.w; + if(rect.h) input->mouse.y = (my - rect.y) * TIC80_HEIGHT / rect.h; } { - tic->ram.input.mouse.left = mb & SDL_BUTTON_LMASK; - tic->ram.input.mouse.middle = mb & SDL_BUTTON_MMASK; - tic->ram.input.mouse.right = mb & SDL_BUTTON_RMASK; + input->mouse.left = mb & SDL_BUTTON_LMASK; + input->mouse.middle = mb & SDL_BUTTON_MMASK; + input->mouse.right = mb & SDL_BUTTON_RMASK; } // for(int i = 0; i < COUNT_OF(studioImpl.mouse.state); i++) @@ -229,9 +229,26 @@ static void processMouse() // } } -static SDL_Event* pollEvent() +static void processKeyboard() { - static SDL_Event event; + static const u8 KeyboardCodes[] = + { + #include "keycodes.c" + }; + + tic80_input* input = &platform.studio->tic->ram.input; + input->keyboard.data = 0; + + const u8* keyboard = SDL_GetKeyboardState(NULL); + + for(s32 i = 0, c = 0; i < COUNT_OF(KeyboardCodes) && c < COUNT_OF(input->keyboard.keys); i++) + if(keyboard[i] && KeyboardCodes[i] > tic_key_unknown) + input->keyboard.keys[c++] = KeyboardCodes[i]; +} + +static void pollEvent() +{ + SDL_Event event; if(SDL_PollEvent(&event)) { @@ -326,7 +343,9 @@ static SDL_Event* pollEvent() // processGesture(); // if(!platform.gesture.active) - processMouse(); + + processMouse(); + processKeyboard(); // if(platform.mode == TIC_RUN_MODE) // { @@ -338,8 +357,6 @@ static SDL_Event* pollEvent() // { // processGamepadInput(); // } - - return NULL; } static void blitTexture() diff --git a/src/studio.c b/src/studio.c index 93765c2..6148af1 100644 --- a/src/studio.c +++ b/src/studio.c @@ -1915,22 +1915,22 @@ static void processGamepadInput() // mouse->right = studioImpl.mouse.state[2].down ? 1 : 0; // } -static void processKeyboardInput() -{ - static const u8 KeyboardCodes[] = - { - #include "keycodes.c" - }; +// static void processKeyboardInput() +// { +// static const u8 KeyboardCodes[] = +// { +// #include "keycodes.c" +// }; - tic80_input* input = &studioImpl.studio.tic->ram.input; - input->keyboard.data = 0; +// tic80_input* input = &studioImpl.studio.tic->ram.input; +// input->keyboard.data = 0; - studioImpl.keyboard = SDL_GetKeyboardState(NULL); +// studioImpl.keyboard = SDL_GetKeyboardState(NULL); - for(s32 i = 0, c = 0; i < COUNT_OF(KeyboardCodes) && c < COUNT_OF(input->keyboard.keys); i++) - if(studioImpl.keyboard[i] && KeyboardCodes[i] > tic_key_unknown) - input->keyboard.keys[c++] = KeyboardCodes[i]; -} +// for(s32 i = 0, c = 0; i < COUNT_OF(KeyboardCodes) && c < COUNT_OF(input->keyboard.keys); i++) +// if(studioImpl.keyboard[i] && KeyboardCodes[i] > tic_key_unknown) +// input->keyboard.keys[c++] = KeyboardCodes[i]; +// } #if defined(TIC80_PRO) diff --git a/src/tic.c b/src/tic.c index 852ea31..77e56bb 100644 --- a/src/tic.c +++ b/src/tic.c @@ -1263,7 +1263,7 @@ static bool isNoiseWaveform(const tic_waveform* wave) static bool isKeyPressed(const tic80_keyboard* input, tic_key key) { - for(s32 i = 0; i < TIC_KEY_BUFFER; i++) + for(s32 i = 0; i < TIC80_KEY_BUFFER; i++) if(input->keys[i] == key) return true; @@ -1689,7 +1689,7 @@ static bool api_keyp(tic_mem* tic, tic_key key, s32 hold, s32 period) return !prevDown && down; } - for(s32 i = 0; i < TIC_KEY_BUFFER; i++) + for(s32 i = 0; i < TIC80_KEY_BUFFER; i++) { tic_key key = tic->ram.input.keyboard.keys[i]; @@ -1697,7 +1697,7 @@ static bool api_keyp(tic_mem* tic, tic_key key, s32 hold, s32 period) { bool wasPressed = false; - for(s32 p = 0; p < TIC_KEY_BUFFER; p++) + for(s32 p = 0; p < TIC80_KEY_BUFFER; p++) { if(machine->state.keyboard.previous.keys[p] == key) { @@ -1714,7 +1714,23 @@ static bool api_keyp(tic_mem* tic, tic_key key, s32 hold, s32 period) return false; } +static char api_keytext(tic_mem* tic) +{ + static const char Symbols[] = "abcdefghijklmnopqrstuvwxyz0123456789-=[]\\;'`,./ "; + static const char Shift[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ)!@#$%^&*(_+{}|:\"~<>? "; + enum{Count = sizeof Symbols, Hold = 20, Period = 3}; + + for(s32 i = 0; i < TIC80_KEY_BUFFER; i++) + { + tic_key key = tic->ram.input.keyboard.keys[i]; + + if(key > 0 && key < Count && tic->api.keyp(tic, key, Hold, Period)) + return tic->api.key(tic, tic_key_shift) ? Shift[key-1] : Symbols[key-1]; + } + + return 0; +} static void api_load(tic_cartridge* cart, const u8* buffer, s32 size, bool palette) { const u8* end = buffer + size; @@ -1944,6 +1960,7 @@ static void initApi(tic_api* api) INIT_API(btnp); INIT_API(key); INIT_API(keyp); + INIT_API(keytext); INIT_API(load); INIT_API(save); INIT_API(tick_start); diff --git a/src/ticapi.h b/src/ticapi.h index 4fc66f4..f6ae5eb 100644 --- a/src/ticapi.h +++ b/src/ticapi.h @@ -156,6 +156,7 @@ typedef struct u32 (*btnp) (tic_mem* memory, s32 id, s32 hold, s32 period); bool (*key) (tic_mem* memory, tic_key key); bool (*keyp) (tic_mem* memory, tic_key key, s32 hold, s32 period); + char (*keytext) (tic_mem* memory); void (*load) (tic_cartridge* rom, const u8* buffer, s32 size, bool palette); s32 (*save) (const tic_cartridge* rom, u8* buffer);