From acbacb574e016d3d0966edd80d25b0baaf6e2359 Mon Sep 17 00:00:00 2001 From: "BADIM-PC\\Vadim" Date: Fri, 29 Dec 2017 15:25:29 +0300 Subject: [PATCH] no message --- include/tic80/tic80.h | 5 +- src/keycodes.c | 227 ++++++++++++++++++++++++++++++++++++++++++ src/luaapi.c | 99 +++++++++++++++++- src/machine.h | 7 +- src/studio.c | 14 +++ src/tic.c | 1 + src/tic.h | 81 +++++++++++++++ 7 files changed, 430 insertions(+), 4 deletions(-) create mode 100644 src/keycodes.c diff --git a/include/tic80/tic80.h b/include/tic80/tic80.h index 65989db..430e979 100644 --- a/include/tic80/tic80.h +++ b/include/tic80/tic80.h @@ -104,9 +104,10 @@ typedef struct }; } tic80_mouse; -typedef struct +typedef union { - u8 data[4]; + u8 keys[4]; + u32 data; } tic80_keyboard; typedef struct diff --git a/src/keycodes.c b/src/keycodes.c new file mode 100644 index 0000000..cb0430f --- /dev/null +++ b/src/keycodes.c @@ -0,0 +1,227 @@ +[0] = UnkCode, +[1] = UnkCode, +[2] = UnkCode, +[3] = UnkCode, +[SDL_SCANCODE_A] = tic_key_a, +[SDL_SCANCODE_B] = tic_key_b, +[SDL_SCANCODE_C] = tic_key_c, +[SDL_SCANCODE_D] = tic_key_d, +[SDL_SCANCODE_E] = tic_key_e, +[SDL_SCANCODE_F] = tic_key_f, +[SDL_SCANCODE_G] = tic_key_g, +[SDL_SCANCODE_H] = tic_key_h, +[SDL_SCANCODE_I] = tic_key_i, +[SDL_SCANCODE_J] = tic_key_j, +[SDL_SCANCODE_K] = tic_key_k, +[SDL_SCANCODE_L] = tic_key_l, +[SDL_SCANCODE_M] = tic_key_m, +[SDL_SCANCODE_N] = tic_key_n, +[SDL_SCANCODE_O] = tic_key_o, +[SDL_SCANCODE_P] = tic_key_p, +[SDL_SCANCODE_Q] = tic_key_q, +[SDL_SCANCODE_R] = tic_key_r, +[SDL_SCANCODE_S] = tic_key_s, +[SDL_SCANCODE_T] = tic_key_t, +[SDL_SCANCODE_U] = tic_key_u, +[SDL_SCANCODE_V] = tic_key_v, +[SDL_SCANCODE_W] = tic_key_w, +[SDL_SCANCODE_X] = tic_key_x, +[SDL_SCANCODE_Y] = tic_key_y, +[SDL_SCANCODE_Z] = tic_key_z, +[SDL_SCANCODE_1] = tic_key_1, +[SDL_SCANCODE_2] = tic_key_2, +[SDL_SCANCODE_3] = tic_key_3, +[SDL_SCANCODE_4] = tic_key_4, +[SDL_SCANCODE_5] = tic_key_5, +[SDL_SCANCODE_6] = tic_key_6, +[SDL_SCANCODE_7] = tic_key_7, +[SDL_SCANCODE_8] = tic_key_8, +[SDL_SCANCODE_9] = tic_key_9, +[SDL_SCANCODE_0] = tic_key_0, +[SDL_SCANCODE_RETURN] = tic_key_return, +[SDL_SCANCODE_ESCAPE] = tic_key_escape, +[SDL_SCANCODE_BACKSPACE] = tic_key_backspace, +[SDL_SCANCODE_TAB] = tic_key_tab, +[SDL_SCANCODE_SPACE] = tic_key_space, +[SDL_SCANCODE_MINUS] = tic_key_minus, +[SDL_SCANCODE_EQUALS] = tic_key_equals, +[SDL_SCANCODE_LEFTBRACKET] = tic_key_leftbracket, +[SDL_SCANCODE_RIGHTBRACKET] = tic_key_rightbracket, +[SDL_SCANCODE_BACKSLASH] = tic_key_backslash, +[50] = UnkCode, +[SDL_SCANCODE_SEMICOLON] = tic_key_semicolon, +[SDL_SCANCODE_APOSTROPHE] = tic_key_apostrophe, +[SDL_SCANCODE_GRAVE] = tic_key_grave, +[SDL_SCANCODE_COMMA] = tic_key_comma, +[SDL_SCANCODE_PERIOD] = tic_key_period, +[SDL_SCANCODE_SLASH] = tic_key_slash, +[SDL_SCANCODE_CAPSLOCK] = tic_key_capslock, +[SDL_SCANCODE_F1] = tic_key_f1, +[SDL_SCANCODE_F2] = tic_key_f2, +[SDL_SCANCODE_F3] = tic_key_f3, +[SDL_SCANCODE_F4] = tic_key_f4, +[SDL_SCANCODE_F5] = tic_key_f5, +[SDL_SCANCODE_F6] = tic_key_f6, +[SDL_SCANCODE_F7] = tic_key_f7, +[SDL_SCANCODE_F8] = tic_key_f8, +[SDL_SCANCODE_F9] = tic_key_f9, +[SDL_SCANCODE_F10] = tic_key_f10, +[SDL_SCANCODE_F11] = tic_key_f11, +[SDL_SCANCODE_F12] = tic_key_f12, +[70] = UnkCode, +[71] = UnkCode, +[72] = UnkCode, +[SDL_SCANCODE_INSERT] = tic_key_insert, +[SDL_SCANCODE_HOME] = tic_key_home, +[SDL_SCANCODE_PAGEUP] = tic_key_pageup, +[SDL_SCANCODE_DELETE] = tic_key_delete, +[SDL_SCANCODE_END] = tic_key_end, +[SDL_SCANCODE_PAGEDOWN] = tic_key_pagedown, +[SDL_SCANCODE_RIGHT] = tic_key_right, +[SDL_SCANCODE_LEFT] = tic_key_left, +[SDL_SCANCODE_DOWN] = tic_key_down, +[SDL_SCANCODE_UP] = tic_key_up, +[83] = UnkCode, +[84] = UnkCode, +[85] = UnkCode, +[86] = UnkCode, +[87] = UnkCode, +[88] = UnkCode, +[89] = UnkCode, +[90] = UnkCode, +[91] = UnkCode, +[92] = UnkCode, +[93] = UnkCode, +[94] = UnkCode, +[95] = UnkCode, +[96] = UnkCode, +[97] = UnkCode, +[98] = UnkCode, +[99] = UnkCode, +[100] = UnkCode, +[101] = UnkCode, +[102] = UnkCode, +[103] = UnkCode, +[104] = UnkCode, +[105] = UnkCode, +[106] = UnkCode, +[107] = UnkCode, +[108] = UnkCode, +[109] = UnkCode, +[110] = UnkCode, +[111] = UnkCode, +[112] = UnkCode, +[113] = UnkCode, +[114] = UnkCode, +[115] = UnkCode, +[116] = UnkCode, +[117] = UnkCode, +[118] = UnkCode, +[119] = UnkCode, +[120] = UnkCode, +[121] = UnkCode, +[122] = UnkCode, +[123] = UnkCode, +[124] = UnkCode, +[125] = UnkCode, +[126] = UnkCode, +[127] = UnkCode, +[128] = UnkCode, +[129] = UnkCode, +[130] = UnkCode, +[131] = UnkCode, +[132] = UnkCode, +[133] = UnkCode, +[134] = UnkCode, +[135] = UnkCode, +[136] = UnkCode, +[137] = UnkCode, +[138] = UnkCode, +[139] = UnkCode, +[140] = UnkCode, +[141] = UnkCode, +[142] = UnkCode, +[143] = UnkCode, +[144] = UnkCode, +[145] = UnkCode, +[146] = UnkCode, +[147] = UnkCode, +[148] = UnkCode, +[149] = UnkCode, +[150] = UnkCode, +[151] = UnkCode, +[152] = UnkCode, +[153] = UnkCode, +[154] = UnkCode, +[155] = UnkCode, +[156] = UnkCode, +[157] = UnkCode, +[158] = UnkCode, +[159] = UnkCode, +[160] = UnkCode, +[161] = UnkCode, +[162] = UnkCode, +[163] = UnkCode, +[164] = UnkCode, +[165] = UnkCode, +[166] = UnkCode, +[167] = UnkCode, +[168] = UnkCode, +[169] = UnkCode, +[170] = UnkCode, +[171] = UnkCode, +[172] = UnkCode, +[173] = UnkCode, +[174] = UnkCode, +[175] = UnkCode, +[176] = UnkCode, +[177] = UnkCode, +[178] = UnkCode, +[179] = UnkCode, +[180] = UnkCode, +[181] = UnkCode, +[182] = UnkCode, +[183] = UnkCode, +[184] = UnkCode, +[185] = UnkCode, +[186] = UnkCode, +[187] = UnkCode, +[188] = UnkCode, +[189] = UnkCode, +[190] = UnkCode, +[191] = UnkCode, +[192] = UnkCode, +[193] = UnkCode, +[194] = UnkCode, +[195] = UnkCode, +[196] = UnkCode, +[197] = UnkCode, +[198] = UnkCode, +[199] = UnkCode, +[200] = UnkCode, +[201] = UnkCode, +[202] = UnkCode, +[203] = UnkCode, +[204] = UnkCode, +[205] = UnkCode, +[206] = UnkCode, +[207] = UnkCode, +[208] = UnkCode, +[209] = UnkCode, +[210] = UnkCode, +[211] = UnkCode, +[212] = UnkCode, +[213] = UnkCode, +[214] = UnkCode, +[215] = UnkCode, +[216] = UnkCode, +[217] = UnkCode, +[218] = UnkCode, +[219] = UnkCode, +[220] = UnkCode, +[221] = UnkCode, +[222] = UnkCode, +[223] = UnkCode, +[SDL_SCANCODE_LCTRL] = tic_key_ctrl, +[SDL_SCANCODE_LSHIFT] = tic_key_shift, +[SDL_SCANCODE_LALT] = tic_key_alt, \ No newline at end of file diff --git a/src/luaapi.c b/src/luaapi.c index b8625e3..ba035b3 100644 --- a/src/luaapi.c +++ b/src/luaapi.c @@ -784,6 +784,102 @@ static s32 lua_reset(lua_State* lua) return 0; } +static s32 lua_key(lua_State* lua) +{ + tic_machine* machine = getLuaMachine(lua); + + s32 top = lua_gettop(lua); + + tic80_input* input = &machine->memory.ram.input; + + if (top == 1) + { + u8 index = getLuaNumber(lua, 1); + + if(index != 0xff) + { + for(s32 i = 0; i < COUNT_OF(input->keyboard.keys); i++) + { + if(input->keyboard.keys[i] == index) + { + lua_pushboolean(lua, true); + return 1; + } + } + } + + lua_pushboolean(lua, false); + return 1; + } + else luaL_error(lua, "invalid params, key ( code )\n"); + + return 0; +} + +static s32 lua_keyp(lua_State* lua) +{ + tic_machine* machine = getLuaMachine(lua); + + s32 top = lua_gettop(lua); + tic80_input* input = &machine->memory.ram.input; + + if (top == 0) + { + for(s32 i = 0; i < COUNT_OF(input->keyboard.keys); i++) + { + s32 code = input->keyboard.keys[i]; + + bool found = false; + + for(s32 p = 0; p < COUNT_OF(machine->state.keyboard.previous.keys); p++) + { + if(machine->state.keyboard.previous.keys[p] == code) + { + found = true; + break; + } + } + + if(!found) + { + lua_pushinteger(lua, code); + return 1; + } + } + } + else if(top == 1) + { + { + s32 code = getLuaNumber(lua, 1); + + if(code > 0) + { + for(s32 i = 0; i < COUNT_OF(input->keyboard.keys); i++) + { + if(input->keyboard.keys[i] == code) + { + for(s32 p = 0; p < COUNT_OF(machine->state.keyboard.previous.keys); p++) + { + if(machine->state.keyboard.previous.keys[p] == code) + { + lua_pushboolean(lua, true); + return 1; + } + } + } + } + + } + + lua_pushboolean(lua, false); + return 1; + } + } + else luaL_error(lua, "invalid params, keyp [ code ]\n"); + + return 0; +} + static s32 lua_memcpy(lua_State* lua) { s32 top = lua_gettop(lua); @@ -1087,7 +1183,8 @@ static const lua_CFunction ApiFunc[] = lua_rectb, lua_spr, lua_btn, lua_btnp, lua_sfx, lua_map, lua_mget, lua_mset, lua_peek, lua_poke, lua_peek4, lua_poke4, lua_memcpy, lua_memset, lua_trace, lua_pmem, lua_time, lua_exit, lua_font, lua_mouse, - lua_circ, lua_circb, lua_tri, lua_textri, lua_clip, lua_music, lua_sync, lua_reset + lua_circ, lua_circb, lua_tri, lua_textri, lua_clip, lua_music, lua_sync, lua_reset, + lua_key, lua_keyp }; STATIC_ASSERT(api_func, COUNT_OF(ApiKeywords) == COUNT_OF(ApiFunc)); diff --git a/src/machine.h b/src/machine.h index 4ad3891..22a1112 100644 --- a/src/machine.h +++ b/src/machine.h @@ -31,7 +31,7 @@ #define API_KEYWORDS {"TIC", "scanline", "OVR", "print", "cls", "pix", "line", "rect", "rectb", \ "spr", "btn", "btnp", "sfx", "map", "mget", "mset", "peek", "poke", "peek4", "poke4", \ "memcpy", "memset", "trace", "pmem", "time", "exit", "font", "mouse", "circ", "circb", "tri", "textri", \ - "clip", "music", "sync", "reset"} + "clip", "music", "sync", "reset", "key", "keyp"} typedef struct { @@ -69,6 +69,11 @@ typedef struct u32 holds[sizeof(tic80_gamepads) * BITS_IN_BYTE]; } gamepads; + struct + { + tic80_keyboard previous; + } keyboard; + Clip clip; tic_sound_register_data registers[TIC_SOUND_CHANNELS]; diff --git a/src/studio.c b/src/studio.c index 9caff94..c13fdbb 100644 --- a/src/studio.c +++ b/src/studio.c @@ -1904,7 +1904,21 @@ static void processMouseInput() static void processKeyboardInput() { + enum{UnkCode = 0xff}; + static const u8 KeyboardCodes[] = + { + #include "keycodes.c" + }; + + tic80_input* input = &studio.tic->ram.input; + input->keyboard.data = SDL_FOURCC(UnkCode, UnkCode, UnkCode, UnkCode); + + studio.keyboard = SDL_GetKeyboardState(NULL); + + for(s32 i = 0, c = 0; i < COUNT_OF(KeyboardCodes) && c < COUNT_OF(input->keyboard.keys); i++) + if(studio.keyboard[i] && KeyboardCodes[i] != UnkCode) + input->keyboard.keys[c++] = KeyboardCodes[i]; } #if defined(TIC80_PRO) diff --git a/src/tic.c b/src/tic.c index bc05039..500888e 100644 --- a/src/tic.c +++ b/src/tic.c @@ -1287,6 +1287,7 @@ static void api_tick_end(tic_mem* memory) tic_machine* machine = (tic_machine*)memory; machine->state.gamepads.previous.data = machine->memory.ram.input.gamepads.data; + machine->state.keyboard.previous.data = machine->memory.ram.input.keyboard.data; enum {EndTime = CLOCKRATE / TIC_FRAMERATE}; for (s32 i = 0; i < TIC_SOUND_CHANNELS; ++i ) diff --git a/src/tic.h b/src/tic.h index 428f076..baa23a7 100644 --- a/src/tic.h +++ b/src/tic.h @@ -417,3 +417,84 @@ typedef union u8 data[TIC_RAM_SIZE]; } tic_ram; +enum +{ + tic_key_up, + tic_key_down, + tic_key_left, + tic_key_right, + tic_key_a, + tic_key_b, + tic_key_c, + tic_key_d, + tic_key_e, + tic_key_f, + tic_key_g, + tic_key_h, + tic_key_i, + tic_key_j, + tic_key_k, + tic_key_l, + tic_key_m, + tic_key_n, + tic_key_o, + tic_key_p, + tic_key_q, + tic_key_r, + tic_key_s, + tic_key_t, + tic_key_u, + tic_key_v, + tic_key_w, + tic_key_x, + tic_key_y, + tic_key_z, + tic_key_0, + tic_key_1, + tic_key_2, + tic_key_3, + tic_key_4, + tic_key_5, + tic_key_6, + tic_key_7, + tic_key_8, + tic_key_9, + tic_key_return, + tic_key_escape, + tic_key_backspace, + tic_key_tab, + tic_key_space, + tic_key_minus, + tic_key_equals, + tic_key_leftbracket, + tic_key_rightbracket, + tic_key_backslash, + tic_key_semicolon, + tic_key_apostrophe, + tic_key_grave, + tic_key_comma, + tic_key_period, + tic_key_slash, + tic_key_capslock, + tic_key_f1, + tic_key_f2, + tic_key_f3, + tic_key_f4, + tic_key_f5, + tic_key_f6, + tic_key_f7, + tic_key_f8, + tic_key_f9, + tic_key_f10, + tic_key_f11, + tic_key_f12, + tic_key_insert, + tic_key_home, + tic_key_pageup, + tic_key_delete, + tic_key_end, + tic_key_pagedown, + tic_key_ctrl, + tic_key_shift, + tic_key_alt +};