From ad8144f0e813d80a9fa83b0d2b9452a1a6469836 Mon Sep 17 00:00:00 2001 From: Sam Hocevar Date: Sun, 23 Jun 2013 13:38:56 +0000 Subject: [PATCH] scene: use a global g_scene object instead of Scene::GetDefault(). --- src/debug/lines.cpp | 8 ++++---- src/easymesh/easymesh.cpp | 6 +++--- src/emitter.cpp | 4 ++-- src/font.cpp | 4 +--- src/gpu/rendercontext.cpp | 3 --- src/gpu/renderer.cpp | 4 +++- src/gradient.cpp | 4 ++-- src/layer.cpp | 11 +++++------ src/light.cpp | 2 +- src/scene.cpp | 26 ++++++++++++-------------- src/scene.h | 11 +++++++---- src/sprite.cpp | 3 +-- src/ticker.cpp | 4 ++-- src/video.cpp | 4 ++++ test/btphystest.cpp | 4 ++-- test/meshviewer.cpp | 26 +++++++++++++------------- tutorial/05_easymesh.cpp | 4 ++-- tutorial/06_sprite.cpp | 8 ++++---- 18 files changed, 68 insertions(+), 68 deletions(-) diff --git a/src/debug/lines.cpp b/src/debug/lines.cpp index e8e4c93f..982a294f 100644 --- a/src/debug/lines.cpp +++ b/src/debug/lines.cpp @@ -19,7 +19,7 @@ namespace lol void Debug::DrawLine(vec3 a, vec3 b, vec4 color) { - Scene::GetDefault()->AddLine(a, b, color); + g_scene->AddLine(a, b, color); } void Debug::DrawBox(vec3 a, vec3 b, vec4 color) @@ -36,9 +36,9 @@ void Debug::DrawBox(vec3 a, vec3 b, vec4 color) { int j = ((i & 1) << 1) | ((i >> 1) ^ 1); - Scene::GetDefault()->AddLine(v[i], v[i + 4], color); - Scene::GetDefault()->AddLine(v[i], v[j], color); - Scene::GetDefault()->AddLine(v[i + 4], v[j + 4], color); + g_scene->AddLine(v[i], v[i + 4], color); + g_scene->AddLine(v[i], v[j], color); + g_scene->AddLine(v[i + 4], v[j + 4], color); } } diff --git a/src/easymesh/easymesh.cpp b/src/easymesh/easymesh.cpp index ea0b2929..61223513 100644 --- a/src/easymesh/easymesh.cpp +++ b/src/easymesh/easymesh.cpp @@ -133,15 +133,15 @@ void DefaultShaderData::SetupDefaultData(bool with_UV) //----------------------------------------------------------------------------- void DefaultShaderData::SetupShaderDatas(mat4 const &model) { - mat4 proj = Scene::GetDefault()->GetCamera()->GetProjection(); - mat4 view = Scene::GetDefault()->GetCamera()->GetView(); + mat4 proj = g_scene->GetCamera()->GetProjection(); + mat4 view = g_scene->GetCamera()->GetView(); mat4 modelview = view * model; mat3 normalmat = transpose(inverse(mat3(modelview))); /* FIXME: this should be hidden in the shader */ /* FIXME: the 4th component of the position can be used for other things */ /* FIXME: GetUniform("blabla") is costly */ - Array const lights = Scene::GetDefault()->GetLights(); + Array const lights = g_scene->GetLights(); Array light_data; for (int i = 0; i < lights.Count(); ++i) light_data << lights[i]->GetPosition() << lights[i]->GetColor(); diff --git a/src/emitter.cpp b/src/emitter.cpp index 18007484..55e85e38 100644 --- a/src/emitter.cpp +++ b/src/emitter.cpp @@ -73,8 +73,8 @@ void Emitter::TickDraw(float seconds) Entity::TickDraw(seconds); for (int i = 0; i < data->nparticles; i++) - Scene::GetDefault()->AddTile(data->tileset, data->particles[i], - data->positions[i], 0, vec2(1.0f)); + g_scene->AddTile(data->tileset, data->particles[i], + data->positions[i], 0, vec2(1.0f)); } void Emitter::AddParticle(int id, vec3 pos, vec3 vel) diff --git a/src/font.cpp b/src/font.cpp index ac0a20d6..e5178b2e 100644 --- a/src/font.cpp +++ b/src/font.cpp @@ -69,14 +69,12 @@ char const *Font::GetName() void Font::Print(vec3 pos, char const *str, vec2 scale) { - Scene *scene = Scene::GetDefault(); - while (*str) { uint32_t ch = (uint8_t)*str++; if (ch != ' ') - scene->AddTile(data->tileset, ch & 255, pos, 0, scale); + g_scene->AddTile(data->tileset, ch & 255, pos, 0, scale); pos.x += data->size.x * scale.x; } diff --git a/src/gpu/rendercontext.cpp b/src/gpu/rendercontext.cpp index d6c8d61e..f92f8b5d 100644 --- a/src/gpu/rendercontext.cpp +++ b/src/gpu/rendercontext.cpp @@ -52,8 +52,6 @@ class RenderContextData friend class RenderContext; private: - Scene *m_scene; - TrackedState m_viewport; TrackedState m_clear_color; TrackedState m_clear_depth; @@ -71,7 +69,6 @@ private: RenderContext::RenderContext() : m_data(new RenderContextData()) { - m_data->m_scene = Scene::GetDefault(); } RenderContext::~RenderContext() diff --git a/src/gpu/renderer.cpp b/src/gpu/renderer.cpp index 2fc3480f..30343d50 100644 --- a/src/gpu/renderer.cpp +++ b/src/gpu/renderer.cpp @@ -44,11 +44,13 @@ namespace lol /* * The global g_renderer object, initialised by Video::Init */ -Renderer *g_renderer; + +Renderer *g_renderer = nullptr; /* * Private RendererData class */ + class RendererData { friend class Renderer; diff --git a/src/gradient.cpp b/src/gradient.cpp index f85cc937..f1d5cab4 100644 --- a/src/gradient.cpp +++ b/src/gradient.cpp @@ -94,9 +94,9 @@ void Gradient::TickDraw(float seconds) data->shader->Bind(); uni_mat = data->shader->GetUniformLocation("proj_matrix"); - data->shader->SetUniform(uni_mat, Scene::GetDefault()->GetCamera()->GetProjection()); + data->shader->SetUniform(uni_mat, g_scene->GetCamera()->GetProjection()); uni_mat = data->shader->GetUniformLocation("view_matrix"); - data->shader->SetUniform(uni_mat, Scene::GetDefault()->GetCamera()->GetView()); + data->shader->SetUniform(uni_mat, g_scene->GetCamera()->GetView()); uni_mat = data->shader->GetUniformLocation("model_matrix"); data->shader->SetUniform(uni_mat, model_matrix); diff --git a/src/layer.cpp b/src/layer.cpp index 9f479a55..7e1624ad 100644 --- a/src/layer.cpp +++ b/src/layer.cpp @@ -51,15 +51,14 @@ void Layer::Render(int /* x */, int /* y */, int /* z */) if (error && !(error = 0)) Log::Error("FIXME: Layer::Render no longer works\n"); #if 0 - Scene *scene = Scene::GetDefault(); for (int j = 0; j < height; j++) for (int i = 0; i < width; i++) if (data[j * width + i]) - scene->AddTile(data[j * width + i], - vec3(x + i * 32, - y + j * 32 - altitude, - altitude + z), - orientation); + g_scene->AddTile(data[j * width + i], + vec3(x + i * 32, + y + j * 32 - altitude, + altitude + z), + orientation); #endif } diff --git a/src/light.cpp b/src/light.cpp index efd31473..fccf94f1 100644 --- a/src/light.cpp +++ b/src/light.cpp @@ -64,7 +64,7 @@ void Light::TickDraw(float seconds) { WorldEntity::TickDraw(seconds); - Scene::GetDefault()->AddLight(this); + g_scene->AddLight(this); } } /* namespace lol */ diff --git a/src/scene.cpp b/src/scene.cpp index 671bc537..920eb9ba 100644 --- a/src/scene.cpp +++ b/src/scene.cpp @@ -28,6 +28,16 @@ LOLFX_RESOURCE_DECLARE(line); namespace lol { +/* + * The global g_scene object, initialised by Video::Init + */ + +Scene *g_scene = nullptr; + +/* + * A quick and dirty Tile structure for 2D blits + */ + struct Tile { TileSet *tileset; @@ -71,23 +81,18 @@ private: Camera *m_default_cam; Array m_camera_stack; - - static Scene *scene; }; -Scene *SceneData::scene = nullptr; - /* * Public Scene class */ -Scene::Scene() +Scene::Scene(ivec2 size) : data(new SceneData()) { /* Create a default orthographic camera, in case the user doesn’t. */ data->m_default_cam = new Camera(); - mat4 proj = mat4::ortho(0, Video::GetSize().x, 0, Video::GetSize().y, - -1000.f, 1000.f); + mat4 proj = mat4::ortho(0, size.x, 0, size.y, -1000.f, 1000.f); data->m_default_cam->SetProjection(proj); PushCamera(data->m_default_cam); @@ -114,13 +119,6 @@ Scene::~Scene() delete data; } -Scene *Scene::GetDefault() -{ - if (!SceneData::scene) - SceneData::scene = new Scene(); - return SceneData::scene; -} - void Scene::PushCamera(Camera *cam) { Ticker::Ref(cam); diff --git a/src/scene.h b/src/scene.h index 440621e1..38537742 100644 --- a/src/scene.h +++ b/src/scene.h @@ -29,12 +29,13 @@ class SceneData; class Scene { -public: - Scene(); - ~Scene(); + friend class Video; - static Scene *GetDefault(); +private: + Scene(ivec2 size); + ~Scene(); +public: Camera *GetCamera(); void PushCamera(Camera *cam); void PopCamera(Camera *cam); @@ -54,6 +55,8 @@ private: SceneData *data; }; +extern Scene *g_scene; + } /* namespace lol */ #endif // __LOL_SCENE_H__ diff --git a/src/sprite.cpp b/src/sprite.cpp index 61bd0947..5bf7c553 100644 --- a/src/sprite.cpp +++ b/src/sprite.cpp @@ -50,8 +50,7 @@ void Sprite::TickDraw(float seconds) { Entity::TickDraw(seconds); - Scene::GetDefault()->AddTile(data->tileset, data->id, m_position, - 0, vec2(1.0f)); + g_scene->AddTile(data->tileset, data->id, m_position, 0, vec2(1.0f)); } Sprite::~Sprite() diff --git a/src/ticker.cpp b/src/ticker.cpp index 8cb23d9b..0649d056 100644 --- a/src/ticker.cpp +++ b/src/ticker.cpp @@ -386,7 +386,7 @@ void TickerData::DrawThreadTick() switch (i) { case Entity::DRAWGROUP_BEGIN: - Scene::GetDefault()->Reset(); + g_scene->Reset(); g_renderer->Clear(ClearMask::All); break; default: @@ -412,7 +412,7 @@ void TickerData::DrawThreadTick() } /* Do this render step */ - Scene::GetDefault()->Render(); + g_scene->Render(); } Profiler::Stop(Profiler::STAT_TICK_DRAW); diff --git a/src/video.cpp b/src/video.cpp index a9e12983..7591cb58 100644 --- a/src/video.cpp +++ b/src/video.cpp @@ -51,6 +51,7 @@ DebugRenderMode VideoData::render_mode = DebugRenderMode::Default; void Video::Setup(ivec2 size) { g_renderer = new Renderer(size); + g_scene = new Scene(size); /* Initialise reasonable scene default properties */ SetDebugRenderMode(DebugRenderMode::Default); @@ -114,6 +115,9 @@ DebugRenderMode Video::GetDebugRenderMode() void Video::Destroy() { + delete g_scene; + g_scene = nullptr; + delete g_renderer; g_renderer = nullptr; } diff --git a/test/btphystest.cpp b/test/btphystest.cpp index 5d8ef0b3..a345476c 100644 --- a/test/btphystest.cpp +++ b/test/btphystest.cpp @@ -56,7 +56,7 @@ BtPhysTest::BtPhysTest(bool editor) vec3(0.f, 0.f, 0.f), vec3(0, 1, 0)); m_camera->SetProjection(mat4::perspective(45.f, 1280.f, 960.f, .1f, 1000.f)); - Scene::GetDefault()->PushCamera(m_camera); + g_scene->PushCamera(m_camera); m_ready = false; @@ -421,7 +421,7 @@ void BtPhysTest::TickDraw(float seconds) BtPhysTest::~BtPhysTest() { - Scene::GetDefault()->PopCamera(m_camera); + g_scene->PopCamera(m_camera); Ticker::Unref(m_light1); Ticker::Unref(m_light2); diff --git a/test/meshviewer.cpp b/test/meshviewer.cpp index bc9b65a4..a70539d4 100644 --- a/test/meshviewer.cpp +++ b/test/meshviewer.cpp @@ -61,9 +61,9 @@ public: void SetFov(float new_fov=60.0f, vec2 video_size = vec2(Video::GetSize())) { if (new_fov > MIN_FOV) - Scene::GetDefault()->GetCamera()->SetProjection(mat4::perspective(new_fov, video_size.x, video_size.y, .1f, 1000.f)); + g_scene->GetCamera()->SetProjection(mat4::perspective(new_fov, video_size.x, video_size.y, .1f, 1000.f)); else - Scene::GetDefault()->GetCamera()->SetProjection(mat4::ortho(video_size.x, video_size.y, .1f, 1000.f)); + g_scene->GetCamera()->SetProjection(mat4::ortho(video_size.x, video_size.y, .1f, 1000.f)); } MeshViewer(char const *file_name = "data/mesh-buffer.txt") @@ -107,7 +107,7 @@ public: m_camera->SetView(vec3(0.f, 0.f, 10.f), vec3(0.f, 0.f, 0.f), vec3(0.f, 1.f, 0.f)); - Scene::GetDefault()->PushCamera(m_camera); + g_scene->PushCamera(m_camera); //Lights setup m_lights << new Light(); @@ -144,7 +144,7 @@ public: ~MeshViewer() { - Scene::GetDefault()->PopCamera(m_camera); + g_scene->PopCamera(m_camera); for (int i = 0; i < m_lights.Count(); ++i) Ticker::Unref(m_lights[i]); } @@ -178,8 +178,8 @@ public: if (m_meshes[mesh_id].m2) break; - mat4 world_cam = Scene::GetDefault()->GetCamera()->GetView(); - mat4 cam_screen = Scene::GetDefault()->GetCamera()->GetProjection(); + mat4 world_cam = g_scene->GetCamera()->GetView(); + mat4 cam_screen = g_scene->GetCamera()->GetProjection(); if (m_meshes.Count() && mesh_id >= 0) { @@ -250,13 +250,13 @@ public: SetFov(m_fov_damp); //Move modification - vec3 campos = Scene::GetDefault()->GetCamera()->GetPosition(); + vec3 campos = g_scene->GetCamera()->GetPosition(); if (m_fov_damp < MIN_FOV) - Scene::GetDefault()->GetCamera()->SetView(vec3(campos.xy, 10.f), quat(1.f)); + g_scene->GetCamera()->SetView(vec3(campos.xy, 10.f), quat(1.f)); else if (fov_ratio > .0f) - Scene::GetDefault()->GetCamera()->SetView(vec3(campos.xy, campos.z * fov_ratio * 1.1f), quat(1.f)); + g_scene->GetCamera()->SetView(vec3(campos.xy, campos.z * fov_ratio * 1.1f), quat(1.f)); #else - Camera* cur_cam = Scene::GetDefault()->GetCamera(); + Camera* cur_cam = g_scene->GetCamera(); vec3 min_max_diff = (cam_min_max[1] - cam_min_max[0]); float screen_size = max(max(lol::abs(min_max_diff.x), lol::abs(min_max_diff.y)), max( lol::abs(min_max_diff.x), lol::abs(min_max_diff.y))); @@ -442,7 +442,7 @@ public: g_renderer->SetClearColor(vec4(0.0f, 0.0f, 0.0f, 1.0f)); - mat4 default_proj = Scene::GetDefault()->GetCamera()->GetProjection(); + mat4 default_proj = g_scene->GetCamera()->GetProjection(); int max_drawn = m_meshes.Count() - m_mesh_shown; for (int i = max_drawn; i < m_meshes.Count(); i++) m_meshes[i].m4 = vec3(.0f); @@ -461,7 +461,7 @@ public: } m_meshes[i].m4 = damp(m_meshes[i].m4, new_mesh_offset, .35f, seconds); - Scene::GetDefault()->GetCamera()->SetProjection( + g_scene->GetCamera()->SetProjection( mat4::translate(m_meshes[i].m4) * mat4::translate(vec3(m_mesh_screen_offset_damp, .0f)) * mat4::scale(vec3(vec2(m_meshes[i].m3), 1.0f)) * @@ -475,7 +475,7 @@ public: g_renderer->Clear(ClearMask::Depth); } } - Scene::GetDefault()->GetCamera()->SetProjection(default_proj); + g_scene->GetCamera()->SetProjection(default_proj); } private: diff --git a/tutorial/05_easymesh.cpp b/tutorial/05_easymesh.cpp index b1672833..8cf71187 100644 --- a/tutorial/05_easymesh.cpp +++ b/tutorial/05_easymesh.cpp @@ -51,7 +51,7 @@ public: m_camera->SetView(mat4::lookat(vec3(-15.f, 5.f, 0.f), vec3(0.f, -1.f, 0.f), vec3(0.f, 1.f, 0.f))); - Scene::GetDefault()->PushCamera(m_camera); + g_scene->PushCamera(m_camera); /* Add a white directional light */ m_light1 = new Light(); @@ -70,7 +70,7 @@ public: ~EasyMeshTutorial() { - Scene::GetDefault()->PopCamera(m_camera); + g_scene->PopCamera(m_camera); Ticker::Unref(m_light1); Ticker::Unref(m_light2); } diff --git a/tutorial/06_sprite.cpp b/tutorial/06_sprite.cpp index 0e47bf48..fa56caba 100644 --- a/tutorial/06_sprite.cpp +++ b/tutorial/06_sprite.cpp @@ -26,7 +26,7 @@ public: m_camera = new Camera(); m_camera->SetView(mat4(1.f)); m_camera->SetProjection(mat4::ortho(0.f, 640.f, 0.f, 480.f, -100.f, 100.f)); - Scene::GetDefault()->PushCamera(m_camera); + g_scene->PushCamera(m_camera); Ticker::Ref(m_camera); m_tileset = Tiler::Register("06_sprite.png"); @@ -46,7 +46,7 @@ public: { Tiler::Deregister(m_tileset); - Scene::GetDefault()->PopCamera(m_camera); + g_scene->PopCamera(m_camera); Ticker::Unref(m_camera); } @@ -77,8 +77,8 @@ public: { int frame = (int)(m_sprites[i].m2 * FRAME_COUNT); // m_sprites[i].m1.z = frame; - Scene::GetDefault()->AddTile(m_tileset, frame, - (ivec3)m_sprites[i].m1, 0, vec2(2.f)); + g_scene->AddTile(m_tileset, frame, + (ivec3)m_sprites[i].m1, 0, vec2(2.f)); } }