|
| 1 | +//doomgeneric for soso os |
| 2 | + |
| 3 | +#include "doomkeys.h" |
| 4 | +#include "m_argv.h" |
| 5 | +#include "doomgeneric.h" |
| 6 | + |
| 7 | +#include <stdio.h> |
| 8 | +#include <unistd.h> |
| 9 | + |
| 10 | +#include <stdbool.h> |
| 11 | +#include <SDL.h> |
| 12 | + |
| 13 | +SDL_Window* window = NULL; |
| 14 | +SDL_Renderer* renderer = NULL; |
| 15 | +SDL_Texture* texture; |
| 16 | + |
| 17 | +#define KEYQUEUE_SIZE 16 |
| 18 | + |
| 19 | +static unsigned short s_KeyQueue[KEYQUEUE_SIZE]; |
| 20 | +static unsigned int s_KeyQueueWriteIndex = 0; |
| 21 | +static unsigned int s_KeyQueueReadIndex = 0; |
| 22 | + |
| 23 | +static unsigned char convertToDoomKey(unsigned int key){ |
| 24 | + switch (key) |
| 25 | + { |
| 26 | + case SDLK_RETURN: |
| 27 | + key = KEY_ENTER; |
| 28 | + break; |
| 29 | + case SDLK_ESCAPE: |
| 30 | + key = KEY_ESCAPE; |
| 31 | + break; |
| 32 | + case SDLK_LEFT: |
| 33 | + key = KEY_LEFTARROW; |
| 34 | + break; |
| 35 | + case SDLK_RIGHT: |
| 36 | + key = KEY_RIGHTARROW; |
| 37 | + break; |
| 38 | + case SDLK_UP: |
| 39 | + key = KEY_UPARROW; |
| 40 | + break; |
| 41 | + case SDLK_DOWN: |
| 42 | + key = KEY_DOWNARROW; |
| 43 | + break; |
| 44 | + case SDLK_LCTRL: |
| 45 | + case SDLK_RCTRL: |
| 46 | + key = KEY_FIRE; |
| 47 | + break; |
| 48 | + case SDLK_SPACE: |
| 49 | + key = KEY_USE; |
| 50 | + break; |
| 51 | + case SDLK_LSHIFT: |
| 52 | + case SDLK_RSHIFT: |
| 53 | + key = KEY_RSHIFT; |
| 54 | + break; |
| 55 | + default: |
| 56 | + key = tolower(key); |
| 57 | + break; |
| 58 | + } |
| 59 | + |
| 60 | + return key; |
| 61 | +} |
| 62 | + |
| 63 | +static void addKeyToQueue(int pressed, unsigned int keyCode){ |
| 64 | + unsigned char key = convertToDoomKey(keyCode); |
| 65 | + |
| 66 | + unsigned short keyData = (pressed << 8) | key; |
| 67 | + |
| 68 | + s_KeyQueue[s_KeyQueueWriteIndex] = keyData; |
| 69 | + s_KeyQueueWriteIndex++; |
| 70 | + s_KeyQueueWriteIndex %= KEYQUEUE_SIZE; |
| 71 | +} |
| 72 | +static void handleKeyInput(){ |
| 73 | + SDL_Event e; |
| 74 | + while (SDL_PollEvent(&e)){ |
| 75 | + if (e.type == SDL_QUIT){ |
| 76 | + puts("Quit requested"); |
| 77 | + atexit(SDL_Quit); |
| 78 | + exit(1); |
| 79 | + } |
| 80 | + if (e.type == SDL_KEYDOWN) { |
| 81 | + //KeySym sym = XKeycodeToKeysym(s_Display, e.xkey.keycode, 0); |
| 82 | + //printf("KeyPress:%d sym:%d\n", e.xkey.keycode, sym); |
| 83 | + addKeyToQueue(1, e.key.keysym.sym); |
| 84 | + } else if (e.type == SDL_KEYUP) { |
| 85 | + //KeySym sym = XKeycodeToKeysym(s_Display, e.xkey.keycode, 0); |
| 86 | + //printf("KeyRelease:%d sym:%d\n", e.xkey.keycode, sym); |
| 87 | + addKeyToQueue(0, e.key.keysym.sym); |
| 88 | + } |
| 89 | + } |
| 90 | +} |
| 91 | + |
| 92 | + |
| 93 | +void DG_Init(){ |
| 94 | + window = SDL_CreateWindow("DOOM", |
| 95 | + SDL_WINDOWPOS_UNDEFINED, |
| 96 | + SDL_WINDOWPOS_UNDEFINED, |
| 97 | + DOOMGENERIC_RESX, |
| 98 | + DOOMGENERIC_RESY, |
| 99 | + SDL_WINDOW_SHOWN |
| 100 | + ); |
| 101 | + |
| 102 | + // Setup renderer |
| 103 | + renderer = SDL_CreateRenderer( window, -1, SDL_RENDERER_ACCELERATED); |
| 104 | + // Clear winow |
| 105 | + SDL_RenderClear( renderer ); |
| 106 | + // Render the rect to the screen |
| 107 | + SDL_RenderPresent(renderer); |
| 108 | + |
| 109 | + texture = SDL_CreateTexture(renderer, SDL_PIXELFORMAT_RGB888, SDL_TEXTUREACCESS_TARGET, DOOMGENERIC_RESX, DOOMGENERIC_RESY); |
| 110 | +} |
| 111 | + |
| 112 | +void DG_DrawFrame() |
| 113 | +{ |
| 114 | + SDL_UpdateTexture(texture, NULL, DG_ScreenBuffer, DOOMGENERIC_RESX*sizeof(uint32_t)); |
| 115 | + |
| 116 | + SDL_RenderClear(renderer); |
| 117 | + SDL_RenderCopy(renderer, texture, NULL, NULL); |
| 118 | + SDL_RenderPresent(renderer); |
| 119 | + |
| 120 | + handleKeyInput(); |
| 121 | +} |
| 122 | + |
| 123 | +void DG_SleepMs(uint32_t ms) |
| 124 | +{ |
| 125 | + SDL_Delay(ms); |
| 126 | +} |
| 127 | + |
| 128 | +uint32_t DG_GetTicksMs() |
| 129 | +{ |
| 130 | + return SDL_GetTicks(); |
| 131 | +} |
| 132 | + |
| 133 | +int DG_GetKey(int* pressed, unsigned char* doomKey) |
| 134 | +{ |
| 135 | + if (s_KeyQueueReadIndex == s_KeyQueueWriteIndex){ |
| 136 | + //key queue is empty |
| 137 | + return 0; |
| 138 | + }else{ |
| 139 | + unsigned short keyData = s_KeyQueue[s_KeyQueueReadIndex]; |
| 140 | + s_KeyQueueReadIndex++; |
| 141 | + s_KeyQueueReadIndex %= KEYQUEUE_SIZE; |
| 142 | + |
| 143 | + *pressed = keyData >> 8; |
| 144 | + *doomKey = keyData & 0xFF; |
| 145 | + |
| 146 | + return 1; |
| 147 | + } |
| 148 | + |
| 149 | + return 0; |
| 150 | +} |
| 151 | + |
| 152 | +void DG_SetWindowTitle(const char * title) |
| 153 | +{ |
| 154 | + if (window != NULL){ |
| 155 | + SDL_SetWindowTitle(window, title); |
| 156 | + } |
| 157 | +} |
0 commit comments