diff --git a/examples/dev_testing/src/main.cpp b/examples/dev_testing/src/main.cpp index c09386a..86a4422 100644 --- a/examples/dev_testing/src/main.cpp +++ b/examples/dev_testing/src/main.cpp @@ -53,8 +53,8 @@ int main(int argc, char *argv[]) { {glm::vec3(0.f, 0.5f, 0.f), glm::vec3(0.f, 0.f, 1.f), glm::vec2(0.5f, 1.0f)}, // top }; - std::shared_ptr tri(new simpleengine::shapes_2d::Triangle(base_shader_program, vertices)); - //tri->set_texture(wall_texture); + auto tri = std::make_shared(base_shader_program, vertices); + tri->set_texture(wall_texture); game.add_event(tri); /* std::vector vertices = { @@ -69,7 +69,8 @@ int main(int argc, char *argv[]) { 1, 2, 3 }; - std::shared_ptr square(new simpleengine::shapes_2d::Square(base_shader_program, crate_texture, vertices, indicies)); + auto square = std::make_shared(base_shader_program, vertices, indicies); + //square->set_texture(crate_texture); game.add_event(square); */ return game.run(); diff --git a/include/simpleengine/shapes/2d/square.h b/include/simpleengine/shapes/2d/square.h index eabab85..054207a 100644 --- a/include/simpleengine/shapes/2d/square.h +++ b/include/simpleengine/shapes/2d/square.h @@ -19,17 +19,20 @@ #include "../../gfx/vbo.h" #include "../../gfx/vao.h" #include "../../gfx/texture.h" +#include "../../gfx/shader.h" #include #include +#include "../../optional.h" + namespace simpleengine::shapes_2d { class Square : public simpleengine::Renderable { private: using super = simpleengine::Renderable; private: - std::shared_ptr shader_program; - Texture& texture; + gfx::Shader shader; + nonstd::optional texture; public: std::vector vertices; std::vector indicies; @@ -37,42 +40,17 @@ namespace simpleengine::shapes_2d { gfx::VBO vbo; gfx::VAO vao; - Square(std::shared_ptr shader_program, Texture& texture, std::vector vertices, std::vector indicies) : - super(nullptr), shader_program(shader_program), vertices(vertices), indicies(indicies), - ebo(gfx::VBO(GL_ELEMENT_ARRAY_BUFFER, false)), vbo(gfx::VBO(GL_ARRAY_BUFFER, false)), - texture(texture) { + Square(gfx::Shader shader, std::vector vertices, std::vector indicies); - vao.bind(); - vbo.buffer(vertices.data(), 0, sizeof(Vertex) * vertices.size()); - ebo.buffer(indicies.data(), 0, indicies.size() * sizeof(GLuint)); - - glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, sizeof(Vertex), (GLvoid*)offsetof(Vertex, position)); - glEnableVertexAttribArray(0); - - glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, sizeof(Vertex), (GLvoid*)offsetof(Vertex, color)); - glEnableVertexAttribArray(1); - - glVertexAttribPointer(2, 2, GL_FLOAT, GL_FALSE, sizeof(Vertex), (GLvoid*)offsetof(Vertex, tex_coord)); - glEnableVertexAttribArray(2); - - glBindBuffer(GL_ARRAY_BUFFER, 0); - glBindVertexArray(0); - } + Square(std::shared_ptr shader_program, std::vector vertices, std::vector indicies); virtual ~Square() = default; + + void set_texture(Texture texture); - virtual void update(const float& delta_time) override { + virtual void update(const float& delta_time) override; - } - - virtual void render(std::shared_ptr target) override { - texture.bind(); - - glUseProgram(*shader_program); - - vao.bind(); - glDrawElements(GL_TRIANGLES, indicies.size(), GL_UNSIGNED_INT, 0); - } + virtual void render(std::shared_ptr target) override; }; } diff --git a/include/simpleengine/shapes/2d/triangle.h b/include/simpleengine/shapes/2d/triangle.h index a34c4cb..4990bd4 100644 --- a/include/simpleengine/shapes/2d/triangle.h +++ b/include/simpleengine/shapes/2d/triangle.h @@ -35,48 +35,17 @@ namespace simpleengine::shapes_2d { gfx::VBO vbo; gfx::VAO vao; - Triangle(gfx::Shader shader, std::vector vertices) : super(nullptr), - shader(shader), vertices(vertices), vbo(gfx::VBO(GL_ARRAY_BUFFER, false)), texture(nonstd::nullopt) { - vao.bind(); - vbo.buffer(vertices.data(), 0, sizeof(Vertex) * vertices.size()); + Triangle(gfx::Shader shader, std::vector vertices); - vao.enable_attrib(vbo, 0, 3, GL_FLOAT, sizeof(Vertex), offsetof(Vertex, position)); - vao.enable_attrib(vbo, 1, 3, GL_FLOAT, sizeof(Vertex), offsetof(Vertex, color)); - vao.enable_attrib(vbo, 2, 2, GL_FLOAT, sizeof(Vertex), offsetof(Vertex, tex_coord)); - - glBindBuffer(GL_ARRAY_BUFFER, 0); - glBindVertexArray(0); - } - - Triangle(std::shared_ptr shader_program, std::vector vertices) : - Triangle(gfx::Shader(shader_program), vertices) { - - } + Triangle(std::shared_ptr shader_program, std::vector vertices); virtual ~Triangle() = default; - void set_texture(Texture texture) { - this->texture = texture; - } + void set_texture(Texture texture); - virtual void update(const float& delta_time) override { + virtual void update(const float& delta_time) override; - } - - virtual void render(std::shared_ptr target) override { - shader.use(); - - // If theres a texture set, tell the fragment shader that and bind to the texture for drawing. - if (texture.has_value()) { - shader.setUniformInt("texture_is_set", true, false); - texture.value().bind(); - } else { - shader.setUniformInt("texture_is_set", false, false); - } - - vao.bind(); - glDrawArrays(GL_TRIANGLES, 0, vertices.size()); - } + virtual void render(std::shared_ptr target) override; }; } diff --git a/src/shapes/2d/square.cpp b/src/shapes/2d/square.cpp new file mode 100644 index 0000000..ac8eb2a --- /dev/null +++ b/src/shapes/2d/square.cpp @@ -0,0 +1,53 @@ +#include "shapes/2d/square.h" + +namespace simpleengine::shapes_2d { + Square::Square(gfx::Shader shader, std::vector vertices, std::vector indicies) : + super(nullptr), shader(shader), vertices(vertices), indicies(indicies), + ebo(gfx::VBO(GL_ELEMENT_ARRAY_BUFFER, false)), vbo(gfx::VBO(GL_ARRAY_BUFFER, false)), + texture(nonstd::nullopt) { + + vao.bind(); + vbo.buffer(vertices.data(), 0, sizeof(Vertex) * vertices.size()); + ebo.buffer(indicies.data(), 0, indicies.size() * sizeof(GLuint)); + + glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, sizeof(Vertex), (GLvoid*)offsetof(Vertex, position)); + glEnableVertexAttribArray(0); + + glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, sizeof(Vertex), (GLvoid*)offsetof(Vertex, color)); + glEnableVertexAttribArray(1); + + glVertexAttribPointer(2, 2, GL_FLOAT, GL_FALSE, sizeof(Vertex), (GLvoid*)offsetof(Vertex, tex_coord)); + glEnableVertexAttribArray(2); + + glBindBuffer(GL_ARRAY_BUFFER, 0); + glBindVertexArray(0); + } + + Square::Square(std::shared_ptr shader_program, std::vector vertices, std::vector indicies) : + Square(gfx::Shader(shader_program), vertices, indicies) { + + } + + void Square::set_texture(Texture texture) { + this->texture = texture; + } + + void Square::update(const float& delta_time) { + + } + + void Square::render(std::shared_ptr target) { + shader.use(); + + // If theres a texture set, tell the fragment shader that and bind to the texture for drawing. + if (texture.has_value()) { + shader.setUniformInt("texture_is_set", true, false); + texture.value().bind(); + } else { + shader.setUniformInt("texture_is_set", false, false); + } + + vao.bind(); + glDrawElements(GL_TRIANGLES, indicies.size(), GL_UNSIGNED_INT, 0); + } +} \ No newline at end of file diff --git a/src/shapes/2d/triangle.cpp b/src/shapes/2d/triangle.cpp new file mode 100644 index 0000000..b85fc42 --- /dev/null +++ b/src/shapes/2d/triangle.cpp @@ -0,0 +1,44 @@ +#include "shapes/2d/triangle.h" + +namespace simpleengine::shapes_2d { + Triangle::Triangle(gfx::Shader shader, std::vector vertices) : super(nullptr), + shader(shader), vertices(vertices), vbo(gfx::VBO(GL_ARRAY_BUFFER, false)), texture(nonstd::nullopt) { + vao.bind(); + vbo.buffer(vertices.data(), 0, sizeof(Vertex) * vertices.size()); + + vao.enable_attrib(vbo, 0, 3, GL_FLOAT, sizeof(Vertex), offsetof(Vertex, position)); + vao.enable_attrib(vbo, 1, 3, GL_FLOAT, sizeof(Vertex), offsetof(Vertex, color)); + vao.enable_attrib(vbo, 2, 2, GL_FLOAT, sizeof(Vertex), offsetof(Vertex, tex_coord)); + + glBindBuffer(GL_ARRAY_BUFFER, 0); + glBindVertexArray(0); + } + + Triangle::Triangle(std::shared_ptr shader_program, std::vector vertices) : + Triangle(gfx::Shader(shader_program), vertices) { + + } + + void Triangle::set_texture(Texture texture) { + this->texture = texture; + } + + void Triangle::update(const float& delta_time) { + + } + + void Triangle::render(std::shared_ptr target) { + shader.use(); + + // If theres a texture set, tell the fragment shader that and bind to the texture for drawing. + if (texture.has_value()) { + shader.setUniformInt("texture_is_set", true, false); + texture.value().bind(); + } else { + shader.setUniformInt("texture_is_set", false, false); + } + + vao.bind(); + glDrawArrays(GL_TRIANGLES, 0, vertices.size()); + } +} \ No newline at end of file