Root/src/surface.h

1/***************************************************************************
2 * Copyright (C) 2006 by Massimiliano Torromeo *
3 * massimiliano.torromeo@gmail.com *
4 * Copyright (C) 2010-2014 by various authors; see Git log *
5 * *
6 * This program is free software; you can redistribute it and/or modify *
7 * it under the terms of the GNU General Public License as published by *
8 * the Free Software Foundation; either version 2 of the License, or *
9 * (at your option) any later version. *
10 * *
11 * This program is distributed in the hope that it will be useful, *
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
14 * GNU General Public License for more details. *
15 * *
16 * You should have received a copy of the GNU General Public License *
17 * along with this program; if not, write to the *
18 * Free Software Foundation, Inc., *
19 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
20 ***************************************************************************/
21
22#ifndef SURFACE_H
23#define SURFACE_H
24
25#include "font.h"
26
27#include <SDL.h>
28
29#include <cstdint>
30#include <memory>
31#include <ostream>
32#include <string>
33
34struct RGBAColor {
35    uint8_t r, g, b, a;
36    static RGBAColor fromString(std::string const& strColor);
37    RGBAColor() : r(0), g(0), b(0), a(0) {}
38    RGBAColor(uint8_t r, uint8_t g, uint8_t b, uint8_t a = 255)
39        : r(r), g(g), b(b), a(a) {}
40    Uint32 pixelValue(SDL_PixelFormat *fmt) const {
41        return SDL_MapRGBA(fmt, r, g, b, a);
42    }
43};
44std::ostream& operator<<(std::ostream& os, RGBAColor const& color);
45
46/**
47 * Abstract base class for surfaces; wraps SDL_Surface.
48 */
49class Surface {
50public:
51    Surface& operator=(Surface const& other) = delete;
52
53    int width() const { return raw->w; }
54    int height() const { return raw->h; }
55
56    void clearClipRect();
57    void setClipRect(int x, int y, int w, int h);
58    void setClipRect(SDL_Rect rect);
59
60    void blit(Surface& destination, int x, int y, int w=0, int h=0, int a=-1) const;
61    void blit(Surface& destination, SDL_Rect container, Font::HAlign halign = Font::HAlignLeft, Font::VAlign valign = Font::VAlignTop) const;
62    void blitCenter(Surface& destination, int x, int y, int w=0, int h=0, int a=-1) const;
63    void blitRight(Surface& destination, int x, int y, int w=0, int h=0, int a=-1) const;
64
65    void box(SDL_Rect re, RGBAColor c);
66    void box(Sint16 x, Sint16 y, Uint16 w, Uint16 h, RGBAColor c) {
67        box((SDL_Rect){ x, y, w, h }, c);
68    }
69    void box(Sint16 x, Sint16 y, Uint16 w, Uint16 h, Uint8 r, Uint8 g, Uint8 b, Uint8 a) {
70        box((SDL_Rect){ x, y, w, h }, RGBAColor(r, g, b, a));
71    }
72    void rectangle(SDL_Rect re, RGBAColor c);
73    void rectangle(Sint16 x, Sint16 y, Uint16 w, Uint16 h, RGBAColor c) {
74        rectangle((SDL_Rect){ x, y, w, h }, c);
75    }
76    void rectangle(Sint16 x, Sint16 y, Uint16 w, Uint16 h, Uint8 r, Uint8 g, Uint8 b, Uint8 a) {
77        rectangle((SDL_Rect){ x, y, w, h }, RGBAColor(r, g, b, a));
78    }
79
80protected:
81    Surface(SDL_Surface *raw) : raw(raw) {}
82    Surface(Surface const& other);
83
84    SDL_Surface *raw;
85
86    // For direct access to "raw".
87    friend class Font;
88
89private:
90    void blit(SDL_Surface *destination, int x, int y, int w=0, int h=0, int a=-1) const;
91    void blitCenter(SDL_Surface *destination, int x, int y, int w=0, int h=0, int a=-1) const;
92    void blitRight(SDL_Surface *destination, int x, int y, int w=0, int h=0, int a=-1) const;
93
94    /** Draws the given rectangle on this surface in the given color, blended
95      * according to the alpha value of the color argument.
96      */
97    void fillRectAlpha(SDL_Rect rect, RGBAColor c);
98
99    /** Clips the given rectangle against this surface's active clipping
100      * rectangle.
101      */
102    void applyClipRect(SDL_Rect& rect);
103};
104
105/**
106 * A surface that is off-screen: not visible.
107 */
108class OffscreenSurface: public Surface {
109public:
110    static std::unique_ptr<OffscreenSurface> emptySurface(
111            int width, int height);
112    static std::unique_ptr<OffscreenSurface> loadImage(
113            std::string const& img, bool loadAlpha = true);
114
115    OffscreenSurface(Surface const& other) : Surface(other) {}
116    OffscreenSurface(OffscreenSurface const& other) : Surface(other) {}
117    OffscreenSurface(OffscreenSurface&& other);
118    ~OffscreenSurface();
119    OffscreenSurface& operator=(OffscreenSurface other);
120    void swap(OffscreenSurface& other);
121
122    /**
123     * Converts the underlying surface to the same pixel format as the frame
124     * buffer, for faster blitting. This removes the alpha channel if the
125     * image has one.
126     */
127    void convertToDisplayFormat();
128
129private:
130    OffscreenSurface(SDL_Surface *raw) : Surface(raw) {}
131};
132
133/**
134 * A surface that is used for writing to a video output device.
135 */
136class OutputSurface: public Surface {
137public:
138    static std::unique_ptr<OutputSurface> open(
139            int width, int height, int bitsPerPixel);
140
141    /**
142     * Offers the current buffer to the video system to be presented and
143     * acquires a new buffer to draw into.
144     */
145    void flip();
146
147private:
148    OutputSurface(SDL_Surface *raw) : Surface(raw) {}
149};
150
151#endif
152

Archive Download this file



interactive