From 2fb19ea144c97855440fad49e7cf54ada25b1ff0 Mon Sep 17 00:00:00 2001 From: Ilari Liusvaara Date: Wed, 7 Mar 2012 13:33:54 +0200 Subject: [PATCH] Refactor render queue management This makes it easier to switch render queue object memory management to internal implementation, speeding stuff up. --- include/core/render.hpp | 24 +++++++++++++++--------- src/core/render.cpp | 8 ++++++++ src/lua/gui-bitmap.cpp | 4 ++-- src/lua/gui-circle.cpp | 2 +- src/lua/gui-crosshair.cpp | 2 +- src/lua/gui-line.cpp | 2 +- src/lua/gui-pixel.cpp | 2 +- src/lua/gui-rectangle.cpp | 4 ++-- src/lua/gui-text.cpp | 2 +- 9 files changed, 32 insertions(+), 18 deletions(-) diff --git a/include/core/render.hpp b/include/core/render.hpp index 76a76005..41af23ec 100644 --- a/include/core/render.hpp +++ b/include/core/render.hpp @@ -331,15 +331,7 @@ struct premultiplied_color struct render_queue { /** - * Adds new object to render queue. The object must be allocated by new. - * - * parameter obj: The object to add - * throws std::bad_alloc: Not enough memory. - */ - void add(struct render_object& obj) throw(std::bad_alloc); - -/** - * Applies all objects in the queue in order, freeing them in progress. + * Applies all objects in the queue in order. * * parameter scr: The screen to apply queue to. */ @@ -351,10 +343,24 @@ struct render_queue void clear() throw(); /** + * Get memory from internal allocator. + */ + void* alloc(size_t block) throw(std::bad_alloc); + +/** + * Call object constructor on internal memory. + */ + template void create_add(U... args) + { + add(*new(alloc(sizeof(T))) T(args...)); + } + +/** * Destructor. */ ~render_queue() throw(); private: + void add(struct render_object& obj) throw(std::bad_alloc); std::list q; }; diff --git a/src/core/render.cpp b/src/core/render.cpp index 76ce2883..0d514dbc 100644 --- a/src/core/render.cpp +++ b/src/core/render.cpp @@ -305,6 +305,14 @@ void render_queue::clear() throw() q.clear(); } +void* render_queue::alloc(size_t block) throw(std::bad_alloc) +{ + void* ptr = malloc(block); + if(!ptr) + throw std::bad_alloc(); + return ptr; +} + render_queue::~render_queue() throw() { clear(); diff --git a/src/lua/gui-bitmap.cpp b/src/lua/gui-bitmap.cpp index c85a22c5..402d4c57 100644 --- a/src/lua/gui-bitmap.cpp +++ b/src/lua/gui-bitmap.cpp @@ -105,11 +105,11 @@ namespace lua_class::get(LS, 4, fname.c_str()); auto b = lua_class::pin(LS, 3, fname.c_str()); auto p = lua_class::pin(LS, 4, fname.c_str()); - lua_render_ctx->queue->add(*new render_object_bitmap(x, y, b, p)); + lua_render_ctx->queue->create_add(x, y, b, p); } else if(lua_class::is(LS, 3)) { lua_class::get(LS, 3, fname.c_str()); auto b = lua_class::pin(LS, 3, fname.c_str()); - lua_render_ctx->queue->add(*new render_object_bitmap(x, y, b)); + lua_render_ctx->queue->create_add(x, y, b); } else { lua_pushstring(LS, "Expected BITMAP or DBITMAP as argument 3 for gui.bitmap_draw."); lua_error(LS); diff --git a/src/lua/gui-circle.cpp b/src/lua/gui-circle.cpp index 4d9a9ea8..64220b1d 100644 --- a/src/lua/gui-circle.cpp +++ b/src/lua/gui-circle.cpp @@ -67,7 +67,7 @@ namespace get_numeric_argument(LS, 6, fill, fname.c_str()); premultiplied_color poutline(outline); premultiplied_color pfill(fill); - lua_render_ctx->queue->add(*new render_object_circle(x, y, radius, poutline, pfill, thickness)); + lua_render_ctx->queue->create_add(x, y, radius, poutline, pfill, thickness); return 0; }); } diff --git a/src/lua/gui-crosshair.cpp b/src/lua/gui-crosshair.cpp index 9dd4ecc2..666b588c 100644 --- a/src/lua/gui-crosshair.cpp +++ b/src/lua/gui-crosshair.cpp @@ -41,7 +41,7 @@ namespace get_numeric_argument(LS, 3, length, fname.c_str()); get_numeric_argument(LS, 4, color, fname.c_str()); premultiplied_color pcolor(color); - lua_render_ctx->queue->add(*new render_object_crosshair(x, y, pcolor, length)); + lua_render_ctx->queue->create_add(x, y, pcolor, length); return 0; }); } diff --git a/src/lua/gui-line.cpp b/src/lua/gui-line.cpp index d01517cc..1e9f2fa7 100644 --- a/src/lua/gui-line.cpp +++ b/src/lua/gui-line.cpp @@ -90,7 +90,7 @@ nodraw2: int32_t y2 = get_numeric_argument(LS, 4, fname.c_str()); get_numeric_argument(LS, 5, color, fname.c_str()); premultiplied_color pcolor(color); - lua_render_ctx->queue->add(*new render_object_line(x1, x2, y1, y2, pcolor)); + lua_render_ctx->queue->create_add(x1, x2, y1, y2, pcolor); return 0; }); } diff --git a/src/lua/gui-pixel.cpp b/src/lua/gui-pixel.cpp index bbca494f..4089cf81 100644 --- a/src/lua/gui-pixel.cpp +++ b/src/lua/gui-pixel.cpp @@ -33,7 +33,7 @@ namespace int32_t y = get_numeric_argument(LS, 2, fname.c_str()); get_numeric_argument(LS, 3, color, fname.c_str()); premultiplied_color pcolor(color); - lua_render_ctx->queue->add(*new render_object_pixel(x, y, pcolor)); + lua_render_ctx->queue->create_add(x, y, pcolor); return 0; }); } diff --git a/src/lua/gui-rectangle.cpp b/src/lua/gui-rectangle.cpp index bb8bdd2b..48abc5c0 100644 --- a/src/lua/gui-rectangle.cpp +++ b/src/lua/gui-rectangle.cpp @@ -56,8 +56,8 @@ namespace get_numeric_argument(LS, 7, fill, fname.c_str()); premultiplied_color poutline(outline); premultiplied_color pfill(fill); - lua_render_ctx->queue->add(*new render_object_rectangle(x, y, width, height, poutline, pfill, - thickness)); + lua_render_ctx->queue->create_add(x, y, width, height, poutline, pfill, + thickness); return 0; }); } diff --git a/src/lua/gui-text.cpp b/src/lua/gui-text.cpp index 6b259836..c8073d02 100644 --- a/src/lua/gui-text.cpp +++ b/src/lua/gui-text.cpp @@ -38,7 +38,7 @@ namespace std::string text = get_string_argument(LS, 3, fname.c_str()); premultiplied_color fg(fgc); premultiplied_color bg(bgc); - lua_render_ctx->queue->add(*new render_object_text(_x, _y, text, fg, bg, hdbl, vdbl)); + lua_render_ctx->queue->create_add(_x, _y, text, fg, bg, hdbl, vdbl); return 0; } -- 2.11.4.GIT