gbdk-2020 | GameBoy Development Kit |
| download: https://git.y1.nz/archives/gbdk.tar.gz | |
| README | Files | Log | Refs | LICENSE |
gbdk-lib/examples/gb/hblank_copy/src/main.c
1 #include <gbdk/platform.h>
2 #include <gb/hblankcpy.h>
3
4 #include <stdint.h>
5 #include <stdbool.h>
6
7 #include "data.h"
8
9 // X and Y posisions are centered
10 #define POSITION_X ((DEVICE_SCREEN_WIDTH - MAP_WIDTH) >> 1)
11 #define POSITION_Y ((DEVICE_SCREEN_HEIGHT - MAP_HEIGHT) >> 1)
12
13 const frame_desc_t * current_frame = frames; // pointer to the current animation frame
14
15 void LCD_ISR(void) {
16 static bool odd_even_frame = false;
17 uint8_t _save = CURRENT_BANK;
18 SWITCH_ROM(current_frame->bank);
19
20 if (odd_even_frame = !odd_even_frame) {
21 LCDC_REG &= ~LCDCF_BG8000;
22 hblank_copy_destination = _VRAM8000;
23 hblank_copy_vram(current_frame->tiles, MAP_WIDTH * MAP_HEIGHT);
24 } else {
25 LCDC_REG |= LCDCF_BG8000;
26 hblank_copy_destination = _VRAM9000;
27 hblank_copy_vram(current_frame->tiles, MAP_WIDTH * MAP_HEIGHT);
28 }
29 SWITCH_ROM(_save);
30 }
31
32 uint8_t joy = 0, old_joy;
33 inline void PROCESS_INPUT(void) {
34 old_joy = joy, joy = joypad();
35 }
36 inline uint8_t KEY_PRESSED(uint8_t key) {
37 return ((joy & ~old_joy) & key);
38 }
39
40 bool animation = true; // animation enabled
41 bool animation_direction = true; // animate forward or back
42 uint8_t animation_speed = 1, animation_counter = 0; // animation speed and counter
43
44 void main(void) {
45 DISPLAY_OFF;
46
47 CRITICAL {
48 LYC_REG = 0, STAT_REG |= STATF_LYC;
49 add_LCD(LCD_ISR);
50 }
51 set_interrupts(IE_REG | LCD_IFLAG);
52
53 // set up palette
54 BGP_REG = DMG_PALETTE(DMG_WHITE, DMG_LITE_GRAY, DMG_DARK_GRAY, DMG_BLACK);
55
56 // clear screen and set tile map
57 fill_bkg_rect(0, 0, 32, 32, 0);
58 uint8_t v = 0;
59 for (uint8_t y = POSITION_Y; y != POSITION_Y + MAP_HEIGHT; y++) {
60 uint8_t * addr = set_bkg_tile_xy(POSITION_X, y, v++);
61 for (uint8_t x = 0; x != MAP_WIDTH - 1; x++) set_vram_byte(++addr, v++);
62 }
63
64 SHOW_BKG;
65 DISPLAY_ON;
66
67 bool step_animation = false;
68
69 while (true) {
70 vsync();
71 PROCESS_INPUT();
72 // stepping through animation frames with LEFT/RIGHT
73 if (joy & J_LEFT) {
74 animation = false;
75 animation_direction = false;
76 step_animation = true;
77 } else if (joy & J_RIGHT) {
78 animation = false;
79 animation_direction = true;
80 step_animation = true;
81 }
82 // change the animation speed with UP/DOWN
83 if (KEY_PRESSED(J_UP)) {
84 if (animation_speed) animation_speed--;
85 } else if (KEY_PRESSED(J_DOWN)) {
86 if (++animation_speed > 10) animation_speed = 10;
87 }
88 // start/stop animation with START
89 if (KEY_PRESSED(J_START)) animation = !animation;
90
91 // process animation
92 if ((step_animation) || ((animation) && (++animation_counter > animation_speed))) {
93 animation_counter = 0;
94 if (animation_direction) CRITICAL {
95 if (++current_frame == (frames + ANIMATION_FRAME_COUNT)) current_frame = frames;
96 } else CRITICAL {
97 if (--current_frame < frames) current_frame = frames + (ANIMATION_FRAME_COUNT - 1);
98 }
99 step_animation = false;
100 }
101 }
102 }
This webpage is intended to be an accessible preview of this repository. To get a fuller picture, clone it and use the git CLI.