From 66991ce9b81f4af3c095d38fa2187837d94e5469 Mon Sep 17 00:00:00 2001 From: Clyne Sullivan Date: Mon, 5 Aug 2024 10:33:14 -0400 Subject: refactor and split into headers --- include/components/player.hpp | 9 +++++++++ include/components/point.hpp | 9 +++++++++ include/components/texture.hpp | 33 +++++++++++++++++++++++++++++++++ include/components/velocity.hpp | 10 ++++++++++ include/vec2.hpp | 15 +++++++++++++++ include/window.hpp | 16 ++++++++++++++++ 6 files changed, 92 insertions(+) create mode 100644 include/components/player.hpp create mode 100644 include/components/point.hpp create mode 100644 include/components/texture.hpp create mode 100644 include/components/velocity.hpp create mode 100644 include/vec2.hpp create mode 100644 include/window.hpp (limited to 'include') diff --git a/include/components/player.hpp b/include/components/player.hpp new file mode 100644 index 0000000..0940195 --- /dev/null +++ b/include/components/player.hpp @@ -0,0 +1,9 @@ +#ifndef COMPONENTS_PLAYER_HPP +#define COMPONENTS_PLAYER_HPP + +struct Player { + char unused = 0; +}; + +#endif // COMPONENTS_PLAYER_HPP + diff --git a/include/components/point.hpp b/include/components/point.hpp new file mode 100644 index 0000000..b0d0f35 --- /dev/null +++ b/include/components/point.hpp @@ -0,0 +1,9 @@ +#ifndef COMPONENTS_POINT_HPP +#define COMPONENTS_POINT_HPP + +#include "vec2.hpp" + +struct Point : public Vec2 {}; + +#endif // COMPONENTS_POINT_HPP + diff --git a/include/components/texture.hpp b/include/components/texture.hpp new file mode 100644 index 0000000..f4d414e --- /dev/null +++ b/include/components/texture.hpp @@ -0,0 +1,33 @@ +#ifndef COMPONENTS_TEXTURE_HPP +#define COMPONENTS_TEXTURE_HPP + +#include "window.hpp" + +class Texture +{ +public: + Texture(const char *path) { + tex = sdl2LoadTexture(path); + } + + ~Texture() { + if (tex != nullptr) + SDL_DestroyTexture(tex); + } + + void operator()(SDL_Renderer *rend, Point p) const noexcept { + const int x = static_cast(p.x); + const int y = static_cast(p.y); + SDL_Rect rect {x, y, 0, 0}; + + /* TODO err check */ + SDL_QueryTexture(tex, nullptr, nullptr, &rect.w, &rect.h); + SDL_RenderCopy(rend, tex, nullptr, &rect); + } + +private: + SDL_Texture *tex; +}; + +#endif // COMPONENTS_TEXTURE_HPP + diff --git a/include/components/velocity.hpp b/include/components/velocity.hpp new file mode 100644 index 0000000..2337ebd --- /dev/null +++ b/include/components/velocity.hpp @@ -0,0 +1,10 @@ +#ifndef COMPONENTS_VELOCITY_HPP +#define COMPONENTS_VELOCITY_HPP + +#include "vec2.hpp" + +struct Velocity : public Vec2 {}; + +#endif // COMPONENTS_VELOCITY_HPP + + diff --git a/include/vec2.hpp b/include/vec2.hpp new file mode 100644 index 0000000..dcd4b93 --- /dev/null +++ b/include/vec2.hpp @@ -0,0 +1,15 @@ +#ifndef VEC2_HPP +#define VEC2_HPP + +struct Vec2 { + float x, y; + + auto& operator+=(const Vec2& o) noexcept { + x += o.x; + y += o.y; + return *this; + } +}; + +#endif // VEC2_HPP + diff --git a/include/window.hpp b/include/window.hpp new file mode 100644 index 0000000..d245b33 --- /dev/null +++ b/include/window.hpp @@ -0,0 +1,16 @@ +#ifndef WINDOW_HPP +#define WINDOW_HPP + +#include + +constexpr auto WINDOW_WIDTH = 640; +constexpr auto WINDOW_HEIGHT = 480; + +extern SDL_Window *window; +extern SDL_Renderer *renderer; + +int sdl2Initialize(); +SDL_Texture *sdl2LoadTexture(const char *path); + +#endif // WINDOW_HPP + -- cgit v1.2.3