Simulant  21.12-194
A portable game engine for Windows, OSX, Linux, Dreamcast, and PSP
sprite.h
1 /* * Copyright (c) 2011-2017 Luke Benstead https://simulant-engine.appspot.com
2  *
3  * This file is part of Simulant.
4  *
5  * Simulant is free software: you can redistribute it and/or modify
6  * it under the terms of the GNU Lesser General Public License as published by
7  * the Free Software Foundation, either version 3 of the License, or
8  * (at your option) any later version.
9  *
10  * Simulant is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public License
16  * along with Simulant. If not, see <http://www.gnu.org/licenses/>.
17  */
18 
19 #ifndef SPRITE_H
20 #define SPRITE_H
21 
22 #include "stage_node.h"
23 
24 #include "../types.h"
25 #include "../interfaces.h"
26 #include "../generic/managed.h"
27 #include "../generic/identifiable.h"
28 #include "../sound.h"
29 #include "../animation.h"
30 #include "../macros.h"
31 
32 namespace smlt {
33 
34 class KeyFrameAnimationState;
35 class SpriteManager;
36 
38  uint32_t margin = 0;
39  uint32_t spacing = 0;
40  uint32_t padding_vertical = 0;
41  uint32_t padding_horizontal = 0;
42 };
43 
44 class Sprite :
45  public TypedDestroyableObject<Sprite, SpriteManager>,
46  public ContainerNode,
47  public generic::Identifiable<SpriteID>,
48  public KeyFrameAnimated,
49  public AudioSource,
50  public ChainNameable<Sprite> {
51 
52 public:
53  using ContainerNode::_get_renderables;
54 
55  bool init() override;
56  void clean_up() override;
57  void update(float dt) override;
58 
59  void destroy() override;
60  void destroy_immediately() override;
61 
62  Sprite(SpriteManager *manager, SoundDriver *sound_driver);
63 
64  void set_render_dimensions(float width, float height);
65  void set_render_dimensions_from_width(float width);
66  void set_render_dimensions_from_height(float height);
67 
68  void set_render_priority(smlt::RenderPriority priority);
69 
70  void set_alpha(float alpha);
71 
72  float alpha() const { return alpha_; }
73  MaterialID material_id() const { return material_id_; }
74 
75  void set_spritesheet(
76  TextureID texture_id,
77  uint32_t frame_width,
78  uint32_t frame_height,
80  );
81 
82  void flip_vertically(bool value=true);
83  void flip_horizontally(bool value=true);
84 
85  const AABB& aabb() const override;
86 private:
87  SpriteManager* manager_;
88 
89  float frame_width_ = 0;
90  float frame_height_ = 0;
91  float sprite_sheet_margin_ = 0;
92  float sprite_sheet_spacing_ = 0;
93  std::pair<uint32_t, uint32_t> sprite_sheet_padding_;
94  float render_width_ = 1.0;
95  float render_height_ = 1.0;
96 
97  ActorPtr actor_ = nullptr;
98  ActorID actor_id_;
99 
100  MeshID mesh_id_;
101  MaterialID material_id_;
102 
103  float image_width_ = 0;
104  float image_height_ = 0;
105 
106  float alpha_ = 1.0f;
107 
108  void update_texture_coordinates();
109 
110  bool flipped_vertically_ = false;
111  bool flipped_horizontally_ = false;
112 
113  void refresh_animation_state(uint32_t current_frame, uint32_t next_frame, float interp) {
114  _S_UNUSED(current_frame);
115  _S_UNUSED(next_frame);
116  _S_UNUSED(interp);
117 
118  update_texture_coordinates();
119  }
120 
121  std::shared_ptr<KeyFrameAnimationState> animation_state_;
122 
123 public:
124  Property<ActorPtr Sprite::*> actor = {this, &Sprite::actor_};
125  Property<decltype(&Sprite::animation_state_)> animations = {this, &Sprite::animation_state_};
126 };
127 
128 }
129 #endif // SPRITE_H
smlt::Property
Definition: property.h:202
smlt::AudioSource
Definition: sound.h:103
smlt
Definition: animation.cpp:25
smlt::Sprite
Definition: sprite.h:50
smlt::generic::Identifiable
Definition: identifiable.h:26
smlt::ContainerNode
Definition: stage_node.h:285
smlt::UniqueID< MaterialPtr >
smlt::TypedDestroyableObject
Definition: manual_object.h:33
smlt::SpritesheetAttrs
Definition: sprite.h:37
smlt::SpriteManager
Definition: sprite_manager.h:16
smlt::ChainNameable
Definition: nameable.h:34
smlt::AABB
Definition: aabb.h:22
smlt::KeyFrameAnimated
Definition: animation.h:40
smlt::default_init_ptr< Actor >
smlt::SoundDriver
Definition: sound_driver.h:74