attributes can be accessed as a typed vector; box set gl mode function; mouse events are suppressed in addition to key events when input is suppressed
This commit is contained in:
parent
863db5467b
commit
03d179eed4
|
@ -210,6 +210,14 @@ namespace sb
|
|||
add({std::initializer_list<XType>({coordinate_0, coordinate_1, coordinates ...})});
|
||||
}
|
||||
|
||||
/* Return the attributes as a reference to a typed vector. If the type is not the alternative in use by the
|
||||
* attributes, std::bad_variant_access will be thrown. */
|
||||
template<typename VertexType>
|
||||
operator const std::vector<VertexType>&() const
|
||||
{
|
||||
return std::get<std::vector<VertexType>>(vertices);
|
||||
}
|
||||
|
||||
void extend(const Attributes&, std::size_t = 1);
|
||||
void index(GLint);
|
||||
GLint index() const;
|
||||
|
|
35
src/Box.cpp
35
src/Box.cpp
|
@ -1,24 +1,39 @@
|
|||
#include "extension.hpp"
|
||||
#include "Segment.hpp"
|
||||
/* /\ +--------------------------------------------------------------+
|
||||
____/ \____ /| - zlib/MIT/Unlicenced game framework licensed to freely use, |
|
||||
\ / / | copy, modify and sell without restriction |
|
||||
+--\ ^__^ /--+ | |
|
||||
| ~/ \~ | | - originally created at [http://nugget.fun] |
|
||||
| ~~~~~~~~~~~~ | +--------------------------------------------------------------+
|
||||
| SPACE ~~~~~ | /
|
||||
| ~~~~~~~ BOX |/
|
||||
+-------------*/
|
||||
|
||||
#include "Box.hpp"
|
||||
|
||||
/* Construct a Box by giving a corner coordinate (top left in SDL coordinates, bottom left in GL coordinates)
|
||||
* and size (as width, height). The gl argument indicates that the bottom left is (0, 0) rather than the default
|
||||
* case where the top left is (0, 0). */
|
||||
* and size (as width, height). The gl argument indicates that the Y-coordinate increases from the bottom of
|
||||
* the screen to the top rather than the default case where it increases from the top to the bottom. */
|
||||
Box::Box(const glm::vec2& corner, const glm::vec2& size, bool use_gl_coordinates)
|
||||
{
|
||||
x = corner.x;
|
||||
y = corner.y;
|
||||
w = size.x;
|
||||
h = size.y;
|
||||
this->use_gl_coordinates = use_gl_coordinates;
|
||||
gl(use_gl_coordinates);
|
||||
}
|
||||
|
||||
/* Construct a Box by passing an SDL_Rect struct, which is of the form {x, y, w, h} and limited to int arguments */
|
||||
Box::Box(const SDL_Rect& rect, bool use_gl_coordinates) : Box({rect.x, rect.y}, {rect.w, rect.h}, use_gl_coordinates) {}
|
||||
|
||||
/* Returns true if GL coordinate mode was set, meaning the lower left is (0, 0), and coordinates go up from there.
|
||||
* Otherwise, returns false, meaning SDL mode is set, so the top left is (0, 0), and coordinates go down from there. */
|
||||
/* Set GL mode to true, meaning the Y-coordinate increases from the bottom of the screen to the top, or false,
|
||||
* meaning SDL mode where the Y-coordinate increases from the top of the screen to the bottom. */
|
||||
void Box::gl(bool use_gl_coordinates)
|
||||
{
|
||||
this->use_gl_coordinates = use_gl_coordinates;
|
||||
}
|
||||
|
||||
/* Returns true if GL coordinate mode was set, meaning the Y-coordinate increases from the bottom of the screen to
|
||||
* the top, or false, meaning SDL mode where the Y-coordinate increases from the top of the screen to the bottom. */
|
||||
bool Box::gl() const
|
||||
{
|
||||
return use_gl_coordinates;
|
||||
|
@ -82,7 +97,7 @@ float Box::area() const
|
|||
/* Return the y coordinate representing the top of the box */
|
||||
float Box::top() const
|
||||
{
|
||||
/* if lower left is (0, 0), use the bottom to determine this value */
|
||||
/* if Y-coordinate increases from bottom to top, use the bottom to determine this value */
|
||||
if (gl())
|
||||
{
|
||||
return bottom() + height();
|
||||
|
@ -103,7 +118,7 @@ float Box::right() const
|
|||
/* Return the y coordinate representing the bottom of the box */
|
||||
float Box::bottom() const
|
||||
{
|
||||
/* if the lower left is (0, 0), use the stored Y-coordinate */
|
||||
/* if Y-coordinate increases from bottom to top, use the stored Y-coordinate */
|
||||
if (gl())
|
||||
{
|
||||
return y;
|
||||
|
@ -130,7 +145,7 @@ float Box::cx() const
|
|||
/* Return the y component of the center coordinates */
|
||||
float Box::cy() const
|
||||
{
|
||||
/* if the lower left is (0, 0), add from the bottom */
|
||||
/* if Y-coordinate increases from bottom to top, add from the bottom */
|
||||
if (gl())
|
||||
{
|
||||
return bottom() + height() / 2.0f;
|
||||
|
|
14
src/Box.hpp
14
src/Box.hpp
|
@ -1,3 +1,13 @@
|
|||
/* /\ +--------------------------------------------------------------+
|
||||
____/ \____ /| - zlib/MIT/Unlicenced game framework licensed to freely use, |
|
||||
\ / / | copy, modify and sell without restriction |
|
||||
+--\ ^__^ /--+ | |
|
||||
| ~/ \~ | | - originally created at [http://nugget.fun] |
|
||||
| ~~~~~~~~~~~~ | +--------------------------------------------------------------+
|
||||
| SPACE ~~~~~ | /
|
||||
| ~~~~~~~ BOX |/
|
||||
+-------------*/
|
||||
|
||||
#ifndef Box_h_
|
||||
#define Box_h_
|
||||
|
||||
|
@ -24,6 +34,7 @@ public:
|
|||
|
||||
Box(const glm::vec2& = {0, 0}, const glm::vec2& = {0, 0}, bool = false);
|
||||
Box(const SDL_Rect&, bool = false);
|
||||
void gl(bool);
|
||||
bool gl() const;
|
||||
float width() const;
|
||||
void width(float);
|
||||
|
@ -92,4 +103,7 @@ namespace std
|
|||
std::ostream& operator<<(std::ostream&, const Box&);
|
||||
}
|
||||
|
||||
#include "extension.hpp"
|
||||
#include "Segment.hpp"
|
||||
|
||||
#endif
|
||||
|
|
|
@ -1,6 +1,12 @@
|
|||
#include <fstream>
|
||||
#include <ostream>
|
||||
#include <iomanip>
|
||||
/* /\ +--------------------------------------------------------------+
|
||||
____/ \____ /| - zlib/MIT/Unlicenced game framework licensed to freely use, |
|
||||
\ / / | copy, modify and sell without restriction |
|
||||
+--\ ^__^ /--+ | |
|
||||
| ~/ \~ | | - originally created at [http://nugget.fun] |
|
||||
| ~~~~~~~~~~~~ | +--------------------------------------------------------------+
|
||||
| SPACE ~~~~~ | /
|
||||
| ~~~~~~~ BOX |/
|
||||
+-------------*/
|
||||
|
||||
#include "Configuration.hpp"
|
||||
|
||||
|
|
|
@ -1,6 +1,19 @@
|
|||
#ifndef Configuration_h_
|
||||
#define Configuration_h_
|
||||
/* /\ +--------------------------------------------------------------+
|
||||
____/ \____ /| - zlib/MIT/Unlicenced game framework licensed to freely use, |
|
||||
\ / / | copy, modify and sell without restriction |
|
||||
+--\ ^__^ /--+ | |
|
||||
| ~/ \~ | | - originally created at [http://nugget.fun] |
|
||||
| ~~~~~~~~~~~~ | +--------------------------------------------------------------+
|
||||
| SPACE ~~~~~ | /
|
||||
| ~~~~~~~ BOX |/
|
||||
+-------------*/
|
||||
|
||||
#ifndef SB_CONFIGURATION_H_
|
||||
#define SB_CONFIGURATION_H_
|
||||
|
||||
#include <fstream>
|
||||
#include <ostream>
|
||||
#include <iomanip>
|
||||
#include "json/json.hpp"
|
||||
#include "filesystem.hpp"
|
||||
#include "Node.hpp"
|
||||
|
|
|
@ -1,8 +1,14 @@
|
|||
#include "Input.hpp"
|
||||
#include "Game.hpp"
|
||||
#include "Delegate.hpp"
|
||||
/* /\ +--------------------------------------------------------------+
|
||||
____/ \____ /| - zlib/MIT/Unlicenced game framework licensed to freely use, |
|
||||
\ / / | copy, modify and sell without restriction |
|
||||
+--\ ^__^ /--+ | |
|
||||
| ~/ \~ | | - originally created at [http://nugget.fun] |
|
||||
| ~~~~~~~~~~~~ | +--------------------------------------------------------------+
|
||||
| SPACE ~~~~~ | /
|
||||
| ~~~~~~~ BOX |/
|
||||
+-------------*/
|
||||
|
||||
std::uint32_t Delegate::command_event_type = SDL_RegisterEvents(1);
|
||||
#include "Delegate.hpp"
|
||||
|
||||
Delegate::Delegate(Node* parent) : Node(parent) {}
|
||||
|
||||
|
@ -20,7 +26,9 @@ void Delegate::dispatch()
|
|||
SDL_Event event;
|
||||
while (SDL_PollEvent(&event))
|
||||
{
|
||||
if (get_input().is_suppressed() && (event.type == SDL_KEYDOWN || event.type == SDL_KEYUP))
|
||||
/* Don't even notify subscribers of the event if input is suppressed and the input is a key or mouse click. */
|
||||
if (get_input().is_suppressed() && (event.type == SDL_KEYDOWN || event.type == SDL_KEYUP ||
|
||||
event.type == SDL_MOUSEBUTTONDOWN || event.type == SDL_MOUSEBUTTONUP))
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
|
|
@ -1,5 +1,15 @@
|
|||
#ifndef Delegate_h_
|
||||
#define Delegate_h_
|
||||
/* /\ +--------------------------------------------------------------+
|
||||
____/ \____ /| - zlib/MIT/Unlicenced game framework licensed to freely use, |
|
||||
\ / / | copy, modify and sell without restriction |
|
||||
+--\ ^__^ /--+ | |
|
||||
| ~/ \~ | | - originally created at [http://nugget.fun] |
|
||||
| ~~~~~~~~~~~~ | +--------------------------------------------------------------+
|
||||
| SPACE ~~~~~ | /
|
||||
| ~~~~~~~ BOX |/
|
||||
+-------------*/
|
||||
|
||||
#ifndef SB_DELEGATE_H_
|
||||
#define SB_DELEGATE_H_
|
||||
|
||||
#include <string>
|
||||
#include <map>
|
||||
|
@ -7,9 +17,7 @@
|
|||
#include <functional>
|
||||
#include <typeinfo>
|
||||
#include <typeindex>
|
||||
|
||||
#include "SDL.h"
|
||||
|
||||
#include "Node.hpp"
|
||||
|
||||
struct Subscriber
|
||||
|
@ -18,13 +26,18 @@ struct Subscriber
|
|||
Node* o;
|
||||
};
|
||||
|
||||
struct Delegate : Node
|
||||
class Delegate : public Node
|
||||
{
|
||||
|
||||
private:
|
||||
|
||||
std::map<std::uint32_t, std::vector<Subscriber>> subscribers;
|
||||
static std::uint32_t command_event_type;
|
||||
bool cancelling_propagation = false;
|
||||
|
||||
public:
|
||||
|
||||
inline static std::uint32_t command_event_type = SDL_RegisterEvents(1);
|
||||
|
||||
Delegate(Node*);
|
||||
void add_subscriber(Subscriber, std::uint32_t);
|
||||
void dispatch();
|
||||
|
@ -49,10 +62,6 @@ struct Delegate : Node
|
|||
{
|
||||
for (auto s = t->second.begin(); s != t->second.end();)
|
||||
{
|
||||
// const std::type_info& info = typeid(p);
|
||||
// std::cout << p << " " << s->o << " " << s->f.target_type().name() << " " << info.name() << " " <<
|
||||
// std::type_index(info).name() << " " <<
|
||||
// (s->f.target_type() == info) << std::endl;
|
||||
if (p == s->o)
|
||||
{
|
||||
s = t->second.erase(s);
|
||||
|
@ -67,4 +76,7 @@ struct Delegate : Node
|
|||
|
||||
};
|
||||
|
||||
#include "Input.hpp"
|
||||
#include "Game.hpp"
|
||||
|
||||
#endif
|
||||
|
|
|
@ -544,10 +544,7 @@ void Game::run()
|
|||
|
||||
void Game::frame(float ticks)
|
||||
{
|
||||
// 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)
|
||||
// if (ticks - last_frame_timestamp >= frame_length)
|
||||
{
|
||||
last_frame_length = ticks - last_frame_timestamp;
|
||||
if (frame_length_history.size() == 5000)
|
||||
|
@ -557,7 +554,6 @@ void Game::frame(float ticks)
|
|||
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]";
|
||||
if (last_frame_length < 1000)
|
||||
{
|
||||
recorder.update();
|
||||
|
@ -575,7 +571,7 @@ void Game::frame(float ticks)
|
|||
}
|
||||
// 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_count_this_second++;
|
||||
|
|
|
@ -1,9 +1,14 @@
|
|||
#include "extension.hpp"
|
||||
#include "Configuration.hpp"
|
||||
#include "Delegate.hpp"
|
||||
#include "Input.hpp"
|
||||
/* /\ +--------------------------------------------------------------+
|
||||
____/ \____ /| - zlib/MIT/Unlicenced game framework licensed to freely use, |
|
||||
\ / / | copy, modify and sell without restriction |
|
||||
+--\ ^__^ /--+ | |
|
||||
| ~/ \~ | | - originally created at [http://nugget.fun] |
|
||||
| ~~~~~~~~~~~~ | +--------------------------------------------------------------+
|
||||
| SPACE ~~~~~ | /
|
||||
| ~~~~~~~ BOX |/
|
||||
+-------------*/
|
||||
|
||||
std::string Input::any = "any";
|
||||
#include "Input.hpp"
|
||||
|
||||
Input::Input(Node *parent) : Node(parent)
|
||||
{
|
||||
|
@ -72,8 +77,7 @@ int Input::get_key_code(const std::string& name)
|
|||
}
|
||||
}
|
||||
|
||||
void Input::add_to_key_map(
|
||||
std::string command, SDL_Keycode key_code, bool ctrl, bool shift, bool alt)
|
||||
void Input::add_to_key_map(std::string command, SDL_Keycode key_code, bool ctrl, bool shift, bool alt)
|
||||
{
|
||||
key_map.push_back((KeyCombination){command, key_code, ctrl, shift, alt});
|
||||
}
|
||||
|
@ -94,8 +98,7 @@ void Input::respond(SDL_Event &event)
|
|||
{
|
||||
for (KeyCombination& combination : key_map)
|
||||
{
|
||||
if (sym == combination.key && (!combination.ctrl || ctrl) && (!combination.shift || shift) &&
|
||||
(!combination.alt || alt))
|
||||
if (sym == combination.key && (!combination.ctrl || ctrl) && (!combination.shift || shift) && (!combination.alt || alt))
|
||||
{
|
||||
post_command(combination.command, cancel);
|
||||
if (!sb::is_in_container(system_any_key_ignore, combination.command) && !found_command &&
|
||||
|
|
|
@ -1,13 +1,21 @@
|
|||
#ifndef Input_h_
|
||||
#define Input_h_
|
||||
/* /\ +--------------------------------------------------------------+
|
||||
____/ \____ /| - zlib/MIT/Unlicenced game framework licensed to freely use, |
|
||||
\ / / | copy, modify and sell without restriction |
|
||||
+--\ ^__^ /--+ | |
|
||||
| ~/ \~ | | - originally created at [http://nugget.fun] |
|
||||
| ~~~~~~~~~~~~ | +--------------------------------------------------------------+
|
||||
| SPACE ~~~~~ | /
|
||||
| ~~~~~~~ BOX |/
|
||||
+-------------*/
|
||||
|
||||
#ifndef SB_INPUT_H_
|
||||
#define SB_INPUT_H_
|
||||
|
||||
#include <string>
|
||||
#include <map>
|
||||
#include <functional>
|
||||
#include <algorithm>
|
||||
|
||||
#include "SDL.h"
|
||||
|
||||
#include "Node.hpp"
|
||||
#include "Animation.hpp"
|
||||
|
||||
|
@ -20,9 +28,11 @@ struct KeyCombination
|
|||
bool alt;
|
||||
};
|
||||
|
||||
struct Input : Node
|
||||
class Input : public Node
|
||||
{
|
||||
|
||||
|
||||
private:
|
||||
|
||||
std::map<std::string, SDL_Keycode> key_ids
|
||||
{
|
||||
{"up", SDLK_UP},
|
||||
|
@ -45,16 +55,18 @@ struct Input : Node
|
|||
{"space", SDLK_SPACE}
|
||||
};
|
||||
std::vector<KeyCombination> key_map;
|
||||
static std::string any;
|
||||
bool suppressed = false;
|
||||
|
||||
public:
|
||||
|
||||
inline static std::string any = "any";
|
||||
Animation unsuppress_animation = Animation(&Input::unsuppress, this);
|
||||
|
||||
Input(Node*);
|
||||
void respond(SDL_Event&);
|
||||
void load_key_map();
|
||||
SDL_Keycode get_key_code(const std::string&);
|
||||
void add_to_key_map(
|
||||
std::string, SDL_Keycode, bool = false, bool = false, bool = false);
|
||||
void add_to_key_map(std::string, SDL_Keycode, bool = false, bool = false, bool = false);
|
||||
void print_key_combination(const KeyCombination&) const;
|
||||
static void post_command(std::string&, const bool&);
|
||||
void suppress();
|
||||
|
@ -64,4 +76,8 @@ struct Input : Node
|
|||
|
||||
};
|
||||
|
||||
#include "extension.hpp"
|
||||
#include "Configuration.hpp"
|
||||
#include "Delegate.hpp"
|
||||
|
||||
#endif
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
#ifndef Node_h_
|
||||
#define Node_h_
|
||||
#ifndef SB_NODE_H_
|
||||
#define SB_NODE_H_
|
||||
|
||||
#include <string>
|
||||
#include <iostream>
|
||||
|
@ -10,9 +10,9 @@
|
|||
#include "filesystem.hpp"
|
||||
|
||||
class Game;
|
||||
struct Delegate;
|
||||
class Delegate;
|
||||
class Display;
|
||||
struct Input;
|
||||
class Input;
|
||||
class Box;
|
||||
struct Audio;
|
||||
|
||||
|
|
|
@ -33,12 +33,12 @@ void Texture::generate()
|
|||
GLObject::generate(glGenTextures);
|
||||
}
|
||||
|
||||
/* Generate a GL_TEXTURE_2D texture ID and allocate GL_RGB8 storage for the given size */
|
||||
/* Generate a GL_TEXTURE_2D texture ID and allocate GL_RGBA8 storage for the given size */
|
||||
void Texture::generate(glm::vec2 size)
|
||||
{
|
||||
generate();
|
||||
bind();
|
||||
glTexStorage2D(GL_TEXTURE_2D, 1, GL_RGB8, size.x, size.y);
|
||||
glTexStorage2D(GL_TEXTURE_2D, 1, GL_RGBA8, size.x, size.y);
|
||||
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
|
||||
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
|
||||
sb::Log::gl_errors();
|
||||
|
@ -75,7 +75,8 @@ void Texture::load(SDL_Surface* surface)
|
|||
{
|
||||
load(surface->pixels, {surface->w, surface->h}, GL_RGBA, GL_UNSIGNED_BYTE);
|
||||
std::ostringstream message;
|
||||
message << "loaded " << path << " (" << surface->w << "x" << surface->h << ")";
|
||||
message << "loaded " << path << " (" << surface->w << "x" << surface->h << ", " <<
|
||||
SDL_GetPixelFormatName(surface->format->format) << ")";
|
||||
sb::Log::log(message);
|
||||
}
|
||||
|
||||
|
@ -88,7 +89,7 @@ void Texture::load(void* pixels, glm::vec2 size, GLenum format, GLenum type)
|
|||
}
|
||||
bind();
|
||||
glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, size.x, size.y, format, type, pixels);
|
||||
sb::Log::gl_errors();
|
||||
sb::Log::gl_errors("after loading texture");
|
||||
}
|
||||
|
||||
/* The texture must have been previously generated with a size to use this generic pixel data load function */
|
||||
|
|
|
@ -8,8 +8,8 @@
|
|||
| ~~~~~~~ BOX |/
|
||||
+-------------*/
|
||||
|
||||
#ifndef extension_h_
|
||||
#define extension_h_
|
||||
#ifndef SB_EXTENSION_H_
|
||||
#define SB_EXTENSION_H_
|
||||
|
||||
#include <vector>
|
||||
#include <iostream>
|
||||
|
@ -220,7 +220,7 @@ namespace std
|
|||
glm::mat<rows, columns, Type, qualifier> transpose = glm::transpose(mat);
|
||||
out << std::endl << "{";
|
||||
/* print each column of the transpose, therefore printing each row of the original */
|
||||
for (std::size_t ii = 0; ii < transpose.length(); ii++)
|
||||
for (std::size_t ii = 0; ii < static_cast<std::size_t>(transpose.length()); ii++)
|
||||
{
|
||||
if (ii > 0)
|
||||
{
|
||||
|
|
|
@ -0,0 +1,7 @@
|
|||
#include "utility.hpp"
|
||||
|
||||
/* Modulus that handles negative arguments */
|
||||
int sb::mod(int a, int b)
|
||||
{
|
||||
return (b + (a % b)) % b;
|
||||
}
|
Loading…
Reference in New Issue