Root/
Source at commit 79dcc8a created 9 years 8 months ago. By Maarten ter Huurne, Implemented stream output for 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 | #ifndef SURFACE_H |
22 | #define SURFACE_H |
23 | |
24 | #include "font.h" |
25 | |
26 | #include <SDL.h> |
27 | |
28 | #include <cstdint> |
29 | #include <ostream> |
30 | #include <string> |
31 | |
32 | struct RGBAColor { |
33 | uint8_t r, g, b, a; |
34 | static RGBAColor fromString(std::string const& strColor); |
35 | RGBAColor() : r(0), g(0), b(0), a(0) {} |
36 | RGBAColor(uint8_t r, uint8_t g, uint8_t b, uint8_t a = 255) |
37 | : r(r), g(g), b(b), a(a) {} |
38 | Uint32 pixelValue(SDL_PixelFormat *fmt) const { |
39 | return SDL_MapRGBA(fmt, r, g, b, a); |
40 | } |
41 | }; |
42 | std::ostream& operator<<(std::ostream& os, RGBAColor const& color); |
43 | |
44 | /** |
45 | Wrapper around SDL_Surface |
46 | @author Massimiliano Torromeo <massimiliano.torromeo@gmail.com> |
47 | */ |
48 | class Surface { |
49 | public: |
50 | static Surface *openOutputSurface(int width, int height, int bitsperpixel); |
51 | static Surface *emptySurface(int width, int height); |
52 | static Surface *loadImage(const std::string &img, |
53 | const std::string &skin="", bool loadAlpha=true); |
54 | |
55 | Surface(Surface *s); |
56 | ~Surface(); |
57 | |
58 | /** Converts the underlying surface to the same pixel format as the frame |
59 | * buffer, for faster blitting. This removes the alpha channel if the |
60 | * image has done. |
61 | */ |
62 | void convertToDisplayFormat(); |
63 | |
64 | int width() const { return raw->w; } |
65 | int height() const { return raw->h; } |
66 | |
67 | void flip(); |
68 | |
69 | void clearClipRect(); |
70 | void setClipRect(int x, int y, int w, int h); |
71 | void setClipRect(SDL_Rect rect); |
72 | |
73 | void blit(Surface *destination, int x, int y, int w=0, int h=0, int a=-1) const; |
74 | void blit(Surface *destination, SDL_Rect container, Font::HAlign halign = Font::HAlignLeft, Font::VAlign valign = Font::VAlignTop) const; |
75 | void blitCenter(Surface *destination, int x, int y, int w=0, int h=0, int a=-1) const; |
76 | void blitRight(Surface *destination, int x, int y, int w=0, int h=0, int a=-1) const; |
77 | |
78 | void write(Font *font, const std::string &text, int x, int y, |
79 | Font::HAlign halign = Font::HAlignLeft, |
80 | Font::VAlign valign = Font::VAlignTop) { |
81 | font->write(this, text, x, y, halign, valign); |
82 | } |
83 | |
84 | void box(SDL_Rect re, RGBAColor c); |
85 | void box(Sint16 x, Sint16 y, Uint16 w, Uint16 h, RGBAColor c) { |
86 | box((SDL_Rect){ x, y, w, h }, c); |
87 | } |
88 | void box(Sint16 x, Sint16 y, Uint16 w, Uint16 h, Uint8 r, Uint8 g, Uint8 b, Uint8 a) { |
89 | box((SDL_Rect){ x, y, w, h }, RGBAColor(r, g, b, a)); |
90 | } |
91 | void rectangle(SDL_Rect re, RGBAColor c); |
92 | void rectangle(Sint16 x, Sint16 y, Uint16 w, Uint16 h, RGBAColor c) { |
93 | rectangle((SDL_Rect){ x, y, w, h }, c); |
94 | } |
95 | void rectangle(Sint16 x, Sint16 y, Uint16 w, Uint16 h, Uint8 r, Uint8 g, Uint8 b, Uint8 a) { |
96 | rectangle((SDL_Rect){ x, y, w, h }, RGBAColor(r, g, b, a)); |
97 | } |
98 | |
99 | private: |
100 | Surface(SDL_Surface *raw, bool freeWhenDone); |
101 | void blit(SDL_Surface *destination, int x, int y, int w=0, int h=0, int a=-1) const; |
102 | void blitCenter(SDL_Surface *destination, int x, int y, int w=0, int h=0, int a=-1) const; |
103 | void blitRight(SDL_Surface *destination, int x, int y, int w=0, int h=0, int a=-1) const; |
104 | |
105 | /** Draws the given rectangle on this surface in the given color, blended |
106 | * according to the alpha value of the color argument. |
107 | */ |
108 | void fillRectAlpha(SDL_Rect rect, RGBAColor c); |
109 | |
110 | /** Clips the given rectangle against this surface's active clipping |
111 | * rectangle. |
112 | */ |
113 | void applyClipRect(SDL_Rect& rect); |
114 | |
115 | SDL_Surface *raw; |
116 | bool freeWhenDone; |
117 | int halfW, halfH; |
118 | |
119 | // For direct access to "raw". |
120 | friend class Font; |
121 | }; |
122 | |
123 | #endif |
124 |
Branches:
install_locations
master
opkrun
packages