From c7b830bb4a8d91bb0b568eeb57e5b50877090857 Mon Sep 17 00:00:00 2001 From: Peter Evans Date: Wed, 27 Dec 2017 16:31:02 -0600 Subject: [PATCH] Switch references from SDL_Rect to vm_area We still use SDL_Rect internally, but we want to abstract the use of it in our API. The only thing the rest of the app should care about is the vm_area struct. --- include/vm_bitfont.h | 2 +- include/vm_screen.h | 26 +++++++++++++++++++++++++- src/apple2.draw.c | 10 +++++----- src/vm_bitfont.c | 22 +++++++++++----------- src/vm_screen.c | 17 ++++++++--------- tests/vm_screen.c | 8 ++++---- 6 files changed, 54 insertions(+), 31 deletions(-) diff --git a/include/vm_bitfont.h b/include/vm_bitfont.h index c63a9ea..507770c 100644 --- a/include/vm_bitfont.h +++ b/include/vm_bitfont.h @@ -15,7 +15,7 @@ typedef struct { char cmask; } vm_bitfont; -extern int vm_bitfont_render(vm_bitfont *, vm_screen *, SDL_Rect *, char); +extern int vm_bitfont_render(vm_bitfont *, vm_screen *, vm_area *, char); extern vm_bitfont *vm_bitfont_create(vm_screen *, const char *, int, int, char); extern void vm_bitfont_free(vm_bitfont *); extern void vm_bitfont_offset(vm_bitfont *, char, int *, int *); diff --git a/include/vm_screen.h b/include/vm_screen.h index 0a83ce6..2152fb9 100644 --- a/include/vm_screen.h +++ b/include/vm_screen.h @@ -15,15 +15,39 @@ #define vm_screen_draw_pixel(screen, xpos, ypos) \ vm_screen_draw_rect(screen, xpos, ypos, 1, 1) +typedef struct { + int xoff; + int yoff; + + int width; + int height; +} vm_area; + typedef struct { SDL_Window *window; SDL_Renderer *render; - SDL_Rect rect; + vm_area area; int xcoords; int ycoords; } vm_screen; +#define SET_SDL_RECT(name, a) \ + (name).x = (a).xoff; \ + (name).y = (a).yoff; \ + (name).w = (a).width; \ + (name).h = (a).height + +#define MAKE_SDL_RECT(name, a) \ + SDL_Rect name; \ + SET_SDL_RECT(name, a) + +#define vm_area_set(a, x, y, w, h) \ + (a)->xoff = x; \ + (a)->yoff = y; \ + (a)->width = w; \ + (a)->height = h + extern bool vm_screen_active(vm_screen *); extern int vm_screen_add_window(vm_screen *, int, int); extern int vm_screen_init(); diff --git a/src/apple2.draw.c b/src/apple2.draw.c index b27dd72..b0bc4e8 100644 --- a/src/apple2.draw.c +++ b/src/apple2.draw.c @@ -72,7 +72,7 @@ apple2_draw_text(apple2 *mach, vm_16bit addr) { vm_8bit lsb, msb; vm_16bit page_base; - SDL_Rect dest; + vm_area dest; char ch; // The text display buffers are located at "Page 1" and "Page 2", @@ -120,18 +120,18 @@ apple2_draw_text(apple2 *mach, vm_16bit addr) // The absolute column position will be the font width times the // lsb. - dest.x = lsb * mach->sysfont->width; + dest.xoff = lsb * mach->sysfont->width; // The absolute row position will be the font height times the msb // minus the page base (because the height is the same regardless of // what page we're in). So if we're msb $0400, then we're starting // on pixel row 0; but if we're msb $0480, then we are starting on // pixel row 8 (where the font height is 8); etc. - dest.y = ((addr & 0xff80) - page_base) * mach->sysfont->height; + dest.yoff = ((addr & 0xff80) - page_base) * mach->sysfont->height; // Our width and height must be that of the font. - dest.w = mach->sysfont->width; - dest.h = mach->sysfont->height; + dest.width = mach->sysfont->width; + dest.height = mach->sysfont->height; // And...lastly...what's in the address? ch = (char)vm_segment_get(mach->memory, addr); diff --git a/src/vm_bitfont.c b/src/vm_bitfont.c index 82b1cb7..7e2a188 100644 --- a/src/vm_bitfont.c +++ b/src/vm_bitfont.c @@ -83,10 +83,11 @@ vm_bitfont_offset(vm_bitfont *font, char ch, int *xcoord, int *ycoord) int vm_bitfont_render(vm_bitfont *font, vm_screen *screen, - SDL_Rect *dest, + vm_area *dest, char ch) { - SDL_Rect src; + SDL_Rect src_rect; + SDL_Rect dest_rect; // Our bitmap font may not be able to support all 256 possible // values that a character can hold; the cmask will limit us to @@ -95,20 +96,19 @@ vm_bitfont_render(vm_bitfont *font, // The width and height of the glyph are as indicated by the font // struct - src.w = font->width; - src.h = font->height; + src_rect.w = font->width; + src_rect.h = font->height; - // The source and destination width/height must match. This might be - // a faulty assumption down the road; for now, it holds. - dest->w = src.w; - dest->h = src.h; + // Bring the destination attributes into the SDL_Rect we need to + // pass into SDL_RenderCopy(). + SET_SDL_RECT(dest_rect, *dest); // Get the spot in the bitmap where the glyph is found - vm_bitfont_offset(font, ch, &src.x, &src.y); + vm_bitfont_offset(font, ch, &src_rect.x, &src_rect.y); - log_critical("src.x = %d, src.y = %d", src.x, src.y); + log_critical("src.x = %d, src.y = %d", src_rect.x, src_rect.y); - if (SDL_RenderCopy(screen->render, font->texture, &src, dest) < 0) { + if (SDL_RenderCopy(screen->render, font->texture, &src_rect, &dest_rect) < 0) { log_critical("Failed to render glyph: %s", SDL_GetError()); return ERR_GFXOP; } diff --git a/src/vm_screen.c b/src/vm_screen.c index b5515eb..7b1a3cd 100644 --- a/src/vm_screen.c +++ b/src/vm_screen.c @@ -61,10 +61,7 @@ vm_screen_create() screen->window = NULL; screen->render = NULL; - screen->rect.x = 0; - screen->rect.y = 0; - screen->rect.w = 0; - screen->rect.h = 0; + vm_area_set(&screen->area, 0, 0, 0, 0); return screen; } @@ -213,12 +210,14 @@ vm_screen_draw_rect(vm_screen *screen, int xsize, int ysize) { + SDL_Rect rect; + // The renderer will take care of translating the positions and // sizes into whatever the window is really at. - screen->rect.x = xpos; - screen->rect.y = ypos; - screen->rect.w = xsize; - screen->rect.h = ysize; + rect.x = xpos; + rect.y = ypos; + rect.w = xsize; + rect.h = ysize; - SDL_RenderFillRect(screen->render, &screen->rect); + SDL_RenderFillRect(screen->render, &rect); } diff --git a/tests/vm_screen.c b/tests/vm_screen.c index a61279c..5a40f7e 100644 --- a/tests/vm_screen.c +++ b/tests/vm_screen.c @@ -33,10 +33,10 @@ Test(vm_screen, create) { cr_assert_eq(screen->window, NULL); cr_assert_eq(screen->render, NULL); - cr_assert_eq(screen->rect.x, 0); - cr_assert_eq(screen->rect.y, 0); - cr_assert_eq(screen->rect.w, 0); - cr_assert_eq(screen->rect.h, 0); + cr_assert_eq(screen->area.xoff, 0); + cr_assert_eq(screen->area.yoff, 0); + cr_assert_eq(screen->area.width, 0); + cr_assert_eq(screen->area.height, 0); cr_assert_eq(screen->xcoords, 0); cr_assert_eq(screen->ycoords, 0); }