-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain.c
More file actions
75 lines (65 loc) · 1.45 KB
/
main.c
File metadata and controls
75 lines (65 loc) · 1.45 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
#include <gb/gb.h>
#include <gb/cgb.h>
#include <stdint.h>
#include "data.h"
#include "joypad.h"
#include "player.h"
#include "graphics.h"
#include "util.h"
/**
* Bitfield for buttons that are currently being held down.
*
* @see J_START, J_SELECT, J_A, J_B, J_UP, J_DOWN, J_LEFT, J_RIGHT
*/
uint8_t joypad_down;
/**
* Bitfield for buttons that were pressed as of this frame.
*
* @see J_START, J_SELECT, J_A, J_B, J_UP, J_DOWN, J_LEFT, J_RIGHT
*/
uint8_t joypad_pressed;
/**
* Initializes graphics, sound, and game state.
*/
void initialize(void) {
load_palettes();
load_tileset();
draw_level();
init_player();
joypad_down = 0;
joypad_pressed = 0;
}
/**
* Executes core gameloop logic.
*/
void game_loop(void) {
update_player();
}
/**
* Executes rendering logic that must occurr during a VBLANK.
* Note: unsure if this is needed given the GBDK abstractions, should test if
* updating VRAM before the vsync() call has any effect.
*/
void render(void) {
}
/**
* Main function for the game. Initializes state and starts the game loop.
*/
void main(void) {
// Initialize the game state
lcd_off();
initialize();
lcd_on();
// Start the main game loop
while (1) {
// Update joypad bitfields
uint8_t last = joypad_down;
joypad_down = joypad();
joypad_pressed = (last ^ joypad_down) & joypad_down;
// Execute game logic
game_loop();
// Wait for a VBLANK then execute rendering logic
vsync();
render();
}
}