create resource management objects on stack

This commit is contained in:
Frank DeMarco 2019-05-18 17:39:47 -04:00
parent 040a2b52ff
commit e45806f8bc
9 changed files with 50 additions and 41 deletions

View File

@ -3,7 +3,7 @@
reset, pause, auto reset, analog d-pad, gamepad config, any key, confirm exit
game, screen wipes, screen offset, screen scale
:) SWEATY HANDS :) OILY SNACKS :) AND BAD HYGIENE :)
*surf's up broccoli* <it's surfing time but it's treacherous>
*surf wizard* <it's surfing time but it's treacherous>
***/
@ -120,18 +120,18 @@ 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 = new Mushroom(this);
Sprite *grass = new Sprite(this, "resource/Field.png");
// Mushroom mushroom = Mushroom(this);
// Sprite grass = Sprite(this, "resource/Field.png");
Demo()
{
Mix_Music *music = Mix_LoadMUS("resource/Field.mp3");
Mix_PlayMusic(music, -1);
load_gl_context();
delegate->subscribe(&Demo::respond, this);
Input *input = new Input(this);
input->print_branch();
mushroom->print_branch();
delegate.subscribe(&Demo::respond, this);
// Input input = Input(this);
// input.print_branch();
// mushroom.print_branch();
}
std::string get_class_name()
@ -142,15 +142,15 @@ struct Demo : Game
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
@ -296,7 +296,7 @@ struct Demo : Game
void respond(SDL_Event& event)
{
if (delegate->compare(event, "context"))
if (delegate.compare(event, "context"))
{
if (is_gl_context)
{
@ -422,22 +422,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++;
@ -478,8 +478,8 @@ void Mushroom::update()
int main(int argc, char *argv[])
{
Demo *demo = new Demo();
demo->run();
demo->quit();
Demo demo = Demo();
demo.run();
demo.quit();
return 0;
}

View File

@ -22,6 +22,7 @@ struct Delegate : Node
void add_subscriber(subscriber, int);
void dispatch();
bool compare(SDL_Event&, std::string);
void remove_subscriber(subscriber, int);
template<typename T>
void subscribe(void(T::*f)(SDL_Event&), T* o, int type = command_event_type)

View File

@ -2,7 +2,7 @@
Game::Game()
{
delegate->subscribe(&Game::handle_quit_event, this, SDL_QUIT);
delegate.subscribe(&Game::handle_quit_event, this, SDL_QUIT);
std::cout << "GLEW " << glewGetString(GLEW_VERSION) << std::endl;
putenv("SDL_VIDEO_X11_LEGACY_FULLSCREEN=0");
putenv("SDL_VIDEO_CENTERED=1");
@ -130,8 +130,8 @@ void Game::run()
last_frame_length = ticks - last_frame_timestamp;
frame_time_overflow = last_frame_length + frame_time_overflow - frame_length;
last_frame_timestamp = ticks;
recorder->update();
delegate->dispatch();
recorder.update();
delegate.dispatch();
update();
}
SDL_Delay(15);

View File

@ -20,6 +20,7 @@
#include "Delegate.hpp"
#include "Display.hpp"
#include "Recorder.hpp"
#include "Input.hpp"
struct Game : Node
{
@ -37,10 +38,11 @@ struct Game : Node
last_frame_length;
float frame_length = 1000.0 / framerate;
bool done = false, show_framerate = false, is_gl_context = true;
Configuration* configuration = new Configuration(this);
Delegate* delegate = new Delegate(this);
Display* display = new Display(this);
Recorder* recorder = new Recorder(this);
Configuration configuration = Configuration(this);
Delegate delegate = Delegate(this);
Display display = Display(this);
Recorder recorder = Recorder(this);
Input input = Input(this);
Game();
void print_error(std::string);

View File

@ -3,8 +3,7 @@
Input::Input(Node *parent) : Node(parent)
{
load_key_map();
get_delegate()->subscribe(&Input::respond, this, SDL_KEYDOWN);
get_delegate()->subscribe(&Input::respond, this);
get_delegate().subscribe(&Input::respond, this, SDL_KEYDOWN);
for (KeyCombination& combination : key_map)
{
print_key_combination(combination);

View File

@ -48,12 +48,12 @@ struct Input : Node
Input(Node*);
void respond(SDL_Event&);
std::string get_class_name() { return "Input"; }
void load_key_map();
SDL_Keycode get_key_code(std::string);
void add_to_key_map(
std::string, SDL_Keycode, bool = false, bool = false, bool = false);
void print_key_combination(KeyCombination&);
std::string get_class_name() { return "Input"; }
};

View File

@ -10,9 +10,15 @@ Node::Node(Node *parent) : parent(parent)
print_branch();
}
Node::~Node()
{
std::cout << "Destructing ";
print_branch();
}
nlohmann::json& Node::get_configuration()
{
return get_root()->configuration->config;
return get_root()->configuration.config;
}
Game* Node::get_root()
@ -25,12 +31,12 @@ Game* Node::get_root()
return static_cast<Game*>(current);
}
Delegate* Node::get_delegate()
Delegate& Node::get_delegate()
{
return get_root()->delegate;
}
Display* Node::get_display()
Display& Node::get_display()
{
return get_root()->display;
}

View File

@ -20,10 +20,11 @@ struct Node
Node();
Node(Node*);
~Node();
Game* get_root();
nlohmann::json& get_configuration();
Delegate* get_delegate();
Display* get_display();
Delegate& get_delegate();
Display& get_display();
void print_branch();
virtual std::string get_class_name() { return "Node"; };

View File

@ -2,16 +2,16 @@
Recorder::Recorder(Node* parent) : Node(parent)
{
get_delegate()->subscribe(&Recorder::respond, this);
get_delegate().subscribe(&Recorder::respond, this);
}
void Recorder::respond(SDL_Event& event)
{
if (get_delegate()->compare(event, "screenshot"))
if (get_delegate().compare(event, "screenshot"))
{
capture_screen();
}
else if (get_delegate()->compare(event, "record"))
else if (get_delegate().compare(event, "record"))
{
if (animation.playing)
{
@ -27,7 +27,7 @@ void Recorder::respond(SDL_Event& event)
void Recorder::capture_screen()
{
nlohmann::json config = get_configuration();
SDL_Surface* surface = get_display()->get_screen_surface();
SDL_Surface* surface = get_display().get_screen_surface();
fs::path directory = config["path"]["screenshots"];
fs::create_directories(directory);
std::string prefix = config["display"]["screenshot-prefix"].
@ -49,7 +49,7 @@ void Recorder::start_recording()
void Recorder::add_frame_to_video()
{
frames.push_back(get_display()->get_screen_surface());
frames.push_back(get_display().get_screen_surface());
}
void Recorder::end_recording()