ugBASIC 1.18
An isomorphic BASIC language compiler for retrocomputers
Loading...
Searching...
No Matches
csprite_init.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"
36
37/****************************************************************************
38 * CODE SECTION
39 ****************************************************************************/
40
41#if defined(__c64__) || defined(__c64reu__) || defined(__c128__)
42
49
50/* <usermanual>
51@keyword CSPRITE
52
53@english
54
55This statement allows you to define a composite sprite. Composite sprites are those
56sprites that are rendered by means of an overlay of hardware sprites. The definition
57corresponds to the transfer of graphic information from the graphic resource to the
58memory space of the machine's hardware. In practice, it is the moment in which the
59sprite is defined graphically, to be displayed.
60
61The function accepts the ''name'' of a graphic resource, among those previously
62loaded. This must correspond to a single image: therefore it can be an image
63or a frame of an ''ATLAS'' or a ''SEQUENCE''. The system, in a completely
64automatic way, will convert the resource into graphic data compatible with the
65sprite format, decoding each color component with a separate sprite.
66
67The command accepts a series of parameters, which can be added after the
68name of the graphic resource. The ''EXPAND VERTICAL'' parameter allows you
69to double the vertical dimensions, making each pixel occupy two or more
70vertical pixels. On the other hand, the ''COMPRESS VERTICAL'' command allows
71you to restore the original dimensions. The ''EXPAND HORIZONTAL'' parameter
72allows you to double the vertical dimensions, making each pixel occupy
73two or more vertical pixels. On the other hand, the ''COMPRESS HORIZONTAL''
74command allows you to restore the original dimensions. Finally, it is possible
75to indicate one of the colors in the palette as "transparent", and therefore
76that it does not need to generate a hardware sprite. This can be useful where
77the graphic resource does not use the color black, which is the standard color
78to characterize the background. Due to the isomorphic nature of the language,
79not all flags are usable, or have an effect, on all targets that support sprites.
80
81The function provides additional syntax, valid for redefining a sprite already
82defined. The purpose of such syntax is to modify the graphic information inherent
83to a sprite already defined previously, thus being able to dynamically replace the
84appearance of the sprite. In this case, it is necessary to provide a reference to
85the previously defined sprite (''previous''), and ensure that the graphic
86characteristics (such as the number of colors) are identical.
87
88@italian
89
90@syntax = CSPRITE ( name flags )
91@syntax = CSPRITE ( name, previous flags )
92
93@example spaceshift = CSPRITE( LOAD IMAGE( "spaceship.png" ) IGNORE COLOR GREEN )
94@example alien = CSPRITE( LOAD IMAGE( "alien1.png" ) )
95@example alien = CSPRITE( LOAD IMAGE( "alien2.png" ), alien )
96
97@seeAlso SPRITE
98@seeAlso MSPRITE
99
100@target c64
101@target c128
102@target c64reu
103</usermanual> */
104
105Variable * csprite_init( Environment * _environment, char * _image, char * _sprite, int _flags ) {
106
107 if ( _environment->deployed.msprite ) {
109 }
110
111 Variable * index;
112 Variable * startIndex;
113 Variable * result = variable_temporary( _environment, VT_SPRITE, "(sprite index)" );
114
115 Variable * spriteCount;
116
117 if ( _sprite ) {
118 outline0("; ---------------------------vvvvvvvv");
119 index = variable_retrieve_or_define( _environment, _sprite, VT_SPRITE, 0 );
120 startIndex = variable_temporary( _environment, VT_SPRITE, "(sprite index)" );
121 cpu_move_8bit( _environment, index->realName, startIndex->realName );
122 cpu_math_and_const_8bit( _environment, startIndex->realName, 0x0f );
123 spriteCount = variable_temporary( _environment, VT_SPRITE, "(sprite index)" );
124 cpu_move_8bit( _environment, index->realName, spriteCount->realName );
125 cpu_math_div2_const_8bit( _environment, spriteCount->realName, 4, 0, NULL );
126 cpu_math_and_const_8bit( _environment, spriteCount->realName, 0x0f );
127 cpu_math_sub_8bit( _environment, spriteCount->realName, startIndex->realName, spriteCount->realName );
128 cpu_move_8bit( _environment, startIndex->realName, index->realName );
129 cpu_move_8bit( _environment, startIndex->realName, spriteCount->realName );
130 outline0("; ---------------------------^^^^^^^^^^");
131 } else {
132 index = variable_temporary( _environment, VT_SPRITE, "(sprite index)" );
133 startIndex = variable_temporary( _environment, VT_SPRITE, "(sprite index)" );
134 spriteCount = variable_retrieve( _environment, "SPRITECOUNT" );
135 variable_move_naked( _environment, spriteCount->name, startIndex->name );
136 }
137
138 Variable * image = variable_retrieve( _environment, _image );
139
140 int colorTransparency = COLOR_BLACK;
141
142 if ( _flags & SPRITE_FLAG_TRANSPARENCY_COLOR ) {
143 colorTransparency = _flags & 0x000f;
144 }
145
146 for (int i=0; i<image->originalColors; ++i ) {
147 if ( image->originalPalette[i].index == colorTransparency ) continue;
148 variable_move_naked( _environment, spriteCount->name, index->name );
149 Variable * realImage = sprite_converter( _environment, image->originalBitmap, image->originalWidth, image->originalHeight, image->originalDepth, &image->originalPalette[i], _flags, 0, 0 );
150 vic2_sprite_data_from( _environment, index->name, realImage->name );
151 cpu_inc( _environment, spriteCount->realName );
152 cpu_inc( _environment, index->realName );
153 }
154
155 cpu_combine_nibbles( _environment, startIndex->realName, index->realName, result->realName );
156
157 if ( _flags & SPRITE_FLAG_MULTICOLOR) {
158 sprite_multicolor_var( _environment, result->name );
159 } else {
160 sprite_monocolor_var( _environment, result->name );
161 }
162
163 if ( _flags & SPRITE_FLAG_EXPAND_HORIZONTAL ) {
164 sprite_expand_horizontal_var( _environment, result->name );
165 } else {
166 sprite_compress_horizontal_var( _environment, result->name );
167 }
168
169 if ( _flags & SPRITE_FLAG_EXPAND_VERTICAL) {
170 sprite_expand_vertical_var( _environment, result->name );
171 } else {
172 sprite_compress_vertical_var( _environment, result->name );
173 }
174
175 return result;
176
177}
178
179#endif
void cpu_combine_nibbles(Environment *_environment, char *_low_nibble, char *_hi_nibble, char *_byte)
Definition 6309.c:3724
void cpu_math_sub_8bit(Environment *_environment, char *_source, char *_destination, char *_other)
CPU 6309: emit code to subtract two 8 bit values
Definition 6309.c:1049
void cpu_math_div2_const_8bit(Environment *_environment, char *_source, int _steps, int _signed, char *_remainder)
CPU 6309: emit code to halves for several times a 8 bit value
Definition 6309.c:1329
void cpu_inc(Environment *_environment, char *_variable)
Definition 6309.c:4555
void cpu_math_and_const_8bit(Environment *_environment, char *_source, int _mask)
CPU 6309: emit code to mask with "and" a value of 8 bit
Definition 6309.c:1451
void cpu_move_8bit(Environment *_environment, char *_source, char *_destination)
CPU 6309: emit code to move 8 bit
Definition 6309.c:743
#define COLOR_BLACK
Definition 6847.h:36
Variable * variable_retrieve(Environment *_environment, char *_name)
Variable * variable_retrieve_or_define(Environment *_environment, char *_name, VariableType _type, int _value)
Variable * variable_move_naked(Environment *_environment, char *_source, char *_destination)
Store the value of a variable inside another variable without conversion.
Variable * variable_temporary(Environment *_environment, VariableType _type, char *_meaning)
Define a temporary variable.
Variable * csprite_init(Environment *_environment, char *_image, char *_sprite, int _flags)
Emit code for SPRITE(...).
void sprite_compress_horizontal_var(Environment *_environment, char *_sprite)
Emit ASM code for SPRITE [int] COMPRESS HORIZONTAL.
void sprite_compress_vertical_var(Environment *_environment, char *_sprite)
Emit ASM code for SPRITE [expression] COMPRESS VERTICAL.
Variable * sprite_converter(Environment *_environment, char *_data, int _width, int _height, int _depth, RGBi *_color, int _flags, int _slot_x, int _slot_y)
void sprite_expand_horizontal_var(Environment *_environment, char *_sprite)
Emit ASM code for SPRITE [expression] EXPAND HORIZONTAL.
void sprite_expand_vertical_var(Environment *_environment, char *_sprite)
Emit ASM code for SPRITE [expression] EXPAND VERTICAL.
void sprite_monocolor_var(Environment *_environment, char *_sprite)
Emit ASM code for SPRITE [expression] MONOCOLOR.
void sprite_multicolor_var(Environment *_environment, char *_sprite)
Emit ASM code for SPRITE [expression] MULTICOLOR.
int msprite
Definition ugbc.h:1727
Deployed deployed
Definition ugbc.h:2921
unsigned char index
Definition ugbc.h:437
char * originalBitmap
Definition ugbc.h:1142
int originalHeight
Definition ugbc.h:1148
int originalDepth
Definition ugbc.h:1151
char * name
Definition ugbc.h:979
int originalColors
Definition ugbc.h:1154
int originalWidth
Definition ugbc.h:1145
RGBi originalPalette[MAX_PALETTE]
Definition ugbc.h:1169
char * realName
Definition ugbc.h:982
#define SPRITE_FLAG_TRANSPARENCY_COLOR
Definition ugbc.h:4565
#define SPRITE_FLAG_EXPAND_VERTICAL
Definition ugbc.h:4561
struct _Variable Variable
Structure of a single variable.
#define SPRITE_FLAG_MULTICOLOR
Definition ugbc.h:4559
struct _Environment Environment
Structure of compilation environment.
@ VT_SPRITE
Definition ugbc.h:501
#define CRITICAL_CANNOT_MIX_SPRITES_MSPRITES()
Definition ugbc.h:3739
#define outline0(s)
Definition ugbc.h:4252
#define SPRITE_FLAG_EXPAND_HORIZONTAL
Definition ugbc.h:4563
void vic2_sprite_data_from(Environment *_environment, char *_sprite, char *_image)
Definition vic2.c:1511