Root/
| 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 "utilities.h" |
| 26 | |
| 27 | #include <algorithm> |
| 28 | #include <cassert> |
| 29 | #include <iomanip> |
| 30 | #include <utility> |
| 31 | |
| 32 | using namespace std; |
| 33 | |
| 34 | |
| 35 | // RGBAColor: |
| 36 | |
| 37 | RGBAColor RGBAColor::fromString(const string &strColor) { |
| 38 | return { |
| 39 | uint8_t(constrain(strtol(strColor.substr(0, 2).c_str(), nullptr, 16), |
| 40 | 0, 255)), |
| 41 | uint8_t(constrain(strtol(strColor.substr(2, 2).c_str(), nullptr, 16), |
| 42 | 0, 255)), |
| 43 | uint8_t(constrain(strtol(strColor.substr(4, 2).c_str(), nullptr, 16), |
| 44 | 0, 255)), |
| 45 | uint8_t(constrain(strtol(strColor.substr(6, 2).c_str(), nullptr, 16), |
| 46 | 0, 255)), |
| 47 | }; |
| 48 | } |
| 49 | |
| 50 | ostream& operator<<(ostream& os, RGBAColor const& color) { |
| 51 | auto oldfill = os.fill('0'); |
| 52 | auto oldflags = os.setf(ios::hex | ios::right, |
| 53 | ios::basefield | ios::adjustfield); |
| 54 | os << setw(2) << uint32_t(color.r) |
| 55 | << setw(2) << uint32_t(color.g) |
| 56 | << setw(2) << uint32_t(color.b) |
| 57 | << setw(2) << uint32_t(color.a); |
| 58 | os.fill(oldfill); |
| 59 | os.setf(oldflags); |
| 60 | return os; |
| 61 | } |
| 62 | |
| 63 | |
| 64 | // Surface: |
| 65 | |
| 66 | Surface::Surface(Surface const& other) |
| 67 | : Surface(SDL_ConvertSurface(other.raw, other.raw->format, SDL_SWSURFACE)) |
| 68 | { |
| 69 | // Note: A bug in SDL_ConvertSurface() leaves the per-surface alpha |
| 70 | // undefined when converting from RGBA to RGBA. This can cause |
| 71 | // problems if the surface is later converted to a format without |
| 72 | // an alpha channel, such as the display format. |
| 73 | raw->format->alpha = other.raw->format->alpha; |
| 74 | } |
| 75 | |
| 76 | void Surface::blit(SDL_Surface *destination, int x, int y, int w, int h, int a) const { |
| 77 | if (destination == NULL || a==0) return; |
| 78 | |
| 79 | SDL_Rect src = { 0, 0, static_cast<Uint16>(w), static_cast<Uint16>(h) }; |
| 80 | SDL_Rect dest; |
| 81 | dest.x = x; |
| 82 | dest.y = y; |
| 83 | if (a>0 && a!=raw->format->alpha) |
| 84 | SDL_SetAlpha(raw, SDL_SRCALPHA|SDL_RLEACCEL, a); |
| 85 | SDL_BlitSurface(raw, (w==0 || h==0) ? NULL : &src, destination, &dest); |
| 86 | } |
| 87 | void Surface::blit(Surface& destination, int x, int y, int w, int h, int a) const { |
| 88 | blit(destination.raw, x, y, w, h, a); |
| 89 | } |
| 90 | |
| 91 | void Surface::blitCenter(SDL_Surface *destination, int x, int y, int w, int h, int a) const { |
| 92 | int ow = raw->w / 2; if (w != 0) ow = min(ow, w / 2); |
| 93 | int oh = raw->h / 2; if (h != 0) oh = min(oh, h / 2); |
| 94 | blit(destination, x - ow, y - oh, w, h, a); |
| 95 | } |
| 96 | void Surface::blitCenter(Surface& destination, int x, int y, int w, int h, int a) const { |
| 97 | blitCenter(destination.raw, x, y, w, h, a); |
| 98 | } |
| 99 | |
| 100 | void Surface::blitRight(SDL_Surface *destination, int x, int y, int w, int h, int a) const { |
| 101 | if (!w) w = raw->w; |
| 102 | blit(destination, x - min(raw->w, w), y, w, h, a); |
| 103 | } |
| 104 | void Surface::blitRight(Surface& destination, int x, int y, int w, int h, int a) const { |
| 105 | if (!w) w = raw->w; |
| 106 | blitRight(destination.raw, x, y, w, h, a); |
| 107 | } |
| 108 | |
| 109 | void Surface::box(SDL_Rect re, RGBAColor c) { |
| 110 | if (c.a == 255) { |
| 111 | SDL_FillRect(raw, &re, c.pixelValue(raw->format)); |
| 112 | } else if (c.a != 0) { |
| 113 | fillRectAlpha(re, c); |
| 114 | } |
| 115 | } |
| 116 | |
| 117 | void Surface::rectangle(SDL_Rect re, RGBAColor c) { |
| 118 | if (re.h >= 1) { |
| 119 | // Top. |
| 120 | box(SDL_Rect { re.x, re.y, re.w, 1 }, c); |
| 121 | } |
| 122 | if (re.h >= 2) { |
| 123 | Sint16 ey = re.y + re.h - 1; |
| 124 | // Bottom. |
| 125 | box(SDL_Rect { re.x, ey, re.w, 1 }, c); |
| 126 | |
| 127 | Sint16 ex = re.x + re.w - 1; |
| 128 | Sint16 sy = re.y + 1; |
| 129 | Uint16 sh = re.h - 2; |
| 130 | // Left. |
| 131 | if (re.w >= 1) { |
| 132 | box(SDL_Rect { re.x, sy, 1, sh }, c); |
| 133 | } |
| 134 | // Right. |
| 135 | if (re.w >= 2) { |
| 136 | box(SDL_Rect { ex, sy, 1, sh }, c); |
| 137 | } |
| 138 | } |
| 139 | } |
| 140 | |
| 141 | void Surface::clearClipRect() { |
| 142 | SDL_SetClipRect(raw,NULL); |
| 143 | } |
| 144 | |
| 145 | void Surface::setClipRect(int x, int y, int w, int h) { |
| 146 | SDL_Rect rect = { |
| 147 | static_cast<Sint16>(x), static_cast<Sint16>(y), |
| 148 | static_cast<Uint16>(w), static_cast<Uint16>(h) |
| 149 | }; |
| 150 | setClipRect(rect); |
| 151 | } |
| 152 | |
| 153 | void Surface::setClipRect(SDL_Rect rect) { |
| 154 | SDL_SetClipRect(raw,&rect); |
| 155 | } |
| 156 | |
| 157 | void Surface::applyClipRect(SDL_Rect& rect) { |
| 158 | SDL_Rect clip; |
| 159 | SDL_GetClipRect(raw, &clip); |
| 160 | |
| 161 | // Clip along X-axis. |
| 162 | if (rect.x < clip.x) { |
| 163 | rect.w = max(rect.x + rect.w - clip.x, 0); |
| 164 | rect.x = clip.x; |
| 165 | } |
| 166 | if (rect.x + rect.w > clip.x + clip.w) { |
| 167 | rect.w = max(clip.x + clip.w - rect.x, 0); |
| 168 | } |
| 169 | |
| 170 | // Clip along Y-axis. |
| 171 | if (rect.y < clip.y) { |
| 172 | rect.h = max(rect.y + rect.h - clip.y, 0); |
| 173 | rect.y = clip.y; |
| 174 | } |
| 175 | if (rect.y + rect.h > clip.y + clip.h) { |
| 176 | rect.h = max(clip.y + clip.h - rect.y, 0); |
| 177 | } |
| 178 | } |
| 179 | |
| 180 | void Surface::blit(Surface& destination, SDL_Rect container, Font::HAlign halign, Font::VAlign valign) const { |
| 181 | switch (halign) { |
| 182 | case Font::HAlignLeft: |
| 183 | break; |
| 184 | case Font::HAlignCenter: |
| 185 | container.x += container.w / 2 - raw->w / 2; |
| 186 | break; |
| 187 | case Font::HAlignRight: |
| 188 | container.x += container.w-raw->w; |
| 189 | break; |
| 190 | } |
| 191 | |
| 192 | switch (valign) { |
| 193 | case Font::VAlignTop: |
| 194 | break; |
| 195 | case Font::VAlignMiddle: |
| 196 | container.y += container.h / 2 - raw->h / 2; |
| 197 | break; |
| 198 | case Font::VAlignBottom: |
| 199 | container.y += container.h-raw->h; |
| 200 | break; |
| 201 | } |
| 202 | |
| 203 | blit(destination, container.x, container.y); |
| 204 | } |
| 205 | |
| 206 | static inline uint32_t mult8x4(uint32_t c, uint8_t a) { |
| 207 | return ((((c >> 8) & 0x00FF00FF) * a) & 0xFF00FF00) |
| 208 | | ((((c & 0x00FF00FF) * a) & 0xFF00FF00) >> 8); |
| 209 | } |
| 210 | |
| 211 | void Surface::fillRectAlpha(SDL_Rect rect, RGBAColor c) { |
| 212 | applyClipRect(rect); |
| 213 | if (rect.w == 0 || rect.h == 0) { |
| 214 | // Entire rectangle is outside clipping area. |
| 215 | return; |
| 216 | } |
| 217 | |
| 218 | if (SDL_MUSTLOCK(raw)) { |
| 219 | if (SDL_LockSurface(raw) < 0) { |
| 220 | return; |
| 221 | } |
| 222 | } |
| 223 | |
| 224 | SDL_PixelFormat *format = raw->format; |
| 225 | uint32_t color = c.pixelValue(format); |
| 226 | uint8_t alpha = c.a; |
| 227 | |
| 228 | uint8_t* edge = static_cast<uint8_t*>(raw->pixels) |
| 229 | + rect.y * raw->pitch |
| 230 | + rect.x * format->BytesPerPixel; |
| 231 | |
| 232 | // Blending: surf' = surf * (1 - alpha) + fill * alpha |
| 233 | |
| 234 | if (format->BytesPerPixel == 2) { |
| 235 | uint32_t Rmask = format->Rmask; |
| 236 | uint32_t Gmask = format->Gmask; |
| 237 | uint32_t Bmask = format->Bmask; |
| 238 | |
| 239 | // Pre-multiply the fill color. We're hardcoding alpha to 1: 15/16bpp |
| 240 | // modes are unlikely to have an alpha channel and even if they do, |
| 241 | // the written alpha isn't used by gmenu2x. |
| 242 | uint16_t f = (((color & Rmask) * alpha >> 8) & Rmask) |
| 243 | | (((color & Gmask) * alpha >> 8) & Gmask) |
| 244 | | (((color & Bmask) * alpha >> 8) & Bmask) |
| 245 | | format->Amask; |
| 246 | alpha = 255 - alpha; |
| 247 | |
| 248 | for (auto y = 0; y < rect.h; y++) { |
| 249 | for (auto x = 0; x < rect.w; x++) { |
| 250 | uint16_t& pixel = reinterpret_cast<uint16_t*>(edge)[x]; |
| 251 | uint32_t R = ((pixel & Rmask) * alpha >> 8) & Rmask; |
| 252 | uint32_t G = ((pixel & Gmask) * alpha >> 8) & Gmask; |
| 253 | uint32_t B = ((pixel & Bmask) * alpha >> 8) & Bmask; |
| 254 | pixel = uint16_t(R | G | B) + f; |
| 255 | } |
| 256 | edge += raw->pitch; |
| 257 | } |
| 258 | } else if (format->BytesPerPixel == 4) { |
| 259 | // Assume the pixel format uses 8 bits per component; we don't care |
| 260 | // which component is where since they all blend the same. |
| 261 | uint32_t f = mult8x4(color, alpha); // pre-multiply the fill color |
| 262 | alpha = 255 - alpha; |
| 263 | |
| 264 | for (auto y = 0; y < rect.h; y++) { |
| 265 | for (auto x = 0; x < rect.w; x++) { |
| 266 | uint32_t& pixel = reinterpret_cast<uint32_t*>(edge)[x]; |
| 267 | pixel = mult8x4(pixel, alpha) + f; |
| 268 | } |
| 269 | edge += raw->pitch; |
| 270 | } |
| 271 | } else { |
| 272 | assert(false); |
| 273 | } |
| 274 | |
| 275 | if (SDL_MUSTLOCK(raw)) { |
| 276 | SDL_UnlockSurface(raw); |
| 277 | } |
| 278 | } |
| 279 | |
| 280 | |
| 281 | // OffscreenSurface: |
| 282 | |
| 283 | unique_ptr<OffscreenSurface> OffscreenSurface::emptySurface( |
| 284 | int width, int height) |
| 285 | { |
| 286 | SDL_Surface *raw = SDL_CreateRGBSurface( |
| 287 | SDL_SWSURFACE, width, height, 32, 0, 0, 0, 0); |
| 288 | if (!raw) return unique_ptr<OffscreenSurface>(); |
| 289 | SDL_FillRect(raw, nullptr, SDL_MapRGB(raw->format, 0, 0, 0)); |
| 290 | return unique_ptr<OffscreenSurface>(new OffscreenSurface(raw)); |
| 291 | } |
| 292 | |
| 293 | unique_ptr<OffscreenSurface> OffscreenSurface::loadImage( |
| 294 | string const& img, bool loadAlpha) |
| 295 | { |
| 296 | SDL_Surface *raw = loadPNG(img, loadAlpha); |
| 297 | if (!raw) { |
| 298 | DEBUG("Couldn't load surface '%s'\n", img.c_str()); |
| 299 | return unique_ptr<OffscreenSurface>(); |
| 300 | } |
| 301 | |
| 302 | return unique_ptr<OffscreenSurface>(new OffscreenSurface(raw)); |
| 303 | } |
| 304 | |
| 305 | OffscreenSurface::OffscreenSurface(OffscreenSurface&& other) |
| 306 | : Surface(other.raw) |
| 307 | { |
| 308 | other.raw = nullptr; |
| 309 | } |
| 310 | |
| 311 | OffscreenSurface::~OffscreenSurface() |
| 312 | { |
| 313 | SDL_FreeSurface(raw); |
| 314 | } |
| 315 | |
| 316 | OffscreenSurface& OffscreenSurface::operator=(OffscreenSurface other) |
| 317 | { |
| 318 | swap(other); |
| 319 | return *this; |
| 320 | } |
| 321 | |
| 322 | void OffscreenSurface::swap(OffscreenSurface& other) |
| 323 | { |
| 324 | std::swap(raw, other.raw); |
| 325 | } |
| 326 | |
| 327 | void OffscreenSurface::convertToDisplayFormat() { |
| 328 | SDL_Surface *newSurface = SDL_DisplayFormat(raw); |
| 329 | if (newSurface) { |
| 330 | SDL_FreeSurface(raw); |
| 331 | raw = newSurface; |
| 332 | } |
| 333 | } |
| 334 | |
| 335 | |
| 336 | // OutputSurface: |
| 337 | |
| 338 | unique_ptr<OutputSurface> OutputSurface::open( |
| 339 | int width, int height, int bitsPerPixel) |
| 340 | { |
| 341 | SDL_ShowCursor(SDL_DISABLE); |
| 342 | SDL_Surface *raw = SDL_SetVideoMode( |
| 343 | width, height, bitsPerPixel, SDL_HWSURFACE | SDL_DOUBLEBUF); |
| 344 | return unique_ptr<OutputSurface>(raw ? new OutputSurface(raw) : nullptr); |
| 345 | } |
| 346 | |
| 347 | void OutputSurface::flip() { |
| 348 | SDL_Flip(raw); |
| 349 | } |
| 350 |
Branches:
install_locations
master
opkrun
packages
