Temp commit. Textures not working anymor :(

This commit is contained in:
Bram Verhulst
2024-03-27 11:40:44 +01:00
parent 61d1d17795
commit d441222173
43 changed files with 119 additions and 93 deletions

View File

@@ -216,7 +216,7 @@ void Texture::CreateFromSurface( SDL_Surface* pSurface )
glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST );
}
void Texture::Draw( const Point2f& dstBottomLeft, const Rectf& srcRect ) const
void Texture::Draw( const Point2f& dstBottomLeft, const Rectf& srcRect, bool flip) const
{
const float epsilon{ 0.001f };
if ( !m_CreationOk )
@@ -242,7 +242,7 @@ void Texture::Draw( const Point2f& dstBottomLeft, const Rectf& srcRect ) const
}
}
void Texture::Draw( const Rectf& dstRect, const Rectf& srcRect ) const
void Texture::Draw( const Rectf& dstRect, const Rectf& srcRect, bool flip ) const
{
const float epsilon{ 0.001f };
if ( !m_CreationOk )
@@ -296,7 +296,9 @@ void Texture::Draw( const Rectf& dstRect, const Rectf& srcRect ) const
{
vertexRight = vertexLeft + dstRect.width;
vertexTop = vertexBottom + dstRect.height;
}
if(flip) {
std::swap(vertexLeft, vertexRight);
}
// Tell opengl which texture we will use

View File

@@ -13,8 +13,8 @@ public:
Texture& operator=( Texture&& other ) noexcept;
~Texture();
void Draw(const Point2f& dstBottomLeft = {}, const Rectf& srcRect = {}) const;
void Draw( const Rectf& dstRect, const Rectf& srcRect = {} ) const;
void Draw(const Point2f& dstBottomLeft = {}, const Rectf& srcRect = {}, bool flip = false) const;
void Draw( const Rectf& dstRect, const Rectf& srcRect = {}, bool flip = false) const;
float GetWidth() const;
float GetHeight() const;

View File

@@ -40,15 +40,15 @@ void Game::Update(float elapsedSec) {
m_MouseOffset = m_Camera.GetPosition();
}
m_WorldLevel.Update(elapsedSec);
}
void Game::Draw() const {
utils::ClearBackground(Color4f(0.0f, 0.0f, 0.3f, 1.0f));
m_WorldLevel.Draw();
TextureManager::GetInstance()->GetTexture("dingus.png")->Draw(Point2f{0,0}, {}, true);
TextureManager::GetInstance()->GetTexture("dingus.png")->Draw(Point2f{0,300}, {}, false);
}
void Game::ProcessKeyDownEvent(const SDL_KeyboardEvent& e) {

View File

@@ -58,7 +58,7 @@ void Player::Update(float elapsedTime, WorldLevel& level) {
m_Vel.y = -100;
if(m_Grounded) {
if(m_ContactMap[Collision::CollisionDirection::Bottom] != nullptr) {
m_ContactMap[Collision::CollisionDirection::Bottom]->SetTileType(GroundTileTypes::Air);
m_ContactMap[Collision::CollisionDirection::Bottom]->SetTileType(Tiles::AIR);
WorldTile* tile = m_ContactMap[Collision::CollisionDirection::Bottom];
//center of tile
Point2f tileCenter = tile->GetCollisionRect().getCollisionRect().pos + tile->GetCollisionRect().getCollisionRect().size / 2;
@@ -79,7 +79,7 @@ void Player::Update(float elapsedTime, WorldLevel& level) {
if(m_Grounded && !m_DidJustDigRight) {
//Check if the player doesnt come from digging a tile
if(m_ContactMap[Collision::CollisionDirection::Right] != nullptr) {
m_ContactMap[Collision::CollisionDirection::Right]->SetTileType(GroundTileTypes::Air);
m_ContactMap[Collision::CollisionDirection::Right]->SetTileType(Tiles::AIR);
WorldTile* tile = m_ContactMap[Collision::CollisionDirection::Right];
//center of tile
Point2f tileCenter = tile->GetCollisionRect().getCollisionRect().pos + tile->GetCollisionRect().getCollisionRect().size / 2;
@@ -112,7 +112,7 @@ void Player::Update(float elapsedTime, WorldLevel& level) {
for (int x { 0 }; x < WORLD_WIDTH; ++x) {
for(int y { 0 }; y < WORLD_HEIGHT; ++y) {
if(gridManager.GetTileAtIndex(x,y)->GetTileType() == GroundTileTypes::Dirt) {
if(gridManager.GetTileAtIndex(x,y)->GetTileType() == Tiles::DIRT) {
gridManager.GetTileAtIndex(x,y)->m_Hightlight = false;
if(Collision::DynamicRectVsRect(this->GetCollisionRect(), elapsedTime, gridManager.GetTileAtIndex(x, y)->GetCollisionRect().getCollisionRect(), intersectionPoint, normal, t)) {
contactTimes.push_back(std::pair<int, float>{x + y * WORLD_WIDTH, t});
@@ -125,56 +125,40 @@ void Player::Update(float elapsedTime, WorldLevel& level) {
return a.second < b.second;
});
//check if grounded
// if(contactTimes.size() == 0) {
// m_Grounded = false;
// }
for (std::pair<int, float> contact_time : contactTimes) {
int x = contact_time.first % WORLD_WIDTH;
int y = contact_time.first / WORLD_WIDTH;
WorldTile* world_tile = gridManager.GetTileAtIndex(x,y);
bool isDiggingCandidate = true;
Collision::CollisionRect tileRect = world_tile->GetCollisionRect().getCollisionRect();
//check if the tile is above the player
if(tileRect.pos.y > m_Position.y + m_Size.y) {
isDiggingCandidate = false;
}
//check if tile is left of player
if(tileRect.pos.x > m_Position.x + m_Size.x) {
if(tileRect.pos.y + tileRect.size.y > m_Position.y) {
world_tile->m_Hightlight = true;
m_ContactMap[Collision::CollisionDirection::Right] = world_tile;
isDiggingCandidate = false;
}
}
//check if tile is right of player
if(tileRect.pos.x + tileRect.size.x < m_Position.x) {
isDiggingCandidate = false;
}
//check if the collision happend under the player
if(tileRect.pos.y < m_Position.y - m_Size.y) {
//Tile is under player
const Point2f WorldTilePos = world_tile->GetCollisionRect().getCollisionRect().pos;
const Point2f WorldTileSize = world_tile->GetCollisionRect().getCollisionRect().size;
//check if the center of the player is between the centers of the tiles left and right
if(m_Position.x + m_Size.x / 2 > tileRect.pos.x && m_Position.x + m_Size.x / 2 < tileRect.pos.x + tileRect.size.x) {
if(isDiggingCandidate) {
//check if the distance to the tile is less than the size of a tile
if(m_Position.y + tileRect.size.y > tileRect.pos.y) {
m_ContactMap[Collision::CollisionDirection::Bottom] = world_tile;
m_Grounded = true;
}
//Dig the tile
if(WorldTilePos.y + WorldTileSize.y > m_Position.y) {
if(WorldTilePos.x + WorldTileSize.x > m_Position.x) {
if(WorldTilePos.y + WorldTileSize.y / 2 > m_Position.y && m_Position.y + m_Size.y / 2 > WorldTilePos.y) {
//Right of player
m_ContactMap[Collision::CollisionDirection::Right] = world_tile;
}
}
}
//m_ContactMap[Collision::CollisionDirection::Bottom] = world_tile;
//m_Grounded = true;
if(WorldTilePos.y + WorldTileSize.y > m_Position.y) {
if(WorldTilePos.x < m_Position.x + m_Size.x) {
if(WorldTilePos.y + WorldTileSize.y / 2 > m_Position.y && m_Position.y + m_Size.y / 2 > WorldTilePos.y) {
//Left of player
m_ContactMap[Collision::CollisionDirection::Left] = world_tile;
}
}
}
//Below the player
if(WorldTilePos.y + WorldTileSize.y <= m_Position.y) {
if(WorldTilePos.x + WorldTileSize.x / 2 > m_Position.x && m_Position.x + m_Size.x / 2 > WorldTilePos.x) {
m_ContactMap[Collision::CollisionDirection::Bottom] = world_tile;
m_Grounded = true;
world_tile->m_Hightlight = true;
}
}
Collision::CollisionRect rect = world_tile->GetCollisionRect().getCollisionRect(); //TODO: fix this mess
Collision::ResolvePlayerVsRect(*this, elapsedTime, &rect);

View File

@@ -13,25 +13,23 @@
WorldLevel::WorldLevel(Camera* camera, Rectf viewport):
Level(camera),
m_gridManager(WorldGridManager()),
m_player(Player { Point2f { 0, 100 } }),
m_mousePos { 0, 0 },
m_viewport(viewport),
m_gridManager(WorldGridManager())
m_viewport(viewport)
{
// The grid is 34 x 50 big, the top center is 0,0 in world coords
for (size_t x { 0 }; x < WORLD_WIDTH; ++x) {
for (size_t y { 0 }; y < WORLD_HEIGHT; ++y) {
int actualX = x - WORLD_WIDTH / 2;
Point2f pos = Point2f{ float(actualX * TILE_WIDTH), -float(y * TILE_HEIGHT) - TILE_HEIGHT};
GroundTileTypes type = rand() % 2 == 0 ? GroundTileTypes::Dirt : GroundTileTypes::Dirt;
m_gridManager.SetTileAtIndex(x,y, new WorldTile{ pos, type, TextureManager::GetInstance() });
GroundTileType type = rand() % 2 == 0 ? Tiles::DIRT : Tiles::DIRT;
m_gridManager.SetTileAtIndex(x,y, new WorldTile{ pos, Tiles::DIRT, TextureManager::GetInstance() });
}
}
//
// for (size_t x { 0 }; x < WORLD_WIDTH; ++x) {
// m_gridManager.GetTileAtIndex(x, 0)->SetTileType(GroundTileTypes::Dirt);
// }
for (size_t x { 0 }; x < WORLD_WIDTH; ++x) {
m_gridManager.GetTileAtIndex(x, 0)->SetTileType(Tiles::AIR);
}
}
WorldLevel::~WorldLevel() {
//delete m_pTextTexture;
@@ -56,7 +54,7 @@ void WorldLevel::Update(float elapsedSec) {
}
if(selectedTile != nullptr) {
if(utils::isMouseDown(SDL_BUTTON_LEFT)) {
selectedTile->SetTileType(GroundTileTypes::Air);
selectedTile->SetTileType(Tiles::AIR);
}
}
@@ -64,7 +62,7 @@ void WorldLevel::Update(float elapsedSec) {
WorldTile* t = m_gridManager.GetTileAtWorldPos(m_mousePos);
if(t != nullptr) {
t->SetTileType(GroundTileTypes::Air);
t->SetTileType(Tiles::AIR);
}
//Point2f playerPos = m_player.GetPosition();
@@ -82,13 +80,8 @@ void WorldLevel::Draw() const {
utils::DrawRect(rect.pos, rect.size.x, rect.size.y);
}
Point2f RayPoint = Point2f{m_pCamera->Viewport.width, m_pCamera->Viewport.height};
Point2f RayDir = Point2f{m_mousePos.x - RayPoint.x, m_mousePos.y - RayPoint.y};
utils::SetColor(Colors::WHITE);
utils::DrawLine(RayPoint, m_mousePos);
utils::FillEllipse(m_mousePos, 20, 20);
utils::FillEllipse(m_mousePos, 2, 2);
for (size_t x { 0 }; x < WORLD_WIDTH; ++x) {
for (size_t y { 0 }; y < WORLD_HEIGHT; ++y) {
@@ -99,7 +92,7 @@ void WorldLevel::Draw() const {
//loop over worldtiles
for (int x { 0 }; x < WORLD_WIDTH; ++x) {
for (int y { 0 }; y < WORLD_HEIGHT; ++y) {
if(m_gridManager.GetTileAtIndex(x,y)->GetTileType() == GroundTileTypes::Dirt) {
if(m_gridManager.GetTileAtIndex(x,y)->GetTileType() == Tiles::DIRT) {
Collision::CollisionRect rect = m_gridManager.GetTileAtIndex(x,y)->GetCollisionRect().getCollisionRect();
utils::SetColor(Colors::GREEN);
utils::DrawRect(rect.pos, rect.size.x, rect.size.y);
@@ -157,15 +150,7 @@ void WorldLevel::ProcessImGui() {
ImGui::Begin("Player Info", &m_ShowPlayerInfo, ImGuiWindowFlags_AlwaysAutoResize);
ImGui::Text("Player Position: (%f, %f)", m_player.GetPosition().x, m_player.GetPosition().y);
ImGui::Text("Player Velocity: (%f, %f)", m_player.GetVelocity().x, m_player.GetVelocity().y);
//PLAYER COLLISIONS
ImGui::Text("Player Collisions:");
// for (std::pair<Collision::CollisionDirection, Collision::CollisionRect*> contact : m_player.GetContactMap()) {
// ImGui::Text("Direction: %d", contact.first);
// ImGui::Text("Position: (%f, %f)", contact.second->pos.x, contact.second->pos.y);
// ImGui::Text("Size: (%f, %f)", contact.second->size.x, contact.second->size.y);
// }
ImGui::End();
m_player.ProcessImGui();
}
}

View File

@@ -5,23 +5,18 @@
#include "TextureManager.h"
#include "utils.h"
WorldTile::WorldTile() {
}
WorldTile::WorldTile(const Point2f& position, GroundTileTypes groundTileType, TextureManager* pTextureManager) : m_Position { position }, m_GroundTileType { groundTileType } {
const std::string dirtPath = + "tiles/dirt/dirt" + std::to_string(utils::randRange(1, 5)) + ".png";
WorldTile::WorldTile(const Point2f& position, GroundTileType groundTileType, TextureManager* pTextureManager) : m_Position { position }, m_GroundTileType { groundTileType } {
// const std::string dirtPath = + "tiles/dirt/dirt" + std::to_string(utils::randRange(1, 5)) + ".png";
// m_pTexture = new Texture(dirtPath);
m_pTexture = pTextureManager->GetTexture(dirtPath);
m_pTexture = pTextureManager->GetTexture(groundTileType.getPath());
}
WorldTile::~WorldTile() {
delete m_pTexture;
}
void WorldTile::Draw() const {
if (m_GroundTileType != GroundTileTypes::Air) {
if (m_GroundTileType == Tiles::DIRT) {
m_pTexture->Draw(m_Position);
//utils::SetColor(Color4f{ 0.5f, 0.5f, 0.5f, 1.0f});
//utils::FillRect(m_Position.x, m_Position.y, 50, 50);
if(m_Hightlight) {
utils::SetColor(Colors::GREEN);
utils::FillRect(m_Position, 50, 50);

View File

@@ -3,16 +3,76 @@
#include "Texture.h"
#include "TextureManager.h"
enum class GroundTileTypes
{
Air,
Dirt,
Stone,
Iron
};
class GroundTileType {
public:
GroundTileType(const std::string& filePath, GroundTileTypes type): m_filePath(filePath), m_type(type) {}
virtual ~GroundTileType() = default;
bool operator== ( const GroundTileType& rhs ) const {
return m_type == rhs.m_type;
}
bool operator!= ( const GroundTileType& rhs) const {
return m_type != rhs.m_type;
}
bool operator== (const GroundTileType* rhs) const {
return rhs->m_type == m_type;
}
bool operator!= (const GroundTileType* rhs) const {
return rhs->m_type != m_type;
}
virtual std::string getPath() {
return m_filePath;
}
protected:
std::string m_filePath;
private:
GroundTileTypes m_type;
};
class RandomGroundTile: public GroundTileType {
public:
RandomGroundTile(const std::string& filePath, GroundTileTypes type, int maxRandom): GroundTileType(filePath, type), m_variant(utils::randRange(1, maxRandom)) {
}
~RandomGroundTile() override = default;
std::string getPath() override {
std::string toReplace{ "[0]" };
std::string replacement = std::to_string(m_variant);
size_t found = m_filePath.find(toReplace);
std::string newFilePath{ m_filePath };
if(found != std::string::npos) {
newFilePath.replace(found, 3, replacement);
}
return newFilePath;
}
private:
int m_variant;
};
namespace Tiles
{
static GroundTileType AIR = GroundTileType("", GroundTileTypes::Air);
static GroundTileType DIRT = RandomGroundTile("tiles/dirt/dirt[0].png", GroundTileTypes::Dirt, 6);
static GroundTileType IRON = GroundTileType("tiles/ores/Ore_Ironium.png", GroundTileTypes::Iron);
}
class WorldTile {
public:
WorldTile();
WorldTile(const Point2f& position, GroundTileTypes groundTileType, TextureManager* pTextureManager);
WorldTile() = default;
WorldTile(const Point2f& position, GroundTileType groundTileType, TextureManager* pTextureManager);
~WorldTile();
void Draw() const;
@@ -22,8 +82,8 @@ public:
Point2f GetSize() const { return Point2f{ 50, 50 }; }
GroundTileTypes GetTileType() const { return m_GroundTileType; }
void SetTileType(GroundTileTypes type) { m_GroundTileType = type; }
GroundTileType GetTileType() const { return m_GroundTileType; }
void SetTileType(GroundTileType type) { m_GroundTileType = type; }
Collision::TileCollisionRect GetCollisionRect();
@@ -32,7 +92,7 @@ public:
private:
Point2f m_Position;
GroundTileTypes m_GroundTileType;
GroundTileType m_GroundTileType;
Texture* m_pTexture;

View File

@@ -12,7 +12,7 @@ int SDL_main(int argv, char** args)
StartHeapControl();
Game* pGame { new Game { Window { "Project name - Name, first name - 1DAEXX", 846.f, 500.f } } };
Game* pGame { new Game { Window { "Motherload - Verhulst, Bram - 1DAEGD16E", 846.f, 500.f } } };
pGame->Run();
delete pGame;

Binary file not shown.

After

Width:  |  Height:  |  Size: 6.7 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 6.4 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 6.0 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 6.4 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 5.8 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 6.4 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 6.3 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 6.5 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 6.5 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 6.0 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 6.5 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 6.3 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 6.4 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 6.6 KiB

BIN
Resources/dingus.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 2.6 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 6.7 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 6.4 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 6.0 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 6.4 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 5.8 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 6.4 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 6.3 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 6.5 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 6.5 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 6.0 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 4.6 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 4.5 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 4.8 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 6.5 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 6.3 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 6.4 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 6.6 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 3.9 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 3.9 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 3.9 KiB