0
0
mirror of https://github.com/Nimac0/SDL_Minigame synced 2026-01-12 23:33:41 +00:00

Compare commits

..

No commits in common. "a5c3ec36837b2aa58c2e1772de92629bd544ccbb" and "e493960fe0c1107662df6446f2f8a12cae27f681" have entirely different histories.

18 changed files with 49 additions and 130 deletions

View File

@ -9,6 +9,7 @@ public:
virtual void init() {} virtual void init() {}
virtual void update() {} virtual void update() {}
virtual void draw() {}
virtual ~Component() = default; virtual ~Component() = default;
}; };

View File

@ -42,6 +42,7 @@ public:
manager(mManager) { }; manager(mManager) { };
void update() const; void update() const;
void draw() const;
bool isActive() const { return this->active; } bool isActive() const { return this->active; }
void destroy() { void destroy() {

View File

@ -9,7 +9,6 @@
#include "Manager.h" #include "Manager.h"
#include "Vector2D.h" #include "Vector2D.h"
#include "Entity.h" #include "Entity.h"
#include "RenderManager.h"
typedef std::function<void()> gamefunction; typedef std::function<void()> gamefunction;
@ -43,8 +42,8 @@ public:
/* static */ TextureManager* textureManager; /* static */ TextureManager* textureManager;
/* static */ SoundManager* soundManager; /* static */ SoundManager* soundManager;
// moved globals
Manager manager; Manager manager;
RenderManager renderManager;
Map* map; // game specific, might not be needed for all types of games Map* map; // game specific, might not be needed for all types of games
std::vector<Entity*>& tiles; std::vector<Entity*>& tiles;

View File

@ -16,6 +16,7 @@ public:
Manager(GameInternal* game) : game(game) {}; Manager(GameInternal* game) : game(game) {};
void update(); void update();
void draw();
void refresh(); void refresh();
void addToGroup(Entity* mEntity, Group mGroup); void addToGroup(Entity* mEntity, Group mGroup);

View File

@ -1,29 +0,0 @@
#pragma once
#include <vector>
#include "RenderObject.h"
class RenderManager {
public:
RenderManager() {};
/*
* Remove an object from the list of objects to be rendered
* \param renderObject The object to be removed
* \sa RenderObject
*/
void remove(RenderObject* obj);
/*
* Add an object to be rendered
* \param renderObject The object to be rendered
* \sa RenderObject
*/
void add(RenderObject* obj);
void renderAll(); //!< Render all objects. If the list has been modified, sorts it based on z-index first
private:
std::vector<RenderObject*> renderObjects;
bool isSorted;
};

View File

@ -1,27 +0,0 @@
#pragma once
class RenderManager;
class RenderObject
{
public:
virtual void draw() = 0;
RenderObject(int zIndex, RenderManager& renderManager);
~RenderObject();
int getZIndex() { return this->zIndex; };
//! Comparitor to compare two ptr based on z-index
struct ZIndexComparator {
bool operator()(RenderObject const *lhs, RenderObject const *rhs ) const {
return lhs->zIndex < rhs->zIndex;
}
};
private:
int zIndex = 0;
protected:
RenderManager& renderManager;
};

View File

@ -8,11 +8,10 @@
#include "AnimationHandler.h" #include "AnimationHandler.h"
#include "Component.h" #include "Component.h"
#include "Direction.h" #include "Direction.h"
#include "RenderObject.h"
class TransformComponent; class TransformComponent;
class SpriteComponent : public Component, public RenderObject class SpriteComponent : public Component
{ {
public: public:
int animationIndex = 0; int animationIndex = 0;
@ -32,13 +31,13 @@ private:
bool flipped = false; bool flipped = false;
public: public:
SpriteComponent(const char* path, int zIndex); SpriteComponent() = default;
SpriteComponent(const char* path);
SpriteComponent( SpriteComponent(
const char* path, const char* path,
bool isAnimated, bool isAnimated,
std::map<std::string, std::unique_ptr<Animation>>* animationList, std::map<std::string, std::unique_ptr<Animation>>* animationList,
std::string defaultAnimation, std::string defaultAnimation);
int zIndex);
~SpriteComponent(); ~SpriteComponent();
void setTexture(const char* path); void setTexture(const char* path);

View File

@ -1,9 +0,0 @@
#include "GameInternal.h"
namespace vego {
extern GameInternal* game;
}
inline GameInternal& VEGO_Game() {
return *vego::game;
};

View File

@ -50,7 +50,7 @@ void AssetManager::createProjectile(Vector2D pos, Vector2D velocity, int scale,
auto& projectile(man->addEntity()); auto& projectile(man->addEntity());
projectile.addComponent<TransformComponent>(pos.x, pos.y, 32, 32, scale); //32x32 is standard size for objects projectile.addComponent<TransformComponent>(pos.x, pos.y, 32, 32, scale); //32x32 is standard size for objects
projectile.addComponent<SpriteComponent>(texturePath, 4); projectile.addComponent<SpriteComponent>(texturePath);
projectile.addComponent<ProjectileComponent>(range, speed, velocity, owner); projectile.addComponent<ProjectileComponent>(range, speed, velocity, owner);
projectile.addComponent<ColliderComponent>("projectile", 0.6f); projectile.addComponent<ColliderComponent>("projectile", 0.6f);
projectile.addGroup((size_t)Entity::GroupLabel::PROJECTILE); projectile.addGroup((size_t)Entity::GroupLabel::PROJECTILE);
@ -62,7 +62,7 @@ void AssetManager::createPowerup(Vector2D pos, std::function<void (Entity*)> pic
powerups.addComponent<TransformComponent>(pos.x, pos.y, 32, 32, 1); //32x32 is standard size for objects powerups.addComponent<TransformComponent>(pos.x, pos.y, 32, 32, 1); //32x32 is standard size for objects
try { try {
powerups.addComponent<SpriteComponent>(texturePath.c_str(), 3); powerups.addComponent<SpriteComponent>(texturePath.c_str());
} }
catch (std::runtime_error e) { catch (std::runtime_error e) {
std::cout << e.what() << std::endl; std::cout << e.what() << std::endl;

View File

@ -9,6 +9,11 @@ void Entity::update() const
for (auto const& c : components) c->update(); for (auto const& c : components) c->update();
} }
void Entity::draw() const
{
for (auto const& c : components) c->draw();
}
bool Entity::hasGroup(Group mGroup) bool Entity::hasGroup(Group mGroup)
{ {
return groupBitSet[mGroup]; return groupBitSet[mGroup];

View File

@ -4,7 +4,6 @@
#include "CollisionHandler.h" #include "CollisionHandler.h"
#include "AssetManager.h" #include "AssetManager.h"
#include "RenderManager.h"
#include "SoundManager.h" #include "SoundManager.h"
#include "TileComponent.h" #include "TileComponent.h"
#include "Direction.h" #include "Direction.h"
@ -19,7 +18,6 @@
GameInternal::GameInternal() : GameInternal::GameInternal() :
manager(this), manager(this),
renderManager(),
tiles(manager.getGroup((size_t)Entity::GroupLabel::MAPTILES)), tiles(manager.getGroup((size_t)Entity::GroupLabel::MAPTILES)),
players(manager.getGroup((size_t)Entity::GroupLabel::PLAYERS)), players(manager.getGroup((size_t)Entity::GroupLabel::PLAYERS)),
projectiles(manager.getGroup((size_t)Entity::GroupLabel::PROJECTILE)), projectiles(manager.getGroup((size_t)Entity::GroupLabel::PROJECTILE)),
@ -171,7 +169,21 @@ void GameInternal::update()
void GameInternal::render() void GameInternal::render()
{ {
SDL_RenderClear(renderer); SDL_RenderClear(renderer);
this->renderManager.renderAll(); for (auto& t : tiles)
t->draw();
for (auto& p : powerups)
p->draw();
for (auto& p : players)
p->draw();
for (auto& p : projectiles)
p->draw();
for (auto& h : hearts)
h->draw();
SDL_RenderPresent(renderer); SDL_RenderPresent(renderer);
} }

View File

@ -6,6 +6,11 @@
#include "Constants.h" #include "Constants.h"
#include "Entity.h" #include "Entity.h"
void Manager::draw()
{
for (auto& e : entities) e->draw();
}
void Manager::refresh() void Manager::refresh()
{ {
for (auto i(0u); i < MAX_GROUPS; i++) for (auto i(0u); i < MAX_GROUPS; i++)

View File

@ -1,24 +0,0 @@
#include "RenderManager.h"
#include "RenderObject.h"
#include <algorithm>
void RenderManager::renderAll()
{
if (!this->isSorted) {
std::ranges::sort(this->renderObjects, RenderObject::ZIndexComparator());
}
for (RenderObject* obj : this->renderObjects) {
obj->draw();
}
}
void RenderManager::add(RenderObject* renderObject) {
this->renderObjects.emplace_back(renderObject);
this->isSorted = false;
}
void RenderManager::remove(RenderObject* renderObject)
{
this->renderObjects.erase(std::remove(this->renderObjects.begin(), this->renderObjects.end(), renderObject), this->renderObjects.end());
this->isSorted = false;
}

View File

@ -1,10 +0,0 @@
#include "RenderObject.h"
#include "RenderManager.h"
RenderObject::RenderObject(int zIndex, RenderManager& renderManager) : zIndex(zIndex), renderManager(renderManager) {
renderManager.add(this);
}
RenderObject::~RenderObject() {
this->renderManager.remove(this);
}

View File

@ -6,16 +6,13 @@
#include "AnimationHandler.h" #include "AnimationHandler.h"
#include "Direction.h" #include "Direction.h"
#include "ProjectileComponent.h"
#include "RenderObject.h"
#include "TextureManager.h" #include "TextureManager.h"
#include "Entity.h" #include "Entity.h"
#include "TransformComponent.h" #include "TransformComponent.h"
#include "GameInternal.h" #include "GameInternal.h"
#include "Manager.h" #include "Manager.h"
#include "VEGO.h"
SpriteComponent::SpriteComponent(const char* path, int zIndex) : RenderObject(zIndex, VEGO_Game().renderManager) SpriteComponent::SpriteComponent(const char* path)
{ {
this->texturePath = path; this->texturePath = path;
} }
@ -24,8 +21,7 @@ SpriteComponent::SpriteComponent(
const char* path, const char* path,
bool isAnimated, bool isAnimated,
std::map<std::string, std::unique_ptr<Animation>>* animationMap, std::map<std::string, std::unique_ptr<Animation>>* animationMap,
std::string defaultAnimation, std::string defaultAnimation)
int zIndex) : RenderObject(zIndex, VEGO_Game().renderManager)
{ {
animated = isAnimated; animated = isAnimated;
@ -40,7 +36,7 @@ SpriteComponent::~SpriteComponent() {}
void SpriteComponent::setTexture(const char* path) void SpriteComponent::setTexture(const char* path)
{ {
this->texture = VEGO_Game().textureManager->loadTexture(path); this->texture = this->entity->getManager().getGame()->textureManager->loadTexture(path);
} }
void SpriteComponent::init() void SpriteComponent::init()
@ -72,7 +68,7 @@ void SpriteComponent::update()
void SpriteComponent::draw() void SpriteComponent::draw()
{ {
this->entity->getManager().getGame()->textureManager->draw(VEGO_Game().renderer, this->texture, this->srcRect, this->destRect, this->animated && this->flipped); this->entity->getManager().getGame()->textureManager->draw(this->entity->getManager().getGame()->renderer, this->texture, this->srcRect, this->destRect, this->animated && this->flipped);
} }
void SpriteComponent::playAnimation(std::string type) void SpriteComponent::playAnimation(std::string type)

View File

@ -31,7 +31,7 @@ void TileComponent::init()
this->entity->addComponent<TransformComponent>(this->tileRect.x, this->tileRect.y, this->tileRect.w, this->tileRect.h, 1); this->entity->addComponent<TransformComponent>(this->tileRect.x, this->tileRect.y, this->tileRect.w, this->tileRect.h, 1);
this->transform = &entity->getComponent<TransformComponent>(); this->transform = &entity->getComponent<TransformComponent>();
this->entity->addComponent<SpriteComponent>(this->path, 0); this->entity->addComponent<SpriteComponent>(this->path);
this->sprite = &entity->getComponent<SpriteComponent>(); this->sprite = &entity->getComponent<SpriteComponent>();
} }

View File

@ -72,7 +72,7 @@ void TransformComponent::modifySpeed(int8_t modifier)
void TransformComponent::setPositionAfterCollision(Vector2D& positionChange) void TransformComponent::setPositionAfterCollision(Vector2D& positionChange)
{ {
std::initializer_list<Entity::GroupLabel> colliders = { Entity::GroupLabel::MAPTILES, Entity::GroupLabel::COLLIDERS }; std::initializer_list colliders = { Entity::GroupLabel::MAPTILES, Entity::GroupLabel::COLLIDERS };
IntersectionBitSet intersections = IntersectionBitSet intersections =
(CollisionHandler::getIntersectionWithBounds(entity, Vector2D(positionChange.x, 0)) | (CollisionHandler::getIntersectionWithBounds(entity, Vector2D(positionChange.x, 0)) |
(this->entity->getManager() (this->entity->getManager()

View File

@ -1,12 +1,11 @@
#include <iostream> #include <iostream>
#include <ctime> #include <ctime>
#include "VEGO.h"
#include "Entity.h" #include "Entity.h"
#include "GameInternal.h" #include "GameInternal.h"
#include "Constants.h" #include "Constants.h"
GameInternal* vego::game = nullptr; GameInternal* game = nullptr;
int main(int argc, char* argv[]) int main(int argc, char* argv[])
{ {
@ -18,15 +17,15 @@ int main(int argc, char* argv[])
Uint32 frameStart; Uint32 frameStart;
int frameTime; int frameTime;
vego::game = new GameInternal(); game = new GameInternal();
vego::game->init("No_Name_Chicken_Game", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, SCREEN_SIZE_WIDTH, SCREEN_SIZE_HEIGHT, false); game->init("No_Name_Chicken_Game", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, SCREEN_SIZE_WIDTH, SCREEN_SIZE_HEIGHT, false);
while (vego::game->isRunning()) { while (game->isRunning()) {
frameStart = SDL_GetTicks(); frameStart = SDL_GetTicks();
vego::game->handleEvents(); game->handleEvents();
vego::game->update(); game->update();
vego::game->render(); game->render();
frameTime = SDL_GetTicks() - frameStart; frameTime = SDL_GetTicks() - frameStart;
@ -35,7 +34,7 @@ int main(int argc, char* argv[])
} }
} }
vego::game->clean(); game->clean();
return 0; return 0;
} }