Attach command buffer to Renderer Window (#1472)
This commit is contained in:
parent
8e71d3f050
commit
dfaa7be4cc
|
@ -311,7 +311,7 @@ static int f_set_clip_rect(lua_State *L) {
|
||||||
lua_Number w = luaL_checknumber(L, 3);
|
lua_Number w = luaL_checknumber(L, 3);
|
||||||
lua_Number h = luaL_checknumber(L, 4);
|
lua_Number h = luaL_checknumber(L, 4);
|
||||||
RenRect rect = rect_to_grid(x, y, w, h);
|
RenRect rect = rect_to_grid(x, y, w, h);
|
||||||
rencache_set_clip_rect(rect);
|
rencache_set_clip_rect(&window_renderer, rect);
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -323,7 +323,7 @@ static int f_draw_rect(lua_State *L) {
|
||||||
lua_Number h = luaL_checknumber(L, 4);
|
lua_Number h = luaL_checknumber(L, 4);
|
||||||
RenRect rect = rect_to_grid(x, y, w, h);
|
RenRect rect = rect_to_grid(x, y, w, h);
|
||||||
RenColor color = checkcolor(L, 5, 255);
|
RenColor color = checkcolor(L, 5, 255);
|
||||||
rencache_draw_rect(rect, color);
|
rencache_draw_rect(&window_renderer, rect, color);
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -65,10 +65,7 @@ static unsigned cells_buf2[CELLS_X * CELLS_Y];
|
||||||
static unsigned *cells_prev = cells_buf1;
|
static unsigned *cells_prev = cells_buf1;
|
||||||
static unsigned *cells = cells_buf2;
|
static unsigned *cells = cells_buf2;
|
||||||
static RenRect rect_buf[CELLS_X * CELLS_Y / 2];
|
static RenRect rect_buf[CELLS_X * CELLS_Y / 2];
|
||||||
size_t command_buf_size = 0;
|
|
||||||
uint8_t *command_buf = NULL;
|
|
||||||
static bool resize_issue;
|
static bool resize_issue;
|
||||||
static int command_buf_idx;
|
|
||||||
static RenRect screen_rect;
|
static RenRect screen_rect;
|
||||||
static RenRect last_clip_rect;
|
static RenRect last_clip_rect;
|
||||||
static bool show_debug;
|
static bool show_debug;
|
||||||
|
@ -116,21 +113,21 @@ static RenRect merge_rects(RenRect a, RenRect b) {
|
||||||
return (RenRect) { x1, y1, x2 - x1, y2 - y1 };
|
return (RenRect) { x1, y1, x2 - x1, y2 - y1 };
|
||||||
}
|
}
|
||||||
|
|
||||||
static bool expand_command_buffer() {
|
static bool expand_command_buffer(RenWindow *window_renderer) {
|
||||||
size_t new_size = command_buf_size * CMD_BUF_RESIZE_RATE;
|
size_t new_size = window_renderer->command_buf_size * CMD_BUF_RESIZE_RATE;
|
||||||
if (new_size == 0) {
|
if (new_size == 0) {
|
||||||
new_size = CMD_BUF_INIT_SIZE;
|
new_size = CMD_BUF_INIT_SIZE;
|
||||||
}
|
}
|
||||||
uint8_t *new_command_buf = realloc(command_buf, new_size);
|
uint8_t *new_command_buf = realloc(window_renderer->command_buf, new_size);
|
||||||
if (!new_command_buf) {
|
if (!new_command_buf) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
command_buf_size = new_size;
|
window_renderer->command_buf_size = new_size;
|
||||||
command_buf = new_command_buf;
|
window_renderer->command_buf = new_command_buf;
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
static void* push_command(enum CommandType type, int size) {
|
static void* push_command(RenWindow *window_renderer, enum CommandType type, int size) {
|
||||||
if (resize_issue) {
|
if (resize_issue) {
|
||||||
// Don't push new commands as we had problems resizing the command buffer.
|
// Don't push new commands as we had problems resizing the command buffer.
|
||||||
// Let's wait for the next frame.
|
// Let's wait for the next frame.
|
||||||
|
@ -139,17 +136,17 @@ static void* push_command(enum CommandType type, int size) {
|
||||||
size_t alignment = alignof(max_align_t) - 1;
|
size_t alignment = alignof(max_align_t) - 1;
|
||||||
size += COMMAND_BARE_SIZE;
|
size += COMMAND_BARE_SIZE;
|
||||||
size = (size + alignment) & ~alignment;
|
size = (size + alignment) & ~alignment;
|
||||||
int n = command_buf_idx + size;
|
int n = window_renderer->command_buf_idx + size;
|
||||||
while (n > command_buf_size) {
|
while (n > window_renderer->command_buf_size) {
|
||||||
if (!expand_command_buffer()) {
|
if (!expand_command_buffer(window_renderer)) {
|
||||||
fprintf(stderr, "Warning: (" __FILE__ "): unable to resize command buffer (%zu)\n",
|
fprintf(stderr, "Warning: (" __FILE__ "): unable to resize command buffer (%zu)\n",
|
||||||
(size_t)(command_buf_size * CMD_BUF_RESIZE_RATE));
|
(size_t)(window_renderer->command_buf_size * CMD_BUF_RESIZE_RATE));
|
||||||
resize_issue = true;
|
resize_issue = true;
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Command *cmd = (Command*) (command_buf + command_buf_idx);
|
Command *cmd = (Command*) (window_renderer->command_buf + window_renderer->command_buf_idx);
|
||||||
command_buf_idx = n;
|
window_renderer->command_buf_idx = n;
|
||||||
memset(cmd, 0, size);
|
memset(cmd, 0, size);
|
||||||
cmd->type = type;
|
cmd->type = type;
|
||||||
cmd->size = size;
|
cmd->size = size;
|
||||||
|
@ -157,13 +154,13 @@ static void* push_command(enum CommandType type, int size) {
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
static bool next_command(Command **prev) {
|
static bool next_command(RenWindow *window_renderer, Command **prev) {
|
||||||
if (*prev == NULL) {
|
if (*prev == NULL) {
|
||||||
*prev = (Command*) command_buf;
|
*prev = (Command*) window_renderer->command_buf;
|
||||||
} else {
|
} else {
|
||||||
*prev = (Command*) (((char*) *prev) + (*prev)->size);
|
*prev = (Command*) (((char*) *prev) + (*prev)->size);
|
||||||
}
|
}
|
||||||
return *prev != ((Command*) (command_buf + command_buf_idx));
|
return *prev != ((Command*) (window_renderer->command_buf + window_renderer->command_buf_idx));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@ -172,8 +169,8 @@ void rencache_show_debug(bool enable) {
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
void rencache_set_clip_rect(RenRect rect) {
|
void rencache_set_clip_rect(RenWindow *window_renderer, RenRect rect) {
|
||||||
SetClipCommand *cmd = push_command(SET_CLIP, sizeof(SetClipCommand));
|
SetClipCommand *cmd = push_command(window_renderer, SET_CLIP, sizeof(SetClipCommand));
|
||||||
if (cmd) {
|
if (cmd) {
|
||||||
cmd->rect = intersect_rects(rect, screen_rect);
|
cmd->rect = intersect_rects(rect, screen_rect);
|
||||||
last_clip_rect = cmd->rect;
|
last_clip_rect = cmd->rect;
|
||||||
|
@ -181,11 +178,11 @@ void rencache_set_clip_rect(RenRect rect) {
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
void rencache_draw_rect(RenRect rect, RenColor color) {
|
void rencache_draw_rect(RenWindow *window_renderer, RenRect rect, RenColor color) {
|
||||||
if (rect.width == 0 || rect.height == 0 || !rects_overlap(last_clip_rect, rect)) {
|
if (rect.width == 0 || rect.height == 0 || !rects_overlap(last_clip_rect, rect)) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
DrawRectCommand *cmd = push_command(DRAW_RECT, sizeof(DrawRectCommand));
|
DrawRectCommand *cmd = push_command(window_renderer, DRAW_RECT, sizeof(DrawRectCommand));
|
||||||
if (cmd) {
|
if (cmd) {
|
||||||
cmd->rect = rect;
|
cmd->rect = rect;
|
||||||
cmd->color = color;
|
cmd->color = color;
|
||||||
|
@ -198,7 +195,7 @@ double rencache_draw_text(RenWindow *window_renderer, RenFont **fonts, const cha
|
||||||
RenRect rect = { x, y, (int)width, ren_font_group_get_height(fonts) };
|
RenRect rect = { x, y, (int)width, ren_font_group_get_height(fonts) };
|
||||||
if (rects_overlap(last_clip_rect, rect)) {
|
if (rects_overlap(last_clip_rect, rect)) {
|
||||||
int sz = len + 1;
|
int sz = len + 1;
|
||||||
DrawTextCommand *cmd = push_command(DRAW_TEXT, sizeof(DrawTextCommand) + sz);
|
DrawTextCommand *cmd = push_command(window_renderer, DRAW_TEXT, sizeof(DrawTextCommand) + sz);
|
||||||
if (cmd) {
|
if (cmd) {
|
||||||
memcpy(cmd->text, text, sz);
|
memcpy(cmd->text, text, sz);
|
||||||
cmd->color = color;
|
cmd->color = color;
|
||||||
|
@ -265,7 +262,7 @@ void rencache_end_frame(RenWindow *window_renderer) {
|
||||||
/* update cells from commands */
|
/* update cells from commands */
|
||||||
Command *cmd = NULL;
|
Command *cmd = NULL;
|
||||||
RenRect cr = screen_rect;
|
RenRect cr = screen_rect;
|
||||||
while (next_command(&cmd)) {
|
while (next_command(window_renderer, &cmd)) {
|
||||||
/* cmd->command[0] should always be the Command rect */
|
/* cmd->command[0] should always be the Command rect */
|
||||||
if (cmd->type == SET_CLIP) { cr = cmd->command[0]; }
|
if (cmd->type == SET_CLIP) { cr = cmd->command[0]; }
|
||||||
RenRect r = intersect_rects(cmd->command[0], cr);
|
RenRect r = intersect_rects(cmd->command[0], cr);
|
||||||
|
@ -308,7 +305,7 @@ void rencache_end_frame(RenWindow *window_renderer) {
|
||||||
ren_set_clip_rect(window_renderer, r);
|
ren_set_clip_rect(window_renderer, r);
|
||||||
|
|
||||||
cmd = NULL;
|
cmd = NULL;
|
||||||
while (next_command(&cmd)) {
|
while (next_command(window_renderer, &cmd)) {
|
||||||
SetClipCommand *ccmd = (SetClipCommand*)&cmd->command;
|
SetClipCommand *ccmd = (SetClipCommand*)&cmd->command;
|
||||||
DrawRectCommand *rcmd = (DrawRectCommand*)&cmd->command;
|
DrawRectCommand *rcmd = (DrawRectCommand*)&cmd->command;
|
||||||
DrawTextCommand *tcmd = (DrawTextCommand*)&cmd->command;
|
DrawTextCommand *tcmd = (DrawTextCommand*)&cmd->command;
|
||||||
|
@ -341,6 +338,6 @@ void rencache_end_frame(RenWindow *window_renderer) {
|
||||||
unsigned *tmp = cells;
|
unsigned *tmp = cells;
|
||||||
cells = cells_prev;
|
cells = cells_prev;
|
||||||
cells_prev = tmp;
|
cells_prev = tmp;
|
||||||
command_buf_idx = 0;
|
window_renderer->command_buf_idx = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -6,8 +6,8 @@
|
||||||
#include "renderer.h"
|
#include "renderer.h"
|
||||||
|
|
||||||
void rencache_show_debug(bool enable);
|
void rencache_show_debug(bool enable);
|
||||||
void rencache_set_clip_rect(RenRect rect);
|
void rencache_set_clip_rect(RenWindow *window_renderer, RenRect rect);
|
||||||
void rencache_draw_rect(RenRect rect, RenColor color);
|
void rencache_draw_rect(RenWindow *window_renderer, RenRect rect, RenColor color);
|
||||||
double rencache_draw_text(RenWindow *window_renderer, RenFont **font, const char *text, size_t len, double x, int y, RenColor color);
|
double rencache_draw_text(RenWindow *window_renderer, RenFont **font, const char *text, size_t len, double x, int y, RenColor color);
|
||||||
void rencache_invalidate(void);
|
void rencache_invalidate(void);
|
||||||
void rencache_begin_frame(RenWindow *window_renderer);
|
void rencache_begin_frame(RenWindow *window_renderer);
|
||||||
|
|
|
@ -506,13 +506,11 @@ void ren_draw_rect(RenSurface *rs, RenRect rect, RenColor color) {
|
||||||
|
|
||||||
/*************** Window Management ****************/
|
/*************** Window Management ****************/
|
||||||
void ren_free_window_resources(RenWindow *window_renderer) {
|
void ren_free_window_resources(RenWindow *window_renderer) {
|
||||||
extern uint8_t *command_buf;
|
|
||||||
extern size_t command_buf_size;
|
|
||||||
renwin_free(window_renderer);
|
renwin_free(window_renderer);
|
||||||
SDL_FreeSurface(draw_rect_surface);
|
SDL_FreeSurface(draw_rect_surface);
|
||||||
free(command_buf);
|
free(window_renderer->command_buf);
|
||||||
command_buf = NULL;
|
window_renderer->command_buf = NULL;
|
||||||
command_buf_size = 0;
|
window_renderer->command_buf_size = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO remove global and return RenWindow*
|
// TODO remove global and return RenWindow*
|
||||||
|
@ -525,6 +523,7 @@ void ren_init(SDL_Window *win) {
|
||||||
}
|
}
|
||||||
window_renderer.window = win;
|
window_renderer.window = win;
|
||||||
renwin_init_surface(&window_renderer);
|
renwin_init_surface(&window_renderer);
|
||||||
|
renwin_init_command_buf(&window_renderer);
|
||||||
renwin_clip_to_surface(&window_renderer);
|
renwin_clip_to_surface(&window_renderer);
|
||||||
draw_rect_surface = SDL_CreateRGBSurface(0, 1, 1, 32,
|
draw_rect_surface = SDL_CreateRGBSurface(0, 1, 1, 32,
|
||||||
0xFF000000, 0x00FF0000, 0x0000FF00, 0x000000FF);
|
0xFF000000, 0x00FF0000, 0x0000FF00, 0x000000FF);
|
||||||
|
|
|
@ -45,6 +45,12 @@ void renwin_init_surface(UNUSED RenWindow *ren) {
|
||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void renwin_init_command_buf(RenWindow *ren) {
|
||||||
|
ren->command_buf = NULL;
|
||||||
|
ren->command_buf_idx = 0;
|
||||||
|
ren->command_buf_size = 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
static RenRect scaled_rect(const RenRect rect, const int scale) {
|
static RenRect scaled_rect(const RenRect rect, const int scale) {
|
||||||
return (RenRect) {rect.x * scale, rect.y * scale, rect.width * scale, rect.height * scale};
|
return (RenRect) {rect.x * scale, rect.y * scale, rect.width * scale, rect.height * scale};
|
||||||
|
|
|
@ -3,6 +3,9 @@
|
||||||
|
|
||||||
struct RenWindow {
|
struct RenWindow {
|
||||||
SDL_Window *window;
|
SDL_Window *window;
|
||||||
|
uint8_t *command_buf;
|
||||||
|
size_t command_buf_idx;
|
||||||
|
size_t command_buf_size;
|
||||||
#ifdef LITE_USE_SDL_RENDERER
|
#ifdef LITE_USE_SDL_RENDERER
|
||||||
SDL_Renderer *renderer;
|
SDL_Renderer *renderer;
|
||||||
SDL_Texture *texture;
|
SDL_Texture *texture;
|
||||||
|
@ -12,6 +15,7 @@ struct RenWindow {
|
||||||
typedef struct RenWindow RenWindow;
|
typedef struct RenWindow RenWindow;
|
||||||
|
|
||||||
void renwin_init_surface(RenWindow *ren);
|
void renwin_init_surface(RenWindow *ren);
|
||||||
|
void renwin_init_command_buf(RenWindow *ren);
|
||||||
void renwin_clip_to_surface(RenWindow *ren);
|
void renwin_clip_to_surface(RenWindow *ren);
|
||||||
void renwin_set_clip_rect(RenWindow *ren, RenRect rect);
|
void renwin_set_clip_rect(RenWindow *ren, RenRect rect);
|
||||||
void renwin_resize_surface(RenWindow *ren);
|
void renwin_resize_surface(RenWindow *ren);
|
||||||
|
|
Loading…
Reference in New Issue