Skip to content

Commit

Permalink
fix texture finding
Browse files Browse the repository at this point in the history
  • Loading branch information
nightingazer committed Feb 2, 2023
1 parent 50087c5 commit 5505c1a
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 7 deletions.
6 changes: 3 additions & 3 deletions AGE/src/Age/Renderer/Renderer2D.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ namespace AGE {

uint32_t QuadIndexCount = 0;

std::array<Ref<Texture2D>, MaxTextureSlots> TextureSlots;
std::array<Ref<Texture2D>, MaxTextureSlots> TextureSlots{UnitTexture};
uint32_t TextureSlotIndex = 1; //0 - Unit Texture;

glm::vec4 QuadVertexPositions[4];
Expand Down Expand Up @@ -320,7 +320,7 @@ namespace AGE {
}

int Renderer2D::FindTextureIndex(const Ref<Texture2D>& texture) {
int textureIndex = 0;
int textureIndex = -1;

if (texture != nullptr) {
for (int i{0}; i < s_Data.TextureSlotIndex; i++) {
Expand All @@ -330,7 +330,7 @@ namespace AGE {
}
}

if (textureIndex == 0) {
if (textureIndex == -1) {
textureIndex = (int)s_Data.TextureSlotIndex;
s_Data.TextureSlots[textureIndex] = texture;
s_Data.TextureSlotIndex++;
Expand Down
2 changes: 1 addition & 1 deletion AGE/src/Age/Renderer/Texture.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ namespace AGE {
static Ref<Texture2D> Create(const uint32_t width, const uint32_t height);
static Ref<Texture2D> ErrorTexture();

virtual bool operator==(const Texture2D& other) const = 0;
virtual bool operator==(const Texture2D& other) const { return ID() == other.ID(); };
};
}

Expand Down
43 changes: 40 additions & 3 deletions Sandbox/src/Sandbox2DLayer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ static const char* s_MapTiles =
"WWWWWWWWDDDDDDDDDWWWWWWW"
"WWWWWWWWWWWWWWWWWWWWWWWW";

Sandbox2DLayer::Sandbox2DLayer() : m_CameraController(1280.0f / 720.0f) {
Sandbox2DLayer::Sandbox2DLayer() : m_CameraController(1280.0f / 720.0f), m_Particles(5000) {
AGE_PROFILE_FUNCTION();

m_SpriteSheet = AGE::Texture2D::Create("assets/textures/tilemap.png");
Expand All @@ -50,13 +50,50 @@ void Sandbox2DLayer::OnUpdate(AGE::Timestep ts) {
for(uint32_t x{0}; x < mapWidth; x++) {
float normX = (float)x - ((float)mapWidth / 2.0f);
if(m_TileMap.find(s_MapTiles[y * mapWidth + x]) == m_TileMap.end()) {
AGE::Renderer2D::DrawQuad({normX, normY}, {1.0f, 1.0f}, AGE::Texture2D::ErrorTexture());
AGE::Renderer2D::DrawQuad({normX, normY, -0.1f}, {1.0f, 1.0f}, AGE::Texture2D::ErrorTexture());
continue;
}
AGE::Renderer2D::DrawQuad({normX, normY}, {1.0f, 1.0f}, m_TileMap[s_MapTiles[y * mapWidth + x]]);
AGE::Renderer2D::DrawQuad({normX, normY, -0.1f}, {1.0f, 1.0f}, m_TileMap[s_MapTiles[y * mapWidth + x]]);
}
}

static float lastParticleSpawn{0.0f};

static ParticleProps props{glm::vec2{0.0f},
glm::vec2{0},
1.5f,
45.0f,
90.0f,
glm::vec4{2.5f, 2.0f, 1.0f, 1.0f},
glm::vec4{0.6f, 0.0f, 0.0f, 0.3f},
0.05f,
0.1f,
0.05f,
0.01,
1.0f,
0.0f};

lastParticleSpawn += ts;

if(AGE::Input::IsMousePressed(AGE::Mouse::ButtonLeft)) {
auto [x,y] = AGE::Input::MousePos();
auto windowWidth = AGE::Application::Instance()->Window()->Width();
auto windowHeight = AGE::Application::Instance()->Window()->Height();

auto bounds = m_CameraController.Bounds();
auto cameraPos = m_CameraController.GetCamera().Position();
x = ((float)x/(float)windowWidth) * bounds.Width() - bounds.Width() * 0.5f;
y = bounds.Height() * 0.5f - ((float)y/(float)windowHeight) * bounds.Height();

props.Position = {x,y};
for(int i{0}; i < 10; i++) {
m_Particles.Emit(props);
}
}

m_Particles.OnUpdate(ts);
m_Particles.Render();

AGE::Renderer2D::EndScene();
}

Expand Down
2 changes: 2 additions & 0 deletions Sandbox/src/Sandbox2DLayer.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ class Sandbox2DLayer : public AGE::Layer {
std::unordered_map<char, AGE::Ref<AGE::SubTexture2D>> m_TileMap;

friend class Sandbox2DUI;

ParticleSystem m_Particles;
};

class Sandbox2DUI : public AGE::ImGuiLayer {
Expand Down

0 comments on commit 5505c1a

Please sign in to comment.