Skip to content

Commit

Permalink
0.24 - JSON level loading
Browse files Browse the repository at this point in the history
- Levels now load from JSON file
- Currently a bug with the obj tilemap loading
- Linux setup script added!
  • Loading branch information
jalowe13 committed Jul 25, 2023
1 parent 17dc2a7 commit 641f56a
Show file tree
Hide file tree
Showing 6 changed files with 119 additions and 18 deletions.
2 changes: 1 addition & 1 deletion TheOneSDL/Application.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ bool Application::init()

if (!window)
{
throw "Window creation failed.";
throw "Window creation failed!";
}

std::cout << "-----Window Created" << std::endl;
Expand Down
6 changes: 3 additions & 3 deletions TheOneSDL/Application.h
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down Expand Up @@ -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();

Expand Down
86 changes: 74 additions & 12 deletions TheOneSDL/Terrain.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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";
Expand Down
3 changes: 3 additions & 0 deletions TheOneSDL/Terrain.h
Original file line number Diff line number Diff line change
Expand Up @@ -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'},
Expand Down
4 changes: 2 additions & 2 deletions TheOneSDL/levels.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
"~~~~~~~~~~~~~~~~~~~~~~~~~",
"~~~~~~~~~~~~~~~~~~~~~~~~~"
],
"obj_tilemap:":[
"obj_tilemap":[
"~~~~~~~~~~~~~~~~~~~~~~~~~",
"~~~~~~~~~~~~~~~~~~~~~~~~~",
"~~~~~~~~~~~~~~~~~~~~~~~~~",
Expand Down Expand Up @@ -67,7 +67,7 @@
"wwwwwwwwwwwwwwwwwwwwwwwww",
"wwwwwwwwwwwwwwwwwwwwwwwww"
],
"obj_tilemap:":[
"obj_tilemap":[
"ggggggggggggggggggggggggg",
"~~~~~~~~~~~~~~~~~~~~~~~~~",
"~~~~~~~~~~~~~~~~~~~~~~~~~",
Expand Down
36 changes: 36 additions & 0 deletions TheOneSDL/linux_setup.sh
Original file line number Diff line number Diff line change
@@ -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 "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"

0 comments on commit 641f56a

Please sign in to comment.