Skip to content

Commit

Permalink
Add support for M5Stack Cardputer
Browse files Browse the repository at this point in the history
Related to #4
  • Loading branch information
vs4vijay committed Dec 10, 2024
1 parent 8fbec6c commit f1b1394
Show file tree
Hide file tree
Showing 2 changed files with 126 additions and 2 deletions.
3 changes: 1 addition & 2 deletions doomgeneric/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -51,4 +51,3 @@ $(OBJDIR)/%.o: %.c

print:
@echo OBJS: $(OBJS)

125 changes: 125 additions & 0 deletions doomgeneric/doomgeneric_m5card.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
#include "doomkeys.h"
#include "m_argv.h"
#include "doomgeneric.h"

#include <M5Unified.h>
#include <M5Cardputer.h>
#include <M5GFX.h>

#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;
}

0 comments on commit f1b1394

Please sign in to comment.