From 96736217fea2e2e748c034c88872875d5d46dcfa Mon Sep 17 00:00:00 2001 From: Sam Hocevar Date: Fri, 8 Mar 2019 13:58:14 +0100 Subject: [PATCH] doc: update some tutorials to use the new entity init/release mechanism. --- doc/tutorial/01_triangle.cpp | 52 ++++++++++--------- doc/tutorial/02_cube.cpp | 81 ++++++++++++++++-------------- doc/tutorial/03_noise.cpp | 64 ++++++++++++------------ doc/tutorial/04_texture.cpp | 64 +++++++++++++----------- doc/tutorial/08_fbo.cpp | 96 ++++++++++++++++++------------------ 5 files changed, 188 insertions(+), 169 deletions(-) diff --git a/doc/tutorial/01_triangle.cpp b/doc/tutorial/01_triangle.cpp index 5487daad..6d879a7d 100644 --- a/doc/tutorial/01_triangle.cpp +++ b/doc/tutorial/01_triangle.cpp @@ -26,34 +26,31 @@ LOLFX_RESOURCE_DECLARE(01_triangle); class Triangle : public WorldEntity { public: - Triangle() - : m_vertices({ vec2( 0.0f, 0.8f), - vec2(-0.8f, -0.8f), - vec2( 0.8f, -0.8f) }), - m_ready(false) + virtual bool init_draw() override { - } - - virtual void tick_draw(float seconds, Scene &scene) - { - WorldEntity::tick_draw(seconds, scene); - - if (!m_ready) + array vertices { - m_shader = Shader::Create(LOLFX_RESOURCE_NAME(01_triangle)); - m_coord = m_shader->GetAttribLocation(VertexUsage::Position, 0); + vec2( 0.0f, 0.8f), + vec2(-0.8f, -0.8f), + vec2( 0.8f, -0.8f), + }; - m_vdecl = std::make_shared(VertexStream(VertexUsage::Position)); + m_shader = Shader::Create(LOLFX_RESOURCE_NAME(01_triangle)); + m_coord = m_shader->GetAttribLocation(VertexUsage::Position, 0); - m_vbo = std::make_shared(m_vertices.bytes()); - void *vertices = m_vbo->Lock(0, 0); - memcpy(vertices, &m_vertices[0], m_vertices.bytes()); - m_vbo->Unlock(); + m_vdecl = std::make_shared(VertexStream(VertexUsage::Position)); - m_ready = true; + m_vbo = std::make_shared(vertices.bytes()); + void *data = m_vbo->Lock(0, 0); + memcpy(data, vertices.data(), vertices.bytes()); + m_vbo->Unlock(); - /* FIXME: this object never cleans up */ - } + return true; + } + + virtual void tick_draw(float seconds, Scene &scene) override + { + WorldEntity::tick_draw(seconds, scene); m_shader->Bind(); m_vdecl->SetStream(m_vbo, m_coord); @@ -62,13 +59,20 @@ public: m_vdecl->Unbind(); } + virtual bool release_draw() override + { + m_shader.reset(); + m_vdecl.reset(); + m_vbo.reset(); + + return true; + } + private: - array m_vertices; std::shared_ptr m_shader; ShaderAttrib m_coord; std::shared_ptr m_vdecl; std::shared_ptr m_vbo; - bool m_ready; }; int main(int argc, char **argv) diff --git a/doc/tutorial/02_cube.cpp b/doc/tutorial/02_cube.cpp index 669f406a..1ad431f5 100644 --- a/doc/tutorial/02_cube.cpp +++ b/doc/tutorial/02_cube.cpp @@ -41,8 +41,7 @@ public: 0, 4, 1, 5, 2, 6, 3, 7, }), m_faces_indices({ 0, 1, 2, 2, 3, 0, 1, 5, 6, 6, 2, 1, 7, 6, 5, 5, 4, 7, 4, 0, 3, 3, 7, 4, - 4, 5, 1, 1, 0, 4, 3, 2, 6, 6, 7, 3, }), - m_ready(false) + 4, 5, 1, 1, 0, 4, 3, 2, 6, 6, 7, 3, }) { m_camera = new Camera(); m_camera->SetProjection(mat4::perspective(radians(30.f), 640.f, 480.f, .1f, 1000.f)); @@ -52,7 +51,6 @@ public: Scene& scene = Scene::GetScene(); scene.PushCamera(m_camera); Ticker::Ref(m_camera); - } ~Cube() @@ -62,7 +60,37 @@ public: Ticker::Unref(m_camera); } - virtual void tick_game(float seconds) + virtual bool init_draw() override + { + m_shader = Shader::Create(LOLFX_RESOURCE_NAME(02_cube)); + + m_mvp = m_shader->GetUniformLocation("u_matrix"); + m_coord = m_shader->GetAttribLocation(VertexUsage::Position, 0); + m_color = m_shader->GetAttribLocation(VertexUsage::Color, 0); + + m_vdecl = std::make_shared( + VertexStream(VertexUsage::Position, + VertexUsage::Color)); + + m_vbo = std::make_shared(m_mesh.bytes()); + void *mesh = m_vbo->Lock(0, 0); + memcpy(mesh, m_mesh.data(), m_mesh.bytes()); + m_vbo->Unlock(); + + m_lines_ibo = std::make_shared(m_lines_indices.bytes()); + void *indices = m_lines_ibo->Lock(0, 0); + memcpy(indices, m_lines_indices.data(), m_lines_indices.bytes()); + m_lines_ibo->Unlock(); + + m_faces_ibo = std::make_shared(m_faces_indices.bytes()); + indices = m_faces_ibo->Lock(0, 0); + memcpy(indices, m_faces_indices.data(), m_faces_indices.bytes()); + m_faces_ibo->Unlock(); + + return true; + } + + virtual void tick_game(float seconds) override { WorldEntity::tick_game(seconds); @@ -95,41 +123,10 @@ public: } } - virtual void tick_draw(float seconds, Scene &scene) + virtual void tick_draw(float seconds, Scene &scene) override { WorldEntity::tick_draw(seconds, scene); - if (!m_ready) - { - m_shader = Shader::Create(LOLFX_RESOURCE_NAME(02_cube)); - - m_mvp = m_shader->GetUniformLocation("u_matrix"); - m_coord = m_shader->GetAttribLocation(VertexUsage::Position, 0); - m_color = m_shader->GetAttribLocation(VertexUsage::Color, 0); - - m_vdecl = std::make_shared( - VertexStream(VertexUsage::Position, - VertexUsage::Color)); - - m_vbo = std::make_shared(m_mesh.bytes()); - void *mesh = m_vbo->Lock(0, 0); - memcpy(mesh, &m_mesh[0], m_mesh.bytes()); - m_vbo->Unlock(); - - m_lines_ibo = std::make_shared(m_lines_indices.bytes()); - void *indices = m_lines_ibo->Lock(0, 0); - memcpy(indices, &m_lines_indices[0], m_lines_indices.bytes()); - m_lines_ibo->Unlock(); - - m_faces_ibo = std::make_shared(m_faces_indices.bytes()); - indices = m_faces_ibo->Lock(0, 0); - memcpy(indices, &m_faces_indices[0], m_faces_indices.bytes()); - m_faces_ibo->Unlock(); - - /* FIXME: this object never cleans up */ - m_ready = true; - } - scene.get_renderer()->SetClearColor(vec4(0.0f, 0.0f, 0.0f, 1.0f)); m_shader->Bind(); @@ -149,6 +146,16 @@ public: m_vdecl->Unbind(); } + virtual bool release_draw() override + { + m_shader.reset(); + m_vdecl.reset(); + m_vbo.reset(); + m_lines_ibo.reset(); + m_faces_ibo.reset(); + return true; + } + private: Camera* m_camera = nullptr; float m_angle; @@ -162,8 +169,6 @@ private: std::shared_ptr m_vdecl; std::shared_ptr m_vbo; std::shared_ptr m_lines_ibo, m_faces_ibo; - - bool m_ready; }; int main(int argc, char **argv) diff --git a/doc/tutorial/03_noise.cpp b/doc/tutorial/03_noise.cpp index 0bcd9745..780d5ce7 100644 --- a/doc/tutorial/03_noise.cpp +++ b/doc/tutorial/03_noise.cpp @@ -26,42 +26,38 @@ LOLFX_RESOURCE_DECLARE(03_noise); class NoiseDemo : public WorldEntity { public: - NoiseDemo() - : m_vertices({ vec2(-1.0, 1.0), - vec2(-1.0, -1.0), - vec2( 1.0, -1.0), - vec2(-1.0, 1.0), - vec2( 1.0, -1.0), - vec2( 1.0, 1.0), }), - m_time(0.0), - m_ready(false) + virtual bool init_draw() override { + array vertices + { + vec2(-1.0, 1.0), + vec2(-1.0, -1.0), + vec2( 1.0, -1.0), + vec2(-1.0, 1.0), + vec2( 1.0, -1.0), + vec2( 1.0, 1.0), + }; + + m_shader = Shader::Create(LOLFX_RESOURCE_NAME(03_noise)); + m_coord = m_shader->GetAttribLocation(VertexUsage::Position, 0); + m_time_uni = m_shader->GetUniformLocation("u_time"); + + m_vdecl = std::make_shared(VertexStream(VertexUsage::Position)); + + m_vbo = std::make_shared(vertices.bytes()); + void *data = m_vbo->Lock(0, 0); + memcpy(data, vertices.data(), vertices.bytes()); + m_vbo->Unlock(); + + return true; } - virtual void tick_draw(float seconds, Scene &scene) + virtual void tick_draw(float seconds, Scene &scene) override { WorldEntity::tick_draw(seconds, scene); m_time += seconds; - if (!m_ready) - { - m_shader = Shader::Create(LOLFX_RESOURCE_NAME(03_noise)); - m_coord = m_shader->GetAttribLocation(VertexUsage::Position, 0); - m_time_uni = m_shader->GetUniformLocation("u_time"); - - m_vdecl = std::make_shared(VertexStream(VertexUsage::Position)); - - m_vbo = std::make_shared(m_vertices.bytes()); - void *vertices = m_vbo->Lock(0, 0); - memcpy(vertices, &m_vertices[0], m_vertices.bytes()); - m_vbo->Unlock(); - - m_ready = true; - - /* FIXME: this object never cleans up */ - } - m_shader->Bind(); m_shader->SetUniform(m_time_uni, m_time); m_vdecl->SetStream(m_vbo, m_coord); @@ -70,15 +66,21 @@ public: m_vdecl->Unbind(); } + virtual bool release_draw() override + { + m_shader.reset(); + m_vdecl.reset(); + m_vbo.reset(); + return true; + } + private: - array m_vertices; std::shared_ptr m_shader; ShaderAttrib m_coord; ShaderUniform m_time_uni; std::shared_ptr m_vdecl; std::shared_ptr m_vbo; - float m_time; - bool m_ready; + float m_time = 0.0; }; int main(int argc, char **argv) diff --git a/doc/tutorial/04_texture.cpp b/doc/tutorial/04_texture.cpp index 14d55524..c5da5c84 100644 --- a/doc/tutorial/04_texture.cpp +++ b/doc/tutorial/04_texture.cpp @@ -27,19 +27,11 @@ class TextureDemo : public WorldEntity { public: TextureDemo() - : m_vertices({ vec2(-1.0, 1.0), - vec2(-1.0, -1.0), - vec2( 1.0, -1.0), - vec2(-1.0, 1.0), - vec2( 1.0, -1.0), - vec2( 1.0, 1.0), }), - m_frames(0), - m_ready(false) { m_heightmap.resize(TEXTURE_WIDTH * 1); } - virtual void tick_game(float seconds) + virtual void tick_game(float seconds) override { WorldEntity::tick_game(seconds); @@ -60,30 +52,37 @@ public: ++m_frames; } - virtual void tick_draw(float seconds, Scene &scene) + virtual bool init_draw() override { - WorldEntity::tick_draw(seconds, scene); - - /* Initialise GPU data */ - if (!m_ready) + array vertices { - m_texture = std::make_shared(ivec2(TEXTURE_WIDTH, 1), PixelFormat::Y_8); + vec2(-1.0, 1.0), + vec2(-1.0, -1.0), + vec2( 1.0, -1.0), + vec2(-1.0, 1.0), + vec2( 1.0, -1.0), + vec2( 1.0, 1.0), + }; - m_shader = Shader::Create(LOLFX_RESOURCE_NAME(04_texture)); - m_coord = m_shader->GetAttribLocation(VertexUsage::Position, 0); - m_texture_uni = m_shader->GetUniformLocation("u_texture"); + m_texture = std::make_shared(ivec2(TEXTURE_WIDTH, 1), PixelFormat::Y_8); - m_vdecl = std::make_shared(VertexStream(VertexUsage::Position)); + m_shader = Shader::Create(LOLFX_RESOURCE_NAME(04_texture)); + m_coord = m_shader->GetAttribLocation(VertexUsage::Position, 0); + m_texture_uni = m_shader->GetUniformLocation("u_texture"); - m_vbo = std::make_shared(m_vertices.bytes()); - void *vertices = m_vbo->Lock(0, 0); - memcpy(vertices, &m_vertices[0], m_vertices.bytes()); - m_vbo->Unlock(); + m_vdecl = std::make_shared(VertexStream(VertexUsage::Position)); - m_ready = true; + m_vbo = std::make_shared(vertices.bytes()); + void *data = m_vbo->Lock(0, 0); + memcpy(data, vertices.data(), vertices.bytes()); + m_vbo->Unlock(); - /* FIXME: this object never cleans up */ - } + return true; + } + + virtual void tick_draw(float seconds, Scene &scene) + { + WorldEntity::tick_draw(seconds, scene); /* Send new heightmap to GPU */ m_texture->Bind(); @@ -97,8 +96,16 @@ public: m_vdecl->Unbind(); } + virtual bool release_draw() override + { + m_texture.reset(); + m_shader.reset(); + m_vdecl.reset(); + m_vbo.reset(); + return true; + } + private: - array m_vertices; std::shared_ptr m_texture; std::shared_ptr m_shader; ShaderAttrib m_coord; @@ -106,8 +113,7 @@ private: std::shared_ptr m_vdecl; std::shared_ptr m_vbo; array m_heightmap; - int m_frames; - bool m_ready; + int m_frames = 0; }; int main(int argc, char **argv) diff --git a/doc/tutorial/08_fbo.cpp b/doc/tutorial/08_fbo.cpp index cccf9a6d..96b6ced1 100644 --- a/doc/tutorial/08_fbo.cpp +++ b/doc/tutorial/08_fbo.cpp @@ -25,19 +25,7 @@ LOLFX_RESOURCE_DECLARE(08_fbo); class FBO : public WorldEntity { public: - FBO() - : m_vertices { vec2( 1.0, 1.0), - vec2(-1.0, -1.0), - vec2( 1.0, -1.0), - vec2(-1.0, -1.0), - vec2( 1.0, 1.0), - vec2(-1.0, 1.0), }, - m_time(0.f), - m_ready(false) - { - } - - virtual void tick_game(float seconds) + virtual void tick_game(float seconds) override { WorldEntity::tick_game(seconds); @@ -54,42 +42,49 @@ public: m_color /= x; } - virtual void tick_draw(float seconds, Scene &scene) + virtual bool init_draw() override { - WorldEntity::tick_draw(seconds, scene); - - if (!m_ready) + array vertices { - m_shader = Shader::Create(LOLFX_RESOURCE_NAME(08_fbo)); - m_coord = m_shader->GetAttribLocation(VertexUsage::Position, 0); - m_uni_flag = m_shader->GetUniformLocation("u_flag"); - m_uni_point = m_shader->GetUniformLocation("u_point"); - m_uni_color = m_shader->GetUniformLocation("u_color"); - m_uni_texture = m_shader->GetUniformLocation("u_texture"); - - m_vdecl = std::make_shared(VertexStream(VertexUsage::Position)); - - m_vbo = std::make_shared(m_vertices.bytes()); - void *vertices = m_vbo->Lock(0, 0); - memcpy(vertices, &m_vertices[0], m_vertices.bytes()); - m_vbo->Unlock(); - - m_fbo = std::make_shared(Video::GetSize()); - m_fbo->Bind(); - - { - render_context rc(scene.get_renderer()); - rc.clear_color(vec4(0.f, 0.f, 0.f, 1.f)); - rc.clear_depth(1.f); - scene.get_renderer()->Clear(ClearMask::Color | ClearMask::Depth); - } - - m_fbo->Unbind(); + vec2( 1.0, 1.0), + vec2(-1.0, -1.0), + vec2( 1.0, -1.0), + vec2(-1.0, -1.0), + vec2( 1.0, 1.0), + vec2(-1.0, 1.0), + }; + + m_shader = Shader::Create(LOLFX_RESOURCE_NAME(08_fbo)); + m_coord = m_shader->GetAttribLocation(VertexUsage::Position, 0); + m_uni_flag = m_shader->GetUniformLocation("u_flag"); + m_uni_point = m_shader->GetUniformLocation("u_point"); + m_uni_color = m_shader->GetUniformLocation("u_color"); + m_uni_texture = m_shader->GetUniformLocation("u_texture"); + + m_vdecl = std::make_shared(VertexStream(VertexUsage::Position)); + + m_vbo = std::make_shared(vertices.bytes()); + void *data = m_vbo->Lock(0, 0); + memcpy(data, vertices.data(), vertices.bytes()); + m_vbo->Unlock(); + + m_fbo = std::make_shared(Video::GetSize()); + return true; + } - m_ready = true; + virtual void tick_draw(float seconds, Scene &scene) override + { + WorldEntity::tick_draw(seconds, scene); - /* FIXME: this object never cleans up */ + // FIXME: this should only be done once! + m_fbo->Bind(); + { + render_context rc(scene.get_renderer()); + rc.clear_color(vec4(0.f, 0.f, 0.f, 1.f)); + rc.clear_depth(1.f); + scene.get_renderer()->Clear(ClearMask::Color | ClearMask::Depth); } + m_fbo->Unbind(); /* FIXME: we should just disable depth test in the shader */ render_context rc(scene.get_renderer()); @@ -120,17 +115,24 @@ public: m_shader->Unbind(); } + virtual bool release_draw() override + { + m_shader.reset(); + m_vdecl.reset(); + m_vbo.reset(); + m_fbo.reset(); + return true; + } + private: - array m_vertices; std::shared_ptr m_shader; ShaderAttrib m_coord; ShaderUniform m_uni_flag, m_uni_point, m_uni_color, m_uni_texture; std::shared_ptr m_vdecl; std::shared_ptr m_vbo; std::shared_ptr m_fbo; - double m_time; + double m_time = 0.0f; vec3 m_hotspot, m_color; - bool m_ready; }; int main(int argc, char **argv)