ugBASIC 1.18
An isomorphic BASIC language compiler for retrocomputers
Loading...
Searching...
No Matches
center.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
47/* <usermanual>
48@keyword CENTRE
49
50@english
51
52@italian
53
54@syntax CENTRE text [;]
55
56@example CENTRE "HELLO!"
57@example CENTRE "HELLO!";
58
59@usedInExample texts_position_07.bas
60@usedInExample contrib_joystick.bas
61@usedInExample contrib_What_a_wonderful_world.bas
62@usedInExample screens_resolution_02.bas
63
64@target all
65
66@alias CENTER
67
68@verified
69</usermanual> */
70/* <usermanual>
71@keyword CENTER
72
73@english
74
75The ''CENTER'' command aligns a text string in the center of a line. In other words,
76it allows you to position a word or phrase so that it occupies the available space
77in a symmetrical manner. The ''CENTER'' command calculates the white space needed
78on the sides of the string to center it within the specified width and
79adds the necessary spaces. If the statement is followed by a semicolon, the cursor
80will remain on the same line. Otherwise, it will move to the next line.
81
82This command can be used to create aesthetically pleasing section or chapter titles,
83to center menu options in the center of the console and to format data neatly
84in a report. Centered text is easier to read and makes programs look neater.
85
86By using CENTER, you can ensure that text is aligned uniformly in different
87parts of the program, and on different targets. Moreover, reduce the need
88to manually calculate character positions.
89
90@italian
91
92Il comando ''CENTER'' allinea una stringa di testo al centro di una riga.
93In altre parole, consente di posizionare una parola o una frase in modo
94che occupi lo spazio disponibile in modo simmetrico. Il comando ''CENTER''
95calcola lo spazio bianco necessario ai lati della stringa per centrarla entro
96la larghezza specificata e aggiunge gli spazi necessari. Se l'istruzione è
97seguita da un punto e virgola, il cursore rimarrà sulla stessa riga. Altrimenti,
98si sposterà alla riga successiva.
99
100Questo comando può essere utilizzato per creare titoli di sezioni o capitoli
101esteticamente gradevoli, per centrare le opzioni di menu al centro della console
102e per formattare i dati in modo ordinato in un report. Il testo centrato è più
103facile da leggere e rende i programmi più ordinati.
104
105Utilizzando CENTER, è possibile garantire che il testo sia allineato uniformemente
106in diverse parti del programma e su diversi target. Inoltre, si riduce la necessità
107di calcolare manualmente le posizioni dei caratteri.
108
109@syntax CENTER text [;]
110
111@example CENTER "HELLO!"
112@example CENTER "HELLO!";
113
114@usedInExample texts_position_07.bas
115@usedInExample contrib_joystick.bas
116@usedInExample contrib_What_a_wonderful_world.bas
117@usedInExample screens_resolution_02.bas
118
119@target all
120
121@alias CENTRE
122</usermanual> */
123
124void center( Environment * _environment, char * _string, int _newline, char * _width ) {
125
127
129
130 Variable * string = variable_define( _environment, "center__string", VT_DSTRING, 0 );
131 Variable * currentWidth = variable_define( _environment, "center__currentWidth", VT_BYTE, 0 );
132 Variable * newLine = variable_define( _environment, "center__newLine", VT_BYTE, 0 );
133
134 Variable * len = variable_string_len( _environment, string->name );
135 Variable * result = variable_temporary( _environment, VT_BYTE, "(compare)");
136 Variable * zero = variable_temporary( _environment, VT_BYTE, "(zero)");
137
138 cpu_store_8bit( _environment, zero->realName, 0 );
139
140 cpu_greater_than_8bit( _environment, len->realName, currentWidth->realName, result->realName, 1, 0 );
141
142 char nothingLabel[MAX_TEMPORARY_STORAGE]; sprintf( nothingLabel, "%snothing", label );
143 char newLineLabel[MAX_TEMPORARY_STORAGE]; sprintf( newLineLabel, "%snewline", label );
144 char doneLabel[MAX_TEMPORARY_STORAGE]; sprintf( doneLabel, "%sdone", label );
145
146 cpu_bvneq( _environment, result->realName, nothingLabel );
147
148 Variable * w = variable_sub( _environment, currentWidth->name, len->name );
149 w = variable_sr_const( _environment, w->name, 1 );
150
151 locate( _environment, w->name, NULL );
152
153 cpu_jump( _environment, doneLabel );
154
155 cpu_label( _environment, nothingLabel );
156
157 locate( _environment, zero->name, NULL );
158
159 cpu_label( _environment, doneLabel );
160
161 print( _environment, string->name, 0, _environment->printRaw );
162
163 cpu_bveq( _environment, newLine->realName, newLineLabel );
164
165 text_newline( _environment );
166
167 cpu_label( _environment, newLineLabel );
168
169 cpu_return( _environment );
170
172
173 Variable * string = variable_retrieve( _environment, "center__string" );
174 Variable * stringParameter = variable_retrieve( _environment, _string );
175 Variable * currentWidthParameter;
176 if ( _width ) {
177 currentWidthParameter = variable_retrieve( _environment, _width);
178 } else {
179 currentWidthParameter = variable_retrieve( _environment, "CONSOLEW");
180 }
181 Variable * currentWidth = variable_retrieve( _environment, "center__currentWidth" );
182 Variable * newLine = variable_retrieve( _environment, "center__newLine" );
183
184 variable_move( _environment, stringParameter->name, string->name );
185 variable_move( _environment, currentWidthParameter->name, currentWidth->name );
186 variable_store( _environment, newLine->name, ( _newline && ( _environment->centerWithoutNewLine == 0 ) ) ? 1 : 0 );
187
188 cpu_call( _environment, "lib_center");
189
190
191}
void cpu_bveq(Environment *_environment, char *_value, char *_label)
Definition 6309.c:334
void cpu_greater_than_8bit(Environment *_environment, char *_source, char *_destination, char *_other, int _equal, int _signed)
CPU 6309: emit code to compare two 8 bit values
Definition 6309.c:989
void cpu_label(Environment *_environment, char *_label)
Definition 6309.c:356
void cpu_call(Environment *_environment, char *_label)
Definition 6309.c:3755
void cpu_store_8bit(Environment *_environment, char *_destination, int _value)
CPU 6309: emit code to store 8 bit
Definition 6309.c:761
void cpu_jump(Environment *_environment, char *_label)
Definition 6309.c:3739
void cpu_bvneq(Environment *_environment, char *_value, char *_label)
Definition 6309.c:345
void cpu_return(Environment *_environment)
Definition 6309.c:4030
Variable * variable_retrieve(Environment *_environment, char *_name)
Variable * variable_sr_const(Environment *_environment, char *_destination, int _bits)
Variable * variable_move(Environment *_environment, char *_source, char *_destination)
Store the value of a variable inside another variable by converting it.
Variable * variable_define(Environment *_environment, char *_name, VariableType _type, int _value)
Define a variable for the program.
Variable * variable_sub(Environment *_environment, char *_source, char *_dest)
Make a differenze between two variable and return the difference of them.
Variable * variable_string_len(Environment *_environment, char *_string)
Emit code for = LEN( ... ).
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.
void center(Environment *_environment, char *_string, int _newline, char *_width)
Emit code for CENTRE ....
Definition center.c:124
void locate(Environment *_environment, char *_x, char *_y)
Emit code for LOCATE ...,....
Definition locate.c:110
void print(Environment *_environment, char *_value, int _new_line, int _raw)
Emit code for PRINT... instruction.
Definition print.c:141
int centerWithoutNewLine
Definition ugbc.h:2968
int printRaw
Definition ugbc.h:3274
char * name
Definition ugbc.h:979
char * realName
Definition ugbc.h:982
void text_newline(Environment *_environment)
Definition text.c:51
#define deploy_end(s)
Definition ugbc.h:4365
#define MAX_TEMPORARY_STORAGE
Definition ugbc.h:563
struct _Variable Variable
Structure of a single variable.
struct _Environment Environment
Structure of compilation environment.
@ VT_BYTE
Definition ugbc.h:450
@ VT_DSTRING
Definition ugbc.h:483
#define deploy_begin(s)
Definition ugbc.h:4356
#define MAKE_LABEL
Definition ugbc.h:3351