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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
|
package demonchime
import "base:runtime"
import "core:c"
import "core:c/libc"
import "core:fmt"
import "core:log"
import "core:mem"
import "core:math/linalg"
import rl "vendor:raylib"
import "phys"
Vec2 :: [2]f32
Rect :: struct {
start: Vec2,
size: Vec2,
}
ROOM_FADE_SIZE :: 8
state: struct {
debug_mode: bool,
camera_target: Vec2,
camera: rl.Camera2D,
platform_list: Entity_List(Platform),
bullet_list: Entity_List(Bullet),
}
logger: log.Logger
raylib_log :: proc "c" (
msg_type: rl.TraceLogLevel,
fmt: cstring,
args: ^c.va_list,
) {
context = runtime.default_context()
context.logger = logger
msg_bytes: [1028]u8
libc.vsnprintf(raw_data(msg_bytes[:]), 1028, fmt, args)
msg := string(msg_bytes[:])
#partial switch msg_type {
case .DEBUG: log.debug(msg)
case .INFO: log.info(msg)
case .WARNING: log.warn(msg)
case .ERROR: log.error(msg)
case .FATAL: log.fatal(msg)
}
}
init :: proc() {
state.camera.zoom = 1
init_keybinds()
init_draw()
init_player()
open_room(.Carrabassett0)
}
frame :: proc() {
if is_keybind_just_down(actions.toggle_debug_mode) {
state.debug_mode = !state.debug_mode
}
dt := rl.GetFrameTime()
for cb in update_callbacks {
cb(dt)
}
state.camera.target = linalg.lerp(
state.camera.target,
state.camera_target,
10 * dt,
)
draw_new_frame()
cam := state.camera
cam.target = linalg.round(cam.target)
rl.BeginMode2D(cam)
renderer.tint = {0.2, 0.2, 0.2, 1}
draw_rect({
{0, 0},
{f32(current_room.width), f32(current_room.height)},
})
renderer.tint = {1, 1, 1, 1}
for cb in draw_callbacks {
cb()
}
draw_room(current_room.id)
rl.DrawRectangleGradientH(
0,
0,
ROOM_FADE_SIZE,
current_room.height,
rl.BLACK,
rl.BLANK,
)
rl.DrawRectangleGradientH(
current_room.width - ROOM_FADE_SIZE,
0,
ROOM_FADE_SIZE,
current_room.height,
rl.BLANK,
rl.BLACK,
)
rl.DrawRectangleGradientV(
0,
0,
current_room.width,
ROOM_FADE_SIZE,
rl.BLACK,
rl.BLANK,
)
rl.DrawRectangleGradientV(
0,
current_room.height - ROOM_FADE_SIZE,
current_room.width,
ROOM_FADE_SIZE,
rl.BLANK,
rl.BLACK,
)
if state.debug_mode {
// Draw all collisions
body_it: int
renderer.tint = {1, 0.25, 0.5, 0.25}
for body in phys.iterate_bodies(&body_it) {
rect := phys.get_rect(body)
rect.start += phys.get_position(body)
draw_linerect(transmute(Rect)rect)
draw_rect(transmute(Rect)rect)
}
renderer.tint = {1, 1, 1, 1}
}
rl.EndMode2D()
fps_text := fmt.caprintf(
"FPS: %v",
rl.GetFPS(),
allocator = context.temp_allocator,
)
rl.DrawText(fps_text, 5, 5, 8, rl.GREEN)
if state.debug_mode {
rl.DrawText("Debug Mode", 5, 5 + 8, 8, rl.YELLOW)
}
draw_end_frame()
}
cleanup :: proc() {
deinit_player()
delete_entity_list(state.platform_list)
delete_entity_list(state.bullet_list)
phys.destroy_world()
}
main :: proc() {
when ODIN_DEBUG {
track: mem.Tracking_Allocator
mem.tracking_allocator_init(&track, context.allocator)
context.allocator = mem.tracking_allocator(&track)
defer {
if len(track.allocation_map) > 0 {
fmt.eprintf(
"=== %v allocations not freed: ===\n",
len(track.allocation_map),
)
total := 0
for _, entry in track.allocation_map {
fmt.eprintf(
"- %v bytes @ %v (%v, mode %v)\n",
entry.size,
entry.location,
entry.err,
entry.mode,
)
total += entry.size
}
fmt.eprintf("=== a total of %v bytes unfreed ===\n", total)
}
mem.tracking_allocator_destroy(&track)
}
}
logger = log.create_console_logger()
context.logger = logger
defer log.destroy_console_logger(logger)
rl.SetTraceLogCallback(raylib_log)
rl.InitWindow(SCREEN_WIDTH * 3, SCREEN_HEIGHT * 3, "Demonchime")
rl.SetWindowState({.WINDOW_RESIZABLE})
rl.SetTargetFPS(240)
init()
for !rl.WindowShouldClose() {
frame()
free_all(context.temp_allocator)
}
cleanup()
}
|