From f1b1394b019c6ed5127821994fcc59e6ef57f69c Mon Sep 17 00:00:00 2001 From: Vijay Soni Date: Tue, 10 Dec 2024 23:41:06 +0530 Subject: [PATCH 1/2] Add support for M5Stack Cardputer Related to #4 --- doomgeneric/Makefile | 3 +- doomgeneric/doomgeneric_m5card.c | 125 +++++++++++++++++++++++++++++++ 2 files changed, 126 insertions(+), 2 deletions(-) create mode 100644 doomgeneric/doomgeneric_m5card.c diff --git a/doomgeneric/Makefile b/doomgeneric/Makefile index 503e0dca2..1e2b26282 100644 --- a/doomgeneric/Makefile +++ b/doomgeneric/Makefile @@ -22,7 +22,7 @@ LIBS+=-lm -lc -lX11 OBJDIR=build OUTPUT=doomgeneric -SRC_DOOM = dummy.o am_map.o doomdef.o doomstat.o dstrings.o d_event.o d_items.o d_iwad.o d_loop.o d_main.o d_mode.o d_net.o f_finale.o f_wipe.o g_game.o hu_lib.o hu_stuff.o info.o i_cdmus.o i_endoom.o i_joystick.o i_scale.o i_sound.o i_system.o i_timer.o memio.o m_argv.o m_bbox.o m_cheat.o m_config.o m_controls.o m_fixed.o m_menu.o m_misc.o m_random.o p_ceilng.o p_doors.o p_enemy.o p_floor.o p_inter.o p_lights.o p_map.o p_maputl.o p_mobj.o p_plats.o p_pspr.o p_saveg.o p_setup.o p_sight.o p_spec.o p_switch.o p_telept.o p_tick.o p_user.o r_bsp.o r_data.o r_draw.o r_main.o r_plane.o r_segs.o r_sky.o r_things.o sha1.o sounds.o statdump.o st_lib.o st_stuff.o s_sound.o tables.o v_video.o wi_stuff.o w_checksum.o w_file.o w_main.o w_wad.o z_zone.o w_file_stdc.o i_input.o i_video.o doomgeneric.o doomgeneric_xlib.o +SRC_DOOM = dummy.o am_map.o doomdef.o doomstat.o dstrings.o d_event.o d_items.o d_iwad.o d_loop.o d_main.o d_mode.o d_net.o f_finale.o f_wipe.o g_game.o hu_lib.o hu_stuff.o info.o i_cdmus.o i_endoom.o i_joystick.o i_scale.o i_sound.o i_system.o i_timer.o memio.o m_argv.o m_bbox.o m_cheat.o m_config.o m_controls.o m_fixed.o m_menu.o m_misc.o m_random.o p_ceilng.o p_doors.o p_enemy.o p_floor.o p_inter.o p_lights.o p_map.o p_maputl.o p_mobj.o p_plats.o p_pspr.o p_saveg.o p_setup.o p_sight.o p_spec.o p_switch.o p_telept.o p_tick.o p_user.o r_bsp.o r_data.o r_draw.o r_main.o r_plane.o r_segs.o r_sky.o r_things.o sha1.o sounds.o statdump.o st_lib.o st_stuff.o s_sound.o tables.o v_video.o wi_stuff.o w_checksum.o w_file.o w_main.o w_wad.o z_zone.o w_file_stdc.o i_input.o i_video.o doomgeneric.o doomgeneric_xlib.o doomgeneric_m5card.o OBJS += $(addprefix $(OBJDIR)/, $(SRC_DOOM)) all: $(OUTPUT) @@ -51,4 +51,3 @@ $(OBJDIR)/%.o: %.c print: @echo OBJS: $(OBJS) - diff --git a/doomgeneric/doomgeneric_m5card.c b/doomgeneric/doomgeneric_m5card.c new file mode 100644 index 000000000..757e35656 --- /dev/null +++ b/doomgeneric/doomgeneric_m5card.c @@ -0,0 +1,125 @@ +#include "doomkeys.h" +#include "m_argv.h" +#include "doomgeneric.h" + +#include +#include +#include + +#define KEYQUEUE_SIZE 16 + +static unsigned short s_KeyQueue[KEYQUEUE_SIZE]; +static unsigned int s_KeyQueueWriteIndex = 0; +static unsigned int s_KeyQueueReadIndex = 0; + +static unsigned char convertToDoomKey(unsigned int key) +{ + switch (key) + { + case M5Cardputer::Key::Enter: + return KEY_ENTER; + case M5Cardputer::Key::Escape: + return KEY_ESCAPE; + case M5Cardputer::Key::Left: + return KEY_LEFTARROW; + case M5Cardputer::Key::Right: + return KEY_RIGHTARROW; + case M5Cardputer::Key::Up: + return KEY_UPARROW; + case M5Cardputer::Key::Down: + return KEY_DOWNARROW; + case M5Cardputer::Key::Ctrl: + return KEY_FIRE; + case M5Cardputer::Key::Space: + return KEY_USE; + case M5Cardputer::Key::Shift: + return KEY_RSHIFT; + default: + return tolower(key); + } +} + +static void addKeyToQueue(int pressed, unsigned int keyCode) +{ + unsigned char key = convertToDoomKey(keyCode); + unsigned short keyData = (pressed << 8) | key; + + s_KeyQueue[s_KeyQueueWriteIndex] = keyData; + s_KeyQueueWriteIndex++; + s_KeyQueueWriteIndex %= KEYQUEUE_SIZE; +} + +static void handleKeyInput() +{ + M5Cardputer::KeyEvent e; + while (M5Cardputer::pollEvent(e)) + { + if (e.type == M5Cardputer::KeyEvent::KeyDown) + { + addKeyToQueue(1, e.key); + } + else if (e.type == M5Cardputer::KeyEvent::KeyUp) + { + addKeyToQueue(0, e.key); + } + } +} + +void DG_Init() +{ + M5.begin(); + M5Cardputer::begin(); + M5GFX::begin(); +} + +void DG_DrawFrame() +{ + M5GFX::drawBitmap(0, 0, DOOMGENERIC_RESX, DOOMGENERIC_RESY, DG_ScreenBuffer); + handleKeyInput(); +} + +void DG_SleepMs(uint32_t ms) +{ + delay(ms); +} + +uint32_t DG_GetTicksMs() +{ + return millis(); +} + +int DG_GetKey(int *pressed, unsigned char *doomKey) +{ + if (s_KeyQueueReadIndex == s_KeyQueueWriteIndex) + { + return 0; + } + else + { + unsigned short keyData = s_KeyQueue[s_KeyQueueReadIndex]; + s_KeyQueueReadIndex++; + s_KeyQueueReadIndex %= KEYQUEUE_SIZE; + + *pressed = keyData >> 8; + *doomKey = keyData & 0xFF; + + return 1; + } +} + +void DG_SetWindowTitle(const char *title) +{ + // No window title to set for M5Stack Cardputer +} + +int main(int argc, char **argv) +{ + doomgeneric_Create(argc, argv); + + for (;;) + { + doomgeneric_Tick(); + } + + return 0; +} From 2e1f2334efca0b504d0a2d7234f193bcea62060b Mon Sep 17 00:00:00 2001 From: Vijay Soni Date: Tue, 10 Dec 2024 23:50:59 +0530 Subject: [PATCH 2/2] Update main.cpp --- src/main.cpp | 114 +++++++++++++++++++++++++++++---------------------- 1 file changed, 64 insertions(+), 50 deletions(-) diff --git a/src/main.cpp b/src/main.cpp index 9afdc24db..de126f93f 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -1,5 +1,7 @@ // #include "doomgeneric.h" #include "../doomgeneric/doomgeneric.h" +#include "../doomgeneric/m_argv.h" +#include "../doomgeneric/doomgeneric.h" #include #include #include @@ -10,27 +12,70 @@ static unsigned short s_KeyQueue[KEYQUEUE_SIZE]; static unsigned int s_KeyQueueWriteIndex = 0; static unsigned int s_KeyQueueReadIndex = 0; -#ifdef __cplusplus -extern "C" +static unsigned char convertToDoomKey(unsigned int key) +{ + switch (key) + { + case M5Cardputer::Key::Enter: + return KEY_ENTER; + case M5Cardputer::Key::Escape: + return KEY_ESCAPE; + case M5Cardputer::Key::Left: + return KEY_LEFTARROW; + case M5Cardputer::Key::Right: + return KEY_RIGHTARROW; + case M5Cardputer::Key::Up: + return KEY_UPARROW; + case M5Cardputer::Key::Down: + return KEY_DOWNARROW; + case M5Cardputer::Key::Ctrl: + return KEY_FIRE; + case M5Cardputer::Key::Space: + return KEY_USE; + case M5Cardputer::Key::Shift: + return KEY_RSHIFT; + default: + return tolower(key); + } +} + +static void addKeyToQueue(int pressed, unsigned int keyCode) { -#endif + unsigned char key = convertToDoomKey(keyCode); + unsigned short keyData = (pressed << 8) | key; - void doomgeneric_Create(int argc, char **argv); - void doomgeneric_Tick(); + s_KeyQueue[s_KeyQueueWriteIndex] = keyData; + s_KeyQueueWriteIndex++; + s_KeyQueueWriteIndex %= KEYQUEUE_SIZE; +} -#ifdef __cplusplus +static void handleKeyInput() +{ + M5Cardputer::KeyEvent e; + while (M5Cardputer::pollEvent(e)) + { + if (e.type == M5Cardputer::KeyEvent::KeyDown) + { + addKeyToQueue(1, e.key); + } + else if (e.type == M5Cardputer::KeyEvent::KeyUp) + { + addKeyToQueue(0, e.key); + } + } } -#endif void DG_Init() { M5.begin(); - // Initialize M5Stack Cardputer + M5Cardputer::begin(); + M5GFX::begin(); } void DG_DrawFrame() { - M5.Lcd.drawBitmap(0, 0, DOOMGENERIC_RESX, DOOMGENERIC_RESY, (uint16_t *)DG_ScreenBuffer); + M5GFX::drawBitmap(0, 0, DOOMGENERIC_RESX, DOOMGENERIC_RESY, DG_ScreenBuffer); + handleKeyInput(); } void DG_SleepMs(uint32_t ms) @@ -43,34 +88,10 @@ uint32_t DG_GetTicksMs() return millis(); } -// int DG_GetKey(int* pressed, unsigned char* key) { -// *pressed = 0; -// *key = 0; - -// if(M5.BtnA.wasPressed()) { -// *pressed = 1; -// *key = KEY_ENTER; -// return 1; -// } -// else if(M5.BtnB.wasPressed()) { -// *pressed = 1; -// *key = KEY_FIRE; -// return 1; -// } -// else if(M5.BtnC.wasPressed()) { -// *pressed = 1; -// *key = KEY_USE; -// return 1; -// } - -// return 0; -// } - int DG_GetKey(int *pressed, unsigned char *doomKey) { if (s_KeyQueueReadIndex == s_KeyQueueWriteIndex) { - // key queue is empty return 0; } else @@ -84,28 +105,21 @@ int DG_GetKey(int *pressed, unsigned char *doomKey) return 1; } - - return 0; } -void setup() +void DG_SetWindowTitle(const char *title) { - doomgeneric_Create(0, nullptr); + // No window title to set for M5Stack Cardputer } -void loop() +int main(int argc, char **argv) { - doomgeneric_Tick(); -} - -// int main(int argc, char **argv) -// { -// doomgeneric_Create(argc, argv); + doomgeneric_Create(argc, argv); -// while (1) -// { -// doomgeneric_Tick(); -// } + for (;;) + { + doomgeneric_Tick(); + } -// return 0; -// } \ No newline at end of file + return 0; +}