diff --git a/doc/samples/btphystest.cpp b/doc/samples/btphystest.cpp index bb0bcf7a..2e46a243 100644 --- a/doc/samples/btphystest.cpp +++ b/doc/samples/btphystest.cpp @@ -721,7 +721,7 @@ void BtPhysTest::tick_draw(float seconds, Scene &scene) //----------------------------------------------------------------------------- // CShaderData //----------------------------------------------------------------------------- -CatShaderData::CatShaderData(uint32_t vert_decl_flags, Shader* shader) +CatShaderData::CatShaderData(uint32_t vert_decl_flags, std::shared_ptr shader) : GpuShaderData(vert_decl_flags, shader, DebugRenderMode::Default) { m_sprite_orientation = .0f; diff --git a/doc/samples/btphystest.h b/doc/samples/btphystest.h index 9a50c79e..5251da8c 100644 --- a/doc/samples/btphystest.h +++ b/doc/samples/btphystest.h @@ -2,7 +2,7 @@ // Lol Engine — Bullet physics test // // Copyright © 2009—2013 Benjamin “Touky” Huet -// © 2012—2018 Sam Hocevar +// © 2012—2019 Sam Hocevar // // Lol Engine is free software. It comes without any warranty, to // the extent permitted by applicable law. You can redistribute it @@ -13,6 +13,7 @@ #pragma once +#include #include #include @@ -22,7 +23,7 @@ class CatShaderData : public GpuShaderData public: //--- - CatShaderData(uint32_t vert_decl_flags, Shader* shader); + CatShaderData(uint32_t vert_decl_flags, std::shared_ptr shader); //--- void SetupDefaultData(); virtual void SetupShaderDatas(mat4 const &model); @@ -89,7 +90,7 @@ private: TileSet* m_cat_texture; - Shader* m_cat_shader; + std::shared_ptr m_cat_shader; CatShaderData* m_cat_sdata; Camera* m_camera; Controller* m_controller; diff --git a/doc/tutorial/01_triangle.cpp b/doc/tutorial/01_triangle.cpp index daaeb595..5487daad 100644 --- a/doc/tutorial/01_triangle.cpp +++ b/doc/tutorial/01_triangle.cpp @@ -1,7 +1,7 @@ // // Lol Engine — Triangle tutorial // -// Copyright © 2012—2015 Sam Hocevar +// Copyright © 2012—2019 Sam Hocevar // // Lol Engine is free software. It comes without any warranty, to // the extent permitted by applicable law. You can redistribute it @@ -14,6 +14,8 @@ # include "config.h" #endif +#include + #include #include "loldebug.h" @@ -41,9 +43,9 @@ public: m_shader = Shader::Create(LOLFX_RESOURCE_NAME(01_triangle)); m_coord = m_shader->GetAttribLocation(VertexUsage::Position, 0); - m_vdecl = new VertexDeclaration(VertexStream(VertexUsage::Position)); + m_vdecl = std::make_shared(VertexStream(VertexUsage::Position)); - m_vbo = new VertexBuffer(m_vertices.bytes()); + 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(); @@ -62,10 +64,10 @@ public: private: array m_vertices; - Shader *m_shader; + std::shared_ptr m_shader; ShaderAttrib m_coord; - VertexDeclaration *m_vdecl; - VertexBuffer *m_vbo; + std::shared_ptr m_vdecl; + std::shared_ptr m_vbo; bool m_ready; }; diff --git a/doc/tutorial/02_cube.cpp b/doc/tutorial/02_cube.cpp index 1717d641..669f406a 100644 --- a/doc/tutorial/02_cube.cpp +++ b/doc/tutorial/02_cube.cpp @@ -107,21 +107,21 @@ public: m_coord = m_shader->GetAttribLocation(VertexUsage::Position, 0); m_color = m_shader->GetAttribLocation(VertexUsage::Color, 0); - m_vdecl = - new VertexDeclaration(VertexStream(VertexUsage::Position, + m_vdecl = std::make_shared( + VertexStream(VertexUsage::Position, VertexUsage::Color)); - m_vbo = new VertexBuffer(m_mesh.bytes()); + 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 = new IndexBuffer(m_lines_indices.bytes()); + 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 = new IndexBuffer(m_faces_indices.bytes()); + 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(); @@ -156,12 +156,12 @@ private: array m_mesh; array m_lines_indices, m_faces_indices; - Shader *m_shader; + std::shared_ptr m_shader; ShaderAttrib m_coord, m_color; ShaderUniform m_mvp; - VertexDeclaration *m_vdecl; - VertexBuffer *m_vbo; - IndexBuffer *m_lines_ibo, *m_faces_ibo; + std::shared_ptr m_vdecl; + std::shared_ptr m_vbo; + std::shared_ptr m_lines_ibo, m_faces_ibo; bool m_ready; }; diff --git a/doc/tutorial/03_noise.cpp b/doc/tutorial/03_noise.cpp index 7f5e0e7a..0bcd9745 100644 --- a/doc/tutorial/03_noise.cpp +++ b/doc/tutorial/03_noise.cpp @@ -1,7 +1,7 @@ // // Lol Engine — Noise tutorial // -// Copyright © 2012—2015 Sam Hocevar +// Copyright © 2012—2019 Sam Hocevar // // Lol Engine is free software. It comes without any warranty, to // the extent permitted by applicable law. You can redistribute it @@ -14,6 +14,8 @@ # include "config.h" #endif +#include + #include #include "loldebug.h" @@ -48,9 +50,9 @@ public: m_coord = m_shader->GetAttribLocation(VertexUsage::Position, 0); m_time_uni = m_shader->GetUniformLocation("u_time"); - m_vdecl = new VertexDeclaration(VertexStream(VertexUsage::Position)); + m_vdecl = std::make_shared(VertexStream(VertexUsage::Position)); - m_vbo = new VertexBuffer(m_vertices.bytes()); + 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(); @@ -70,11 +72,11 @@ public: private: array m_vertices; - Shader *m_shader; + std::shared_ptr m_shader; ShaderAttrib m_coord; ShaderUniform m_time_uni; - VertexDeclaration *m_vdecl; - VertexBuffer *m_vbo; + std::shared_ptr m_vdecl; + std::shared_ptr m_vbo; float m_time; bool m_ready; }; diff --git a/doc/tutorial/04_texture.cpp b/doc/tutorial/04_texture.cpp index 39f75b6b..14d55524 100644 --- a/doc/tutorial/04_texture.cpp +++ b/doc/tutorial/04_texture.cpp @@ -1,7 +1,7 @@ // // Lol Engine — Graphing tutorial // -// Copyright © 2012—2015 Sam Hocevar +// Copyright © 2012—2019 Sam Hocevar // // Lol Engine is free software. It comes without any warranty, to // the extent permitted by applicable law. You can redistribute it @@ -67,15 +67,15 @@ public: /* Initialise GPU data */ if (!m_ready) { - m_texture = new Texture(ivec2(TEXTURE_WIDTH, 1), PixelFormat::Y_8); + m_texture = std::make_shared(ivec2(TEXTURE_WIDTH, 1), PixelFormat::Y_8); 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_vdecl = new VertexDeclaration(VertexStream(VertexUsage::Position)); + m_vdecl = std::make_shared(VertexStream(VertexUsage::Position)); - m_vbo = new VertexBuffer(m_vertices.bytes()); + 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(); @@ -99,12 +99,12 @@ public: private: array m_vertices; - Texture *m_texture; - Shader *m_shader; + std::shared_ptr m_texture; + std::shared_ptr m_shader; ShaderAttrib m_coord; ShaderUniform m_texture_uni; - VertexDeclaration *m_vdecl; - VertexBuffer *m_vbo; + std::shared_ptr m_vdecl; + std::shared_ptr m_vbo; array m_heightmap; int m_frames; bool m_ready; @@ -116,7 +116,6 @@ int main(int argc, char **argv) Application app("Tutorial 4: Texture", ivec2(1280, 720), 60.0f); -//new DebugFps(50, 50); new TextureDemo(); app.Run(); diff --git a/doc/tutorial/05_easymesh.cpp b/doc/tutorial/05_easymesh.cpp index 1216a9d9..e85c315c 100644 --- a/doc/tutorial/05_easymesh.cpp +++ b/doc/tutorial/05_easymesh.cpp @@ -137,7 +137,7 @@ public: #if USE_CUSTOM_SHADER /* Custom Shader: Init the shader */ - Shader *custom_shader = Shader::Create(LOLFX_RESOURCE_NAME(easymesh_shiny)); + auto custom_shader = Shader::Create(LOLFX_RESOURCE_NAME(easymesh_shiny)); // any other shader stuf here (Get uniform, mostly, and set texture) for (int i = 0; i < m_gears.count(); i++) @@ -154,7 +154,6 @@ public: } private: - Shader* m_custom_shader; array m_gears; float m_angle; mat4 m_mat; diff --git a/doc/tutorial/07_input.cpp b/doc/tutorial/07_input.cpp index edf32d60..0f21d0a4 100644 --- a/doc/tutorial/07_input.cpp +++ b/doc/tutorial/07_input.cpp @@ -168,21 +168,21 @@ public: m_coord = m_shader->GetAttribLocation(VertexUsage::Position, 0); m_color = m_shader->GetAttribLocation(VertexUsage::Color, 0); - m_vdecl = - new VertexDeclaration(VertexStream(VertexUsage::Position, + m_vdecl = std::make_shared( + VertexStream(VertexUsage::Position, VertexUsage::Color)); - m_vbo = new VertexBuffer(m_mesh.bytes()); + 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 = new IndexBuffer(m_lines_indices.bytes()); + 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 = new IndexBuffer(m_faces_indices.bytes()); + 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(); @@ -238,12 +238,12 @@ private: array m_mesh; array m_lines_indices, m_faces_indices; - Shader *m_shader; + std::shared_ptr m_shader; ShaderAttrib m_coord, m_color; ShaderUniform m_mvp; - VertexDeclaration *m_vdecl; - VertexBuffer *m_vbo; - IndexBuffer *m_lines_ibo, *m_faces_ibo; + std::shared_ptr m_vdecl; + std::shared_ptr m_vbo; + std::shared_ptr m_lines_ibo, m_faces_ibo; Text *m_text; bool m_ready; diff --git a/doc/tutorial/08_fbo.cpp b/doc/tutorial/08_fbo.cpp index 3f1ba7b2..61cecd0a 100644 --- a/doc/tutorial/08_fbo.cpp +++ b/doc/tutorial/08_fbo.cpp @@ -16,6 +16,8 @@ #include +#include + using namespace lol; LOLFX_RESOURCE_DECLARE(08_fbo); @@ -65,14 +67,14 @@ public: m_uni_color = m_shader->GetUniformLocation("u_color"); m_uni_texture = m_shader->GetUniformLocation("u_texture"); - m_vdecl = new VertexDeclaration(VertexStream(VertexUsage::Position)); + m_vdecl = std::make_shared(VertexStream(VertexUsage::Position)); - m_vbo = new VertexBuffer(m_vertices.bytes()); + 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 = new Framebuffer(Video::GetSize()); + m_fbo = std::make_shared(Video::GetSize()); m_fbo->Bind(); { @@ -120,12 +122,12 @@ public: private: array m_vertices; - Shader *m_shader; + std::shared_ptr m_shader; ShaderAttrib m_coord; ShaderUniform m_uni_flag, m_uni_point, m_uni_color, m_uni_texture; - VertexDeclaration *m_vdecl; - VertexBuffer *m_vbo; - Framebuffer *m_fbo; + std::shared_ptr m_vdecl; + std::shared_ptr m_vbo; + std::shared_ptr m_fbo; double m_time; vec3 m_hotspot, m_color; bool m_ready; diff --git a/doc/tutorial/11_fractal.cpp b/doc/tutorial/11_fractal.cpp index 2c0666cf..fec44c75 100644 --- a/doc/tutorial/11_fractal.cpp +++ b/doc/tutorial/11_fractal.cpp @@ -1,7 +1,7 @@ // // Lol Engine — Fractal tutorial // -// Copyright © 2011—2016 Sam Hocevar +// Copyright © 2011—2019 Sam Hocevar // // Lol Engine is free software. It comes without any warranty, to // the extent permitted by applicable law. You can redistribute it @@ -14,6 +14,7 @@ # include "config.h" #endif +#include #include #include @@ -460,8 +461,8 @@ public: { /* Create a texture of half the width and twice the height * so that we can upload four different subimages each frame. */ - m_texture = new Texture(ivec2(m_size.x / 2, m_size.y * 2), - PixelFormat::RGBA_8); + m_texture = std::make_shared(ivec2(m_size.x / 2, m_size.y * 2), + PixelFormat::RGBA_8); /* Ensure the texture data is complete at least once, otherwise * uploading subimages will not work. */ @@ -476,11 +477,11 @@ public: m_screenuni = m_shader->GetUniformLocation("u_screen_size"); m_zoomuni = m_shader->GetUniformLocation("u_zoom_settings"); - m_vdecl = - new VertexDeclaration(VertexStream(VertexUsage::Position), + m_vdecl = std::make_shared( + VertexStream(VertexUsage::Position), VertexStream(VertexUsage::TexCoord)); - m_vbo = new VertexBuffer(sizeof(vertices)); - m_tbo = new VertexBuffer(sizeof(texcoords)); + m_vbo = std::make_shared(sizeof(vertices)); + m_tbo = std::make_shared(sizeof(texcoords)); void *tmp = m_vbo->Lock(0, 0); memcpy(tmp, vertices, sizeof(vertices)); @@ -537,13 +538,13 @@ private: dvec2 m_texel2world; array m_pixels, m_palette; - Shader *m_shader; + std::shared_ptr m_shader; ShaderAttrib m_vertexattrib, m_texattrib; ShaderUniform m_texuni, m_texeluni, m_screenuni, m_zoomuni; - VertexDeclaration *m_vdecl; - VertexBuffer *m_vbo, *m_tbo; - Texture *m_texture; + std::shared_ptr m_vdecl; + std::shared_ptr m_vbo, m_tbo; + std::shared_ptr m_texture; int m_frame, m_slices, m_dirty[4]; bool m_ready, m_drag; diff --git a/doc/tutorial/12_voronoi.cpp b/doc/tutorial/12_voronoi.cpp index 281b7c1e..b45c8012 100644 --- a/doc/tutorial/12_voronoi.cpp +++ b/doc/tutorial/12_voronoi.cpp @@ -15,6 +15,8 @@ # include "config.h" #endif +#include + #include #include "loldebug.h" @@ -102,9 +104,9 @@ public: if (!m_ready) { - m_vdecl = new VertexDeclaration(VertexStream(VertexUsage::Position)); + m_vdecl = std::make_shared(VertexStream(VertexUsage::Position)); - m_vbo = new VertexBuffer(m_vertices.bytes()); + 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(); @@ -115,7 +117,7 @@ public: for (int i = 0; i < MaxFboType; ++i) { - m_fbos.push(new Framebuffer(Video::GetSize()), 0, array(), array() ); + m_fbos.push(std::make_shared(Video::GetSize()), 0, array(), array() ); if (i == SrcVoronoiFbo) { @@ -152,7 +154,7 @@ public: m_fbos.last().m1->Unbind(); } - temp_buffer = new Framebuffer(Video::GetSize()); + temp_buffer = std::make_shared(Video::GetSize()); temp_buffer->Bind(); { RenderContext rc(scene.get_renderer()); @@ -259,8 +261,7 @@ public: int buf = voronoi_points.count() % 2; for (int j = 0; j < voronoi_points.count(); ++j) { - Framebuffer *dst_buf; - Framebuffer *src_buf; + std::shared_ptr dst_buf, src_buf; if (buf) { @@ -314,9 +315,8 @@ public: int buf = 0; while (1) { - Framebuffer *dst_buf; - Framebuffer *src_buf; - Shader *shader; + std::shared_ptr dst_buf, src_buf; + std::shared_ptr shader; if (curres == ivec2::zero) shader = m_screen_shader; @@ -385,15 +385,15 @@ private: Controller* m_controller; array voronoi_points; array m_vertices; - Shader *m_screen_shader; + std::shared_ptr m_screen_shader; ShaderAttrib m_screen_coord; ShaderUniform m_screen_texture; - VertexDeclaration *m_vdecl; - VertexBuffer *m_vbo; + std::shared_ptr m_vdecl; + std::shared_ptr m_vbo; - array, array > m_fbos; - Framebuffer *temp_buffer; + array, std::shared_ptr, array, array > m_fbos; + std::shared_ptr temp_buffer; int mode; int m_cur_fbo; diff --git a/doc/tutorial/13_shader_builder.cpp b/doc/tutorial/13_shader_builder.cpp index ee9bbfaf..036ca0ad 100644 --- a/doc/tutorial/13_shader_builder.cpp +++ b/doc/tutorial/13_shader_builder.cpp @@ -114,8 +114,8 @@ public: //code = file.ReadString(); file.Close(); - Shader* shader = Shader::Create(builder.GetName(), code); - Shader::Destroy(shader); + std::shared_ptr shader = Shader::Create(builder.GetName(), code); + shader = nullptr; Ticker::Shutdown(); } diff --git a/src/easymesh/easymeshrender.cpp b/src/easymesh/easymeshrender.cpp index 9f5200b0..d406db29 100644 --- a/src/easymesh/easymeshrender.cpp +++ b/src/easymesh/easymeshrender.cpp @@ -2,7 +2,7 @@ // Lol Engine // // Copyright © 2009—2013 Benjamin “Touky” Huet -// © 2010—2018 Sam Hocevar +// © 2010—2019 Sam Hocevar // © 2009—2013 Cédric Lecacheur // // Lol Engine is free software. It comes without any warranty, to @@ -29,10 +29,10 @@ LOLFX_RESOURCE_DECLARE(easymesh_shiny_SK); void EasyMesh::MeshConvert() { /* Default material */ - Shader *shader = Shader::Create(LOLFX_RESOURCE_NAME(easymesh_shiny)); + auto shader = Shader::Create(LOLFX_RESOURCE_NAME(easymesh_shiny)); /* Push index buffer to GPU */ - IndexBuffer *ibo = new IndexBuffer(m_indices.count() * sizeof(uint16_t)); + auto ibo = std::make_shared(m_indices.count() * sizeof(uint16_t)); uint16_t *indices = (uint16_t *)ibo->Lock(0, 0); for (int i = 0; i < m_indices.count(); ++i) indices[i] = m_indices[i]; @@ -46,13 +46,13 @@ void EasyMesh::MeshConvert() vec4 texcoord; }; - VertexDeclaration *vdecl = new VertexDeclaration( + auto vdecl = std::make_shared( VertexStream(VertexUsage::Position, VertexUsage::Normal, VertexUsage::Color, VertexUsage::TexCoord)); - VertexBuffer *vbo = new VertexBuffer(m_vert.count() * sizeof(Vertex)); + auto vbo = std::make_shared(m_vert.count() * sizeof(Vertex)); Vertex *vert = (Vertex *)vbo->Lock(0, 0); for (int i = 0; i < m_vert.count(); ++i) { @@ -64,9 +64,9 @@ void EasyMesh::MeshConvert() vbo->Unlock(); /* Reference our new data in our submesh */ - m_submeshes.push(new SubMesh(shader, vdecl)); - m_submeshes.last()->SetIndexBuffer(ibo); - m_submeshes.last()->SetVertexBuffer(0, vbo); + m_submeshes.push_back(std::make_shared(shader, vdecl)); + m_submeshes.back()->SetIndexBuffer(ibo); + m_submeshes.back()->SetVertexBuffer(0, vbo); m_state = MeshRender::CanRender; } @@ -78,11 +78,11 @@ GpuShaderData::GpuShaderData() } //----------------------------------------------------------------------------- -GpuShaderData::GpuShaderData(uint16_t vert_decl_flags, Shader* shader, DebugRenderMode render_mode) +GpuShaderData::GpuShaderData(uint16_t vert_decl_flags, std::shared_ptr shader, DebugRenderMode render_mode) + : m_render_mode(render_mode), + m_shader(shader), + m_vert_decl_flags(vert_decl_flags) { - m_render_mode = render_mode; - m_shader = shader; - m_vert_decl_flags = vert_decl_flags; } //----------------------------------------------------------------------------- @@ -152,7 +152,7 @@ DefaultShaderData::DefaultShaderData(DebugRenderMode render_mode) } //----------------------------------------------------------------------------- -DefaultShaderData::DefaultShaderData(uint16_t vert_decl_flags, Shader* shader, bool with_UV) +DefaultShaderData::DefaultShaderData(uint16_t vert_decl_flags, std::shared_ptr shader, bool with_UV) : GpuShaderData(vert_decl_flags, shader, DebugRenderMode::Default) { StoreUniformNames(); @@ -222,16 +222,13 @@ GpuEasyMeshData::GpuEasyMeshData() { m_vertexcount = 0; m_indexcount = 0; - m_ibo = nullptr; } //----------------------------------------------------------------------------- GpuEasyMeshData::~GpuEasyMeshData() { - m_gpudatas.clear(); - m_vdatas.clear(); - if (m_ibo) - delete m_ibo; + m_gpudata.clear(); + m_vdata.clear(); } #define BUILD_VFLAG(bool_value, flag_value, check_flag) \ @@ -245,7 +242,7 @@ GpuEasyMeshData::~GpuEasyMeshData() count_value += (int)bool_value; //----------------------------------------------------------------------------- -void GpuEasyMeshData::AddGpuData(GpuShaderData* gpudata, EasyMesh* src_mesh) +void GpuEasyMeshData::AddGpuData(std::shared_ptr gpudata, std::shared_ptr src_mesh) { uint16_t vflags = gpudata->m_vert_decl_flags; @@ -273,7 +270,7 @@ void GpuEasyMeshData::AddGpuData(GpuShaderData* gpudata, EasyMesh* src_mesh) indexlist << src_mesh->m_indices[i + 2]; } - m_ibo = new IndexBuffer(indexlist.bytes()); + m_ibo = std::make_shared(indexlist.bytes()); void *indices = m_ibo->Lock(0, 0); memcpy(indices, &indexlist[0], indexlist.bytes()); m_ibo->Unlock(); @@ -282,26 +279,26 @@ void GpuEasyMeshData::AddGpuData(GpuShaderData* gpudata, EasyMesh* src_mesh) } //init to a minimum of gpudata->m_render_mode size - if (m_gpudatas.count() <= gpudata->m_render_mode) + if (m_gpudata.count() <= gpudata->m_render_mode) { - int i = m_gpudatas.count(); + int i = m_gpudata.count(); int max = gpudata->m_render_mode + 1; - m_gpudatas.reserve(max); + m_gpudata.reserve(max); for (; i < max; i++) - m_gpudatas << nullptr; + m_gpudata.push(nullptr); } - m_gpudatas[gpudata->m_render_mode] = gpudata; + m_gpudata[gpudata->m_render_mode] = gpudata; } //----------------------------------------------------------------------------- -void GpuEasyMeshData::SetupVertexData(uint16_t vflags, EasyMesh* src_mesh) +void GpuEasyMeshData::SetupVertexData(uint16_t vflags, std::shared_ptr src_mesh) { - for (int i = 0; i < m_vdatas.count(); ++i) - if (m_vdatas[i].m1 == vflags) + for (int i = 0; i < m_vdata.count(); ++i) + if (m_vdata[i].m1 == vflags) return; - VertexDeclaration* new_vdecl = nullptr; - VertexBuffer* new_vbo = nullptr; + std::shared_ptr new_vdecl; + std::shared_ptr new_vbo; void *vbo_data = nullptr; int vbo_bytes = 0; @@ -309,7 +306,7 @@ void GpuEasyMeshData::SetupVertexData(uint16_t vflags, EasyMesh* src_mesh) vbo_data = &vertexlist[0]; \ vbo_bytes = vertexlist.bytes(); \ m_vertexcount = vertexlist.count(); \ - new_vbo = new VertexBuffer(vbo_bytes); \ + new_vbo = std::make_shared(vbo_bytes); \ void *mesh = new_vbo->Lock(0, 0); \ memcpy(mesh, vbo_data, vbo_bytes); \ new_vbo->Unlock(); @@ -326,7 +323,7 @@ void GpuEasyMeshData::SetupVertexData(uint16_t vflags, EasyMesh* src_mesh) if (flagnb == 5 && has_position && has_normal && has_color && has_texcoord && has_texcoordExt) { - new_vdecl = new VertexDeclaration( + new_vdecl = std::make_shared( VertexStream( VertexUsage::Position, VertexUsage::Normal, @@ -343,7 +340,7 @@ void GpuEasyMeshData::SetupVertexData(uint16_t vflags, EasyMesh* src_mesh) } else if (flagnb == 4 && has_position && has_normal && has_color && has_texcoord) { - new_vdecl = new VertexDeclaration( + new_vdecl = std::make_shared( VertexStream( VertexUsage::Position, VertexUsage::Normal, @@ -360,7 +357,7 @@ void GpuEasyMeshData::SetupVertexData(uint16_t vflags, EasyMesh* src_mesh) } else if (flagnb == 4 && has_position && has_color && has_texcoord && has_texcoordExt) { - new_vdecl = new VertexDeclaration(VertexStream(VertexUsage::Position, VertexUsage::Color, VertexUsage::TexCoord)); + new_vdecl = std::make_shared(VertexStream(VertexUsage::Position, VertexUsage::Color, VertexUsage::TexCoord)); array vertexlist; for (int i = 0; i < src_mesh->m_vert.count(); i++) @@ -369,7 +366,7 @@ void GpuEasyMeshData::SetupVertexData(uint16_t vflags, EasyMesh* src_mesh) } else if (flagnb == 3 && has_position && has_normal && has_color) { - new_vdecl = new VertexDeclaration( + new_vdecl = std::make_shared( VertexStream( VertexUsage::Position, VertexUsage::Normal, @@ -384,7 +381,7 @@ void GpuEasyMeshData::SetupVertexData(uint16_t vflags, EasyMesh* src_mesh) } else if (flagnb == 3 && has_position && has_texcoord && has_texcoordExt) { - new_vdecl = new VertexDeclaration(VertexStream(VertexUsage::Position, VertexUsage::TexCoord)); + new_vdecl = std::make_shared(VertexStream(VertexUsage::Position, VertexUsage::TexCoord)); array vertexlist; for (int i = 0; i < src_mesh->m_vert.count(); i++) @@ -393,7 +390,7 @@ void GpuEasyMeshData::SetupVertexData(uint16_t vflags, EasyMesh* src_mesh) } else if (flagnb == 2 && has_position && has_texcoord) { - new_vdecl = new VertexDeclaration(VertexStream(VertexUsage::Position, VertexUsage::TexCoord)); + new_vdecl = std::make_shared(VertexStream(VertexUsage::Position, VertexUsage::TexCoord)); array vertexlist; for (int i = 0; i < src_mesh->m_vert.count(); i++) @@ -402,7 +399,7 @@ void GpuEasyMeshData::SetupVertexData(uint16_t vflags, EasyMesh* src_mesh) } else if (flagnb == 2 && has_position && has_color) { - new_vdecl = new VertexDeclaration(VertexStream(VertexUsage::Position, VertexUsage::Color)); + new_vdecl = std::make_shared(VertexStream(VertexUsage::Position, VertexUsage::Color)); array vertexlist; for (int i = 0; i < src_mesh->m_vert.count(); i++) @@ -412,27 +409,27 @@ void GpuEasyMeshData::SetupVertexData(uint16_t vflags, EasyMesh* src_mesh) else ASSERT(0, "no Vertex Declaration combination for 0x%04x", vflags); - m_vdatas.push(vflags, new_vdecl, new_vbo); + m_vdata.push(vflags, new_vdecl, new_vbo); } //----------------------------------------------------------------------------- void GpuEasyMeshData::RenderMeshData(mat4 const &model, int render_mode) { - ASSERT(0 <= render_mode && render_mode < m_gpudatas.count(), "render mode is not in the defined range"); - ASSERT(m_gpudatas[render_mode], "gpu datas for this render mode don't exist"); - GpuShaderData& gpu_sd = *(m_gpudatas[render_mode]); + ASSERT(0 <= render_mode && render_mode < m_gpudata.count(), "render mode is not in the defined range"); + ASSERT(m_gpudata[render_mode], "gpu datas for this render mode don't exist"); + GpuShaderData& gpu_sd = *(m_gpudata[render_mode]); int vdecl_idx = 0; - for (; vdecl_idx < m_vdatas.count(); ++vdecl_idx) - if (m_vdatas[vdecl_idx].m1 == gpu_sd.m_vert_decl_flags) + for (; vdecl_idx < m_vdata.count(); ++vdecl_idx) + if (m_vdata[vdecl_idx].m1 == gpu_sd.m_vert_decl_flags) break; - if (vdecl_idx >= m_vdatas.count()) + if (vdecl_idx >= m_vdata.count()) return; - uint16_t vflags = m_vdatas[vdecl_idx].m1; - VertexDeclaration* vdecl = m_vdatas[vdecl_idx].m2; - VertexBuffer* vbo = m_vdatas[vdecl_idx].m3; + uint16_t vflags = m_vdata[vdecl_idx].m1; + auto vdecl = m_vdata[vdecl_idx].m2; + auto vbo = m_vdata[vdecl_idx].m3; gpu_sd.m_shader->Bind(); gpu_sd.SetupShaderDatas(model); diff --git a/src/easymesh/easymeshrender.h b/src/easymesh/easymeshrender.h index 0de84341..9031ebe7 100644 --- a/src/easymesh/easymeshrender.h +++ b/src/easymesh/easymeshrender.h @@ -2,7 +2,7 @@ // Lol Engine // // Copyright © 2009—2013 Benjamin “Touky” Huet -// © 2010—2018 Sam Hocevar +// © 2010—2019 Sam Hocevar // © 2009—2013 Cédric Lecacheur // // Lol Engine is free software. It comes without any warranty, to @@ -79,7 +79,7 @@ protected: GpuShaderData(); public: //-- - GpuShaderData(uint16_t vert_decl_flags, Shader* shader, DebugRenderMode render_mode); + GpuShaderData(uint16_t vert_decl_flags, std::shared_ptr shader, DebugRenderMode render_mode); virtual ~GpuShaderData(); //-- void AddUniform(std::string const &new_uniform); @@ -92,7 +92,7 @@ public: protected: uint16_t m_vert_decl_flags; - Shader *m_shader; + std::shared_ptr m_shader; int m_render_mode; array m_shader_uniform; array m_shader_attrib; @@ -103,7 +103,7 @@ class DefaultShaderData : public GpuShaderData public: //--- DefaultShaderData(DebugRenderMode render_mode); - DefaultShaderData(uint16_t vert_decl_flags, Shader* shader, bool with_UV); + DefaultShaderData(uint16_t vert_decl_flags, std::shared_ptr shader, bool with_UV); virtual ~DefaultShaderData() {} void StoreUniformNames(); //--- @@ -122,21 +122,20 @@ public: GpuEasyMeshData(); ~GpuEasyMeshData(); //--- - void AddGpuData(GpuShaderData* gpudata, class EasyMesh* src_mesh); + void AddGpuData(std::shared_ptr gpudata, std::shared_ptr src_mesh); void RenderMeshData(mat4 const &model, int render_mode=Video::GetDebugRenderMode()); - bool HasData(int render_mode) { return (0 <= render_mode && render_mode < m_gpudatas.count() && !!m_gpudatas[render_mode]); } + bool HasData(int render_mode) { return (0 <= render_mode && render_mode < m_gpudata.count() && !!m_gpudata[render_mode]); } private: - void SetupVertexData(uint16_t vdecl_flags, EasyMesh* src_mesh); + void SetupVertexData(uint16_t vdecl_flags, std::shared_ptr src_mesh); - array m_gpudatas; + array> m_gpudata; //uint16_t are the vdecl/vbo flags to avoid copy same vdecl several times. - array m_vdatas; - int m_vertexcount; + array, std::shared_ptr> m_vdata; + int m_vertexcount; //We only need only one ibo for the whole mesh - IndexBuffer * m_ibo; - int m_indexcount; + std::shared_ptr m_ibo; + int m_indexcount; }; } /* namespace lol */ diff --git a/src/gpu/framebuffer.cpp b/src/gpu/framebuffer.cpp index bed35d08..440582e6 100644 --- a/src/gpu/framebuffer.cpp +++ b/src/gpu/framebuffer.cpp @@ -373,7 +373,7 @@ void Framebuffer::Bind() /* FIXME: this should be done in the RenderContext object * instead, maybe by getting rid of Framebuffer::Bind() and * creating RenderContext::SetFramebuffer() instead. */ - auto *renderer = Scene::GetScene(0).get_renderer(); + auto renderer = Scene::GetScene(0).get_renderer(); m_data->m_saved_viewport = renderer->GetViewport(); renderer->SetViewport(ibox2(ivec2::zero, m_data->m_size)); m_data->m_bound = true; @@ -389,7 +389,7 @@ void Framebuffer::Unbind() glBindFramebufferOES(GL_FRAMEBUFFER_OES, 0); #endif - auto *renderer = Scene::GetScene(0).get_renderer(); + auto renderer = Scene::GetScene(0).get_renderer(); renderer->SetViewport(m_data->m_saved_viewport); m_data->m_bound = false; } diff --git a/src/gpu/rendercontext.cpp b/src/gpu/rendercontext.cpp index 8235f1f3..040b097b 100644 --- a/src/gpu/rendercontext.cpp +++ b/src/gpu/rendercontext.cpp @@ -69,9 +69,9 @@ private: * Public RenderContext class */ -RenderContext::RenderContext(Renderer *renderer) +RenderContext::RenderContext(std::shared_ptr renderer) : m_renderer(renderer), - m_data(new RenderContextData()) + m_data(std::make_unique()) { } @@ -108,8 +108,6 @@ RenderContext::~RenderContext() if (m_data->m_scissor_mode.is_dirty()) m_renderer->SetScissorMode(m_data->m_scissor_mode.get()); - - delete m_data; } void RenderContext::SetViewport(ibox2 viewport) diff --git a/src/gpu/renderer.cpp b/src/gpu/renderer.cpp index d7e42c2c..12cdbe4a 100644 --- a/src/gpu/renderer.cpp +++ b/src/gpu/renderer.cpp @@ -116,7 +116,6 @@ Renderer::Renderer(ivec2 size) Renderer::~Renderer() { - delete m_data; } /* diff --git a/src/gpu/shader.cpp b/src/gpu/shader.cpp index a72f8a45..2dca99ca 100644 --- a/src/gpu/shader.cpp +++ b/src/gpu/shader.cpp @@ -13,7 +13,9 @@ #include #include +#include #include +#include #include #include @@ -89,14 +91,10 @@ private: /* Shader patcher */ static int GetVersion(); static std::string Patch(std::string const &code, ShaderType type); - - /* Global shader cache */ - static Shader *shaders[]; - static int nshaders; }; -Shader *ShaderData::shaders[256]; -int ShaderData::nshaders = 0; +/* Global shader cache */ +static std::set> g_shaders; /* * LolFx parser @@ -187,7 +185,7 @@ struct lolfx_parser::action * Public Shader class */ -Shader *Shader::Create(std::string const &name, std::string const &code) +std::shared_ptr Shader::Create(std::string const &name, std::string const &code) { lolfx_parser p(code); @@ -203,31 +201,22 @@ Shader *Shader::Create(std::string const &name, std::string const &code) size_t new_vert_crc = std::hash{}(vert); size_t new_frag_crc = std::hash{}(frag); - for (int n = 0; n < ShaderData::nshaders; n++) + for (auto shader : g_shaders) { - if (ShaderData::shaders[n]->data->vert_crc == new_vert_crc - && ShaderData::shaders[n]->data->frag_crc == new_frag_crc) - { - return ShaderData::shaders[n]; - } + if (shader->data->vert_crc == new_vert_crc + && shader->data->frag_crc == new_frag_crc) + return shader; } - Shader *ret = new Shader(name, vert, frag); - ShaderData::shaders[ShaderData::nshaders] = ret; - ShaderData::nshaders++; - + // FIXME: the cache never expires! + auto ret = std::make_shared(name, vert, frag); + g_shaders.insert(ret); return ret; } -void Shader::Destroy(Shader *shader) -{ - /* XXX: do nothing! the shader should remain in cache */ - UNUSED(shader); -} - Shader::Shader(std::string const &name, std::string const &vert, std::string const &frag) - : data(new ShaderData()) + : data(std::make_unique()) { data->m_name = name; @@ -515,8 +504,6 @@ Shader::~Shader() glDeleteShader(data->vert_id); glDeleteShader(data->frag_id); glDeleteProgram(data->prog_id); - - delete data; } /* Try to detect shader compiler features */ diff --git a/src/gpu/vertexbuffer.cpp b/src/gpu/vertexbuffer.cpp index a920646a..aeef3d5b 100644 --- a/src/gpu/vertexbuffer.cpp +++ b/src/gpu/vertexbuffer.cpp @@ -150,18 +150,19 @@ void VertexDeclaration::Unbind() glBindBuffer(GL_ARRAY_BUFFER, 0); } -void VertexDeclaration::SetStream(VertexBuffer *vb, ShaderAttrib attr1, - ShaderAttrib attr2, - ShaderAttrib attr3, - ShaderAttrib attr4, - ShaderAttrib attr5, - ShaderAttrib attr6, - ShaderAttrib attr7, - ShaderAttrib attr8, - ShaderAttrib attr9, - ShaderAttrib attr10, - ShaderAttrib attr11, - ShaderAttrib attr12) +void VertexDeclaration::SetStream(std::shared_ptr vb, + ShaderAttrib attr1, + ShaderAttrib attr2, + ShaderAttrib attr3, + ShaderAttrib attr4, + ShaderAttrib attr5, + ShaderAttrib attr6, + ShaderAttrib attr7, + ShaderAttrib attr8, + ShaderAttrib attr9, + ShaderAttrib attr10, + ShaderAttrib attr11, + ShaderAttrib attr12) { ShaderAttrib attribs[12] = { attr1, attr2, attr3, attr4, attr5, attr6, attr7, attr8, attr9, attr10, attr11, attr12 }; @@ -169,7 +170,7 @@ void VertexDeclaration::SetStream(VertexBuffer *vb, ShaderAttrib attr1, SetStream(vb, attribs); } -void VertexDeclaration::SetStream(VertexBuffer *vb, ShaderAttrib attribs[]) +void VertexDeclaration::SetStream(std::shared_ptr vb, ShaderAttrib attribs[]) { if (!vb->m_data->m_size) return; diff --git a/src/gradient.cpp b/src/gradient.cpp index 20ff83ce..463311de 100644 --- a/src/gradient.cpp +++ b/src/gradient.cpp @@ -1,15 +1,19 @@ // -// Lol Engine +// Lol Engine // -// Copyright: (c) 2010-2013 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—2019 Sam Hocevar +// +// Lol Engine 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 +#include + LOLFX_RESOURCE_DECLARE(gradient); namespace lol @@ -24,9 +28,9 @@ class GradientData friend class Gradient; private: - Shader *shader; - VertexDeclaration *m_vdecl; - VertexBuffer *m_vbo, *m_cbo; + std::shared_ptr shader; + std::shared_ptr m_vdecl; + std::shared_ptr m_vbo, m_cbo; }; /* @@ -34,14 +38,12 @@ private: */ Gradient::Gradient(vec3 aa, vec3 bb) - : data(new GradientData()) + : data(std::make_unique()) { /* FIXME: this should not be hardcoded */ m_position = aa; m_aabb.aa = aa; m_aabb.bb = bb; - - data->shader = nullptr; } void Gradient::tick_game(float seconds) @@ -71,11 +73,12 @@ void Gradient::tick_draw(float seconds, Scene &scene) { data->shader = Shader::Create(LOLFX_RESOURCE_NAME(gradient)); - data->m_vbo = new VertexBuffer(sizeof(vertex)); - data->m_cbo = new VertexBuffer(sizeof(color)); + data->m_vbo = std::make_shared(sizeof(vertex)); + data->m_cbo = std::make_shared(sizeof(color)); - data->m_vdecl = new VertexDeclaration(VertexStream(VertexUsage::Position), - VertexStream(VertexUsage::Color)); + data->m_vdecl = std::make_shared + (VertexStream(VertexUsage::Position), + VertexStream(VertexUsage::Color)); } mat4 model_matrix = mat4(1.0f); @@ -115,8 +118,6 @@ void Gradient::tick_draw(float seconds, Scene &scene) Gradient::~Gradient() { - /* FIXME: destroy shader */ - delete data; } } /* namespace lol */ diff --git a/src/gradient.h b/src/gradient.h index e2e8b654..054dfa2a 100644 --- a/src/gradient.h +++ b/src/gradient.h @@ -1,7 +1,7 @@ // // Lol Engine // -// Copyright © 2010—2018 Sam Hocevar +// Copyright © 2010—2019 Sam Hocevar // // Lol Engine is free software. It comes without any warranty, to // the extent permitted by applicable law. You can redistribute it @@ -19,6 +19,8 @@ #include "engine/worldentity.h" +#include + namespace lol { @@ -37,7 +39,7 @@ protected: virtual void tick_draw(float seconds, Scene &scene); private: - GradientData *data; + std::unique_ptr data; }; } /* namespace lol */ diff --git a/src/lol/gpu/rendercontext.h b/src/lol/gpu/rendercontext.h index 0c32984c..132f11c2 100644 --- a/src/lol/gpu/rendercontext.h +++ b/src/lol/gpu/rendercontext.h @@ -14,6 +14,8 @@ #include +#include + // // The RenderContext class // ----------------------- @@ -27,7 +29,7 @@ class RenderContextData; class RenderContext { public: - RenderContext(Renderer *renderer); + RenderContext(std::shared_ptr renderer); ~RenderContext(); void SetViewport(ibox2 viewport); @@ -60,8 +62,8 @@ public: vec4 GetScissorRect(); private: - Renderer *m_renderer; - RenderContextData *m_data; + std::shared_ptr m_renderer; + std::unique_ptr m_data; }; } /* namespace lol */ diff --git a/src/lol/gpu/renderer.h b/src/lol/gpu/renderer.h index 347ccbf3..e977f11d 100644 --- a/src/lol/gpu/renderer.h +++ b/src/lol/gpu/renderer.h @@ -134,14 +134,11 @@ enum class ScissorMode : uint8_t class Renderer { -private: - /* Only the Scene class can create a renderer for now. */ - friend class Scene; - +public: + // FIXME: only the Scene class should be allowed to create a renderer Renderer(ivec2 size); ~Renderer(); -public: void Clear(ClearMask mask); void SetViewport(ibox2 viewport); @@ -185,7 +182,7 @@ public: vec4 GetScissorRect() const; private: - RendererData *m_data; + std::unique_ptr m_data; }; } /* namespace lol */ diff --git a/src/lol/gpu/shader.h b/src/lol/gpu/shader.h index fe4ce155..a4c5b582 100644 --- a/src/lol/gpu/shader.h +++ b/src/lol/gpu/shader.h @@ -19,6 +19,7 @@ #include #include +#include #include #include "engine/entity.h" @@ -316,8 +317,7 @@ class ShaderData; class Shader : public Entity { public: - static Shader *Create(std::string const &name, std::string const &code); - static void Destroy(Shader *shader); + static std::shared_ptr Create(std::string const &name, std::string const &code); int GetAttribCount() const; ShaderAttrib GetAttribLocation(VertexUsage usage, int index) const; @@ -345,12 +345,11 @@ public: void Bind() const; void Unbind() const; -protected: Shader(std::string const &name, std::string const &vert, std::string const &frag); ~Shader(); private: - ShaderData *data; + std::unique_ptr data; public: static std::string GetVariablePrefix(const ShaderVariable variable); diff --git a/src/lol/gpu/vertexbuffer.h b/src/lol/gpu/vertexbuffer.h index f44a84d0..38dd34cd 100644 --- a/src/lol/gpu/vertexbuffer.h +++ b/src/lol/gpu/vertexbuffer.h @@ -214,20 +214,22 @@ public: void DrawIndexedElements(MeshPrimitive type, int count, const short* skip = nullptr, short typeSize = 2); void Unbind(); - void SetStream(VertexBuffer *vb, ShaderAttrib attr1, - ShaderAttrib attr2 = ShaderAttrib(), - ShaderAttrib attr3 = ShaderAttrib(), - ShaderAttrib attr4 = ShaderAttrib(), - ShaderAttrib attr5 = ShaderAttrib(), - ShaderAttrib attr6 = ShaderAttrib(), - ShaderAttrib attr7 = ShaderAttrib(), - ShaderAttrib attr8 = ShaderAttrib(), - ShaderAttrib attr9 = ShaderAttrib(), - ShaderAttrib attr10 = ShaderAttrib(), - ShaderAttrib attr11 = ShaderAttrib(), - ShaderAttrib attr12 = ShaderAttrib()); - - void SetStream(VertexBuffer *vb, ShaderAttrib attribs[]); + void SetStream(std::shared_ptr vb, + ShaderAttrib attr1, + ShaderAttrib attr2 = ShaderAttrib(), + ShaderAttrib attr3 = ShaderAttrib(), + ShaderAttrib attr4 = ShaderAttrib(), + ShaderAttrib attr5 = ShaderAttrib(), + ShaderAttrib attr6 = ShaderAttrib(), + ShaderAttrib attr7 = ShaderAttrib(), + ShaderAttrib attr8 = ShaderAttrib(), + ShaderAttrib attr9 = ShaderAttrib(), + ShaderAttrib attr10 = ShaderAttrib(), + ShaderAttrib attr11 = ShaderAttrib(), + ShaderAttrib attr12 = ShaderAttrib()); + + void SetStream(std::shared_ptr vb, + ShaderAttrib attribs[]); int GetStreamCount() const; diff --git a/src/lolimgui.cpp b/src/lolimgui.cpp index bf9e807f..2f5ab5ce 100644 --- a/src/lolimgui.cpp +++ b/src/lolimgui.cpp @@ -103,7 +103,6 @@ LolImGui::~LolImGui() Ticker::Unref(m_font); m_font = nullptr; - Shader::Destroy(m_shader); delete m_vdecl; ImGui::DestroyContext(); @@ -295,10 +294,10 @@ void LolImGui::tick_draw(float seconds, Scene &scene) { super::tick_draw(seconds, scene); - scene.AddPrimitiveRenderer(this, new PrimitiveLolImGui()); + scene.AddPrimitiveRenderer(this, std::make_shared()); } -void PrimitiveLolImGui::Render(Scene& scene, PrimitiveSource* primitive) +void PrimitiveLolImGui::Render(Scene& scene, std::shared_ptr primitive) { UNUSED(scene, primitive); @@ -383,12 +382,12 @@ void LolImGui::RenderDrawListsMethod(ImDrawData* draw_data) u8vec4 color; }; - VertexBuffer* vbo = new VertexBuffer(command_list.VtxBuffer.Size * sizeof(ImDrawVert)); + auto vbo = std::make_shared(command_list.VtxBuffer.Size * sizeof(ImDrawVert)); ImDrawVert *vert = (ImDrawVert *)vbo->Lock(0, 0); memcpy(vert, command_list.VtxBuffer.Data, command_list.VtxBuffer.Size * sizeof(ImDrawVert)); vbo->Unlock(); - IndexBuffer *ibo = new IndexBuffer(command_list.IdxBuffer.Size * sizeof(ImDrawIdx)); + auto ibo = std::make_shared(command_list.IdxBuffer.Size * sizeof(ImDrawIdx)); ImDrawIdx *indices = (ImDrawIdx *)ibo->Lock(0, 0); memcpy(indices, command_list.IdxBuffer.Data, command_list.IdxBuffer.Size * sizeof(ImDrawIdx)); ibo->Unlock(); @@ -460,9 +459,6 @@ void LolImGui::RenderDrawListsMethod(ImDrawData* draw_data) m_vdecl->Unbind(); ibo->Unbind(); - - delete vbo; - delete ibo; } m_shader->Unbind(); diff --git a/src/lolimgui.h b/src/lolimgui.h index 0ecf089c..e79abf09 100644 --- a/src/lolimgui.h +++ b/src/lolimgui.h @@ -2,7 +2,7 @@ // Lol Engine // // Copyright © 2009—2015 Benjamin “Touky” Huet -// © 2017—2018 Sam Hocevar +// © 2017—2019 Sam Hocevar // // Lol Engine is free software. It comes without any warranty, to // the extent permitted by applicable law. You can redistribute it @@ -15,6 +15,7 @@ #include #include +#include // // The Imgui integration @@ -186,7 +187,7 @@ protected: //------------------------------------------------------------------------- TextureImage* m_font = nullptr; ShaderBuilder m_builder = ShaderBuilder("imgui_shader", "120"); - Shader* m_shader = nullptr; + std::shared_ptr m_shader = nullptr; Uniform m_ortho; Uniform m_texture; array m_attribs; @@ -205,7 +206,7 @@ class PrimitiveLolImGui : public PrimitiveRenderer { public: PrimitiveLolImGui() { } - virtual void Render(Scene& scene, PrimitiveSource* primitive); + virtual void Render(Scene& scene, std::shared_ptr primitive); }; //bool ImGui_ImplGlfw_Init(GLFWwindow* window, bool install_callbacks); diff --git a/src/mesh/mesh.cpp b/src/mesh/mesh.cpp index f4ea28e2..eccd4c5c 100644 --- a/src/mesh/mesh.cpp +++ b/src/mesh/mesh.cpp @@ -12,6 +12,7 @@ #include +#include #include #include @@ -34,38 +35,38 @@ void Mesh::Render(Scene& scene, mat4 const &matrix) { //if (scene.HasPrimitiveRenderer(this) < m_submeshes.count()) { - for (int i = 0; i < m_submeshes.count(); ++i) - scene.AddPrimitiveRenderer(this, new PrimitiveMesh(m_submeshes[i], matrix)); + for (auto submesh : m_submeshes) + scene.AddPrimitiveRenderer(this, std::make_shared(submesh, matrix)); } } void Mesh::Render() { - for (int i = 0; i < m_submeshes.count(); ++i) - m_submeshes[i]->Render(); + for (auto submesh : m_submeshes) + submesh->Render(); } -void Mesh::SetMaterial(Shader *shader) +void Mesh::SetMaterial(std::shared_ptr shader) { - for (int i = 0; i < m_submeshes.count(); ++i) - m_submeshes[i]->SetShader(shader); + for (auto submesh : m_submeshes) + submesh->SetShader(shader); } /* * SubMesh class */ -SubMesh::SubMesh(Shader *shader, VertexDeclaration *vdecl) +SubMesh::SubMesh(std::shared_ptr shader, std::shared_ptr vdecl) : m_mesh_prim(MeshPrimitive::Triangles), m_shader(shader), m_vdecl(vdecl) { - Ticker::Ref(m_shader); + Ticker::Ref(m_shader.get()); } SubMesh::~SubMesh() { - Ticker::Unref(m_shader); + Ticker::Unref(m_shader.get()); // TODO: cleanup } @@ -74,24 +75,24 @@ void SubMesh::SetMeshPrimitive(MeshPrimitive mesh_primitive) m_mesh_prim = mesh_primitive; } -void SubMesh::SetShader(Shader *shader) +void SubMesh::SetShader(std::shared_ptr shader) { - Ticker::Unref(m_shader); + Ticker::Unref(m_shader.get()); m_shader = shader; - Ticker::Ref(m_shader); + Ticker::Ref(m_shader.get()); } -Shader *SubMesh::GetShader() +std::shared_ptr SubMesh::GetShader() { return m_shader; } -void SubMesh::SetVertexDeclaration(VertexDeclaration *vdecl) +void SubMesh::SetVertexDeclaration(std::shared_ptr vdecl) { m_vdecl = vdecl; } -void SubMesh::SetVertexBuffer(int index, VertexBuffer* vbo) +void SubMesh::SetVertexBuffer(int index, std::shared_ptr vbo) { while (index >= m_vbos.count()) m_vbos.push(nullptr); @@ -99,12 +100,12 @@ void SubMesh::SetVertexBuffer(int index, VertexBuffer* vbo) m_vbos[index] = vbo; } -void SubMesh::SetIndexBuffer(IndexBuffer* ibo) +void SubMesh::SetIndexBuffer(std::shared_ptr ibo) { m_ibo = ibo; } -void SubMesh::AddTexture(std::string const &name, Texture* texture) +void SubMesh::AddTexture(std::string const &name, std::shared_ptr texture) { m_textures.push(name, texture); } diff --git a/src/mesh/mesh.h b/src/mesh/mesh.h index 7035db39..11e7f656 100644 --- a/src/mesh/mesh.h +++ b/src/mesh/mesh.h @@ -1,7 +1,7 @@ // // Lol Engine // -// Copyright © 2010—2018 Sam Hocevar +// Copyright © 2010—2019 Sam Hocevar // // Lol Engine is free software. It comes without any warranty, to // the extent permitted by applicable law. You can redistribute it @@ -17,6 +17,9 @@ // -------------- // +#include +#include + #include #include @@ -59,7 +62,7 @@ public: /* FIXME: this should eventually take a “material” as argument, which * may behave differently between submeshes. */ - void SetMaterial(Shader *shader); + void SetMaterial(std::shared_ptr shader); //TODO: Not sure about the name void Render(Scene& scene, mat4 const &matrix); @@ -68,7 +71,7 @@ protected: void Render(); public: - array m_submeshes; + std::vector> m_submeshes; }; /* @@ -85,27 +88,27 @@ class SubMesh friend class Mesh; public: - SubMesh(Shader *shader, VertexDeclaration* vdecl); + SubMesh(std::shared_ptr shader, std::shared_ptr vdecl); ~SubMesh(); void SetMeshPrimitive(MeshPrimitive mesh_primitive); - void SetShader(Shader *shader); - Shader *GetShader(); - void SetVertexDeclaration(VertexDeclaration *vdecl); - void SetVertexBuffer(int index, VertexBuffer* vbo); - void SetIndexBuffer(IndexBuffer* ibo); - void AddTexture(std::string const &name, Texture* texture); + void SetShader(std::shared_ptr shader); + std::shared_ptr GetShader(); + void SetVertexDeclaration(std::shared_ptr vdecl); + void SetVertexBuffer(int index, std::shared_ptr vbo); + void SetIndexBuffer(std::shared_ptr ibo); + void AddTexture(std::string const &name, std::shared_ptr texture); protected: void Render(); MeshPrimitive m_mesh_prim; - Shader *m_shader; - VertexDeclaration* m_vdecl; - array m_vbos; - IndexBuffer *m_ibo; + std::shared_ptr m_shader; + std::shared_ptr m_vdecl; + array> m_vbos; + std::shared_ptr m_ibo; - array m_textures; + array> m_textures; }; } /* namespace lol */ diff --git a/src/mesh/primitivemesh.cpp b/src/mesh/primitivemesh.cpp index 737afa15..78af560c 100644 --- a/src/mesh/primitivemesh.cpp +++ b/src/mesh/primitivemesh.cpp @@ -1,15 +1,18 @@ // -// Lol Engine +// Lol Engine // -// Copyright: (c) 2010-2013 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—2019 Sam Hocevar +// +// Lol Engine 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 +#include #include #include @@ -20,7 +23,7 @@ namespace lol * Primitive class */ -PrimitiveMesh::PrimitiveMesh(SubMesh *submesh, mat4 const &matrix) +PrimitiveMesh::PrimitiveMesh(std::shared_ptr submesh, mat4 const &matrix) : m_submesh(submesh), m_matrix(matrix) { @@ -30,12 +33,12 @@ PrimitiveMesh::~PrimitiveMesh() { } -void PrimitiveMesh::Render(Scene& scene, PrimitiveSource* primitive) +void PrimitiveMesh::Render(Scene& scene, std::shared_ptr primitive) { /* TODO: this should be the main entry for rendering of all * primitives found in the scene graph. When we have one. */ - Shader *shader = nullptr; + std::shared_ptr shader; ShaderUniform u_model, u_modelview, u_normalmat, uni_tex, uni_texsize; ShaderAttrib a_pos, a_tex; diff --git a/src/mesh/primitivemesh.h b/src/mesh/primitivemesh.h index 34a26a3e..9eff1f0d 100644 --- a/src/mesh/primitivemesh.h +++ b/src/mesh/primitivemesh.h @@ -1,11 +1,13 @@ // -// Lol Engine +// Lol Engine // -// Copyright: (c) 2010-2013 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—2019 Sam Hocevar +// +// Lol Engine 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. // #pragma once @@ -23,12 +25,12 @@ class PrimitiveMesh : public PrimitiveRenderer friend class Scene; public: - PrimitiveMesh(SubMesh *submesh, mat4 const &matrix); + PrimitiveMesh(std::shared_ptr submesh, mat4 const &matrix); virtual ~PrimitiveMesh(); - virtual void Render(Scene& scene, PrimitiveSource* primitive); + virtual void Render(Scene& scene, std::shared_ptr primitive); private: - SubMesh *m_submesh; + std::shared_ptr m_submesh; mat4 m_matrix; }; diff --git a/src/scene.cpp b/src/scene.cpp index af80215f..9fcb0489 100644 --- a/src/scene.cpp +++ b/src/scene.cpp @@ -109,7 +109,7 @@ void SceneDisplay::Disable() void PrimitiveSource::Render(Scene& scene) { UNUSED(scene); } -void PrimitiveRenderer::Render(Scene& scene, PrimitiveSource* primitive) +void PrimitiveRenderer::Render(Scene& scene, std::shared_ptr primitive) { UNUSED(scene); UNUSED(primitive); @@ -123,7 +123,7 @@ class SceneData { friend class Scene; -private: +public: SceneData() { /* TODO: FIX THAT */ @@ -132,6 +132,7 @@ private: m_used_id = m_used_id << 1; } +private: /* Mask ID */ /* TODO: Do a mask class that handles more than 64 slots */ static uint64_t m_used_id; @@ -146,9 +147,9 @@ private: struct postprocess { - Shader *m_shader[2]; - VertexBuffer *m_vbo = nullptr; - VertexDeclaration *m_vdecl = nullptr; + std::shared_ptr m_shader[2]; + std::shared_ptr m_vbo; + std::shared_ptr m_vdecl; ShaderUniform m_buffer_uni[2][3]; ShaderAttrib m_coord[2]; } @@ -161,8 +162,8 @@ private: * - Updated by entity * - Marked Fire&Forget * - Scene is destroyed */ - std::map> m_prim_renderers; - static std::map> m_prim_sources; + std::map>> m_prim_renderers; + static std::map>> m_prim_sources; static mutex m_prim_mutex; Camera *m_default_cam; @@ -175,8 +176,8 @@ private: //vec4 m_color; array m_lines; int /*m_mask,*/ m_debug_mask; - Shader *m_shader; - VertexDeclaration *m_vdecl; + std::shared_ptr m_shader; + std::shared_ptr m_vdecl; } m_line_api; @@ -188,16 +189,16 @@ private: array m_palettes; array m_lights; - Shader *m_shader; - Shader *m_palette_shader; + std::shared_ptr m_shader; + std::shared_ptr m_palette_shader; - VertexDeclaration *m_vdecl; - array m_bufs; + std::shared_ptr m_vdecl; + array> m_bufs; } m_tile_api; }; uint64_t SceneData::m_used_id = 1; -std::map> SceneData::m_prim_sources; +std::map>> SceneData::m_prim_sources; mutex SceneData::m_prim_mutex; /* @@ -206,8 +207,8 @@ mutex SceneData::m_prim_mutex; Scene::Scene(ivec2 size) : m_size(size), m_wanted_size(size), - data(new SceneData()), - m_renderer(new Renderer(size)) + data(std::make_unique()), + m_renderer(std::make_shared(size)) { data->m_renderbuffer[0] = new Framebuffer(m_size); data->m_renderbuffer[1] = new Framebuffer(m_size); @@ -217,7 +218,7 @@ Scene::Scene(ivec2 size) data->m_pp.m_shader[1] = Shader::Create(LOLFX_RESOURCE_NAME(gpu_postprocess)); data->m_pp.m_coord[0] = data->m_pp.m_shader[0]->GetAttribLocation(VertexUsage::Position, 0); data->m_pp.m_coord[1] = data->m_pp.m_shader[1]->GetAttribLocation(VertexUsage::Position, 0); - data->m_pp.m_vdecl = new VertexDeclaration(VertexStream(VertexUsage::Position)); + data->m_pp.m_vdecl = std::make_shared(VertexStream(VertexUsage::Position)); data->m_pp.m_buffer_uni[0][0] = data->m_pp.m_shader[0]->GetUniformLocation("u_buffer"); data->m_pp.m_buffer_uni[1][0] = data->m_pp.m_shader[1]->GetUniformLocation("u_buffer"); data->m_pp.m_buffer_uni[1][1] = data->m_pp.m_shader[1]->GetUniformLocation("u_prev_buffer"); @@ -226,7 +227,7 @@ Scene::Scene(ivec2 size) array quad { 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), }; - data->m_pp.m_vbo = new VertexBuffer(quad.bytes()); + data->m_pp.m_vbo = std::make_shared(quad.bytes()); void *vertices = data->m_pp.m_vbo->Lock(0, 0); memcpy(vertices, quad.data(), quad.bytes()); data->m_pp.m_vbo->Unlock(); @@ -240,11 +241,11 @@ Scene::Scene(ivec2 size) data->m_tile_api.m_cam = -1; data->m_tile_api.m_shader = 0; data->m_tile_api.m_palette_shader = 0; - data->m_tile_api.m_vdecl = new VertexDeclaration(VertexStream(VertexUsage::Position), - VertexStream(VertexUsage::TexCoord)); + data->m_tile_api.m_vdecl = std::make_shared(VertexStream(VertexUsage::Position), + VertexStream(VertexUsage::TexCoord)); data->m_line_api.m_shader = 0; - data->m_line_api.m_vdecl = new VertexDeclaration(VertexStream(VertexUsage::Position, VertexUsage::Color)); + data->m_line_api.m_vdecl = std::make_shared(VertexStream(VertexUsage::Position, VertexUsage::Color)); data->m_line_api.m_debug_mask = 1; } @@ -264,11 +265,6 @@ Scene::~Scene() /* FIXME: also, make sure we do not add code to Reset() that will * reallocate stuff */ Reset(); - - delete data->m_line_api.m_vdecl; - delete data->m_tile_api.m_vdecl; - delete data; - delete m_renderer; } //----------------------------------------------------------------------------- @@ -381,10 +377,7 @@ void Scene::Reset() ReleasePrimitiveRenderer(idx--, key); } - for (int i = 0; i < data->m_tile_api.m_bufs.count(); i++) - delete data->m_tile_api.m_bufs[i]; data->m_tile_api.m_bufs.clear(); - data->m_tile_api.m_lights.clear(); } @@ -400,7 +393,7 @@ int Scene::HasPrimitiveSource(uintptr_t key) return count; } -int Scene::AddPrimitiveSource(uintptr_t key, PrimitiveSource* source) +int Scene::AddPrimitiveSource(uintptr_t key, std::shared_ptr source) { int count; SceneData::m_prim_mutex.lock(); @@ -412,12 +405,14 @@ int Scene::AddPrimitiveSource(uintptr_t key, PrimitiveSource* source) return count; } -void Scene::SetPrimitiveSource(int index, uintptr_t key, PrimitiveSource* source) +void Scene::SetPrimitiveSource(int index, uintptr_t key, std::shared_ptr source) { ASSERT(source); ASSERT(index >= 0); - PrimitiveSource* old = nullptr; + // Keep reference to old source until AFTER we release the lock + std::shared_ptr old; + SceneData::m_prim_mutex.lock(); { if (index < SceneData::m_prim_sources[key].count()) @@ -427,14 +422,11 @@ void Scene::SetPrimitiveSource(int index, uintptr_t key, PrimitiveSource* source SceneData::m_prim_sources[key][index] = source; } SceneData::m_prim_mutex.unlock(); - - // Delete old AFTER having released the lock - delete old; } void Scene::ReleasePrimitiveSource(int index, uintptr_t key) { - PrimitiveSource* old = nullptr; + std::shared_ptr old; SceneData::m_prim_mutex.lock(); { ASSERT(0 <= index && index < SceneData::m_prim_sources[key].count()); @@ -442,26 +434,21 @@ void Scene::ReleasePrimitiveSource(int index, uintptr_t key) SceneData::m_prim_sources[key].remove(index); } SceneData::m_prim_mutex.unlock(); - - // Delete old AFTER having released the lock - delete old; } void Scene::ReleaseAllPrimitiveSources(uintptr_t key) { - array oldies; + // Delete oldies AFTER having released the lock + array> oldies; + SceneData::m_prim_mutex.lock(); { oldies.reserve(SceneData::m_prim_sources[key].count()); - for (PrimitiveSource* source : SceneData::m_prim_sources[key]) + for (auto source : SceneData::m_prim_sources[key]) oldies << source; SceneData::m_prim_sources[key].clear(); } SceneData::m_prim_mutex.unlock(); - - // Delete oldies AFTER having released the lock - for (PrimitiveSource* old : oldies) - delete old; } //---- Primitive renderer stuff ----------------------------------------------- @@ -470,23 +457,18 @@ int Scene::HasPrimitiveRenderer(uintptr_t key) return data->m_prim_renderers[key].count(); } -void Scene::AddPrimitiveRenderer(uintptr_t key, PrimitiveRenderer* renderer) +void Scene::AddPrimitiveRenderer(uintptr_t key, std::shared_ptr renderer) { renderer->m_fire_and_forget = true; data->m_prim_renderers[key].push(renderer); } -void Scene::SetPrimitiveRenderer(int index, uintptr_t key, PrimitiveRenderer* renderer) +void Scene::SetPrimitiveRenderer(int index, uintptr_t key, std::shared_ptr renderer) { ASSERT(renderer); ASSERT(index >= 0); - if (index < data->m_prim_renderers[key].count()) - { - ASSERT(data->m_prim_renderers[key][index]); - delete data->m_prim_renderers[key][index]; - } - else + if (index >= data->m_prim_renderers[key].count()) data->m_prim_renderers[key].resize(index + 1); data->m_prim_renderers[key][index] = renderer; } @@ -495,17 +477,13 @@ void Scene::ReleasePrimitiveRenderer(int index, uintptr_t key) { ASSERT(0 <= index && index < data->m_prim_renderers[key].count()); - delete data->m_prim_renderers[key][index]; data->m_prim_renderers[key].remove(index); } void Scene::ReleaseAllPrimitiveRenderers(uintptr_t key) { - for (PrimitiveRenderer*& renderer : data->m_prim_renderers[key]) - { - delete renderer; + for (auto& renderer : data->m_prim_renderers[key]) renderer = nullptr; - } } //----------------------------------------------------------------------------- @@ -719,7 +697,8 @@ void Scene::render_primitives() for (int idx = 0; idx < data->m_prim_renderers[key].count(); ++idx) { /* TODO: Not sure if thread compliant */ - data->m_prim_renderers[key][idx]->Render(*this, idx < SceneData::m_prim_sources[key].count() ? SceneData::m_prim_sources[key][idx] : nullptr); + if (idx < SceneData::m_prim_sources[key].count()) + data->m_prim_renderers[key][idx]->Render(*this, SceneData::m_prim_sources[key][idx]); } } } @@ -755,8 +734,8 @@ void Scene::render_tiles() // XXX: rename to Blit() for (int p = 0; p < 2; p++) { - Shader *shader = (p == 0) ? data->m_tile_api.m_shader : data->m_tile_api.m_palette_shader; - array& tiles = (p == 0) ? data->m_tile_api.m_tiles : data->m_tile_api.m_palettes; + auto shader = (p == 0) ? data->m_tile_api.m_shader : data->m_tile_api.m_palette_shader; + auto &tiles = (p == 0) ? data->m_tile_api.m_tiles : data->m_tile_api.m_palettes; if (tiles.count() == 0) continue; @@ -787,9 +766,9 @@ void Scene::render_tiles() // XXX: rename to Blit() break; /* Create a vertex array object */ - VertexBuffer *vb1 = new VertexBuffer(6 * (n - i) * sizeof(vec3)); + auto vb1 = std::make_shared(6 * (n - i) * sizeof(vec3)); vec3 *vertex = (vec3 *)vb1->Lock(0, 0); - VertexBuffer *vb2 = new VertexBuffer(6 * (n - i) * sizeof(vec2)); + auto vb2 = std::make_shared(6 * (n - i) * sizeof(vec2)); vec2 *texture = (vec2 *)vb2->Lock(0, 0); data->m_tile_api.m_bufs.push(vb1); @@ -889,7 +868,7 @@ void Scene::render_lines(float seconds) linecount--; } } - VertexBuffer *vb = new VertexBuffer(buff.bytes()); + auto vb = std::make_shared(buff.bytes()); float *vertex = (float *)vb->Lock(0, 0); memcpy(vertex, buff.data(), buff.bytes()); vb->Unlock(); @@ -915,7 +894,6 @@ void Scene::render_lines(float seconds) data->m_line_api.m_shader->Unbind(); //data->m_line_api.m_lines.clear(); - delete vb; } } /* namespace lol */ diff --git a/src/scene.h b/src/scene.h index fb140c9c..87a0dd1a 100644 --- a/src/scene.h +++ b/src/scene.h @@ -18,7 +18,8 @@ // --------------- // -#include +#include +#include #include "tileset.h" #include "light.h" @@ -51,7 +52,7 @@ class PrimitiveRenderer public: PrimitiveRenderer() { } virtual ~PrimitiveRenderer() { } - virtual void Render(Scene& scene, PrimitiveSource* primitive); + virtual void Render(Scene& scene, std::shared_ptr primitive); private: bool m_fire_and_forget = false; @@ -129,15 +130,15 @@ public: void Reset(); - Renderer *get_renderer() { return m_renderer; } + std::shared_ptr get_renderer() { return m_renderer; } /* ============================== */ # define _KEY_IDX (uintptr_t)key /* TOUKY: I don't like that. hash should be fixed to handle these custom stuff */ /* ============================== */ private: int HasPrimitiveSource(uintptr_t key); - int AddPrimitiveSource(uintptr_t key, class PrimitiveSource* source); - void SetPrimitiveSource(int index, uintptr_t key, class PrimitiveSource* source); + int AddPrimitiveSource(uintptr_t key, std::shared_ptr source); + void SetPrimitiveSource(int index, uintptr_t key, std::shared_ptr source); void ReleasePrimitiveSource(int index, uintptr_t key); void ReleaseAllPrimitiveSources(uintptr_t key); public: @@ -152,7 +153,7 @@ public: /* Add a primitive sources linked to the given entity * Returns the slot number */ template - int AddPrimitiveSource(T* key, class PrimitiveSource* source) + int AddPrimitiveSource(T* key, std::shared_ptr source) { ASSERT(key); return AddPrimitiveSource(_KEY_IDX, source); @@ -161,7 +162,7 @@ public: * Deletes the old one * The slot is kept even if source == nullptr */ template - void SetPrimitiveSource(int index, T* key, class PrimitiveSource* source) + void SetPrimitiveSource(int index, T* key, std::shared_ptr source) { ASSERT(key); SetPrimitiveSource(index, _KEY_IDX, source); @@ -184,8 +185,8 @@ public: private: int HasPrimitiveRenderer(uintptr_t key); - void AddPrimitiveRenderer(uintptr_t key, class PrimitiveRenderer* renderer); - void SetPrimitiveRenderer(int index, uintptr_t key, class PrimitiveRenderer* renderer); + void AddPrimitiveRenderer(uintptr_t key, std::shared_ptr renderer); + void SetPrimitiveRenderer(int index, uintptr_t key, std::shared_ptr renderer); void ReleasePrimitiveRenderer(int index, uintptr_t key); void ReleaseAllPrimitiveRenderers(uintptr_t key); public: @@ -201,7 +202,7 @@ public: * The primitive is considered as Fire&Forget and * will be destroyed at the end of the frame */ template - void AddPrimitiveRenderer(T* key, class PrimitiveRenderer* renderer) + void AddPrimitiveRenderer(T* key, std::shared_ptr renderer) { ASSERT(key); AddPrimitiveRenderer(_KEY_IDX, renderer); @@ -210,7 +211,7 @@ public: * Deletes the old one * Will assert if renderer == nullptr */ template - void SetPrimitiveRenderer(int index, T* key, class PrimitiveRenderer* renderer) + void SetPrimitiveRenderer(int index, T* key, std::shared_ptr renderer) { ASSERT(key && renderer); SetPrimitiveRenderer(index, _KEY_IDX, renderer); @@ -263,8 +264,8 @@ private: ivec2 m_size, m_wanted_size; - SceneData *data; - Renderer *m_renderer; + std::unique_ptr data; + std::shared_ptr m_renderer; }; } /* namespace lol */