ugBASIC 1.18
An isomorphic BASIC language compiler for retrocomputers
Loading...
Searching...
No Matches
tilemap_load.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 "../../ugbc.h"
37#include "../../libs/msc1.h"
38#include "../../libs/tsx.h"
39
40/****************************************************************************
41 * CODE SECTION
42 ****************************************************************************/
43
51/* <usermanual>
52@keyword LOAD TILEMAP
53
54@english
55
56The ''LOAD TILEMAP'' command allow to load a TILEMAP datatype that will represent a map
57of those drawn by means of Tiled.
58
59@italian
60
61Il comando ''LOAD TILEMAP'' permette di caricare un tipo di dati TILEMAP che rappresenterà
62una mappa di quelle disegnate tramite Tiled.
63
64@syntax = LOAD TILEMAP(filename)
65
66@example x = LOAD TILEMAP("dungeon.tmx")
67
68@target all
69</usermanual> */
70Variable * tilemap_load( Environment * _environment, char * _filename, char * _alias, int _mode, int _flags, int _transparent_color, int _background_color, int _bank_expansion ) {
71
73
74 Variable * final = variable_temporary( _environment, VT_TILEMAP, 0 );
75
76 if ( _environment->emptyProcedure ) {
77 return final;
78 }
79
80 if ( _environment->tenLinerRulesEnforced ) {
81 CRITICAL_10_LINE_RULES_ENFORCED( "LOAD TILEMAP");
82 }
83
84 if ( _environment->sandbox ) {
85 CRITICAL_SANDBOX_ENFORCED( "LOAD TILEMAP");
86 }
87
88 LoadedFile * first = _environment->loadedFiles;
89 char *lookfor = _filename;
90 if ( _alias ) {
91 lookfor = _alias;
92 }
93 while( first ) {
94 if ( strcmp(lookfor, first->fileName ) == 0 ) {
95 return first->variable;
96 }
97 first = first->next;
98 }
99
100 int width = 0;
101 int height = 0;
102 int depth = 0;
103
104 char * lookedFilename = resource_load_asserts( _environment, _filename );
105
106 TmxMap * tilemap = tmx_load( lookedFilename );
107
108 final->originalTilemap = tilemap;
109
110 if ( !tilemap ) {
112 }
113
114 if ( !tilemap->layers ) {
116 }
117
118 if ( !tilemap->tilesets ) {
120 }
121
122 TsxTileset * tileset = tilemap->tilesets;
123
124 while( tileset ) {
125 if ( final->tileset ) {
127 }
128 char name[MAX_TEMPORARY_STORAGE]; sprintf( name, "%stileset", label );
129 variable_global( _environment, name );
130 final->tileset = variable_define( _environment, name, VT_IMAGES, 0 );
131 char * tilesetFileName = strdup( lookedFilename );
132 char * tilesetFileNameWithPath = malloc( MAX_TEMPORARY_STORAGE );
133 memset( tilesetFileNameWithPath, 0, MAX_TEMPORARY_STORAGE );
134 char * separator = strrchr( tilesetFileName, '/' );
135 if ( separator ) {
136 *(separator+1) = 0;
137 strcopy( tilesetFileNameWithPath, tilesetFileName );
138 }
139 strcat( tilesetFileNameWithPath, tileset->source );
140 lookedFilename = resource_load_asserts( _environment, tilesetFileNameWithPath );
141 variable_direct_assign( _environment, final->tileset->name, tileset_load( _environment, lookedFilename, NULL, _mode, _flags, _transparent_color, _background_color, _bank_expansion )->name );
142 final->tileset->firstGid = tileset->firstgid;
143 tileset = tileset->next;
144 }
145
146 TmxLayer * layer = tilemap->layers;
147
148 while( layer ) {
149 // if ( final->valueBuffer ) {
150 // CRITICAL_TILEMAP_LOAD_ONLY_ONE_LAYER( _filename );
151 // }
152 int size = layer->width * layer->height;
153 char * mapData = NULL;
154 if ( final->valueBuffer ) {
155 if ( final->size != size ) {
157 }
158 mapData = realloc( final->valueBuffer, final->size + size );
159 final->valueBuffer = mapData;
160 mapData += final->size;
161 } else {
162 mapData = malloc( size );
163 final->valueBuffer = mapData;
164 }
165 memset( mapData, 0, size );
166 for(int i=0; i<size; ++i ) {
167 if ( layer->data[i] >= final->tileset->firstGid ) {
168 mapData[i] = ((unsigned char)layer->data[i]) - (final->tileset->firstGid);
169 } else {
170 mapData[i] = 0xff;
171 }
172 }
173 final->size += size;
174 final->mapWidth = layer->width;
175 final->mapHeight = layer->height;
176 ++final->mapLayers;
177 layer = layer->next;
178 }
179
180
181 LoadedFile * loaded = malloc( sizeof( LoadedFile ) );
182 loaded->next = first;
183 loaded->variable = final;
184 loaded->fileName = lookfor;
185 _environment->loadedFiles = loaded;
186
187 if ( _alias ) {
188 const_define_numeric( _environment, _alias, UNIQUE_RESOURCE_ID );
189 }
190
191 final->readonly = 1;
192
193 return final;
194
195}
void const_define_numeric(Environment *_environment, char *_name, int _value)
void variable_global(Environment *_environment, char *_pattern)
Variable * variable_define(Environment *_environment, char *_name, VariableType _type, int _value)
Define a variable for the program.
Variable * variable_direct_assign(Environment *_environment, char *_var, char *_expr)
char * resource_load_asserts(Environment *_environment, char *_filename)
Variable * variable_temporary(Environment *_environment, VariableType _type, char *_meaning)
Define a temporary variable.
int size
Definition _optimizer.c:678
char * name
Definition _optimizer.c:672
LoadedFile * loadedFiles
Definition ugbc.h:2636
int sandbox
Definition ugbc.h:2990
int tenLinerRulesEnforced
Definition ugbc.h:2985
int emptyProcedure
Definition ugbc.h:2932
char * fileName
Definition ugbc.h:1481
struct _LoadedFile * next
Definition ugbc.h:1486
Variable * variable
Definition ugbc.h:1483
int width
Definition tmx.h:62
struct _TmxLayer * next
Definition tmx.h:67
int * data
Definition tmx.h:65
int height
Definition tmx.h:63
struct _TsxTileset * tilesets
Definition tmx.h:85
struct _TmxLayer * layers
Definition tmx.h:87
struct _TsxTileset * next
Definition tsx.h:74
int firstgid
Definition tsx.h:66
char * source
Definition tsx.h:61
Variable * tilemap_load(Environment *_environment, char *_filename, char *_alias, int _mode, int _flags, int _transparent_color, int _background_color, int _bank_expansion)
Emit code for LOAD TILEMAP(...).
Variable * tileset_load(Environment *_environment, char *_filename, char *_alias, int _mode, int _flags, int _transparent_color, int _background_color, int _bank_expansion)
Emit code for LOAD TILESERT(...).
TmxMap * tmx_load(char *_filename)
Definition tmx.c:48
struct _TmxMap TmxMap
struct _TmxLayer TmxLayer
struct _TsxTileset TsxTileset
void * malloc(YYSIZE_T)
#define CRITICAL_TILEMAP_LOAD_ONLY_ONE_TILESET(v)
Definition ugbc.h:3641
#define CRITICAL_10_LINE_RULES_ENFORCED(v)
Definition ugbc.h:3550
#define CRITICAL_TILEMAP_LOAD_UNKNOWN_FORMAT(v)
Definition ugbc.h:3638
#define CRITICAL_TILEMAP_LOAD_ONLY_SAME_SIZE_LAYER(v)
Definition ugbc.h:3653
#define MAX_TEMPORARY_STORAGE
Definition ugbc.h:563
#define UNIQUE_RESOURCE_ID
Definition ugbc.h:3350
struct _Variable Variable
Structure of a single variable.
#define CRITICAL_TILEMAP_LOAD_MISSING_TILESET(v)
Definition ugbc.h:3640
struct _Environment Environment
Structure of compilation environment.
@ VT_TILEMAP
Definition ugbc.h:525
@ VT_IMAGES
Definition ugbc.h:495
#define CRITICAL_SANDBOX_ENFORCED(v)
Definition ugbc.h:3687
struct _LoadedFile LoadedFile
#define CRITICAL_TILEMAP_LOAD_MISSING_LAYER(v)
Definition ugbc.h:3639
#define MAKE_LABEL
Definition ugbc.h:3351
char * strcopy(char *_dest, char *_source)