mirror of
https://github.com/Nimac0/SDL_Minigame
synced 2026-01-12 21:23:41 +00:00
Merge abe018f99bf6b19c2d389580b64b247bc3cdbd60 into 7c50c8d1fb622cfa37ba91b42e65c5afe5512443
This commit is contained in:
commit
b1c5e2187d
3
.gitmodules
vendored
3
.gitmodules
vendored
@ -17,3 +17,6 @@
|
|||||||
path = docs/doxygen-awesome-css
|
path = docs/doxygen-awesome-css
|
||||||
url = https://github.com/jothepro/doxygen-awesome-css.git
|
url = https://github.com/jothepro/doxygen-awesome-css.git
|
||||||
|
|
||||||
|
[submodule "extern/nlohmann_json"]
|
||||||
|
path = extern/nlohmann_json
|
||||||
|
url = https://github.com/nlohmann/json.git
|
||||||
|
|||||||
@ -24,10 +24,13 @@ add_subdirectory(extern/SDL_image EXCLUDE_FROM_ALL)
|
|||||||
add_subdirectory(extern/SDL_mixer EXCLUDE_FROM_ALL)
|
add_subdirectory(extern/SDL_mixer EXCLUDE_FROM_ALL)
|
||||||
add_subdirectory(extern/SDL_ttf EXCLUDE_FROM_ALL)
|
add_subdirectory(extern/SDL_ttf EXCLUDE_FROM_ALL)
|
||||||
add_subdirectory(extern/tmxlite/tmxlite EXCLUDE_FROM_ALL)
|
add_subdirectory(extern/tmxlite/tmxlite EXCLUDE_FROM_ALL)
|
||||||
|
include_directories(extern/nlohmann_json/include)
|
||||||
|
|
||||||
|
|
||||||
file(GLOB_RECURSE SOURCES ${ENGINE_SOURCE_DIR}/src/*.cpp)
|
file(GLOB_RECURSE SOURCES ${ENGINE_SOURCE_DIR}/src/*.cpp)
|
||||||
add_library(${PROJECT_NAME} ${SOURCES})
|
add_library(${PROJECT_NAME} ${SOURCES}
|
||||||
|
include/ConfigLoader.h
|
||||||
|
src/ConfigLoader.cpp)
|
||||||
|
|
||||||
target_include_directories(${PROJECT_NAME} PUBLIC ${ENGINE_INCLUDE_DIR})
|
target_include_directories(${PROJECT_NAME} PUBLIC ${ENGINE_INCLUDE_DIR})
|
||||||
|
|
||||||
|
|||||||
7
config.json
Normal file
7
config.json
Normal file
@ -0,0 +1,7 @@
|
|||||||
|
{
|
||||||
|
"fullscreen": false,
|
||||||
|
"title": "VGG (Very Good Game)",
|
||||||
|
"height": 100,
|
||||||
|
"width": 100,
|
||||||
|
"icon": "./engine/internalAssets/iconImage.bmp"
|
||||||
|
}
|
||||||
1
extern/nlohmann_json
vendored
Submodule
1
extern/nlohmann_json
vendored
Submodule
@ -0,0 +1 @@
|
|||||||
|
Subproject commit a006a7a48bb30a247f0344b788c62c2806edd90b
|
||||||
26
include/ConfigLoader.h
Normal file
26
include/ConfigLoader.h
Normal file
@ -0,0 +1,26 @@
|
|||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include <nlohmann/json.hpp>
|
||||||
|
|
||||||
|
using json = nlohmann::json;
|
||||||
|
|
||||||
|
class ConfigLoader {
|
||||||
|
|
||||||
|
public:
|
||||||
|
ConfigLoader();
|
||||||
|
~ConfigLoader();
|
||||||
|
|
||||||
|
void init();
|
||||||
|
|
||||||
|
void setCustomConfig(const std::optional<std::string>& path);
|
||||||
|
|
||||||
|
json getFinalConfig();
|
||||||
|
|
||||||
|
private:
|
||||||
|
json baseConfig;
|
||||||
|
std::optional<json> customConfig;
|
||||||
|
json finalConfig;
|
||||||
|
|
||||||
|
json loadConfigFromJSON(const std::string& path);
|
||||||
|
json mergeConfigs(json baseConfig, json customConfig); //<! Merges 2 config.json files, prioritising the custom to the base one
|
||||||
|
};
|
||||||
@ -11,9 +11,6 @@ constexpr std::size_t MAX_GROUPS = 32;
|
|||||||
constexpr std::size_t MAX_STATS = 8;
|
constexpr std::size_t MAX_STATS = 8;
|
||||||
constexpr std::size_t MAX_TEAMS = 8;
|
constexpr std::size_t MAX_TEAMS = 8;
|
||||||
|
|
||||||
constexpr int SCREEN_SIZE_HEIGHT = 640;
|
|
||||||
constexpr int SCREEN_SIZE_WIDTH = 800;
|
|
||||||
|
|
||||||
constexpr int FPS = 60;
|
constexpr int FPS = 60;
|
||||||
|
|
||||||
constexpr int TILE_SIZE = 32;
|
constexpr int TILE_SIZE = 32;
|
||||||
|
|||||||
@ -9,6 +9,7 @@ public:
|
|||||||
|
|
||||||
virtual void init() = 0;
|
virtual void init() = 0;
|
||||||
virtual void update(uint_fast16_t diffTime) = 0;
|
virtual void update(uint_fast16_t diffTime) = 0;
|
||||||
|
virtual std::optional<std::string> getConfigFilePath() {return std::nullopt;}
|
||||||
|
|
||||||
GameInternal* gameInternal; //!< \deprecated
|
GameInternal* gameInternal; //!< \deprecated
|
||||||
};
|
};
|
||||||
|
|||||||
@ -11,6 +11,7 @@
|
|||||||
#include "Vector2D.h"
|
#include "Vector2D.h"
|
||||||
#include "Entity.h"
|
#include "Entity.h"
|
||||||
#include "RenderManager.h"
|
#include "RenderManager.h"
|
||||||
|
#include "ConfigLoader.h"
|
||||||
|
|
||||||
typedef std::function<void()> gamefunction;
|
typedef std::function<void()> gamefunction;
|
||||||
|
|
||||||
@ -27,7 +28,7 @@ public:
|
|||||||
GameInternal();
|
GameInternal();
|
||||||
~GameInternal();
|
~GameInternal();
|
||||||
|
|
||||||
SDL_AppResult init(const char* title, int xpos, int ypos, int width, int height, bool fullscreen);
|
SDL_AppResult init();
|
||||||
|
|
||||||
void handleEvents();
|
void handleEvents();
|
||||||
void update(Uint64 frameTime);
|
void update(Uint64 frameTime);
|
||||||
@ -48,6 +49,8 @@ public:
|
|||||||
RenderManager renderManager;
|
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
|
||||||
|
|
||||||
|
ConfigLoader* config;
|
||||||
|
|
||||||
std::vector<Entity*>& tiles;
|
std::vector<Entity*>& tiles;
|
||||||
std::vector<Entity*>& players;
|
std::vector<Entity*>& players;
|
||||||
std::vector<Entity*>& projectiles;
|
std::vector<Entity*>& projectiles;
|
||||||
|
|||||||
BIN
internalAssets/iconImage.bmp
Normal file
BIN
internalAssets/iconImage.bmp
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 3.1 KiB |
@ -14,6 +14,7 @@
|
|||||||
#include "Vector2D.h"
|
#include "Vector2D.h"
|
||||||
#include "PowerupComponent.h"
|
#include "PowerupComponent.h"
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
|
#include <VEGO.h>
|
||||||
|
|
||||||
#include "Textures.h"
|
#include "Textures.h"
|
||||||
|
|
||||||
@ -76,8 +77,8 @@ Vector2D AssetManager::calculateSpawnPosition()
|
|||||||
{
|
{
|
||||||
SDL_Rect spawnRect;
|
SDL_Rect spawnRect;
|
||||||
spawnRect.h = spawnRect.w = 32;
|
spawnRect.h = spawnRect.w = 32;
|
||||||
spawnRect.x = rand() % (SCREEN_SIZE_WIDTH - spawnRect.w);
|
spawnRect.x = rand() % (VEGO_Game().config->getFinalConfig().at("width").get<int>() - spawnRect.w);
|
||||||
spawnRect.y = rand() % (SCREEN_SIZE_HEIGHT - spawnRect.h);
|
spawnRect.y = rand() % (VEGO_Game().config->getFinalConfig().at("height").get<int>() - spawnRect.h);
|
||||||
conflict = false;
|
conflict = false;
|
||||||
for (auto cc : this->man->getGame()->collisionHandler->getColliders({ Entity::GroupLabel::MAPTILES }))
|
for (auto cc : this->man->getGame()->collisionHandler->getColliders({ Entity::GroupLabel::MAPTILES }))
|
||||||
{
|
{
|
||||||
|
|||||||
@ -10,6 +10,7 @@
|
|||||||
#include <bitset>
|
#include <bitset>
|
||||||
#include <cstdio>
|
#include <cstdio>
|
||||||
#include <memory>
|
#include <memory>
|
||||||
|
#include <VEGO.h>
|
||||||
|
|
||||||
IntersectionBitSet CollisionHandler::getIntersection(Entity* entityA, Entity* entityB, Vector2D posModA, Vector2D posModB)
|
IntersectionBitSet CollisionHandler::getIntersection(Entity* entityA, Entity* entityB, Vector2D posModA, Vector2D posModB)
|
||||||
{
|
{
|
||||||
@ -66,20 +67,20 @@ IntersectionBitSet CollisionHandler::getIntersectionWithBounds(Entity* entity, V
|
|||||||
|
|
||||||
// all 4 directions and both sides to allow checking for fully out of bounds
|
// all 4 directions and both sides to allow checking for fully out of bounds
|
||||||
if (collider->x + posMod.x < 0 ||
|
if (collider->x + posMod.x < 0 ||
|
||||||
collider->x + posMod.x > SCREEN_SIZE_WIDTH) {
|
collider->x + posMod.x > VEGO_Game().config->getFinalConfig().at("width")) {
|
||||||
intersections.set((size_t) Direction::LEFT);
|
intersections.set((size_t) Direction::LEFT);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (collider->x + collider->w + posMod.x < 0 ||
|
if (collider->x + collider->w + posMod.x < 0 ||
|
||||||
collider->x + collider->w + posMod.x > SCREEN_SIZE_WIDTH)
|
collider->x + collider->w + posMod.x > VEGO_Game().config->getFinalConfig().at("width"))
|
||||||
intersections.set((size_t) Direction::RIGHT);
|
intersections.set((size_t) Direction::RIGHT);
|
||||||
|
|
||||||
if (collider->y + posMod.y < 0 ||
|
if (collider->y + posMod.y < 0 ||
|
||||||
collider->y + posMod.y > SCREEN_SIZE_HEIGHT)
|
collider->y + posMod.y > VEGO_Game().config->getFinalConfig().at("height"))
|
||||||
intersections.set((size_t) Direction::UP);
|
intersections.set((size_t) Direction::UP);
|
||||||
|
|
||||||
if (collider->y + collider->h + posMod.y < 0 ||
|
if (collider->y + collider->h + posMod.y < 0 ||
|
||||||
collider->y + collider->h + posMod.y > SCREEN_SIZE_HEIGHT)
|
collider->y + collider->h + posMod.y > VEGO_Game().config->getFinalConfig().at("height"))
|
||||||
intersections.set((size_t) Direction::DOWN);
|
intersections.set((size_t) Direction::DOWN);
|
||||||
|
|
||||||
return intersections;
|
return intersections;
|
||||||
|
|||||||
52
src/ConfigLoader.cpp
Normal file
52
src/ConfigLoader.cpp
Normal file
@ -0,0 +1,52 @@
|
|||||||
|
#include "ConfigLoader.h"
|
||||||
|
|
||||||
|
#include <fstream>
|
||||||
|
|
||||||
|
ConfigLoader::ConfigLoader() {
|
||||||
|
//TODO: look into adaptive paths for better handling as this requires the implemented game
|
||||||
|
// to have ./engine in the root folder
|
||||||
|
baseConfig = loadConfigFromJSON("./engine/config.json");
|
||||||
|
}
|
||||||
|
|
||||||
|
ConfigLoader::~ConfigLoader() {}
|
||||||
|
|
||||||
|
void ConfigLoader::init() {
|
||||||
|
if (!customConfig.has_value()) {
|
||||||
|
finalConfig = baseConfig;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
finalConfig = mergeConfigs(baseConfig, customConfig.value());
|
||||||
|
}
|
||||||
|
|
||||||
|
json ConfigLoader::loadConfigFromJSON(const std::string& path) {
|
||||||
|
std::ifstream config_file(path);
|
||||||
|
json config;
|
||||||
|
|
||||||
|
if (!config_file.is_open()) {
|
||||||
|
throw std::runtime_error(std::string("Could not load config file at: " + path));
|
||||||
|
}
|
||||||
|
|
||||||
|
config_file >> config;
|
||||||
|
return config;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
void ConfigLoader::setCustomConfig(const std::optional<std::string>& path) {
|
||||||
|
if (path.has_value()) {
|
||||||
|
customConfig = loadConfigFromJSON(path.value());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
json ConfigLoader::mergeConfigs(json baseConfig, json customConfig) {
|
||||||
|
for (auto& entry : customConfig.items()) {
|
||||||
|
baseConfig[entry.key()] = entry.value();
|
||||||
|
}
|
||||||
|
return baseConfig;
|
||||||
|
}
|
||||||
|
|
||||||
|
json ConfigLoader::getFinalConfig() {
|
||||||
|
return finalConfig;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
@ -15,6 +15,8 @@
|
|||||||
|
|
||||||
#include <VEGO.h>
|
#include <VEGO.h>
|
||||||
|
|
||||||
|
#include "ConfigLoader.h"
|
||||||
|
|
||||||
GameInternal::GameInternal() :
|
GameInternal::GameInternal() :
|
||||||
manager(this),
|
manager(this),
|
||||||
renderManager(),
|
renderManager(),
|
||||||
@ -27,15 +29,23 @@ GameInternal::GameInternal() :
|
|||||||
|
|
||||||
GameInternal::~GameInternal() = default;
|
GameInternal::~GameInternal() = default;
|
||||||
|
|
||||||
SDL_AppResult GameInternal::init(const char* title, int xpos, int ypos, int width, int height, bool fullscreen)
|
SDL_AppResult GameInternal::init()
|
||||||
{
|
{
|
||||||
|
config = new ConfigLoader;
|
||||||
|
|
||||||
|
this->gameInstance = GameFactory::instance().create(this);
|
||||||
|
config->setCustomConfig(this->gameInstance->getConfigFilePath());
|
||||||
|
config->init();
|
||||||
|
|
||||||
|
json finalConfig = config->getFinalConfig();
|
||||||
|
|
||||||
GameInternal::assets = new AssetManager(&manager);
|
GameInternal::assets = new AssetManager(&manager);
|
||||||
GameInternal::textureManager = new TextureManager(&manager);
|
GameInternal::textureManager = new TextureManager(&manager);
|
||||||
GameInternal::soundManager = new SoundManager();
|
GameInternal::soundManager = new SoundManager();
|
||||||
GameInternal::collisionHandler = new CollisionHandler(manager); // why does this use a referrence, but AssetManager a pointer?
|
GameInternal::collisionHandler = new CollisionHandler(manager); // why does this use a referrence, but AssetManager a pointer?
|
||||||
|
|
||||||
int flags = 0;
|
int flags = 0;
|
||||||
if (fullscreen)
|
if (finalConfig.at("fullscreen"))
|
||||||
{
|
{
|
||||||
flags = SDL_WINDOW_FULLSCREEN;
|
flags = SDL_WINDOW_FULLSCREEN;
|
||||||
}
|
}
|
||||||
@ -52,7 +62,7 @@ SDL_AppResult GameInternal::init(const char* title, int xpos, int ypos, int widt
|
|||||||
return SDL_APP_FAILURE;
|
return SDL_APP_FAILURE;
|
||||||
}
|
}
|
||||||
|
|
||||||
window = SDL_CreateWindow(title, width, height, flags);
|
window = SDL_CreateWindow(finalConfig.at("title").get<std::string>().c_str(), finalConfig.at("width"), finalConfig.at("height"), flags);
|
||||||
if (!window)
|
if (!window)
|
||||||
{
|
{
|
||||||
std::cout << "ERROR: Window couldnt be created! " << SDL_GetError() << std::endl;
|
std::cout << "ERROR: Window couldnt be created! " << SDL_GetError() << std::endl;
|
||||||
@ -62,7 +72,7 @@ SDL_AppResult GameInternal::init(const char* title, int xpos, int ypos, int widt
|
|||||||
|
|
||||||
// bad
|
// bad
|
||||||
SDL_Surface* icon;
|
SDL_Surface* icon;
|
||||||
if((icon = SDL_LoadBMP("assets/iconImage.bmp")))
|
if((icon = SDL_LoadBMP(finalConfig.at("icon").get<std::string>().c_str())))
|
||||||
{
|
{
|
||||||
SDL_SetWindowIcon(window, icon);
|
SDL_SetWindowIcon(window, icon);
|
||||||
}
|
}
|
||||||
@ -95,7 +105,6 @@ SDL_AppResult GameInternal::init(const char* title, int xpos, int ypos, int widt
|
|||||||
// loading music
|
// loading music
|
||||||
// assets->addMusic("background_music", "assets/sound/background_music.mp3");
|
// assets->addMusic("background_music", "assets/sound/background_music.mp3");
|
||||||
|
|
||||||
this->gameInstance = GameFactory::instance().create(this);
|
|
||||||
this->gameInstance->init();
|
this->gameInstance->init();
|
||||||
|
|
||||||
return SDL_APP_CONTINUE;
|
return SDL_APP_CONTINUE;
|
||||||
|
|||||||
@ -19,7 +19,7 @@ SDL_AppResult SDL_AppInit(void **appstate, int argc, char **argv) {
|
|||||||
|
|
||||||
*appstate = vego::game = new GameInternal();
|
*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);
|
return vego::game->init();
|
||||||
}
|
}
|
||||||
|
|
||||||
SDL_AppResult SDL_AppIterate(void *appstate) {
|
SDL_AppResult SDL_AppIterate(void *appstate) {
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user