From 48c7070243882d9480f862d8e53a7dc89e2cb312 Mon Sep 17 00:00:00 2001 From: Sam Hocevar Date: Wed, 18 Apr 2012 18:20:30 +0000 Subject: [PATCH] gpu: replace exit(0) in D3D error checks with lol::Abort(). --- src/Makefile.am | 2 +- src/core.h | 1 + src/debug/debug.h | 32 ++++++++++++++++++++++++++++++++ src/gpu/vertexbuffer.cpp | 33 ++++++++++++++++++++++----------- src/platform/sdl/sdlapp.cpp | 9 ++++++--- src/scene.cpp | 12 +----------- src/video.cpp | 5 +++-- test/tutorial/tut01.cpp | 2 +- test/tutorial/tut02.cpp | 17 +++++++++++------ win32/lolcore.vcxproj | 1 + 10 files changed, 79 insertions(+), 35 deletions(-) create mode 100644 src/debug/debug.h diff --git a/src/Makefile.am b/src/Makefile.am index 8113f3b0..3b573a3b 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -2,7 +2,7 @@ noinst_LIBRARIES = liblol.a liblol_a_SOURCES = \ - core.h tiler.cpp tiler.h dict.cpp dict.h \ + core.h tiler.cpp tiler.h dict.cpp dict.h debug/debug.h \ audio.cpp audio.h scene.cpp scene.h font.cpp font.h layer.cpp layer.h \ map.cpp map.h entity.cpp entity.h ticker.cpp ticker.h lolgl.h \ tileset.cpp tileset.h forge.cpp forge.h video.cpp video.h log.cpp log.h \ diff --git a/src/core.h b/src/core.h index c3e7cf8f..5569bf33 100644 --- a/src/core.h +++ b/src/core.h @@ -71,6 +71,7 @@ static inline int isnan(float f) #include "thread/thread.h" // Static classes +#include "debug/debug.h" #include "log.h" #include "platform.h" #include "video.h" diff --git a/src/debug/debug.h b/src/debug/debug.h new file mode 100644 index 00000000..521c907f --- /dev/null +++ b/src/debug/debug.h @@ -0,0 +1,32 @@ +// +// Lol Engine +// +// Copyright: (c) 2010-2012 Sam Hocevar +// This program is free software; you can redistribute it and/or +// modify it under the terms of the Do What The Fuck You Want To +// Public License, Version 2, as published by Sam Hocevar. See +// http://sam.zoy.org/projects/COPYING.WTFPL for more details. +// + +// +// Debug utilities +// --------------- +// + +#if !defined __LOL_DEBUG_H__ +#define __LOL_DEBUG_H__ + +#include "entity.h" + +namespace lol +{ + +static void Abort() +{ + *(int *)NULL = 0; +} + +} /* namespace lol */ + +#endif // __LOL_DEBUG_H__ + diff --git a/src/gpu/vertexbuffer.cpp b/src/gpu/vertexbuffer.cpp index f3767477..d1df4c39 100644 --- a/src/gpu/vertexbuffer.cpp +++ b/src/gpu/vertexbuffer.cpp @@ -97,7 +97,8 @@ VertexDeclaration::~VertexDeclaration() D3DVertexDeclaration *vdecl = (D3DVertexDeclaration *)m_data; # endif - vdecl->Release(); + if (FAILED(vdecl->Release())) + Abort(); #else #endif @@ -112,7 +113,8 @@ void VertexDeclaration::Bind() D3DVertexDeclaration *vdecl = (D3DVertexDeclaration *)m_data; # endif - g_d3ddevice->SetVertexDeclaration(vdecl); + if (FAILED(g_d3ddevice->SetVertexDeclaration(vdecl))) + Abort(); #else /* FIXME: Nothing to do? */ #endif @@ -121,11 +123,13 @@ void VertexDeclaration::Bind() void VertexDeclaration::DrawElements(MeshPrimitive type, int skip, int count) { #if defined _XBOX || defined USE_D3D9 - g_d3ddevice->SetRenderState(D3DRS_CULLMODE, D3DCULL_CW); + if (FAILED(g_d3ddevice->SetRenderState(D3DRS_CULLMODE, D3DCULL_CW))) + Abort(); switch (type) { case MeshPrimitive::Triangles: - g_d3ddevice->DrawPrimitive(D3DPT_TRIANGLELIST, skip, count); + if (FAILED(g_d3ddevice->DrawPrimitive(D3DPT_TRIANGLELIST, skip, count))) + Abort(); break; } #else @@ -193,7 +197,10 @@ void VertexDeclaration::SetStream(VertexBuffer *vb, ShaderAttrib attr1, /* Now we know the stream index and the element stride */ /* FIXME: precompute most of the crap above! */ if (stream >= 0) - g_d3ddevice->SetStreamSource(stream, vb->m_data->m_vbo, 0, stride); + { + if (FAILED(g_d3ddevice->SetStreamSource(stream, vb->m_data->m_vbo, 0, stride))) + Abort(); + } #else glBindBuffer(GL_ARRAY_BUFFER, vb->m_data->m_vbo); ShaderAttrib l[12] = { attr1, attr2, attr3, attr4, attr5, attr6, @@ -334,7 +341,8 @@ void VertexDeclaration::Initialize() D3DVertexDeclaration *vdecl; # endif - g_d3ddevice->CreateVertexDeclaration(elements, &vdecl); + if (FAILED(g_d3ddevice->CreateVertexDeclaration(elements, &vdecl))) + Abort(); m_data = vdecl; #else @@ -365,8 +373,9 @@ VertexBuffer::VertexBuffer(size_t size) : m_data(new VertexBufferData) { #if defined USE_D3D9 || defined _XBOX - g_d3ddevice->CreateVertexBuffer(size, D3DUSAGE_WRITEONLY, NULL, - D3DPOOL_MANAGED, &m_data->m_vbo, NULL); + if (FAILED(g_d3ddevice->CreateVertexBuffer(size, D3DUSAGE_WRITEONLY, NULL, + D3DPOOL_MANAGED, &m_data->m_vbo, NULL))) + Abort(); new uint8_t[size]; #elif !defined __CELLOS_LV2__ && !defined __ANDROID__ glGenBuffers(1, &m_data->m_vbo); @@ -378,7 +387,8 @@ VertexBuffer::VertexBuffer(size_t size) VertexBuffer::~VertexBuffer() { #if defined USE_D3D9 || defined _XBOX - m_data->m_vbo->Release(); + if (FAILED(m_data->m_vbo->Release())) + Abort(); #elif !defined __CELLOS_LV2__ && !defined __ANDROID__ glDeleteBuffers(1, &m_data->m_vbo); delete[] m_data->m_memory; @@ -390,7 +400,7 @@ void *VertexBuffer::Lock(size_t offset, size_t size) #if defined USE_D3D9 || defined _XBOX void *ret; if (FAILED(m_data->m_vbo->Lock(offset, size, (void **)&ret, 0))) - exit(0); + Abort(); return ret; #elif !defined __CELLOS_LV2__ && !defined __ANDROID__ return m_data->m_memory + offset; @@ -400,7 +410,8 @@ void *VertexBuffer::Lock(size_t offset, size_t size) void VertexBuffer::Unlock() { #if defined USE_D3D9 || defined _XBOX - m_data->m_vbo->Unlock(); + if (FAILED(m_data->m_vbo->Unlock())) + Abort(); #elif !defined __CELLOS_LV2__ && !defined __ANDROID__ glBindBuffer(GL_ARRAY_BUFFER, m_data->m_vbo); glBufferData(GL_ARRAY_BUFFER, m_data->m_size, m_data->m_memory, diff --git a/src/platform/sdl/sdlapp.cpp b/src/platform/sdl/sdlapp.cpp index 67c9a8af..31071b00 100644 --- a/src/platform/sdl/sdlapp.cpp +++ b/src/platform/sdl/sdlapp.cpp @@ -102,14 +102,17 @@ void SdlApp::Run() while (!Ticker::Finished()) { #if defined USE_SDL && defined USE_D3D9 - g_d3ddevice->BeginScene(); + if (FAILED(g_d3ddevice->BeginScene())) + Abort(); #endif /* Tick the renderer, show the frame and clamp to desired framerate. */ Ticker::TickDraw(); #if defined USE_SDL # if defined USE_D3D9 - g_d3ddevice->EndScene(); - g_d3ddevice->Present(NULL, NULL, NULL, NULL); + if (FAILED(g_d3ddevice->EndScene())) + Abort(); + if (FAILED(g_d3ddevice->Present(NULL, NULL, NULL, NULL))) + Abort(); # else SDL_GL_SwapBuffers(); # endif diff --git a/src/scene.cpp b/src/scene.cpp index bb78dcd8..33f953a7 100644 --- a/src/scene.cpp +++ b/src/scene.cpp @@ -18,22 +18,11 @@ #ifdef WIN32 # define WIN32_LEAN_AND_MEAN # include -# if defined USE_D3D9 -# define FAR -# define NEAR -# include -# endif #endif #include "core.h" #include "lolgl.h" -#if defined USE_D3D9 -extern IDirect3DDevice9 *g_d3ddevice; -#elif defined _XBOX -extern D3DDevice *g_d3ddevice; -#endif - namespace lol { @@ -106,6 +95,7 @@ Scene::~Scene() for (int i = 0; i < data->nbufs; i++) delete data->bufs[i]; free(data->bufs); + delete data->m_vdecl; delete data; } diff --git a/src/video.cpp b/src/video.cpp index 90038591..9850d0cd 100644 --- a/src/video.cpp +++ b/src/video.cpp @@ -228,9 +228,10 @@ void Video::Clear() { ivec2 size = GetSize(); #if defined USE_D3D9 || defined _XBOX - VideoData::d3d_dev->Clear(0, NULL, D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER + if (FAILED(VideoData::d3d_dev->Clear(0, NULL, D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER | D3DCLEAR_STENCIL, - VideoData::clear_color, 1.0f, 0); + VideoData::clear_color, 1.0f, 0))) + Abort(); #else glViewport(0, 0, size.x, size.y); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT); diff --git a/test/tutorial/tut01.cpp b/test/tutorial/tut01.cpp index 59717f42..15bffe21 100644 --- a/test/tutorial/tut01.cpp +++ b/test/tutorial/tut01.cpp @@ -86,8 +86,8 @@ public: } m_shader->Bind(); - m_vdecl->Bind(); m_vdecl->SetStream(m_vbo, m_coord); + m_vdecl->Bind(); m_vdecl->DrawElements(MeshPrimitive::Triangles, 0, 1); m_vdecl->Unbind(); } diff --git a/test/tutorial/tut02.cpp b/test/tutorial/tut02.cpp index b21a4794..f33679b2 100644 --- a/test/tutorial/tut02.cpp +++ b/test/tutorial/tut02.cpp @@ -167,9 +167,9 @@ public: #elif defined _XBOX || defined USE_D3D9 int16_t *indices; if (FAILED(g_d3ddevice->CreateIndexBuffer(sizeof(m_indices), D3DUSAGE_WRITEONLY, D3DFMT_INDEX16, D3DPOOL_MANAGED, &m_ibo, NULL))) - exit(0); + Abort(); if (FAILED(m_ibo->Lock(0, 0, (void **)&indices, 0))) - exit(0); + Abort(); memcpy(indices, m_indices, sizeof(m_indices)); m_ibo->Unlock(); #else @@ -180,16 +180,21 @@ public: m_ready = true; } + Video::SetClearColor(vec4(0.0f, 0.0f, 0.0f, 1.0f)); + m_shader->Bind(); m_shader->SetUniform(m_mvp, m_matrix); - m_vdecl->Bind(); m_vdecl->SetStream(m_vbo, m_coord); m_vdecl->SetStream(m_cbo, m_color); + m_vdecl->Bind(); #if defined _XBOX || defined USE_D3D9 - g_d3ddevice->SetRenderState(D3DRS_CULLMODE, D3DCULL_CW); - g_d3ddevice->SetIndices(m_ibo); - g_d3ddevice->DrawIndexedPrimitive(D3DPT_TRIANGLELIST, 0, 0, 0, 0, sizeof(m_indices) / sizeof(*m_indices)); + if (FAILED(g_d3ddevice->SetRenderState(D3DRS_CULLMODE, D3DCULL_CW))) + Abort(); + if (FAILED(g_d3ddevice->SetIndices(m_ibo))) + Abort(); + if (FAILED(g_d3ddevice->DrawIndexedPrimitive(D3DPT_TRIANGLELIST, 0, 0, 0, 0, sizeof(m_indices) / sizeof(*m_indices)))) + Abort(); #elif !defined __CELLOS_LV2__ && !defined __ANDROID__ && !defined __APPLE__ glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, m_ibo); int size; diff --git a/win32/lolcore.vcxproj b/win32/lolcore.vcxproj index e4529a3a..9523acb0 100644 --- a/win32/lolcore.vcxproj +++ b/win32/lolcore.vcxproj @@ -133,6 +133,7 @@ +