diff --git a/demo/Demo.cpp b/demo/Demo.cpp index 1284ec5..12f14d4 100644 --- a/demo/Demo.cpp +++ b/demo/Demo.cpp @@ -12,7 +12,8 @@ changed, gradients, level select code input, logging, variable screen resolution, debug display, loading wheel animation, shadowed sprite, separate update and draw, sprite movement cage, multiple windows, multiple renderers, - node children list, node animations list + node children list, node animations list, copy constructor for node, private + and public class members :) SWEATY HANDS :) OILY SNACKS :) AND BAD HYGIENE :) diff --git a/src/Color.hpp b/src/Color.hpp index 7051a46..161ced6 100644 --- a/src/Color.hpp +++ b/src/Color.hpp @@ -14,7 +14,7 @@ struct Color std::uint8_t r, g, b, a; Color(); - Color(std::uint8_t, std::uint8_t, std::uint8_t, std::uint8_t); + Color(std::uint8_t, std::uint8_t, std::uint8_t, std::uint8_t = 255); Color(const SDL_Color&); void set_rgb_float(const float&, const float&, const float&); void set_hsv(const float&, const float& = 1.0f, const float& = 1.0f); diff --git a/src/Configuration.cpp b/src/Configuration.cpp index 9bbfe36..bdc79fa 100644 --- a/src/Configuration.cpp +++ b/src/Configuration.cpp @@ -42,7 +42,9 @@ void Configuration::set_defaults() {"dimensions", {640, 480}}, {"framerate", 60}, {"title", "sfw"}, - {"debug", false} + {"debug", false}, + {"show-cursor", false}, + {"render-test-spacing", 2} }; sys_config["recording"] = { {"enabled", false}, diff --git a/src/Game.cpp b/src/Game.cpp index 1d934ed..a5f5505 100644 --- a/src/Game.cpp +++ b/src/Game.cpp @@ -44,7 +44,7 @@ void FramerateIndicator::refresh() } } -Game::Game() : Node() +Game::Game() { frame_length_history.reserve(5000); set_framerate(get_configuration()["display"]["framerate"]); @@ -75,16 +75,37 @@ Game::Game() : Node() SDL_GL_SetAttribute(SDL_GL_BLUE_SIZE, 8); SDL_GL_SetAttribute(SDL_GL_ALPHA_SIZE, 8); print_gl_attributes(); - std::vector window_size = get_configuration()["display"]["dimensions"]; + auto window_size = get_configuration()["display"]["dimensions"].get(); window = SDL_CreateWindow( get_configuration()["display"]["title"].get_ref().c_str(), SDL_WINDOWPOS_CENTERED, - SDL_WINDOWPOS_CENTERED, window_size[0], window_size[1], SDL_WINDOW_OPENGL); + SDL_WINDOWPOS_CENTERED, window_size.x, window_size.y, SDL_WINDOW_OPENGL); if (window == NULL) { print_sdl_error("Could not create window"); flag_to_end(); } - SDL_ShowCursor(0); + if ((renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_TARGETTEXTURE)) == NULL) + { + print_sdl_error("Could not create renderer"); + flag_to_end(); + } + else + { + SDL_SetRenderTarget(renderer, NULL); + SDL_SetRenderDrawColor(renderer, 0, 0, 0, 255); + SDL_RenderClear(renderer); + int step = get_configuration()["display"]["render-test-spacing"]; + for (int x = 0, r = 1, g = 0, b = 2; x < window_size.x; x += step) + { + SDL_SetRenderDrawColor(renderer, (r++ % 3 == 0) * 255, (g++ % 3 == 0) * 255, (b++ % 3 == 0) * 255, 255); + const SDL_Rect rect = {x, 0, step, window_size.y}; + SDL_RenderFillRect(renderer, &rect); + } + SDL_RenderPresent(renderer); + SDL_RenderFlush(renderer); + SDL_DestroyRenderer(renderer); + } + SDL_ShowCursor(get_configuration()["display"]["show-cursor"]); if (TTF_Init() < 0) { print_sdl_error("Could not initialize SDL ttf"); diff --git a/src/Game.hpp b/src/Game.hpp index c35c81d..bd65e09 100644 --- a/src/Game.hpp +++ b/src/Game.hpp @@ -13,23 +13,19 @@ #define GL_GLEXT_PROTOTYPES #define GLEW_STATIC - #if defined(__EMSCRIPTEN__) - #include #include #include - #else - #include "glew/glew.h" - #endif #include "Node.hpp" #include "Input.hpp" #include "Recorder.hpp" #include "Sprite.hpp" +#include "extension.hpp" struct FramerateIndicator : Sprite { diff --git a/src/Node.cpp b/src/Node.cpp index 3b681c0..6f5c92b 100644 --- a/src/Node.cpp +++ b/src/Node.cpp @@ -3,7 +3,7 @@ Node::Node() : Node(NULL) {} -Node::Node(Node *parent) : parent(parent) +Node::Node(Node* parent) : parent(parent) { std::cout << "Constructing "; print_branch(); @@ -61,12 +61,12 @@ const Input& Node::get_input() const const Game* Node::get_root() const { - const Node* root = this; - while (root->parent != NULL) + const Node* r = this; + while (r->parent != NULL) { - root = parent; + r = r->parent; } - return dynamic_cast(root); + return dynamic_cast(r); } Box Node::get_window_box() diff --git a/src/extension.hpp b/src/extension.hpp index 35ee3e7..4195dc7 100644 --- a/src/extension.hpp +++ b/src/extension.hpp @@ -18,6 +18,7 @@ #include "glm/trigonometric.hpp" #include "glm/vec2.hpp" #include "glm/gtx/vector_angle.hpp" +#include "json/json.hpp" #include "Box.hpp" #include "Segment.hpp" @@ -158,6 +159,22 @@ bool operator<(const SDL_Color& color_1, const SDL_Color& color_2); bool operator==(const SDL_Color& color_1, const SDL_Color& color_2); std::ostream& operator<<(std::ostream&, const SDL_Color&); +namespace glm +{ + template + void to_json(nlohmann::json& j, const vec<2, T, defaultp>& v) + { + j = nlohmann::json{{"x", v.x}, {"y", v.y}}; + } + + template + void from_json(const nlohmann::json& j, vec<2, T, defaultp>& v) + { + j.at(0).get_to(v.x); + j.at(1).get_to(v.y); + } +} + #include "Node.hpp" #endif