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.
a5c3ec3683
...
e493960fe0
@ -9,6 +9,7 @@ public:
|
||||
|
||||
virtual void init() {}
|
||||
virtual void update() {}
|
||||
virtual void draw() {}
|
||||
|
||||
virtual ~Component() = default;
|
||||
};
|
||||
@ -42,6 +42,7 @@ public:
|
||||
manager(mManager) { };
|
||||
|
||||
void update() const;
|
||||
void draw() const;
|
||||
|
||||
bool isActive() const { return this->active; }
|
||||
void destroy() {
|
||||
|
||||
@ -9,7 +9,6 @@
|
||||
#include "Manager.h"
|
||||
#include "Vector2D.h"
|
||||
#include "Entity.h"
|
||||
#include "RenderManager.h"
|
||||
|
||||
typedef std::function<void()> gamefunction;
|
||||
|
||||
@ -43,8 +42,8 @@ public:
|
||||
/* static */ TextureManager* textureManager;
|
||||
/* static */ SoundManager* soundManager;
|
||||
|
||||
// moved globals
|
||||
Manager manager;
|
||||
RenderManager renderManager;
|
||||
Map* map; // game specific, might not be needed for all types of games
|
||||
|
||||
std::vector<Entity*>& tiles;
|
||||
|
||||
@ -16,6 +16,7 @@ public:
|
||||
Manager(GameInternal* game) : game(game) {};
|
||||
|
||||
void update();
|
||||
void draw();
|
||||
void refresh();
|
||||
|
||||
void addToGroup(Entity* mEntity, Group mGroup);
|
||||
|
||||
@ -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;
|
||||
};
|
||||
@ -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;
|
||||
};
|
||||
@ -8,11 +8,10 @@
|
||||
#include "AnimationHandler.h"
|
||||
#include "Component.h"
|
||||
#include "Direction.h"
|
||||
#include "RenderObject.h"
|
||||
|
||||
class TransformComponent;
|
||||
|
||||
class SpriteComponent : public Component, public RenderObject
|
||||
class SpriteComponent : public Component
|
||||
{
|
||||
public:
|
||||
int animationIndex = 0;
|
||||
@ -32,13 +31,13 @@ private:
|
||||
bool flipped = false;
|
||||
|
||||
public:
|
||||
SpriteComponent(const char* path, int zIndex);
|
||||
SpriteComponent() = default;
|
||||
SpriteComponent(const char* path);
|
||||
SpriteComponent(
|
||||
const char* path,
|
||||
bool isAnimated,
|
||||
std::map<std::string, std::unique_ptr<Animation>>* animationList,
|
||||
std::string defaultAnimation,
|
||||
int zIndex);
|
||||
std::string defaultAnimation);
|
||||
~SpriteComponent();
|
||||
|
||||
void setTexture(const char* path);
|
||||
|
||||
@ -1,9 +0,0 @@
|
||||
#include "GameInternal.h"
|
||||
|
||||
namespace vego {
|
||||
extern GameInternal* game;
|
||||
}
|
||||
|
||||
inline GameInternal& VEGO_Game() {
|
||||
return *vego::game;
|
||||
};
|
||||
@ -50,7 +50,7 @@ void AssetManager::createProjectile(Vector2D pos, Vector2D velocity, int scale,
|
||||
|
||||
auto& projectile(man->addEntity());
|
||||
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<ColliderComponent>("projectile", 0.6f);
|
||||
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
|
||||
|
||||
try {
|
||||
powerups.addComponent<SpriteComponent>(texturePath.c_str(), 3);
|
||||
powerups.addComponent<SpriteComponent>(texturePath.c_str());
|
||||
}
|
||||
catch (std::runtime_error e) {
|
||||
std::cout << e.what() << std::endl;
|
||||
|
||||
@ -9,6 +9,11 @@ void Entity::update() const
|
||||
for (auto const& c : components) c->update();
|
||||
}
|
||||
|
||||
void Entity::draw() const
|
||||
{
|
||||
for (auto const& c : components) c->draw();
|
||||
}
|
||||
|
||||
bool Entity::hasGroup(Group mGroup)
|
||||
{
|
||||
return groupBitSet[mGroup];
|
||||
|
||||
@ -4,7 +4,6 @@
|
||||
|
||||
#include "CollisionHandler.h"
|
||||
#include "AssetManager.h"
|
||||
#include "RenderManager.h"
|
||||
#include "SoundManager.h"
|
||||
#include "TileComponent.h"
|
||||
#include "Direction.h"
|
||||
@ -19,7 +18,6 @@
|
||||
|
||||
GameInternal::GameInternal() :
|
||||
manager(this),
|
||||
renderManager(),
|
||||
tiles(manager.getGroup((size_t)Entity::GroupLabel::MAPTILES)),
|
||||
players(manager.getGroup((size_t)Entity::GroupLabel::PLAYERS)),
|
||||
projectiles(manager.getGroup((size_t)Entity::GroupLabel::PROJECTILE)),
|
||||
@ -171,7 +169,21 @@ void GameInternal::update()
|
||||
void GameInternal::render()
|
||||
{
|
||||
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);
|
||||
}
|
||||
|
||||
|
||||
@ -6,6 +6,11 @@
|
||||
#include "Constants.h"
|
||||
#include "Entity.h"
|
||||
|
||||
void Manager::draw()
|
||||
{
|
||||
for (auto& e : entities) e->draw();
|
||||
}
|
||||
|
||||
void Manager::refresh()
|
||||
{
|
||||
for (auto i(0u); i < MAX_GROUPS; i++)
|
||||
|
||||
@ -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;
|
||||
}
|
||||
@ -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);
|
||||
}
|
||||
@ -6,16 +6,13 @@
|
||||
|
||||
#include "AnimationHandler.h"
|
||||
#include "Direction.h"
|
||||
#include "ProjectileComponent.h"
|
||||
#include "RenderObject.h"
|
||||
#include "TextureManager.h"
|
||||
#include "Entity.h"
|
||||
#include "TransformComponent.h"
|
||||
#include "GameInternal.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;
|
||||
}
|
||||
@ -24,8 +21,7 @@ SpriteComponent::SpriteComponent(
|
||||
const char* path,
|
||||
bool isAnimated,
|
||||
std::map<std::string, std::unique_ptr<Animation>>* animationMap,
|
||||
std::string defaultAnimation,
|
||||
int zIndex) : RenderObject(zIndex, VEGO_Game().renderManager)
|
||||
std::string defaultAnimation)
|
||||
{
|
||||
animated = isAnimated;
|
||||
|
||||
@ -40,7 +36,7 @@ SpriteComponent::~SpriteComponent() {}
|
||||
|
||||
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()
|
||||
@ -72,7 +68,7 @@ void SpriteComponent::update()
|
||||
|
||||
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)
|
||||
|
||||
@ -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->transform = &entity->getComponent<TransformComponent>();
|
||||
|
||||
this->entity->addComponent<SpriteComponent>(this->path, 0);
|
||||
this->entity->addComponent<SpriteComponent>(this->path);
|
||||
this->sprite = &entity->getComponent<SpriteComponent>();
|
||||
}
|
||||
|
||||
|
||||
@ -72,7 +72,7 @@ void TransformComponent::modifySpeed(int8_t modifier)
|
||||
|
||||
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 =
|
||||
(CollisionHandler::getIntersectionWithBounds(entity, Vector2D(positionChange.x, 0)) |
|
||||
(this->entity->getManager()
|
||||
|
||||
17
src/main.cpp
17
src/main.cpp
@ -1,12 +1,11 @@
|
||||
#include <iostream>
|
||||
#include <ctime>
|
||||
|
||||
#include "VEGO.h"
|
||||
#include "Entity.h"
|
||||
#include "GameInternal.h"
|
||||
#include "Constants.h"
|
||||
|
||||
GameInternal* vego::game = nullptr;
|
||||
GameInternal* game = nullptr;
|
||||
|
||||
int main(int argc, char* argv[])
|
||||
{
|
||||
@ -18,15 +17,15 @@ int main(int argc, char* argv[])
|
||||
Uint32 frameStart;
|
||||
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);
|
||||
while (vego::game->isRunning()) {
|
||||
game->init("No_Name_Chicken_Game", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, SCREEN_SIZE_WIDTH, SCREEN_SIZE_HEIGHT, false);
|
||||
while (game->isRunning()) {
|
||||
frameStart = SDL_GetTicks();
|
||||
|
||||
vego::game->handleEvents();
|
||||
vego::game->update();
|
||||
vego::game->render();
|
||||
game->handleEvents();
|
||||
game->update();
|
||||
game->render();
|
||||
|
||||
frameTime = SDL_GetTicks() - frameStart;
|
||||
|
||||
@ -35,7 +34,7 @@ int main(int argc, char* argv[])
|
||||
}
|
||||
}
|
||||
|
||||
vego::game->clean();
|
||||
game->clean();
|
||||
|
||||
return 0;
|
||||
}
|
||||
Loading…
x
Reference in New Issue
Block a user