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 "nomlib/config.hpp"
35#include "nomlib/math/Transformable.hpp"
36#include "nomlib/math/Rect.hpp"
37#include "nomlib/math/Point2.hpp"
38#include "nomlib/system/SDL_helpers.hpp"
39
40namespace nom {
41
42// Forward declarations
43class Texture;
44class RenderWindow;
45typedef const RenderWindow RenderTarget;
46
48class Sprite: public Transformable
49{
50 public:
51 typedef Sprite self_type;
52 typedef Transformable derived_class;
53
54 Sprite();
55
56 virtual ~Sprite();
57
69 bool init_with_color(const Color4i& color, const Size2i& dims);
70
74 ObjectTypeInfo type() const override;
75
77 virtual void set_position(const Point2i& pos) override;
78
80 virtual void set_size(const Size2i& dims) override;
81
83 std::unique_ptr<Sprite> clone() const;
84
85 std::shared_ptr<Texture> texture() const;
86
87 bool valid() const;
88
90 Color4i color() const;
91
93 BlendMode color_blend_mode() const;
94
96 uint8 alpha() const;
97
109
120
128 bool set_texture(std::shared_ptr<Texture>& tex);
129
130 bool set_alpha(uint8 opacity);
131
132 bool set_color(const Color4i& color);
133
134 bool set_color_blend_mode(BlendMode blend);
135
141
142 virtual void draw(RenderTarget& target) const override;
143
148 virtual void draw(RenderTarget& target, real64 angle) const;
149
150 protected:
152 std::shared_ptr<Texture> texture_;
153
154 private:
155 virtual void update() override;
156
157 // TODO: Implement these member variables:
158
159 // nom::SpriteBatch can begin using this immediately. Long term, I would
160 // like to either re-implement some concept of rescaling, or at the very
161 // minimum, let nom::Sprite always hold a reference to a value and do
162 // calculations based off it from other APIs or what not.
163 // Size2f scale_factor_ = 1.0f;
164
165 // Render queues and z-sorting, oh my! Perhaps we could even just upgrade
166 // nom::Transformable to Point3..?
167 // real32 z_depth_;
168};
169
173std::unique_ptr<Sprite>
174make_unique_sprite(Texture& tex);
175
179std::unique_ptr<Sprite>
180make_unique_sprite(Texture* tex);
181
185std::unique_ptr<Sprite>
186make_unique_sprite(std::shared_ptr<Texture>& tex);
187
191std::shared_ptr<Sprite>
192make_shared_sprite(Texture& tex);
193
197std::shared_ptr<Sprite>
198make_shared_sprite(Texture* tex);
199
203std::shared_ptr<Sprite>
204make_shared_sprite(std::shared_ptr<Texture>& tex);
205
206} // namespace nom
207
208#endif // include guard defined
const RenderWindow RenderTarget
Definition IDrawable.hpp:59
Run-time type information (RTTI) container class.
std::shared_ptr< Texture > texture_
The underlying texture for the sprite.
Definition Sprite.hpp:152
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.