Skip to content

Commit

Permalink
Can now render tiles!
Browse files Browse the repository at this point in the history
  • Loading branch information
anchpop committed Oct 11, 2024
1 parent bee9db8 commit fa1a151
Show file tree
Hide file tree
Showing 6 changed files with 21 additions and 26 deletions.
4 changes: 2 additions & 2 deletions OpenGL/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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/*")
Expand Down
3 changes: 3 additions & 0 deletions OpenGL/src/Application.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -417,6 +418,8 @@ int main(void) {
std::vector<std::unique_ptr<GameObject>> gameobjects;
gameobjects.push_back(std::make_unique<Background>("Stars"));
gameobjects.push_back(std::make_unique<SquareObject>("Floor", DrawPriority::Floor, 0, 0, "floor.png"));
gameobjects.push_back(std::make_unique<Tile>("Tile", true, true, 1, 0));
gameobjects.push_back(std::make_unique<Tile>("Tile", false, true, 0, 1));

// Not Emscripten-friendly
if (!application.initialized) {
Expand Down
4 changes: 2 additions & 2 deletions OpenGL/src/game_objects/SquareObject.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<SquareObjectFragmentUniform>::create(SquareObjectFragmentUniform{glm::vec4(0.0f, 0.0f, 0.0f, 0.0f)})),
texture(Texture(texturePath)) {}
texture(std::make_shared<Texture>(texturePath)) {}

void SquareObject::render(Renderer& renderer) {
this->vertexUniform.Update(SquareObjectVertexUniform{CalculateMVP(glm::vec2{tile_x, tile_y}, 0, 1)});
Expand All @@ -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<uint32_t> offset{
(uint32_t)vertexUniform.getOffset(),
(uint32_t)fragmentUniform.getOffset(),
Expand Down
4 changes: 3 additions & 1 deletion OpenGL/src/game_objects/SquareObject.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,7 @@ class SquareObject : public GameObject {
IndexBuffer indexBuffer;
BufferView<SquareObjectVertexUniform> vertexUniform;
BufferView<SquareObjectFragmentUniform> fragmentUniform;
Texture texture;

protected:
std::shared_ptr<Texture> texture;
};
22 changes: 6 additions & 16 deletions OpenGL/src/game_objects/Tile.cpp
Original file line number Diff line number Diff line change
@@ -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<std::string> 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<std::string> 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<Texture>("alt-wall-bright.png"))
, wallTextureUnbreakable(std::make_shared<Texture>("alt-wall-unbreakable.png"))
, floorTexture(
std::make_shared<Texture>(std::vector<std::string>{"2-alt-floor.png", "2-alt-floor-2.png"}[rand() % 2])) {
setTexture();
}

Expand Down
10 changes: 5 additions & 5 deletions OpenGL/src/rendering/Buffer.h
Original file line number Diff line number Diff line change
Expand Up @@ -98,13 +98,13 @@ class Buffer : public std::enable_shared_from_this<Buffer<T, Uniform>> {
} 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_;
}
}

Expand Down Expand Up @@ -169,7 +169,7 @@ class Buffer : public std::enable_shared_from_this<Buffer<T, Uniform>> {
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)
Expand Down

0 comments on commit fa1a151

Please sign in to comment.