diff --git a/src/one.c b/src/one.c index 3ff34cc..ae201b4 100644 --- a/src/one.c +++ b/src/one.c @@ -279,7 +279,8 @@ int ewm_one_main(int argc, char **argv) { SDL_StartTextInput(); - Uint32 ticks = SDL_GetTicks(); + uint32_t ticks = SDL_GetTicks(); + uint32_t phase = 1; while (true) { if (!ewm_one_poll_event(one, window)) { // TODO Move window into one @@ -288,20 +289,27 @@ int ewm_one_main(int argc, char **argv) { // This is very basic throttling that does bursts of CPU cycles. - if ((SDL_GetTicks() - ticks) >= (1000 / 30)) { // TODO EWM_ONE_TTY_FPS ? - if (!ewm_one_step_cpu(one, 1000000 / 30)) { + if ((SDL_GetTicks() - ticks) >= (1000 / EWM_ONE_FPS)) { + if (!ewm_one_step_cpu(one, EWM_ONE_CPS / EWM_ONE_FPS)) { break; } - if (one->tty->screen_dirty) { + if (one->tty->screen_dirty || (phase == 0) || ((phase % (EWM_ONE_FPS / 4)) == 0)) { SDL_SetRenderDrawColor(one->tty->renderer, 0, 0, 0, 255); SDL_RenderClear(one->tty->renderer); - ewm_tty_refresh(one->tty); + + ewm_tty_refresh(one->tty, phase, EWM_ONE_FPS); one->tty->screen_dirty = false; + SDL_RenderPresent(one->tty->renderer); } ticks = SDL_GetTicks(); + + phase += 1; + if (phase == EWM_ONE_FPS) { + phase = 0; + } } } diff --git a/src/one.h b/src/one.h index e479329..d583326 100644 --- a/src/one.h +++ b/src/one.h @@ -23,11 +23,14 @@ #ifndef EWM_ONE_H #define EWM_ONE_H +#include + #define EWM_ONE_MODEL_APPLE1 (0) #define EWM_ONE_MODEL_REPLICA1 (1) #define EWM_ONE_MODEL_DEFAULT (EWM_ONE_MODEL_REPLICA1) -#include +#define EWM_ONE_FPS (40) +#define EWM_ONE_CPS (1023000) struct cpu_t; struct ewm_tty_t; diff --git a/src/tty.c b/src/tty.c index 208e107..1f7421e 100644 --- a/src/tty.c +++ b/src/tty.c @@ -89,11 +89,18 @@ void ewm_tty_reset(struct ewm_tty_t *tty) { tty->screen_dirty = true; } -void ewm_tty_refresh(struct ewm_tty_t *tty) { +void ewm_tty_refresh(struct ewm_tty_t *tty, uint32_t phase, uint32_t fps) { for (int row = 0; row < 24; row++) { for (int column = 0; column < 40; column++) { ewm_tty_render_character(tty, row, column, tty->screen_buffer[(row * EWM_ONE_TTY_COLUMNS) + column]); } } - ewm_tty_render_character(tty, tty->screen_cursor_row, tty->screen_cursor_column, EWM_ONE_TTY_CURSOR); + + if ((phase % (fps / 4)) == 0) { + tty->screen_cursor_blink = !tty->screen_cursor_blink; + } + + if (tty->screen_cursor_blink) { + ewm_tty_render_character(tty, tty->screen_cursor_row, tty->screen_cursor_column, EWM_ONE_TTY_CURSOR); + } } diff --git a/src/tty.h b/src/tty.h index 37ddc2d..129851d 100644 --- a/src/tty.h +++ b/src/tty.h @@ -41,12 +41,13 @@ struct ewm_tty_t { uint8_t screen_buffer[EWM_ONE_TTY_ROWS * EWM_ONE_TTY_COLUMNS]; int screen_cursor_row; int screen_cursor_column; + int screen_cursor_blink; }; struct ewm_tty_t *ewm_tty_create(SDL_Renderer *renderer); void ewm_tty_destroy(struct ewm_tty_t *tty); void ewm_tty_write(struct ewm_tty_t *tty, uint8_t v); void ewm_tty_reset(struct ewm_tty_t *tty); -void ewm_tty_refresh(struct ewm_tty_t *tty); +void ewm_tty_refresh(struct ewm_tty_t *tty, uint32_t phase, uint32_t fps); #endif // EWM_TTY_H