From e8fe9370f20f906ca7f613a32c69c408c87a4b2b Mon Sep 17 00:00:00 2001 From: Stefan Arentz Date: Thu, 29 Dec 2016 11:12:27 -0500 Subject: [PATCH] Fixes #88 Add an optional status row (#107) --- src/scr.c | 2 ++ src/two.c | 54 +++++++++++++++++++++++++++++++++++++++++++++++++++++- src/two.h | 4 ++++ 3 files changed, 59 insertions(+), 1 deletion(-) diff --git a/src/scr.c b/src/scr.c index 2ae579f..31df21d 100644 --- a/src/scr.c +++ b/src/scr.c @@ -62,6 +62,8 @@ static inline void scr_render_character(struct scr_t *scr, int row, int column, if (scr->color_scheme == EWM_SCR_COLOR_SCHEME_MONOCHROME) { SDL_SetTextureColorMod(scr->chr->characters[c], 0, 255, 0); + } else { + SDL_SetTextureColorMod(scr->chr->characters[c], 255, 255, 255); } SDL_RenderCopy(scr->renderer, scr->chr->characters[c], NULL, &dst); diff --git a/src/two.c b/src/two.c index 3118818..e6bd319 100644 --- a/src/two.c +++ b/src/two.c @@ -24,6 +24,7 @@ #include #include #include +#include #include @@ -31,6 +32,7 @@ #include "mem.h" #include "dsk.h" #include "alc.h" +#include "chr.h" #include "scr.h" #include "two.h" @@ -405,6 +407,11 @@ static bool ewm_two_poll_event(struct ewm_two_t *two, SDL_Window *window) { // T SDL_SetWindowFullscreen(window, SDL_WINDOW_FULLSCREEN); } break; + case SDLK_i: + two->status_bar_visible = !two->status_bar_visible; + SDL_SetWindowSize(window, 40*7*3, 24*8*3 + (two->status_bar_visible ? (9*3) : 0)); + SDL_RenderSetLogicalSize(two->scr->renderer, 40*7*3, 24*8*3 + (two->status_bar_visible ? (9*3) : 0)); + break; } } else if (event.key.keysym.mod == KMOD_NONE) { switch (event.key.keysym.sym) { @@ -493,6 +500,36 @@ static bool ewm_two_step_cpu(struct ewm_two_t *two, int cycles) { return true; } +static void ewm_two_update_status_bar(struct ewm_two_t *two, double mhz) { + + SDL_Rect rect = { .x = 0, .y = (24*8*3), .w = (40*7*3), .h = (9*3) }; + SDL_SetRenderDrawColor(two->scr->renderer, 39, 39, 39, 0); + SDL_RenderFillRect(two->scr->renderer, &rect); + + char s[41]; + snprintf(s, 41, "%1.3f MHZ [1][2]", mhz); + // 1234567890123456789012345678901234567890 + + for (int i = 0; i < 40; i++) { + int c = s[i] + 0x80; + if (two->scr->chr->characters[c] != NULL) { + SDL_Rect dst; + dst.x = i * 21; + dst.y = 24 * 24 + 3; + dst.w = 21; + dst.h = 24; + + if (two->dsk->on && ((i == 35 && two->dsk->drive == EWM_DSK_DRIVE1) || (i == 38 && two->dsk->drive == EWM_DSK_DRIVE2))) { + SDL_SetTextureColorMod(two->scr->chr->characters[c], 145, 193, 75); + } else { + SDL_SetTextureColorMod(two->scr->chr->characters[c], 255, 0, 0); + } + + SDL_RenderCopy(two->scr->renderer, two->scr->chr->characters[c], NULL, &dst); + } + } +} + #define EWM_TWO_OPT_DRIVE1 (0) #define EWM_TWO_OPT_DRIVE2 (1) #define EWM_TWO_OPT_COLOR (2) @@ -630,13 +667,16 @@ int ewm_two_main(int argc, char **argv) { uint32_t ticks = SDL_GetTicks(); uint32_t phase = 1; + uint64_t counter = two->cpu->counter; + double mhz = 1.0; + while (true) { if (!ewm_two_poll_event(two, window)) { break; } if ((SDL_GetTicks() - ticks) >= (1000 / fps)) { - if (!ewm_two_step_cpu(two, 1000000 / fps)) { + if (!ewm_two_step_cpu(two, EWM_TWO_SPEED / fps)) { break; } @@ -647,6 +687,11 @@ int ewm_two_main(int argc, char **argv) { if (two->screen_dirty || (phase == 0) || (phase == (fps / 2))) { ewm_scr_update(two->scr, phase, fps); two->screen_dirty = false; + + if (two->status_bar_visible) { + ewm_two_update_status_bar(two, mhz); + } + SDL_RenderPresent(two->scr->renderer); } @@ -654,6 +699,13 @@ int ewm_two_main(int argc, char **argv) { phase += 1; if (phase == fps) { phase = 0; + + // Calculate the number of cycles we have done in the past + // second. TODO This will always equal 1023000 - It needs + // to be actual clock time based instead. Good for now, + // but not ideal. + mhz = (two->cpu->counter - counter) / 1000000.0; + counter = two->cpu->counter; } } } diff --git a/src/two.h b/src/two.h index 5165d24..c5c2db7 100644 --- a/src/two.h +++ b/src/two.h @@ -23,6 +23,7 @@ #ifndef EWM_TWO_H #define EWM_TWO_H +#include #include #include @@ -50,6 +51,7 @@ #define EWM_A2P_BUTTON_COUNT 4 #define EWM_TWO_FPS_DEFAULT (30) +#define EWM_TWO_SPEED (1023000) struct mem_t; struct ewm_dsk_t; @@ -93,6 +95,8 @@ struct ewm_two_t { uint8_t padl3_value; SDL_Joystick *joystick; + + bool status_bar_visible; }; struct ewm_two_t *ewm_two_create(int type, SDL_Renderer *renderer, SDL_Joystick *joystick);