]> git.localhorst.tv Git - l2e.git/blob - src/graphics/Texture.cpp
added right aligning font drawing functions
[l2e.git] / src / graphics / Texture.cpp
1 /*
2  * Texture.cpp
3  *
4  *  Created on: Oct 21, 2012
5  *      Author: holy
6  */
7
8 #include "Texture.h"
9
10 #include "../sdl/utility.h"
11
12 using geometry::Vector;
13
14 namespace graphics {
15
16 Texture::Texture(
17                 SDL_Surface *surface,
18                 const Vector<int> &size,
19                 const Vector<int> &offset)
20 : surface(surface)
21 , size(size)
22 , offset(offset) {
23
24 }
25
26 Texture::~Texture() {
27
28 }
29
30
31 void Texture::Render(SDL_Surface *dest, const Vector<int> &from, const Vector<int> &to) const {
32         SDL_Rect destRect;
33         destRect.x = from.X();
34         destRect.y = from.Y();
35         if (!surface || size == Vector<int>()) {
36                 destRect.w = to.X() - from.X();
37                 destRect.h = to.Y() - from.Y();
38                 SDL_FillRect(dest, &destRect, SDL_MapRGB(dest->format, 0xFF, 0x00, 0x00));
39                 return;
40         }
41
42         SDL_Rect srcRect;
43         srcRect.x = offset.X();
44         srcRect.y = offset.Y();
45
46         for (destRect.y = from.Y(); destRect.y < to.Y(); destRect.y += size.Y()) {
47                 srcRect.h = size.Y();
48                 destRect.h = size.Y();
49                 if (destRect.y + destRect.h > to.Y()) {
50                         srcRect.h = to.Y() - destRect.y;
51                         destRect.h = to.Y() - destRect.y;
52                 }
53                 for (destRect.x = from.X(); destRect.x < to.X(); destRect.x += size.X()) {
54                         srcRect.w = size.X();
55                         destRect.w = size.X();
56                         if (destRect.x + destRect.w > to.X()) {
57                                 srcRect.w = to.X() - destRect.x;
58                                 destRect.w = to.X() - destRect.x;
59                         }
60                         SDL_BlitSurface(surface, &srcRect, dest, &destRect);
61                 }
62         }
63 }
64
65 }