aboutsummaryrefslogtreecommitdiff
path: root/tr8vm.c
diff options
context:
space:
mode:
authorJuan J. Martinez <jjm@usebox.net>2023-05-07 23:57:23 +0100
committerJuan J. Martinez <jjm@usebox.net>2023-05-07 23:57:23 +0100
commitfdd6efd9ad3e0e213a50ed962ec628c17779794a (patch)
tree2c4bb3fbb6e20bfcbca0e58c4051ce74ba3e30b2 /tr8vm.c
parent856a86a7f7796e34efaeada0dc11f771fedf2c67 (diff)
downloadtr8vm-fdd6efd9ad3e0e213a50ed962ec628c17779794a.tar.gz
tr8vm-fdd6efd9ad3e0e213a50ed962ec628c17779794a.zip
Report non recoverable errors on the playerui-test
Diffstat (limited to 'tr8vm.c')
-rw-r--r--tr8vm.c123
1 files changed, 107 insertions, 16 deletions
diff --git a/tr8vm.c b/tr8vm.c
index 59d08b7..7d254e0 100644
--- a/tr8vm.c
+++ b/tr8vm.c
@@ -30,7 +30,9 @@
#include "vm.h"
-#define ARGB(r, g, b) ((uint32_t)(((r)<<16)|((g)<<8)|(b)))
+#include "font.h"
+
+#define ARGB(r, g, b) ((uint32_t)(0xff000000|((r)<<16)|((g)<<8)|(b)))
#define APP_NAME "TR8 VM Player"
#define APP_VERSION "0.1-alpha"
@@ -91,6 +93,47 @@ static uint32_t palette[] =
ARGB(0xff, 0xff, 0xff),
};
+uint32_t *ui_data = NULL;
+
+static void ui_put_glyph(uint32_t x, uint32_t y, uint8_t c)
+{
+ uint32_t *p = ui_data + x + (y * WINDOW_W);
+
+ for (uint8_t j = 0; j < 8; j++)
+ for (uint8_t i = 0; i < 8; i++)
+ if (font[c][j] & (128 >> i))
+ p[i + j * WINDOW_W] = palette[12];
+}
+
+static void ui_put_text(uint32_t x, uint32_t y, char *text)
+{
+ uint32_t ox = x;
+
+ while (*text)
+ {
+ uint8_t c = *text++;
+
+ if (c == '\n')
+ {
+ y += 12;
+ if (y > WINDOW_H)
+ break;
+
+ x = ox;
+ continue;
+ }
+
+ ui_put_glyph(x, y, c);
+
+ x += 8;
+ if (x > WINDOW_W)
+ {
+ x = 0;
+ y += 12;
+ }
+ }
+}
+
uint8_t ram[UINT16_MAX + 1] = { 0 };
uint32_t *fb_data = NULL;
@@ -234,6 +277,14 @@ int main(int argc, char *argv[])
}
SDL_SetTextureBlendMode(fb, SDL_BLENDMODE_NONE);
+ SDL_Texture *ui = SDL_CreateTexture(renderer, SDL_PIXELFORMAT_ARGB8888, SDL_TEXTUREACCESS_STREAMING, WINDOW_W, WINDOW_H);
+ if (!ui)
+ {
+ fprintf(stderr, "Failed to create the UI: %s\n", SDL_GetError());
+ return 1;
+ }
+ SDL_SetTextureBlendMode(ui, SDL_BLENDMODE_BLEND);
+
int fullscreen = 0;
SDL_Rect dst;
resize_full_screen(screen, renderer, fullscreen, &dst);
@@ -244,7 +295,7 @@ int main(int argc, char *argv[])
Tr8 vm;
tr8_init(&vm, write_m, read_m, port);
int pitch = 0;
- uint8_t rc;
+ uint8_t rc = 1;
/* update full fb once to sync with RAM */
pitch = 0;
@@ -253,6 +304,13 @@ int main(int argc, char *argv[])
update_fb();
SDL_UnlockTexture(fb);
+ /* prepare UI layer */
+ pitch = 0;
+ ui_data = NULL;
+ SDL_LockTexture(ui, NULL, (void **)&ui_data, &pitch);
+ memset(ui_data, 0x20202080, sizeof(uint32_t) * WINDOW_W * WINDOW_H);
+ SDL_UnlockTexture(ui);
+
SDL_Event ev;
while (1)
{
@@ -278,14 +336,29 @@ int main(int argc, char *argv[])
}
}
- pitch = 0;
- fb_data = NULL;
- SDL_LockTexture(fb, NULL, (void **)&fb_data, &pitch);
- rc = tr8_eval(&vm);
- SDL_UnlockTexture(fb);
+ if (rc)
+ {
+ pitch = 0;
+ fb_data = NULL;
+ SDL_LockTexture(fb, NULL, (void **)&fb_data, &pitch);
+ rc = tr8_eval(&vm);
+ SDL_UnlockTexture(fb);
- if (!rc)
- break;
+ if (!rc)
+ {
+ /* non recoverable error */
+ pitch = 0;
+ ui_data = NULL;
+ SDL_LockTexture(ui, NULL, (void **)&ui_data, &pitch);
+ ui_put_text(10, 10, "[VM HALT]");
+
+ char buffer[256];
+ tr8_dump(&vm, buffer);
+ ui_put_text(10, 22, buffer);
+
+ SDL_UnlockTexture(ui);
+ }
+ }
/* render to the canvas */
SDL_SetRenderTarget(renderer, canvas);
@@ -298,16 +371,34 @@ int main(int argc, char *argv[])
SDL_RenderClear(renderer);
SDL_RenderCopy(renderer, canvas, NULL, &dst);
+ if (!rc)
+ SDL_RenderCopy(renderer, ui, NULL, &dst);
+
SDL_RenderPresent(renderer);
- pitch = 0;
- fb_data = NULL;
- SDL_LockTexture(fb, NULL, (void **)&fb_data, &pitch);
- rc = tr8_frame_int(&vm);
- SDL_UnlockTexture(fb);
+ if (rc)
+ {
+ pitch = 0;
+ fb_data = NULL;
+ SDL_LockTexture(fb, NULL, (void **)&ui_data, &pitch);
+ rc = tr8_frame_int(&vm);
+ SDL_UnlockTexture(fb);
- if (!rc)
- break;
+ if (!rc)
+ {
+ /* non recoverable error */
+ pitch = 0;
+ ui_data = NULL;
+ SDL_LockTexture(ui, NULL, (void **)&fb_data, &pitch);
+ ui_put_text(10, 10, "[VM HALT]");
+
+ char buffer[256];
+ tr8_dump(&vm, buffer);
+ ui_put_text(10, 22, buffer);
+
+ SDL_UnlockTexture(ui);
+ }
+ }
}
if (canvas)