More paint / reset fixes

- Refactor the code related to killing requests.
- Free the pins when destroying killed requests (otherwise memory will
  leak).
- Customfont texts have associated requests, so those need to be killed
  if the font goes away.
This commit is contained in:
Ilari Liusvaara 2013-02-28 10:36:57 +02:00
parent 80c9dcee2d
commit 1295518041
5 changed files with 42 additions and 16 deletions

View file

@ -351,6 +351,10 @@ private:
*/
struct render_object
{
/**
* Constructor.
*/
render_object() throw();
/**
* Destructor.
*/
@ -359,6 +363,10 @@ struct render_object
* Kill object function. If it returns true, kill the request. Default is to return false.
*/
virtual bool kill_request(void* obj) throw();
/**
* Return true if myobj and killobj are equal and not NULL.
*/
bool kill_request_ifeq(void* myobj, void* killobj);
/**
* Draw the object.
*

View file

@ -70,6 +70,11 @@ private:
lua_obj_pin& operator=(const lua_obj_pin&);
};
template<typename T> void* unbox_any_pin(struct lua_obj_pin<T>* pin)
{
return pin ? pin->object() : NULL;
}
template<class T> struct lua_class_bind_data
{
int (T::*fn)(lua_State* LS);

View file

@ -577,10 +577,23 @@ render_queue::~render_queue() throw()
clear();
}
render_object::render_object() throw()
{
}
render_object::~render_object() throw()
{
}
bool render_object::kill_request_ifeq(void* myobj, void* killobj)
{
if(!killobj)
return false;
if(myobj == killobj)
return true;
return false;
}
bool render_object::kill_request(void* obj) throw()
{
return false;

View file

@ -51,7 +51,6 @@ namespace
b = _bitmap;
b2 = NULL;
p = _palette;
killed = false;
}
render_object_bitmap(int32_t _x, int32_t _y, lua_obj_pin<lua_dbitmap>* _bitmap) throw()
@ -61,13 +60,10 @@ namespace
b = NULL;
b2 = _bitmap;
p = NULL;
killed = false;
}
~render_object_bitmap() throw()
{
if(killed)
return;
delete b;
delete b2;
delete p;
@ -75,15 +71,9 @@ namespace
bool kill_request(void* obj) throw()
{
if(!obj)
return false; //Can't kill on NULL object.
if(p && p->object() == obj)
return killed = true;
if(b && b->object() == obj)
return killed = true;
if(b2 && b2->object() == obj)
return killed = true;
return false;
return kill_request_ifeq(unbox_any_pin(p), obj) ||
kill_request_ifeq(unbox_any_pin(b), obj) ||
kill_request_ifeq(unbox_any_pin(b2), obj);
}
template<bool T> void composite_op(struct framebuffer<T>& scr) throw()
@ -139,7 +129,6 @@ namespace
lua_obj_pin<lua_bitmap>* b;
lua_obj_pin<lua_dbitmap>* b2;
lua_obj_pin<lua_palette>* p;
bool killed;
};
function_ptr_luafun gui_bitmap("gui.bitmap_draw", [](lua_State* LS, const std::string& fname) -> int {

View file

@ -1,5 +1,6 @@
#include "lua/internal.hpp"
#include "fonts/wrapper.hpp"
#include "core/framebuffer.hpp"
#include "library/framebuffer.hpp"
#include "library/customfont.hpp"
#include "library/utf8.hpp"
@ -42,7 +43,10 @@ namespace
render_object_text_cf(int32_t _x, int32_t _y, const std::string& _text, premultiplied_color _fg,
premultiplied_color _bg, lua_obj_pin<lua_customfont>* _font) throw()
: x(_x), y(_y), text(_text), fg(_fg), bg(_bg), font(_font) {}
~render_object_text_cf() throw() {}
~render_object_text_cf() throw()
{
delete font;
}
template<bool X> void op(struct framebuffer<X>& scr) throw()
{
fg.set_palette(scr);
@ -72,6 +76,10 @@ namespace
}
}
bool kill_request(void* obj) throw()
{
return kill_request_ifeq(unbox_any_pin(font), obj);
}
void operator()(struct framebuffer<true>& scr) throw() { op(scr); }
void operator()(struct framebuffer<false>& scr) throw() { op(scr); }
private:
@ -92,7 +100,10 @@ namespace
}
}
lua_customfont::~lua_customfont() throw() {}
lua_customfont::~lua_customfont() throw()
{
render_kill_request(this);
}
int lua_customfont::draw(lua_State* LS)
{