nomlib
Loading...
Searching...
No Matches
Texture.hpp
1/******************************************************************************
2
3 nomlib - C++11 cross-platform game engine
4
5Copyright (c) 2013, 2014 Jeffrey Carpenter <i8degrees@gmail.com>
6All rights reserved.
7
8Redistribution and use in source and binary forms, with or without
9modification, are permitted provided that the following conditions are met:
10
111. Redistributions of source code must retain the above copyright notice, this
12 list of conditions and the following disclaimer.
132. Redistributions in binary form must reproduce the above copyright notice,
14 this list of conditions and the following disclaimer in the documentation
15 and/or other materials provided with the distribution.
16
17THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
18ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
19WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
20DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
21ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
22(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
23LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
24ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
26SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27
28******************************************************************************/
29#ifndef NOMLIB_SDL2_TEXTURE_HEADERS
30#define NOMLIB_SDL2_TEXTURE_HEADERS
31
32#include <iostream>
33#include <string>
34#include <memory>
35
36#include <SDL.h>
37
38#include "nomlib/config.hpp"
39#include "nomlib/math/Color4.hpp"
40#include "nomlib/math/Point2.hpp"
41#include "nomlib/math/Size2.hpp"
42#include "nomlib/math/Rect.hpp"
43
44// Dump the rescaled Texture as a PNG file
45//#define NOM_DEBUG_SDL2_RESIZE_PNG
46
47namespace nom {
48
49// Forward declarations
50class Image;
51class RenderWindow;
52
54{
55 public:
56 typedef Texture self_type;
57
60 {
61 None = 0, // No resizing is applied
62 scale2x,
63 scale3x, // Reserved for future implementation
64 scale4x, // Reserved for future implementation
65 hq2x,
66 hq3x,
67 hq4x
68 };
69
70 enum Access
71 {
72 Invalid = 0,
73 Static = SDL_TEXTUREACCESS_STATIC, // Changes rarely; not lockable
74 Streaming = SDL_TEXTUREACCESS_STREAMING, // Frequent changes; lockable, RW
75 RenderTarget = SDL_TEXTUREACCESS_TARGET // Render (texture) to target
76 };
77
83 Texture( void );
84
91 ~Texture( void );
92
94 Texture ( const Texture& copy );
95
97 Texture& operator = ( const Texture& other );
98
110 Texture* clone() const;
111
120 bool initialize ( uint32 format, uint32 flags, int width, int height );
121
123 bool initialize(uint32 format, uint32 flags, const Size2i& dims);
124
126 bool create(SDL_Texture* source);
127
129 bool create ( const Image& source );
130
133 bool create( const Image& source, uint32 pixel_format, enum Texture::Access type );
134
135 const Point2i& position() const;
136
144 const Size2i& size() const;
145
146 const IntRect& bounds() const;
147
149 SDL_Texture* texture() const;
150
152 bool valid() const;
153
157 enum Texture::Access access ( void ) const;
158
159 void set_position(const Point2i& pos);
160
165 void set_size(const Size2i& size);
166
168 void set_bounds(const IntRect& bounds);
169
173 int32 width ( void ) const;
174
178 int32 height ( void ) const;
179
180 int pitch ( void ) const;
181
182 void* pixels ( void ) const;
183
184 uint8 bytes_per_pixel ( void ) const;
185
186 uint32 pixel_format ( void ) const;
187
192 uint8 bits_per_pixel ( void ) const;
193
195 const SDL_BlendMode blend_mode ( void ) const;
196
201 const Color4i& colorkey ( void ) const;
202
207 uint8 alpha ( void ) const;
208
213 const Color4i color_modulation ( void ) const;
214
225 static const Point2i maximum_size ( void );
226
228 bool locked() const;
229
234 bool lock();
235
243
244 bool lock(const IntRect& bounds);
245
249 void unlock ( void );
250
266 bool load ( const std::string& filename,
267 bool use_cache = false,
268 enum Texture::Access type = Access::Static
269 );
270
281 bool update_pixels(const void* source, uint16 pitch, const IntRect& bounds);
282
287 void draw(SDL_Renderer* target) const;
288
294 void draw(const RenderWindow& target) const;
295
302 void draw(SDL_Renderer* target, const real64 angle) const;
303
308 void draw(const RenderWindow& target, const real64 angle) const;
309
315 bool set_alpha ( uint8 opacity );
316
330 uint32 pixel ( int x, int y );
331
346 bool resize( enum ResizeAlgorithm scaling_algorithm );
347
349 int scale_factor( enum ResizeAlgorithm scaling_algorithm ) const;
350 int scale_factor() const;
351
353 bool set_blend_mode ( const SDL_BlendMode blend );
354
366 bool set_colorkey(const Color4i& colorkey);
367
378 bool set_color_modulation ( const Color4i& color );
379
380 bool copy_pixels ( const void* source, int pitch );
381
382 private:
383 void set_scale_factor(int factor);
384
385 std::shared_ptr<SDL_Texture> texture_;
386
388 void* pixels_;
389
392
397 Point2i position_; // This should probably be an IntRect. (Global bounds).
398
399 Size2i size_;
400
407 IntRect bounds_; // Local bounds
408
410 Color4i colorkey_;
411
412 int scale_factor_;
413};
414
415
416} // namespace nom
417
418#endif // include guard defined
Bitmap storage container.
Definition Image.hpp:48
int32 height(void) const
const SDL_BlendMode blend_mode(void) const
Obtain the blending mode used for texture copies.
bool create(const Image &source)
Texture(void)
Default constructor.
bool set_colorkey(const Color4i &colorkey)
bool initialize(uint32 format, uint32 flags, const Size2i &dims)
enum Texture::Access access(void) const
Query texture access type.
void set_bounds(const IntRect &bounds)
Set bounding coordinates of the Texture.
bool initialize(uint32 format, uint32 flags, int width, int height)
Color4i colorkey_
Cached upon use of the set_colorkey method for use by external classes.
Definition Texture.hpp:410
void set_size(const Size2i &size)
Set the width & height dimensions of the texture.
bool load(const std::string &filename, bool use_cache=false, enum Texture::Access type=Access::Static)
void unlock(void)
Texture(const Texture &copy)
Copy constructor.
void draw(const RenderWindow &target, const real64 angle) const
bool lock()
Lock the entire bounds of the texture for write access to the pixel buffer.
bool set_blend_mode(const SDL_BlendMode blend)
Set a new blending mode for this texture.
int pitch_
Texture's pixel pitch; these are only available when a Texture is locked.
Definition Texture.hpp:391
Texture * clone() const
Get a shallow-copy of the underlying stored texture.
void draw(SDL_Renderer *target, const real64 angle) const
void draw(const RenderWindow &target) const
Texture & operator=(const Texture &other)
Copy assignment operator.
const Color4i color_modulation(void) const
Obtain the additional color value multiplied into render copy operations.
bool set_color_modulation(const Color4i &color)
Set an additional color value multiplied into render copy operations.
static const Point2i maximum_size(void)
Obtain the largest nom::Texture size allowable by your graphics hardware.
uint8 bits_per_pixel(void) const
Point2i position_
Definition Texture.hpp:397
void * pixels_
Texture's pixels; these are only available when a Texture is locked.
Definition Texture.hpp:388
bool update_pixels(const void *source, uint16 pitch, const IntRect &bounds)
Upload pixels to texture.
bool create(SDL_Texture *source)
SDL_Texture * texture() const
Get the underlying texture stored.
IntRect bounds_
Definition Texture.hpp:407
bool lock(const IntRect &bounds)
Lock a portion of the texture for write access to the pixel buffer.
bool valid() const
Is this object initialized – not nullptr?
uint8 alpha(void) const
Obtain the set color key for this image.
int scale_factor(enum ResizeAlgorithm scaling_algorithm) const
Return the scaling factor of the chosen algorithm.
int32 width(void) const
bool locked() const
Query lock status of texture.
uint32 pixel(int x, int y)
Read a RGBA pixel from the video surface.
bool set_alpha(uint8 opacity)
Set an additional alpha value multiplied into render copy operations.
bool create(const Image &source, uint32 pixel_format, enum Texture::Access type)
const Color4i & colorkey(void) const
bool resize(enum ResizeAlgorithm scaling_algorithm)
Rescale the Texture with the chosen rescaling algorithm.
void draw(SDL_Renderer *target) const
ResizeAlgorithm
Available pixel rescaling algorithms.
Definition Texture.hpp:60
const Size2i & size() const
Get the width & height dimensions of the texture.
~Texture(void)
Destructor.