UFO: Alien Invasion
Loading...
Searching...
No Matches
ui_sprite.cpp
Go to the documentation of this file.
1
4
5/*
6Copyright (C) 2002-2025 UFO: Alien Invasion.
7
8This program is free software; you can redistribute it and/or
9modify it under the terms of the GNU General Public License
10as published by the Free Software Foundation; either version 2
11of the License, or (at your option) any later version.
12
13This program is distributed in the hope that it will be useful,
14but WITHOUT ANY WARRANTY; without even the implied warranty of
15MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
16
17See the GNU General Public License for more details.
18
19You should have received a copy of the GNU General Public License
20along with this program; if not, write to the Free Software
21Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
22
23*/
24
25#include "ui_main.h"
26#include "ui_internal.h"
27#include "ui_parse.h"
28#include "ui_sprite.h"
29#include "ui_render.h"
30
32 {"size", V_POS, offsetof(uiSprite_t, size), MEMBER_SIZEOF(uiSprite_t, size)},
33 {"single", V_BOOL, offsetof(uiSprite_t, single), 0},
34 {"blend", V_BOOL, offsetof(uiSprite_t, blend), 0},
35 {"pack64", V_BOOL, offsetof(uiSprite_t, pack64), 0},
36 {"tiled_17_1_3", V_BOOL, offsetof(uiSprite_t, tiled_17_1_3), 0},
37 {"tiled_25_1_3", V_BOOL, offsetof(uiSprite_t, tiled_25_1_3), 0},
38 {"tiled_popup", V_BOOL, offsetof(uiSprite_t, tiled_popup), 0},
39 {"border", V_INT, offsetof(uiSprite_t, border), MEMBER_SIZEOF(uiSprite_t, border)},
40
42 {"hoveredtexl", V_POS, offsetof(uiSprite_t, pos[SPRITE_STATUS_HOVER]), MEMBER_SIZEOF(uiSprite_t, pos[SPRITE_STATUS_HOVER])},
45
46 {"image", (valueTypes_t)V_REF_OF_STRING, offsetof(uiSprite_t, image[SPRITE_STATUS_NORMAL]), 0},
47 {"hoveredimage", (valueTypes_t)V_REF_OF_STRING, offsetof(uiSprite_t, image[SPRITE_STATUS_HOVER]), 0},
48 {"disabledimage", (valueTypes_t)V_REF_OF_STRING, offsetof(uiSprite_t, image[SPRITE_STATUS_DISABLED]), 0},
49 {"clickedimage", (valueTypes_t)V_REF_OF_STRING, offsetof(uiSprite_t, image[SPRITE_STATUS_CLICKED]), 0},
50
52 {"hoveredcolor", V_COLOR, offsetof(uiSprite_t, color[SPRITE_STATUS_HOVER]), MEMBER_SIZEOF(uiSprite_t, color[SPRITE_STATUS_HOVER])},
53 {"disabledcolor", V_COLOR, offsetof(uiSprite_t, color[SPRITE_STATUS_DISABLED]), MEMBER_SIZEOF(uiSprite_t, color[SPRITE_STATUS_DISABLED])},
54 {"clickedcolor", V_COLOR, offsetof(uiSprite_t, color[SPRITE_STATUS_CLICKED]), MEMBER_SIZEOF(uiSprite_t, color[SPRITE_STATUS_CLICKED])},
55
56 {nullptr, V_NULL, 0, 0}
57};
58
66{
67 uiSprite_t* sprite = nullptr;
68 const char* suffix[SPRITE_STATUS_MAX] = {"", "_hovered", "_disabled", "_clicked"};
69 char basePicNameBuf[MAX_QPATH];
70 const image_t* pic;
71 int i;
72
73 Q_strncpyz(basePicNameBuf, name, sizeof(basePicNameBuf));
74
75 pic = UI_LoadImage(basePicNameBuf);
76 if (pic == nullptr)
77 return nullptr;
78
79 sprite = UI_AllocStaticSprite(basePicNameBuf);
80 sprite->image[SPRITE_STATUS_NORMAL] = UI_AllocStaticString(basePicNameBuf, 0);
81 sprite->size[0] = pic->width;
82 sprite->size[1] = pic->height;
83 for (i = 1; i < SPRITE_STATUS_MAX; i++) {
84 char picNameBuf[MAX_QPATH];
85 Com_sprintf(picNameBuf, sizeof(picNameBuf), "%s%s", basePicNameBuf, suffix[i]);
86 pic = UI_LoadImage(picNameBuf);
87 if (pic != nullptr)
88 sprite->image[i] = UI_AllocStaticString(picNameBuf, 0);
89 }
90 return sprite;
91}
92
99static bool UI_SpriteExists (const char* name)
100{
101 for (int i = 0; i < ui_global.numSprites; i++) {
102 if (strncmp(name, ui_global.sprites[i].name, MEMBER_SIZEOF(uiSprite_t, name)) != 0)
103 continue;
104 return true;
105 }
106 return false;
107}
108
116{
117 for (int i = 0; i < ui_global.numSprites; i++) {
118 if (Q_streq(name, ui_global.sprites[i].name))
119 return &ui_global.sprites[i];
120 }
122}
123
131{
132 uiSprite_t* result;
133
135 Com_Error(ERR_FATAL, "UI_AllocStaticSprite: \"%s\" sprite already allocated. Check your scripts.", name);
136
137 if (ui_global.numSprites >= UI_MAX_SPRITES)
138 Com_Error(ERR_FATAL, "UI_AllocStaticSprite: UI_MAX_SPRITES hit");
139
140 result = &ui_global.sprites[ui_global.numSprites];
141 ui_global.numSprites++;
142
143 OBJZERO(*result);
144 Q_strncpyz(result->name, name, sizeof(result->name));
145 return result;
146}
147
153static const int tile_template_17_1_3[] = {
154 17, 1, 17,
155 17, 1, 17,
156 3
157};
158
164static const int tile_template_25_1_3[] = {
165 25, 1, 25,
166 25, 1, 25,
167 3
168};
169
174static const int tile_template_popup[] = {
175 20, 1, 19,
176 46, 1, 19,
177 3
178};
179
187void UI_DrawSpriteInBox (bool flip, const uiSprite_t* sprite, uiSpriteStatus_t status, int posX, int posY, int sizeX, int sizeY)
188{
189 float texX;
190 float texY;
191 const char* image;
192
194 if (status >= SPRITE_STATUS_MAX)
195 return;
196
198 if (sprite->single || sprite->blend) {
199 texX = sprite->pos[SPRITE_STATUS_NORMAL][0];
200 texY = sprite->pos[SPRITE_STATUS_NORMAL][1];
201 image = sprite->image[SPRITE_STATUS_NORMAL];
202 } else if (sprite->pack64) {
203 texX = sprite->pos[SPRITE_STATUS_NORMAL][0];
204 texY = sprite->pos[SPRITE_STATUS_NORMAL][1] + (64 * status);
205 image = sprite->image[SPRITE_STATUS_NORMAL];
206 } else {
207 texX = sprite->pos[status][0];
208 texY = sprite->pos[status][1];
209 image = sprite->image[status];
210 if (!image) {
211 if (texX == 0 && texY == 0) {
212 texX = sprite->pos[SPRITE_STATUS_NORMAL][0];
213 texY = sprite->pos[SPRITE_STATUS_NORMAL][1];
214 image = sprite->image[SPRITE_STATUS_NORMAL];
215 } else {
216 image = sprite->image[SPRITE_STATUS_NORMAL];
217 }
218 }
219 }
220 if (!image)
221 return;
222
223 if (sprite->blend) {
224 const vec_t* color = sprite->color[status];
225 R_Color(color);
226 }
227
228 if (sprite->tiled_17_1_3) {
229 const vec2_t pos = Vector2FromInt(posX, posY);
230 const vec2_t size = Vector2FromInt(sizeX, sizeY);
231 UI_DrawPanel(pos, size, image, texX, texY, tile_template_17_1_3);
232 } else if (sprite->tiled_25_1_3) {
233 const vec2_t pos = Vector2FromInt(posX, posY);
234 const vec2_t size = Vector2FromInt(sizeX, sizeY);
235 UI_DrawPanel(pos, size, image, texX, texY, tile_template_25_1_3);
236 } else if (sprite->tiled_popup) {
237 const vec2_t pos = Vector2FromInt(posX, posY);
238 const vec2_t size = Vector2FromInt(sizeX, sizeY);
239 UI_DrawPanel(pos, size, image, texX, texY, tile_template_popup);
240 } else if (sprite->border != 0) {
241 const vec2_t pos = Vector2FromInt(posX, posY);
242 const vec2_t size = Vector2FromInt(sizeX, sizeY);
243 UI_DrawBorderedPanel(pos, size, image, texX, texY, sprite->size[0], sprite->size[1], sprite->border);
244 } else {
245 posX += (sizeX - sprite->size[0]) / 2;
246 posY += (sizeY - sprite->size[1]) / 2;
247 UI_DrawNormImageByName(flip, posX, posY, sprite->size[0], sprite->size[1],
248 texX + sprite->size[0], texY + sprite->size[1], texX, texY, image);
249 }
250
251 if (sprite->blend)
252 R_Color(nullptr);
253}
void R_Color(const vec4_t rgba)
Change the color to given value.
Definition r_state.cpp:1011
void Com_Error(int code, const char *fmt,...)
Definition common.cpp:459
#define ERR_FATAL
Definition common.h:210
#define MAX_QPATH
Definition filesys.h:40
voidpf void uLong size
Definition ioapi.h:42
QGL_EXTERN GLint i
Definition r_gl.h:113
QGL_EXTERN GLuint GLsizei GLsizei GLint GLenum GLchar * name
Definition r_gl.h:110
valueTypes_t
possible values for parsing functions
Definition scripts.h:48
@ V_BOOL
Definition scripts.h:50
@ V_NULL
Definition scripts.h:49
@ V_INT
Definition scripts.h:52
@ V_COLOR
Definition scripts.h:57
@ V_POS
Definition scripts.h:55
#define MEMBER_SIZEOF(TYPE, MEMBER)
Definition scripts.h:34
#define Q_streq(a, b)
Definition shared.h:136
#define OBJZERO(obj)
Definition shared.h:178
void Q_strncpyz(char *dest, const char *src, size_t destsize)
Safe strncpy that ensures a trailing zero.
Definition shared.cpp:457
bool Com_sprintf(char *dest, size_t size, const char *fmt,...)
copies formatted string with buffer-size checking
Definition shared.cpp:494
int height
Definition r_image.h:64
int width
Definition r_image.h:64
bool tiled_popup
Definition ui_sprite.h:49
bool blend
Definition ui_sprite.h:45
char name[MAX_VAR]
Definition ui_sprite.h:42
bool pack64
Definition ui_sprite.h:46
bool tiled_17_1_3
Definition ui_sprite.h:47
char * image[SPRITE_STATUS_MAX]
Definition ui_sprite.h:53
vec2_t pos[SPRITE_STATUS_MAX]
Definition ui_sprite.h:54
vec4_t color[SPRITE_STATUS_MAX]
Definition ui_sprite.h:52
vec2_t size
Definition ui_sprite.h:43
bool single
Definition ui_sprite.h:44
bool tiled_25_1_3
Definition ui_sprite.h:48
float vec_t
Definition ufotypes.h:37
vec_t vec2_t[2]
Definition ufotypes.h:38
Internal data use by the UI package.
uiGlobal_t ui_global
Definition ui_main.cpp:38
char * UI_AllocStaticString(const char *string, int size)
Allocate a string into the UI static memory.
Definition ui_parse.cpp:204
#define V_REF_OF_STRING
Definition ui_parse.h:71
void UI_DrawPanel(const vec2_t pos, const vec2_t size, const char *texture, int texX, int texY, const int panelDef[7])
draw a panel from a texture as we can see on the image
const struct image_s * UI_LoadImage(const char *name)
Searches for an image in the image array.
Definition ui_render.cpp:91
void UI_DrawBorderedPanel(const vec2_t pos, const vec2_t size, const char *texture, int texX, int texY, int texW, int texH, int border)
draw a panel from a texture as we can see on the image
const image_t * UI_DrawNormImageByName(bool flip, float x, float y, float w, float h, float sh, float th, float sl, float tl, const char *name)
Draws an image or parts of it.
const value_t ui_spriteProperties[]
Definition ui_sprite.cpp:31
static const int tile_template_17_1_3[]
static uiSprite_t * UI_AutoGenerateSprite(const char *name)
Search a file name inside pics/ according to the sprite name If it exists, generate a "single" sprite...
Definition ui_sprite.cpp:65
uiSprite_t * UI_AllocStaticSprite(const char *name)
Allocate an sprite to the UI static memory.
static const int tile_template_popup[]
static const int tile_template_25_1_3[]
void UI_DrawSpriteInBox(bool flip, const uiSprite_t *sprite, uiSpriteStatus_t status, int posX, int posY, int sizeX, int sizeY)
static bool UI_SpriteExists(const char *name)
Check if an sprite name exists.
Definition ui_sprite.cpp:99
uiSprite_t * UI_GetSpriteByName(const char *name)
Return an sprite by is name.
uiSpriteStatus_t
Definition ui_sprite.h:32
@ SPRITE_STATUS_MAX
Definition ui_sprite.h:38
@ SPRITE_STATUS_DISABLED
Definition ui_sprite.h:35
@ SPRITE_STATUS_HOVER
Definition ui_sprite.h:34
@ SPRITE_STATUS_CLICKED
Definition ui_sprite.h:36
@ SPRITE_STATUS_NORMAL
Definition ui_sprite.h:33
#define UI_MAX_SPRITES
Definition ui_sprite.h:27
#define Vector2FromInt(x, y)
Definition vector.h:40