Fixes #126 Implement the same cpu throttling/screen update loop as we have on the two

This commit is contained in:
Stefan Arentz 2017-01-08 22:18:38 -05:00
parent f6beef7e34
commit 37d508c367
4 changed files with 28 additions and 9 deletions

View File

@ -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;
}
}
}

View File

@ -23,11 +23,14 @@
#ifndef EWM_ONE_H
#define EWM_ONE_H
#include <SDL2/SDL.h>
#define EWM_ONE_MODEL_APPLE1 (0)
#define EWM_ONE_MODEL_REPLICA1 (1)
#define EWM_ONE_MODEL_DEFAULT (EWM_ONE_MODEL_REPLICA1)
#include <SDL2/SDL.h>
#define EWM_ONE_FPS (40)
#define EWM_ONE_CPS (1023000)
struct cpu_t;
struct ewm_tty_t;

View File

@ -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);
}
}

View File

@ -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