Root/
Source at commit 5f454a8 created 9 years 8 months ago. By Maarten ter Huurne, Made strtorgba into a factory method of RGBAColor | |
---|---|
1 | /*************************************************************************** |
2 | * Copyright (C) 2006 by Massimiliano Torromeo * |
3 | * massimiliano.torromeo@gmail.com * |
4 | * * |
5 | * This program is free software; you can redistribute it and/or modify * |
6 | * it under the terms of the GNU General Public License as published by * |
7 | * the Free Software Foundation; either version 2 of the License, or * |
8 | * (at your option) any later version. * |
9 | * * |
10 | * This program is distributed in the hope that it will be useful, * |
11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of * |
12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * |
13 | * GNU General Public License for more details. * |
14 | * * |
15 | * You should have received a copy of the GNU General Public License * |
16 | * along with this program; if not, write to the * |
17 | * Free Software Foundation, Inc., * |
18 | * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. * |
19 | ***************************************************************************/ |
20 | |
21 | #include "surface.h" |
22 | |
23 | #include "debug.h" |
24 | #include "imageio.h" |
25 | #include "surfacecollection.h" |
26 | #include "utilities.h" |
27 | |
28 | #include <SDL_gfxPrimitives.h> |
29 | |
30 | #include <iostream> |
31 | |
32 | using namespace std; |
33 | |
34 | RGBAColor RGBAColor::fromString(const string &strColor) { |
35 | return { |
36 | uint8_t(constrain(strtol(strColor.substr(0, 2).c_str(), nullptr, 16), |
37 | 0, 255)), |
38 | uint8_t(constrain(strtol(strColor.substr(2, 2).c_str(), nullptr, 16), |
39 | 0, 255)), |
40 | uint8_t(constrain(strtol(strColor.substr(4, 2).c_str(), nullptr, 16), |
41 | 0, 255)), |
42 | uint8_t(constrain(strtol(strColor.substr(6, 2).c_str(), nullptr, 16), |
43 | 0, 255)), |
44 | }; |
45 | } |
46 | |
47 | Surface *Surface::openOutputSurface(int width, int height, int bitsperpixel) { |
48 | SDL_ShowCursor(SDL_DISABLE); |
49 | SDL_Surface *raw = SDL_SetVideoMode( |
50 | width, height, bitsperpixel, SDL_HWSURFACE | SDL_DOUBLEBUF); |
51 | return raw ? new Surface(raw, false) : NULL; |
52 | } |
53 | |
54 | Surface *Surface::emptySurface(int width, int height) { |
55 | SDL_Surface *raw = SDL_CreateRGBSurface( |
56 | SDL_SWSURFACE, width, height, 32, 0, 0, 0, 0); |
57 | if (!raw) return NULL; |
58 | SDL_FillRect(raw, NULL, SDL_MapRGB(raw->format, 0, 0, 0)); |
59 | return new Surface(raw, true); |
60 | } |
61 | |
62 | Surface *Surface::loadImage(const string &img, const string &skin, bool loadAlpha) { |
63 | string skinpath; |
64 | if (!skin.empty() && !img.empty() && img[0]!='/') |
65 | skinpath = SurfaceCollection::getSkinFilePath(skin, img); |
66 | else |
67 | skinpath = img; |
68 | |
69 | SDL_Surface *raw = loadPNG(skinpath, loadAlpha); |
70 | if (!raw) { |
71 | ERROR("Couldn't load surface '%s'\n", img.c_str()); |
72 | return NULL; |
73 | } |
74 | |
75 | return new Surface(raw, true); |
76 | } |
77 | |
78 | Surface::Surface(SDL_Surface *raw_, bool freeWhenDone_) |
79 | : raw(raw_) |
80 | , freeWhenDone(freeWhenDone_) |
81 | { |
82 | halfW = raw->w/2; |
83 | halfH = raw->h/2; |
84 | } |
85 | |
86 | Surface::Surface(Surface *s) { |
87 | raw = SDL_ConvertSurface(s->raw, s->raw->format, SDL_SWSURFACE); |
88 | // Note: A bug in SDL_ConvertSurface() leaves the per-surface alpha |
89 | // undefined when converting from RGBA to RGBA. This can cause |
90 | // problems if the surface is later converted to a format without |
91 | // an alpha channel, such as the display format. |
92 | raw->format->alpha = s->raw->format->alpha; |
93 | freeWhenDone = true; |
94 | halfW = raw->w/2; |
95 | halfH = raw->h/2; |
96 | } |
97 | |
98 | Surface::~Surface() { |
99 | if (freeWhenDone) { |
100 | SDL_FreeSurface(raw); |
101 | } |
102 | } |
103 | |
104 | void Surface::convertToDisplayFormat() { |
105 | SDL_Surface *newSurface = SDL_DisplayFormat(raw); |
106 | if (newSurface) { |
107 | if (freeWhenDone) { |
108 | SDL_FreeSurface(raw); |
109 | } |
110 | raw = newSurface; |
111 | freeWhenDone = true; |
112 | } |
113 | } |
114 | |
115 | void Surface::flip() { |
116 | SDL_Flip(raw); |
117 | } |
118 | |
119 | void Surface::blit(SDL_Surface *destination, int x, int y, int w, int h, int a) const { |
120 | if (destination == NULL || a==0) return; |
121 | |
122 | SDL_Rect src = { 0, 0, static_cast<Uint16>(w), static_cast<Uint16>(h) }; |
123 | SDL_Rect dest; |
124 | dest.x = x; |
125 | dest.y = y; |
126 | if (a>0 && a!=raw->format->alpha) |
127 | SDL_SetAlpha(raw, SDL_SRCALPHA|SDL_RLEACCEL, a); |
128 | SDL_BlitSurface(raw, (w==0 || h==0) ? NULL : &src, destination, &dest); |
129 | } |
130 | void Surface::blit(Surface *destination, int x, int y, int w, int h, int a) const { |
131 | blit(destination->raw,x,y,w,h,a); |
132 | } |
133 | |
134 | void Surface::blitCenter(SDL_Surface *destination, int x, int y, int w, int h, int a) const { |
135 | int oh, ow; |
136 | if (w==0) ow = halfW; else ow = min(halfW,w/2); |
137 | if (h==0) oh = halfH; else oh = min(halfH,h/2); |
138 | blit(destination,x-ow,y-oh,w,h,a); |
139 | } |
140 | void Surface::blitCenter(Surface *destination, int x, int y, int w, int h, int a) const { |
141 | blitCenter(destination->raw,x,y,w,h,a); |
142 | } |
143 | |
144 | void Surface::blitRight(SDL_Surface *destination, int x, int y, int w, int h, int a) const { |
145 | if (!w) w = raw->w; |
146 | blit(destination,x-min(raw->w,w),y,w,h,a); |
147 | } |
148 | void Surface::blitRight(Surface *destination, int x, int y, int w, int h, int a) const { |
149 | if (!w) w = raw->w; |
150 | blitRight(destination->raw,x,y,w,h,a); |
151 | } |
152 | |
153 | void Surface::box(Sint16 x, Sint16 y, Uint16 w, Uint16 h, Uint8 r, Uint8 g, Uint8 b, Uint8 a) { |
154 | boxRGBA(raw, x, y, x + w - 1, y + h - 1, r, g, b, a); |
155 | } |
156 | void Surface::box(Sint16 x, Sint16 y, Uint16 w, Uint16 h, Uint8 r, Uint8 g, Uint8 b) { |
157 | SDL_Rect re = { x, y, w, h }; |
158 | SDL_FillRect(raw, &re, SDL_MapRGBA(raw->format, r, g, b, 255)); |
159 | } |
160 | void Surface::box(Sint16 x, Sint16 y, Uint16 w, Uint16 h, RGBAColor c) { |
161 | box(x, y, w, h, c.r, c.g, c.b, c.a); |
162 | } |
163 | void Surface::box(SDL_Rect re, RGBAColor c) { |
164 | boxRGBA(raw, re.x, re.y, re.x + re.w - 1, re.y + re.h - 1, c.r, c.g, c.b, c.a); |
165 | } |
166 | |
167 | void Surface::rectangle(Sint16 x, Sint16 y, Uint16 w, Uint16 h, Uint8 r, Uint8 g, Uint8 b, Uint8 a) { |
168 | rectangleRGBA(raw, x, y, x + w - 1, y + h - 1, r, g, b, a); |
169 | } |
170 | void Surface::rectangle(Sint16 x, Sint16 y, Uint16 w, Uint16 h, RGBAColor c) { |
171 | rectangle(x, y, w, h, c.r, c.g, c.b, c.a); |
172 | } |
173 | void Surface::rectangle(SDL_Rect re, RGBAColor c) { |
174 | rectangle(re.x, re.y, re.w, re.h, c.r, c.g, c.b, c.a); |
175 | } |
176 | |
177 | void Surface::clearClipRect() { |
178 | SDL_SetClipRect(raw,NULL); |
179 | } |
180 | |
181 | void Surface::setClipRect(int x, int y, int w, int h) { |
182 | SDL_Rect rect = { |
183 | static_cast<Sint16>(x), static_cast<Sint16>(y), |
184 | static_cast<Uint16>(w), static_cast<Uint16>(h) |
185 | }; |
186 | setClipRect(rect); |
187 | } |
188 | |
189 | void Surface::setClipRect(SDL_Rect rect) { |
190 | SDL_SetClipRect(raw,&rect); |
191 | } |
192 | |
193 | void Surface::blit(Surface *destination, SDL_Rect container, Font::HAlign halign, Font::VAlign valign) const { |
194 | switch (halign) { |
195 | case Font::HAlignLeft: |
196 | break; |
197 | case Font::HAlignCenter: |
198 | container.x += container.w/2-halfW; |
199 | break; |
200 | case Font::HAlignRight: |
201 | container.x += container.w-raw->w; |
202 | break; |
203 | } |
204 | |
205 | switch (valign) { |
206 | case Font::VAlignTop: |
207 | break; |
208 | case Font::VAlignMiddle: |
209 | container.y += container.h/2-halfH; |
210 | break; |
211 | case Font::VAlignBottom: |
212 | container.y += container.h-raw->h; |
213 | break; |
214 | } |
215 | |
216 | blit(destination,container.x,container.y); |
217 | } |
218 |
Branches:
install_locations
master
opkrun
packages