1
0
mirror of https://github.com/pevans/erc-c.git synced 2024-07-02 11:29:36 +00:00
erc-c/src/vm_screen.c

142 lines
3.0 KiB
C
Raw Normal View History

2017-12-09 04:12:31 +00:00
/*
* vm_screen.c
*
* Functions here support drawing to the virtual machine's "screen";
* exactly how that is done is an abstraction to the rest of the
* program, which only knows to call the functions here.
*/
2017-12-18 01:09:54 +00:00
#include <stdbool.h>
2017-11-22 05:24:51 +00:00
#include <stdlib.h>
#include "log.h"
#include "vm_screen.h"
int
vm_screen_init()
{
if (SDL_Init(SDL_INIT_VIDEO) < 0) {
log_critical("Could not initialize video: %s", SDL_GetError());
return ERR_GFXINIT;
}
return OK;
}
void
vm_screen_finish()
{
SDL_Quit();
}
2017-12-07 03:25:47 +00:00
/*
2017-12-17 22:42:05 +00:00
* Return a new screen. We also set the color to black.
2017-12-07 03:25:47 +00:00
*/
2017-12-17 22:42:05 +00:00
vm_screen *
vm_screen_create(int xcoords, int ycoords, int scale)
2017-11-22 05:24:51 +00:00
{
2017-12-17 22:42:05 +00:00
vm_screen *screen;
2017-11-22 05:24:51 +00:00
2017-12-17 22:42:05 +00:00
screen = (vm_screen *)malloc(sizeof(vm_screen));
if (screen == NULL) {
log_critical("Failed to allocate vm_screen");
2017-11-22 05:24:51 +00:00
exit(1);
}
screen->xcoords = xcoords;
screen->ycoords = ycoords;
screen->scale = scale;
screen->window = NULL;
screen->render = NULL;
screen->rect.x = 0;
screen->rect.y = 0;
screen->rect.w = 0;
screen->rect.h = 0;
2017-12-17 22:42:05 +00:00
return screen;
2017-11-22 05:24:51 +00:00
}
int
2017-12-17 22:42:05 +00:00
vm_screen_add_window(vm_screen *screen)
{
SDL_CreateWindowAndRenderer(screen->xcoords * screen->scale,
screen->ycoords * screen->scale,
0, &screen->window, &screen->render);
if (screen->window == NULL || screen->render == NULL) {
log_critical("Could not create window: %s", SDL_GetError());
return ERR_GFXINIT;
}
vm_screen_set_color(screen, 0, 0, 0, 0);
SDL_RenderClear(screen->render);
return OK;
}
2017-12-07 03:25:47 +00:00
/*
2017-12-17 22:42:05 +00:00
* Free the contents of a screen.
2017-12-07 03:25:47 +00:00
*/
2017-12-02 19:05:53 +00:00
void
2017-12-17 22:42:05 +00:00
vm_screen_free(vm_screen *screen)
2017-12-02 19:05:53 +00:00
{
SDL_DestroyRenderer(screen->render);
SDL_DestroyWindow(screen->window);
2017-12-17 22:42:05 +00:00
free(screen);
2017-12-02 19:05:53 +00:00
}
bool
2017-12-17 22:42:05 +00:00
vm_screen_active(vm_screen *screen)
{
static int counter = 5;
if (counter--) {
return true;
}
2017-12-18 02:20:11 +00:00
return false;
}
void
2017-12-17 22:42:05 +00:00
vm_screen_refresh(vm_screen *screen)
{
SDL_RenderPresent(screen->render);
SDL_Delay(2000);
}
2017-12-07 03:25:47 +00:00
/*
2017-12-17 22:42:05 +00:00
* Set the color of a screen screen to a given RGBA value.
2017-12-07 03:25:47 +00:00
*/
2017-11-22 05:24:51 +00:00
void
2017-12-17 22:42:05 +00:00
vm_screen_set_color(vm_screen *screen,
uint8_t red,
uint8_t green,
uint8_t blue,
uint8_t alpha)
2017-11-22 05:24:51 +00:00
{
SDL_SetRenderDrawColor(screen->render, red, green, blue, alpha);
2017-11-22 05:24:51 +00:00
}
2017-12-07 03:25:47 +00:00
/*
* Draw a rectangle on the screen at a given x/y position, with a given
2017-12-17 22:42:05 +00:00
* set of x/y dimensions, with a given screen.
2017-12-07 03:25:47 +00:00
*/
2017-11-22 05:24:51 +00:00
void
2017-12-17 22:42:05 +00:00
vm_screen_draw_rect(vm_screen *screen,
2017-11-22 05:24:51 +00:00
int xpos,
int ypos,
int xsize,
int ysize)
{
// We need to scale the position and sizes, since the presumption is
// that we are plotting based on the literal x and y coordinates in
// the screen.
screen->rect.x = xpos * screen->scale;
screen->rect.y = ypos * screen->scale;
screen->rect.w = xsize * screen->scale;
screen->rect.h = ysize * screen->scale;
SDL_RenderFillRect(screen->render, &screen->rect);
2017-11-22 05:24:51 +00:00
}