This commit is contained in:
yvt 2013-11-17 00:26:05 +09:00
parent ba8adaac51
commit e4b83484db
14 changed files with 54 additions and 24 deletions

View File

@ -1269,7 +1269,7 @@ namespace spades {
// Well done! // Well done!
renderer->FrameDone(); renderer->FrameDone();
Handle<Bitmap> bmp = renderer->ReadBitmap(); Handle<Bitmap> bmp(renderer->ReadBitmap(), false);
// force 100% opacity // force 100% opacity
uint32_t *pixels = bmp->GetPixels(); uint32_t *pixels = bmp->GetPixels();

View File

@ -44,6 +44,9 @@ namespace spades {
additive = false; additive = false;
blockHitAction = Delete; blockHitAction = Delete;
if(image != NULL)
image->AddRef();
renderer = cli->GetRenderer(); renderer = cli->GetRenderer();
if(cli->GetWorld()) if(cli->GetWorld())
map = cli->GetWorld()->GetMap(); map = cli->GetWorld()->GetMap();
@ -51,6 +54,12 @@ namespace spades {
map = NULL; map = NULL;
} }
ParticleSpriteEntity::~ParticleSpriteEntity() {
if(image != NULL){
image->Release();
}
}
void ParticleSpriteEntity::SetLifeTime(float lifeTime, void ParticleSpriteEntity::SetLifeTime(float lifeTime,
float fadeIn, float fadeIn,
float fadeOut){ float fadeOut){
@ -162,9 +171,11 @@ namespace spades {
} }
void ParticleSpriteEntity::SetImage(IImage *img) { void ParticleSpriteEntity::SetImage(IImage *img) {
if(img == image) return; if(img == image) return;
image->Release(); if(image != NULL)
image->Release();
image = img; image = img;
image->AddRef(); if(image != NULL)
image->AddRef();
} }
} }
} }

View File

@ -57,7 +57,7 @@ namespace spades {
public: public:
ParticleSpriteEntity(Client *cli, IImage *image, Vector4 color); ParticleSpriteEntity(Client *cli, IImage *image, Vector4 color);
virtual ~ParticleSpriteEntity(){} virtual ~ParticleSpriteEntity();
virtual bool Update(float dt); virtual bool Update(float dt);
virtual void Render3D(); virtual void Render3D();

View File

@ -61,7 +61,7 @@ namespace spades {
Handle<Bitmap> bmp; Handle<Bitmap> bmp;
try{ try{
bmp = new Bitmap(width, height); bmp.Set(new Bitmap(width, height), false);
}catch(...){ }catch(...){
delete img; delete img;
throw; throw;

View File

@ -20,6 +20,7 @@
#include "RefCountedObject.h" #include "RefCountedObject.h"
#include "../ScriptBindings/ScriptManager.h" #include "../ScriptBindings/ScriptManager.h"
#include "Exception.h"
namespace spades { namespace spades {
RefCountedObject::RefCountedObject() { RefCountedObject::RefCountedObject() {
@ -35,7 +36,10 @@ namespace spades {
} }
void RefCountedObject::Release() { void RefCountedObject::Release() {
if(asAtomicInc(refCount) <= 0) int cnt = asAtomicDec(refCount);
if(cnt == 0)
delete this; delete this;
else if(cnt < 0)
SPRaise("Attempted to release already destroyed object");
} }
} }

View File

@ -39,7 +39,7 @@ namespace spades {
class Handle { class Handle {
T *ptr; T *ptr;
public: public:
Handle(T *ptr, bool add = false):ptr(ptr) { Handle(T *ptr, bool add = true):ptr(ptr) {
if(ptr && add) if(ptr && add)
ptr->AddRef(); ptr->AddRef();
} }
@ -68,7 +68,7 @@ namespace spades {
SPAssert(ptr != NULL); SPAssert(ptr != NULL);
return *ptr; return *ptr;
} }
void Set(T *p, bool add = false) { void Set(T *p, bool add = true) {
if(p == ptr){ if(p == ptr){
if(!add) if(!add)
ptr->Release(); ptr->Release();

View File

@ -37,7 +37,7 @@ namespace spades {
for(int i = 0; i < chunkRows * chunkCols; i++) for(int i = 0; i < chunkRows * chunkCols; i++)
chunkInvalid.push_back(false); chunkInvalid.push_back(false);
Handle<Bitmap> bmp = GenerateBitmap(0, 0, m->Width(), m->Height()); Handle<Bitmap> bmp(GenerateBitmap(0, 0, m->Width(), m->Height()), false);
try{ try{
image = static_cast<GLImage *>(renderer->CreateImage(bmp)); image = static_cast<GLImage *>(renderer->CreateImage(bmp));
}catch(...){ }catch(...){
@ -61,7 +61,7 @@ namespace spades {
Bitmap *GLFlatMapRenderer::GenerateBitmap(int mx, int my, int w, int h){ Bitmap *GLFlatMapRenderer::GenerateBitmap(int mx, int my, int w, int h){
SPADES_MARK_FUNCTION(); SPADES_MARK_FUNCTION();
Handle<Bitmap> bmp = new Bitmap(w, h); Handle<Bitmap> bmp(new Bitmap(w, h), false);
try{ try{
uint32_t *pixels = bmp->GetPixels(); uint32_t *pixels = bmp->GetPixels();
@ -112,9 +112,9 @@ namespace spades {
int chunkX = ((int)i) % chunkCols; int chunkX = ((int)i) % chunkCols;
int chunkY = ((int)i) / chunkCols; int chunkY = ((int)i) / chunkCols;
Handle<Bitmap> bmp = GenerateBitmap(chunkX * ChunkSize, Handle<Bitmap> bmp(GenerateBitmap(chunkX * ChunkSize,
chunkY * ChunkSize, chunkY * ChunkSize,
ChunkSize, ChunkSize); ChunkSize, ChunkSize), false);
try{ try{
image->SubImage(bmp, chunkX * ChunkSize, chunkY * ChunkSize); image->SubImage(bmp, chunkX * ChunkSize, chunkY * ChunkSize);
}catch(...){ }catch(...){

View File

@ -63,6 +63,10 @@ namespace spades{
} }
GLImageRenderer::~GLImageRenderer(){ GLImageRenderer::~GLImageRenderer(){
if(image != NULL){
image->Release();
image = NULL;
}
delete positionAttribute; delete positionAttribute;
delete colorAttribute; delete colorAttribute;
delete textureCoordAttribute; delete textureCoordAttribute;
@ -117,6 +121,8 @@ namespace spades{
vertices.clear(); vertices.clear();
indices.clear(); indices.clear();
image->Release();
image = NULL;
} }
void GLImageRenderer::SetImage(spades::draw::GLImage *img){ void GLImageRenderer::SetImage(spades::draw::GLImage *img){
@ -124,6 +130,7 @@ namespace spades{
return; return;
Flush(); Flush();
image = img; image = img;
image->AddRef();
} }
void GLImageRenderer::Add(float dx1, float dy1, void GLImageRenderer::Add(float dx1, float dy1,

View File

@ -32,8 +32,8 @@ namespace spades {
} }
GLModelRenderer::~GLModelRenderer() { GLModelRenderer::~GLModelRenderer() {
SPADES_MARK_FUNCTION(); SPADES_MARK_FUNCTION();
Clear();
} }
void GLModelRenderer::AddModel(GLModel *model, void GLModelRenderer::AddModel(GLModel *model,
@ -43,6 +43,7 @@ namespace spades {
model->renderId = (int)models.size(); model->renderId = (int)models.size();
RenderModel m; RenderModel m;
m.model = model; m.model = model;
model->AddRef();
models.push_back(m); models.push_back(m);
} }
modelCount++; modelCount++;
@ -108,6 +109,7 @@ namespace spades {
// last phase: clear scene // last phase: clear scene
for(size_t i = 0; i < models.size(); i++){ for(size_t i = 0; i < models.size(); i++){
models[i].model->renderId = -1; models[i].model->renderId = -1;
models[i].model->Release();
} }
models.clear(); models.clear();

View File

@ -113,7 +113,7 @@ namespace spades {
} }
BitmapAtlasGenerator::Result result = atlasGen.Pack(); BitmapAtlasGenerator::Result result = atlasGen.Pack();
Handle<Bitmap> bmp = result.bitmap; Handle<Bitmap> bmp(result.bitmap, false);
SPAssert(result.items.size() == bmps.size()); SPAssert(result.items.size() == bmps.size());
for(size_t i = 0; i < bmps.size(); i++){ for(size_t i = 0; i < bmps.size(); i++){
bmps[i]->Release(); bmps[i]->Release();

View File

@ -905,14 +905,14 @@ namespace spades {
device->BindFramebuffer(IGLDevice::Framebuffer, 0); device->BindFramebuffer(IGLDevice::Framebuffer, 0);
device->Enable(IGLDevice::Blend, false); device->Enable(IGLDevice::Blend, false);
device->Viewport(0, 0, handle.GetWidth(), handle.GetHeight()); device->Viewport(0, 0, handle.GetWidth(), handle.GetHeight());
Handle<GLImage> image = new GLImage(handle.GetTexture(), Handle<GLImage> image(new GLImage(handle.GetTexture(),
device, device,
handle.GetWidth(), handle.GetWidth(),
handle.GetHeight(), handle.GetHeight(),
false); false), false);
SetColor(MakeVector4(1, 1, 1, 1)); SetColor(MakeVector4(1, 1, 1, 1));
DrawImage(image, AABB2(0,handle.GetHeight(),handle.GetWidth(),-handle.GetHeight())); DrawImage(image, AABB2(0,handle.GetHeight(),handle.GetWidth(),-handle.GetHeight()));
imageRenderer->Flush(); // must flush now because handle is released soon imageRenderer->Flush();
} }
handle.Release(); handle.Release();
@ -1093,10 +1093,10 @@ namespace spades {
device->BindFramebuffer(IGLDevice::Framebuffer, 0); device->BindFramebuffer(IGLDevice::Framebuffer, 0);
device->Enable(IGLDevice::Blend, false); device->Enable(IGLDevice::Blend, false);
device->Viewport(0, 0, w,h); device->Viewport(0, 0, w,h);
Handle<GLImage> image = new GLImage(lastColorBufferTexture, Handle<GLImage> image(new GLImage(lastColorBufferTexture,
device, device,
w,h, w,h,
false); false), false);
SetColor(MakeVector4(1, 1, 1, 1)); SetColor(MakeVector4(1, 1, 1, 1));
DrawImage(image, AABB2(0,h,w,-h)); DrawImage(image, AABB2(0,h,w,-h));
imageRenderer->Flush(); // must flush now because handle is released soon imageRenderer->Flush(); // must flush now because handle is released soon

View File

@ -260,12 +260,11 @@ namespace spades {
{ {
SDLGLDevice glDevice(surface); SDLGLDevice glDevice(surface);
Handle<draw::GLRenderer> renderer = new draw::GLRenderer(&glDevice); Handle<draw::GLRenderer> renderer(new draw::GLRenderer(&glDevice), false);
audio::ALDevice audio; audio::ALDevice audio;
RunClientLoop(renderer, &audio); RunClientLoop(renderer, &audio);
renderer->Shutdown();
} }
}catch(...){ }catch(...){
SDL_Quit(); SDL_Quit();

View File

@ -20,6 +20,7 @@
#include "ScriptManager.h" #include "ScriptManager.h"
#include <Client/IAudioDevice.h> #include <Client/IAudioDevice.h>
#include <Client/IAudioChunk.h>
namespace spades { namespace spades {
namespace client{ namespace client{
@ -32,7 +33,9 @@ namespace spades {
} }
static IAudioChunk *RegisterSound(const std::string& name, IAudioDevice *dev) { static IAudioChunk *RegisterSound(const std::string& name, IAudioDevice *dev) {
try{ try{
return dev->RegisterSound(name.c_str()); IAudioChunk *c = dev->RegisterSound(name.c_str());
c->AddRef();
return c;
}catch(const std::exception& ex) { }catch(const std::exception& ex) {
ScriptContextUtils().SetNativeException(ex); ScriptContextUtils().SetNativeException(ex);
return NULL; return NULL;

View File

@ -31,7 +31,9 @@ namespace spades {
static IModel *RegisterModel(const std::string& str, static IModel *RegisterModel(const std::string& str,
IRenderer *r) { IRenderer *r) {
try{ try{
return r->RegisterModel(str.c_str()); IModel *m = r->RegisterModel(str.c_str());
m->AddRef();
return m;
}catch(const std::exception& ex) { }catch(const std::exception& ex) {
ScriptContextUtils().SetNativeException(ex); ScriptContextUtils().SetNativeException(ex);
return NULL; return NULL;
@ -40,7 +42,9 @@ namespace spades {
static IImage *RegisterImage(const std::string& str, static IImage *RegisterImage(const std::string& str,
IRenderer *r) { IRenderer *r) {
try{ try{
return r->RegisterImage(str.c_str()); IImage *im = r->RegisterImage(str.c_str());
im->AddRef();
return im;
}catch(const std::exception& ex) { }catch(const std::exception& ex) {
ScriptContextUtils().SetNativeException(ex); ScriptContextUtils().SetNativeException(ex);
return NULL; return NULL;