From cf695b3d1004157ab070a2a00737b6cd6d70f0a2 Mon Sep 17 00:00:00 2001 From: Frank DeMarco Date: Thu, 2 May 2019 20:11:45 -0400 Subject: [PATCH] class name --- demo/Demo.cpp | 45 +++++++++++++++++++++++++------------------ demo/Demo.hpp | 2 ++ demo/Makefile | 12 ++++++------ src/Configuration.cpp | 4 ++-- src/Configuration.hpp | 3 +++ src/Game.cpp | 2 +- src/Game.hpp | 3 ++- src/Location.hpp | 2 +- src/Node.cpp | 31 +++++++++++++++++++++-------- src/Node.hpp | 4 ++-- src/Sprite.cpp | 7 +++++++ src/Sprite.hpp | 14 +++----------- 12 files changed, 78 insertions(+), 51 deletions(-) diff --git a/demo/Demo.cpp b/demo/Demo.cpp index 3fb3ebb..6d7e753 100644 --- a/demo/Demo.cpp +++ b/demo/Demo.cpp @@ -166,28 +166,38 @@ struct Demo : Game GLuint vbo, space_texture_id, mvp_id, framerate_texture_id, flat_program, world_program, fake_texture_id; glm::mat4 projection, view, model = glm::mat4(1.0f), mvp; - Mushroom mushroom = Mushroom(this); - Sprite grass = Sprite(this, "resource/Field.png"); + Mushroom *mushroom = new Mushroom(this); + Sprite *grass = new Sprite(this, "resource/Field.png"); Demo() : Game() { Mix_Music *music = Mix_LoadMUS("resource/Field.mp3"); Mix_PlayMusic(music, -1); load_gl_context(); + Input *input = new Input(this); + input->print_branch(); + mushroom->print_branch(); + // Game* root = get_root(); + // std::cout << root << std::endl; + } + + std::string get_class_name() + { + return "Demo"; } void load_sdl_context() { Game::load_sdl_context(); - grass.load(); - mushroom.load(); + grass->load(); + mushroom->load(); } void load_gl_context() { Game::load_gl_context(); - grass.unload(); - mushroom.unload(); + grass->unload(); + mushroom->unload(); /* v0-v1-v2 (front) v2-v3-v0 @@ -488,22 +498,22 @@ struct Demo : Game int speed = 2; if (up_active) { - grass.move(0, -speed); + grass->move(0, -speed); } if (right_active) { - grass.move(speed); + grass->move(speed); } if (down_active) { - grass.move(0, speed); + grass->move(0, speed); } if (left_active) { - grass.move(-speed, 0); + grass->move(-speed, 0); } - grass.update(); - mushroom.update(); + grass->update(); + mushroom->update(); SDL_RenderPresent(renderer); } frame_count++; @@ -520,10 +530,7 @@ struct Demo : Game }; -Mushroom::Mushroom(Node *parent) : Sprite(parent, "resource/shrooms") -{ - std::cout << "Constructing Mushroom with parent " << parent << std::endl; -} +Mushroom::Mushroom(Node *parent) : Sprite(parent, "resource/shrooms") {} void Mushroom::update() { @@ -547,8 +554,8 @@ void Mushroom::update() int main(int argc, char *argv[]) { - Demo demo; - demo.run(); - demo.quit(); + Demo *demo = new Demo(); + demo->run(); + demo->quit(); return 0; } diff --git a/demo/Demo.hpp b/demo/Demo.hpp index 0ce6eb9..e84c587 100644 --- a/demo/Demo.hpp +++ b/demo/Demo.hpp @@ -33,6 +33,7 @@ #include "Node.hpp" #include "Location.hpp" #include "Sprite.hpp" +#include "Input.hpp" struct Mushroom : Sprite { @@ -41,5 +42,6 @@ struct Mushroom : Sprite Mushroom (Node*); void update(); + std::string get_class_name() { return "Mushroom"; } }; diff --git a/demo/Makefile b/demo/Makefile index 30f9575..b4d2101 100644 --- a/demo/Makefile +++ b/demo/Makefile @@ -36,15 +36,15 @@ $(SDLGFX2_DIR)%.o: $(SDLGFX2_DIR)%.c $(SDLGFX2_DIR)%.h $(GLEW_DIR)%.o: $(GLEW_DIR)%.c $(GLEW_DIR)%.h $(CC_LINUX) $(CFLAGS) $< -o $@ -$(SFW_SRC_DIR)Sprite.o: $(addprefix $(SFW_SRC_DIR),Node.o Game.o Location.o) -$(SFW_SRC_DIR)Game.o: $(addprefix $(SFW_SRC_DIR),Node.o Sprite.o Configuration.o) -$(SFW_SRC_DIR)Node.o: $(SFW_SRC_DIR)Game.o -$(SFW_SRC_DIR)Configuration.o: $(SFW_SRC_DIR)Node.o -$(SFW_SRC_DIR)Input.o: $(SFW_SRC_DIR)Node.o +$(SFW_SRC_DIR)Sprite.o: $(addprefix $(SFW_SRC_DIR),Node.hpp Game.hpp Location.hpp) +$(SFW_SRC_DIR)Game.o: $(addprefix $(SFW_SRC_DIR),Node.hpp Sprite.hpp Configuration.hpp) +$(SFW_SRC_DIR)Node.o: $(addprefix $(SFW_SRC_DIR),Game.hpp Configuration.hpp) +$(SFW_SRC_DIR)Configuration.o: $(SFW_SRC_DIR)Node.hpp +$(SFW_SRC_DIR)Input.o: $(SFW_SRC_DIR)Node.hpp $(SFW_SRC_DIR)%.o: $(addprefix $(SFW_SRC_DIR),%.cpp %.hpp) $(CPPC_LINUX) $(CPP_FLAGS) $(SDL_FLAGS) $< -o $@ -Demo.o: Demo.cpp Demo.hpp $(addprefix $(SFW_SRC_DIR),Sprite.o Node.o Game.o Location.o) +Demo.o: Demo.cpp Demo.hpp $(addprefix $(SFW_SRC_DIR),Sprite.hpp Node.hpp Game.hpp Location.hpp Input.hpp) $(CPPC_LINUX) $(CPP_FLAGS) $(SDL_FLAGS) $< -o $@ linux: Demo.o $(addprefix $(SFW_SRC_DIR),Sprite.o Node.o Game.o Location.o Configuration.o Input.o) \ diff --git a/src/Configuration.cpp b/src/Configuration.cpp index c20b489..1778ae6 100644 --- a/src/Configuration.cpp +++ b/src/Configuration.cpp @@ -4,8 +4,8 @@ Configuration::Configuration(Node *parent) : Configuration(parent, "config") {} Configuration::Configuration(Node *parent, fs::path path) : Node(parent) { - std::cout << "Constructing Configuration with parent " << parent << - " and path " << path << std::endl; + // std::cout << "Constructing Configuration with parent " << parent << + // " and path " << path << std::endl; config_path = path; set_defaults(); load(); diff --git a/src/Configuration.hpp b/src/Configuration.hpp index 1502631..8275402 100644 --- a/src/Configuration.hpp +++ b/src/Configuration.hpp @@ -15,6 +15,7 @@ struct Configuration : Node nlohmann::json sys_config, config; fs::path config_path; int tab_width = 4; + std::string class_name = "Configuration"; Configuration(Node*); Configuration(Node*, fs::path); @@ -23,6 +24,8 @@ struct Configuration : Node void load(fs::path path); void write(); void write(fs::path path); + std::string get_class_name() { return "Configuration"; } + }; #endif diff --git a/src/Game.cpp b/src/Game.cpp index eab5883..6cc13f6 100644 --- a/src/Game.cpp +++ b/src/Game.cpp @@ -1,6 +1,6 @@ #include "Game.hpp" -Game::Game() : configuration(this) +Game::Game() { std::cout << "GLEW " << glewGetString(GLEW_VERSION) << std::endl; putenv("SDL_VIDEO_X11_LEGACY_FULLSCREEN=0"); diff --git a/src/Game.hpp b/src/Game.hpp index 9ce98ac..0626906 100644 --- a/src/Game.hpp +++ b/src/Game.hpp @@ -34,7 +34,7 @@ struct Game : Node last_frame_length; float frame_length = 1000.0 / framerate; bool done = false, show_framerate = false, is_gl_context = true; - Configuration configuration; + Configuration *configuration = new Configuration(this); Game(); void print_error(std::string); @@ -47,6 +47,7 @@ struct Game : Node virtual void update() = 0; void set_framerate(int); void quit(); + std::string get_class_name() { return "Game"; } template float get_weighted_amount(T amount) diff --git a/src/Location.hpp b/src/Location.hpp index 61022dd..7a29e1a 100644 --- a/src/Location.hpp +++ b/src/Location.hpp @@ -13,9 +13,9 @@ struct Location glm::vec2 overflow; Location() { }; - int get_x(); int get_y(); + std::string get_class_name() { return "Location"; } template void move_ip(T1 dx, T2 dy = 0) diff --git a/src/Node.cpp b/src/Node.cpp index a510c40..1199ec3 100644 --- a/src/Node.cpp +++ b/src/Node.cpp @@ -1,20 +1,17 @@ #include "Node.hpp" +#include "Game.hpp" -Node::Node() -{ - std::cout << "Default constructing Node with parent " << parent << std::endl; -} +Node::Node() : Node(NULL) {} Node::Node(Node *parent) : parent(parent) { - std::cout << "Constructing Node with parent " << parent << std::endl; + std::cout << "Constructing "; + print_branch(); } Configuration* Node::get_configuration() { - Game* game = get_root(); - Configuration* configuration = &game->configuration; - return configuration; + return get_root()->configuration; } Game* Node::get_root() @@ -26,3 +23,21 @@ Game* Node::get_root() } return static_cast(current); } + +void Node::print_branch() +{ + Node *current = this; + while (current != NULL) + { + std::cout << current->get_class_name() << " @ " << current; + if (current->parent != NULL) + { + std::cout << " -> "; + } + else + { + std::cout << std::endl; + } + current = current->parent; + } +} diff --git a/src/Node.hpp b/src/Node.hpp index 148081c..76337bf 100644 --- a/src/Node.hpp +++ b/src/Node.hpp @@ -17,9 +17,9 @@ struct Node Node(Node*); Game *get_root(); Configuration* get_configuration(); + void print_branch(); + virtual std::string get_class_name() { return "Node"; }; }; -#include "Game.hpp" - #endif diff --git a/src/Sprite.cpp b/src/Sprite.cpp index b7d79a5..e3755e2 100644 --- a/src/Sprite.cpp +++ b/src/Sprite.cpp @@ -1,5 +1,12 @@ #include "Sprite.hpp" +Sprite::Sprite(Node *parent) : Node(parent) {} + +Sprite::Sprite(Node *parent, std::string path) : Node(parent) +{ + associate(path); +} + void Sprite::associate(std::string path) { if (fs::is_regular_file(path)) diff --git a/src/Sprite.hpp b/src/Sprite.hpp index c138873..7daec65 100644 --- a/src/Sprite.hpp +++ b/src/Sprite.hpp @@ -21,23 +21,15 @@ struct Sprite : Node int frame_ii = 0; Location location; - Sprite(Node *parent) : Node(parent) - { - std::cout << "Constructing Sprite with parent " << parent << std::endl; - } - - Sprite(Node *parent, std::string path) : Node(parent) - { - std::cout << "Constructing Sprite with " << parent << " and " << path << std::endl; - associate(path); - } - + Sprite(Node*); + Sprite(Node*, std::string); void associate(std::string); void load(); void load_file(fs::path); void add_frame(SDL_Texture*); void unload(); void update(); + std::string get_class_name() { return "Sprite"; } template void move(T1 dx, T2 dy = 0)