mirror of
https://github.com/Nimac0/SDL_Minigame
synced 2026-01-12 13:43:41 +00:00
Compare commits
6 Commits
ff27a0e55c
...
7c50c8d1fb
| Author | SHA1 | Date | |
|---|---|---|---|
| 7c50c8d1fb | |||
| 6a0b5197f9 | |||
| 2483b75983 | |||
| b497991975 | |||
| 25414524a0 | |||
| 625ac98a57 |
8
.editorconfig
Normal file
8
.editorconfig
Normal file
@ -0,0 +1,8 @@
|
||||
# allow game dev to overwrite settings
|
||||
root=false
|
||||
|
||||
[*]
|
||||
end_of_line = lf
|
||||
indent_style = space
|
||||
indent_size = 4
|
||||
trim_trailing_whitespace = true
|
||||
4
.gitmodules
vendored
4
.gitmodules
vendored
@ -1,19 +1,15 @@
|
||||
[submodule "SDL"]
|
||||
path = extern/SDL
|
||||
url = https://github.com/libsdl-org/SDL.git
|
||||
branch = release-2.28.x
|
||||
[submodule "SDL_image"]
|
||||
path = extern/SDL_image
|
||||
url = https://github.com/libsdl-org/SDL_image.git
|
||||
branch = release-2.8.x
|
||||
[submodule "extern/SDL_mixer"]
|
||||
path = extern/SDL_mixer
|
||||
url = https://github.com/libsdl-org/SDL_mixer.git
|
||||
branch = release-2.8.x
|
||||
[submodule "extern/SDL_ttf"]
|
||||
path = extern/SDL_ttf
|
||||
url = https://github.com/libsdl-org/SDL_ttf.git
|
||||
branch = release-2.22.x
|
||||
[submodule "extern/tmxlite"]
|
||||
path = extern/tmxlite
|
||||
url = https://github.com/fallahn/tmxlite.git
|
||||
|
||||
@ -32,11 +32,10 @@ add_library(${PROJECT_NAME} ${SOURCES})
|
||||
target_include_directories(${PROJECT_NAME} PUBLIC ${ENGINE_INCLUDE_DIR})
|
||||
|
||||
target_link_libraries(${PROJECT_NAME} PUBLIC # should be private when all SDL functionality has a wrapper
|
||||
SDL2::SDL2main
|
||||
SDL2::SDL2-static
|
||||
SDL2_image::SDL2_image-static
|
||||
SDL2_mixer::SDL2_mixer-static
|
||||
SDL2_ttf::SDL2_ttf-static
|
||||
SDL3::SDL3-static
|
||||
SDL3_image::SDL3_image-static
|
||||
SDL3_mixer::SDL3_mixer-static
|
||||
SDL3_ttf::SDL3_ttf-static
|
||||
tmxlite
|
||||
)
|
||||
|
||||
|
||||
@ -2,12 +2,12 @@
|
||||
"folders":
|
||||
[
|
||||
{
|
||||
"path": "."
|
||||
"path": ".",
|
||||
}
|
||||
],
|
||||
],
|
||||
"settings":
|
||||
{
|
||||
"tab_size": 4
|
||||
"tab_size": 4,
|
||||
},
|
||||
"build_systems": [
|
||||
{
|
||||
@ -23,7 +23,7 @@
|
||||
"name": "Release",
|
||||
"shell_cmd": "cmake -DCMAKE_BUILD_TYPE=Release build && cmake --build build",
|
||||
},
|
||||
]
|
||||
],
|
||||
},
|
||||
{
|
||||
"name": "Generate CMake",
|
||||
@ -43,7 +43,10 @@
|
||||
"name": "Generate documentation",
|
||||
"shell_cmd": "docker run --rm -v \"$project_path:/source\" -v \"$project_path/docs:/output\" -v \"$project_path/docs/Doxyfile:/Doxyfile\" vego_engine-docker",
|
||||
},
|
||||
]
|
||||
],
|
||||
}
|
||||
]
|
||||
],
|
||||
"debugger_configurations":
|
||||
[
|
||||
],
|
||||
}
|
||||
|
||||
2
extern/SDL
vendored
2
extern/SDL
vendored
@ -1 +1 @@
|
||||
Subproject commit 05eb08053d48fea9052ad02b3d619244aeb868d3
|
||||
Subproject commit e027b85cc457556071cbb2f3f1bcf8803c1bc001
|
||||
2
extern/SDL_image
vendored
2
extern/SDL_image
vendored
@ -1 +1 @@
|
||||
Subproject commit abcf63aa71b4e3ac32120fa9870a6500ddcdcc89
|
||||
Subproject commit b1c8ec7d75e3d8398940c9e04a8b82886ae6163d
|
||||
2
extern/SDL_mixer
vendored
2
extern/SDL_mixer
vendored
@ -1 +1 @@
|
||||
Subproject commit 5bcd40ad962dc72a3c051084ce128d78f7656566
|
||||
Subproject commit 5e2a70519294bc6ec44f1870b019ecd4760fde7d
|
||||
2
extern/SDL_ttf
vendored
2
extern/SDL_ttf
vendored
@ -1 +1 @@
|
||||
Subproject commit 4a318f8dfaa1bb6f10e0c5e54052e25d3c7f3440
|
||||
Subproject commit 4a8bda9197cc4d6fafd188bc9df6c7e8749a43a2
|
||||
@ -1,6 +1,6 @@
|
||||
#pragma once
|
||||
#include <SDL_render.h>
|
||||
#include <SDL_mixer.h>
|
||||
#include <SDL3/SDL_render.h>
|
||||
#include <SDL3_mixer/SDL_mixer.h>
|
||||
#include <map>
|
||||
#include <string>
|
||||
#include <functional>
|
||||
@ -24,7 +24,7 @@ public:
|
||||
AssetManager(Manager* manager);
|
||||
~AssetManager();
|
||||
|
||||
void createProjectile(Vector2D pos, Vector2D velocity, int scale, int range, int speed, Textures textureEnum, Entity* owner);
|
||||
void createProjectile(Vector2D pos, Vector2D velocity, int scale, int range, float speed, Textures textureEnum, Entity* owner);
|
||||
void createPowerup(Vector2D pos, std::function<void (Entity*)> pickupFunc, Textures texture);
|
||||
|
||||
Vector2D calculateSpawnPosition();
|
||||
|
||||
@ -1,6 +1,6 @@
|
||||
#pragma once
|
||||
|
||||
#include <SDL.h>
|
||||
#include <SDL3/SDL.h>
|
||||
|
||||
#include "Component.h"
|
||||
#include "Vector2D.h"
|
||||
@ -22,7 +22,7 @@ public:
|
||||
ColliderComponent(const char* tag, float hitboxScale);
|
||||
|
||||
void init() override;
|
||||
void update() override;
|
||||
void update(uint_fast16_t diffTime) override;
|
||||
void removeCollision();
|
||||
|
||||
void handleCollision(Vector2D& characterPos, SDL_Rect& characterCollider, SDL_Rect& componentCollider);
|
||||
|
||||
@ -7,7 +7,7 @@
|
||||
#include "ColliderComponent.h"
|
||||
#include "Constants.h"
|
||||
#include "Entity.h"
|
||||
#include "SDL_rect.h"
|
||||
#include <SDL3/SDL_rect.h>
|
||||
#include "SpriteComponent.h"
|
||||
#include "Vector2D.h"
|
||||
#include "Manager.h"
|
||||
|
||||
@ -1,5 +1,7 @@
|
||||
#pragma once
|
||||
|
||||
#include <cstdint>
|
||||
|
||||
class Entity;
|
||||
|
||||
class Component
|
||||
@ -8,7 +10,7 @@ public:
|
||||
Entity* entity;
|
||||
|
||||
virtual void init() {}
|
||||
virtual void update() {}
|
||||
virtual void update(uint_fast16_t diffTime) {}
|
||||
|
||||
virtual ~Component() = default;
|
||||
};
|
||||
@ -59,7 +59,7 @@ public:
|
||||
explicit Entity(Manager& mManager) :
|
||||
manager(mManager) { };
|
||||
|
||||
void update() const; //!< Call each frame to update all components
|
||||
void update(uint_fast16_t diffTime) const; //!< Call each frame to update all components
|
||||
|
||||
bool isActive() const { return this->active; } //!< \sa destroy()
|
||||
//! Mark for destruction for Manager::refresh() and disables collision
|
||||
|
||||
@ -8,7 +8,7 @@ public:
|
||||
virtual ~Game() {}
|
||||
|
||||
virtual void init() = 0;
|
||||
virtual void update() = 0;
|
||||
virtual void update(uint_fast16_t diffTime) = 0;
|
||||
|
||||
GameInternal* gameInternal; //!< \deprecated
|
||||
};
|
||||
|
||||
@ -1,8 +1,9 @@
|
||||
#pragma once
|
||||
|
||||
#include <SDL.h>
|
||||
#include <SDL_image.h>
|
||||
#include <SDL_mixer.h>
|
||||
#include <SDL3/SDL.h>
|
||||
#include <SDL3_image/SDL_image.h>
|
||||
#include <SDL3_mixer/SDL_mixer.h>
|
||||
#include <cstdint>
|
||||
#include <functional>
|
||||
#include <vector>
|
||||
|
||||
@ -26,10 +27,10 @@ public:
|
||||
GameInternal();
|
||||
~GameInternal();
|
||||
|
||||
void init(const char* title, int xpos, int ypos, int width, int height, bool fullscreen);
|
||||
SDL_AppResult init(const char* title, int xpos, int ypos, int width, int height, bool fullscreen);
|
||||
|
||||
void handleEvents();
|
||||
void update();
|
||||
void update(Uint64 frameTime);
|
||||
void render();
|
||||
void clean();
|
||||
bool isRunning() const;
|
||||
@ -63,4 +64,6 @@ private:
|
||||
int counter = 0;
|
||||
bool running = true;
|
||||
SDL_Window* window;
|
||||
|
||||
Uint64 lastFrameTime = 0;
|
||||
};
|
||||
|
||||
@ -1,5 +1,5 @@
|
||||
#pragma once
|
||||
#include <SDL.h>
|
||||
#include <SDL3/SDL.h>
|
||||
#include <map>
|
||||
|
||||
#include "Component.h"
|
||||
@ -94,12 +94,12 @@ public:
|
||||
~InputComponent();
|
||||
|
||||
void init() override;
|
||||
void update() override;
|
||||
void update(uint_fast16_t diffTime) override;
|
||||
|
||||
bool isKeyDown(Key key);
|
||||
|
||||
private:
|
||||
const Uint8* m_keyStates;
|
||||
const bool* m_keyStates;
|
||||
SDL_Scancode mapKeyToSDL(Key key);
|
||||
std::map<Key, SDL_Scancode> m_keyMappings;
|
||||
void InitKeyMappings();
|
||||
|
||||
@ -24,7 +24,7 @@ class Manager
|
||||
public:
|
||||
Manager(GameInternal* game) : game(game) {};
|
||||
|
||||
void update(); //!< \sa Entity::update()
|
||||
void update(uint_fast16_t diffTime); //!< \sa Entity::update()
|
||||
//! Disables all functionality of entities marked for destruction
|
||||
//! \sa Entity::destroy()
|
||||
void refresh();
|
||||
|
||||
@ -34,7 +34,7 @@ private:
|
||||
|
||||
struct TileSetData {
|
||||
std::string texturePath{};
|
||||
tmx::Vector2i textureSize;
|
||||
tmx::Vector2f textureSize;
|
||||
uint32_t tileCount{};
|
||||
tmx::Vector2u tileCount2D;
|
||||
uint32_t firstGID{};
|
||||
|
||||
@ -9,7 +9,7 @@ public:
|
||||
PowerupComponent(std::function<void (Entity*)> func);
|
||||
~PowerupComponent() {};
|
||||
|
||||
void update() override;
|
||||
void update(uint_fast16_t diffTime) override;
|
||||
|
||||
private:
|
||||
std::function<void (Entity*)> pickupFunc;
|
||||
|
||||
@ -16,14 +16,14 @@ public:
|
||||
~ProjectileComponent() {}
|
||||
|
||||
void init() override;
|
||||
void update() override;
|
||||
void update(uint_fast16_t diffTime) override;
|
||||
|
||||
private:
|
||||
TransformComponent* transformComponent;
|
||||
|
||||
int range = 0;
|
||||
int speed = 0;
|
||||
int distance = 0;
|
||||
float speed = 0;
|
||||
float distance = 0;
|
||||
|
||||
Entity* owner = nullptr;
|
||||
|
||||
|
||||
@ -1,6 +1,6 @@
|
||||
#pragma once
|
||||
|
||||
#include <SDL_mixer.h>
|
||||
#include <SDL3_mixer/SDL_mixer.h>
|
||||
#include <map>
|
||||
#include <vector>
|
||||
|
||||
|
||||
@ -1,7 +1,7 @@
|
||||
#pragma once
|
||||
|
||||
#include <map>
|
||||
#include <SDL_render.h>
|
||||
#include <SDL3/SDL_render.h>
|
||||
#include <memory>
|
||||
#include <string>
|
||||
|
||||
@ -23,7 +23,7 @@ public:
|
||||
private:
|
||||
TransformComponent* transform;
|
||||
SDL_Texture* texture;
|
||||
SDL_Rect srcRect, destRect;
|
||||
SDL_FRect srcRect, destRect;
|
||||
|
||||
Textures textureEnum;
|
||||
|
||||
@ -55,7 +55,7 @@ public:
|
||||
void setMapTileTexture(const char* path);
|
||||
|
||||
void init() override;
|
||||
void update() override;
|
||||
void update(uint_fast16_t diffTime) override;
|
||||
void draw() override;
|
||||
void playAnimation(std::string type);
|
||||
void setDirection(Direction direction);
|
||||
|
||||
@ -16,7 +16,7 @@ public:
|
||||
~StatEffectsComponent() {};
|
||||
|
||||
void init() override;
|
||||
void update() override;
|
||||
void update(uint_fast16_t diffTime) override;
|
||||
|
||||
void modifyStatDur(Stats stat, int duration, int value);
|
||||
|
||||
|
||||
@ -1,7 +1,8 @@
|
||||
#pragma once
|
||||
|
||||
#include "ECS.h"
|
||||
#include <SDL_render.h>
|
||||
#include "SDL3/SDL_surface.h"
|
||||
#include <SDL3/SDL_render.h>
|
||||
#include <map>
|
||||
#include <memory>
|
||||
#include <string>
|
||||
@ -65,7 +66,9 @@ class TextureManager
|
||||
*/
|
||||
SDL_Texture* loadTexture(Textures texture);
|
||||
static std::vector<SDL_Rect> splitSpriteSheet(SDL_Texture* spriteSheet, int width, int height, int spritesOnSheet);
|
||||
static void draw(SDL_Renderer* renderer, SDL_Texture* texture, SDL_Rect src, SDL_Rect dest, bool flipped = false);
|
||||
static void draw(SDL_Renderer* renderer, SDL_Texture* texture, SDL_FRect src, SDL_FRect dest, bool flipped = false);
|
||||
|
||||
void setScaleMode(SDL_ScaleMode scaleMode) { this->scaleMode = scaleMode; }
|
||||
|
||||
/*!
|
||||
* \brief Loads a map tile texture from the file system and caches it.
|
||||
@ -81,6 +84,7 @@ class TextureManager
|
||||
SDL_Texture* loadMapTileTexture(const char* path);
|
||||
|
||||
private:
|
||||
SDL_ScaleMode scaleMode = SDL_SCALEMODE_NEAREST;
|
||||
Manager* manager;
|
||||
std::map<Textures, SDL_Texture*> texture_cache;
|
||||
std::map<std::string, SDL_Texture*> mapTile_texture_cache;
|
||||
|
||||
@ -1,6 +1,6 @@
|
||||
#pragma once
|
||||
|
||||
#include <SDL.h>
|
||||
#include <SDL3/SDL.h>
|
||||
#include <string>
|
||||
#include <map>
|
||||
|
||||
|
||||
@ -25,11 +25,11 @@ public:
|
||||
|
||||
void init() override;
|
||||
/*! TODO: document usage of collision handler */
|
||||
void update() override;
|
||||
void update(uint_fast16_t diffTime) override;
|
||||
void setPositionAfterCollision(Vector2D& positionChange);
|
||||
void modifySpeed(int8_t modifier);
|
||||
|
||||
private:
|
||||
int speed = 3;
|
||||
int speed = 180;
|
||||
int speedMod = 0;
|
||||
};
|
||||
|
||||
@ -1,7 +1,7 @@
|
||||
#pragma once
|
||||
|
||||
#include <SDL.h>
|
||||
#include <SDL_rect.h>
|
||||
#include <SDL3/SDL.h>
|
||||
#include <SDL3/SDL_rect.h>
|
||||
|
||||
class Vector2D
|
||||
{
|
||||
|
||||
@ -41,7 +41,7 @@ Mix_Music* AssetManager::getMusic(std::string id)
|
||||
return music.at(id);
|
||||
}
|
||||
|
||||
void AssetManager::createProjectile(Vector2D pos, Vector2D velocity, int scale, int range, int speed, Textures textureEnum, Entity* owner) {
|
||||
void AssetManager::createProjectile(Vector2D pos, Vector2D velocity, int scale, int range, float speed, Textures textureEnum, Entity* owner) {
|
||||
|
||||
auto& projectile(man->addEntity());
|
||||
projectile.addComponent<TransformComponent>(pos.x, pos.y, 32, 32, scale); //32x32 is standard size for objects
|
||||
@ -81,7 +81,7 @@ Vector2D AssetManager::calculateSpawnPosition()
|
||||
conflict = false;
|
||||
for (auto cc : this->man->getGame()->collisionHandler->getColliders({ Entity::GroupLabel::MAPTILES }))
|
||||
{
|
||||
if (SDL_HasIntersection(&spawnRect, &cc->collider) && strcmp(cc->tag, "projectile"))
|
||||
if (SDL_HasRectIntersection(&spawnRect, &cc->collider) && strcmp(cc->tag, "projectile"))
|
||||
{
|
||||
conflict = true;
|
||||
break;
|
||||
|
||||
@ -31,10 +31,10 @@ void ColliderComponent::init()
|
||||
}
|
||||
|
||||
transform = &entity->getComponent<TransformComponent>();
|
||||
this->update();
|
||||
this->update(0);
|
||||
}
|
||||
|
||||
void ColliderComponent::update()
|
||||
void ColliderComponent::update(uint_fast16_t diffTime)
|
||||
{
|
||||
collider.x = transform->position.x - (transform->width - transform->width * transform->scale * this->hitboxScale) / 2;
|
||||
collider.y = transform->position.y - (transform->width - transform->width * transform->scale * this->hitboxScale) / 2;
|
||||
|
||||
@ -6,7 +6,7 @@
|
||||
#include "Manager.h"
|
||||
#include "Vector2D.h"
|
||||
|
||||
#include <SDL_rect.h>
|
||||
#include <SDL3/SDL_rect.h>
|
||||
#include <bitset>
|
||||
#include <cstdio>
|
||||
#include <memory>
|
||||
@ -26,7 +26,7 @@ IntersectionBitSet CollisionHandler::getIntersection(Entity* entityA, Entity* en
|
||||
colliderB.x += posModB.x;
|
||||
colliderB.y += posModB.y;
|
||||
|
||||
if (!SDL_HasIntersection(
|
||||
if (!SDL_HasRectIntersection(
|
||||
&colliderA,
|
||||
&colliderB))
|
||||
return std::bitset<DIRECTION_C>();
|
||||
@ -152,7 +152,7 @@ Entity* CollisionHandler::getAnyIntersection<Entity*>(
|
||||
if (!entity->hasComponent<ColliderComponent>()) return nullptr;
|
||||
for (auto& collider : getColliders(groupLabels, excludedEntities)) {
|
||||
SDL_Rect rect = entity->getComponent<ColliderComponent>().collider + posMod;
|
||||
if (SDL_HasIntersection(&rect, &collider->collider)) {
|
||||
if (SDL_HasRectIntersection(&rect, &collider->collider)) {
|
||||
return collider->entity;
|
||||
}
|
||||
}
|
||||
@ -175,7 +175,7 @@ bool CollisionHandler::getAnyIntersection<bool>(
|
||||
if (!entity->hasComponent<ColliderComponent>()) return false;
|
||||
for (auto& collider : getColliders(groupLabels, excludedEntities)) {
|
||||
SDL_Rect rect = entity->getComponent<ColliderComponent>().collider + posMod;
|
||||
if (SDL_HasIntersection(&rect, &collider->collider)) {
|
||||
if (SDL_HasRectIntersection(&rect, &collider->collider)) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
@ -4,9 +4,9 @@
|
||||
#include "Component.h"
|
||||
#include <cstddef>
|
||||
|
||||
void Entity::update() const
|
||||
void Entity::update(uint_fast16_t diffTime) const
|
||||
{
|
||||
for (auto const& c : components) c->update();
|
||||
for (auto const& c : components) c->update(diffTime);
|
||||
}
|
||||
|
||||
bool Entity::hasGroup(Group mGroup)
|
||||
|
||||
@ -1,23 +1,20 @@
|
||||
#include "GameInternal.h"
|
||||
|
||||
#include <SDL_error.h>
|
||||
|
||||
#include "CollisionHandler.h"
|
||||
#include "AssetManager.h"
|
||||
#include "RenderManager.h"
|
||||
#include "SDL_mixer.h"
|
||||
#include <SDL3_mixer/SDL_mixer.h>
|
||||
#include "SDL3/SDL_init.h"
|
||||
#include "SoundManager.h"
|
||||
#include "TileComponent.h"
|
||||
#include "Direction.h"
|
||||
#include "Entity.h"
|
||||
#include "HealthComponent.h"
|
||||
#include "Map.h"
|
||||
#include "TextureManager.h"
|
||||
#include "StatEffectsComponent.h"
|
||||
#include "Constants.h"
|
||||
#include "Game.h"
|
||||
#include "GameFactory.h"
|
||||
|
||||
#include <VEGO.h>
|
||||
|
||||
GameInternal::GameInternal() :
|
||||
manager(this),
|
||||
renderManager(),
|
||||
@ -30,7 +27,7 @@ GameInternal::GameInternal() :
|
||||
|
||||
GameInternal::~GameInternal() = default;
|
||||
|
||||
void GameInternal::init(const char* title, int xpos, int ypos, int width, int height, bool fullscreen)
|
||||
SDL_AppResult GameInternal::init(const char* title, int xpos, int ypos, int width, int height, bool fullscreen)
|
||||
{
|
||||
GameInternal::assets = new AssetManager(&manager);
|
||||
GameInternal::textureManager = new TextureManager(&manager);
|
||||
@ -43,24 +40,24 @@ void GameInternal::init(const char* title, int xpos, int ypos, int width, int he
|
||||
flags = SDL_WINDOW_FULLSCREEN;
|
||||
}
|
||||
|
||||
if (SDL_Init(SDL_INIT_EVERYTHING) != 0)
|
||||
if (!SDL_Init(SDL_INIT_AUDIO | SDL_INIT_VIDEO))
|
||||
{
|
||||
std::cout << "ERROR. Subsystem couldnt be initialized! " << SDL_GetError() << std::endl;
|
||||
SDL_ClearError();
|
||||
return;
|
||||
return SDL_APP_FAILURE;
|
||||
}
|
||||
|
||||
if (Mix_Init(MIX_INIT_MP3) != MIX_INIT_MP3) {
|
||||
std::cout << "ERROR. Subsystem couldnt be initialized!" << std::endl;
|
||||
return;
|
||||
return SDL_APP_FAILURE;
|
||||
}
|
||||
|
||||
window = SDL_CreateWindow(title, xpos, ypos, width, height, flags);
|
||||
window = SDL_CreateWindow(title, width, height, flags);
|
||||
if (!window)
|
||||
{
|
||||
std::cout << "ERROR: Window couldnt be created! " << SDL_GetError() << std::endl;
|
||||
SDL_ClearError();
|
||||
return;
|
||||
return SDL_APP_FAILURE;
|
||||
}
|
||||
|
||||
// bad
|
||||
@ -72,20 +69,20 @@ void GameInternal::init(const char* title, int xpos, int ypos, int width, int he
|
||||
|
||||
SDL_SetWindowIcon(window, icon);
|
||||
|
||||
renderer = SDL_CreateRenderer(window, -1, 0);
|
||||
renderer = SDL_CreateRenderer(window, NULL);
|
||||
if (!renderer)
|
||||
{
|
||||
std::cout << "ERROR: Renderer couldnt be created! " << SDL_GetError() << std::endl;
|
||||
SDL_ClearError();
|
||||
return;
|
||||
return SDL_APP_FAILURE;
|
||||
}
|
||||
SDL_SetRenderDrawColor(renderer, 255, 255, 255, 255);
|
||||
|
||||
if (Mix_OpenAudio(44100, MIX_DEFAULT_FORMAT, 2, 2048) < 0)
|
||||
if (!Mix_OpenAudio(0, NULL))
|
||||
{
|
||||
std::cout << "ERROR: Mixer couldnt be initialized! " << SDL_GetError() << std::endl;
|
||||
SDL_ClearError();
|
||||
return;
|
||||
return SDL_APP_FAILURE;
|
||||
}
|
||||
|
||||
Mix_Volume(-1, MIX_MAX_VOLUME);
|
||||
@ -100,6 +97,8 @@ void GameInternal::init(const char* title, int xpos, int ypos, int width, int he
|
||||
|
||||
this->gameInstance = GameFactory::instance().create(this);
|
||||
this->gameInstance->init();
|
||||
|
||||
return SDL_APP_CONTINUE;
|
||||
}
|
||||
|
||||
void GameInternal::handleEvents()
|
||||
@ -108,7 +107,7 @@ void GameInternal::handleEvents()
|
||||
|
||||
switch (event.type)
|
||||
{
|
||||
case SDL_QUIT: this->setRunning(false);
|
||||
case SDL_EVENT_QUIT: this->setRunning(false);
|
||||
break;
|
||||
|
||||
default:
|
||||
@ -116,12 +115,16 @@ void GameInternal::handleEvents()
|
||||
}
|
||||
}
|
||||
|
||||
void GameInternal::update()
|
||||
void GameInternal::update(Uint64 frameTime)
|
||||
{
|
||||
manager.refresh();
|
||||
manager.update();
|
||||
|
||||
this->gameInstance->update(); // TODO: this might have to be split up into two update functions, before and after manager...
|
||||
uint_fast16_t diffTime = frameTime - this->lastFrameTime;
|
||||
manager.update(diffTime);
|
||||
|
||||
this->gameInstance->update(diffTime); // TODO: this might have to be split up into two update functions, before and after manager...
|
||||
|
||||
this->lastFrameTime = frameTime;
|
||||
}
|
||||
|
||||
void GameInternal::render()
|
||||
|
||||
@ -10,7 +10,7 @@ InputComponent::~InputComponent() = default;
|
||||
|
||||
void InputComponent::init(){}
|
||||
|
||||
void InputComponent::update()
|
||||
void InputComponent::update(uint_fast16_t diffTime)
|
||||
{
|
||||
SDL_PumpEvents();
|
||||
}
|
||||
|
||||
@ -27,9 +27,9 @@ void Manager::refresh()
|
||||
std::end(entities));
|
||||
}
|
||||
|
||||
void Manager::update()
|
||||
void Manager::update(uint_fast16_t diffTime)
|
||||
{
|
||||
for (auto& e : entities) e->update();
|
||||
for (auto& e : entities) e->update(diffTime);
|
||||
}
|
||||
|
||||
void Manager::addToGroup(Entity* mEntity, Group mGroup)
|
||||
|
||||
12
src/Map.cpp
12
src/Map.cpp
@ -9,8 +9,8 @@
|
||||
#include <ranges>
|
||||
#include <vector>
|
||||
|
||||
#include <SDL_error.h>
|
||||
#include <SDL_render.h>
|
||||
#include <SDL3/SDL_error.h>
|
||||
#include <SDL3/SDL_render.h>
|
||||
|
||||
#include <tmxlite/Layer.hpp>
|
||||
#include <tmxlite/Map.hpp>
|
||||
@ -102,16 +102,14 @@ void Map::loadTileLayer(const tmx::TileLayer& layer)
|
||||
| std::views::transform([&](uint16_t i) {
|
||||
const char* texturePath = this->mapData.texturePaths->at(i).c_str();
|
||||
|
||||
tmx::Vector2i textureSize;
|
||||
SDL_QueryTexture(
|
||||
tmx::Vector2f textureSize;
|
||||
SDL_GetTextureSize(
|
||||
VEGO_Game().textureManager->loadMapTileTexture(texturePath),
|
||||
nullptr,
|
||||
nullptr,
|
||||
&(textureSize.x),
|
||||
&(textureSize.y)
|
||||
);
|
||||
|
||||
tmx::Vector2u tileCount2D = { textureSize.x / this->mapData.mapTileSize->x, textureSize.y / this->mapData.mapTileSize->y };
|
||||
tmx::Vector2u tileCount2D = { static_cast<unsigned int>(textureSize.x / this->mapData.mapTileSize->x), static_cast<unsigned int>(textureSize.y / this->mapData.mapTileSize->y) };
|
||||
|
||||
uint32_t tileCount = this->mapData.tileSets->at(i).getTileCount();
|
||||
uint32_t firstGID = this->mapData.tileSets->at(i).getFirstGID();
|
||||
|
||||
@ -12,7 +12,7 @@ PowerupComponent::PowerupComponent(std::function<void (Entity*)> func)
|
||||
this->pickupFunc = func;
|
||||
}
|
||||
|
||||
void PowerupComponent::update()
|
||||
void PowerupComponent::update(uint_fast16_t diffTime)
|
||||
{
|
||||
Entity* player;
|
||||
if ((player = this->entity->getManager().getGame()->collisionHandler->getAnyIntersection<Entity*>(
|
||||
|
||||
@ -17,9 +17,9 @@ void ProjectileComponent::init()
|
||||
SoundManager::playSound(this->entity->getManager().getGame(), "throw_egg", true, PLAY_ONCE, MAX_VOLUME, -1);
|
||||
}
|
||||
|
||||
void ProjectileComponent::update()
|
||||
void ProjectileComponent::update(uint_fast16_t diffTime)
|
||||
{
|
||||
distance += speed;
|
||||
distance += speed * diffTime * (1.f/1000);
|
||||
|
||||
IntersectionBitSet boundsIntersection = this->entity->getManager().getGame()->collisionHandler->getIntersectionWithBounds(entity);
|
||||
|
||||
|
||||
@ -1,9 +1,10 @@
|
||||
#include "SoundManager.h"
|
||||
|
||||
#include <stdexcept>
|
||||
#include <string>
|
||||
#include <iostream>
|
||||
|
||||
#include <SDL3_mixer/SDL_mixer.h>
|
||||
|
||||
#include "GameInternal.h"
|
||||
#include "AssetManager.h"
|
||||
|
||||
@ -65,12 +66,12 @@ void SoundManager::playSound(GameInternal* game, std::string sound, bool canOver
|
||||
|
||||
if(Mix_VolumeChunk(game->assets->getSound(sound), volume) == -1)
|
||||
{
|
||||
std::cerr << "Error adjusting volume: " << Mix_GetError() << std::endl;
|
||||
std::cerr << "Error adjusting volume: " << SDL_GetError() << std::endl;
|
||||
}
|
||||
|
||||
if (Mix_PlayChannel(channel, game->assets->getSound(sound), loops) == -1)
|
||||
{
|
||||
std::cerr << "Error playing sound '" << sound << "': " << Mix_GetError() << std::endl;
|
||||
std::cerr << "Error playing sound '" << sound << "': " << SDL_GetError() << std::endl;
|
||||
}
|
||||
}
|
||||
|
||||
@ -87,12 +88,12 @@ void SoundManager::playMusic(GameInternal* game, std::string music, int loops, i
|
||||
|
||||
if(Mix_VolumeMusic(volume) == -1)
|
||||
{
|
||||
std::cerr << "Error adjusting volume: " << Mix_GetError() << std::endl;
|
||||
std::cerr << "Error adjusting volume: " << SDL_GetError() << std::endl;
|
||||
}
|
||||
|
||||
if (Mix_PlayMusic(game->assets->getMusic(music), loops) == -1)
|
||||
{
|
||||
std::cerr << "Error playing music '" << music << "': " << Mix_GetError() << std::endl;
|
||||
std::cerr << "Error playing music '" << music << "': " << SDL_GetError() << std::endl;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@ -1,6 +1,6 @@
|
||||
#include "SpriteComponent.h"
|
||||
|
||||
#include <SDL_timer.h>
|
||||
#include <SDL3/SDL_timer.h>
|
||||
#include <cstring>
|
||||
#include <memory>
|
||||
|
||||
@ -73,14 +73,14 @@ void SpriteComponent::init()
|
||||
this->srcRect.x = this->textureXOffset * this->srcRect.w;
|
||||
this->srcRect.y = this->textureYOffset * this->srcRect.h;;
|
||||
|
||||
this->update();
|
||||
this->update(0);
|
||||
}
|
||||
|
||||
void SpriteComponent::update()
|
||||
void SpriteComponent::update(uint_fast16_t diffTime)
|
||||
{
|
||||
// This code is not compatible for animated tiles
|
||||
if (animated) {
|
||||
srcRect.x = srcRect.w * static_cast<int>((SDL_GetTicks() / speed) % frames);
|
||||
srcRect.x = srcRect.w * static_cast<int>((SDL_GetTicks() / speed) % frames); // TODO: should not call SDL_GetTicks() but use diffTime
|
||||
|
||||
srcRect.y = animationIndex * transform->height;
|
||||
}
|
||||
|
||||
@ -8,16 +8,14 @@
|
||||
void StatEffectsComponent::init()
|
||||
{}
|
||||
|
||||
void StatEffectsComponent::update()
|
||||
void StatEffectsComponent::update(uint_fast16_t diffTime)
|
||||
{
|
||||
for (int i = 0; i < MAX_STATS; i++)
|
||||
{
|
||||
if (this->buffs.at(i) == 0) continue;
|
||||
if (this->buffs.at(i) - 1 == 0)
|
||||
{
|
||||
this->buffs.at(i) -= diffTime;
|
||||
if (this->buffs.at(i) <= 0) {
|
||||
this->resetStatValue((Stats)i);
|
||||
}
|
||||
this->buffs.at(i) -= 1;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@ -1,6 +1,5 @@
|
||||
#include "TextureManager.h"
|
||||
|
||||
#include <memory>
|
||||
#include <stdexcept>
|
||||
#include <string>
|
||||
#include <VEGO.h>
|
||||
@ -14,6 +13,7 @@ void TextureManager::addSingleTexture(Textures texture, const char* filePath) {
|
||||
if (sdlTexture == nullptr)
|
||||
throw std::runtime_error(std::string("Couldn't load texture '") + filePath + "'");
|
||||
|
||||
SDL_SetTextureScaleMode(sdlTexture, this->scaleMode); // linear scaling results in blurry images
|
||||
this->texture_cache.emplace(texture, sdlTexture);
|
||||
std::cout << "Loaded texture at " << filePath << std::endl;
|
||||
}
|
||||
@ -35,10 +35,10 @@ SDL_Texture* TextureManager::loadTexture(Textures texture) {
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
void TextureManager::draw(SDL_Renderer* renderer, SDL_Texture* texture, SDL_Rect src, SDL_Rect dest, bool flipped)
|
||||
void TextureManager::draw(SDL_Renderer* renderer, SDL_Texture* texture, SDL_FRect src, SDL_FRect dest, bool flipped)
|
||||
{
|
||||
SDL_RendererFlip flip = flipped ? SDL_FLIP_HORIZONTAL : SDL_FLIP_NONE;
|
||||
SDL_RenderCopyEx(renderer, texture, &src, &dest, 0, NULL, flip);
|
||||
SDL_FlipMode flip = flipped ? SDL_FLIP_HORIZONTAL : SDL_FLIP_NONE;
|
||||
SDL_RenderTextureRotated(renderer, texture, &src, &dest, 0, NULL, flip);
|
||||
}
|
||||
|
||||
SDL_Texture* TextureManager::loadMapTileTexture(const char *path) {
|
||||
@ -51,7 +51,8 @@ SDL_Texture* TextureManager::loadMapTileTexture(const char *path) {
|
||||
|
||||
if (newTexture == nullptr)
|
||||
throw std::runtime_error(std::string("Couldn't load texture '") + path + "'");
|
||||
|
||||
|
||||
SDL_SetTextureScaleMode(newTexture, this->scaleMode); // linear scaling results in blurry images
|
||||
this->mapTile_texture_cache.emplace(std::string(path), newTexture);
|
||||
|
||||
return newTexture;
|
||||
|
||||
@ -50,12 +50,12 @@ void TransformComponent::init()
|
||||
direction.zero();
|
||||
}
|
||||
|
||||
void TransformComponent::update()
|
||||
void TransformComponent::update(uint_fast16_t diffTime)
|
||||
{
|
||||
float multiplier = direction.x != 0 && direction.y != 0 ? 0.707 : 1; // normalizes vector; only works if directions are in increments of 45°
|
||||
Vector2D positionChange(
|
||||
direction.x * this->getSpeed() * multiplier,
|
||||
direction.y * this->getSpeed() * multiplier
|
||||
direction.x * this->getSpeed() * multiplier * diffTime * (1.f/1000),
|
||||
direction.y * this->getSpeed() * multiplier * diffTime * (1.f/1000)
|
||||
);
|
||||
|
||||
if (this->entity->hasGroup((size_t)Entity::GroupLabel::PLAYERS)){
|
||||
|
||||
@ -1,5 +1,5 @@
|
||||
#include "Vector2D.h"
|
||||
#include "SDL_rect.h"
|
||||
#include <SDL3/SDL_rect.h>
|
||||
|
||||
Vector2D::Vector2D()
|
||||
{
|
||||
|
||||
48
src/_Init.cpp
Normal file
48
src/_Init.cpp
Normal file
@ -0,0 +1,48 @@
|
||||
#include "SDL3/SDL_init.h"
|
||||
#include <cstdint>
|
||||
#define SDL_MAIN_USE_CALLBACKS
|
||||
#include <SDL3/SDL_main.h>
|
||||
|
||||
#include <iostream>
|
||||
#include <ctime>
|
||||
|
||||
#include "VEGO.h"
|
||||
#include "Entity.h"
|
||||
#include "GameInternal.h"
|
||||
#include "Constants.h"
|
||||
|
||||
GameInternal* vego::game = nullptr;
|
||||
|
||||
SDL_AppResult SDL_AppInit(void **appstate, int argc, char **argv) {
|
||||
srand(time(NULL));
|
||||
bool playing = true;
|
||||
|
||||
*appstate = vego::game = new GameInternal();
|
||||
|
||||
return vego::game->init("No_Name_Chicken_Game", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, SCREEN_SIZE_WIDTH, SCREEN_SIZE_HEIGHT, false);
|
||||
}
|
||||
|
||||
SDL_AppResult SDL_AppIterate(void *appstate) {
|
||||
if (!vego::game->isRunning()) {
|
||||
return SDL_APP_SUCCESS;
|
||||
}
|
||||
|
||||
vego::game->handleEvents(); // bad
|
||||
|
||||
Uint64 frameStart = SDL_GetTicks();
|
||||
|
||||
vego::game->update(frameStart);
|
||||
vego::game->render();
|
||||
|
||||
int frameTime = SDL_GetTicks() - frameStart;
|
||||
|
||||
return SDL_APP_CONTINUE;
|
||||
}
|
||||
|
||||
SDL_AppResult SDL_AppEvent(void *appstate, SDL_Event *event) {
|
||||
return SDL_APP_CONTINUE;
|
||||
}
|
||||
|
||||
void SDL_AppQuit(void *appstate, SDL_AppResult result) {
|
||||
vego::game->clean();
|
||||
}
|
||||
41
src/main.cpp
41
src/main.cpp
@ -1,41 +0,0 @@
|
||||
#include <iostream>
|
||||
#include <ctime>
|
||||
|
||||
#include "VEGO.h"
|
||||
#include "Entity.h"
|
||||
#include "GameInternal.h"
|
||||
#include "Constants.h"
|
||||
|
||||
GameInternal* vego::game = nullptr;
|
||||
|
||||
int main(int argc, char* argv[])
|
||||
{
|
||||
srand(time(NULL));
|
||||
bool playing = true;
|
||||
|
||||
const int frameDelay = 1000 / FPS;
|
||||
|
||||
Uint32 frameStart;
|
||||
int frameTime;
|
||||
|
||||
vego::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()) {
|
||||
frameStart = SDL_GetTicks();
|
||||
|
||||
vego::game->handleEvents();
|
||||
vego::game->update();
|
||||
vego::game->render();
|
||||
|
||||
frameTime = SDL_GetTicks() - frameStart;
|
||||
|
||||
if (frameDelay > frameTime) {
|
||||
SDL_Delay(frameDelay - frameTime);
|
||||
}
|
||||
}
|
||||
|
||||
vego::game->clean();
|
||||
|
||||
return 0;
|
||||
}
|
||||
Loading…
x
Reference in New Issue
Block a user