/
main.c
160 lines (132 loc) · 3.32 KB
/
main.c
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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
#include "main.h"
#include <gb/gb.h>
#include <string.h>
#include "OAMManager.h"
#include "Scroll.h"
#include "Keys.h"
#include "gbt_player.h"
#include "SpriteManager.h"
#include "BankManager.h"
#include "Fade.h"
#include "Palette.h"
extern UINT8 next_state;
UINT8 delta_time;
UINT8 current_state;
UINT8 state_running = 0;
void SetState(UINT8 state) {
state_running = 0;
next_state = state;
}
void* last_music = 0;
void PlayMusic(const unsigned char* music[], unsigned char bank, unsigned char loop) {
if(music != last_music) {
last_music = music;
gbt_play(music, bank, 7);
gbt_loop(loop);
REFRESH_BANK;
}
}
UINT8 vbl_count;
INT16 old_scroll_x, old_scroll_y;
UINT8 music_mute_frames = 0;
void vbl_update() {
vbl_count ++;
//Instead of assigning scroll_y to SCX_REG I do a small interpolation that smooths the scroll transition giving the
//Illusion of a better frame rate
if(old_scroll_x < scroll_x)
old_scroll_x += (scroll_x - old_scroll_x + 1) >> 1;
else if(old_scroll_x > scroll_x)
old_scroll_x -= (old_scroll_x - scroll_x + 1) >> 1;
SCX_REG = old_scroll_x + (scroll_offset_x << 3);
if(old_scroll_y < scroll_y)
old_scroll_y += (scroll_y - old_scroll_y + 1) >> 1;
else if(old_scroll_y > scroll_y)
old_scroll_y -= (old_scroll_y - scroll_y + 1) >> 1;
SCY_REG = old_scroll_y + (scroll_offset_y << 3);
if(music_mute_frames != 0) {
music_mute_frames --;
if(music_mute_frames == 0) {
gbt_enable_channels(0xF);
}
}
}
void InitStates();
void InitSprites();
void MusicUpdate() {
gbt_update();
REFRESH_BANK;
}
extern UWORD ZGB_Fading_BPal[32];
extern UWORD ZGB_Fading_SPal[32];
#ifdef CGB
void SetPalette(PALETTE_TYPE t, UINT8 first_palette, UINT8 nb_palettes, UINT16 *rgb_data, UINT8 bank) {
UWORD* pal_ptr = (t == BG_PALETTE) ? ZGB_Fading_BPal : ZGB_Fading_SPal;
PUSH_BANK(bank);
if(t == BG_PALETTE) {
set_bkg_palette(first_palette, nb_palettes, rgb_data);
} else {
set_sprite_palette(first_palette, nb_palettes, rgb_data);
}
memcpy(&pal_ptr[first_palette << 2], rgb_data, nb_palettes << 3);
POP_BANK;
}
#endif
UINT16 default_palette[] = {RGB(31, 31, 31), RGB(20, 20, 20), RGB(10, 10, 10), RGB(0, 0, 0)};
void main() {
#ifdef CGB
cpu_fast();
#endif
PUSH_BANK(1);
InitStates();
InitSprites();
POP_BANK;
disable_interrupts();
add_VBL(vbl_update);
add_TIM(MusicUpdate);
#ifdef CGB
TMA_REG = _cpu == CGB_TYPE ? 120U : 0xBCU;
#else
TMA_REG = 0xBCU;
#endif
TAC_REG = 0x04U;
set_interrupts(VBL_IFLAG | TIM_IFLAG);
enable_interrupts();
while(1) {
while (state_running) {
if(!vbl_count)
wait_vbl_done();
delta_time = vbl_count == 1u ? 0u : 1u;
vbl_count = 0;
UPDATE_KEYS();
SpriteManagerUpdate();
PUSH_BANK(stateBanks[current_state]);
updateFuncs[current_state]();
POP_BANK;
}
FadeIn();
DISPLAY_OFF
gbt_stop();
last_music = 0;
last_sprite_loaded = 0;
SpriteManagerReset();
state_running = 1;
current_state = next_state;
scroll_target = 0;
#ifdef CGB
if (_cpu == CGB_TYPE) {
SetPalette(BG_PALETTE, 0, 1, default_palette, 1);
SetPalette(SPRITES_PALETTE, 0, 1, default_palette, 1);
} else
#endif
BGP_REG = OBP0_REG = OBP1_REG = PAL_DEF(0, 1, 2, 3);
PUSH_BANK(stateBanks[current_state]);
(startFuncs[current_state])();
POP_BANK;
old_scroll_x = scroll_x;
old_scroll_y = scroll_y;
if(state_running) {
DISPLAY_ON;
FadeOut();
}
}
}