ugBASIC 1.18
An isomorphic BASIC language compiler for retrocomputers
Loading...
Searching...
No Matches
create_path.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
50/* <usermanual>
51@keyword CREATE PATH
52
53@english
54
55The ''CREATE PATH'' command is designed to define a straight path between two specified points.
56Instead of immediately drawing the line, this command is intended to prepare an internal
57"data structure" that contains the information needed to "travel" this path, point by point,
58later, using the ''TRAVEL'' command. It therefore separates the logic of "where to go" from the
59logic of "how to get there", making the code more modular and readable.
60
61The main goal is to separate the definition of the path from its actual graphical
62implementation. This approach can be useful in several situations: you can define paths
63once, and then have an object move along that path in an animated way, you can update the
64position of an object at regular intervals using ''TRAVEL''.
65
66@italian
67
68Il comando ''CREATE PATH'' è progettato per definire un percorso rettilineo tra due punti
69specificati. Invece di disegnare immediatamente la linea, questo comando ha lo scopo di
70preparare una "struttura dati" interna che contiene le informazioni necessarie per
71"percorrere" questo tragitto, punto per punto, in un secondo momento, utilizzando il
72comando ''TRAVEL''. Separa quindi la logica del "dove andare" dalla logica del "come
73arrivare", rendendo il codice più modulare e leggibile.
74
75L'obiettivo principale è scindere la definizione del percorso dalla sua effettiva
76realizzazione grafica. Questo approccio può essere utile in diverse situazioni: si
77possono definire percorsi una sola volta, e poi far muovere un oggetto lungo quel
78percorso in modo animato, si può aggiornare la posizione di un oggetto a intervalli
79regolari utilizzando ''TRAVEL''.
80
81@syntax = CREATE PATH( x0, y0 TO x1, y1 )
82@syntax = CREATE PATH( x0, y0, x1, y1 )
83
84@example DIM p AS PATH
85@example p = CREATE PATH( 0, 0 TO 100, 100 )
86@example p = CREATE PATH( 0, 0, 100, 100 )
87
88</usermanual> */
89
90Variable * create_path( Environment * _environment, char * _x0, char * _y0, char * _x1, char * _y1 ) {
91
93
94 Variable * path = variable_define( _environment, "createpath__path", VT_PATH, 0 );
95 path->bankReadOrWrite = 1;
96
97 Variable * x0 = variable_define( _environment, "createpath__x0", VT_POSITION, 0 );
98 Variable * y0 = variable_define( _environment, "createpath__y0", VT_POSITION, 0 );
99 Variable * x1 = variable_define( _environment, "createpath__x1", VT_POSITION, 0 );
100 Variable * y1 = variable_define( _environment, "createpath__y1", VT_POSITION, 0 );
101
102 Variable * dx = variable_temporary( _environment, VT_POSITION, "(dx2)");
103 Variable * dy = variable_temporary( _environment, VT_POSITION, "(dy2)");
104 Variable * dx2 = variable_temporary( _environment, VT_POSITION, "(dx2)");
105 Variable * dy2 = variable_temporary( _environment, VT_POSITION, "(dy2)");
106 Variable * stepx = variable_temporary( _environment, VT_SBYTE, "(stepx)");
107 Variable * stepy = variable_temporary( _environment, VT_SBYTE, "(stepy)");
108 Variable * fraction = variable_temporary( _environment, VT_POSITION, "(fraction)" );
109
110 variable_move( _environment, variable_sub( _environment, x1->name, x0->name )->name, dx->name );
111
112 variable_move( _environment, variable_sub( _environment, y1->name, y0->name )->name, dy->name );
113
114 if_then( _environment, variable_less_than_const( _environment, dy->name, 0, 0 )->name );
115 variable_move( _environment, variable_complement_const( _environment, dy->name, 0 )->name, dy->name );
116 variable_store( _environment, stepy->name, -1 );
117 else_if_then_label( _environment );
118 else_if_then( _environment, NULL );
119 variable_store( _environment, stepy->name, 1 );
120 end_if_then( _environment );
121
122 if_then( _environment, variable_less_than_const( _environment, dx->name, 0, 0 )->name );
123 variable_move( _environment, variable_complement_const( _environment, dx->name, 0 )->name, dx->name );
124 variable_store( _environment, stepx->name, -1 );
125 else_if_then_label( _environment );
126 else_if_then( _environment, NULL );
127 variable_store( _environment, stepx->name, 1 );
128 end_if_then( _environment );
129
130 variable_move_naked( _environment, dy->name, dy2->name );
131 variable_move_naked( _environment, variable_sl_const( _environment, dy2->name, 1 )->name, dy2->name );
132 variable_move_naked( _environment, dx->name, dx2->name );
133 variable_move_naked( _environment, variable_sl_const( _environment, dx2->name, 1 )->name, dx2->name );;
134
135 if_then( _environment, variable_greater_than( _environment, dx2->name, dy2->name, 0 )->name );
136 variable_move( _environment, variable_sub( _environment, dy2->name, dx->name)->name, fraction->name);
137 else_if_then_label( _environment );
138 else_if_then( _environment, NULL );
139 variable_move( _environment, variable_sub( _environment, dx2->name, dy->name)->name, fraction->name);
140 end_if_then( _environment );
141
142 cpu_move_16bit( _environment, x0->realName, path->realName );
143 cpu_move_16bit( _environment, y0->realName, address_displacement( _environment, path->realName, "2" ) );
144 cpu_move_16bit( _environment, dx2->realName, address_displacement( _environment, path->realName, "4" ) );
145 cpu_move_16bit( _environment, dy2->realName, address_displacement( _environment, path->realName, "6" ) );
146 cpu_move_8bit( _environment, stepx->realName, address_displacement( _environment, path->realName, "8" ) );
147 cpu_move_8bit( _environment, stepy->realName, address_displacement( _environment, path->realName, "9" ) );
148 cpu_move_16bit( _environment, fraction->realName, address_displacement( _environment, path->realName, "10" ) );
149 cpu_move_16bit( _environment, x1->realName, address_displacement( _environment, path->realName, "12" ) );
150 cpu_move_16bit( _environment, y1->realName, address_displacement( _environment, path->realName, "14" ) );
151
152 cpu_return( _environment );
153
155
156 Variable * x0p = variable_retrieve_or_define( _environment, _x0, VT_POSITION, 0 );
157 Variable * y0p = variable_retrieve_or_define( _environment, _y0, VT_POSITION, 0 );
158 Variable * x1p = variable_retrieve_or_define( _environment, _x1, VT_POSITION, 0 );
159 Variable * y1p = variable_retrieve_or_define( _environment, _y1, VT_POSITION, 0 );
160
161 Variable * path = variable_retrieve( _environment, "createpath__path" );
162
163 Variable * x0 = variable_retrieve( _environment, "createpath__x0" );
164 Variable * y0 = variable_retrieve( _environment, "createpath__y0" );
165 Variable * x1 = variable_retrieve( _environment, "createpath__x1" );
166 Variable * y1 = variable_retrieve( _environment, "createpath__y1" );
167
168 variable_move( _environment, x0p->name, x0->name );
169 variable_move( _environment, y0p->name, y0->name );
170 variable_move( _environment, x1p->name, x1->name );
171 variable_move( _environment, y1p->name, y1->name );
172
173 cpu_call( _environment, "lib_create_path" );
174
175 return path;
176
177}
178
void cpu_move_16bit(Environment *_environment, char *_source, char *_destination)
CPU 6309: emit code to move 16 bit
Definition 6309.c:1474
void cpu_call(Environment *_environment, char *_label)
Definition 6309.c:3755
void cpu_return(Environment *_environment)
Definition 6309.c:4030
void cpu_move_8bit(Environment *_environment, char *_source, char *_destination)
CPU 6309: emit code to move 8 bit
Definition 6309.c:743
Variable * variable_retrieve(Environment *_environment, char *_name)
Variable * variable_less_than_const(Environment *_environment, char *_source, int _destination, int _equal)
Variable * variable_retrieve_or_define(Environment *_environment, char *_name, VariableType _type, int _value)
Variable * variable_greater_than(Environment *_environment, char *_source, char *_destination, int _equal)
Compare two variable and return the result of comparation.
Variable * variable_move(Environment *_environment, char *_source, char *_destination)
Store the value of a variable inside another variable by converting it.
Variable * variable_move_naked(Environment *_environment, char *_source, char *_destination)
Store the value of a variable inside another variable without conversion.
Variable * variable_define(Environment *_environment, char *_name, VariableType _type, int _value)
Define a variable for the program.
Variable * variable_sl_const(Environment *_environment, char *_destination, int _steps)
Variable * variable_sub(Environment *_environment, char *_source, char *_dest)
Make a differenze between two variable and return the difference of them.
Variable * variable_temporary(Environment *_environment, VariableType _type, char *_meaning)
Define a temporary variable.
Variable * variable_store(Environment *_environment, char *_destination, unsigned int _value)
Store a direct value to a variable.
Variable * variable_complement_const(Environment *_environment, char *_source, int _value)
Calculate the complement of a variable.
char * address_displacement(Environment *_environment, char *_address, char *_displacement)
Variable * create_path(Environment *_environment, char *_x0, char *_y0, char *_x1, char *_y1)
Emit ASM code to implement CREATE PATH command.
Definition create_path.c:90
void else_if_then_label(Environment *_environment)
Emit ASM code for ... ELSE [IF] ....
void else_if_then(Environment *_environment, char *_expression)
Emit ASM code for ... ELSE [IF] ....
void end_if_then(Environment *_environment)
Emit ASM code for ENDIF.
Definition end_if_then.c:50
void if_then(Environment *_environment, char *_expression)
Emit ASM code for IF ... THEN ....
Definition if_then.c:123
int bankReadOrWrite
Definition ugbc.h:1181
char * name
Definition ugbc.h:979
char * realName
Definition ugbc.h:982
#define deploy_end(s)
Definition ugbc.h:4365
struct _Variable Variable
Structure of a single variable.
struct _Environment Environment
Structure of compilation environment.
@ VT_POSITION
Definition ugbc.h:468
@ VT_SBYTE
Definition ugbc.h:452
@ VT_PATH
Definition ugbc.h:540
#define deploy_begin(s)
Definition ugbc.h:4356