- 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:
parent
605360bae5
commit
d471bdd651
30
src/Box.cpp
30
src/Box.cpp
|
@ -6,22 +6,22 @@ Box::Box(glm::vec2 nw, glm::vec2 size)
|
||||||
set_size(size);
|
set_size(size);
|
||||||
}
|
}
|
||||||
|
|
||||||
float Box::get_x()
|
float Box::get_x() const
|
||||||
{
|
{
|
||||||
return rect.x;
|
return rect.x;
|
||||||
}
|
}
|
||||||
|
|
||||||
float Box::get_y()
|
float Box::get_y() const
|
||||||
{
|
{
|
||||||
return rect.y;
|
return rect.y;
|
||||||
}
|
}
|
||||||
|
|
||||||
float Box::get_w()
|
float Box::get_w() const
|
||||||
{
|
{
|
||||||
return rect.w;
|
return rect.w;
|
||||||
}
|
}
|
||||||
|
|
||||||
float Box::get_h()
|
float Box::get_h() const
|
||||||
{
|
{
|
||||||
return rect.h;
|
return rect.h;
|
||||||
}
|
}
|
||||||
|
@ -46,7 +46,7 @@ void Box::set_h(float h)
|
||||||
rect.h = h;
|
rect.h = h;
|
||||||
}
|
}
|
||||||
|
|
||||||
glm::vec2 Box::get_size()
|
glm::vec2 Box::get_size() const
|
||||||
{
|
{
|
||||||
return glm::vec2(get_w(), get_h());
|
return glm::vec2(get_w(), get_h());
|
||||||
}
|
}
|
||||||
|
@ -57,22 +57,22 @@ void Box::set_size(glm::vec2 size)
|
||||||
set_h(size.y);
|
set_h(size.y);
|
||||||
}
|
}
|
||||||
|
|
||||||
float Box::get_top()
|
float Box::get_top() const
|
||||||
{
|
{
|
||||||
return get_y();
|
return get_y();
|
||||||
}
|
}
|
||||||
|
|
||||||
float Box::get_right()
|
float Box::get_right() const
|
||||||
{
|
{
|
||||||
return get_x() + get_w();
|
return get_x() + get_w();
|
||||||
}
|
}
|
||||||
|
|
||||||
float Box::get_bottom()
|
float Box::get_bottom() const
|
||||||
{
|
{
|
||||||
return get_y() + get_h();
|
return get_y() + get_h();
|
||||||
}
|
}
|
||||||
|
|
||||||
float Box::get_left()
|
float Box::get_left() const
|
||||||
{
|
{
|
||||||
return get_x();
|
return get_x();
|
||||||
}
|
}
|
||||||
|
@ -97,32 +97,32 @@ void Box::set_left(float left)
|
||||||
set_x(left);
|
set_x(left);
|
||||||
}
|
}
|
||||||
|
|
||||||
glm::vec2 Box::get_nw()
|
glm::vec2 Box::get_nw() const
|
||||||
{
|
{
|
||||||
return glm::vec2(get_x(), get_y());
|
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());
|
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);
|
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());
|
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);
|
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);
|
return glm::vec2(get_x() + get_w() / 2, get_y() + get_h() / 2);
|
||||||
}
|
}
|
||||||
|
|
30
src/Box.hpp
30
src/Box.hpp
|
@ -14,30 +14,30 @@ struct Box
|
||||||
SDL_FRect rect = {0, 0, 0, 0};
|
SDL_FRect rect = {0, 0, 0, 0};
|
||||||
|
|
||||||
Box(glm::vec2 = {0, 0}, glm::vec2 = {0, 0});
|
Box(glm::vec2 = {0, 0}, glm::vec2 = {0, 0});
|
||||||
float get_x();
|
float get_x() const;
|
||||||
float get_y();
|
float get_y() const;
|
||||||
float get_w();
|
float get_w() const;
|
||||||
float get_h();
|
float get_h() const;
|
||||||
void set_x(float);
|
void set_x(float);
|
||||||
void set_y(float);
|
void set_y(float);
|
||||||
void set_w(float);
|
void set_w(float);
|
||||||
void set_h(float);
|
void set_h(float);
|
||||||
glm::vec2 get_size();
|
glm::vec2 get_size() const;
|
||||||
void set_size(glm::vec2);
|
void set_size(glm::vec2);
|
||||||
float get_top();
|
float get_top() const;
|
||||||
float get_right();
|
float get_right() const;
|
||||||
float get_bottom();
|
float get_bottom() const;
|
||||||
float get_left();
|
float get_left() const;
|
||||||
void set_top(float);
|
void set_top(float);
|
||||||
void set_right(float);
|
void set_right(float);
|
||||||
void set_bottom(float);
|
void set_bottom(float);
|
||||||
void set_left(float);
|
void set_left(float);
|
||||||
glm::vec2 get_nw();
|
glm::vec2 get_nw() const;
|
||||||
glm::vec2 get_north();
|
glm::vec2 get_north() const;
|
||||||
glm::vec2 get_east();
|
glm::vec2 get_east() const;
|
||||||
glm::vec2 get_south();
|
glm::vec2 get_south() const;
|
||||||
glm::vec2 get_west();
|
glm::vec2 get_west() const;
|
||||||
glm::vec2 get_center();
|
glm::vec2 get_center() const;
|
||||||
void set_nw(glm::vec2);
|
void set_nw(glm::vec2);
|
||||||
void set_north(glm::vec2);
|
void set_north(glm::vec2);
|
||||||
void set_east(glm::vec2);
|
void set_east(glm::vec2);
|
||||||
|
|
23
src/Game.cpp
23
src/Game.cpp
|
@ -9,6 +9,7 @@
|
||||||
|
|
||||||
Game::Game()
|
Game::Game()
|
||||||
{
|
{
|
||||||
|
frame_length_history.reserve(5000);
|
||||||
set_framerate(get_configuration()["display"]["fps"]);
|
set_framerate(get_configuration()["display"]["fps"]);
|
||||||
delegate.subscribe(&Game::handle_quit_event, this, SDL_QUIT);
|
delegate.subscribe(&Game::handle_quit_event, this, SDL_QUIT);
|
||||||
SDL_Log("GLEW %s", glewGetString(GLEW_VERSION));
|
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);
|
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()
|
void Game::load_sdl_context()
|
||||||
{
|
{
|
||||||
if (glcontext != NULL)
|
if (glcontext != NULL)
|
||||||
|
@ -408,10 +418,12 @@ void Game::run()
|
||||||
|
|
||||||
#if defined(__EMSCRIPTEN__)
|
#if defined(__EMSCRIPTEN__)
|
||||||
|
|
||||||
|
SDL_Log("using emscripten main loop");
|
||||||
emscripten_set_main_loop_arg(&loop, this, -1, true);
|
emscripten_set_main_loop_arg(&loop, this, -1, true);
|
||||||
|
|
||||||
#else
|
#else
|
||||||
|
|
||||||
|
SDL_Log("using standard main loop");
|
||||||
while (not done)
|
while (not done)
|
||||||
{
|
{
|
||||||
frame(SDL_GetTicks());
|
frame(SDL_GetTicks());
|
||||||
|
@ -424,20 +436,25 @@ void Game::run()
|
||||||
|
|
||||||
void Game::frame(float ticks)
|
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;
|
// ", frame_time_overflow: " << frame_time_overflow;
|
||||||
if (ticks - last_frame_timestamp + frame_time_overflow >= frame_length)
|
if (ticks - last_frame_timestamp + frame_time_overflow >= frame_length)
|
||||||
{
|
{
|
||||||
last_frame_length = ticks - last_frame_timestamp;
|
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;
|
frame_time_overflow = last_frame_length + frame_time_overflow - frame_length;
|
||||||
last_frame_timestamp = ticks;
|
last_frame_timestamp = ticks;
|
||||||
|
// std::cout << ", last_frame_length: " << last_frame_length << " [rendering frame]";
|
||||||
recorder.update();
|
recorder.update();
|
||||||
delegate.dispatch();
|
delegate.dispatch();
|
||||||
update();
|
update();
|
||||||
if (frame_time_overflow > frame_length)
|
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;
|
frame_time_overflow = 0;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -63,12 +63,14 @@ struct Game : Node
|
||||||
Display display = Display(this);
|
Display display = Display(this);
|
||||||
Recorder recorder = Recorder(this);
|
Recorder recorder = Recorder(this);
|
||||||
Input input = Input(this);
|
Input input = Input(this);
|
||||||
|
std::vector<float> frame_length_history;
|
||||||
|
|
||||||
Game();
|
Game();
|
||||||
~Game();
|
~Game();
|
||||||
void print_error(std::string);
|
void print_error(std::string);
|
||||||
void print_sdl_error(std::string);
|
void print_sdl_error(std::string);
|
||||||
void print_gl_attributes();
|
void print_gl_attributes();
|
||||||
|
void print_frame_length_history();
|
||||||
void load_sdl_context();
|
void load_sdl_context();
|
||||||
void load_gl_context();
|
void load_gl_context();
|
||||||
bool log_gl_errors(std::string);
|
bool log_gl_errors(std::string);
|
||||||
|
|
13
src/Node.cpp
13
src/Node.cpp
|
@ -15,11 +15,9 @@ Node::Node(Node *parent, bool active) : parent(parent)
|
||||||
print_branch();
|
print_branch();
|
||||||
}
|
}
|
||||||
|
|
||||||
Node::~Node()
|
void Node::set_parent(Node* other)
|
||||||
{
|
{
|
||||||
std::cout << "Destructing ";
|
parent = other;
|
||||||
print_branch();
|
|
||||||
get_delegate().unsubscribe(this);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void Node::activate()
|
void Node::activate()
|
||||||
|
@ -79,3 +77,10 @@ void Node::print_branch()
|
||||||
current = current->parent;
|
current = current->parent;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Node::~Node()
|
||||||
|
{
|
||||||
|
std::cout << "Destructing ";
|
||||||
|
print_branch();
|
||||||
|
get_delegate().unsubscribe(this);
|
||||||
|
}
|
||||||
|
|
|
@ -16,12 +16,12 @@ struct TimeFilter;
|
||||||
struct Node
|
struct Node
|
||||||
{
|
{
|
||||||
|
|
||||||
Node *parent = NULL;
|
Node *parent;
|
||||||
bool active = false;
|
bool active;
|
||||||
|
|
||||||
Node();
|
Node();
|
||||||
Node(Node*, bool = true);
|
Node(Node*, bool = true);
|
||||||
virtual ~Node();
|
void set_parent(Node*);
|
||||||
void activate();
|
void activate();
|
||||||
void deactivate();
|
void deactivate();
|
||||||
bool is_active();
|
bool is_active();
|
||||||
|
@ -31,6 +31,7 @@ struct Node
|
||||||
Display& get_display();
|
Display& get_display();
|
||||||
void print_branch();
|
void print_branch();
|
||||||
virtual std::string get_class_name() { return "Node"; };
|
virtual std::string get_class_name() { return "Node"; };
|
||||||
|
virtual ~Node();
|
||||||
|
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
|
@ -1,6 +1,8 @@
|
||||||
#include "Sprite.hpp"
|
#include "Sprite.hpp"
|
||||||
#include "Game.hpp"
|
#include "Game.hpp"
|
||||||
|
|
||||||
|
Sprite::Sprite() : Sprite(NULL) {}
|
||||||
|
|
||||||
Sprite::Sprite(Node* parent) : Node(parent, true)
|
Sprite::Sprite(Node* parent) : Node(parent, true)
|
||||||
{
|
{
|
||||||
frame_animation.play();
|
frame_animation.play();
|
||||||
|
@ -118,52 +120,52 @@ void Sprite::set_step(glm::vec2 s)
|
||||||
step.y = s.y;
|
step.y = s.y;
|
||||||
}
|
}
|
||||||
|
|
||||||
float Sprite::get_w()
|
float Sprite::get_w() const
|
||||||
{
|
{
|
||||||
return box.get_w();
|
return box.get_w();
|
||||||
}
|
}
|
||||||
|
|
||||||
float Sprite::get_h()
|
float Sprite::get_h() const
|
||||||
{
|
{
|
||||||
return box.get_h();
|
return box.get_h();
|
||||||
}
|
}
|
||||||
|
|
||||||
glm::vec2 Sprite::get_size()
|
glm::vec2 Sprite::get_size() const
|
||||||
{
|
{
|
||||||
return box.get_size();
|
return box.get_size();
|
||||||
}
|
}
|
||||||
|
|
||||||
float Sprite::get_top()
|
float Sprite::get_top() const
|
||||||
{
|
{
|
||||||
return box.get_top();
|
return box.get_top();
|
||||||
}
|
}
|
||||||
|
|
||||||
float Sprite::get_right()
|
float Sprite::get_right() const
|
||||||
{
|
{
|
||||||
return box.get_right();
|
return box.get_right();
|
||||||
}
|
}
|
||||||
|
|
||||||
float Sprite::get_bottom()
|
float Sprite::get_bottom() const
|
||||||
{
|
{
|
||||||
return box.get_bottom();
|
return box.get_bottom();
|
||||||
}
|
}
|
||||||
|
|
||||||
float Sprite::get_left()
|
float Sprite::get_left() const
|
||||||
{
|
{
|
||||||
return box.get_left();
|
return box.get_left();
|
||||||
}
|
}
|
||||||
|
|
||||||
glm::vec2 Sprite::get_nw()
|
glm::vec2 Sprite::get_nw() const
|
||||||
{
|
{
|
||||||
return box.get_nw();
|
return box.get_nw();
|
||||||
}
|
}
|
||||||
|
|
||||||
glm::vec2 Sprite::get_north()
|
glm::vec2 Sprite::get_north() const
|
||||||
{
|
{
|
||||||
return box.get_north();
|
return box.get_north();
|
||||||
}
|
}
|
||||||
|
|
||||||
glm::vec2 Sprite::get_west()
|
glm::vec2 Sprite::get_west() const
|
||||||
{
|
{
|
||||||
return box.get_west();
|
return box.get_west();
|
||||||
}
|
}
|
||||||
|
|
|
@ -27,6 +27,7 @@ struct Sprite : Node
|
||||||
glm::vec2 step = {0, 0};
|
glm::vec2 step = {0, 0};
|
||||||
Uint8 alpha_mod = 255;
|
Uint8 alpha_mod = 255;
|
||||||
|
|
||||||
|
Sprite();
|
||||||
Sprite(Node*);
|
Sprite(Node*);
|
||||||
Sprite(Node*, std::string);
|
Sprite(Node*, std::string);
|
||||||
void set_frame_length(float);
|
void set_frame_length(float);
|
||||||
|
@ -40,16 +41,16 @@ struct Sprite : Node
|
||||||
void unhide();
|
void unhide();
|
||||||
void toggle_hidden();
|
void toggle_hidden();
|
||||||
void set_step(glm::vec2);
|
void set_step(glm::vec2);
|
||||||
float get_w();
|
float get_w() const;
|
||||||
float get_h();
|
float get_h() const;
|
||||||
glm::vec2 get_size();
|
glm::vec2 get_size() const;
|
||||||
float get_top();
|
float get_top() const;
|
||||||
float get_right();
|
float get_right() const;
|
||||||
float get_bottom();
|
float get_bottom() const;
|
||||||
float get_left();
|
float get_left() const;
|
||||||
glm::vec2 get_nw();
|
glm::vec2 get_nw() const;
|
||||||
glm::vec2 get_north();
|
glm::vec2 get_north() const;
|
||||||
glm::vec2 get_west();
|
glm::vec2 get_west() const;
|
||||||
void set_nw(glm::vec2);
|
void set_nw(glm::vec2);
|
||||||
void move(glm::vec2, bool = true);
|
void move(glm::vec2, bool = true);
|
||||||
void update();
|
void update();
|
||||||
|
|
|
@ -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));
|
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)
|
Box sfw::get_texture_box(SDL_Texture* texture)
|
||||||
{
|
{
|
||||||
int w, h;
|
int w, h;
|
||||||
|
|
|
@ -11,6 +11,7 @@
|
||||||
#define GLM_ENABLE_EXPERIMENTAL
|
#define GLM_ENABLE_EXPERIMENTAL
|
||||||
#include "glm/trigonometric.hpp"
|
#include "glm/trigonometric.hpp"
|
||||||
#include "glm/vec2.hpp"
|
#include "glm/vec2.hpp"
|
||||||
|
#include "glm/gtx/vector_angle.hpp"
|
||||||
|
|
||||||
#include "SDL.h"
|
#include "SDL.h"
|
||||||
|
|
||||||
|
@ -20,6 +21,7 @@
|
||||||
namespace sfw
|
namespace sfw
|
||||||
{
|
{
|
||||||
glm::vec2 get_step(glm::vec2, glm::vec2, float);
|
glm::vec2 get_step(glm::vec2, glm::vec2, float);
|
||||||
|
void set_magnitude(glm::vec2&, float);
|
||||||
Box get_texture_box(SDL_Texture*);
|
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*, int, int, int, int = 0xff);
|
||||||
void fill_texture(SDL_Renderer*, SDL_Texture*, SDL_Texture*);
|
void fill_texture(SDL_Renderer*, SDL_Texture*, SDL_Texture*);
|
||||||
|
|
Loading…
Reference in New Issue