ugBASIC 1.18
An isomorphic BASIC language compiler for retrocomputers
Loading...
Searching...
No Matches
tsx.c
Go to the documentation of this file.
1/*****************************************************************************
2 * ugBASIC - an isomorphic BASIC language compiler for retrocomputers *
3 *****************************************************************************
4 * Copyright 2021-2026 Marco Spedaletti (asimov@mclink.it)
5 *
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
17 *----------------------------------------------------------------------------
18 * Concesso in licenza secondo i termini della Licenza Apache, versione 2.0
19 * (la "Licenza"); è proibito usare questo file se non in conformità alla
20 * Licenza. Una copia della Licenza è disponibile all'indirizzo:
21 *
22 * http://www.apache.org/licenses/LICENSE-2.0
23 *
24 * Se non richiesto dalla legislazione vigente o concordato per iscritto,
25 * il software distribuito nei termini della Licenza è distribuito
26 * "COSÌ COM'È", SENZA GARANZIE O CONDIZIONI DI ALCUN TIPO, esplicite o
27 * implicite. Consultare la Licenza per il testo specifico che regola le
28 * autorizzazioni e le limitazioni previste dalla medesima.
29 ****************************************************************************/
30
31/****************************************************************************
32 * INCLUDE SECTION
33 ****************************************************************************/
34
35#include "tsx.h"
36
37#include <stdio.h>
38#include <string.h>
39#include <stdlib.h>
40#include <limits.h>
41#include <unistd.h>
42#include <libxml/parser.h>
43#include <libxml/tree.h>
44
45char * strcopy( char * _dest, const char * _source );
46
47TsxTileset * tsx_load( char * _filename ) {
48
49 TsxTileset * result = NULL;
50
51 xmlDocPtr doc;
52
53 doc = xmlReadFile( _filename, NULL, 0 );
54
55 if ( ! doc )
56 return result;
57
58 result = malloc( sizeof( TsxTileset ) );
59 memset( result, 0, sizeof ( TsxTileset ) );
60
61 xmlNode *root_element = NULL;
62 xmlNode *cur_node = NULL;
63
64 root_element = xmlDocGetRootElement(doc);
65
66 for (cur_node = root_element; cur_node; cur_node = cur_node->next) {
67 if (cur_node->type == XML_ELEMENT_NODE) {
68 if ( strcmp( cur_node->name, "tileset" ) == 0 ) {
69
70 struct _xmlAttr * properties = cur_node->properties;
71 while( properties ) {
72 if ( properties->type == XML_ATTRIBUTE_NODE ) {
73 xmlChar* value = xmlNodeListGetString(cur_node->doc, properties->children, 1);
74 if ( strcmp( properties->name, "version") == 0 ) {
75 result->version = strdup( value );
76 } else if ( strcmp( properties->name, "tiledVersion") == 0 ) {
77 result->tiledversion = strdup( value );
78 } else if ( strcmp( properties->name, "name") == 0 ) {
79 result->name = strdup( value );
80 } else if ( strcmp( properties->name, "tilewidth") == 0 ) {
81 result->tilewidth = atoi( value );
82 } else if ( strcmp( properties->name, "tileheight") == 0 ) {
83 result->tileheight = atoi( value );
84 } else if ( strcmp( properties->name, "tilecount") == 0 ) {
85 result->tilecount = atoi( value );
86 } else if ( strcmp( properties->name, "columns") == 0 ) {
87 result->columns = atoi( value );
88 } else if ( strcmp( properties->name, "spacing") == 0 ) {
89 result->spacing = atoi( value );
90 } else if ( strcmp( properties->name, "margin") == 0 ) {
91 result->margin = atoi( value );
92 } else if ( strcmp( properties->name, "objectalignment") == 0 ) {
93
94 // Object Alignment
95 // The alignment to use for tile objects referring to tiles from this tileset.
96 // This affects the placement of the tile relative to the position of the object
97 // (the origin) and is also the location around which the rotation is applied.
98 // Possible values are: Unspecified (the default), Top Left, Top, Top Right,
99 // Left, Center, Right, Bottom Left, Bottom and Bottom Right. When unspecified,
100 // tile object alignment is generally Bottom Left, except for Isometric maps
101 // where it is Bottom.
102
103 // currently unsupported
104
105 } else if ( strcmp( properties->name, "tilerendersize") == 0 ) {
106
107 // currently unsupported
108
109 } else if ( strcmp( properties->name, "backgroundcolor") == 0 ) {
110
111 // currently unsupported
112
113 } else if ( strcmp( properties->name, "fillmode") == 0 ) {
114
115 // currently unsupported
116
117 }
118 xmlFree(value);
119 }
120 properties = properties->next;
121 }
122
123 xmlNode * child = cur_node->children;
124 while( child ) {
125 if ( strcmp( child->name, "image" ) == 0 ) {
126
127 TsxImage * image = malloc( sizeof( TsxImage ) );
128 memset( image, 0, sizeof ( TsxImage ) );
129
130 struct _xmlAttr * properties = child->properties;
131 while( properties ) {
132 if ( properties->type == XML_ATTRIBUTE_NODE ) {
133 xmlChar* value = xmlNodeListGetString(child->doc, properties->children, 1);
134 if ( strcmp( properties->name, "source") == 0 ) {
135 image->source = strdup( value );
136 } if ( strcmp( properties->name, "width") == 0 ) {
137 image->width = atoi( value );
138 } if ( strcmp( properties->name, "height") == 0 ) {
139 image->height = atoi( value );
140 }
141 xmlFree(value);
142 }
143 properties = properties->next;
144 }
145
146 result->image = image;
147
148 } else if ( strcmp( child->name, "tileoffset" ) == 0 ) {
149
150 // Drawing Offset
151 // A drawing offset in pixels, applied when rendering any tile from the
152 // tileset (as part of tile layers or as tile objects). This is can be
153 // useful to make your tiles align to the grid.
154
155 // currently unsupported
156
157 } else if ( strcmp( child->name, "grid" ) == 0 ) {
158
159 // Orientation
160 // When the tileset contains isometric tiles, you can set this to Isometric.
161 // This value, along with the Grid Width and Grid Height properties, is
162 // taken into account by overlays rendered on top of the tiles. This helps
163 // for example when specifying Terrain Information. It also affects the
164 // orientation used by the Tile Collision Editor.
165
166 // currently unsupported
167
168 } else if ( strcmp( child->name, "transformations" ) == 0 ) {
169
170 // Tiled supports flipping and rotating tiles. When using terrains, tiles
171 // can be automatically flipped and/or rotated to create variations that
172 // would otherwise not be available in a tileset. This can be enabled in
173 // the Tileset Properties.
174
175 // The following transformation-related options are available:
176
177 // Flip Horizontally
178 // Allow tiles to be flipped horizontally.
179
180 // Flip Vertically
181 // Allow tiles to be flipped vertically. This would be left disabled
182 // when the graphics contain shadows in vertical direction, for example.
183
184 // Rotate
185 // Allow tiles to be rotated (by 90, 180 or 270-degrees).
186
187 // Prefer Untransformed Tiles
188 // When transformations are enabled, it could happen that a certain pattern
189 // can be filled by either a regular tile or a transformed tile. With this
190 // option enabled, the untransformed tiles will always take precedence.
191 // Leaving this option disabled allows transformations to be used to create
192 // more variation.
193
194 // currently unsupported
195
196 } else if ( strcmp( child->name, "tile" ) == 0 ) {
197
198 TsxTile * tile = malloc( sizeof( TsxTile ) );
199 memset( tile, 0, sizeof ( TsxTile ) );
200
201 tile->probability = 1.0f;
202
203 struct _xmlAttr * properties = child->properties;
204 while( properties ) {
205 if ( properties->type == XML_ATTRIBUTE_NODE ) {
206 xmlChar* value = xmlNodeListGetString(child->doc, properties->children, 1);
207 if ( strcmp( properties->name, "id") == 0 ) {
208 tile->id = atoi( value );
209 } if ( strcmp( properties->name, "type") == 0 ) {
210 tile->type = strdup( value );
211 } if ( strcmp( properties->name, "probability") == 0 ) {
212 tile->probability = atof( value );
213 }
214 xmlFree(value);
215 }
216 properties = properties->next;
217 }
218
219 if ( result->tiles ) {
220 tile->next = result->tiles;
221 result->tiles = tile;
222 } else {
223 result->tiles = tile;
224 }
225
226 }
227
228 child = child->next;
229 }
230
231 break;
232
233 }
234 }
235 }
236
237 xmlFreeDoc(doc);
238
239 return result;
240
241}
int width
Definition tsx.h:41
int height
Definition tsx.h:42
char * source
Definition tsx.h:40
double probability
Definition tsx.h:50
char * type
Definition tsx.h:49
struct _TsxTile * next
Definition tsx.h:52
int id
Definition tsx.h:48
int spacing
Definition tsx.h:68
struct _TsxImage * image
Definition tsx.h:70
int columns
Definition tsx.h:65
char * version
Definition tsx.h:58
char * name
Definition tsx.h:60
struct _TsxTile * tiles
Definition tsx.h:72
char * tiledversion
Definition tsx.h:59
int margin
Definition tsx.h:67
int tilecount
Definition tsx.h:64
int tilewidth
Definition tsx.h:62
int tileheight
Definition tsx.h:63
char * strcopy(char *_dest, const char *_source)
TsxTileset * tsx_load(char *_filename)
Definition tsx.c:47
struct _TsxTile TsxTile
struct _TsxTileset TsxTileset
struct _TsxImage TsxImage
void * malloc(YYSIZE_T)