naev 0.12.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"
12
13#include "slots.h"
14
15#include "array.h"
16#include "log.h"
17#include "ndata.h"
18#include "nxml.h"
19
20#define SP_XML_ID "slot"
21
25typedef struct SlotProperty_s {
26 char *name;
27 char *display;
31 int locked;
32 int visible;
35
36static SlotProperty_t *sp_array = NULL;
37
38/*
39 * Prototypes.
40 */
41static int sp_check( unsigned int spid );
42
46int sp_load( void )
47{
48 char **sp_files = ndata_listRecursive( SP_DATA_PATH );
49
50 /* First pass, loads up ammunition. */
52 for ( int i = 0; i < array_size( sp_files ); i++ ) {
54 xmlNodePtr node, cur;
55 xmlDocPtr doc;
56
57 /* Load and read the data. */
58 doc = xml_parsePhysFS( sp_files[i] );
59 if ( doc == NULL )
60 continue;
61
62 /* Check to see if document exists. */
63 node = doc->xmlChildrenNode;
64 if ( !xml_isNode( node, SP_XML_ID ) ) {
65 WARN( _( "Malformed '%s' file: missing root element '%s'" ),
66 sp_files[i], SP_XML_ID );
67 continue;
68 }
69
70 sp = &array_grow( &sp_array );
71 memset( sp, 0, sizeof( SlotProperty_t ) );
72 xmlr_attr_strd( node, "name", sp->name );
73 cur = node->xmlChildrenNode;
74 do {
75 xml_onlyNodes( cur );
76
77 /* Load data. */
78 xmlr_strd( cur, "display", sp->display );
79 xmlr_strd( cur, "description", sp->description );
80 if ( xml_isNode( cur, "required" ) ) {
81 sp->required = 1;
82 continue;
83 }
84 if ( xml_isNode( cur, "exclusive" ) ) {
85 sp->exclusive = 1;
86 continue;
87 }
88 if ( xml_isNode( cur, "locked" ) ) {
89 sp->locked = 1;
90 continue;
91 }
92 if ( xml_isNode( cur, "visible" ) ) {
93 sp->visible = 1;
94 continue;
95 }
96 if ( xml_isNode( cur, "icon" ) ) {
97 char path[STRMAX_SHORT];
98 snprintf( path, sizeof( path ), "gfx/slots/%s", xml_get( cur ) );
99 sp->icon = xml_parseTexture( cur, path, 1, 1, OPENGL_TEX_SDF );
100 continue;
101 }
102
103 WARN( _( "Slot Property '%s' has unknown node '%s'." ), node->name,
104 cur->name );
105 } while ( xml_nextNode( cur ) );
106
107 /* Clean up. */
108 xmlFreeDoc( doc );
109 }
110
111 for ( int i = 0; i < array_size( sp_files ); i++ )
112 free( sp_files[i] );
113 array_free( sp_files );
114 return 0;
115}
116
120void sp_cleanup( void )
121{
122 for ( int i = 0; i < array_size( sp_array ); i++ ) {
123 SlotProperty_t *sp = &sp_array[i];
124 free( sp->name );
125 free( sp->display );
126 free( sp->description );
127 gl_freeTexture( sp->icon );
128 }
130 sp_array = NULL;
131}
132
139unsigned int sp_get( const char *name )
140{
141 if ( name == NULL )
142 return 0;
143 for ( int i = 0; i < array_size( sp_array ); i++ ) {
144 SlotProperty_t *sp = &sp_array[i];
145 if ( strcmp( sp->name, name ) == 0 )
146 return i + 1;
147 }
148 WARN( _( "Slot property '%s' not found in array." ), name );
149 return 0;
150}
151
155static int sp_check( unsigned int spid )
156{
157 if ( ( spid == 0 ) || ( spid > (unsigned int)array_size( sp_array ) ) )
158 return 1;
159 return 0;
160}
161
165const char *sp_display( unsigned int spid )
166{
167 if ( sp_check( spid ) )
168 return NULL;
169 return sp_array[spid - 1].display;
170}
171
175const char *sp_description( unsigned int spid )
176{
177 if ( sp_check( spid ) )
178 return NULL;
179 return sp_array[spid - 1].description;
180}
181
185int sp_required( unsigned int spid )
186{
187 if ( sp_check( spid ) )
188 return 0;
189 return sp_array[spid - 1].required;
190}
191
195int sp_exclusive( unsigned int spid )
196{
197 if ( sp_check( spid ) )
198 return 0;
199 return sp_array[spid - 1].exclusive;
200}
201
205int sp_locked( unsigned int spid )
206{
207 if ( sp_check( spid ) )
208 return 0;
209 return sp_array[spid - 1].locked;
210}
211
215int sp_visible( unsigned int spid )
216{
217 if ( sp_check( spid ) )
218 return 0;
219 return sp_array[spid - 1].visible;
220}
221
225const glTexture *sp_icon( unsigned int spid )
226{
227 if ( sp_check( spid ) )
228 return 0;
229 return sp_array[spid - 1].icon;
230}
Provides macros to work with dynamic arrays.
#define array_free(ptr_array)
Frees memory allocated and sets array to NULL.
Definition array.h:170
static ALWAYS_INLINE int array_size(const void *array)
Returns number of elements in the array.
Definition array.h:179
#define array_grow(ptr_array)
Increases the number of elements by one and returns the last element.
Definition array.h:122
#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:286
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:25
xmlDocPtr xml_parsePhysFS(const char *filename)
Analogous to xmlParseMemory/xmlParseFile.
Definition nxml.c:70
void gl_freeTexture(glTexture *texture)
Frees a texture.
Definition opengl_tex.c:835
int sp_locked(unsigned int spid)
Gets whether or not a slot property is locked.
Definition slots.c:205
unsigned int sp_get(const char *name)
Gets the id of a slot property.
Definition slots.c:139
const char * sp_description(unsigned int spid)
Gets the description of a slot property (in English).
Definition slots.c:175
void sp_cleanup(void)
Cleans up after the slot properties.
Definition slots.c:120
static int sp_check(unsigned int spid)
Checks to see if in bound of array.
Definition slots.c:155
int sp_load(void)
Initializes the slot properties.
Definition slots.c:46
#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:225
const char * sp_display(unsigned int spid)
Gets the display name of a slot property (in English).
Definition slots.c:165
int sp_required(unsigned int spid)
Gets whether or not a slot property is required.
Definition slots.c:185
int sp_exclusive(unsigned int spid)
Gets whether or not a slot property is exclusive.
Definition slots.c:195
int sp_visible(unsigned int spid)
Whether or not the slot should be visible even if locked.
Definition slots.c:215
static SlotProperty_t * sp_array
Definition slots.c:36
Representation of a slot property.
Definition slots.c:25
char * description
Definition slots.c:28
int visible
Definition slots.c:32
int required
Definition slots.c:29
char * display
Definition slots.c:27
int exclusive
Definition slots.c:30
glTexture * icon
Definition slots.c:33
char * name
Definition slots.c:26
Abstraction for rendering sprite sheets.
Definition opengl_tex.h:43