diff --git a/OpenGL/CMakeLists.txt b/OpenGL/CMakeLists.txt index a5ed7473..88bfadae 100644 --- a/OpenGL/CMakeLists.txt +++ b/OpenGL/CMakeLists.txt @@ -7,8 +7,8 @@ set(CMAKE_CXX_STANDARD 23) set(VENDOR_DIR ${CMAKE_CURRENT_SOURCE_DIR}/vendor) # Add application code -file(GLOB cpp_files "src/Application.cpp" "src/rendering/*.cpp" "src/game_objects/GameObject.cpp" "src/game_objects/Background.cpp" "src/game_objects/SquareObject.cpp", "src/Input.cpp") -file(GLOB header_files "src/Application.h" "src/rendering/*.h" "src/game_objects/GameObject.h" "src/game_objects/Background.h" "src/game_objects/SquareObject.h", "src/Input.h") +file(GLOB cpp_files "src/Application.cpp" "src/rendering/*.cpp" "src/game_objects/GameObject.cpp" "src/game_objects/Background.cpp" "src/game_objects/SquareObject.cpp", "src/Input.cpp", "src/game_objects/Tile.cpp") +file(GLOB header_files "src/Application.h" "src/rendering/*.h" "src/game_objects/GameObject.h" "src/game_objects/Background.h" "src/game_objects/SquareObject.h" "src/Input.h" "src/game_objects/Tile.h") # Add resource files file(GLOB_RECURSE res_files "res/*") diff --git a/OpenGL/src/Application.cpp b/OpenGL/src/Application.cpp index 070384af..66643748 100644 --- a/OpenGL/src/Application.cpp +++ b/OpenGL/src/Application.cpp @@ -41,6 +41,7 @@ #include "game_objects/GameObject.h" #include "game_objects/Background.h" #include "game_objects/SquareObject.h" +#include "game_objects/Tile.h" #define GL_SILENCE_DEPRECATION @@ -417,6 +418,8 @@ int main(void) { std::vector> gameobjects; gameobjects.push_back(std::make_unique("Stars")); gameobjects.push_back(std::make_unique("Floor", DrawPriority::Floor, 0, 0, "floor.png")); + gameobjects.push_back(std::make_unique("Tile", true, true, 1, 0)); + gameobjects.push_back(std::make_unique("Tile", false, true, 0, 1)); // Not Emscripten-friendly if (!application.initialized) { diff --git a/OpenGL/src/game_objects/SquareObject.cpp b/OpenGL/src/game_objects/SquareObject.cpp index 09bf89c6..f595e8a9 100644 --- a/OpenGL/src/game_objects/SquareObject.cpp +++ b/OpenGL/src/game_objects/SquareObject.cpp @@ -28,7 +28,7 @@ SquareObject::SquareObject(const std::string& name, DrawPriority drawPriority, i SquareObjectVertexUniform{CalculateMVP(glm::vec2{tile_x, tile_y}, 0, 1)})), fragmentUniform( BufferView::create(SquareObjectFragmentUniform{glm::vec4(0.0f, 0.0f, 0.0f, 0.0f)})), - texture(Texture(texturePath)) {} + texture(std::make_shared(texturePath)) {} void SquareObject::render(Renderer& renderer) { this->vertexUniform.Update(SquareObjectVertexUniform{CalculateMVP(glm::vec2{tile_x, tile_y}, 0, 1)}); @@ -37,7 +37,7 @@ void SquareObject::render(Renderer& renderer) { renderer.renderPass.setPipeline(renderer.squareObject.GetPipeline()); wgpu::BindGroup bindGroup = SquareObjectLayout::BindGroup(renderer.device, vertexUniform, fragmentUniform, - texture.getTextureView(), texture.getSampler()); + texture->getTextureView(), texture->getSampler()); std::vector offset{ (uint32_t)vertexUniform.getOffset(), (uint32_t)fragmentUniform.getOffset(), diff --git a/OpenGL/src/game_objects/SquareObject.h b/OpenGL/src/game_objects/SquareObject.h index d4ba35dd..632fb799 100644 --- a/OpenGL/src/game_objects/SquareObject.h +++ b/OpenGL/src/game_objects/SquareObject.h @@ -18,5 +18,7 @@ class SquareObject : public GameObject { IndexBuffer indexBuffer; BufferView vertexUniform; BufferView fragmentUniform; - Texture texture; + +protected: + std::shared_ptr texture; }; diff --git a/OpenGL/src/game_objects/Tile.cpp b/OpenGL/src/game_objects/Tile.cpp index 73ed2891..25e22a7c 100644 --- a/OpenGL/src/game_objects/Tile.cpp +++ b/OpenGL/src/game_objects/Tile.cpp @@ -1,23 +1,13 @@ #include "Tile.h" Tile::Tile(const std::string& name, bool wall, bool unbreakable, float x, float y) - : SquareObject(name, DrawPriority::Floor, x, y, "textures/alt-wall-bright.png") + : SquareObject(name, DrawPriority::Floor, x, y, "alt-wall-bright.png") , wall(wall) - , unbreakable(unbreakable) { - wallTextureUnbreakable = Texture::create(Renderer::ResPath() + "textures/alt-wall-unbreakable.png"); - wallTexture = Texture::create(Renderer::ResPath() + "textures/alt-wall-bright.png"); - - // floor textures array - std::vector floorTextures = {"textures/2-alt-floor.png", "textures/2-alt-floor-2.png"}; - - // alternate floor textures i made. I'm going to leave them here for convenience, just comment out the above line and - // uncomment this one std::vector floorTextures = {"Textures/alt-floor.png", "Textures/alt-floor-2.png", - // "Textures/alt-floor-2.png", "Textures/alt-floor-3.png"}; - - // randomly select the texture - floorTexture = Texture::create(Renderer::ResPath() + floorTextures[rand() % floorTextures.size()]); - - + , unbreakable(unbreakable) + , wallTexture(std::make_shared("alt-wall-bright.png")) + , wallTextureUnbreakable(std::make_shared("alt-wall-unbreakable.png")) + , floorTexture( + std::make_shared(std::vector{"2-alt-floor.png", "2-alt-floor-2.png"}[rand() % 2])) { setTexture(); } diff --git a/OpenGL/src/rendering/Buffer.h b/OpenGL/src/rendering/Buffer.h index 19aea6c0..2896d11c 100644 --- a/OpenGL/src/rendering/Buffer.h +++ b/OpenGL/src/rendering/Buffer.h @@ -98,13 +98,13 @@ class Buffer : public std::enable_shared_from_this> { } else { // Check if there's space in the current buffer if (capacity_ < count_) { - allocatedIndex = capacity_; - ++capacity_; + allocatedIndex = count_; + ++count_; } else { // Need to resize the buffer expandBuffer(); - allocatedIndex = capacity_; - ++capacity_; + allocatedIndex = count_; + ++count_; } } @@ -169,7 +169,7 @@ class Buffer : public std::enable_shared_from_this> { buffer_ = newBuffer; // Update buffer capaticy now that it's been resized - capacity_ *= 2; + capacity_ = newSize / elementStride(); } // Method to free an index (called by BufferView destructor)