-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathmain.c
More file actions
170 lines (148 loc) · 5.77 KB
/
Copy pathmain.c
File metadata and controls
170 lines (148 loc) · 5.77 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
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
160
161
162
163
164
165
166
167
168
169
170
/* main.c - boot setup and the game loop.
*/
#include "hw.h"
#include "config.h"
#include "raycast.h"
#include "map.h"
/* ---- palette setup --------------------------------------------------- */
static void init_palettes(void) {
/* index 0 of every palette is transparent for sprites; we keep walls
* opaque by only using indices 1..3. */
/* wall, lit (N/S faces): mortar / brick / highlight */
pal_set(PAL_WALL_LIT, 1, RGB( 9, 9, 10));
pal_set(PAL_WALL_LIT, 2, RGB(24, 8, 6));
pal_set(PAL_WALL_LIT, 3, RGB(29, 14, 12));
/* wall, dark (E/W faces): same hues, dimmer */
pal_set(PAL_WALL_DARK, 1, RGB( 5, 5, 6));
pal_set(PAL_WALL_DARK, 2, RGB(14, 4, 3));
pal_set(PAL_WALL_DARK, 3, RGB(18, 8, 7));
/* distance-shaded wall palettes Shading toward black. */
{
/* index: 1=body 2=body 3=highlight (1 and 2 share the wall colour) */
static const u8 base_r[3] = {24, 24, 29};
static const u8 base_g[3] = { 8, 8, 14};
static const u8 base_b[3] = { 6, 6, 12};
for (int b = 0; b < DEPTH_BANDS; b++) {
int fn = 256 - (b * 200) / (DEPTH_BANDS - 1); /* 256 near .. 56 far */
for (int s = 0; s < 2; s++) {
int sf = s ? 140 : 256; /* dark side ~55% */
u16 pal = PAL_DEPTH_BASE + s * DEPTH_BANDS + b;
for (int i = 0; i < 3; i++) {
int r = base_r[i] * fn / 256 * sf / 256;
int g = base_g[i] * fn / 256 * sf / 256;
int bl = base_b[i] * fn / 256 * sf / 256;
pal_set(pal, (u16)(i + 1), RGB((u8)r, (u8)g, (u8)bl));
}
}
}
}
/* backdrop bands */
pal_set(PAL_CEILING, 1, RGB( 6, 7, 12)); /* night sky / ceiling */
pal_set(PAL_FLOOR, 1, RGB(12, 10, 7)); /* dusty floor */
/* minimap */
pal_set(PAL_MAP_WALL, 15, RGB(20, 20, 22)); /* walls */
pal_set(PAL_MAP_PLAYER, 15, RGB( 4, 31, 8)); /* player */
REG_BACKDROP = RGB(0, 0, 0);
}
/* ---- clear the fix layer */
static void clear_fix(void) {
vram_addr(VRAM_FIX);
vram_mod(1);
for (int i = 0; i < 40 * 32; i++) vram_w(0x0000);
}
static int prev_px = -1, prev_py = -1;
static u8 map_on = 1; /* minimap visible? */
static void map_cell(int mx, int my, u16 pal, u16 tile) {
fix_poke((u16)(MAP_FIX_COL + mx), (u16)(MAP_FIX_ROW + my), pal, tile);
}
static void draw_minimap(void) {
for (int my = 0; my < MAP_H; my++)
for (int mx = 0; mx < MAP_W; mx++) {
if (map_at(mx, my))
map_cell(mx, my, PAL_MAP_WALL, FIX_SOLID);
else
map_cell(mx, my, 0, FIX_BLANK);
}
}
/* blank just the minimap's fix region */
static void clear_minimap(void) {
for (int my = 0; my < MAP_H; my++)
for (int mx = 0; mx < MAP_W; mx++)
map_cell(mx, my, 0, FIX_BLANK);
}
/* restore the cell the marker was on, then mark the new one */
static void update_marker(void) {
int px, py;
if (!map_on) return;
rc_player_cell(&px, &py);
if (px == prev_px && py == prev_py) return;
if (prev_px >= 0) { /* repaint old cell as its map content */
if (map_at(prev_px, prev_py)) map_cell(prev_px, prev_py, PAL_MAP_WALL, FIX_SOLID);
else map_cell(prev_px, prev_py, 0, FIX_BLANK);
}
map_cell(px, py, PAL_MAP_PLAYER, FIX_SOLID);
prev_px = px; prev_py = py;
}
static void disable_sprites(void) {
for (u16 s = 1; s < SPR_TOTAL; s++) {
scb2(s, 0x0F, 0x00); /* full width, zero height */
scb3(s, SCRH + 32, 0, 1); /* below the visible area */
scb4(s, 0);
}
}
/* ---- static floor/ceiling backdrop: BG_COUNT full-width columns ------ */
static void init_background(void) {
for (u16 i = 0; i < BG_COUNT; i++) {
u16 spr = BG_BASE + i;
for (u16 t = 0; t < BG_WIN; t++) {
u16 pal = (t < BG_SPLIT) ? PAL_CEILING : PAL_FLOOR;
scb1_tile(spr, t, TILE_SOLID, pal);
}
scb2(spr, 0x0F, 0xFF); /* full size, no shrink (16-tile ref) */
scb3(spr, 0, 0, BG_WIN); /* top of screen */
scb4(spr, i * 16);
}
}
/* ---- wall-slice sprites: fixed X + brick tilemap set once; SCB2/SCB3
* (and palette when shading flips) are updated every frame in rc_blit. --- */
static void init_walls(void) {
for (u16 c = 0; c < NUM_COLS; c++) {
u16 spr = WALL_BASE + c;
scb1_fill(spr, WALL_WIN, TILE_BRICK, PAL_WALL_LIT);
scb4(spr, c * COLW);
scb2(spr, HSHRINK, 0x00);
scb3(spr, 0, 0, WALL_WIN);
}
}
int main(void) {
watchdog_kick();
clear_fix();
init_palettes();
disable_sprites();
init_background();
init_walls();
rc_init();
draw_minimap(); /* static walls, drawn once */
for (;;) {
u8 pressed = (u8)~REG_P1CNT;
rc_input(pressed);
rc_render(); /* DDA during active display */
wait_vblank();
watchdog_kick();
rc_blit(); /* push to VRAM during vblank */
/* button C toggles the minimap */
{
enum { C = 0x40 }; /* P1 bit 6 */
static u8 c_prev = 0;
u8 c_now = pressed & C;
if (c_now && !c_prev) {
map_on = !map_on;
if (map_on) { draw_minimap(); prev_px = -1; } /* -1 forces marker repaint */
else clear_minimap();
}
c_prev = c_now;
}
update_marker(); /* 2 fix writes when the cell changes */
}
return 0;
}