libgui 2.01
libgui is a GUI library for the GameCube, Wii, and Wii U
 
Loading...
Searching...
No Matches
GuiImage.cpp
1/****************************************************************************
2 * libgui
3 * Daryl Borth 2009-2026
4 * GuiImage.cpp
5 ***************************************************************************/
6
7#include "Gui.h"
8#include <malloc.h>
9
11{
12 texture = nullptr;
13 ownsTexture = false;
14 width = 0;
15 height = 0;
16 imageangle = 0;
17 tile = -1;
18 stripe = 0;
19 imgType = IMAGE::TEXTURE;
20}
21
23{
24 texture = nullptr;
25 width = 0;
26 height = 0;
27
28 if(img)
29 {
30 texture = img->getTexture();
31 width = img->getWidth();
32 height = img->getHeight();
33 }
34
35 ownsTexture = false;
36 imageangle = 0;
37 tile = -1;
38 stripe = 0;
39 imgType = IMAGE::TEXTURE;
40}
41
42GuiImage::GuiImage(uint8_t * tex, int w, int h)
43{
44 texture = tex;
45 ownsTexture = false;
46 width = w;
47 height = h;
48 imageangle = 0;
49 tile = -1;
50 stripe = 0;
51 imgType = IMAGE::TEXTURE;
52}
53
55{
56 texture = nullptr;
57 ownsTexture = true;
58 width = w;
59 height = h;
60 imageangle = 0;
61 tile = -1;
62 stripe = 0;
63 imgType = IMAGE::COLOR;
64 baseColor = c;
65}
66
68{
69 if(ownsTexture && texture)
70 {
71 platform->getVideo()->getImageRenderer()->destroyTexture(texture);
72 texture = nullptr;
73 }
74}
75
77{
78 if(ownsTexture && texture)
79 {
80 platform->getVideo()->getImageRenderer()->destroyTexture(texture);
81 texture = nullptr;
82 }
83
84 texture = nullptr;
85 ownsTexture = false;
86 width = 0;
87 height = 0;
88 if(img)
89 {
90 texture = img->getTexture();
91 width = img->getWidth();
92 height = img->getHeight();
93 }
94 imgType = IMAGE::TEXTURE;
95}
96
97void GuiImage::setImage(uint8_t * img, int w, int h)
98{
99 if(ownsTexture && texture)
100 {
101 platform->getVideo()->getImageRenderer()->destroyTexture(texture);
102 texture = nullptr;
103 }
104
105 if(img) {
106 texture = platform->getVideo()->getImageRenderer()->createTexture(w, h);
107 platform->getVideo()->getImageRenderer()->loadTextureData(texture, img, w, h);
108 ownsTexture = true;
109 width = w;
110 height = h;
111 }
112 else {
113 texture = nullptr;
114 ownsTexture = false;
115 width = 0;
116 height = 0;
117 }
118
119 imgType = IMAGE::TEXTURE;
120}
121
122void GuiImage::setTexture(uint8_t * tex, int w, int h)
123{
124 if(ownsTexture && texture)
125 {
126 platform->getVideo()->getImageRenderer()->destroyTexture(texture);
127 texture = nullptr;
128 }
129
130 texture = tex;
131 ownsTexture = false;
132 width = w;
133 height = h;
134 imgType = IMAGE::TEXTURE;
135}
136
138{
139 imageangle = a;
140}
141
143{
144 tile = t;
145}
146
148{
149 stripe = s;
150}
151
153{
154 if(!this->isVisible() || tile == 0)
155 return;
156
157 float currScaleX = this->getScaleX();
158 float currScaleY = this->getScaleY();
159 int currLeft = this->getLeft();
160 int thisTop = this->getTop();
161 int alpha = this->getAlpha();
162
163 if(imgType == IMAGE::COLOR)
164 {
166 c.a = alpha;
167 platform->getVideo()->getImageRenderer()->drawRectangle(currLeft, thisTop, width, height, c);
168 }
169 else if(texture)
170 {
171 if(tile > 0)
172 {
173 for(int i=0; i<tile; ++i)
174 platform->getVideo()->getImageRenderer()->drawTexture(texture, currLeft+width*i, thisTop, width, height, imageangle, currScaleX, currScaleY, alpha);
175 }
176 else
177 {
178 platform->getVideo()->getImageRenderer()->drawTexture(texture, currLeft, thisTop, width, height, imageangle, currScaleX, currScaleY, alpha);
179 }
180 }
181
182 if(stripe > 0)
183 {
184 int thisHeight = this->getHeight();
185 int thisWidth = this->getWidth();
186 for(int y=0; y < thisHeight; y+=6)
187 platform->getVideo()->getImageRenderer()->drawRectangle(currLeft, thisTop+y, thisWidth, 3, (PixelColor){0, 0, 0, (uint8_t)stripe});
188 }
189
190 this->updateEffects();
191}
Umbrella header for the platform-agnostic libgui core.
bool isVisible()
Checks whether or not the element is visible.
int getTop()
Gets the current topmost coordinate of the element Considers vertical alignment, y offset,...
float getScaleX()
Gets the element's current x scale Considers scale, scaleDyn, and the parent element's GetScale() val...
int alpha
Element alpha value (0-255)
Definition GuiElement.h:278
int width
Element width.
Definition GuiElement.h:268
int height
Element height.
Definition GuiElement.h:269
float getScaleY()
Gets the element's current y scale Considers scale, scaleDyn, and the parent element's GetScale() val...
void updateEffects()
Updates the element's effects (dynamic values) Called by Draw(), used for animation purposes.
int getWidth()
Gets the current width of the element.
int getLeft()
Gets the current leftmost coordinate of the element Considers horizontal alignment,...
int getHeight()
Gets the height of the element.
int getAlpha()
Gets the element's alpha value Considers alpha, alphaDyn, and the parent element's GetAlpha() value.
Decodes compressed image data (PNG) into a platform-native texture created from it.
void * getTexture()
Gets the attached platform-native texture.
int getWidth()
Gets the image width.
int getHeight()
Gets the image height.
int stripe
Alpha value (0-255) to apply a stripe effect to the texture.
Definition GuiImage.h:70
bool ownsTexture
Whether this object created texture itself (TEXTURE/COLOR) or borrowed it from a GuiImageData.
Definition GuiImage.h:67
void setTile(int t)
Sets the number of times to draw the image horizontally.
Definition GuiImage.cpp:142
void * texture
Attached platform-native texture.
Definition GuiImage.h:66
PixelColor baseColor
Stored color for IMAGE::COLOR types.
Definition GuiImage.h:71
void setAngle(float a)
Sets the image rotation angle for drawing.
Definition GuiImage.cpp:137
IMAGE imgType
Type of image data (TEXTURE, COLOR)
Definition GuiImage.h:65
void setTexture(uint8_t *tex, int w, int h)
This is an overloaded member function, provided for convenience. It differs from the above function o...
Definition GuiImage.cpp:122
~GuiImage()
Destructor.
Definition GuiImage.cpp:67
float imageangle
Angle to draw the image.
Definition GuiImage.h:68
void setImage(GuiImageData *img)
Sets up a new image using the GuiImageData object specified.
Definition GuiImage.cpp:76
GuiImage()
Constructor.
Definition GuiImage.cpp:10
void draw() override
Constantly called to draw the image.
Definition GuiImage.cpp:152
void setStripe(int s)
Sets a stripe effect on the image, overlaying alpha blended rectangles Does not alter the image data.
Definition GuiImage.cpp:147
int tile
Number of times to draw (tile) the image horizontally.
Definition GuiImage.h:69
8-bit-per-channel RGBA color used by the video interfaces.
Definition Mesh.h:16
uint8_t a
Alpha component.
Definition Mesh.h:20