UFO: Alien Invasion
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
ui_sprite.cpp
Go to the documentation of this file.
1 
5 /*
6 Copyright (C) 2002-2020 UFO: Alien Invasion.
7 
8 This program is free software; you can redistribute it and/or
9 modify it under the terms of the GNU General Public License
10 as published by the Free Software Foundation; either version 2
11 of the License, or (at your option) any later version.
12 
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
16 
17 See the GNU General Public License for more details.
18 
19 You should have received a copy of the GNU General Public License
20 along with this program; if not, write to the Free Software
21 Foundation, 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 
41  {"texl", V_POS, offsetof(uiSprite_t, pos[SPRITE_STATUS_NORMAL]), MEMBER_SIZEOF(uiSprite_t, pos[SPRITE_STATUS_NORMAL])},
42  {"hoveredtexl", V_POS, offsetof(uiSprite_t, pos[SPRITE_STATUS_HOVER]), MEMBER_SIZEOF(uiSprite_t, pos[SPRITE_STATUS_HOVER])},
43  {"disabledtexl", V_POS, offsetof(uiSprite_t, pos[SPRITE_STATUS_DISABLED]), MEMBER_SIZEOF(uiSprite_t, pos[SPRITE_STATUS_DISABLED])},
44  {"clickedtexl", V_POS, offsetof(uiSprite_t, pos[SPRITE_STATUS_CLICKED]), MEMBER_SIZEOF(uiSprite_t, pos[SPRITE_STATUS_CLICKED])},
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 
51  {"color", V_COLOR, offsetof(uiSprite_t, color[SPRITE_STATUS_NORMAL]), MEMBER_SIZEOF(uiSprite_t, color[SPRITE_STATUS_NORMAL])},
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 
65 static uiSprite_t* UI_AutoGenerateSprite (const char* name)
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 
99 static 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  }
121  return UI_AutoGenerateSprite(name);
122 }
123 
131 {
132  uiSprite_t* result;
133 
134  if (UI_SpriteExists(name))
135  Com_Error(ERR_FATAL, "UI_AllocStaticSprite: \"%s\" sprite already allocated. Check your scripts.", name);
136 
138  Com_Error(ERR_FATAL, "UI_AllocStaticSprite: UI_MAX_SPRITES hit");
139 
142 
143  OBJZERO(*result);
144  Q_strncpyz(result->name, name, sizeof(result->name));
145  return result;
146 }
147 
153 static const int tile_template_17_1_3[] = {
154  17, 1, 17,
155  17, 1, 17,
156  3
157 };
158 
164 static const int tile_template_25_1_3[] = {
165  25, 1, 25,
166  25, 1, 25,
167  3
168 };
169 
174 static const int tile_template_popup[] = {
175  20, 1, 19,
176  46, 1, 19,
177  3
178 };
179 
187 void 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 }
vec2_t size
Definition: ui_sprite.h:43
int border
Definition: ui_sprite.h:50
valueTypes_t
possible values for parsing functions
Definition: scripts.h:48
uiGlobal_t ui_global
Definition: ui_main.cpp:38
bool Com_sprintf(char *dest, size_t size, const char *fmt,...)
copies formatted string with buffer-size checking
Definition: shared.cpp:494
float vec_t
Definition: ufotypes.h:37
void UI_DrawSpriteInBox(bool flip, const uiSprite_t *sprite, uiSpriteStatus_t status, int posX, int posY, int sizeX, int sizeY)
Definition: ui_sprite.cpp:187
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
Definition: ui_render.cpp:230
int width
Definition: r_image.h:64
bool tiled_17_1_3
Definition: ui_sprite.h:47
#define V_REF_OF_STRING
Definition: ui_parse.h:71
#define ERR_FATAL
Definition: common.h:210
void R_Color(const vec4_t rgba)
Change the color to given value.
Definition: r_state.cpp:1011
Internal data use by the UI package.
void Com_Error(int code, const char *fmt,...)
Definition: common.cpp:417
uiSprite_t * UI_AllocStaticSprite(const char *name)
Allocate an sprite to the UI static memory.
Definition: ui_sprite.cpp:130
void Q_strncpyz(char *dest, const char *src, size_t destsize)
Safe strncpy that ensures a trailing zero.
Definition: shared.cpp:457
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.
Definition: ui_render.cpp:203
GLsizei size
Definition: r_gl.h:152
uiSprite_t sprites[UI_MAX_SPRITES]
Definition: ui_internal.h:86
#define OBJZERO(obj)
Definition: shared.h:178
bool single
Definition: ui_sprite.h:44
bool tiled_25_1_3
Definition: ui_sprite.h:48
const struct image_s * UI_LoadImage(const char *name)
Searches for an image in the image array.
Definition: ui_render.cpp:91
static const int tile_template_17_1_3[]
Definition: ui_sprite.cpp:153
int numSprites
Definition: ui_internal.h:87
char * image[SPRITE_STATUS_MAX]
Definition: ui_sprite.h:53
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
static bool UI_SpriteExists(const char *name)
Check if an sprite name exists.
Definition: ui_sprite.cpp:99
const value_t ui_spriteProperties[]
Definition: ui_sprite.cpp:31
#define Vector2FromInt(x, y)
Definition: vector.h:40
Definition: scripts.h:49
#define MAX_QPATH
Definition: filesys.h:40
QGL_EXTERN GLint i
Definition: r_gl.h:113
Definition: scripts.h:50
QGL_EXTERN GLuint GLsizei GLsizei GLint GLenum GLchar * name
Definition: r_gl.h:110
int height
Definition: r_image.h:64
vec4_t color[SPRITE_STATUS_MAX]
Definition: ui_sprite.h:52
uiSpriteStatus_t
Definition: ui_sprite.h:32
#define UI_MAX_SPRITES
Definition: ui_sprite.h:27
char name[MAX_VAR]
Definition: ui_sprite.h:42
#define MEMBER_SIZEOF(TYPE, MEMBER)
Definition: scripts.h:34
static const int tile_template_25_1_3[]
Definition: ui_sprite.cpp:164
vec_t vec2_t[2]
Definition: ufotypes.h:38
Definition: scripts.h:52
static const int tile_template_popup[]
Definition: ui_sprite.cpp:174
#define Q_streq(a, b)
Definition: shared.h:136
vec2_t pos[SPRITE_STATUS_MAX]
Definition: ui_sprite.h:54
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
Definition: ui_render.cpp:293
bool blend
Definition: ui_sprite.h:45
uiSprite_t * UI_GetSpriteByName(const char *name)
Return an sprite by is name.
Definition: ui_sprite.cpp:115
bool pack64
Definition: ui_sprite.h:46
char * UI_AllocStaticString(const char *string, int size)
Allocate a string into the UI static memory.
Definition: ui_parse.cpp:204
Definition: scripts.h:55
bool tiled_popup
Definition: ui_sprite.h:49