ugBASIC 1.18
An isomorphic BASIC language compiler for retrocomputers
Loading...
Searching...
No Matches
begin_gameloop.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
52/* <usermanual>
53@keyword BEGIN GAMELOOP
54
55@english
56
57The command ''BEGIN GAMELOOP'' define the starting point of the game loop.
58A game loop is a fundamental concept in video game programming, especially
59in simple video games like those for 8-bit computers. Think of the game
60loop as the heartbeat of a video game: it is a continuous cycle of actions
61that are constantly repeated, giving life to the gaming experience.
62
63In simple terms, the game loop is an infinite loop in the game code that
64takes care of: updating the game state, checking user input
65(button presses, joystick movement), updating the position of objects
66on the screen, calculating collisions, managing enemy AI, and so on.
67
68The game loop is the place where drawing everything that needs
69to be displayed on the screen, based on the current state of the
70game. In some platforms, it will implictly "sync" the activity with the
71vertical blank, so the action inside the loop should be executed
72in a single "frame" of game.
73
74On an 8-bit computer, with limited resources, the game loop was often implemented
75in a very simple way, as an infinite loop. The ''BEGIN GAMELOOP''...''END GAMELOOP''
76instructions create an infinite loop, so that the program would continue to
77execute the same instructions over and over again. Inside the loop, instructions
78were executed to update the game state,
79such as checking if a button had been pressed or if an enemy had moved.
80After updating the state, a routine was called to draw everything
81on the screen. The program would return to the beginning of the loop, ready
82for the next iteration.
83
84The game loop makes the game interactive, allowing the player to hit what
85is happening on the screen, continuously updating the state and redrawing
86the screen creates the illusion of movement and animation. A well-designed
87game loop ensures a smooth and responsive gameplay experience.
88
89@italian
90
91Il comando ''BEGIN GAMELOOP'' definisce il punto di partenza del game loop.
92Un game loop è un concetto fondamentale nella programmazione dei videogiochi,
93specialmente in videogiochi semplici come quelli per computer a 8 bit.
94Si pensi al game loop come al battito cardiaco di un videogioco: è un
95ciclo continuo di azioni che si ripetono costantemente, dando vita
96all'esperienza di gioco.
97
98In parole povere, il game loop è un loop infinito nel codice di gioco
99che si occupa di: aggiornare lo stato del gioco, controllare l'input
100dell'utente (pressione dei pulsanti, movimento del joystick), aggiornare
101la posizione degli oggetti sullo schermo, calcolare le collisioni,
102gestire l'IA nemica e così via.
103
104Il game loop è il luogo in cui disegnare tutto ciò che deve essere
105visualizzato sullo schermo, in base allo stato attuale del gioco.
106In alcune piattaforme, "sincronizza" implicitamente l'attività con
107il vertical blank, quindi l'azione all'interno del loop dovrebbe
108essere eseguita in un singolo "frame" di gioco.
109
110Su un computer a 8 bit, con risorse limitate, il game loop veniva
111spesso implementato in modo molto semplice, come un loop infinito.
112Le istruzioni ''BEGIN GAMELOOP''...''END GAMELOOP'' creano un loop
113infinito, in modo che il programma continui a eseguire le stesse
114istruzioni più e più volte. All'interno del loop, venivano eseguite
115istruzioni per aggiornare lo stato del gioco, come controllare
116se è stato premuto un pulsante o se un nemico si è mosso.
117
118Dopo aver aggiornato lo stato, viene chiamata una routine per disegnare
119tutto sullo schermo. Il programma torna all'inizio del loop, pronto
120per l'iterazione successiva.
121
122Il loop di gioco rende il gioco interattivo, consentendo al giocatore di
123cogliere ciò che sta accadendo sullo schermo, aggiornando continuamente
124lo stato e ridisegnando lo schermo crea l'illusione di movimento e animazione.
125Un loop di gioco ben progettato garantisce un'esperienza di gioco fluida e reattiva.
126
127@syntax BEGIN GAMELOOP
128
129@example BEGIN GAMELOOP
130
131@target all
132</usermanual> */
133void begin_gameloop( Environment * _environment ) {
134
136
137 Loop * loop = malloc( sizeof( Loop ) );
138 memset( loop, 0, sizeof( Loop ) );
139 loop->label = strdup( label );
140 loop->type = LT_GAMELOOP;
141 loop->next = _environment->loops;
142 _environment->loops = loop;
143
144 cpu_label( _environment, loop->label );
145
146 _environment->hasGameLoop = 1;
147
148}
void cpu_label(Environment *_environment, char *_label)
Definition 6309.c:356
void begin_gameloop(Environment *_environment)
Emit ASM code for BEGIN GAMELOOP.
int hasGameLoop
Definition ugbc.h:2646
Loop * loops
Definition ugbc.h:2669
void * malloc(YYSIZE_T)
void loop(Environment *_environment, char *_label)
struct _Environment Environment
Structure of compilation environment.
@ LT_GAMELOOP
Definition ugbc.h:1402
struct _Loop Loop
Structure of a single loop.
#define MAKE_LABEL
Definition ugbc.h:3351