nomlib
Loading...
Searching...
No Matches
Sprite.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_GRAPHICS_SPRITE_HPP
30#define NOMLIB_GRAPHICS_SPRITE_HPP
31
32#include <memory>
33
34#include "export.hpp"
35#include "nomlib/config.hpp"
36#include "nomlib/math/Transformable.hpp"
37#include "nomlib/math/Rect.hpp"
38#include "nomlib/math/Point2.hpp"
39#include "nomlib/system/SDL_helpers.hpp"
40
41namespace nom {
42
43// Forward declarations
44class Texture;
45class RenderWindow;
46typedef const RenderWindow RenderTarget;
47
49class NOM_EXPORT Sprite: public Transformable
50{
51 public:
52 typedef Sprite self_type;
53 typedef Transformable derived_class;
54
55 Sprite();
56
57 virtual ~Sprite();
58
70 bool init_with_color(const Color4i& color, const Size2i& dims);
71
75 ObjectTypeInfo type() const override;
76
78 virtual void set_position(const Point2i& pos) override;
79
81 virtual void set_size(const Size2i& dims) override;
82
84 std::unique_ptr<Sprite> clone() const;
85
86 std::shared_ptr<Texture> texture() const;
87
88 bool valid() const;
89
91 Color4i color() const;
92
94 BlendMode color_blend_mode() const;
95
97 uint8 alpha() const;
98
110
121
129 bool set_texture(std::shared_ptr<Texture>& tex);
130
131 bool set_alpha(uint8 opacity);
132
133 bool set_color(const Color4i& color);
134
135 bool set_color_blend_mode(BlendMode blend);
136
142
143 virtual void draw(RenderTarget& target) const override;
144
149 virtual void draw(RenderTarget& target, real64 angle) const;
150
151 protected:
153 std::shared_ptr<Texture> texture_;
154
155 private:
156 virtual void update() override;
157
158 // TODO: Implement these member variables:
159
160 // nom::SpriteBatch can begin using this immediately. Long term, I would
161 // like to either re-implement some concept of rescaling, or at the very
162 // minimum, let nom::Sprite always hold a reference to a value and do
163 // calculations based off it from other APIs or what not.
164 // Size2f scale_factor_ = 1.0f;
165
166 // Render queues and z-sorting, oh my! Perhaps we could even just upgrade
167 // nom::Transformable to Point3..?
168 // real32 z_depth_;
169};
170
174NOM_EXPORT std::unique_ptr<Sprite>
175make_unique_sprite(Texture& tex);
176
180NOM_EXPORT std::unique_ptr<Sprite>
181make_unique_sprite(Texture* tex);
182
186NOM_EXPORT std::unique_ptr<Sprite>
187make_unique_sprite(std::shared_ptr<Texture>& tex);
188
192NOM_EXPORT std::shared_ptr<Sprite>
193make_shared_sprite(Texture& tex);
194
198NOM_EXPORT std::shared_ptr<Sprite>
199make_shared_sprite(Texture* tex);
200
204NOM_EXPORT std::shared_ptr<Sprite>
205make_shared_sprite(std::shared_ptr<Texture>& tex);
206
207} // namespace nom
208
209#endif // include guard defined
const RenderWindow RenderTarget
Definition IDrawable.hpp:60
Run-time type information (RTTI) container class.
std::shared_ptr< Texture > texture_
The underlying texture for the sprite.
Definition Sprite.hpp:153
void release_texture()
Free the stored texture.
virtual void update() override
std::unique_ptr< Sprite > clone() const
Implements the required IDrawable::clone method.
bool set_texture(Texture &tex)
Construct a sprite from an existing texture source.
Color4i color() const
Get the texture color of the sprite.
ObjectTypeInfo type() const override
Re-implements the IObject::type method.
virtual void set_position(const Point2i &pos) override
Re-implements Transformable::set_position.
virtual void set_size(const Size2i &dims) override
Re-implements Transformable::set_size.
bool set_texture(std::shared_ptr< Texture > &tex)
Construct a sprite from an existing texture source.
virtual void draw(RenderTarget &target, real64 angle) const
bool set_texture(Texture *tex)
Construct a sprite from an existing texture source.
virtual void draw(RenderTarget &target) const override
Render the object onto the primary video buffer – your visible screen.
BlendMode color_blend_mode() const
Get the color blend mode of the sprite.
bool init_with_color(const Color4i &color, const Size2i &dims)
Construct a sprite from a dimension and color.
uint8 alpha() const
Get the alpha value of the sprite.