1 // Copyright Timothy Goya 2007.
2 // Distributed under the Boost Software License, Version 1.0.
3 // (See accompanying file LICENSE_1_0.txt or copy at
4 // http://www.boost.org/LICENSE_1_0.txt)
6 #ifndef UNISON_VIDEO_SURFACE_HPP
7 #define UNISON_VIDEO_SURFACE_HPP
9 #include <unison/video/Blittable.hpp>
10 #include <unison/video/RenderOptions.hpp>
23 /// An image that is optimized for easy manipulation
24 class Surface : public Blittable
27 /// Default constructor
30 /// Create a Surface from an input stream
31 /// \param[in] src The input stream
32 Surface(std::istream &stream);
34 /// Create a Surface from an input stream
35 /// \param[in] src The input stream
36 /// \param[in] colorkey The colorkey used by the file
37 Surface(std::istream &stream, const Color &colorkey);
39 /// Opens image file indicated by filename
40 /// \param[in] filename The filename of the image file
41 Surface(const std::string &filename);
43 /// Opens image file indicated by filename and use the specified color key
44 /// \param[in] filename The filename of the image file
45 /// \param[in] colorkey The colorkey used by the file
46 Surface(const std::string &filename, const Color &colorkey);
48 /// Creates Surface of indicated dimensions
49 /// \param[in] size The size of the desired surface
50 Surface(const Area &size);
53 /// \param[in] rhs The source surface
54 Surface(const Surface &rhs);
59 /// Assignment operator
60 /// \param[in] rhs The source surface
61 Surface &operator =(const Surface &rhs);
63 /// Saves the surface to file
64 /// \param[in] filename The destination filename
65 void save(const std::string &filename) const;
67 /// Retrieves the window's size
68 /// \return The size of the surface
71 return pixels ? pixels->size : Area();
74 /// Retrieves a pixel at the specified coordinates
75 /// \param[in] pos The position of the pixel to retrieve
76 /// \return The pixel at the specified position
77 Color &get_pixel(const Point &pos)
81 return pixels->buffer[pos.y * pixels->size.x + pos.x];
84 /// Retrieves the pixel color at the specified coordinates
85 /// \param[in] x The x position of the pixel to retrieve
86 /// \param[in] y The y position of the pixel to retrieve
87 /// \return The color of the pixel at the specified position
88 Color& get_pixel(int x, int y)
92 return pixels->buffer[y * pixels->size.x + x];
95 /// Retrieves the pixel color at the specified coordinates
96 /// \param[in] pos The position of the pixel to retrieve
97 /// \return The color of the pixel at the specified position
98 Color get_pixel(const Point &pos) const
101 return pixels->buffer[pos.y * pixels->size.x + pos.x];
104 /// Retrieves the pixel color at the specified coordinates
105 /// \param[in] x The x position of the pixel to retrieve
106 /// \param[in] y The y position of the pixel to retrieve
107 /// \return The color of the pixel at the specified position
108 Color get_pixel(int x, int y) const
111 return pixels->buffer[y * pixels->size.x + x];
114 /// Acquire the pixel color buffer
115 /// \return the pixel color buffer
120 return pixels->buffer;
123 /// Acquire the pixel color buffer
124 /// \return the pixel color buffer
125 const Color *get_pixels() const
128 return pixels->buffer;
131 /// Does a surface-to-surface blit
132 /// \param[in] src The source surface
133 /// \param[in] dst_pos The position to blit to
134 /// \param[in] src_rect The part of the source surface to blit from
135 /// \param[in] options Extra blit options
136 void blit(const Surface &src, const Point &dst_pos = Point(), const Rect &src_rect = Rect(), const RenderOptions &options = RenderOptions());
138 /// Does a texture-to-surface blit
139 /// \param[in] src The source texture
140 /// \param[in] dst_pos The position to blit to
141 /// \param[in] src_rect The part of the source texture to blit from
142 /// \param[in] options Extra blit options
143 void blit(const Texture &src, const Point &dst_pos = Point(), const Rect &src_rect = Rect(), const RenderOptions &options = RenderOptions());
145 /// Fills a portion of the image with the given color
146 /// \param[in] color The color
147 /// \param[in] rect The portion to fill
148 void fill(const Color &color, const Rect &rect = Rect());
150 /// Fills and alpha blend a portion of the image with the given color
151 /// \param[in] color The color
152 /// \param[in] rect The portion to fill
153 void fill_blend(const Color &color, const Rect &rect = Rect());
155 /// Scale the surface by a factor of (numerator / denominator)
156 /// \param[in] numerator The numerator of the scale factor
157 /// \param[in] denominator The denominator of the scale factor
158 /// \return The scaled surface
159 Surface scale(unsigned int numerator, unsigned int denominator) const;
161 /// Flip the surface horizontally
162 /// \return The flipped surface
163 Surface h_flip() const;
165 /// Flip the surface vertically
166 /// \return The flipped surface
167 Surface v_flip() const;
169 /// Modulate the image with a color
170 /// \return The modulated surface
171 Surface modulate(const Color &color) const;
173 /// Modulate the image with an alpha
174 /// \return The modulated surface
175 Surface modulate(unsigned char alpha) const;
183 PixelBuffer(Area size)
190 buffer = new Color[size.x * size.y];
206 assert(refcount > 0);
223 /// A section of a surface
230 /// The clip rectangle
233 /// Create a section from an image and a rectangle
234 /// \param[in] image The image
235 /// \param[in] rect The clip rectangle
236 SurfaceSection(const Surface &image = Surface(), const Rect &clip_rect = Rect()) :