class name
This commit is contained in:
parent
e40201cd11
commit
cf695b3d10
|
@ -166,28 +166,38 @@ struct Demo : Game
|
||||||
GLuint vbo, space_texture_id, mvp_id, framerate_texture_id, flat_program,
|
GLuint vbo, space_texture_id, mvp_id, framerate_texture_id, flat_program,
|
||||||
world_program, fake_texture_id;
|
world_program, fake_texture_id;
|
||||||
glm::mat4 projection, view, model = glm::mat4(1.0f), mvp;
|
glm::mat4 projection, view, model = glm::mat4(1.0f), mvp;
|
||||||
Mushroom mushroom = Mushroom(this);
|
Mushroom *mushroom = new Mushroom(this);
|
||||||
Sprite grass = Sprite(this, "resource/Field.png");
|
Sprite *grass = new Sprite(this, "resource/Field.png");
|
||||||
|
|
||||||
Demo() : Game()
|
Demo() : Game()
|
||||||
{
|
{
|
||||||
Mix_Music *music = Mix_LoadMUS("resource/Field.mp3");
|
Mix_Music *music = Mix_LoadMUS("resource/Field.mp3");
|
||||||
Mix_PlayMusic(music, -1);
|
Mix_PlayMusic(music, -1);
|
||||||
load_gl_context();
|
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()
|
void load_sdl_context()
|
||||||
{
|
{
|
||||||
Game::load_sdl_context();
|
Game::load_sdl_context();
|
||||||
grass.load();
|
grass->load();
|
||||||
mushroom.load();
|
mushroom->load();
|
||||||
}
|
}
|
||||||
|
|
||||||
void load_gl_context()
|
void load_gl_context()
|
||||||
{
|
{
|
||||||
Game::load_gl_context();
|
Game::load_gl_context();
|
||||||
grass.unload();
|
grass->unload();
|
||||||
mushroom.unload();
|
mushroom->unload();
|
||||||
/*
|
/*
|
||||||
v0-v1-v2 (front)
|
v0-v1-v2 (front)
|
||||||
v2-v3-v0
|
v2-v3-v0
|
||||||
|
@ -488,22 +498,22 @@ struct Demo : Game
|
||||||
int speed = 2;
|
int speed = 2;
|
||||||
if (up_active)
|
if (up_active)
|
||||||
{
|
{
|
||||||
grass.move(0, -speed);
|
grass->move(0, -speed);
|
||||||
}
|
}
|
||||||
if (right_active)
|
if (right_active)
|
||||||
{
|
{
|
||||||
grass.move(speed);
|
grass->move(speed);
|
||||||
}
|
}
|
||||||
if (down_active)
|
if (down_active)
|
||||||
{
|
{
|
||||||
grass.move(0, speed);
|
grass->move(0, speed);
|
||||||
}
|
}
|
||||||
if (left_active)
|
if (left_active)
|
||||||
{
|
{
|
||||||
grass.move(-speed, 0);
|
grass->move(-speed, 0);
|
||||||
}
|
}
|
||||||
grass.update();
|
grass->update();
|
||||||
mushroom.update();
|
mushroom->update();
|
||||||
SDL_RenderPresent(renderer);
|
SDL_RenderPresent(renderer);
|
||||||
}
|
}
|
||||||
frame_count++;
|
frame_count++;
|
||||||
|
@ -520,10 +530,7 @@ struct Demo : Game
|
||||||
|
|
||||||
};
|
};
|
||||||
|
|
||||||
Mushroom::Mushroom(Node *parent) : Sprite(parent, "resource/shrooms")
|
Mushroom::Mushroom(Node *parent) : Sprite(parent, "resource/shrooms") {}
|
||||||
{
|
|
||||||
std::cout << "Constructing Mushroom with parent " << parent << std::endl;
|
|
||||||
}
|
|
||||||
|
|
||||||
void Mushroom::update()
|
void Mushroom::update()
|
||||||
{
|
{
|
||||||
|
@ -547,8 +554,8 @@ void Mushroom::update()
|
||||||
|
|
||||||
int main(int argc, char *argv[])
|
int main(int argc, char *argv[])
|
||||||
{
|
{
|
||||||
Demo demo;
|
Demo *demo = new Demo();
|
||||||
demo.run();
|
demo->run();
|
||||||
demo.quit();
|
demo->quit();
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
|
@ -33,6 +33,7 @@
|
||||||
#include "Node.hpp"
|
#include "Node.hpp"
|
||||||
#include "Location.hpp"
|
#include "Location.hpp"
|
||||||
#include "Sprite.hpp"
|
#include "Sprite.hpp"
|
||||||
|
#include "Input.hpp"
|
||||||
|
|
||||||
struct Mushroom : Sprite
|
struct Mushroom : Sprite
|
||||||
{
|
{
|
||||||
|
@ -41,5 +42,6 @@ struct Mushroom : Sprite
|
||||||
|
|
||||||
Mushroom (Node*);
|
Mushroom (Node*);
|
||||||
void update();
|
void update();
|
||||||
|
std::string get_class_name() { return "Mushroom"; }
|
||||||
|
|
||||||
};
|
};
|
||||||
|
|
|
@ -36,15 +36,15 @@ $(SDLGFX2_DIR)%.o: $(SDLGFX2_DIR)%.c $(SDLGFX2_DIR)%.h
|
||||||
$(GLEW_DIR)%.o: $(GLEW_DIR)%.c $(GLEW_DIR)%.h
|
$(GLEW_DIR)%.o: $(GLEW_DIR)%.c $(GLEW_DIR)%.h
|
||||||
$(CC_LINUX) $(CFLAGS) $< -o $@
|
$(CC_LINUX) $(CFLAGS) $< -o $@
|
||||||
|
|
||||||
$(SFW_SRC_DIR)Sprite.o: $(addprefix $(SFW_SRC_DIR),Node.o Game.o Location.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.o Sprite.o Configuration.o)
|
$(SFW_SRC_DIR)Game.o: $(addprefix $(SFW_SRC_DIR),Node.hpp Sprite.hpp Configuration.hpp)
|
||||||
$(SFW_SRC_DIR)Node.o: $(SFW_SRC_DIR)Game.o
|
$(SFW_SRC_DIR)Node.o: $(addprefix $(SFW_SRC_DIR),Game.hpp Configuration.hpp)
|
||||||
$(SFW_SRC_DIR)Configuration.o: $(SFW_SRC_DIR)Node.o
|
$(SFW_SRC_DIR)Configuration.o: $(SFW_SRC_DIR)Node.hpp
|
||||||
$(SFW_SRC_DIR)Input.o: $(SFW_SRC_DIR)Node.o
|
$(SFW_SRC_DIR)Input.o: $(SFW_SRC_DIR)Node.hpp
|
||||||
$(SFW_SRC_DIR)%.o: $(addprefix $(SFW_SRC_DIR),%.cpp %.hpp)
|
$(SFW_SRC_DIR)%.o: $(addprefix $(SFW_SRC_DIR),%.cpp %.hpp)
|
||||||
$(CPPC_LINUX) $(CPP_FLAGS) $(SDL_FLAGS) $< -o $@
|
$(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 $@
|
$(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) \
|
linux: Demo.o $(addprefix $(SFW_SRC_DIR),Sprite.o Node.o Game.o Location.o Configuration.o Input.o) \
|
||||||
|
|
|
@ -4,8 +4,8 @@ Configuration::Configuration(Node *parent) : Configuration(parent, "config") {}
|
||||||
|
|
||||||
Configuration::Configuration(Node *parent, fs::path path) : Node(parent)
|
Configuration::Configuration(Node *parent, fs::path path) : Node(parent)
|
||||||
{
|
{
|
||||||
std::cout << "Constructing Configuration with parent " << parent <<
|
// std::cout << "Constructing Configuration with parent " << parent <<
|
||||||
" and path " << path << std::endl;
|
// " and path " << path << std::endl;
|
||||||
config_path = path;
|
config_path = path;
|
||||||
set_defaults();
|
set_defaults();
|
||||||
load();
|
load();
|
||||||
|
|
|
@ -15,6 +15,7 @@ struct Configuration : Node
|
||||||
nlohmann::json sys_config, config;
|
nlohmann::json sys_config, config;
|
||||||
fs::path config_path;
|
fs::path config_path;
|
||||||
int tab_width = 4;
|
int tab_width = 4;
|
||||||
|
std::string class_name = "Configuration";
|
||||||
|
|
||||||
Configuration(Node*);
|
Configuration(Node*);
|
||||||
Configuration(Node*, fs::path);
|
Configuration(Node*, fs::path);
|
||||||
|
@ -23,6 +24,8 @@ struct Configuration : Node
|
||||||
void load(fs::path path);
|
void load(fs::path path);
|
||||||
void write();
|
void write();
|
||||||
void write(fs::path path);
|
void write(fs::path path);
|
||||||
|
std::string get_class_name() { return "Configuration"; }
|
||||||
|
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
#include "Game.hpp"
|
#include "Game.hpp"
|
||||||
|
|
||||||
Game::Game() : configuration(this)
|
Game::Game()
|
||||||
{
|
{
|
||||||
std::cout << "GLEW " << glewGetString(GLEW_VERSION) << std::endl;
|
std::cout << "GLEW " << glewGetString(GLEW_VERSION) << std::endl;
|
||||||
putenv("SDL_VIDEO_X11_LEGACY_FULLSCREEN=0");
|
putenv("SDL_VIDEO_X11_LEGACY_FULLSCREEN=0");
|
||||||
|
|
|
@ -34,7 +34,7 @@ struct Game : Node
|
||||||
last_frame_length;
|
last_frame_length;
|
||||||
float frame_length = 1000.0 / framerate;
|
float frame_length = 1000.0 / framerate;
|
||||||
bool done = false, show_framerate = false, is_gl_context = true;
|
bool done = false, show_framerate = false, is_gl_context = true;
|
||||||
Configuration configuration;
|
Configuration *configuration = new Configuration(this);
|
||||||
|
|
||||||
Game();
|
Game();
|
||||||
void print_error(std::string);
|
void print_error(std::string);
|
||||||
|
@ -47,6 +47,7 @@ struct Game : Node
|
||||||
virtual void update() = 0;
|
virtual void update() = 0;
|
||||||
void set_framerate(int);
|
void set_framerate(int);
|
||||||
void quit();
|
void quit();
|
||||||
|
std::string get_class_name() { return "Game"; }
|
||||||
|
|
||||||
template<typename T>
|
template<typename T>
|
||||||
float get_weighted_amount(T amount)
|
float get_weighted_amount(T amount)
|
||||||
|
|
|
@ -13,9 +13,9 @@ struct Location
|
||||||
glm::vec2 overflow;
|
glm::vec2 overflow;
|
||||||
|
|
||||||
Location() { };
|
Location() { };
|
||||||
|
|
||||||
int get_x();
|
int get_x();
|
||||||
int get_y();
|
int get_y();
|
||||||
|
std::string get_class_name() { return "Location"; }
|
||||||
|
|
||||||
template<typename T1, typename T2>
|
template<typename T1, typename T2>
|
||||||
void move_ip(T1 dx, T2 dy = 0)
|
void move_ip(T1 dx, T2 dy = 0)
|
||||||
|
|
31
src/Node.cpp
31
src/Node.cpp
|
@ -1,20 +1,17 @@
|
||||||
#include "Node.hpp"
|
#include "Node.hpp"
|
||||||
|
#include "Game.hpp"
|
||||||
|
|
||||||
Node::Node()
|
Node::Node() : Node(NULL) {}
|
||||||
{
|
|
||||||
std::cout << "Default constructing Node with parent " << parent << std::endl;
|
|
||||||
}
|
|
||||||
|
|
||||||
Node::Node(Node *parent) : parent(parent)
|
Node::Node(Node *parent) : parent(parent)
|
||||||
{
|
{
|
||||||
std::cout << "Constructing Node with parent " << parent << std::endl;
|
std::cout << "Constructing ";
|
||||||
|
print_branch();
|
||||||
}
|
}
|
||||||
|
|
||||||
Configuration* Node::get_configuration()
|
Configuration* Node::get_configuration()
|
||||||
{
|
{
|
||||||
Game* game = get_root();
|
return get_root()->configuration;
|
||||||
Configuration* configuration = &game->configuration;
|
|
||||||
return configuration;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
Game* Node::get_root()
|
Game* Node::get_root()
|
||||||
|
@ -26,3 +23,21 @@ Game* Node::get_root()
|
||||||
}
|
}
|
||||||
return static_cast<Game*>(current);
|
return static_cast<Game*>(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;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
@ -17,9 +17,9 @@ struct Node
|
||||||
Node(Node*);
|
Node(Node*);
|
||||||
Game *get_root();
|
Game *get_root();
|
||||||
Configuration* get_configuration();
|
Configuration* get_configuration();
|
||||||
|
void print_branch();
|
||||||
|
virtual std::string get_class_name() { return "Node"; };
|
||||||
|
|
||||||
};
|
};
|
||||||
|
|
||||||
#include "Game.hpp"
|
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|
|
@ -1,5 +1,12 @@
|
||||||
#include "Sprite.hpp"
|
#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)
|
void Sprite::associate(std::string path)
|
||||||
{
|
{
|
||||||
if (fs::is_regular_file(path))
|
if (fs::is_regular_file(path))
|
||||||
|
|
|
@ -21,23 +21,15 @@ struct Sprite : Node
|
||||||
int frame_ii = 0;
|
int frame_ii = 0;
|
||||||
Location location;
|
Location location;
|
||||||
|
|
||||||
Sprite(Node *parent) : Node(parent)
|
Sprite(Node*);
|
||||||
{
|
Sprite(Node*, std::string);
|
||||||
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);
|
|
||||||
}
|
|
||||||
|
|
||||||
void associate(std::string);
|
void associate(std::string);
|
||||||
void load();
|
void load();
|
||||||
void load_file(fs::path);
|
void load_file(fs::path);
|
||||||
void add_frame(SDL_Texture*);
|
void add_frame(SDL_Texture*);
|
||||||
void unload();
|
void unload();
|
||||||
void update();
|
void update();
|
||||||
|
std::string get_class_name() { return "Sprite"; }
|
||||||
|
|
||||||
template<typename T1, typename T2 = int>
|
template<typename T1, typename T2 = int>
|
||||||
void move(T1 dx, T2 dy = 0)
|
void move(T1 dx, T2 dy = 0)
|
||||||
|
|
Loading…
Reference in New Issue