Skip to content

Commit

Permalink
editor handles defaults - game will throw exception
Browse files Browse the repository at this point in the history
  • Loading branch information
goopey7 committed Dec 30, 2023
1 parent 9353e31 commit ef60f4d
Show file tree
Hide file tree
Showing 9 changed files with 71 additions and 76 deletions.
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ file(GLOB STB_IMAGE_HEADER "libs/stb/stb_image.h")
add_executable(${PROJECT_NAME} ${ENGINE_SRC_FILES} ${STB_VORBIS_SOURCE})

#define app type
target_compile_definitions(${PROJECT_NAME} PRIVATE GOOP_APPTYPE_EDITOR=1)
#target_compile_definitions(${PROJECT_NAME} PRIVATE GOOP_APPTYPE_EDITOR=1)

# define systems
target_compile_definitions(${PROJECT_NAME} PRIVATE GOOP_RENDERER_VULKAN)
Expand Down
4 changes: 0 additions & 4 deletions cfg.json

This file was deleted.

63 changes: 59 additions & 4 deletions editor/EditorApp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,66 @@
#include <json.hpp>
using json = nlohmann::json;

goop::App* goop::createEditor(int argc, char** argv, App* game) { return new EditorApp(game); }
goop::App* goop::createEditor(int argc, char** argv, App* game, goop::Scene* scene)
{
return new EditorApp(game, scene);
}

void EditorApp::init()
{
auto cfgOpt = loadJson("cfg.json");
if (!cfgOpt.has_value())
{
std::cout << "Failed to load cfg.json" << std::endl;

// create a default cfg.json
json cfg = {
{"game_file", "lvl.json"},
{"start_scene", "lvl_1"},
};

std::ofstream file("cfg.json");
file << cfg;
file.close();

cfgOpt = loadJson("cfg.json");
}

json cfg = cfgOpt.value();

auto lvlOpt = loadJson(cfg["game_file"]);
if (!lvlOpt.has_value())
{
std::cout << "Failed to load " << cfg["game_file"] << std::endl;

// create a default game file
json lvl;
json sceneObj = {{"name", "lvl_1"}};
json scenesArr = {sceneObj};

lvl["scenes"] = scenesArr;

std::ofstream file(cfg["game_file"]);
file << lvl;
file.close();

lvlOpt = loadJson(cfg["game_file"]);
}
json lvl = lvlOpt.value();

json startScene;

// find starting scene
for (json& scene : lvl["scenes"])
{
if (scene["name"] == cfg["start_scene"])
{
startScene = scene;
break;
}
}

void EditorApp::init()
{
game->init();
scene->loadScene(startScene);
}

void EditorApp::update(float dt)
Expand Down
2 changes: 1 addition & 1 deletion editor/EditorApp.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
class EditorApp : public goop::App
{
public:
EditorApp(goop::App* game) : game(game) {}
EditorApp(goop::App* game, goop::Scene* scene) : game(game), App(scene) {}
void init() final;
void update(float dt) final;
void gui() final;
Expand Down
29 changes: 2 additions & 27 deletions game/GameApp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,40 +23,15 @@ void GameApp::init()
if (!cfgOpt.has_value())
{
std::cout << "Failed to load cfg.json" << std::endl;

// create a default cfg.json
json cfg = {
{"game_file", "lvl.json"},
{"start_scene", "lvl_1"},
};

std::ofstream file("cfg.json");
file << cfg;
file.close();

cfgOpt = loadJson("cfg.json");
throw std::runtime_error("Failed to load cfg.json");
}

json cfg = cfgOpt.value();

auto lvlOpt = loadJson(cfg["game_file"]);
if (!lvlOpt.has_value())
{
std::cout << "Failed to load " << cfg["game_file"] << std::endl;

// create a default game file
json lvl = {{
"scenes",
{
{"name", "lvl_1"},
},
}};

std::ofstream file(cfg["game_file"]);
file << lvl;
file.close();

lvlOpt = loadJson(cfg["game_file"]);
throw std::runtime_error("Failed to load " + cfg["game_file"].get<std::string>());
}
json lvl = lvlOpt.value();

Expand Down
4 changes: 2 additions & 2 deletions goop/App.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ namespace goop
class App
{
public:
App(Scene* scene = nullptr) : scene(scene) {}
App(Scene* scene) : scene(scene) {}
virtual void init() = 0;
virtual void update(float dt) = 0;
virtual void gui() = 0;
Expand Down Expand Up @@ -50,7 +50,7 @@ class App
#endif
};
#ifdef GOOP_APPTYPE_EDITOR
extern App* createEditor(int argc, char** argv, App* game);
extern App* createEditor(int argc, char** argv, App* game, Scene* scene);
#endif
extern App* createGame(int argc, char** argv, Scene* scene);
} // namespace goop
2 changes: 1 addition & 1 deletion goop/Core.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ const std::unique_ptr<goop::sys::ResourceManager> goop::rm =
std::make_unique<goop::sys::ResourceManager>();

#ifdef GOOP_APPTYPE_EDITOR
Core::Core(int argc, char** argv) : app(createEditor(argc, argv, createGame(argc, argv, &scene)))
Core::Core(int argc, char** argv) : app(createEditor(argc, argv, createGame(argc, argv, &scene), &scene))
#else
Core::Core(int argc, char** argv) : app(createGame(argc, argv, &scene))
#endif
Expand Down
5 changes: 5 additions & 0 deletions goop/Scene.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,11 @@ Entity Scene::getEntity(const std::string& tag)

void Scene::loadScene(nlohmann::json& startScene)
{
if (startScene["entities"].is_null())
{
return;
}

for (json& entity : startScene["entities"])
{
goop::Entity e = createEntity(entity["name"]);
Expand Down
36 changes: 0 additions & 36 deletions lvl.json

This file was deleted.

0 comments on commit ef60f4d

Please sign in to comment.