ugBASIC 1.18
An isomorphic BASIC language compiler for retrocomputers
Loading...
Searching...
No Matches
tile_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
38/****************************************************************************
39 * CODE SECTION
40 ****************************************************************************/
41
49/* <usermanual>
50@keyword LOAD TILE
51
52@english
53
54The ''LOAD TILE'' command allows you to load an image and to convert it into
55an ''TILE'', that is a format that will be converted into a way that can be efficiently drawn
56on the screen using the tiles. The second parameter is the mode to use to convert
57the given data (by default, it is equal to current mode)
58
59The command support a set of modern image format, like JPEG baseline & progressive,
60PNG 1/2/4/8/16-bit-per-channel, TGA, BMP (non-1bpp, non-RLE), PSD
61(composited view only, no extra channels, 8/16 bit-per-channel),
62GIF, HDR (radiance rgbE format), PIC (Softimage PIC) and PNM (PPM and PGM
63binary only) The image will be converted into a way that can be
64efficiently drawn on the screen.
65
66Since it is possible to load only one file of the same
67type at a time, it is necessary to be able to indicate an "alias" with
68which to exceed this limit. In this regard, there is also
69the ''AS'' syntax, which allows you to load the same file several
70times but with different names.
71
72A series of flags, separated by spaces, can be added at loading time
73to modify the behavior of ugBASIC.
74
75The ''FLIP X'' flag allows you to flip the image horizontally,
76before translating it into the native format. The same is true for the
77''FLIP Y'' command, which instead inverts the image vertically.
78There is also the ''FLIP XY'' (or ''FLIP YX'') parameter to act,
79simultaneously, on both directions.
80
81The ''COMPRESSED'' flag allows you to compress the image, if
82possible. Compression is a space-saving mechanism, in which the
83native data of the image is represented in a more compact form,
84which ugBASIC will be able to quickly convert into graphics at
85the appropriate time.
86
87The ''ROLL X'' flag allows you to SHIFT the (entire) image horizontally,
88for the entire size of a frame, in order to generate intermediate
89frames. The very same for ''ROLL Y'' command, which does the same
90vertically. There is also the ''ROLL XY'' (or ''ROLL YX'') parameter to act,
91simultaneously, on both directions.
92
93@italian
94
95Il comando ''LOAD TILE'' consente di caricare un'immagine e convertirla in
96una ''IMAGE''. Il secondo parametro è la modalità da utilizzare per convertire
97i dati forniti (per impostazione predefinita, è uguale alla modalità corrente)
98
99Il comando supporta un set di formati immagine moderni, come JPEG baseline e progressivo,
100PNG 1/2/4/8/16 bit per canale, TGA, BMP (non 1bpp, non RLE), PSD
101(solo vista composita, nessun canale extra, 8/16 bit per canale),
102GIF, HDR (formato radiance rgbE), PIC (Softimage PIC) e PNM (solo PPM e PGM
103binary). L'immagine verrà convertita in un modo che può essere
104disegnata in modo efficiente sullo schermo.
105
106Poiché è possibile caricare un solo file dello stesso
107tipo alla volta, è necessario poter indicare un "alias" con
108il quale superare questo limite. A questo proposito, esiste anche
109la sintassi ''AS'', che consente di caricare più volte lo stesso
110file ma con nomi diversi.
111
112Una serie di flag, separati da spazi, possono essere aggiunti in fase di caricamento
113per modificare il comportamento di ugBASIC.
114
115Il flag ''FLIP X'' consente di capovolgere l'immagine orizzontalmente,
116prima di tradurla nel formato nativo. Lo stesso vale per il
117comando ''FLIP Y'', che invece inverte l'immagine verticalmente.
118Esiste anche il parametro ''FLIP XY'' (o ''FLIP YX'') per agire,
119contemporaneamente, su entrambe le direzioni.
120
121Il flag ''ROLL X'' consente di spostare l'intera immagine in orizzontale,
122per l'intera dimensione di un frame, per generare i fotogrammi intercalari.
123Lo stesso per il flag ''ROLL Y'', che fa lo stesso verticalmente, e
124''ROLL XY '' (o ''ROLL YX'') per agire, contemporaneamente, su entrambe le
125 direzioni.
126
127Il flag ''COMPRESSED'' consente di comprimere l'immagine, se
128possibile. La compressione è un meccanismo di risparmio di spazio, in cui i dati nativi dell'immagine sono rappresentati in una forma più compatta, che
129ugBASIC sarà in grado di convertire rapidamente in grafica al momento opportuno.
130
131L'immagine può essere caricata come immagine trasparente (se l'immagine originale
132non ha trasparenza) utilizzando la parola chiave ''TRANSPARENCY'',
133seguita da un parametro opzionale che rappresenta il colore
134da considerare come trasparente.
135
136L'immagine può essere caricata come immagine trasparente (se l'immagine originale non ha trasparenza) usando la parola chiave ''OPACITY'',
137seguita da un parametro opzionale che rappresenta il colore
138da considerare come pavimentazione.
139
140L'immagine può essere caricata direttamente nella memoria di espansione
141usando la parola chiave BANKED. Il numero rappresenta
142il residente condiviso da usare come target per questa immagine.
143Per alcuni target questa è l'impostazione predefinita. Se vuoi,
144puoi spostare l'immagine nella memoria residente
145usando la parola chiave ''UNBANKED''.
146
147Infine, se non è previsto che l'immagine cambi durante il gioco, può essere contrassegnata
148con l'attributo ''READONLY'': in questo caso, l'immagine verrà archiviata
149nella memoria di sola lettura, se disponibile.
150
151@syntax = LOAD TILE( filename [AS alias][,mode] ) [fl]
152@syntax fl : [FLIP X] [FLIP Y] [FLIP XY] [FLIP YX]
153@syntax fl : [ROLL X] [ROLL Y] [ROLL XY] [ROLL YX]
154
155@example starship = LOAD TILE("starship.png")
156@example starship2 = LOAD TILE("starship.png" AS "starship2")
157@example alienAt11 = LOAD TILE("alien.jpg",11)
158@example alien2 = LOAD TILE("alien.jpg" AS "alien2",11)
159
160@alias TILE LOAD
161</usermanual> */
162
163/* <usermanual>
164@keyword TILE LOAD
165@alias LOAD TILE
166</usermanual> */
167
168Variable * tile_load( Environment * _environment, char * _filename, int _flags, char * _tileset, int _index ) {
169
170 Variable * tileset = NULL;
171
172 if ( _tileset ) {
173 tileset = variable_retrieve( _environment, _tileset );
174 if ( tileset->type != VT_TILESET ) {
176 }
177 if ( ! _environment->tilesets[tileset->value] ) {
179 }
180 }
181
182 if ( _environment->tenLinerRulesEnforced ) {
184 }
185
186 if ( _environment->sandbox ) {
187 CRITICAL_SANDBOX_ENFORCED( "LOAD TITLE");
188 }
189
190 int width = 0;
191 int height = 0;
192 int depth = 0;
193
194 char * lookedFilename = resource_load_asserts( _environment, _filename );
195
196 adiline2("LT:%s:%s", _filename, lookedFilename );
197
198 unsigned char* source = stbi_load(lookedFilename, &width, &height, &depth, 0);
199
200 if ( !source ) {
202 }
203
204 if ( width != 8 ) {
206 }
207
208 if ( height != 8 ) {
210 }
211
212 if( _flags & FLAG_FLIP_X ) {
213 source = image_flip_x( _environment, source, width, height, depth );
214 }
215 if( _flags & FLAG_FLIP_Y ) {
216 source = image_flip_y( _environment, source, width, height, depth );
217 }
218
219 Variable * index = variable_temporary( _environment, VT_TILE, "(tile index)" );
220
221 Variable * realImage = image_converter( _environment, source, 8, 8, depth, 0, 0, 8, 8, BITMAP_MODE_DEFAULT, 0, _flags );
222
224
225 TileDescriptors * descriptors;
226
227 if ( tileset ) {
228 descriptors = _environment->tilesets[tileset->value];
229 } else {
230 if ( ! _environment->descriptors ) {
231 _environment->descriptors = malloc( sizeof( TileDescriptors ) );
232 memset( _environment->descriptors, 0, sizeof( TileDescriptors ) );
233 _environment->descriptors->count = 0;
234 _environment->descriptors->first = 1;
235 _environment->descriptors->firstFree = _environment->descriptors->first;
236 _environment->descriptors->lastFree = 128;
237 }
238 descriptors = _environment->descriptors;
239 }
240
241 int tile = _index;
242
243 if ( tile == -1 ) {
244 tile = tile_allocate( descriptors, realImage->valueBuffer + IMAGE_WIDTH_SIZE + IMAGE_HEIGHT_SIZE );
245 } else {
246 memcpy( descriptors->data[tile].data, realImage->valueBuffer + IMAGE_WIDTH_SIZE + IMAGE_HEIGHT_SIZE, 8 );
247 descriptors->descriptor[tile] = calculate_tile_descriptor( &descriptors->data[tile] );
248 }
249
250 if ( tile == -1 ) {
252 }
253
254 cpu_store_8bit( _environment, index->realName, tile );
255
256 variable_delete( _environment, realImage->name );
257
258 return index;
259
260}
void cpu_store_8bit(Environment *_environment, char *_destination, int _value)
CPU 6309: emit code to store 8 bit
Definition 6309.c:761
#define IMAGE_HEIGHT_SIZE
Definition 6847.h:202
#define BITMAP_MODE_DEFAULT
Definition 6847.h:95
#define IMAGE_WIDTH_SIZE
Definition 6847.h:200
Variable * variable_retrieve(Environment *_environment, char *_name)
int tile_allocate(TileDescriptors *_tiles, char *_data)
char * image_flip_y(Environment *_environment, char *_source, int _width, int _height, int _depth)
TileDescriptor * calculate_tile_descriptor(TileData *_tileData)
char * image_flip_x(Environment *_environment, char *_source, int _width, int _height, int _depth)
int variable_delete(Environment *_environment, char *_name)
char * resource_load_asserts(Environment *_environment, char *_filename)
Variable * variable_temporary(Environment *_environment, VariableType _type, char *_meaning)
Define a temporary variable.
Variable * image_converter(Environment *_environment, char *_data, int _width, int _height, int _depth, int _offset_x, int _offset_y, int _frame_width, int _frame_height, int _mode, int _transparent_color, int _flags)
STBIDEF void stbi_image_free(void *retval_from_stbi_load)
STBIDEF stbi_uc * stbi_load(char const *filename, int *x, int *y, int *channels_in_file, int desired_channels)
TileDescriptors * tilesets[MAX_TILESETS]
Definition ugbc.h:2949
int sandbox
Definition ugbc.h:2990
int tenLinerRulesEnforced
Definition ugbc.h:2985
TileDescriptors * descriptors
Definition ugbc.h:2939
char data[8]
Definition ugbc.h:2141
TileData data[512]
Definition ugbc.h:2153
TileDescriptor * descriptor[512]
Definition ugbc.h:2152
unsigned char * valueBuffer
Definition ugbc.h:1061
char * name
Definition ugbc.h:979
VariableType type
Definition ugbc.h:988
int value
Definition ugbc.h:1025
char * realName
Definition ugbc.h:982
Variable * tile_load(Environment *_environment, char *_filename, int _flags, char *_tileset, int _index)
Emit code for LOAD TILE(...).
Definition tile_load.c:168
void * malloc(YYSIZE_T)
#define CRITICAL_10_LINE_RULES_ENFORCED(v)
Definition ugbc.h:3550
#define CRITICAL_CANNOT_ALLOCATE_MORE_TILE()
Definition ugbc.h:3556
struct _TileDescriptors TileDescriptors
struct _Variable Variable
Structure of a single variable.
struct _Environment Environment
Structure of compilation environment.
#define FLAG_FLIP_X
Definition ugbc.h:4553
@ VT_TILE
Definition ugbc.h:504
@ VT_TILESET
Definition ugbc.h:510
#define CRITICAL_TILE_LOAD_UNKNOWN_FORMAT(f)
Definition ugbc.h:3558
#define CRITICAL_SANDBOX_ENFORCED(v)
Definition ugbc.h:3687
#define CRITICAL_TILE_LOAD_ON_NON_TILESET(t)
Definition ugbc.h:3561
#define CRITICAL_TILE_INVALID_HEIGHT(h)
Definition ugbc.h:3560
#define FLAG_FLIP_Y
Definition ugbc.h:4554
#define CRITICAL_TILE_INVALID_WIDTH(w)
Definition ugbc.h:3559
#define adiline2(s, a, b)
Definition ugbc.h:4192