Skip to content

Commit

Permalink
Update main.cpp
Browse files Browse the repository at this point in the history
  • Loading branch information
vs4vijay authored Dec 10, 2024
1 parent f1b1394 commit 2e1f233
Showing 1 changed file with 64 additions and 50 deletions.
114 changes: 64 additions & 50 deletions src/main.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
// #include "doomgeneric.h"
#include "../doomgeneric/doomgeneric.h"
#include "../doomgeneric/m_argv.h"
#include "../doomgeneric/doomgeneric.h"
#include <M5Cardputer.h>
#include <M5Unified.h>
#include <M5GFX.h>
Expand All @@ -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)
Expand All @@ -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
Expand All @@ -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;
// }
return 0;
}

0 comments on commit 2e1f233

Please sign in to comment.