From 816bc61d7e14ffcb3846eadd41bd86de9174e36f Mon Sep 17 00:00:00 2001 From: Daniel Karbach Date: Sun, 21 Oct 2012 16:06:12 +0200 Subject: [PATCH] added texture class --- Debug/src/graphics/subdir.mk | 9 +++-- Release/src/graphics/subdir.mk | 9 +++-- src/graphics/Texture.cpp | 60 ++++++++++++++++++++++++++++++++++ src/graphics/Texture.h | 40 +++++++++++++++++++++++ src/graphics/fwd.h | 1 + 5 files changed, 113 insertions(+), 6 deletions(-) create mode 100644 src/graphics/Texture.cpp create mode 100644 src/graphics/Texture.h diff --git a/Debug/src/graphics/subdir.mk b/Debug/src/graphics/subdir.mk index 23f43a8..2c41697 100644 --- a/Debug/src/graphics/subdir.mk +++ b/Debug/src/graphics/subdir.mk @@ -13,7 +13,8 @@ CPP_SRCS += \ ../src/graphics/Gauge.cpp \ ../src/graphics/Menu.cpp \ ../src/graphics/SimpleAnimation.cpp \ -../src/graphics/Sprite.cpp +../src/graphics/Sprite.cpp \ +../src/graphics/Texture.cpp OBJS += \ ./src/graphics/Animation.o \ @@ -25,7 +26,8 @@ OBJS += \ ./src/graphics/Gauge.o \ ./src/graphics/Menu.o \ ./src/graphics/SimpleAnimation.o \ -./src/graphics/Sprite.o +./src/graphics/Sprite.o \ +./src/graphics/Texture.o CPP_DEPS += \ ./src/graphics/Animation.d \ @@ -37,7 +39,8 @@ CPP_DEPS += \ ./src/graphics/Gauge.d \ ./src/graphics/Menu.d \ ./src/graphics/SimpleAnimation.d \ -./src/graphics/Sprite.d +./src/graphics/Sprite.d \ +./src/graphics/Texture.d # Each subdirectory must supply rules for building sources it contributes diff --git a/Release/src/graphics/subdir.mk b/Release/src/graphics/subdir.mk index c020692..347deae 100644 --- a/Release/src/graphics/subdir.mk +++ b/Release/src/graphics/subdir.mk @@ -13,7 +13,8 @@ CPP_SRCS += \ ../src/graphics/Gauge.cpp \ ../src/graphics/Menu.cpp \ ../src/graphics/SimpleAnimation.cpp \ -../src/graphics/Sprite.cpp +../src/graphics/Sprite.cpp \ +../src/graphics/Texture.cpp OBJS += \ ./src/graphics/Animation.o \ @@ -25,7 +26,8 @@ OBJS += \ ./src/graphics/Gauge.o \ ./src/graphics/Menu.o \ ./src/graphics/SimpleAnimation.o \ -./src/graphics/Sprite.o +./src/graphics/Sprite.o \ +./src/graphics/Texture.o CPP_DEPS += \ ./src/graphics/Animation.d \ @@ -37,7 +39,8 @@ CPP_DEPS += \ ./src/graphics/Gauge.d \ ./src/graphics/Menu.d \ ./src/graphics/SimpleAnimation.d \ -./src/graphics/Sprite.d +./src/graphics/Sprite.d \ +./src/graphics/Texture.d # Each subdirectory must supply rules for building sources it contributes diff --git a/src/graphics/Texture.cpp b/src/graphics/Texture.cpp new file mode 100644 index 0000000..6fc8f0e --- /dev/null +++ b/src/graphics/Texture.cpp @@ -0,0 +1,60 @@ +/* + * Texture.cpp + * + * Created on: Oct 21, 2012 + * Author: holy + */ + +#include "Texture.h" + +#include "../sdl/utility.h" + +using geometry::Vector; + +namespace graphics { + +Texture::Texture() +: surface(0) { + +} + +Texture::~Texture() { + +} + + +void Texture::Render(SDL_Surface *dest, const Vector &from, const Vector &to) const { + SDL_Rect destRect; + destRect.x = from.X(); + destRect.y = from.Y(); + if (!surface || size == Vector()) { + destRect.w = to.X() - from.X(); + destRect.h = to.Y() - from.Y(); + SDL_FillRect(dest, &destRect, SDL_MapRGB(dest->format, 0xFF, 0x00, 0x00)); + return; + } + + SDL_Rect srcRect; + srcRect.x = offset.X(); + srcRect.y = offset.Y(); + + for (destRect.y = from.Y(); destRect.y < to.Y(); destRect.y += size.Y()) { + srcRect.h = size.Y(); + destRect.h = size.Y(); + if (destRect.y + destRect.h > to.Y()) { + srcRect.h = to.Y() - destRect.y; + destRect.h = to.Y() - destRect.y; + } + for (destRect.x = from.X(); destRect.x < to.X(); destRect.x += size.X()) { + srcRect.w = size.X(); + destRect.w = size.X(); + if (destRect.x + destRect.w > to.X()) { + srcRect.w = to.X() - destRect.x; + destRect.w = to.X() - destRect.x; + } + SDL_BlitSurface(surface, &srcRect, dest, &destRect); + } + } +} + +} diff --git a/src/graphics/Texture.h b/src/graphics/Texture.h new file mode 100644 index 0000000..3b6357e --- /dev/null +++ b/src/graphics/Texture.h @@ -0,0 +1,40 @@ +/* + * Texture.h + * + * Created on: Oct 21, 2012 + * Author: holy + */ + +#ifndef GRAPHICS_TEXTURE_H_ +#define GRAPHICS_TEXTURE_H_ + +#include "../geometry/Vector.h" + +#include + +namespace graphics { + +class Texture { + +public: + Texture(); + ~Texture(); + +public: + void Render(SDL_Surface *dest, const geometry::Vector &from, const geometry::Vector &to) const; + +public: + void SetSurface(SDL_Surface *s) { surface = s; } + void SetOffset(const geometry::Vector &o) { offset = o; } + void SetSize(const geometry::Vector &s) { size = s; } + +private: + SDL_Surface *surface; + geometry::Vector offset; + geometry::Vector size; + +}; + +} + +#endif /* GRAPHICS_TEXTURE_H_ */ diff --git a/src/graphics/fwd.h b/src/graphics/fwd.h index 31dbf27..961890b 100644 --- a/src/graphics/fwd.h +++ b/src/graphics/fwd.h @@ -24,6 +24,7 @@ class Menu; struct MenuProperties; class SimpleAnimation; class Sprite; +class Texture; } -- 2.39.2