(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_canvas_t **caca_cv;
caca_dither_t **caca_dither; caca_dither_t **caca_dither;
caca_display_t **caca_display; 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; } caca_t;
#endif #endif

View File

@ -34,35 +34,30 @@ static caca_canvas_t *caca_cv = NULL;
static caca_dither_t *caca_dither = NULL; static caca_dither_t *caca_dither = NULL;
static caca_display_t *caca_display = NULL; static caca_display_t *caca_display = NULL;
static unsigned char *caca_menu_frame = 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_free(void *data);
static void caca_gfx_create(void) static void caca_gfx_create(caca_t *caca)
{ {
caca_display = caca_create_display(NULL); caca_display = caca_create_display(NULL);
caca_cv = caca_get_canvas(caca_display); 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_width = caca_get_canvas_width(caca_cv);
caca_video_height = caca_get_canvas_height(caca_cv); caca->video_height = caca_get_canvas_height(caca_cv);
} }
if (caca_rgb32) if (caca->rgb32)
caca_dither = caca_create_dither(32, caca_video_width, caca_video_height, caca_video_pitch, caca_dither = caca_create_dither(32, caca->video_width,
0x00ff0000, 0xff00, 0xff, 0x0); caca->video_height, caca->video_pitch,
0x00ff0000, 0xff00, 0xff, 0x0);
else 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,
0xf800, 0x7e0, 0x1f, 0x0); 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, static void *caca_gfx_init(const video_info_t *video,
@ -70,23 +65,26 @@ static void *caca_gfx_init(const video_info_t *video,
{ {
caca_t *caca = (caca_t*)calloc(1, sizeof(*caca)); caca_t *caca = (caca_t*)calloc(1, sizeof(*caca));
caca->caca_cv = &caca_cv; if (!caca)
caca->caca_dither = &caca_dither; return NULL;
caca->caca_display = &caca_display;
*input = NULL; caca->caca_cv = &caca_cv;
*input_data = NULL; caca->caca_dither = &caca_dither;
caca->caca_display = &caca_display;
caca_video_width = video->width; *input = NULL;
caca_video_height = video->height; *input_data = NULL;
caca_rgb32 = video->rgb32;
caca->video_width = video->width;
caca->video_height = video->height;
caca->rgb32 = video->rgb32;
if (video->rgb32) if (video->rgb32)
caca_video_pitch = video->width * 4; caca->video_pitch = video->width * 4;
else 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) if (!caca_cv || !caca_dither || !caca_display)
{ {
@ -105,34 +103,28 @@ static bool caca_gfx_frame(void *data, const void *frame,
unsigned frame_width, unsigned frame_height, uint64_t frame_count, unsigned frame_width, unsigned frame_height, uint64_t frame_count,
unsigned pitch, const char *msg, video_frame_info_t *video_info) unsigned pitch, const char *msg, video_frame_info_t *video_info)
{ {
size_t len = 0; size_t len = 0;
void *buffer = NULL; void *buffer = NULL;
const void *frame_to_copy = frame; const void *frame_to_copy = frame;
unsigned width = 0; unsigned width = 0;
unsigned height = 0; unsigned height = 0;
bool draw = true; bool draw = true;
caca_t *caca = (caca_t*)data;
(void)data;
(void)frame;
(void)frame_width;
(void)frame_height;
(void)pitch;
(void)msg;
if (!frame || !frame_width || !frame_height) if (!frame || !frame_width || !frame_height)
return true; return true;
if ( caca_video_width != frame_width || if ( caca->video_width != frame_width ||
caca_video_height != frame_height || caca->video_height != frame_height ||
caca_video_pitch != pitch) caca->video_pitch != pitch)
{ {
if (frame_width > 4 && frame_height > 4) if (frame_width > 4 && frame_height > 4)
{ {
caca_video_width = frame_width; caca->video_width = frame_width;
caca_video_height = frame_height; caca->video_height = frame_height;
caca_video_pitch = pitch; caca->video_pitch = pitch;
caca_gfx_free(NULL); caca_gfx_free(caca);
caca_gfx_create(); 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) static bool caca_gfx_alive(void *data)
{ {
(void)data; caca_t *caca = (caca_t*)data;
video_driver_set_size(caca_video_width, caca_video_height); video_driver_set_size(caca->video_width, caca->video_height);
return true; return true;
} }
@ -215,7 +207,7 @@ static bool caca_gfx_has_windowed(void *data)
static void caca_gfx_free(void *data) static void caca_gfx_free(void *data)
{ {
(void)data; caca_t *caca = (caca_t*)data;
if (caca_display) if (caca_display)
{ {
@ -257,6 +249,7 @@ static void caca_set_texture_frame(void *data,
const void *frame, bool rgb32, unsigned width, unsigned height, const void *frame, bool rgb32, unsigned width, unsigned height,
float alpha) float alpha)
{ {
caca_t *caca = (caca_t*)data;
unsigned pitch = width * 2; unsigned pitch = width * 2;
if (rgb32) if (rgb32)
@ -269,9 +262,9 @@ static void caca_set_texture_frame(void *data,
} }
if ( !caca_menu_frame || if ( !caca_menu_frame ||
caca_menu_width != width || caca->menu_width != width ||
caca_menu_height != height || caca->menu_height != height ||
caca_menu_pitch != pitch) caca->menu_pitch != pitch)
if (pitch && height) if (pitch && height)
caca_menu_frame = (unsigned char*)malloc(pitch * height); caca_menu_frame = (unsigned char*)malloc(pitch * height);