From: Daniel Karbach Date: Sun, 19 Jan 2014 13:47:22 +0000 (+0100) Subject: added texture support X-Git-Url: http://git.localhorst.tv/?a=commitdiff_plain;h=ba1cd7abc93eaacc3c25c0ad1b923b2ce7b280e2;hp=e4732374394610bdd3deafd2138cbea267a670e0;p=gworm.git added texture support --- diff --git a/src/graphics/Canvas.cpp b/src/graphics/Canvas.cpp index b461ab4..beabd01 100644 --- a/src/graphics/Canvas.cpp +++ b/src/graphics/Canvas.cpp @@ -45,6 +45,16 @@ Vector Canvas::Size() const { } +Texture Canvas::CreateStaticTexture(Vector size) { + return Texture(canv, Color::Format, SDL_TEXTUREACCESS_STATIC, size); +} + + +void Canvas::Copy(Texture &tex, Vector to) { + tex.Copy(canv, to); +} + + void Canvas::SetColor(Color c) { SDL_SetRenderDrawColor(canv, c.r, c.g, c.b, c.a); } diff --git a/src/graphics/Canvas.h b/src/graphics/Canvas.h index b464dd6..16d565d 100644 --- a/src/graphics/Canvas.h +++ b/src/graphics/Canvas.h @@ -2,6 +2,7 @@ #define GWORM_CANVAS_H_ #include "Color.h" +#include "Texture.h" #include "Vector.h" #include @@ -27,6 +28,10 @@ public: void Present(); + Texture CreateStaticTexture(Vector size); + + void Copy(Texture &, Vector to); + void SetColor(Color); void Fill(); diff --git a/src/graphics/Color.h b/src/graphics/Color.h index f976e1a..ade711c 100644 --- a/src/graphics/Color.h +++ b/src/graphics/Color.h @@ -8,6 +8,9 @@ namespace gworm { struct Color { + // NOTE: this depends on endianness and should be defined accordingly + static constexpr Uint32 Format = SDL_PIXELFORMAT_ABGR8888; + constexpr Color() : Color(0, 0, 0) { } constexpr Color(Uint8 r, Uint8 g, Uint8 b, Uint8 a = 0xFF) diff --git a/src/graphics/Rect.h b/src/graphics/Rect.h new file mode 100644 index 0000000..7cef9d1 --- /dev/null +++ b/src/graphics/Rect.h @@ -0,0 +1,50 @@ +#ifndef GWORM_RECT_H_ +#define GWORM_RECT_H_ + +#include "Vector.h" + +#include + + +namespace gworm { + +template +class Rect { + +public: + constexpr Rect() : x(0), y(0), w(0), h(0) { } + constexpr Rect(Vector pos, Vector size) + : x(pos.x), y(pos.y), w(size.x), h(size.y) { } + +public: + constexpr Vector Pos() const { return Vector(x, y); } + constexpr Vector Size() const { return Vector(w, h); } + +public: + Scalar x; + Scalar y; + Scalar w; + Scalar h; + +}; + + +/// specialization with same layout as SDL_Rect +template<> +class Rect +: public SDL_Rect { + +public: + constexpr Rect() : SDL_Rect({0, 0, 0, 0}) { } + constexpr Rect(Vector pos, Vector size) + : SDL_Rect({pos.x, pos.y, size.x, size.y}) { } + +public: + constexpr Vector Pos() const { return Vector(x, y); } + constexpr Vector Size() const { return Vector(w, h); } + +}; + +} + +#endif diff --git a/src/graphics/Texture.cpp b/src/graphics/Texture.cpp new file mode 100644 index 0000000..f39d1ca --- /dev/null +++ b/src/graphics/Texture.cpp @@ -0,0 +1,69 @@ +#include "Texture.h" + + +namespace gworm { + +Texture::Texture() +: tex(nullptr) +, format(Color::Format) +, size(0, 0) { + +} + +Texture::~Texture() { + if (tex) SDL_DestroyTexture(tex); +} + +Texture::Texture(Texture &&other) +: Texture() { + Swap(other); +} + +Texture &Texture::operator =(Texture &&other) { + Texture temp(std::move(other)); + Swap(temp); + return *this; +} + + +Texture::Texture( + SDL_Renderer *c, + Uint32 f, + int u, + Vector s) +: tex(SDL_CreateTexture(c, f, u, s.x, s.y)) +, format(f) +, size(s) { + +} + + +void Texture::Swap(Texture &other) { + std::swap(tex, other.tex); + std::swap(format, other.format); + std::swap(size, other.size); +} + + +void Texture::SetColors(const Color *values) { + if (format == Color::Format) { + SDL_UpdateTexture(tex, nullptr, values, Size().x * sizeof(Color)); + } else { + // TODO: implement for non-Color pixel formats + } +} + + +void Texture::Fill(SDL_Renderer *canv) { + SDL_RenderCopy(canv, tex, nullptr, nullptr); +} + +void Texture::Copy(SDL_Renderer *canv, Vector to) { + Copy(canv, Rect(to, Size())); +} + +void Texture::Copy(SDL_Renderer *canv, Rect to) { + SDL_RenderCopy(canv, tex, nullptr, &to); +} + +} diff --git a/src/graphics/Texture.h b/src/graphics/Texture.h new file mode 100644 index 0000000..a4985bc --- /dev/null +++ b/src/graphics/Texture.h @@ -0,0 +1,63 @@ +#ifndef GWORM_TEXTURE_H_ +#define GWORM_TEXTURE_H_ + +#include "Color.h" +#include "Rect.h" +#include "Vector.h" + +#include +#include + + +namespace gworm { + +class Texture { + +public: + Texture(); + ~Texture(); + + Texture(Texture &&); + Texture &operator =(Texture &&); + + Texture(const Texture &) = delete; + Texture &operator =(const Texture &) = delete; + + Texture(SDL_Renderer *, Uint32 format, int use, Vector size); + + void Swap(Texture &); + +public: + Vector Size() const { return size; } + + /// stretch this texture to completely fill given render target + void Fill(SDL_Renderer *); + /// copy entire texture as is to given coordinates + void Copy(SDL_Renderer *, Vector to); + /// copy entire texture stretched to given rect + void Copy(SDL_Renderer *, Rect to); + + /// set all color values + /// given array must hold at least Size().x * Size().y values + void SetColors(const Color *); + +private: + SDL_Texture *tex; + Uint32 format; + Vector size; + +}; + +} + + +namespace std { + +template<> +inline void swap(gworm::Texture &lhs, gworm::Texture &rhs) { + lhs.Swap(rhs); +} + +} + +#endif