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