naev 0.11.5
slots.c
Go to the documentation of this file.
1/*
2 * See Licensing and Copyright notice in naev.h
3 */
10#include "naev.h"
13#include "slots.h"
14
15#include "array.h"
16#include "log.h"
17#include "nxml.h"
18#include "ndata.h"
19
20#define SP_XML_ID "slot"
25typedef struct SlotProperty_s {
26 char *name;
27 char *display;
31 int locked;
34
35static SlotProperty_t *sp_array = NULL;
37/*
38 * Prototypes.
39 */
40static int sp_check( unsigned int spid );
41
45int sp_load (void)
46{
47 char **sp_files = ndata_listRecursive( SP_DATA_PATH );
48
49 /* First pass, loads up ammunition. */
51 for (int i=0; i<array_size(sp_files); i++) {
53 xmlNodePtr node, cur;
54 xmlDocPtr doc;
55
56 /* Load and read the data. */
57 doc = xml_parsePhysFS( sp_files[i] );
58 if (doc == NULL)
59 continue;
60
61 /* Check to see if document exists. */
62 node = doc->xmlChildrenNode;
63 if (!xml_isNode(node,SP_XML_ID)) {
64 WARN(_("Malformed '%s' file: missing root element '%s'"), sp_files[i], SP_XML_ID );
65 continue;
66 }
67
68 sp = &array_grow( &sp_array );
69 memset( sp, 0, sizeof(SlotProperty_t) );
70 xmlr_attr_strd( node, "name", sp->name );
71 cur = node->xmlChildrenNode;
72 do {
73 xml_onlyNodes(cur);
74
75 /* Load data. */
76 xmlr_strd( cur, "display", sp->display );
77 xmlr_strd( cur, "description", sp->description );
78 if (xml_isNode( cur, "required" )) {
79 sp->required = 1;
80 continue;
81 }
82 if (xml_isNode( cur, "exclusive" )) {
83 sp->exclusive = 1;
84 continue;
85 }
86 if (xml_isNode( cur, "locked" )) {
87 sp->locked = 1;
88 continue;
89 }
90 if (xml_isNode( cur, "icon" )) {
91 char path[STRMAX_SHORT];
92 snprintf( path, sizeof(path), "gfx/slots/%s", xml_get(cur) );
93 sp->icon = xml_parseTexture( cur, path, 1, 1, OPENGL_TEX_SDF );
94 continue;
95 }
96
97
98 WARN(_("Slot Property '%s' has unknown node '%s'."), node->name, cur->name);
99 } while (xml_nextNode(cur));
100
101 /* Clean up. */
102 xmlFreeDoc(doc);
103 }
104
105 for (int i=0; i<array_size(sp_files); i++)
106 free( sp_files[i] );
107 array_free( sp_files );
108 return 0;
109}
110
114void sp_cleanup (void)
115{
116 for (int i=0; i<array_size(sp_array); i++) {
117 SlotProperty_t *sp = &sp_array[i];
118 free( sp->name );
119 free( sp->display );
120 free( sp->description );
121 gl_freeTexture( sp->icon );
122 }
124 sp_array = NULL;
125}
126
133unsigned int sp_get( const char *name )
134{
135 if (name==NULL)
136 return 0;
137 for (int i=0; i<array_size(sp_array); i++) {
138 SlotProperty_t *sp = &sp_array[i];
139 if (strcmp( sp->name, name ) == 0)
140 return i+1;
141 }
142 WARN(_("Slot property '%s' not found in array."), name);
143 return 0;
144}
145
149static int sp_check( unsigned int spid )
150{
151 if ((spid==0) || (spid > (unsigned int)array_size(sp_array)))
152 return 1;
153 return 0;
154}
155
159const char *sp_display( unsigned int spid )
160{
161 if (sp_check(spid))
162 return NULL;
163 return sp_array[ spid-1 ].display;
164}
165
169const char *sp_description( unsigned int spid )
170{
171 if (sp_check(spid))
172 return NULL;
173 return sp_array[ spid-1 ].description;
174}
175
179int sp_required( unsigned int spid )
180{
181 if (sp_check(spid))
182 return 0;
183 return sp_array[ spid-1 ].required;
184}
185
189int sp_exclusive( unsigned int spid )
190{
191 if (sp_check(spid))
192 return 0;
193 return sp_array[ spid-1 ].exclusive;
194}
195
199int sp_locked( unsigned int spid )
200{
201 if (sp_check(spid))
202 return 0;
203 return sp_array[ spid-1 ].locked;
204}
205
209const glTexture * sp_icon( unsigned int spid )
210{
211 if (sp_check(spid))
212 return 0;
213 return sp_array[ spid-1 ].icon;
214}
Provides macros to work with dynamic arrays.
#define array_free(ptr_array)
Frees memory allocated and sets array to NULL.
Definition array.h:158
static ALWAYS_INLINE int array_size(const void *array)
Returns number of elements in the array.
Definition array.h:168
#define array_grow(ptr_array)
Increases the number of elements by one and returns the last element.
Definition array.h:119
#define array_create(basic_type)
Creates a new dynamic array of ‘basic_type’.
Definition array.h:93
Header file with generic functions and naev-specifics.
char ** ndata_listRecursive(const char *path)
Lists all the visible files in a directory, at any depth.
Definition ndata.c:231
glTexture * xml_parseTexture(xmlNodePtr node, const char *path, int defsx, int defsy, const unsigned int flags)
Parses a texture handling the sx and sy elements.
Definition nxml.c:29
xmlDocPtr xml_parsePhysFS(const char *filename)
Analogous to xmlParseMemory/xmlParseFile.
Definition nxml.c:75
void gl_freeTexture(glTexture *texture)
Frees a texture.
Definition opengl_tex.c:862
int sp_locked(unsigned int spid)
Gets whether or not a slot property is locked.
Definition slots.c:199
unsigned int sp_get(const char *name)
Gets the id of a slot property.
Definition slots.c:133
const char * sp_description(unsigned int spid)
Gets the description of a slot property (in English).
Definition slots.c:169
void sp_cleanup(void)
Cleans up after the slot properties.
Definition slots.c:114
static int sp_check(unsigned int spid)
Checks to see if in bound of array.
Definition slots.c:149
int sp_load(void)
Initializes the slot properties.
Definition slots.c:45
#define SP_XML_ID
Definition slots.c:20
const glTexture * sp_icon(unsigned int spid)
Gets the icon associated with the slot.
Definition slots.c:209
const char * sp_display(unsigned int spid)
Gets the display name of a slot property (in English).
Definition slots.c:159
int sp_required(unsigned int spid)
Gets whether or not a slot property is required.
Definition slots.c:179
int sp_exclusive(unsigned int spid)
Gets whether or not a slot property is exclusive.
Definition slots.c:189
static SlotProperty_t * sp_array
Definition slots.c:35
Representation of a slot property.
Definition slots.c:25
char * description
Definition slots.c:28
int required
Definition slots.c:29
char * display
Definition slots.c:27
int exclusive
Definition slots.c:30
glTexture * icon
Definition slots.c:32
char * name
Definition slots.c:26
Abstraction for rendering sprite sheets.
Definition opengl_tex.h:36