(Caca) Move caca global state to struct

This commit is contained in:
twinaphex 2020-03-07 06:03:32 +01:00
parent e8ff0fddca
commit bdc31234d5
2 changed files with 55 additions and 55 deletions

View File

@ -31,6 +31,13 @@ typedef struct caca
caca_canvas_t **caca_cv;
caca_dither_t **caca_dither;
caca_display_t **caca_display;
unsigned menu_width;
unsigned menu_height;
unsigned menu_pitch;
unsigned video_width;
unsigned video_height;
unsigned video_pitch;
bool rgb32;
} caca_t;
#endif

View File

@ -34,35 +34,30 @@ static caca_canvas_t *caca_cv = NULL;
static caca_dither_t *caca_dither = NULL;
static caca_display_t *caca_display = NULL;
static unsigned char *caca_menu_frame = NULL;
static unsigned caca_menu_width = 0;
static unsigned caca_menu_height = 0;
static unsigned caca_menu_pitch = 0;
static unsigned caca_video_width = 0;
static unsigned caca_video_height = 0;
static unsigned caca_video_pitch = 0;
static bool caca_rgb32 = false;
static void caca_gfx_free(void *data);
static void caca_gfx_create(void)
static void caca_gfx_create(caca_t *caca)
{
caca_display = caca_create_display(NULL);
caca_cv = caca_get_canvas(caca_display);
if(!caca_video_width || !caca_video_height)
if(!caca->video_width || !caca->video_height)
{
caca_video_width = caca_get_canvas_width(caca_cv);
caca_video_height = caca_get_canvas_height(caca_cv);
caca->video_width = caca_get_canvas_width(caca_cv);
caca->video_height = caca_get_canvas_height(caca_cv);
}
if (caca_rgb32)
caca_dither = caca_create_dither(32, caca_video_width, caca_video_height, caca_video_pitch,
if (caca->rgb32)
caca_dither = caca_create_dither(32, caca->video_width,
caca->video_height, caca->video_pitch,
0x00ff0000, 0xff00, 0xff, 0x0);
else
caca_dither = caca_create_dither(16, caca_video_width, caca_video_height, caca_video_pitch,
caca_dither = caca_create_dither(16, caca->video_width,
caca->video_height, caca->video_pitch,
0xf800, 0x7e0, 0x1f, 0x0);
video_driver_set_size(caca_video_width, caca_video_height);
video_driver_set_size(caca->video_width, caca->video_height);
}
static void *caca_gfx_init(const video_info_t *video,
@ -70,6 +65,9 @@ static void *caca_gfx_init(const video_info_t *video,
{
caca_t *caca = (caca_t*)calloc(1, sizeof(*caca));
if (!caca)
return NULL;
caca->caca_cv = &caca_cv;
caca->caca_dither = &caca_dither;
caca->caca_display = &caca_display;
@ -77,16 +75,16 @@ static void *caca_gfx_init(const video_info_t *video,
*input = NULL;
*input_data = NULL;
caca_video_width = video->width;
caca_video_height = video->height;
caca_rgb32 = video->rgb32;
caca->video_width = video->width;
caca->video_height = video->height;
caca->rgb32 = video->rgb32;
if (video->rgb32)
caca_video_pitch = video->width * 4;
caca->video_pitch = video->width * 4;
else
caca_video_pitch = video->width * 2;
caca->video_pitch = video->width * 2;
caca_gfx_create();
caca_gfx_create(caca);
if (!caca_cv || !caca_dither || !caca_display)
{
@ -111,28 +109,22 @@ static bool caca_gfx_frame(void *data, const void *frame,
unsigned width = 0;
unsigned height = 0;
bool draw = true;
(void)data;
(void)frame;
(void)frame_width;
(void)frame_height;
(void)pitch;
(void)msg;
caca_t *caca = (caca_t*)data;
if (!frame || !frame_width || !frame_height)
return true;
if ( caca_video_width != frame_width ||
caca_video_height != frame_height ||
caca_video_pitch != pitch)
if ( caca->video_width != frame_width ||
caca->video_height != frame_height ||
caca->video_pitch != pitch)
{
if (frame_width > 4 && frame_height > 4)
{
caca_video_width = frame_width;
caca_video_height = frame_height;
caca_video_pitch = pitch;
caca_gfx_free(NULL);
caca_gfx_create();
caca->video_width = frame_width;
caca->video_height = frame_height;
caca->video_pitch = pitch;
caca_gfx_free(caca);
caca_gfx_create(caca);
}
}
@ -189,8 +181,8 @@ static void caca_gfx_set_nonblock_state(void *data, bool a,
static bool caca_gfx_alive(void *data)
{
(void)data;
video_driver_set_size(caca_video_width, caca_video_height);
caca_t *caca = (caca_t*)data;
video_driver_set_size(caca->video_width, caca->video_height);
return true;
}
@ -215,7 +207,7 @@ static bool caca_gfx_has_windowed(void *data)
static void caca_gfx_free(void *data)
{
(void)data;
caca_t *caca = (caca_t*)data;
if (caca_display)
{
@ -257,6 +249,7 @@ static void caca_set_texture_frame(void *data,
const void *frame, bool rgb32, unsigned width, unsigned height,
float alpha)
{
caca_t *caca = (caca_t*)data;
unsigned pitch = width * 2;
if (rgb32)
@ -269,9 +262,9 @@ static void caca_set_texture_frame(void *data,
}
if ( !caca_menu_frame ||
caca_menu_width != width ||
caca_menu_height != height ||
caca_menu_pitch != pitch)
caca->menu_width != width ||
caca->menu_height != height ||
caca->menu_pitch != pitch)
if (pitch && height)
caca_menu_frame = (unsigned char*)malloc(pitch * height);