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,
|
||||
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;
|
||||
}
|
||||
|
|
|
@ -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"; }
|
||||
|
||||
};
|
||||
|
|
|
@ -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) \
|
||||
|
|
|
@ -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();
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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");
|
||||
|
|
|
@ -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<typename T>
|
||||
float get_weighted_amount(T amount)
|
||||
|
|
|
@ -13,9 +13,9 @@ struct Location
|
|||
glm::vec2 overflow;
|
||||
|
||||
Location() { };
|
||||
|
||||
int get_x();
|
||||
int get_y();
|
||||
std::string get_class_name() { return "Location"; }
|
||||
|
||||
template<typename T1, typename T2>
|
||||
void move_ip(T1 dx, T2 dy = 0)
|
||||
|
|
31
src/Node.cpp
31
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<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*);
|
||||
Game *get_root();
|
||||
Configuration* get_configuration();
|
||||
void print_branch();
|
||||
virtual std::string get_class_name() { return "Node"; };
|
||||
|
||||
};
|
||||
|
||||
#include "Game.hpp"
|
||||
|
||||
#endif
|
||||
|
|
|
@ -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))
|
||||
|
|
|
@ -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<typename T1, typename T2 = int>
|
||||
void move(T1 dx, T2 dy = 0)
|
||||
|
|
Loading…
Reference in New Issue