TazGraph Project v0.1.0
Loading...
Searching...
No Matches
SpriteComponent.h
1#pragma once
2
3#include "../../../Components.h"
4#include <SDL2/SDL.h>
5#include "GL/glew.h"
6#include "../../../../TextureManager/TextureManager.h"
7#include "../../../Animators/Animation.h"
8#include "../../../Animators/MovingAnimation.h"
9#include "../../../Animators/FlashAnimation.h"
10#include <map>
11#include "../../../../Vertex.h"
12#include <cstddef>
13
14
15// TODO: (extra): can add states for different states (0 for full solid tile or 1 for no solid
16class SpriteComponent : public Component //sprite -> transform
17{
18private:
19 const GLTexture *gl_texture = nullptr;
20 bool _isMainMenu = false;
21
22public:
23 std::string texture_name = "";
24 Color default_color = { 255, 255, 255, 255 };
25 Color color = { 255, 255, 255, 255 };
26
27 TransformComponent* transform = nullptr;
28 SDL_FRect srcRect = {0,0,0,0};
29
30 Animation animation;
31 MovingAnimation moving_animation;
32 FlashAnimation flash_animation;
33
34 SDL_RendererFlip spriteFlip = SDL_FLIP_NONE;
35
36 SpriteComponent() = default;
37 SpriteComponent(std::string id)
38 {
39 setTex(id);
40 }
42 {
43 default_color = clr;
44 color = clr;
45 }
46
47 SpriteComponent(std::string id, bool isMainMenu)
48 {
49 _isMainMenu = isMainMenu;
50 setTex(id);
51 }
52
54 {
55 }
56
57 void setTex(std::string id) //this function is used to change texture of a sprite
58 {
59 texture_name = id;
60 gl_texture = TextureManager::getInstance().Get_GLTexture(id);
61 srcRect.w = (float)gl_texture->width;
62 srcRect.h = (float)gl_texture->height;
63 }
64
65 void init() override
66 {
67 if (!entity->hasComponent<TransformComponent>())
68 {
70 }
71 transform = &entity->GetComponent<TransformComponent>();
72
73 srcRect.x = srcRect.y = 0;
74 srcRect.w = transform->size.x;
75 srcRect.h = transform->size.y;
76
77 }
78
79 void update(float deltaTime) override
80 {
81 }
82
83 void draw(size_t v_index, PlaneModelRenderer& batch, TazGraphEngine::Window& window)
84 {
85 if (gl_texture == NULL)
86 {
87 return;
88 }
89
90 float screenScale = window.getScale();
91
92 glm::vec3 pos(
93 transform->getPosition().x * screenScale,
94 transform->getPosition().y * screenScale,
95 transform->getPosition().z);
96
97 glm::vec2 size(
98 transform->size.x * transform->scale * screenScale,
99 transform->size.y * transform->scale * screenScale);
100
101 float srcUVposX = spriteFlip == SDL_FLIP_HORIZONTAL ?
102 (srcRect.x + srcRect.w) / gl_texture->width :
103 srcRect.x / gl_texture->width;
104 float srcUVposY = (srcRect.y) / gl_texture->height;
105
106 float srcUVw = spriteFlip == SDL_FLIP_HORIZONTAL ?
107 -srcRect.w / gl_texture->width :
108 srcRect.w / gl_texture->width;
109 float srcUVh = srcRect.h / gl_texture->height;
110
111 glm::vec4 uv(srcUVposX, srcUVposY, srcUVw, srcUVh);
112
113 batch.draw(v_index, size, transform->bodyCenter, transform->rotation, uv, gl_texture->id);
114
115 glDisableVertexAttribArray(0);
116 glDisableVertexAttribArray(1);
117 glDisableVertexAttribArray(2);
118
119 glBindBuffer(GL_ARRAY_BUFFER, 0);
120
121 /*glBindTexture(GL_TEXTURE_2D, 0);*/
122 }
123
124 void SetAnimation(int idX, int idY, size_t fr, float sp, const Animation::animType type, int reps = 0)
125 {
126 animation = Animation(idX, idY, fr, sp, type, reps);
127 }
128
129 void SetMovingAnimation(int idX, int idY, size_t fr, float sp, const Animation::animType type, const std::vector<glm::vec2>& _positions, const std::vector<int>& _zIndices, const std::vector<int>& _rotations, int reps = 0)
130 {
131 moving_animation = MovingAnimation(idX, idY, fr, sp, type, _positions, _zIndices, _rotations, reps); // dx,dy needs to be vector, if yes then dont need int dx dy
132 }
133
134 void SetFlashAnimation(int idX, int idY, size_t fr, float sp, const Animation::animType type, const std::vector<float>& flashTimes, Color flashC, int reps = 0)
135 {
136 flash_animation = FlashAnimation(idX, idY, fr, sp, type, flashTimes, flashC, reps);
137 }
138
139 void setCurrFrame() {
140 this->srcRect.x = (this->animation.indexX * this->transform->size.x) /* init */ + ( this->srcRect.w * animation.cur_frame_index/* curframe from total frams */);
141 this->srcRect.y = this->animation.indexY * this->transform->size.y;
142 }
143
144 void setMoveFrame() {
145
146 this->transform->setPosition_X(((this->transform->getPosition().x) + this->moving_animation.indexX) /* init */ + (this->moving_animation.positions[0].x * moving_animation.cur_frame_index));
147 this->transform->setPosition_Y(((this->transform->getPosition().y) + this->moving_animation.indexY) + (this->moving_animation.positions[0].y * moving_animation.cur_frame_index));
148 }
149
150 void setSpecificMoveFrame() {
151
152 this->transform->setPosition_X(((this->transform->getPosition().x) + this->moving_animation.indexX) /* init */ + (this->moving_animation.positions[moving_animation.cur_frame_index].x));
153 this->transform->setPosition_Y(((this->transform->getPosition().y) + this->moving_animation.indexY) + (this->moving_animation.positions[moving_animation.cur_frame_index].y));
154 }
155
156 void setFlashFrame() {
157 this->color = this->flash_animation.flashColor * this->flash_animation.interpolation_a
158 + default_color * (1 - this->flash_animation.interpolation_a);
159 }
160
161 void DestroyTex()
162 {
163 //TextureManager::DestroyTexture(texture);
164 gl_texture = nullptr;
165 }
166 void DestroyGlTex()
167 {
168 gl_texture = NULL;
169 }
170
171 std::string GetComponentName() override {
172 return "SpriteComponent";
173 }
174
175 void showGUI() override {
176 ImGui::Separator();
177
178 // Get the list of texture names
179 std::vector<std::string> textureNames = TextureManager::getInstance().Get_GLTextureNames();
180
181 static int currentItem = 0;
182 if (!textureNames.empty()) {
183 std::vector<const char*> items;
184 for (const std::string& name : textureNames)
185 items.push_back(name.c_str());
186
187 if (ImGui::Combo("Textures", &currentItem, items.data(), (int)items.size())) {
188 if (!textureNames[currentItem].empty()) {
189 setTex(textureNames[currentItem]);
190 }
191 }
192 }
193 };
194};
Definition GECS.h:123
T & addComponent(TArgs &&... mArgs)
have addScript function
Definition GECS.h:239
Definition MovingAnimation.h:9
Definition PlaneModelRenderer.h:21
Definition SpriteComponent.h:17
Definition Window.h:18
Definition TransformComponent.h:6
Definition Animation.h:6
Definition Vertex.h:47
Definition FlashAnimation.h:10
Definition GLTexture.h:5