Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 35 additions & 0 deletions src/graphics/sdl_event.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
#ifndef SDL_event_h
#define SDL_event_h

#include <SDL.h>
#include <stdlib.h>
#include <graphics/sdl_error.h>

typedef struct G_WriteCharEventData {
int x, y;
const char *c;
Uint32 color;
} G_WriteCharEventData;

int G_sendWriteCharEvent(int x, int y, const char c[256], Uint32 color) {
SDL_Event writeCharEvent;
writeCharEvent.type = SDL_USEREVENT;

G_WriteCharEventData *data = (G_WriteCharEventData*)malloc(sizeof(G_WriteCharEventData));
data->x = x;
data->y = y;
data->color = color;
data->c = c;

writeCharEvent.user.code = 37;
writeCharEvent.user.data1 = data;
writeCharEvent.user.timestamp = SDL_GetTicks();

int res = SDL_PushEvent(&writeCharEvent);

if (res == 1) return 0;
if (res == 0) return 1;
else G_error();
}

#endif
26 changes: 22 additions & 4 deletions src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,16 @@
#include "graphics/sdl_graphics.h"
#include "graphics/sdl_error.h"
#include "graphics/sdl_videomem.h"
#include "graphics/sdl_event.h"

#include "charset_debug.c"
#include "charset.c"

#include <stdlib.h>
#include <string.h>

static int fps = 25;

void quit() {
// Delete video memory
G_deleteVideomem();
Expand All @@ -21,12 +25,19 @@ void handleEvent(SDL_Event e) {
// User requests quit
if (e.type == SDL_QUIT)
quit();

if (e.type == SDL_KEYDOWN)
else if (e.type == SDL_KEYDOWN)
{} // handle keydown
if (e.type == SDL_KEYUP)
else if (e.type == SDL_KEYUP)
{} // handle keyup
else if (e.type == SDL_USEREVENT) {
if (e.user.code == 37) {
G_WriteCharEventData *data = e.user.data1;

G_writeChar(data->x, data->y, data->c, data->color);

free(data);
}
}
}

int main(int argc, char *argv[]) {
Expand Down Expand Up @@ -62,13 +73,20 @@ int main(int argc, char *argv[]) {

// Execution loop
for (;;) {
// run instruction
Uint64 ticksStart = SDL_GetTicks64();

SDL_Event event;
while (SDL_PollEvent(&event))
handleEvent(event);

G_render();

Uint64 ticksEnd = SDL_GetTicks64();

Uint64 ticksPassed = ticksEnd - ticksStart;
Uint64 ticksLeft = ticksPassed * 1000 / fps;

SDL_Delay(ticksLeft);
}

quit();
Expand Down