second commit
This commit is contained in:
Vendored
+2
-1
@@ -11,7 +11,8 @@
|
||||
"program": "${workspaceFolder}/debug/d.exe",
|
||||
"args": [],
|
||||
"stopAtEntry": false,
|
||||
"cwd": "${fileDirname}",
|
||||
// "cwd": "${fileDirname}",
|
||||
"cwd": "${workspaceFolder}",
|
||||
"environment": [],
|
||||
"externalConsole": false,
|
||||
"MIMode": "gdb",
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
[Window][Debug##Default]
|
||||
Pos=143,124
|
||||
Pos=952,-6
|
||||
Size=400,400
|
||||
|
||||
[Window][CenterWindow]
|
||||
@@ -19,3 +19,7 @@ Size=180,105
|
||||
Pos=882,174
|
||||
Size=320,170
|
||||
|
||||
[Window][Exit]
|
||||
Pos=1132,60
|
||||
Size=40,40
|
||||
|
||||
|
||||
@@ -11,7 +11,7 @@ LIBS = -lraylib -lwinmm -lgdi32 -lbox2d -lassetpacker
|
||||
CXXFLAGS = -std=c++20 -O2 \
|
||||
-MMD -MP
|
||||
|
||||
DEBUGFLAGS = -std=c++20 -g -Og
|
||||
DEBUGFLAGS = -std=c++20 -g -O0
|
||||
|
||||
INCLUDES = -I./external/raylib/include \
|
||||
-I./external/entt/single_include \
|
||||
@@ -32,8 +32,7 @@ IMGUI_BACKEND = ./external/rlImGui/rlImGui.cpp
|
||||
|
||||
|
||||
|
||||
SRC_FILES = ./src/main.cpp \
|
||||
./src/asset.cpp \
|
||||
SRC_FILES = ./src/asset.cpp \
|
||||
./src/movement.cpp \
|
||||
./src/player.cpp \
|
||||
./src/render.cpp \
|
||||
@@ -41,6 +40,9 @@ SRC_FILES = ./src/main.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))
|
||||
@@ -77,13 +79,19 @@ 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
|
||||
$(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) $(SRC_FILES) $(IMGUI_SRCS) $(IMGUI_BACKEND) $(LDFLAGS) $(LIBS) -o ./debug/d.exe
|
||||
cp ./assets ./debug -rf
|
||||
$(CXX) $(DEBUGFLAGS) $(INCLUDES) $(ENTRY_POINT) $(SRC_FILES) $(IMGUI_OBJS) $(IMGUI_BACKEND_OBJS) $(LDFLAGS) $(LIBS) -o ./debug/d.exe
|
||||
cp ./data.dat ./debug -rf
|
||||
|
||||
|
||||
|
||||
@@ -101,3 +109,6 @@ run:
|
||||
|
||||
run-debug:
|
||||
./debug/d.exe
|
||||
|
||||
run-test: build-test
|
||||
./bin/test.exe
|
||||
|
||||
+65
-15
@@ -13,6 +13,16 @@
|
||||
|
||||
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;
|
||||
@@ -32,16 +42,17 @@ RaylibAssetLoader::~RaylibAssetLoader(){
|
||||
}
|
||||
|
||||
|
||||
|
||||
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;
|
||||
// std::cout << file_path.extension() << std::endl;
|
||||
auto bytes = get_reader().extract_asset(path);
|
||||
|
||||
if (file_path.extension() == ".png"){
|
||||
auto texture_data = get_reader().extract_asset(path);
|
||||
Image img = LoadImageFromMemory(".png", texture_data.data(), texture_data.size());
|
||||
Image img = LoadImageFromMemory(".png", bytes.data(), bytes.size());
|
||||
auto texture = LoadTextureFromImage(img);
|
||||
UnloadImage(img);
|
||||
new_asset.data = ImageData { new Texture(texture)};
|
||||
@@ -49,15 +60,47 @@ void RaylibAssetLoader::load_asset(std::string path){
|
||||
|
||||
}else if(file_path.extension() == ".ogg"){
|
||||
|
||||
}else if(file_path.extension() == ".wav"){
|
||||
|
||||
}else if(file_path.extension() == ".json"){
|
||||
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){
|
||||
@@ -67,20 +110,27 @@ void RaylibAssetLoader::unload_asset(std::string path){
|
||||
using T = std::decay_t<decltype(arg)>;
|
||||
|
||||
if constexpr (std::is_same_v<T, ImageData>) {
|
||||
|
||||
UnloadTexture(*((Texture*) arg.texture));
|
||||
delete (Texture*) arg.texture;
|
||||
Texture* tex = static_cast<Texture*>(arg.texture);
|
||||
UnloadTexture(*tex);
|
||||
delete tex;
|
||||
arg.texture = nullptr;
|
||||
}
|
||||
else if constexpr (std::is_same_v<T, SoundData>) {
|
||||
UnloadSound(*((Sound*) arg.sound));
|
||||
delete (Sound*) arg.sound;
|
||||
Sound* sound = static_cast<Sound*>(arg.sound);
|
||||
UnloadSound(*sound);
|
||||
delete sound;
|
||||
arg.sound = nullptr;
|
||||
}
|
||||
else if constexpr (std::is_same_v<T, MusicData>) {
|
||||
UnloadMusicStream(*((Music*) arg.music));
|
||||
delete (Music*) arg.music;
|
||||
Music* music = static_cast<Music*>(arg.music);
|
||||
UnloadMusicStream(*music);
|
||||
delete music;
|
||||
arg.music = nullptr;
|
||||
}
|
||||
// JsonData requires no manual step because std::string cleans itself up!
|
||||
}, asset.data);
|
||||
}, asset.data);
|
||||
|
||||
assets.erase(path);
|
||||
|
||||
}
|
||||
|
||||
|
||||
+24
-8
@@ -11,24 +11,40 @@ entt::entity player;
|
||||
void create_player(entt::registry& registry,Texture* texture){
|
||||
const auto entity = registry.create();
|
||||
registry.emplace<Position>(entity,100.0f,0.0f);
|
||||
registry.emplace<Velocity>(entity,0.0f,0.0f);
|
||||
registry.emplace<Velocity>(entity,300.0f,0.0f);
|
||||
registry.emplace<Player>(entity);
|
||||
registry.emplace<Sprite>(entity,texture);
|
||||
}
|
||||
|
||||
void update_player_system(entt::registry& registry,float dt){
|
||||
|
||||
auto view = registry.view<const Player, Velocity>();
|
||||
auto view = registry.view<const Player, const Position, Velocity>();
|
||||
|
||||
// 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;
|
||||
});
|
||||
for (entt::entity entity : view){
|
||||
const Position& pos = view.get<Position>(entity);
|
||||
Velocity& vel = view.get<Velocity>(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;
|
||||
}
|
||||
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
@@ -1,9 +1,11 @@
|
||||
#include <imgui.h>
|
||||
#include <player.hpp>
|
||||
#include <movement.hpp>
|
||||
#include <scene.hpp>
|
||||
#include <render.hpp>
|
||||
#include <asset/raylib_asset.hpp>
|
||||
|
||||
#include <format>
|
||||
|
||||
GamePlayScene::GamePlayScene() {
|
||||
registry = entt::registry();
|
||||
@@ -25,6 +27,24 @@ void GamePlayScene::render() {
|
||||
}
|
||||
|
||||
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(){
|
||||
|
||||
@@ -0,0 +1,75 @@
|
||||
#include <asset/raylib_asset.hpp>
|
||||
#include <scene.hpp>
|
||||
#include <components.hpp>
|
||||
|
||||
#include <box2d/box2d.h>
|
||||
#include <box2d/types.h>
|
||||
#include <raylib.h>
|
||||
#include <rlImGui.h>
|
||||
#include <imgui.h>
|
||||
#include <entt/entt.hpp>
|
||||
#include <asset_packer/asset.hpp>
|
||||
|
||||
|
||||
#include <chrono>
|
||||
#include <thread>
|
||||
#include <format>
|
||||
#include <iostream>
|
||||
|
||||
// #include <windows.h>
|
||||
// #include <psapi.h>
|
||||
|
||||
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<int*>(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;
|
||||
}
|
||||
Reference in New Issue
Block a user