From 1bf565b54300120ed3c9d48359e22412e77fc22b Mon Sep 17 00:00:00 2001 From: ShadesAndGrays Date: Thu, 9 Jul 2026 12:30:40 +0100 Subject: [PATCH] second commit --- .gitignore | 10 +- .vscode/imgui.ini | 16 +- .vscode/launch.json | 67 +++--- imgui.ini | 46 +++-- makefile | 217 ++++++++++---------- src/asset.cpp | 314 +++++++++++++++++------------ src/imgui.ini | 16 +- src/include/asset/asset.hpp | 108 +++++----- src/include/asset/raylib_asset.hpp | 62 +++--- src/include/components.hpp | 36 ++-- src/include/movement.hpp | 8 +- src/include/player.hpp | 12 +- src/include/render.hpp | 10 +- src/include/scene.hpp | 172 ++++++++-------- src/main.cpp | 168 +++++++-------- src/movement.cpp | 28 +-- src/player.cpp | 84 ++++---- src/render.cpp | 36 ++-- src/scenes/game_play_scene.cpp | 86 +++++--- src/scenes/menu_scene.cpp | 112 +++++----- src/scenes/scene.cpp | 78 +++---- src/test.cpp | 75 +++++++ 22 files changed, 969 insertions(+), 792 deletions(-) create mode 100644 src/test.cpp diff --git a/.gitignore b/.gitignore index a25d4ad..6fd54a1 100644 --- a/.gitignore +++ b/.gitignore @@ -1,5 +1,5 @@ -assets/ -bin/ -debug/ -external/ -compile_commands.json +assets/ +bin/ +debug/ +external/ +compile_commands.json diff --git a/.vscode/imgui.ini b/.vscode/imgui.ini index 8f5ce5d..ff52523 100644 --- a/.vscode/imgui.ini +++ b/.vscode/imgui.ini @@ -1,8 +1,8 @@ -[Window][Debug##Default] -Pos=60,60 -Size=400,400 - -[Window][VBoxContainer] -Pos=540,275 -Size=200,250 - +[Window][Debug##Default] +Pos=60,60 +Size=400,400 + +[Window][VBoxContainer] +Pos=540,275 +Size=200,250 + diff --git a/.vscode/launch.json b/.vscode/launch.json index 7631b4c..afaa751 100644 --- a/.vscode/launch.json +++ b/.vscode/launch.json @@ -1,34 +1,35 @@ -{ - // Use IntelliSense to learn about possible attributes. - // Hover to view descriptions of existing attributes. - // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387 - "version": "0.2.0", - "configurations": [ - { - "name": "(gdb) Launch", - "type": "cppdbg", - "request": "launch", - "program": "${workspaceFolder}/debug/d.exe", - "args": [], - "stopAtEntry": false, - "cwd": "${fileDirname}", - "environment": [], - "externalConsole": false, - "MIMode": "gdb", - "miDebuggerPath": "gdb", - "setupCommands": [ - { - "description": "Enable pretty-printing for gdb", - "text": "-enable-pretty-printing", - "ignoreFailures": true - }, - { - "description": "Set Disassembly Flavor to Intel", - "text": "-gdb-set disassembly-flavor intel", - "ignoreFailures": true - } - ] - } - - ] +{ + // Use IntelliSense to learn about possible attributes. + // Hover to view descriptions of existing attributes. + // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387 + "version": "0.2.0", + "configurations": [ + { + "name": "(gdb) Launch", + "type": "cppdbg", + "request": "launch", + "program": "${workspaceFolder}/debug/d.exe", + "args": [], + "stopAtEntry": false, + // "cwd": "${fileDirname}", + "cwd": "${workspaceFolder}", + "environment": [], + "externalConsole": false, + "MIMode": "gdb", + "miDebuggerPath": "gdb", + "setupCommands": [ + { + "description": "Enable pretty-printing for gdb", + "text": "-enable-pretty-printing", + "ignoreFailures": true + }, + { + "description": "Set Disassembly Flavor to Intel", + "text": "-gdb-set disassembly-flavor intel", + "ignoreFailures": true + } + ] + } + + ] } \ No newline at end of file diff --git a/imgui.ini b/imgui.ini index b49fefd..09c68b7 100644 --- a/imgui.ini +++ b/imgui.ini @@ -1,21 +1,25 @@ -[Window][Debug##Default] -Pos=143,124 -Size=400,400 - -[Window][CenterWindow] -Pos=0,0 -Size=1280,800 -Collapsed=1 - -[Window][VBoxContainer] -Pos=540,275 -Size=200,250 - -[Window][Box] -Pos=156,64 -Size=180,105 - -[Window][Debug] -Pos=882,174 -Size=320,170 - +[Window][Debug##Default] +Pos=952,-6 +Size=400,400 + +[Window][CenterWindow] +Pos=0,0 +Size=1280,800 +Collapsed=1 + +[Window][VBoxContainer] +Pos=540,275 +Size=200,250 + +[Window][Box] +Pos=156,64 +Size=180,105 + +[Window][Debug] +Pos=882,174 +Size=320,170 + +[Window][Exit] +Pos=1132,60 +Size=40,40 + diff --git a/makefile b/makefile index 0f7cf36..2ed6890 100644 --- a/makefile +++ b/makefile @@ -1,103 +1,114 @@ -.PHONY: all build-gui build-game run - -CXX = g++ - -LDFLAGS = -L./external/raylib/lib \ - -L./external/box2d/build/src \ - -L./external/asset_packer/bin \ - -LIBS = -lraylib -lwinmm -lgdi32 -lbox2d -lassetpacker - -CXXFLAGS = -std=c++20 -O2 \ - -MMD -MP - -DEBUGFLAGS = -std=c++20 -g -Og - -INCLUDES = -I./external/raylib/include \ - -I./external/entt/single_include \ - -I./src/include \ - -I./external/imgui \ - -I./external/rlImGui \ - -I./external/box2d/include \ - -I./external/nhlohmann_json_3.12.0/ \ - -I./external/asset_packer/bin/include \ - -IMGUI_SRCS = ./external/imgui/imgui.cpp \ - ./external/imgui/imgui_demo.cpp \ - ./external/imgui/imgui_draw.cpp \ - ./external/imgui/imgui_tables.cpp \ - ./external/imgui/imgui_widgets.cpp \ - -IMGUI_BACKEND = ./external/rlImGui/rlImGui.cpp - - - -SRC_FILES = ./src/main.cpp \ - ./src/asset.cpp \ - ./src/movement.cpp \ - ./src/player.cpp \ - ./src/render.cpp \ - ./src/scenes/scene.cpp \ - ./src/scenes/game_play_scene.cpp \ - ./src/scenes/menu_scene.cpp - -IMGUI_OBJS = $(patsubst ./external/imgui/%.cpp,./bin/%.o,$(IMGUI_SRCS)) -IMGUI_BACKEND_OBJS = $(patsubst ./external/rlImGui/%.cpp,./bin/%.o,$(IMGUI_BACKEND)) -OBJS_FILES = $(patsubst ./src/%.cpp,./bin/%.o,$(SRC_FILES)) - -DEP_FILES = $(patsubst ./bin/%.o,./bin/%.d,$(OBJS_FILES)) - - -all: build-game run - -./bin/%.o: ./external/imgui/%.cpp - @mkdir -p bin - $(CXX) $(CXXFLAGS) $(INCLUDES) -c $< -o $@ - -./bin/%.o: ./external/rlImGui/%.cpp - @mkdir -p bin - $(CXX) $(CXXFLAGS) $(INCLUDES) -c $< -o $@ - -./bin/%.o: ./src/%.cpp - @mkdir -p bin - @mkdir -p $(dir $@) - $(CXX) $(CXXFLAGS) $(INCLUDES) -c $< -o $@ - -compile_commands.json: clean - @echo "Generating compile_commands.json..." - bear -- $(MAKE) - --include $(DEP_FILES) - -build-gui: $(IMGUI_OBJS) - -build-gui-backend: $(IMGUI_BACKEND_OBJS) - -build-core: $(OBJS_FILES) - -build-game: $(OBJS_FILES) $(IMGUI_OBJS) $(IMGUI_BACKEND_OBJS) - @mkdir -p bin - $(CXX) $(CXXFLAGS) $(OBJS_FILES) $(IMGUI_OBJS) $(IMGUI_BACKEND_OBJS) $(LDFLAGS) $(LIBS) -o ./bin/main.exe - cp ./assets ./bin -rf - -build-debug: - @mkdir -p debug - $(CXX) $(DEBUGFLAGS) $(INCLUDES) $(SRC_FILES) $(IMGUI_SRCS) $(IMGUI_BACKEND) $(LDFLAGS) $(LIBS) -o ./debug/d.exe - cp ./assets ./debug -rf - - - -clean: - @rm -r -f ./bin - @rm -f compile_commands.json - -clean-debug: - @rm -r -f ./debug - -clean-all: clean clean-debug - -run: - ./bin/main.exe - -run-debug: - ./debug/d.exe +.PHONY: all build-gui build-game run + +CXX = g++ + +LDFLAGS = -L./external/raylib/lib \ + -L./external/box2d/build/src \ + -L./external/asset_packer/bin \ + +LIBS = -lraylib -lwinmm -lgdi32 -lbox2d -lassetpacker + +CXXFLAGS = -std=c++20 -O2 \ + -MMD -MP + +DEBUGFLAGS = -std=c++20 -g -O0 + +INCLUDES = -I./external/raylib/include \ + -I./external/entt/single_include \ + -I./src/include \ + -I./external/imgui \ + -I./external/rlImGui \ + -I./external/box2d/include \ + -I./external/nhlohmann_json_3.12.0/ \ + -I./external/asset_packer/bin/include \ + +IMGUI_SRCS = ./external/imgui/imgui.cpp \ + ./external/imgui/imgui_demo.cpp \ + ./external/imgui/imgui_draw.cpp \ + ./external/imgui/imgui_tables.cpp \ + ./external/imgui/imgui_widgets.cpp \ + +IMGUI_BACKEND = ./external/rlImGui/rlImGui.cpp + + + +SRC_FILES = ./src/asset.cpp \ + ./src/movement.cpp \ + ./src/player.cpp \ + ./src/render.cpp \ + ./src/scenes/scene.cpp \ + ./src/scenes/game_play_scene.cpp \ + ./src/scenes/menu_scene.cpp + +ENTRY_POINT = ./src/main.cpp +TEST_ENTRY_POINT = ./src/test.cpp + +IMGUI_OBJS = $(patsubst ./external/imgui/%.cpp,./bin/%.o,$(IMGUI_SRCS)) +IMGUI_BACKEND_OBJS = $(patsubst ./external/rlImGui/%.cpp,./bin/%.o,$(IMGUI_BACKEND)) +OBJS_FILES = $(patsubst ./src/%.cpp,./bin/%.o,$(SRC_FILES)) + +DEP_FILES = $(patsubst ./bin/%.o,./bin/%.d,$(OBJS_FILES)) + + +all: build-game run + +./bin/%.o: ./external/imgui/%.cpp + @mkdir -p bin + $(CXX) $(CXXFLAGS) $(INCLUDES) -c $< -o $@ + +./bin/%.o: ./external/rlImGui/%.cpp + @mkdir -p bin + $(CXX) $(CXXFLAGS) $(INCLUDES) -c $< -o $@ + +./bin/%.o: ./src/%.cpp + @mkdir -p bin + @mkdir -p $(dir $@) + $(CXX) $(CXXFLAGS) $(INCLUDES) -c $< -o $@ + +compile_commands.json: clean + @echo "Generating compile_commands.json..." + bear -- $(MAKE) + +-include $(DEP_FILES) + +build-gui: $(IMGUI_OBJS) + +build-gui-backend: $(IMGUI_BACKEND_OBJS) + +build-core: $(OBJS_FILES) + +build-game: $(OBJS_FILES) $(IMGUI_OBJS) $(IMGUI_BACKEND_OBJS) + @mkdir -p bin + $(CXX) $(CXXFLAGS) $(INCLUDES) $(ENTRY_POINT) $(OBJS_FILES) $(IMGUI_OBJS) $(IMGUI_BACKEND_OBJS) $(LDFLAGS) $(LIBS) -o ./bin/main.exe + cp ./assets ./bin -rf + +build-test: $(OBJS_FILES) $(IMGUI_OBJS) $(IMGUI_BACKEND_OBJS) + @mkdir -p bin + $(CXX) $(CXXFLAGS) $(INCLUDES) $(TEST_ENTRY_POINT) $(OBJS_FILES) $(IMGUI_OBJS) $(IMGUI_BACKEND_OBJS) $(LDFLAGS) $(LIBS) -o ./bin/test.exe + cp ./assets ./bin -rf + + +build-debug: + @mkdir -p debug + $(CXX) $(DEBUGFLAGS) $(INCLUDES) $(ENTRY_POINT) $(SRC_FILES) $(IMGUI_OBJS) $(IMGUI_BACKEND_OBJS) $(LDFLAGS) $(LIBS) -o ./debug/d.exe + cp ./data.dat ./debug -rf + + + +clean: + @rm -r -f ./bin + @rm -f compile_commands.json + +clean-debug: + @rm -r -f ./debug + +clean-all: clean clean-debug + +run: + ./bin/main.exe + +run-debug: + ./debug/d.exe + +run-test: build-test + ./bin/test.exe diff --git a/src/asset.cpp b/src/asset.cpp index 47e5f85..2d17898 100644 --- a/src/asset.cpp +++ b/src/asset.cpp @@ -1,132 +1,182 @@ - -#include - -#include -#include - -#include - -#include -#include -#include -#include - -namespace fs = std::filesystem; - -AssetLoader::AssetLoader(std::string asset_data) : asset_reader(asset_data){ - if (!asset_reader.load_index()){ - std::cerr << "Failed to load assets!!!" << std::endl; - } -} - -AssetReader AssetLoader::get_reader(){ - return asset_reader; -} - -RaylibAssetLoader::RaylibAssetLoader(std::string asset_data) : AssetLoader(asset_data) { - -} - -RaylibAssetLoader::~RaylibAssetLoader(){ - unload_all_assets(); -} - - -void RaylibAssetLoader::load_asset(std::string path){ - fs::path file_path(path); - Asset new_asset; - new_asset.name = file_path.filename().string() ; - - std::cout << file_path.extension() << std::endl; - - if (file_path.extension() == ".png"){ - auto texture_data = get_reader().extract_asset(path); - Image img = LoadImageFromMemory(".png", texture_data.data(), texture_data.size()); - auto texture = LoadTextureFromImage(img); - UnloadImage(img); - new_asset.data = ImageData { new Texture(texture)}; - - - }else if(file_path.extension() == ".ogg"){ - - }else if(file_path.extension() == ".wav"){ - - }else if(file_path.extension() == ".json"){ - - } - - - assets[path] = new_asset; - -} - -void RaylibAssetLoader::unload_asset(std::string path){ - Asset asset = assets[path]; - - std::visit([](auto&& arg) { - using T = std::decay_t; - - if constexpr (std::is_same_v) { - - UnloadTexture(*((Texture*) arg.texture)); - delete (Texture*) arg.texture; - } - else if constexpr (std::is_same_v) { - UnloadSound(*((Sound*) arg.sound)); - delete (Sound*) arg.sound; - } - else if constexpr (std::is_same_v) { - UnloadMusicStream(*((Music*) arg.music)); - delete (Music*) arg.music; - } - // JsonData requires no manual step because std::string cleans itself up! - }, asset.data); - -} - - -void RaylibAssetLoader::unload_all_assets(){ - for (auto [path,_] : assets){ - unload_asset(path); - } -} - -Asset RaylibAssetLoader::get_asset(std::string path){ - if (assets.contains(path)){ - return assets[path]; - }else{ - return {}; - } -} - - -Texture* RaylibAssetLoader::get_texture(std::string path){ - Asset asset = get_asset(path); - if (auto* ptr = std::get_if(&asset.data)) { - return (Texture*)ptr->texture; - } - return nullptr; -} - -Sound* RaylibAssetLoader::get_sound(std::string path){ - Asset asset = get_asset(path); - if (auto* ptr = std::get_if(&asset.data)) { - return (Sound*)ptr->sound; - } - return nullptr; -} -Music* RaylibAssetLoader::get_music(std::string path){ - Asset asset = get_asset(path); - if (auto* ptr = std::get_if(&asset.data)) { - return (Music*)ptr->music; - } - return nullptr; -} - -std::string RaylibAssetLoader::get_json(std::string path){ - Asset asset = get_asset(path); - if (auto* ptr = std::get_if(&asset.data)) { - return ptr->json; - } - return "{}"; -} + +#include + +#include +#include + +#include + +#include +#include +#include +#include + +namespace fs = std::filesystem; + +// Helper to determine if the asset path implies streaming music +bool should_stream_as_music(const std::filesystem::path& asset_path) { + for (const auto& part : asset_path) { + if (part == "music" || part == "bgm") { + return true; + } + } + return false; +} + +AssetLoader::AssetLoader(std::string asset_data) : asset_reader(asset_data){ + if (!asset_reader.load_index()){ + std::cerr << "Failed to load assets!!!" << std::endl; + } +} + +AssetReader AssetLoader::get_reader(){ + return asset_reader; +} + +RaylibAssetLoader::RaylibAssetLoader(std::string asset_data) : AssetLoader(asset_data) { + +} + +RaylibAssetLoader::~RaylibAssetLoader(){ + unload_all_assets(); +} + + + +void RaylibAssetLoader::load_asset(std::string path){ + fs::path file_path(path); + Asset new_asset; + new_asset.name = file_path.filename().string() ; + + // std::cout << file_path.extension() << std::endl; + auto bytes = get_reader().extract_asset(path); + + if (file_path.extension() == ".png"){ + Image img = LoadImageFromMemory(".png", bytes.data(), bytes.size()); + auto texture = LoadTextureFromImage(img); + UnloadImage(img); + new_asset.data = ImageData { new Texture(texture)}; + + + }else if(file_path.extension() == ".ogg"){ + + if (should_stream_as_music(file_path)){ + auto music = LoadMusicStreamFromMemory(".ogg",bytes.data(),bytes.size()); + new_asset.data = MusicData{new Music(music)}; + }else{ + auto wave = LoadWaveFromMemory(".ogg", bytes.data(),bytes.size()); + auto sound = LoadSoundFromWave(wave); + UnloadWave(wave); + new_asset.data = SoundData{new Sound(sound)}; + } + + } + else if(file_path.extension() == ".mp3"){ + if (should_stream_as_music(file_path)){ + auto music = LoadMusicStreamFromMemory(".mp3",bytes.data(),bytes.size()); + new_asset.data = MusicData{new Music(music)}; + }else{ + auto wave = LoadWaveFromMemory(".mp3", bytes.data(),bytes.size()); + auto sound = LoadSoundFromWave(wave); + UnloadWave(wave); + new_asset.data = SoundData{new Sound(sound)}; + } + + } + else if(file_path.extension() == ".wav"){ + if (should_stream_as_music(file_path)){ + auto music = LoadMusicStreamFromMemory(".wav",bytes.data(),bytes.size()); + new_asset.data = MusicData{new Music(music)}; + }else{ + auto wave = LoadWaveFromMemory(".wav", bytes.data(),bytes.size()); + auto sound = LoadSoundFromWave(wave); + UnloadWave(wave); + new_asset.data = SoundData{new Sound(sound)}; + } + } + + else if(file_path.extension() == ".json"){ + auto json = std::string(bytes.begin(),bytes.end()); + new_asset.data = JsonData {json}; + } + + assets[path] = new_asset; +} + +void RaylibAssetLoader::unload_asset(std::string path){ + Asset asset = assets[path]; + + std::visit([](auto&& arg) { + using T = std::decay_t; + + if constexpr (std::is_same_v) { + Texture* tex = static_cast(arg.texture); + UnloadTexture(*tex); + delete tex; + arg.texture = nullptr; + } + else if constexpr (std::is_same_v) { + Sound* sound = static_cast(arg.sound); + UnloadSound(*sound); + delete sound; + arg.sound = nullptr; + } + else if constexpr (std::is_same_v) { + Music* music = static_cast(arg.music); + UnloadMusicStream(*music); + delete music; + arg.music = nullptr; + } + // JsonData requires no manual step because std::string cleans itself up! + }, asset.data); + + assets.erase(path); + +} + + +void RaylibAssetLoader::unload_all_assets(){ + for (auto [path,_] : assets){ + unload_asset(path); + } +} + +Asset RaylibAssetLoader::get_asset(std::string path){ + if (assets.contains(path)){ + return assets[path]; + }else{ + return {}; + } +} + + +Texture* RaylibAssetLoader::get_texture(std::string path){ + Asset asset = get_asset(path); + if (auto* ptr = std::get_if(&asset.data)) { + return (Texture*)ptr->texture; + } + return nullptr; +} + +Sound* RaylibAssetLoader::get_sound(std::string path){ + Asset asset = get_asset(path); + if (auto* ptr = std::get_if(&asset.data)) { + return (Sound*)ptr->sound; + } + return nullptr; +} +Music* RaylibAssetLoader::get_music(std::string path){ + Asset asset = get_asset(path); + if (auto* ptr = std::get_if(&asset.data)) { + return (Music*)ptr->music; + } + return nullptr; +} + +std::string RaylibAssetLoader::get_json(std::string path){ + Asset asset = get_asset(path); + if (auto* ptr = std::get_if(&asset.data)) { + return ptr->json; + } + return "{}"; +} diff --git a/src/imgui.ini b/src/imgui.ini index 8f5ce5d..ff52523 100644 --- a/src/imgui.ini +++ b/src/imgui.ini @@ -1,8 +1,8 @@ -[Window][Debug##Default] -Pos=60,60 -Size=400,400 - -[Window][VBoxContainer] -Pos=540,275 -Size=200,250 - +[Window][Debug##Default] +Pos=60,60 +Size=400,400 + +[Window][VBoxContainer] +Pos=540,275 +Size=200,250 + diff --git a/src/include/asset/asset.hpp b/src/include/asset/asset.hpp index a58e544..9ddab55 100644 --- a/src/include/asset/asset.hpp +++ b/src/include/asset/asset.hpp @@ -1,54 +1,54 @@ -#pragma once -#include - -#include -#include -#include - - - - - - -struct ImageData { - void* texture; -}; - -struct SoundData { - void* sound; -}; - -struct MusicData { - void* music; -}; - -struct JsonData { - std::string json; -}; - -struct Asset { - std::string name; - std::variant data; -}; - - -class AssetLoader{ - - private: - AssetReader asset_reader; - - protected: - std::unordered_map assets; - virtual void load_all_assets(std::string data_path) = 0; - virtual void unload_all_assets() = 0; - AssetReader get_reader(); - - public: - AssetLoader(std::string asset_data); - ~AssetLoader() = default; - virtual void load_asset(std::string path) = 0; - virtual void unload_asset(std::string path) = 0; - virtual Asset get_asset(std::string path) = 0;; - -}; - +#pragma once +#include + +#include +#include +#include + + + + + + +struct ImageData { + void* texture; +}; + +struct SoundData { + void* sound; +}; + +struct MusicData { + void* music; +}; + +struct JsonData { + std::string json; +}; + +struct Asset { + std::string name; + std::variant data; +}; + + +class AssetLoader{ + + private: + AssetReader asset_reader; + + protected: + std::unordered_map assets; + virtual void load_all_assets(std::string data_path) = 0; + virtual void unload_all_assets() = 0; + AssetReader get_reader(); + + public: + AssetLoader(std::string asset_data); + ~AssetLoader() = default; + virtual void load_asset(std::string path) = 0; + virtual void unload_asset(std::string path) = 0; + virtual Asset get_asset(std::string path) = 0;; + +}; + diff --git a/src/include/asset/raylib_asset.hpp b/src/include/asset/raylib_asset.hpp index 1b80d59..ac73934 100644 --- a/src/include/asset/raylib_asset.hpp +++ b/src/include/asset/raylib_asset.hpp @@ -1,31 +1,31 @@ -#pragma once -#include "asset_packer/asset.hpp" -#include -#include - -class RaylibAssetLoader : AssetLoader{ - protected: - void load_all_assets(std::string asset_list) override {}; - void unload_all_assets() override; - - public: - RaylibAssetLoader(std::string asset_data = "data.dat"); - ~RaylibAssetLoader(); - - - void load_asset(std::string path) override; - void unload_asset(std::string path) override; - Asset get_asset(std::string path) override; - - Texture* get_texture(std::string path); - Sound* get_sound(std::string path); - Music* get_music(std::string path); - std::string get_json(std::string path); - -}; - - -extern RaylibAssetLoader asset_loader; - - - +#pragma once +#include "asset_packer/asset.hpp" +#include +#include + +class RaylibAssetLoader : AssetLoader{ + protected: + void load_all_assets(std::string asset_list) override {}; + void unload_all_assets() override; + + public: + RaylibAssetLoader(std::string asset_data = "data.dat"); + ~RaylibAssetLoader(); + + + void load_asset(std::string path) override; + void unload_asset(std::string path) override; + Asset get_asset(std::string path) override; + + Texture* get_texture(std::string path); + Sound* get_sound(std::string path); + Music* get_music(std::string path); + std::string get_json(std::string path); + +}; + + +extern RaylibAssetLoader asset_loader; + + + diff --git a/src/include/components.hpp b/src/include/components.hpp index 678bfb8..c7be5c1 100644 --- a/src/include/components.hpp +++ b/src/include/components.hpp @@ -1,18 +1,18 @@ -#pragma once -#include - -struct Position{ - float x; - float y; -}; - -struct Velocity{ - float dx; - float dy; -}; - -struct Player{}; - -struct Sprite{ - Texture *texture; -}; +#pragma once +#include + +struct Position{ + float x; + float y; +}; + +struct Velocity{ + float dx; + float dy; +}; + +struct Player{}; + +struct Sprite{ + Texture *texture; +}; diff --git a/src/include/movement.hpp b/src/include/movement.hpp index a927b95..0b26b6d 100644 --- a/src/include/movement.hpp +++ b/src/include/movement.hpp @@ -1,4 +1,4 @@ -#pragma once -#include - -void update_movement_system(entt::registry& registry, float dt); +#pragma once +#include + +void update_movement_system(entt::registry& registry, float dt); diff --git a/src/include/player.hpp b/src/include/player.hpp index 91a41a0..57a1db0 100644 --- a/src/include/player.hpp +++ b/src/include/player.hpp @@ -1,6 +1,6 @@ -#pragma once -#include -#include - -void create_player(entt::registry& registry,Texture* texture); -void update_player_system(entt::registry& registry,float dt); +#pragma once +#include +#include + +void create_player(entt::registry& registry,Texture* texture); +void update_player_system(entt::registry& registry,float dt); diff --git a/src/include/render.hpp b/src/include/render.hpp index 585b961..af27b16 100644 --- a/src/include/render.hpp +++ b/src/include/render.hpp @@ -1,5 +1,5 @@ -#pragma once -#include - -void update_render_system(entt::registry ®istry); - +#pragma once +#include + +void update_render_system(entt::registry ®istry); + diff --git a/src/include/scene.hpp b/src/include/scene.hpp index 6c50d02..64a7b20 100644 --- a/src/include/scene.hpp +++ b/src/include/scene.hpp @@ -1,86 +1,86 @@ -#pragma once - -#include - -#include - -enum class SceneType{ - MENU, - GAMEPLAY -}; - - -// Simple global data structure to hold the switch request -struct EngineState { - bool pendingSwitch = false; - SceneType nextScene; - - bool shouldQuit = false; - - void request_switch(SceneType type) { - nextScene = type; - pendingSwitch = true; - } - - void quit(){ - shouldQuit = true; - } -}; - -// Expose the global state to any file that includes scene.hpp -extern EngineState g_Engine; - - -class Scene{ - - public: - Scene() = default; - virtual ~Scene() = default; - - virtual void update(float dt) = 0; - virtual void render() = 0; - virtual void ui() = 0; -}; - -class GamePlayScene : public Scene{ - - private: - entt::registry registry; - - public: - GamePlayScene(); - ~GamePlayScene(); - - void update(float dt) override; - void render() override; - void ui() override; - -}; - -class MenuScene : public Scene{ - - public: - MenuScene(); - ~MenuScene() = default; - - void update(float dt) override; - void render() override; - void ui() override; - -}; - -class SceneManager{ - private: - Scene* current_scene = nullptr; - SceneType next_scene_type; - bool should_switch = false; - - public: - SceneManager(); - ~SceneManager(); - - void update(float dt); - void render(); - void ui(); - void process_scene_switch(); -}; +#pragma once + +#include + +#include + +enum class SceneType{ + MENU, + GAMEPLAY +}; + + +// Simple global data structure to hold the switch request +struct EngineState { + bool pendingSwitch = false; + SceneType nextScene; + + bool shouldQuit = false; + + void request_switch(SceneType type) { + nextScene = type; + pendingSwitch = true; + } + + void quit(){ + shouldQuit = true; + } +}; + +// Expose the global state to any file that includes scene.hpp +extern EngineState g_Engine; + + +class Scene{ + + public: + Scene() = default; + virtual ~Scene() = default; + + virtual void update(float dt) = 0; + virtual void render() = 0; + virtual void ui() = 0; +}; + +class GamePlayScene : public Scene{ + + private: + entt::registry registry; + + public: + GamePlayScene(); + ~GamePlayScene(); + + void update(float dt) override; + void render() override; + void ui() override; + +}; + +class MenuScene : public Scene{ + + public: + MenuScene(); + ~MenuScene() = default; + + void update(float dt) override; + void render() override; + void ui() override; + +}; + +class SceneManager{ + private: + Scene* current_scene = nullptr; + SceneType next_scene_type; + bool should_switch = false; + + public: + SceneManager(); + ~SceneManager(); + + void update(float dt); + void render(); + void ui(); + void process_scene_switch(); +}; diff --git a/src/main.cpp b/src/main.cpp index 2651858..0cb9607 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -1,84 +1,84 @@ -#include -#include -#include - -#include -#include -#include -#include -#include -#include -#include - -#include - - -// Globals -EngineState g_Engine; -RaylibAssetLoader asset_loader; - -int main(){ - -// - int screen_width = 1280; - int screen_height = 800; - - - - // initialized scene manager - SceneManager scene_manager; - - entt::registry registry; - - rlImGuiSetup(true); - - ImGuiStyle& style = ImGui::GetStyle(); - - // Rounded corners - style.WindowRounding = 6.0f; - style.ChildRounding = 4.0f; - style.FrameRounding = 4.0f; - style.PopupRounding = 4.0f; - style.ScrollbarRounding = 9.0f; - style.GrabRounding = 4.0f; - style.TabRounding = 4.0f; - - // Sizes and Spacing - style.WindowPadding = ImVec2(15.0f, 15.0f); - style.FramePadding = ImVec2(6.0f, 6.0f); - style.ItemSpacing = ImVec2(10.0f, 8.0f); - style.ScrollbarSize = 15.0f; - - SetTargetFPS(60); - SetTraceLogLevel(TraceLogLevel::LOG_NONE); - - InitWindow(screen_width, screen_height, "Bullet bird"); - SetWindowState(FLAG_WINDOW_RESIZABLE); - - - while (!WindowShouldClose() && !g_Engine.shouldQuit){ - - float dt = GetFrameTime(); - if (dt > 0.1f) dt = 0.1f; // cap dt just for now - - scene_manager.update(dt); - - BeginDrawing(); - ClearBackground(RAYWHITE); - - scene_manager.render(); - - rlImGuiBegin(); - scene_manager.ui(); - rlImGuiEnd(); - - EndDrawing(); - - scene_manager.process_scene_switch(); // !!! After scene_manager is done with the current loop - } - - CloseWindow(); - rlImGuiShutdown(); - return 0; -} - +#include +#include +#include + +#include +#include +#include +#include +#include +#include +#include + +#include + + +// Globals +EngineState g_Engine; +RaylibAssetLoader asset_loader; + +int main(){ + +// + int screen_width = 1280; + int screen_height = 800; + + + + // initialized scene manager + SceneManager scene_manager; + + entt::registry registry; + + rlImGuiSetup(true); + + ImGuiStyle& style = ImGui::GetStyle(); + + // Rounded corners + style.WindowRounding = 6.0f; + style.ChildRounding = 4.0f; + style.FrameRounding = 4.0f; + style.PopupRounding = 4.0f; + style.ScrollbarRounding = 9.0f; + style.GrabRounding = 4.0f; + style.TabRounding = 4.0f; + + // Sizes and Spacing + style.WindowPadding = ImVec2(15.0f, 15.0f); + style.FramePadding = ImVec2(6.0f, 6.0f); + style.ItemSpacing = ImVec2(10.0f, 8.0f); + style.ScrollbarSize = 15.0f; + + SetTargetFPS(60); + SetTraceLogLevel(TraceLogLevel::LOG_NONE); + + InitWindow(screen_width, screen_height, "Bullet bird"); + SetWindowState(FLAG_WINDOW_RESIZABLE); + + + while (!WindowShouldClose() && !g_Engine.shouldQuit){ + + float dt = GetFrameTime(); + if (dt > 0.1f) dt = 0.1f; // cap dt just for now + + scene_manager.update(dt); + + BeginDrawing(); + ClearBackground(RAYWHITE); + + scene_manager.render(); + + rlImGuiBegin(); + scene_manager.ui(); + rlImGuiEnd(); + + EndDrawing(); + + scene_manager.process_scene_switch(); // !!! After scene_manager is done with the current loop + } + + CloseWindow(); + rlImGuiShutdown(); + return 0; +} + diff --git a/src/movement.cpp b/src/movement.cpp index 812eb80..25dbd77 100644 --- a/src/movement.cpp +++ b/src/movement.cpp @@ -1,14 +1,14 @@ -// src\movement.cpp -#include -#include - -void update_movement_system(entt::registry& registry, float dt) { - // We get Position and Velocity, but EXCLUDE entities that have the Player tag - auto view = registry.view(); - - view.each([dt](Position &position, Velocity &velocity) { - // This will only run for pipes, enemies, or particles! - position.x += velocity.dx * dt; - position.y += velocity.dy * dt; - }); -} +// src\movement.cpp +#include +#include + +void update_movement_system(entt::registry& registry, float dt) { + // We get Position and Velocity, but EXCLUDE entities that have the Player tag + auto view = registry.view(); + + view.each([dt](Position &position, Velocity &velocity) { + // This will only run for pipes, enemies, or particles! + position.x += velocity.dx * dt; + position.y += velocity.dy * dt; + }); +} diff --git a/src/player.cpp b/src/player.cpp index 437ae78..97ea87b 100644 --- a/src/player.cpp +++ b/src/player.cpp @@ -1,34 +1,50 @@ -// src\player.cpp -#include -#include - -#include - -entt::entity player; - - - -void create_player(entt::registry& registry,Texture* texture){ - const auto entity = registry.create(); - registry.emplace(entity,100.0f,0.0f); - registry.emplace(entity,0.0f,0.0f); - registry.emplace(entity); - registry.emplace(entity,texture); -} - -void update_player_system(entt::registry& registry,float dt){ - - auto view = registry.view(); - - // Constants for fine-tuning game feel - const float GRAVITY = 900.0f; // Constant downward pull (pixels/sec^2) - const float JUMP_FORCE = -350.0f; // Negative force to burst UPWARD - - view.each([dt,GRAVITY,JUMP_FORCE](const auto &player, auto &velocity){ - if (IsKeyPressed(KEY_SPACE)){ - velocity.dy = JUMP_FORCE; - } - velocity.dy += GRAVITY * dt; - }); - -} +// src\player.cpp +#include +#include + +#include + +entt::entity player; + + + +void create_player(entt::registry& registry,Texture* texture){ + const auto entity = registry.create(); + registry.emplace(entity,100.0f,0.0f); + registry.emplace(entity,300.0f,0.0f); + registry.emplace(entity); + registry.emplace(entity,texture); +} + +void update_player_system(entt::registry& registry,float dt){ + + auto view = registry.view(); + + // Constants for fine-tuning game feel + const float GRAVITY = 900.0f; // Constant downward pull (pixels/sec^2) + const float JUMP_FORCE = -350.0f; // Negative force to burst UPWARD + + for (entt::entity entity : view){ + const Position& pos = view.get(entity); + Velocity& vel = view.get(entity); + + if (pos.x < -50 && vel.dx < 0){ + vel.dx *= -1; + } else if (pos.x > (GetScreenWidth()-250) && vel.dx > 0){ + vel.dx *= -1; + } + + if (IsKeyPressed(KEY_SPACE)){ + vel.dy = JUMP_FORCE; + } + vel.dy += GRAVITY * dt; + } + + view.each([dt,GRAVITY,JUMP_FORCE](entt::entity entity,const Position &position, auto &velocity){ + if (position.x > 100 && position.x < 120){ + velocity.dx += 20; + } + + }); +} + diff --git a/src/render.cpp b/src/render.cpp index 60fe6c5..3fcf617 100644 --- a/src/render.cpp +++ b/src/render.cpp @@ -1,18 +1,18 @@ - -#include -#include - -#include -#include - -void update_render_system(entt::registry ®istry){ - auto view = registry.view(); - - view.each([] (const auto &sprite, const auto &position){ - if (sprite.texture){ - DrawTexture(*(sprite.texture),position.x,position.y,WHITE); - }else{ - DrawRectangle(position.x,position.y,50, 70,BLACK); - } - }); -} + +#include +#include + +#include +#include + +void update_render_system(entt::registry ®istry){ + auto view = registry.view(); + + view.each([] (const auto &sprite, const auto &position){ + if (sprite.texture){ + DrawTexture(*(sprite.texture),position.x,position.y,WHITE); + }else{ + DrawRectangle(position.x,position.y,50, 70,BLACK); + } + }); +} diff --git a/src/scenes/game_play_scene.cpp b/src/scenes/game_play_scene.cpp index 15f4b6c..3f20e38 100644 --- a/src/scenes/game_play_scene.cpp +++ b/src/scenes/game_play_scene.cpp @@ -1,33 +1,53 @@ -#include -#include -#include -#include -#include - - -GamePlayScene::GamePlayScene() { - registry = entt::registry(); - - asset_loader.load_asset("assets/bird.png"); - create_player(registry,asset_loader.get_texture("assets/bird.png")); - -} - -void GamePlayScene::update(float dt) { - update_movement_system(registry, dt); - update_player_system(registry, dt); -} - - - -void GamePlayScene::render() { - update_render_system(registry); -} - -void GamePlayScene::ui() { -} - -GamePlayScene::~GamePlayScene(){ - asset_loader.unload_asset("assets/bird.png"); - -} +#include +#include +#include +#include +#include +#include + +#include + +GamePlayScene::GamePlayScene() { + registry = entt::registry(); + + asset_loader.load_asset("assets/bird.png"); + create_player(registry,asset_loader.get_texture("assets/bird.png")); + +} + +void GamePlayScene::update(float dt) { + update_movement_system(registry, dt); + update_player_system(registry, dt); +} + + + +void GamePlayScene::render() { + update_render_system(registry); +} + +void GamePlayScene::ui() { + // Get the current screen/viewport size + ImVec2 screen_size = ImGui::GetIO().DisplaySize; + // set next container to be center of the screen + ImVec2 screen_center = ImVec2(screen_size.x * 0.9f, screen_size.y * 0.1f); + ImGui::SetNextWindowPos(screen_center, ImGuiCond_Always,ImVec2(0.5f, 0.5f)); + + ImGui::SetNextWindowSize({40.f,40.f}); + // remove sub window decoration + ImGuiWindowFlags flags = ImGuiWindowFlags_NoDecoration | ImGuiWindowFlags_NoBackground; + + + ImGui::Begin("Exit", nullptr, flags); + + if (ImGui::Button("X",{40.f,40.f})){ + g_Engine.request_switch(SceneType::MENU); + } + + ImGui::End(); +} + +GamePlayScene::~GamePlayScene(){ + asset_loader.unload_asset("assets/bird.png"); + +} diff --git a/src/scenes/menu_scene.cpp b/src/scenes/menu_scene.cpp index f0893d1..277f104 100644 --- a/src/scenes/menu_scene.cpp +++ b/src/scenes/menu_scene.cpp @@ -1,56 +1,56 @@ -#include -#include -#include - -#include - -// #include - - -MenuScene::MenuScene() { -} - -void MenuScene::update(float dt) { -} - -void MenuScene::render() {} - -void MenuScene::ui() { - - // Get the current screen/viewport size - ImVec2 screen_size = ImGui::GetIO().DisplaySize; - - // size of button container - ImVec2 vbox_size = ImVec2(200.0f, 250.0f); - - // set next container to be center of the screen - ImVec2 screen_center = ImVec2(screen_size.x * 0.5f, screen_size.y * 0.5f); - ImGui::SetNextWindowPos(screen_center, ImGuiCond_Always,ImVec2(0.5f, 0.5f)); - ImGui::SetNextWindowSize(vbox_size); - - // remove sub window decoration - ImGuiWindowFlags flags = ImGuiWindowFlags_NoDecoration | ImGuiWindowFlags_NoBackground; - - ImGui::Begin("VBoxContainer", nullptr, flags); - - // set button width to be size fill container - ImVec2 button_size = ImVec2(ImGui::GetContentRegionAvail().x, 50.0f); - - // ImGui::SetCursorPos(centeredPos); - if (ImGui::Button("Play",button_size)){ - g_Engine.request_switch(SceneType::GAMEPLAY); - } - - ImGui::Dummy(ImVec2(0, 10)); // Gap - - if (ImGui::Button("Quit",button_size)){ - g_Engine.quit(); - } - - ImGui::End(); - - // ImGui::Begin("Debug"); - // ImGui::Button("Hello",button_size); - // ImGui::TextUnformatted(std::format("window size: {} {} ", screen_size.x,screen_size.y).c_str()); - // ImGui::End(); -} +#include +#include +#include + +#include + +// #include + + +MenuScene::MenuScene() { +} + +void MenuScene::update(float dt) { +} + +void MenuScene::render() {} + +void MenuScene::ui() { + + // Get the current screen/viewport size + ImVec2 screen_size = ImGui::GetIO().DisplaySize; + + // size of button container + ImVec2 vbox_size = ImVec2(200.0f, 250.0f); + + // set next container to be center of the screen + ImVec2 screen_center = ImVec2(screen_size.x * 0.5f, screen_size.y * 0.5f); + ImGui::SetNextWindowPos(screen_center, ImGuiCond_Always,ImVec2(0.5f, 0.5f)); + ImGui::SetNextWindowSize(vbox_size); + + // remove sub window decoration + ImGuiWindowFlags flags = ImGuiWindowFlags_NoDecoration | ImGuiWindowFlags_NoBackground; + + ImGui::Begin("VBoxContainer", nullptr, flags); + + // set button width to be size fill container + ImVec2 button_size = ImVec2(ImGui::GetContentRegionAvail().x, 50.0f); + + // ImGui::SetCursorPos(centeredPos); + if (ImGui::Button("Play",button_size)){ + g_Engine.request_switch(SceneType::GAMEPLAY); + } + + ImGui::Dummy(ImVec2(0, 10)); // Gap + + if (ImGui::Button("Quit",button_size)){ + g_Engine.quit(); + } + + ImGui::End(); + + // ImGui::Begin("Debug"); + // ImGui::Button("Hello",button_size); + // ImGui::TextUnformatted(std::format("window size: {} {} ", screen_size.x,screen_size.y).c_str()); + // ImGui::End(); +} diff --git a/src/scenes/scene.cpp b/src/scenes/scene.cpp index 6b3cdb2..3cdbff6 100644 --- a/src/scenes/scene.cpp +++ b/src/scenes/scene.cpp @@ -1,39 +1,39 @@ -#include - -SceneManager::SceneManager(){ - current_scene = new MenuScene(); -} - -SceneManager::~SceneManager(){ - delete current_scene; -} - -void SceneManager::process_scene_switch(){ - if (!g_Engine.pendingSwitch) return; - - delete current_scene; - - switch(g_Engine.nextScene){ - case SceneType::MENU: - current_scene = new MenuScene(); - break; - case SceneType::GAMEPLAY: - current_scene = new GamePlayScene(); - break; - } - g_Engine.pendingSwitch = false; -} - -void SceneManager::update(float dt){ - if (current_scene){ - current_scene->update(dt); - } -} - -void SceneManager::render(){ - current_scene->render(); -} - -void SceneManager::ui(){ - current_scene->ui(); -} +#include + +SceneManager::SceneManager(){ + current_scene = new MenuScene(); +} + +SceneManager::~SceneManager(){ + delete current_scene; +} + +void SceneManager::process_scene_switch(){ + if (!g_Engine.pendingSwitch) return; + + delete current_scene; + + switch(g_Engine.nextScene){ + case SceneType::MENU: + current_scene = new MenuScene(); + break; + case SceneType::GAMEPLAY: + current_scene = new GamePlayScene(); + break; + } + g_Engine.pendingSwitch = false; +} + +void SceneManager::update(float dt){ + if (current_scene){ + current_scene->update(dt); + } +} + +void SceneManager::render(){ + current_scene->render(); +} + +void SceneManager::ui(){ + current_scene->ui(); +} diff --git a/src/test.cpp b/src/test.cpp new file mode 100644 index 0000000..69126d6 --- /dev/null +++ b/src/test.cpp @@ -0,0 +1,75 @@ +#include +#include +#include + +#include +#include +#include +#include +#include +#include +#include + + +#include +#include +#include +#include + +// #include +// #include + +EngineState g_Engine; +RaylibAssetLoader asset_loader; + +// size_t get_current_ram_usage() { +// PROCESS_MEMORY_COUNTERS pmc; +// if (GetProcessMemoryInfo(GetCurrentProcess(), &pmc, sizeof(pmc))) { +// return pmc.WorkingSetSize; +// } +// return 0; +// } +void asset_thread(){ + // for (int i = 0; i < 1000000; i++){ + std::this_thread::sleep_for(std::chrono::milliseconds(1000)); + asset_loader.load_asset("assets/bird.png"); + // asset_loader.unload_asset("assets/bird.png"); + // if (WindowShouldClose()) break; + // } +} + +int main() { + + int* num = reinterpret_cast(new char[4]); + SetTraceLogLevel(LOG_NONE); + InitWindow(800,800,"test"); + // std::cout << "Starting Test..." << std::endl; + // std::this_thread::sleep_for(std::chrono::milliseconds(5000)); + + // std::thread t(asset_thread); + // asset_loader.load_asset("assets/bird.png"); + + while(!WindowShouldClose()){ + // size_t ram_bytes = get_current_ram_usage(); + asset_loader.load_asset("assets/bird.png"); + std::this_thread::sleep_for(std::chrono::milliseconds(10)); + asset_loader.unload_asset("assets/bird.png"); + + BeginDrawing(); + ClearBackground(RAYWHITE); + // auto tex = asset_loader.get_texture("assets/bird.png"); + // if (tex != nullptr){ + // DrawTexture(*tex, 0,0,WHITE); + // } + rlImGuiBegin(); + ImGui::TextUnformatted(std::format("hello {}","world").c_str()); + // ImGui::TextUnformatted(std::format("Memory usage: %.2f",ram_bytes/(1024*1024)).c_str()); + rlImGuiEnd(); + EndDrawing(); + } + + // t.join(); + CloseWindow(); + + return 0; +}