Simulant  21.12-574
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  bool destroy() override;
60  bool 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  UniqueIDKey make_key() const override {
88  return make_unique_id_key(id());
89  }
90 
91  SpriteManager* manager_;
92 
93  float frame_width_ = 0;
94  float frame_height_ = 0;
95  float sprite_sheet_margin_ = 0;
96  float sprite_sheet_spacing_ = 0;
97  std::pair<uint32_t, uint32_t> sprite_sheet_padding_;
98  float render_width_ = 1.0;
99  float render_height_ = 1.0;
100 
101  ActorPtr actor_ = nullptr;
102  ActorID actor_id_;
103 
104  MeshID mesh_id_;
105  MaterialID material_id_;
106 
107  float image_width_ = 0;
108  float image_height_ = 0;
109 
110  float alpha_ = 1.0f;
111 
112  void update_texture_coordinates();
113 
114  bool flipped_vertically_ = false;
115  bool flipped_horizontally_ = false;
116 
117  void refresh_animation_state(uint32_t current_frame, uint32_t next_frame, float interp) {
118  _S_UNUSED(current_frame);
119  _S_UNUSED(next_frame);
120  _S_UNUSED(interp);
121 
122  update_texture_coordinates();
123  }
124 
125  std::shared_ptr<KeyFrameAnimationState> animation_state_;
126 
127 public:
128  Property<ActorPtr Sprite::*> actor = {this, &Sprite::actor_};
129  Property<decltype(&Sprite::animation_state_)> animations = {this, &Sprite::animation_state_};
130 };
131 
132 }
133 #endif // SPRITE_H
smlt::Property
Definition: property.h:202
smlt::AudioSource
Definition: sound.h:106
smlt
Definition: animation.cpp:25
smlt::Sprite
Definition: sprite.h:50
smlt::generic::Identifiable
Definition: identifiable.h:26
smlt::ContainerNode
Definition: stage_node.h:304
smlt::UniqueID< MaterialPtr >
smlt::TypedDestroyableObject
Definition: manual_object.h:34
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