From 641f56a4c1b730f2eb9f5569c232a17a7a2d2610 Mon Sep 17 00:00:00 2001 From: jalowe13 Date: Mon, 24 Jul 2023 21:00:43 -0500 Subject: [PATCH] 0.24 - JSON level loading - Levels now load from JSON file - Currently a bug with the obj tilemap loading - Linux setup script added! --- TheOneSDL/Application.cpp | 2 +- TheOneSDL/Application.h | 6 +-- TheOneSDL/Terrain.cpp | 86 +++++++++++++++++++++++++++++++++------ TheOneSDL/Terrain.h | 3 ++ TheOneSDL/levels.json | 4 +- TheOneSDL/linux_setup.sh | 36 ++++++++++++++++ 6 files changed, 119 insertions(+), 18 deletions(-) create mode 100755 TheOneSDL/linux_setup.sh diff --git a/TheOneSDL/Application.cpp b/TheOneSDL/Application.cpp index 57a29f9..138afe9 100644 --- a/TheOneSDL/Application.cpp +++ b/TheOneSDL/Application.cpp @@ -45,7 +45,7 @@ bool Application::init() if (!window) { - throw "Window creation failed."; + throw "Window creation failed!"; } std::cout << "-----Window Created" << std::endl; diff --git a/TheOneSDL/Application.h b/TheOneSDL/Application.h index c517058..4677c22 100644 --- a/TheOneSDL/Application.h +++ b/TheOneSDL/Application.h @@ -3,8 +3,8 @@ // Version Number #define VERSION_MAJOR 0 -#define VERSION_MINOR 23 -#define VERSION_PATCH 10 +#define VERSION_MINOR 24 +#define VERSION_PATCH #define STR_HELPER(x) #x // convert to fit window title #define STR(x) STR_HELPER(x) @@ -39,7 +39,7 @@ class Physics; class Application { public: -const char* windowTitle = "C23 Engine: The One SDL v." STR(VERSION_MAJOR) "." STR(VERSION_MINOR) "." STR(VERSION_PATCH) " FPS:"; +const char* windowTitle = "C23 Engine: The One SDL v." STR(VERSION_MAJOR) "." STR(VERSION_MINOR) STR(VERSION_PATCH) " FPS:"; Application(); ~Application(); diff --git a/TheOneSDL/Terrain.cpp b/TheOneSDL/Terrain.cpp index 610faf0..f9ad6f8 100644 --- a/TheOneSDL/Terrain.cpp +++ b/TheOneSDL/Terrain.cpp @@ -178,7 +178,6 @@ bool Terrain::fillScreen() case 'c': texture = IMG_LoadTexture(renderer, CHOMPER_TEX); name = "Chomper"; - break; } if (texture != NULL && x < SCREEN_WIDTH && y < SCREEN_HEIGHT) { @@ -215,24 +214,87 @@ void Terrain::loadLevel(std::string level) std::cout << "!!!!!Error Parsing Level Data for Level " << level << std::endl; } - for (const auto& levelObject: lvl_data) + if (!lvl_data.isObject()) { - if (levelObject["name"].asString() == level) { - std::cout << "Load Level: " + level << std::endl; - // Parse the background tilemap from json to terrain object - //TODO - // Parse the obj_tilemap from json to terrain object - } + std::cout << "!!!!Error Parsing: Level data is not object \n"; } - std::cout << "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n"; - // std::string line = - - + if (!lvl_data.isArray()) + { + std::cout << "!!!!Error Parsing: Level Data is not an Array\n"; + } + for (int i = 0; i < lvl_data.size(); i++) // Iterate through level data + { + std::string level_name = lvl_data[i]["name"].asString(); // Grab level name + + if (level_name == level) + { + for (int map_i = 0; map_i < 2; map_i++) // Load background and tilemap + { + switch(map_i) + { + case 0: + loadTilemap(lvl_data[i]["background_tilemap"], 0); + break; + case 1: + // std::cout << "Start obj\n"; + // std::cout << lvl_data[i] << std::endl; + loadTilemap(lvl_data[i]["obj_tilemap"], 1); + break; + } + } + } + } + fillScreen(); } +void Terrain::loadTilemap(Json::Value json_tilemap, int map_type) +{ + std::cout << "Load Tilemap" << std::endl; + // Iterators for tilemap + int tilemapY_i = 0; + int tilemapX_i = 0; + std::cout << "size:" << json_tilemap.size() << std::endl; + if (json_tilemap.size() == 0) + { + std::cout << "Error: Tilemap of type " << map_type << " is size " << json_tilemap.size(); + } + for (int j = 0; j < json_tilemap.size(); j++) + { + for (char c : json_tilemap[j].asString()) // Grab new char + { + //std::cout << c << std::endl; + if (tilemapX_i < tilemapX) // Iterate Col + { + //std::cout << "Replacing " << background_tilemap[tilemapY_i][tilemapX_i] << "with " << c << std::endl; + switch(map_type) + { + case 0: + background_tilemap[tilemapY_i][tilemapX_i] = c; + break; + case 1: + //obj_tilemap[tilemapY_i][tilemapX_i] = c; + break; + } + + //std::cout << "[" << tilemapY_i << "," << tilemapX_i << "]" << background_tilemap[tilemapY_i][tilemapX_i] << std::endl; + tilemapX_i++; + } + else + { + tilemapX_i = 0; + if (tilemapY_i < tilemapY) // Iterate Row + { + tilemapY_i++; + } + + } + } + } +} + void Terrain::print_allBlockInfo() { std::cout << "Start\n"; diff --git a/TheOneSDL/Terrain.h b/TheOneSDL/Terrain.h index 68a8bc7..f144658 100644 --- a/TheOneSDL/Terrain.h +++ b/TheOneSDL/Terrain.h @@ -76,6 +76,9 @@ class Terrain // Load level from JSON void loadLevel(std::string level); + // Load Tilemap + void loadTilemap(Json::Value json_tilemap, int map_type); + // Tilemaps char background_tilemap[tilemapY][tilemapX] = { {'w','w','w','w','w','w','w','w','w','w','w','w','w','w','w','w','w','w','w','w','w','w','w','w','w'}, diff --git a/TheOneSDL/levels.json b/TheOneSDL/levels.json index 7e3a6c4..63c13c8 100644 --- a/TheOneSDL/levels.json +++ b/TheOneSDL/levels.json @@ -22,7 +22,7 @@ "~~~~~~~~~~~~~~~~~~~~~~~~~", "~~~~~~~~~~~~~~~~~~~~~~~~~" ], - "obj_tilemap:":[ + "obj_tilemap":[ "~~~~~~~~~~~~~~~~~~~~~~~~~", "~~~~~~~~~~~~~~~~~~~~~~~~~", "~~~~~~~~~~~~~~~~~~~~~~~~~", @@ -67,7 +67,7 @@ "wwwwwwwwwwwwwwwwwwwwwwwww", "wwwwwwwwwwwwwwwwwwwwwwwww" ], - "obj_tilemap:":[ + "obj_tilemap":[ "ggggggggggggggggggggggggg", "~~~~~~~~~~~~~~~~~~~~~~~~~", "~~~~~~~~~~~~~~~~~~~~~~~~~", diff --git a/TheOneSDL/linux_setup.sh b/TheOneSDL/linux_setup.sh new file mode 100755 index 0000000..408dd81 --- /dev/null +++ b/TheOneSDL/linux_setup.sh @@ -0,0 +1,36 @@ +# Jacob Lowe +# Setup for Linux + +echo "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~" +echo "Installing yay" +echo "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~" +cd /opt +sudo git clone https://aur.archlinux.org/yay-git.git +sudo chown -R $USER ./yay-git +cd yay-git +makepkg -si --noconfirm + +echo "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~" +echo "Installing Packages..." +echo "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~" +sudo pacman -Syu --noconfirm make boost cmake gcc jsoncpp +sudo yay -S --noconfirm sdl2-git sdl2_image sdl2_ttf +echo "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~" +echo "Compiling program for the first time..." +echo "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~" +make +echo Setting SDL_VIDEODRIVER to wayland or x11 +export SDL_VIDEODRIVER=wayland,x11 +echo The Current Graphics Driver is "$SDL_VIDEODRIVER" +echo +echo + +echo "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~" +echo "Install complete! Press any key to close the window." +echo "You can now compile program by using the command" +echo "> make" +echo "You make also delete all compiled files by typing" +echo "> make cleanall" +echo "Launch the game by typing the following or just click the exe in the directory" +echo "> .\TheOne.exe" +echo "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"