|
@@ -0,0 +1,119 @@ |
|
|
|
|
|
#include <SDL.h> |
|
|
|
|
|
#include <stdbool.h> |
|
|
|
|
|
#include <stdio.h> |
|
|
|
|
|
|
|
|
|
|
|
const char* APP_NAME = "SDL2"; |
|
|
|
|
|
const unsigned int SCREEN_WIDTH = 640; |
|
|
|
|
|
const unsigned int SCREEN_HEIGHT = 480; |
|
|
|
|
|
|
|
|
|
|
|
const unsigned char CONTROLS = 16; |
|
|
|
|
|
|
|
|
|
|
|
const SDL_KeyCode KEYBOARD_CONTROLS[CONTROLS] = { |
|
|
|
|
|
SDLK_RIGHT, |
|
|
|
|
|
SDLK_DOWN, |
|
|
|
|
|
SDLK_LEFT, |
|
|
|
|
|
SDLK_UP, |
|
|
|
|
|
SDLK_RETURN, // yes |
|
|
|
|
|
SDLK_BACKSPACE, // no |
|
|
|
|
|
SDLK_a, // action0 |
|
|
|
|
|
SDLK_s, // action1 |
|
|
|
|
|
SDLK_d, // action2 |
|
|
|
|
|
SDLK_f, // action3 |
|
|
|
|
|
SDLK_z, // action4 |
|
|
|
|
|
SDLK_x, // action5 |
|
|
|
|
|
SDLK_c, // action6 |
|
|
|
|
|
SDLK_v, // action7 |
|
|
|
|
|
SDLK_w, // action8 |
|
|
|
|
|
SDLK_e, // action9 |
|
|
|
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
int main(int argc, char* args[]) { |
|
|
|
|
|
SDL_Window* window = NULL; |
|
|
|
|
|
SDL_Surface* screenSurface = NULL; |
|
|
|
|
|
|
|
|
|
|
|
if (SDL_Init( |
|
|
|
|
|
SDL_INIT_VIDEO |
|
|
|
|
|
| SDL_INIT_GAMECONTROLLER |
|
|
|
|
|
| SDL_INIT_EVENTS |
|
|
|
|
|
) < 0) { |
|
|
|
|
|
printf("SDL could not initialize! SDL_Error: %s\n", SDL_GetError()); |
|
|
|
|
|
return -1; |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
window = SDL_CreateWindow( |
|
|
|
|
|
APP_NAME, |
|
|
|
|
|
SDL_WINDOWPOS_UNDEFINED, |
|
|
|
|
|
SDL_WINDOWPOS_UNDEFINED, |
|
|
|
|
|
SCREEN_WIDTH, |
|
|
|
|
|
SCREEN_HEIGHT, |
|
|
|
|
|
SDL_WINDOW_SHOWN |
|
|
|
|
|
); |
|
|
|
|
|
|
|
|
|
|
|
if (window == NULL) { |
|
|
|
|
|
printf("Window could not be created! SDL_Error: %s\n", SDL_GetError()); |
|
|
|
|
|
return -2; |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
bool quit = false; |
|
|
|
|
|
SDL_Event e; |
|
|
|
|
|
screenSurface = SDL_GetWindowSurface(window); |
|
|
|
|
|
|
|
|
|
|
|
unsigned short action = 0; |
|
|
|
|
|
while (!quit) { |
|
|
|
|
|
SDL_FillRect(screenSurface, NULL, SDL_MapRGB(screenSurface->format, 0x00, 0x00, 0x00)); |
|
|
|
|
|
uint64_t ticks = SDL_GetTicks64(); |
|
|
|
|
|
for (unsigned char i = 0; i < 64; i += 1) { |
|
|
|
|
|
const unsigned char column = (64 - i) % 32; |
|
|
|
|
|
const unsigned char row = i / 32; |
|
|
|
|
|
const uint64_t bitflag = (0x1lu << i); |
|
|
|
|
|
const unsigned char size = 4; |
|
|
|
|
|
if (ticks & bitflag) { |
|
|
|
|
|
SDL_FillRect(screenSurface, &(SDL_Rect) { |
|
|
|
|
|
column * size, |
|
|
|
|
|
SCREEN_HEIGHT - ((row + 1) * size), |
|
|
|
|
|
size, |
|
|
|
|
|
size |
|
|
|
|
|
}, SDL_MapRGB(screenSurface->format, 0x00, 0xff, 0xff)); |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
while (SDL_PollEvent(&e) != 0) { |
|
|
|
|
|
if (e.type == SDL_QUIT) { |
|
|
|
|
|
quit = true; |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
for (unsigned char i = 0; i < CONTROLS; i += 1) { |
|
|
|
|
|
// TODO do same for gamepad |
|
|
|
|
|
if (e.key.keysym.sym == KEYBOARD_CONTROLS[i]) { |
|
|
|
|
|
const unsigned short bitflag = (0x1 << i); |
|
|
|
|
|
if (e.type == SDL_KEYDOWN) { |
|
|
|
|
|
action |= bitflag; |
|
|
|
|
|
} else if (e.type == SDL_KEYUP) { |
|
|
|
|
|
action &= ~bitflag; |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
for (unsigned char i = 0; i < CONTROLS; i += 1) { |
|
|
|
|
|
const unsigned char column = i % 4; |
|
|
|
|
|
const unsigned char row = i / 4; |
|
|
|
|
|
const unsigned short bitflag = (0x1 << i); |
|
|
|
|
|
const unsigned char size = 4; |
|
|
|
|
|
if (action & bitflag) { |
|
|
|
|
|
SDL_FillRect(screenSurface, &(SDL_Rect) { |
|
|
|
|
|
column * size, |
|
|
|
|
|
row * size, |
|
|
|
|
|
size, |
|
|
|
|
|
size |
|
|
|
|
|
}, SDL_MapRGB(screenSurface->format, 0xff, 0xff, 0x00)); |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
SDL_UpdateWindowSurface(window); |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
SDL_DestroyWindow(window); |
|
|
|
|
|
SDL_Quit(); |
|
|
|
|
|
|
|
|
|
|
|
return 0; |
|
|
|
|
|
} |