1
0
mirror of https://github.com/pevans/erc-c.git synced 2024-06-24 15:29:32 +00:00

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.
This commit is contained in:
Peter Evans 2017-12-27 16:31:02 -06:00
parent 5407ce2d32
commit c7b830bb4a
6 changed files with 54 additions and 31 deletions

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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