(VGA) Move static global variables to struct

This commit is contained in:
twinaphex 2020-03-07 19:33:03 +01:00
parent 9ae4031d13
commit 83ac24e7f1
2 changed files with 82 additions and 87 deletions

View File

@ -24,6 +24,19 @@
typedef struct vga
{
bool color;
bool vga_rgb32;
unsigned vga_menu_width;
unsigned vga_menu_height;
unsigned vga_menu_pitch;
unsigned vga_menu_bits;
unsigned vga_video_width;
unsigned vga_video_height;
unsigned vga_video_pitch;
unsigned vga_video_bits;
unsigned char *vga_menu_frame;
unsigned char *vga_frame;
} vga_t;
#endif

View File

@ -30,17 +30,6 @@
#include "../../driver.h"
#include "../../verbosity.h"
static unsigned char *vga_menu_frame = NULL;
static unsigned char *vga_frame = NULL;
static unsigned vga_menu_width = 0;
static unsigned vga_menu_height = 0;
static unsigned vga_menu_pitch = 0;
static unsigned vga_menu_bits = 0;
static unsigned vga_video_width = 0;
static unsigned vga_video_height = 0;
static unsigned vga_video_pitch = 0;
static unsigned vga_video_bits = 0;
static bool vga_rgb32 = false;
static void vga_set_mode_13h(void)
{
@ -112,27 +101,27 @@ static void vga_gfx_create(void)
static void *vga_gfx_init(const video_info_t *video,
input_driver_t **input, void **input_data)
{
vga_t *vga = (vga_t*)calloc(1, sizeof(*vga));
vga_t *vga = (vga_t*)calloc(1, sizeof(*vga));
*input = NULL;
*input_data = NULL;
vga_video_width = video->width;
vga_video_height = video->height;
vga_rgb32 = video->rgb32;
vga->video_width = video->width;
vga->video_height = video->height;
vga->rgb32 = video->rgb32;
if (video->rgb32)
{
vga_video_pitch = video->width * 4;
vga_video_bits = 32;
vga->video_pitch = video->width * 4;
vga->video_bits = 32;
}
else
{
vga_video_pitch = video->width * 2;
vga_video_bits = 16;
vga->video_pitch = video->width * 2;
vga->video_bits = 16;
}
vga_frame = (unsigned char*)malloc(VGA_WIDTH * VGA_HEIGHT);
vga->frame = (unsigned char*)malloc(VGA_WIDTH * VGA_HEIGHT);
vga_gfx_create();
@ -149,19 +138,12 @@ static bool vga_gfx_frame(void *data, const void *frame,
unsigned frame_width, unsigned frame_height, uint64_t frame_count,
unsigned pitch, const char *msg, video_frame_info_t *video_info)
{
size_t len = 0;
void *buffer = NULL;
unsigned width, height, bits;
size_t len = 0;
void *buffer = NULL;
const void *frame_to_copy = frame;
bool draw = true;
(void)data;
(void)frame;
(void)frame_width;
(void)frame_height;
(void)pitch;
(void)msg;
(void)bits;
bool draw = true;
vga_t *vga = (vga_t*)data;
if (!frame || !frame_width || !frame_height)
return true;
@ -170,32 +152,32 @@ static bool vga_gfx_frame(void *data, const void *frame,
menu_driver_frame(video_info);
#endif
if ( vga_video_width != frame_width ||
vga_video_height != frame_height ||
vga_video_pitch != pitch)
if ( vga->video_width != frame_width ||
vga->video_height != frame_height ||
vga->video_pitch != pitch)
{
if (frame_width > 4 && frame_height > 4)
{
vga_video_width = frame_width;
vga_video_height = frame_height;
vga_video_pitch = pitch;
vga->video_width = frame_width;
vga->video_height = frame_height;
vga->video_pitch = pitch;
}
}
if (vga_menu_frame && video_info->menu_is_alive)
if (vga->menu_frame && video_info->menu_is_alive)
{
frame_to_copy = vga_menu_frame;
width = vga_menu_width;
height = vga_menu_height;
pitch = vga_menu_pitch;
bits = vga_menu_bits;
frame_to_copy = vga->menu_frame;
width = vga->menu_width;
height = vga->menu_height;
pitch = vga->menu_pitch;
bits = vga->menu_bits;
}
else
{
width = vga_video_width;
height = vga_video_height;
pitch = vga_video_pitch;
bits = vga_video_bits;
width = vga->video_width;
height = vga->video_height;
pitch = vga->video_pitch;
bits = vga->video_bits;
if (frame_width == 4 && frame_height == 4 && (frame_width < width && frame_height < height))
draw = false;
@ -208,12 +190,15 @@ static bool vga_gfx_frame(void *data, const void *frame,
{
vga_vsync();
if (frame_to_copy == vga_menu_frame)
dosmemput(frame_to_copy, MIN(VGA_WIDTH,width)*MIN(VGA_HEIGHT,height), 0xA0000);
if (frame_to_copy == vga->menu_frame)
dosmemput(frame_to_copy,
MIN(VGA_WIDTH,width)*MIN(VGA_HEIGHT,height), 0xA0000);
else
{
if (bits == 32)
(void)bits;
{
/* TODO/FIXME - needs to be implemented */
}
else if (bits == 16)
{
unsigned x, y;
@ -223,8 +208,8 @@ static bool vga_gfx_frame(void *data, const void *frame,
for (x = 0; x < VGA_WIDTH; x++)
{
/* scale incoming frame to fit the screen */
unsigned scaled_x = (width * x) / VGA_WIDTH;
unsigned scaled_y = (height * y) / VGA_HEIGHT;
unsigned scaled_x = (width * x) / VGA_WIDTH;
unsigned scaled_y = (height * y) / VGA_HEIGHT;
unsigned short pixel = ((unsigned short*)frame_to_copy)[width * scaled_y + scaled_x];
/* convert RGB565 to BGR332 */
@ -232,11 +217,11 @@ static bool vga_gfx_frame(void *data, const void *frame,
unsigned g = ((pixel & 0x07E0) >> 8);
unsigned b = ((pixel & 0x001F) >> 3);
vga_frame[VGA_WIDTH * y + x] = (b << 6) | (g << 3) | r;
vga->frame[VGA_WIDTH * y + x] = (b << 6) | (g << 3) | r;
}
}
dosmemput(vga_frame, VGA_WIDTH*VGA_HEIGHT, 0xA0000);
dosmemput(vga->frame, VGA_WIDTH*VGA_HEIGHT, 0xA0000);
}
}
}
@ -252,10 +237,12 @@ static bool vga_gfx_frame(void *data, const void *frame,
static void vga_gfx_set_nonblock_state(void *a, bool b, bool c, unsigned d) { }
static bool vga_gfx_alive(void *data)
{
(void)data;
video_driver_set_size(vga_video_width, vga_video_height);
vga_t *vga = (vga_t*)data;
/* TODO/FIXME - check if this is valid */
video_driver_set_size(vga->video_width, vga->video_height);
return true;
}
@ -274,19 +261,18 @@ static bool vga_gfx_suppress_screensaver(void *data, bool enable)
static void vga_gfx_free(void *data)
{
(void)data;
vga_t *vga = (vga_t*)data;
if (vga_frame)
{
free(vga_frame);
vga_frame = NULL;
}
if (!vga)
return;
if (vga_menu_frame)
{
free(vga_menu_frame);
vga_menu_frame = NULL;
}
if (vga->frame)
free(vga->frame);
vga->frame = NULL;
if (vga->menu_frame)
free(vga->menu_frame);
vga->menu_frame = NULL;
vga_return_to_text_mode();
}
@ -305,32 +291,28 @@ static void vga_set_texture_frame(void *data,
const void *frame, bool rgb32, unsigned width, unsigned height,
float alpha)
{
vga_t *vga = (vga_t*)data;
unsigned pitch = width * 2;
if (rgb32)
pitch = width * 4;
if (vga_menu_frame)
{
free(vga_menu_frame);
vga_menu_frame = NULL;
}
if (vga->menu_frame)
free(vga->menu_frame);
vga->menu_frame = NULL;
if ( !vga_menu_frame ||
vga_menu_width != width ||
vga_menu_height != height ||
vga_menu_pitch != pitch)
if ( !vga->menu_frame ||
vga->menu_width != width ||
vga->menu_height != height ||
vga->menu_pitch != pitch)
if (pitch && height)
vga_menu_frame = (unsigned char*)malloc(VGA_WIDTH * VGA_HEIGHT);
vga->menu_frame = (unsigned char*)malloc(VGA_WIDTH * VGA_HEIGHT);
if (vga_menu_frame && frame && pitch && height)
if (vga->menu_frame && frame && pitch && height)
{
unsigned x, y;
if (rgb32)
{
}
else
if (!rgb32)
{
unsigned short *video_frame = (unsigned short*)frame;
@ -345,15 +327,15 @@ static void vga_set_texture_frame(void *data,
unsigned r = ((pixel & 0xF000) >> 13);
unsigned g = ((pixel & 0xF00) >> 9);
unsigned b = ((pixel & 0xF0) >> 6);
vga_menu_frame[VGA_WIDTH * y + x] = (b << 6) | (g << 3) | r;
vga->menu_frame[VGA_WIDTH * y + x] = (b << 6) | (g << 3) | r;
}
}
}
vga_menu_width = width;
vga_menu_height = height;
vga_menu_pitch = pitch;
vga_menu_bits = rgb32 ? 32 : 16;
vga->menu_width = width;
vga->menu_height = height;
vga->menu_pitch = pitch;
vga->menu_bits = rgb32 ? 32 : 16;
}
}