From 5ea13fc857d12b1ec89f78135846b2e43e1777d0 Mon Sep 17 00:00:00 2001 From: Sam Hocevar Date: Sat, 30 May 2015 12:23:41 +0000 Subject: [PATCH] scene: clean up a few things here and there. --- src/scene.cpp | 94 ++++++++++++++++++++++++++++++++------------------- src/scene.h | 12 +++---- 2 files changed, 66 insertions(+), 40 deletions(-) diff --git a/src/scene.cpp b/src/scene.cpp index 674cab0e..24b07547 100644 --- a/src/scene.cpp +++ b/src/scene.cpp @@ -1,11 +1,14 @@ // -// Lol Engine +// Lol Engine // -// Copyright: (c) 2010-2014 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://www.wtfpl.net/ for more details. +// Copyright © 2010—2015 Sam Hocevar +// © 2014—2015 Benjamin “Touky” Huet +// +// This library is free software. It comes without any warranty, to +// the extent permitted by applicable law. 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 the WTFPL Task Force. +// See http://www.wtfpl.net/ for more details. // #include @@ -50,8 +53,9 @@ struct Tile static array m_scene_displays; /* -* Public SceneDisplay class -*/ + * Public SceneDisplay class + */ + void SceneDisplay::Add(SceneDisplay* display) { m_scene_displays << display; @@ -61,11 +65,14 @@ int SceneDisplay::GetCount() { return m_scene_displays.count(); } + SceneDisplay* SceneDisplay::GetDisplay(int index) { - ASSERT(0 <= index && index < m_scene_displays.count()); + ASSERT(0 <= index && index < m_scene_displays.count(), + "invalid display index %d", index); return m_scene_displays[index]; } + void SceneDisplay::DestroyAll() { for (SceneDisplay* display : m_scene_displays) @@ -76,28 +83,30 @@ void SceneDisplay::DestroyAll() /* ------------------------------------------------ */ void SceneDisplay::Enable() { - //TODO: PROFILER STUFF + // TODO: PROFILER STUFF } + void SceneDisplay::Disable() { - //TODO: PROFILER STUFF + // TODO: PROFILER STUFF } -//----------------------------------------------------------------------------- /* -* Primitive implementation class -*/ + * Primitive implementation class + */ + void PrimitiveSource::Render(Scene& scene) { UNUSED(scene); } + void PrimitiveRenderer::Render(Scene& scene, PrimitiveSource* primitive) { UNUSED(scene); UNUSED(primitive); } -//----------------------------------------------------------------------------- /* * Scene implementation class */ + class SceneData { friend class Scene; @@ -121,13 +130,14 @@ private: SceneDisplay* m_display = nullptr; /* Sources are shared by all scenes. - * Renderers are scene-dependent. They get the primitive in the identical slot to render with the given scene + * Renderers are scene-dependent. They get the primitive in the identical + * slot to render with the given scene. * Primitives and renderers will be kept until: * - Updated by entity * - Marked Fire&Forget * - Scene is destroyed */ - map > m_prim_renderers; - static map > m_prim_sources; + map> m_prim_renderers; + static map> m_prim_sources; static mutex m_prim_mutex; /* Old API */ @@ -154,7 +164,7 @@ private: array m_camera_stack; }; uint64_t SceneData::m_used_id = 1; -map > SceneData::m_prim_sources; +map> SceneData::m_prim_sources; mutex SceneData::m_prim_mutex; /* @@ -234,7 +244,8 @@ bool Scene::IsReady(int index) //----------------------------------------------------------------------------- Scene& Scene::GetScene(int index) { - ASSERT(0 <= index && index < g_scenes.count() && !!g_scenes[index], "Trying to get a non-existent scene"); + ASSERT(0 <= index && index < g_scenes.count() && !!g_scenes[index], + "Trying to get a non-existent scene %d", index); return *g_scenes[index]; } @@ -330,6 +341,7 @@ int Scene::HasPrimitiveSource(uintptr_t key) SceneData::m_prim_mutex.unlock(); return count; } + int Scene::AddPrimitiveSource(uintptr_t key, PrimitiveSource* source) { int count; @@ -341,10 +353,12 @@ int Scene::AddPrimitiveSource(uintptr_t key, PrimitiveSource* source) SceneData::m_prim_mutex.unlock(); return count; } + void Scene::SetPrimitiveSource(int index, uintptr_t key, PrimitiveSource* source) { ASSERT(source); - ASSERT(index < 0); + ASSERT(index >= 0); + PrimitiveSource* old = nullptr; SceneData::m_prim_mutex.lock(); { @@ -356,9 +370,10 @@ void Scene::SetPrimitiveSource(int index, uintptr_t key, PrimitiveSource* source } SceneData::m_prim_mutex.unlock(); - //Delete old AFTER having released the lock - if (old) delete old; + // Delete old AFTER having released the lock + delete old; } + void Scene::ReleasePrimitiveSource(int index, uintptr_t key) { PrimitiveSource* old = nullptr; @@ -370,10 +385,11 @@ void Scene::ReleasePrimitiveSource(int index, uintptr_t key) } SceneData::m_prim_mutex.unlock(); - //Delete old AFTER having released the lock - if (old) delete old; + // Delete old AFTER having released the lock + delete old; } -void Scene::ReleaseAllPrimitiveSource(uintptr_t key) + +void Scene::ReleaseAllPrimitiveSources(uintptr_t key) { array oldies; SceneData::m_prim_mutex.lock(); @@ -385,9 +401,9 @@ void Scene::ReleaseAllPrimitiveSource(uintptr_t key) } SceneData::m_prim_mutex.unlock(); - //Delete oldies AFTER having released the lock + // Delete oldies AFTER having released the lock for (PrimitiveSource* old : oldies) - if (old) delete old; + delete old; } //---- Primitive renderer stuff ----------------------------------------------- @@ -395,15 +411,18 @@ int Scene::HasPrimitiveRenderer(uintptr_t key) { return data->m_prim_renderers[key].count(); } + void Scene::AddPrimitiveRenderer(uintptr_t key, PrimitiveRenderer* renderer) { renderer->m_fire_and_forget = true; data->m_prim_renderers[key].push(renderer); } + void Scene::SetPrimitiveRenderer(int index, uintptr_t key, PrimitiveRenderer* renderer) { ASSERT(renderer); - ASSERT(index < 0); + ASSERT(index >= 0); + if (index < data->m_prim_renderers[key].count()) { ASSERT(data->m_prim_renderers[key][index]); @@ -413,19 +432,21 @@ void Scene::SetPrimitiveRenderer(int index, uintptr_t key, PrimitiveRenderer* re data->m_prim_renderers[key].resize(index + 1); data->m_prim_renderers[key][index] = renderer; } + void Scene::ReleasePrimitiveRenderer(int index, uintptr_t key) { ASSERT(0 <= index && index < data->m_prim_renderers[key].count()); - ASSERT(data->m_prim_renderers[key][index]); + delete data->m_prim_renderers[key][index]; data->m_prim_renderers[key].remove(index); } -void Scene::ReleaseAllPrimitiveRenderer(uintptr_t key) + +void Scene::ReleaseAllPrimitiveRenderers(uintptr_t key) { - for (PrimitiveRenderer* renderer : data->m_prim_renderers[key]) + for (PrimitiveRenderer*& renderer : data->m_prim_renderers[key]) { - ASSERT(renderer); delete renderer; + renderer = nullptr; } } @@ -457,18 +478,21 @@ void Scene::SetLineTime(float new_time) data->m_new_line_time = new_time; } + void Scene::SetLineMask(int new_mask) { ASSERT(!!data, "Trying to access a non-ready scene"); data->m_new_line_mask = new_mask; } + void Scene::SetLineSegmentSize(float new_segment_size) { ASSERT(!!data, "Trying to access a non-ready scene"); data->m_new_line_segment_size = new_segment_size; } + void Scene::SetLineColor(vec4 new_color) { ASSERT(!!data, "Trying to access a non-ready scene"); @@ -483,6 +507,7 @@ float Scene::GetLineSegmentSize() return data->m_new_line_segment_size; } + vec4 Scene::GetLineColor() { ASSERT(!!data, "Trying to access a non-ready scene"); @@ -524,11 +549,12 @@ void Scene::SetDisplay(SceneDisplay* display) //----------------------------------------------------------------------------- void Scene::EnableDisplay() { - //If no display has been set, use the default one + // If no display has been set, use the default one if (!data->m_display) SetDisplay(SceneDisplay::GetDisplay()); data->m_display->Enable(); } + void Scene::DisableDisplay() { ASSERT(data->m_display); diff --git a/src/scene.h b/src/scene.h index 7ed16d4d..943967bc 100644 --- a/src/scene.h +++ b/src/scene.h @@ -131,7 +131,7 @@ private: int AddPrimitiveSource(uintptr_t key, class PrimitiveSource* source); void SetPrimitiveSource(int index, uintptr_t key, class PrimitiveSource* source); void ReleasePrimitiveSource(int index, uintptr_t key); - void ReleaseAllPrimitiveSource(uintptr_t key); + void ReleaseAllPrimitiveSources(uintptr_t key); public: /* === Primitive source stuff === */ /* Returns the number of primitive source set to the given entity */ @@ -167,10 +167,10 @@ public: } /* Remove all primitive source set to the given entity */ template - void ReleaseAllPrimitiveSource(T* key) + void ReleaseAllPrimitiveSources(T* key) { ASSERT(key); - ReleaseAllPrimitiveSource(_KEY_IDX); + ReleaseAllPrimitiveSources(_KEY_IDX); } private: @@ -178,7 +178,7 @@ private: void AddPrimitiveRenderer(uintptr_t key, class PrimitiveRenderer* renderer); void SetPrimitiveRenderer(int index, uintptr_t key, class PrimitiveRenderer* renderer); void ReleasePrimitiveRenderer(int index, uintptr_t key); - void ReleaseAllPrimitiveRenderer(uintptr_t key); + void ReleaseAllPrimitiveRenderers(uintptr_t key); public: /* === Primitive renderer stuff === */ /* Returns the number of primitive renderer set to the given entity */ @@ -215,10 +215,10 @@ public: } /* Remove all primitive renderer set to the given entity */ template - void ReleaseAllPrimitiveRenderer(T* key) + void ReleaseAllPrimitiveRenderers(T* key) { ASSERT(key); - ReleaseAllPrimitiveRenderer(_KEY_IDX); + ReleaseAllPrimitiveRenderers(_KEY_IDX); } /* ============================== */ # undef _KEY_IDX /* (uintptr_t)key *//* TOUKY: I don't like that. hash should be fixed to handle these custom stuff */