Initial creation of demo module

This commit is contained in:
hkz 2025-09-06 08:31:11 +02:00
commit 22790bdc44
3 changed files with 182 additions and 3 deletions

165
src/demo_main.c Normal file
View file

@ -0,0 +1,165 @@
#include <stubs.h>
#include <string.h>
#include <calypsi/intrinsics6502.h>
#include "monitor_subroutines.h"
#include "utility.h"
#include "mem_map.h"
#include "shared_page.h"
#include "state_page.h"
#include "dlog_data.h"
#include "game_data.h"
#include "input.h"
#include "game_logic.h"
#include "game_graphics.h"
#include "monitor_subroutines.h"
#include "sound.h"
// External initialization requirements
#pragma require __preserve_zp
#pragma require __data_initialization_needed
#define MOVES_TEXT_X 32
#define MOVES_TEXT_Y 61
#define MOVES_TEXT_WIDTH 5
#define SCORE_TEXT_X 32
#define SCORE_TEXT_Y 29
#define SCORE_TEXT_WIDTH 5
#define HIGH_TEXT_X 32
#define HIGH_TEXT_Y 107
#define WIN_SCORE_BONUS 10000
static state_page_data* state_page = (state_page_data*)STATE_PAGE;
static shared_page_data *shared_page = (shared_page_data*)SHARED_PAGE;
void main(void) {
uint16_t moves_count = 0;
uint16_t score = 0;
int8_t done = 0;
// By default, once we return from this, return to the DLOG module and give the master no command to execute
shared_page->master_command = MASTER_COMMAND_NONE;
shared_page->next_module_idx = 3; // Go to the DLOG module
dlog_data *dld = (dlog_data *)(shared_page->module_data);
dlog_data *gad = (dlog_data *)(shared_page->module_data);
// Make sure the buffers are pointing to the correct memory and are clear
clear_display_buffers();
// Reset the game, calculate the initial score depending on which tiles we randomly get
score = reset_game();
// Load the game
if(gad->mode == GAME_MODE_LOAD) {
gad->mode = GAME_MODE_NORMAL;
memcpy(get_front_grid(), (void*)(state_page->save_grid), GRID_SIDE * GRID_SIDE);
moves_count = state_page->saved_moves_count;
score = calculate_score();
// We loaded an empty save, just restart the game
if (!score) score = reset_game();
}
// Draw the initial state of the game
draw_game_background(state_page->hi_score);
draw_number(moves_count, MOVES_TEXT_WIDTH, MOVES_TEXT_X, MOVES_TEXT_Y);
draw_number(score, SCORE_TEXT_WIDTH, SCORE_TEXT_X, SCORE_TEXT_Y);
draw_tiles();
// Swap graphical buffers
swap_display_buffers();
while(1) { // Game loop
lfsr_update();
switch(read_kb()) {
case K_UP:
SND_TAP();
done = step_game(GAME_STEP_UP);
ddraw_direction_arrows(GRAPH_ARROW_UP);
break;
case K_DOWN:
SND_TAP();
done = step_game(GAME_STEP_DOWN);
ddraw_direction_arrows(GRAPH_ARROW_DOWN);
break;
case K_LEFT:
SND_TAP();
done = step_game(GAME_STEP_LEFT);
ddraw_direction_arrows(GRAPH_ARROW_LEFT);
break;
case K_RIGHT:
SND_TAP();
done = step_game(GAME_STEP_RIGHT);
ddraw_direction_arrows(GRAPH_ARROW_RIGHT);
break;
case K_CTRL_R:
snd_mod_button();
score = 0; // We'll reset the score
done = -1;
break;
case K_CTRL_S: // The following two will return early
snd_mod_button();
memcpy((void*)(state_page->save_grid), get_front_grid(), GRID_SIDE * GRID_SIDE);
state_page->saved_moves_count = moves_count;
shared_page->master_command = MASTER_COMMAND_SAVE;
case K_CTRL_L:
snd_mod_button();
sync_display1_buffer();
shared_page->next_module_idx = 4;
gad->mode = GAME_MODE_LOAD;
return;
default:
continue; // Do nothing, loop again
}
// Increase the count of moves we made (unless we lost or reset the game)
if(done >= 0) moves_count++;
// Draw the number of moves
draw_number(moves_count, MOVES_TEXT_WIDTH, MOVES_TEXT_X, MOVES_TEXT_Y);
// Draw the moved tiles
draw_tiles();
// If we have won, or we got a reset request, break out of this loop
if(done) {
score += (done > 0) ? WIN_SCORE_BONUS : 0;
draw_number(score, SCORE_TEXT_WIDTH, SCORE_TEXT_X, SCORE_TEXT_Y);
swap_display_buffers(); // Make sure we show the latest changes
break;
}
// Unable to add a tile: we ran out of space and lost!!!
uint8_t random_tile_off = add_random_tile();
if(!random_tile_off) {
done = -1; // Lost the game
break;
}
score = calculate_score();
// Draw the score
draw_number(score, SCORE_TEXT_WIDTH, SCORE_TEXT_X, SCORE_TEXT_Y);
swap_display_buffers();
// Draw the new tile directly on the front buffer, this way we make it appear with an "animation"
ddraw_single_tile(random_tile_off - 1);
}
// Sync the display buffers
sync_display1_buffer();
dld->mode = (done > 0) ? DLOG_MODE_WIN : DLOG_MODE_LOSE;
dld->score = score;
return;
}

View file

@ -24,7 +24,7 @@
static uint8_t *module_page = (uint8_t*)MODULE_PAGE;
static shared_page_data * shared_page = (shared_page_data*)SHARED_PAGE;
#define FILE_LIST_LEN 5
#define FILE_LIST_LEN 6
#define FNAME_LEN 6
#define STATE_FILE_IDX 1
@ -37,6 +37,7 @@ static const uint8_t file_table[FILE_LIST_LEN][FNAME_LEN] = {
{ 0x80 | 'I', 0x80 | 'N', 0x80 | 'T', 0x80 | 'R', 0x80 | 'O', 0x00}, // INTRO (this executable will show the initial presentation picture)
{ 0x80 | 'D', 0x80 | 'L', 0x80 | 'O', 0x80 | 'G', 0xA0, 0x00}, // DLOG (startup, win, lose dialogs)
{ 0x80 | 'G', 0x80 | 'A', 0x80 | 'M', 0x80 | 'E', 0xA0, 0x00}, // GAME (the actual game)
{ 0x80 | 'D', 0x80 | 'E', 0x80 | 'M', 0x80 | 'O', 0xA0, 0x00}, // DEMO (automatic demo)
};
static uint8_t file_trksec[FILE_LIST_LEN][2]; // This will hold track/sector for initial ts list sector for every one of the listed files. Populated at startup.
@ -46,6 +47,7 @@ static uint16_t file_load_address[FILE_LIST_LEN] = { // This will hold the load
MODULE_PAGE,
MODULE_PAGE,
MODULE_PAGE,
MODULE_PAGE,
};
static void init(void);