mirror of
https://codeberg.org/hkzlab/TK2048.git
synced 2025-12-26 05:22:16 +11:00
117 lines
2.6 KiB
C
117 lines
2.6 KiB
C
#include <stubs.h>
|
|
|
|
#include <stdint.h>
|
|
#include <string.h>
|
|
|
|
#include "utility.h"
|
|
#include "mem_registers.h"
|
|
#include "mem_map.h"
|
|
#include "input.h"
|
|
#include "game_logic.h"
|
|
#include "game_graphics.h"
|
|
#include "monitor_subroutines.h"
|
|
|
|
void init(void);
|
|
|
|
// Low level initialization
|
|
void init(void) {
|
|
POKE(P3_PWRDUP, 0); // Dirty the value checked by the reset vector
|
|
PEEK(IO_ROMSEL); // Make sure the ROM is selected
|
|
|
|
PEEK(IO_DISPLAY_BW); // Disable colors
|
|
PEEK(IO_DISPLAY_PAGE1); // Select the first display page
|
|
|
|
// Clear display memory
|
|
memset((void*)DISPLAY_PAGE_1, 0, DISPLAY_PAGE_SIZE);
|
|
memset((void*)DISPLAY_PAGE_2, 0, DISPLAY_PAGE_SIZE);
|
|
}
|
|
|
|
#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
|
|
#pragma require __loading_screen
|
|
|
|
__task int main(void) {
|
|
uint16_t moves_count;
|
|
uint16_t score;
|
|
uint8_t done;
|
|
|
|
init();
|
|
|
|
while(1){ // Outer loop
|
|
moves_count = 0;
|
|
|
|
score = reset_game();
|
|
|
|
// Draw the initial state of the game
|
|
draw_game_background();
|
|
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:
|
|
BELL1();
|
|
done = step_game(STEP_UP);
|
|
ddraw_direction_arrows(ARROW_UP);
|
|
break;
|
|
case K_DOWN:
|
|
BELL1();
|
|
done = step_game(STEP_DOWN);
|
|
ddraw_direction_arrows(ARROW_DOWN);
|
|
break;
|
|
case K_LEFT:
|
|
BELL1();
|
|
done = step_game(STEP_LEFT);
|
|
ddraw_direction_arrows(ARROW_LEFT);
|
|
break;
|
|
case K_RIGHT:
|
|
BELL1();
|
|
done = step_game(STEP_RIGHT);
|
|
ddraw_direction_arrows(ARROW_RIGHT);
|
|
break;
|
|
default:
|
|
continue; // Do nothing, loop again
|
|
}
|
|
|
|
// Increase the count of moves we made
|
|
moves_count++;
|
|
|
|
// If we have won, break out of this loop
|
|
if(done) break;
|
|
|
|
// Draw the number of moves
|
|
draw_number(moves_count, MOVES_TEXT_WIDTH, MOVES_TEXT_X, MOVES_TEXT_Y);
|
|
|
|
// Draw the moved tiles
|
|
draw_tiles();
|
|
|
|
// Unable to add a tile: we ran out of space and lost!!!
|
|
uint8_t random_tile_off = add_random_tile();
|
|
if(!random_tile_off) 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);
|
|
}
|
|
|
|
};
|
|
|
|
return 0;
|
|
}
|