- added const qualifier to some box and sprite functions

- store frame length history for debugging
- function for setting Node parent after initialization
- set magnitude helper function
This commit is contained in:
Frank DeMarco 2020-07-13 00:53:12 -04:00
parent 605360bae5
commit d471bdd651
10 changed files with 95 additions and 60 deletions

View File

@ -6,22 +6,22 @@ Box::Box(glm::vec2 nw, glm::vec2 size)
set_size(size);
}
float Box::get_x()
float Box::get_x() const
{
return rect.x;
}
float Box::get_y()
float Box::get_y() const
{
return rect.y;
}
float Box::get_w()
float Box::get_w() const
{
return rect.w;
}
float Box::get_h()
float Box::get_h() const
{
return rect.h;
}
@ -46,7 +46,7 @@ void Box::set_h(float h)
rect.h = h;
}
glm::vec2 Box::get_size()
glm::vec2 Box::get_size() const
{
return glm::vec2(get_w(), get_h());
}
@ -57,22 +57,22 @@ void Box::set_size(glm::vec2 size)
set_h(size.y);
}
float Box::get_top()
float Box::get_top() const
{
return get_y();
}
float Box::get_right()
float Box::get_right() const
{
return get_x() + get_w();
}
float Box::get_bottom()
float Box::get_bottom() const
{
return get_y() + get_h();
}
float Box::get_left()
float Box::get_left() const
{
return get_x();
}
@ -97,32 +97,32 @@ void Box::set_left(float left)
set_x(left);
}
glm::vec2 Box::get_nw()
glm::vec2 Box::get_nw() const
{
return glm::vec2(get_x(), get_y());
}
glm::vec2 Box::get_north()
glm::vec2 Box::get_north() const
{
return glm::vec2(get_x() + get_w() / 2, get_y());
}
glm::vec2 Box::get_east()
glm::vec2 Box::get_east() const
{
return glm::vec2(get_right(), get_y() + get_h() / 2);
}
glm::vec2 Box::get_south()
glm::vec2 Box::get_south() const
{
return glm::vec2(get_x() + get_w() / 2, get_bottom());
}
glm::vec2 Box::get_west()
glm::vec2 Box::get_west() const
{
return glm::vec2(get_x(), get_y() + get_h() / 2);
}
glm::vec2 Box::get_center()
glm::vec2 Box::get_center() const
{
return glm::vec2(get_x() + get_w() / 2, get_y() + get_h() / 2);
}

View File

@ -14,30 +14,30 @@ struct Box
SDL_FRect rect = {0, 0, 0, 0};
Box(glm::vec2 = {0, 0}, glm::vec2 = {0, 0});
float get_x();
float get_y();
float get_w();
float get_h();
float get_x() const;
float get_y() const;
float get_w() const;
float get_h() const;
void set_x(float);
void set_y(float);
void set_w(float);
void set_h(float);
glm::vec2 get_size();
glm::vec2 get_size() const;
void set_size(glm::vec2);
float get_top();
float get_right();
float get_bottom();
float get_left();
float get_top() const;
float get_right() const;
float get_bottom() const;
float get_left() const;
void set_top(float);
void set_right(float);
void set_bottom(float);
void set_left(float);
glm::vec2 get_nw();
glm::vec2 get_north();
glm::vec2 get_east();
glm::vec2 get_south();
glm::vec2 get_west();
glm::vec2 get_center();
glm::vec2 get_nw() const;
glm::vec2 get_north() const;
glm::vec2 get_east() const;
glm::vec2 get_south() const;
glm::vec2 get_west() const;
glm::vec2 get_center() const;
void set_nw(glm::vec2);
void set_north(glm::vec2);
void set_east(glm::vec2);

View File

@ -9,6 +9,7 @@
Game::Game()
{
frame_length_history.reserve(5000);
set_framerate(get_configuration()["display"]["fps"]);
delegate.subscribe(&Game::handle_quit_event, this, SDL_QUIT);
SDL_Log("GLEW %s", glewGetString(GLEW_VERSION));
@ -119,6 +120,15 @@ void Game::print_gl_attributes()
SDL_Log("GL PIXELS: red: %i green: %i blue: %i alpha: %i", r, g, b, a);
}
void Game::print_frame_length_history()
{
for (float& frame_length : frame_length_history)
{
std::cout << frame_length << ", ";
}
std::cout << std::endl;
}
void Game::load_sdl_context()
{
if (glcontext != NULL)
@ -408,10 +418,12 @@ void Game::run()
#if defined(__EMSCRIPTEN__)
SDL_Log("using emscripten main loop");
emscripten_set_main_loop_arg(&loop, this, -1, true);
#else
SDL_Log("using standard main loop");
while (not done)
{
frame(SDL_GetTicks());
@ -424,20 +436,25 @@ void Game::run()
void Game::frame(float ticks)
{
// std::cout << "ticks: " << ticks << ", last_frame_timestamp: " << last_frame_timestamp <<
// std::cout << "frame_length: " << frame_length << ", ticks: " << ticks << ", last_frame_timestamp: " << last_frame_timestamp <<
// ", frame_time_overflow: " << frame_time_overflow;
if (ticks - last_frame_timestamp + frame_time_overflow >= frame_length)
{
last_frame_length = ticks - last_frame_timestamp;
// std::cout << ", last_frame_length: " << last_frame_length << " [rendering frame]";
if (frame_length_history.size() == 5000)
{
frame_length_history.pop_back();
}
frame_length_history.insert(frame_length_history.begin(), last_frame_length);
frame_time_overflow = last_frame_length + frame_time_overflow - frame_length;
last_frame_timestamp = ticks;
// std::cout << ", last_frame_length: " << last_frame_length << " [rendering frame]";
recorder.update();
delegate.dispatch();
update();
if (frame_time_overflow > frame_length)
{
SDL_Log("%i frame(s) dropped", ((int) (frame_time_overflow / frame_length)));
// SDL_Log("%i frame(s) dropped", ((int) (frame_time_overflow / frame_length)));
frame_time_overflow = 0;
}
}

View File

@ -63,12 +63,14 @@ struct Game : Node
Display display = Display(this);
Recorder recorder = Recorder(this);
Input input = Input(this);
std::vector<float> frame_length_history;
Game();
~Game();
void print_error(std::string);
void print_sdl_error(std::string);
void print_gl_attributes();
void print_frame_length_history();
void load_sdl_context();
void load_gl_context();
bool log_gl_errors(std::string);

View File

@ -15,11 +15,9 @@ Node::Node(Node *parent, bool active) : parent(parent)
print_branch();
}
Node::~Node()
void Node::set_parent(Node* other)
{
std::cout << "Destructing ";
print_branch();
get_delegate().unsubscribe(this);
parent = other;
}
void Node::activate()
@ -79,3 +77,10 @@ void Node::print_branch()
current = current->parent;
}
}
Node::~Node()
{
std::cout << "Destructing ";
print_branch();
get_delegate().unsubscribe(this);
}

View File

@ -16,12 +16,12 @@ struct TimeFilter;
struct Node
{
Node *parent = NULL;
bool active = false;
Node *parent;
bool active;
Node();
Node(Node*, bool = true);
virtual ~Node();
void set_parent(Node*);
void activate();
void deactivate();
bool is_active();
@ -31,6 +31,7 @@ struct Node
Display& get_display();
void print_branch();
virtual std::string get_class_name() { return "Node"; };
virtual ~Node();
};

View File

@ -1,6 +1,8 @@
#include "Sprite.hpp"
#include "Game.hpp"
Sprite::Sprite() : Sprite(NULL) {}
Sprite::Sprite(Node* parent) : Node(parent, true)
{
frame_animation.play();
@ -118,52 +120,52 @@ void Sprite::set_step(glm::vec2 s)
step.y = s.y;
}
float Sprite::get_w()
float Sprite::get_w() const
{
return box.get_w();
}
float Sprite::get_h()
float Sprite::get_h() const
{
return box.get_h();
}
glm::vec2 Sprite::get_size()
glm::vec2 Sprite::get_size() const
{
return box.get_size();
}
float Sprite::get_top()
float Sprite::get_top() const
{
return box.get_top();
}
float Sprite::get_right()
float Sprite::get_right() const
{
return box.get_right();
}
float Sprite::get_bottom()
float Sprite::get_bottom() const
{
return box.get_bottom();
}
float Sprite::get_left()
float Sprite::get_left() const
{
return box.get_left();
}
glm::vec2 Sprite::get_nw()
glm::vec2 Sprite::get_nw() const
{
return box.get_nw();
}
glm::vec2 Sprite::get_north()
glm::vec2 Sprite::get_north() const
{
return box.get_north();
}
glm::vec2 Sprite::get_west()
glm::vec2 Sprite::get_west() const
{
return box.get_west();
}

View File

@ -27,6 +27,7 @@ struct Sprite : Node
glm::vec2 step = {0, 0};
Uint8 alpha_mod = 255;
Sprite();
Sprite(Node*);
Sprite(Node*, std::string);
void set_frame_length(float);
@ -40,16 +41,16 @@ struct Sprite : Node
void unhide();
void toggle_hidden();
void set_step(glm::vec2);
float get_w();
float get_h();
glm::vec2 get_size();
float get_top();
float get_right();
float get_bottom();
float get_left();
glm::vec2 get_nw();
glm::vec2 get_north();
glm::vec2 get_west();
float get_w() const;
float get_h() const;
glm::vec2 get_size() const;
float get_top() const;
float get_right() const;
float get_bottom() const;
float get_left() const;
glm::vec2 get_nw() const;
glm::vec2 get_north() const;
glm::vec2 get_west() const;
void set_nw(glm::vec2);
void move(glm::vec2, bool = true);
void update();

View File

@ -6,6 +6,11 @@ glm::vec2 sfw::get_step(glm::vec2 start, glm::vec2 end, float speed)
return glm::vec2(speed * glm::sin(angle), speed * glm::cos(angle));
}
void sfw::set_magnitude(glm::vec2& vector, float magnitude)
{
vector = glm::normalize(vector) * magnitude;
}
Box sfw::get_texture_box(SDL_Texture* texture)
{
int w, h;

View File

@ -11,6 +11,7 @@
#define GLM_ENABLE_EXPERIMENTAL
#include "glm/trigonometric.hpp"
#include "glm/vec2.hpp"
#include "glm/gtx/vector_angle.hpp"
#include "SDL.h"
@ -20,6 +21,7 @@
namespace sfw
{
glm::vec2 get_step(glm::vec2, glm::vec2, float);
void set_magnitude(glm::vec2&, float);
Box get_texture_box(SDL_Texture*);
void fill_texture(SDL_Renderer*, SDL_Texture*, int, int, int, int = 0xff);
void fill_texture(SDL_Renderer*, SDL_Texture*, SDL_Texture*);