gbdk-2020 | GameBoy Development Kit |
| download: https://git.y1.nz/archives/gbdk.tar.gz | |
| README | Files | Log | Refs | LICENSE |
gbdk-lib/examples/cross-platform/joytest/src/joytest.c
1 #include <gbdk/platform.h>
2
3 #include <stdint.h>
4 #include <stdbool.h>
5
6 extern const uint8_t sprite_data[64];
7 extern const uint8_t tiles_data[1488];
8
9 extern const uint8_t normal_buttons[8][4];
10 extern const uint8_t pressed_buttons[8][4];
11 extern const uint8_t disabled_buttons[8][4];
12
13 void draw_buttons(uint8_t state, uint8_t x, uint8_t y) {
14 set_bkg_based_tiles(x, y, 2, 2, (state & J_UP) ? pressed_buttons[0] : normal_buttons[0], 1);
15 set_bkg_based_tiles(x + 2, y, 2, 2, (state & J_DOWN) ? pressed_buttons[1] : normal_buttons[1], 1);
16 set_bkg_based_tiles(x + 4, y, 2, 2, (state & J_LEFT) ? pressed_buttons[2] : normal_buttons[2], 1);
17 set_bkg_based_tiles(x + 6, y, 2, 2, (state & J_RIGHT) ? pressed_buttons[3] : normal_buttons[3], 1);
18 set_bkg_based_tiles(x + 8, y, 2, 2, (state & J_A) ? pressed_buttons[4] : normal_buttons[4], 1);
19 set_bkg_based_tiles(x + 10, y, 2, 2, (state & J_B) ? pressed_buttons[5] : normal_buttons[5], 1);
20 set_bkg_based_tiles(x + 12, y, 2, 2, (state & J_START) ? pressed_buttons[6] : normal_buttons[6], 1);
21 set_bkg_based_tiles(x + 14, y, 2, 2, (state & J_SELECT) ? pressed_buttons[7] : normal_buttons[7], 1);
22 }
23
24 void draw_disabled(uint8_t x, uint8_t y) {
25 set_bkg_based_tiles(x, y, 2, 2, disabled_buttons[0], 1);
26 set_bkg_based_tiles(x + 2, y, 2, 2, disabled_buttons[1], 1);
27 set_bkg_based_tiles(x + 4, y, 2, 2, disabled_buttons[2], 1);
28 set_bkg_based_tiles(x + 6, y, 2, 2, disabled_buttons[3], 1);
29 set_bkg_based_tiles(x + 8, y, 2, 2, disabled_buttons[4], 1);
30 set_bkg_based_tiles(x + 10, y, 2, 2, disabled_buttons[5], 1);
31 set_bkg_based_tiles(x + 12, y, 2, 2, disabled_buttons[6], 1);
32 set_bkg_based_tiles(x + 14, y, 2, 2, disabled_buttons[7], 1);
33 }
34
35 void reset_object_pos(void) {
36 for (uint8_t i = 0; i < 4; i++) {
37 set_sprite_tile(i, i);
38 move_sprite(i,
39 DEVICE_SPRITE_PX_OFFSET_X + (i << 3) + ((DEVICE_SCREEN_PX_WIDTH - (4 * 8)) >> 1),
40 (uint8_t)(DEVICE_SPRITE_PX_OFFSET_Y + 48u) + ((DEVICE_SCREEN_PX_HEIGHT - 8) >> 1)
41 );
42 }
43 }
44
45 bool toggle = false;
46 bool isSGB = false;
47
48 joypads_t old_joypads, joypads;
49 uint8_t old_joy = 0xff, joy = 0;
50
51 void main(void) {
52 // Wait 4 frames
53 // For SGB on PAL SNES this delay is required on startup, otherwise borders don't show up
54 for (uint8_t i = 4; i != 0; i--) vsync();
55
56 #ifdef NINTENDO
57 isSGB = sgb_check();
58 #endif
59 // init joypads
60 joypad_init(4, &joypads);
61
62 set_sprite_data(0, sizeof(sprite_data) >> 4, sprite_data);
63 set_bkg_data(1, sizeof(tiles_data) >> 4, tiles_data);
64 fill_bkg_rect(0, 0, DEVICE_SCREEN_WIDTH, DEVICE_SCREEN_HEIGHT, 0);
65
66 if (isSGB) draw_disabled(0, 0); else draw_buttons(joy, 0, 0);
67
68 for (uint8_t i = 0; i != 4; i++) {
69 if (i <= (joypads.npads - 1)) {
70 draw_buttons(joypads.joypads[i], 0, 4 + (i << 1));
71 } else {
72 draw_disabled(0, 4 + (i << 1));
73 }
74 }
75
76 reset_object_pos();
77 SHOW_SPRITES; SHOW_BKG;
78
79 while(TRUE) {
80 // poll joypads
81 if (toggle = !toggle) {
82 // poll joypad() only if SGB was not detected
83 if (!isSGB) {
84 old_joy = joy, joy = joypad();
85
86 // draw button state if changed
87 if (joy != old_joy) draw_buttons(joy, 0, 0);
88
89 // start button resets position
90 if (joy & J_START) reset_object_pos();
91 }
92 } else {
93 old_joypads = joypads;
94 joypad_ex(&joypads);
95 // iterate joypads, move sprites
96 for (uint8_t i = 0; i < joypads.npads; i++) {
97 uint8_t j = joypads.joypads[i];
98
99 // draw joypad
100 if (old_joypads.joypads[i] != j) draw_buttons(j, 0, 4 + (i << 1));
101
102 // move objects
103 if (j & J_LEFT) scroll_sprite(i, -1, 0);
104 if (j & J_RIGHT) scroll_sprite(i, 1, 0);
105 if (j & J_UP) scroll_sprite(i, 0, -1);
106 if (j & J_DOWN) scroll_sprite(i, 0, 1);
107
108 // start button resets position
109 if (j & J_START) reset_object_pos();
110 }
111 }
112 // wait for vsync
113 vsync();
114 }
115 }
This webpage is intended to be an accessible preview of this repository. To get a fuller picture, clone it and use the git CLI.