-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.odin
573 lines (499 loc) · 23 KB
/
main.odin
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
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
package main
import "core:fmt"
import "core:mem"
import "core:math/rand"
import "core:slice"
import rl "vendor:raylib"
TARGET_FPS :: 60
WINDOW_WIDTH :: 1280
WINDOW_HEIGHT :: 720
WINDOW_TITLE :: "Wonderlust"
Wrap_State :: enum {
Measure_State,
Draw_State,
}
TEXT_SIZE :: 20
IMAGE_X :: 140
IMAGE_Y :: 0
BANNER_Y :: 0
LEFT_BANNER_X :: 0
RIGHT_BANNER_X :: 1140
TEXT_AREA_COLOR :: rl.Color {84, 51, 68, 255}
TEXT_BOX_BACKGROUND_COLOR :: rl.Color {81, 82, 98, 255}
TEXT_BOX_TEXT_COLOR :: rl.Color {202, 160, 90, 255}
CHOICE_BOX_COLOR :: rl.Color {174, 106, 71, 128}
CHOICE_BACKGROUND_COLOR :: rl.Color {81, 82, 98, 255}
CHOICE_TEXT_COLOR :: rl.Color {202, 160, 90, 255}
SELECTED_CHOICE_BACKGROUND_COLOR :: rl.Color {202, 160, 90, 255}
SELECTED_CHOICE_TEXT_COLOR :: rl.Color {81, 82, 98, 255}
CHOICE_RECT_X :: 45
CHOICE_RECT_Y :: 590
CHOICE_RECT_Y_OFFSET :: 40
CHOICE_RECT_WIDTH :: 1190
CHOICE_RECT_HEIGHT :: 38
choice :: struct {
text: cstring,
screen_id: cstring,
side_effects: []proc(),
}
restart_choice := choice {
text = "Restart.",
screen_id = "intro",
side_effects = {reset_self_doubt},
}
screen :: struct {
image: rl.Texture2D,
text: cstring,
choices: []choice,
}
screens: map[cstring]screen
self_doubt: int
AMBIVALENT_ENDING_SELF_DOUBT_THRESHOLD :: 5
main :: proc() {
// Tracking allocator code adapted from Karl Zylinski's tutorials.
track: mem.Tracking_Allocator
mem.tracking_allocator_init(&track, context.allocator)
context.allocator = mem.tracking_allocator(&track)
defer {
for _, entry in track.allocation_map {
fmt.eprintf("%v leaked %v bytes.\n", entry.location, entry.size)
}
for entry in track.bad_free_array {
fmt.eprintf("%v bad free.\n", entry.location)
}
mem.tracking_allocator_destroy(&track)
}
rl.SetConfigFlags({.VSYNC_HINT})
rl.SetTargetFPS(TARGET_FPS)
rl.InitWindow(WINDOW_WIDTH, WINDOW_HEIGHT, WINDOW_TITLE)
defer rl.CloseWindow()
rl.InitAudioDevice()
defer rl.CloseAudioDevice()
font := rl.LoadFont("PixAntiqua.ttf")
left_banner_image := rl.LoadTexture("images/left_banner.png")
right_banner_image := rl.LoadTexture("images/right_banner.png")
cave_entrance_image := rl.LoadTexture("images/cave_entrance.png")
cave_exit_image := rl.LoadTexture("images/cave_exit.png")
crystals_image := rl.LoadTexture("images/crystals.png")
crossroads_image := rl.LoadTexture("images/crossroads.png")
high_path_image := rl.LoadTexture("images/high_path.png")
low_path_image := rl.LoadTexture("images/low_path.png")
owl_image := rl.LoadTexture("images/owl.png")
skinwalker_image := rl.LoadTexture("images/skinwalker.png")
music := rl.LoadMusicStream("universfield_quiet_moments.mp3")
defer rl.UnloadMusicStream(music)
rl.PlayMusicStream(music)
text_area_rect := rl.Rectangle {0, 420, 1280, 300,}
text_box_rect := rl.Rectangle {32, 430, 1216, 150,}
choice_box_rect := rl.Rectangle {32, 585, 1216, 130,}
choice_rect1 := rl.Rectangle {
CHOICE_RECT_X,
CHOICE_RECT_Y + 0,
CHOICE_RECT_WIDTH,
CHOICE_RECT_HEIGHT,
}
choice_rect2 := rl.Rectangle {
CHOICE_RECT_X,
CHOICE_RECT_Y + (CHOICE_RECT_Y_OFFSET * 1),
CHOICE_RECT_WIDTH,
CHOICE_RECT_HEIGHT,
}
choice_rect3 := rl.Rectangle {
CHOICE_RECT_X,
CHOICE_RECT_Y + (CHOICE_RECT_Y_OFFSET * 2),
CHOICE_RECT_WIDTH,
CHOICE_RECT_HEIGHT,
}
// TODO: Build a map of screens programmatically using input XML data. Their choices are also built with this data.
// Ensure there is a good way to insert or overwrite screens for the sake of certain screens that loop with variations.
screens = make(map[cstring]screen)
defer delete(screens)
screens["intro"] = {
image = cave_entrance_image,
text = "There stood a young man before the narrow mouth of a cave sticking out of the ground at a low level like an unsightly pimple. He had sandy hair and looked about 15 (maybe he was.) He was covered in bruises, some fresh and some a sickly yellow-and-purple, and he was dressed like a soldier in some antiquated war (maybe he was.) He had beat the road for a solid fortnight before entering a forest. It was unkind to him, so he followed an old path out and found himself here. His feet were sore and he had to make a decision.",
choices = {
{
text = "Plunge in.",
screen_id = "crystals",
},
{
text = "Turn back.",
screen_id = "early_ending",
},
},
}
screens["early_ending"] = {
image = cave_entrance_image,
text = "It was probably for the best. Who could tell?",
choices = {
restart_choice,
},
}
screens["crystals"] = {
image = crystals_image,
text = "The young man squeezed through the low mouth of the cave. He had to stoop for a few moments of shuffling in the dark before the mouth let out into a dimly illuminated tunnel. He could stand here. The source of light was plain to see; a massive growth of shimmering, golden crystals sticking out of the tunnel wall. The young man started to move on, but stopped when he thought he heard something. It sounded like running water rushing away somewhere. There was a monotonous dripping sound, too. Actually, the young man thought it sounded like whispering.",
choices = {
{
text = "Listen closely.",
screen_id = "crystals_silence",
},
{
text = "\"Hello?\"",
screen_id = "crystals_silence",
},
{
text = "Ignore it and keep moving.",
screen_id = "crossroads",
side_effects = {decrement_self_doubt},
},
},
}
screens["crystals_silence"] = {
image = crystals_image,
text = "The whispering or dripping or whatever it was stopped immediately (the boy thought he could still hear it for a second, but definitely not.) The sound of rushing water was still proceeding steadily, presumably somewhere far away. Or was that just the sound of the inside of the boy's ears?",
choices = {
{
text = "Get moving.",
screen_id = "crossroads",
},
},
}
screens["crossroads"] = {
image = crossroads_image,
text = "The tunnel let out two ways, separated by a craggy cavern wall; one went higher and the other went lower. The young man had to choose a path.",
choices = {
{
text = "Take the high path.",
screen_id = "high_path",
side_effects = {decrement_self_doubt},
},
{
text = "Take the low path.",
screen_id = "low_path",
side_effects = {decrement_self_doubt},
},
},
}
screens["high_path"] = {
image = high_path_image,
text = "The young man had to squeeze through a narrow part of the cave to come out onto an even narrower ledge. No longer afforded the crystal's dim glow, he had to let his eyes adjust to the darkness. Once, he nearly stepped off into nothing. When his eyes adjusted, he still couldn't see the bottom of the space he nearly walked off into.",
choices = {
{
text = "Shut your eyes and sidle along.",
screen_id = "owl",
side_effects = {decrement_self_doubt},
},
{
text = "Look down.",
screen_id = "look_down",
side_effects = {increment_self_doubt},
},
},
}
screens["look_down"] = {
image = high_path_image,
text = "The boy felt a sudden rush of vertigo nearly caused him to keel over the edge then and there. He gripped the wall behind him as best he could, scrabbling to hold onto the crumbling, sheer stone. Somehow he didn't fall. His eyes were still peering into the chasm below (he could see now, clearly, that it was deep.) He thought he saw something shiny and golden down there. Some part of the boy's subconscious stirred at that sight. His hands were shaking and his breath was ragged, still enduring that lightheadedness brought on by nearly falling to one's death (or maybe he just imagined it and his footing had been just fine.)",
choices = {
{
text = "Squint your eyes and lean in to get a better look.",
screen_id = "fall_down_ending",
},
{
text = "Breathe slowly until you regain your nerve.",
screen_id = "owl",
side_effects = {decrement_self_doubt},
},
{
text = "Shut your eyes and look away.",
screen_id = "owl",
},
},
}
screens["low_path"] = {
image = low_path_image,
text = "The young man had an easy enough time proceeding down the low path. He just had to avoid stumbling on the little rocks and cracks in the ground, as well as his own feet. The path let out into a tunnel, wider than the one he had just left, but pitch black. He went on for a while and all the ambient sounds that could be distant echoes of the cavern's ecosystem faded out. It was completely still. The only sounds were his own footfalls. Every once in a while, he thought he heard a scraping sound some distance behind him, like someone was raking the cavern wall with a spade.",
choices = {
{
text = "Stop and listen closely.",
screen_id = "who_goes_there",
},
{
text = "Keep walking.",
screen_id = "owl",
side_effects = {decrement_self_doubt},
},
},
}
screens["who_goes_there"] = {
image = low_path_image,
text = "After he heard the scraping sound one too many times for him to discount it completely, the boy stopped and snapped around a hundred and eighty degrees. He inclined his head in the direction he had come from and listened. Silence. He still couldn't see a thing and strongly wished that he could. After what seemed like an hour (was it really?) he heard the scraping sound again, closer and more regular and different some other way. It was like the same sound had echoed and laid over itself three or four times again.",
choices = {
{
text = "Hurry along.",
screen_id = "owl",
side_effects = {decrement_self_doubt},
},
{
text = "Confront whoever's making the sound.",
screen_id = "skinwalker_ending",
},
},
}
screens["owl"] = {
image = owl_image,
text = "The path crested into a small chamber. The young man could see a little better there. At the far end of the chamber was a small rock with something moving sitting on top. His heart raced for a moment and then relented when he realized it was an owl. The animal's flashing gray eyes seemed to stare into the young man's and give him some comfort. To the owl's left, a light shone dimly from the entrance of another narrow path.",
choices = {
{
text = "Examine the owl.",
screen_id = "owl_closer_look",
},
{
text = "\"Who, owl?\"",
screen_id = "owl_closer_look",
},
{
text = "Ignore the owl and move on.",
screen_id = "ending",
side_effects = {generate_ending},
},
},
}
screens["owl_closer_look"] = {
image = owl_image,
text = "The owl hoots and ruffles its feathers.",
choices = {
{
text = "Move on.",
screen_id = "ending",
side_effects = {generate_ending},
},
},
}
screens["fall_down_ending"] = {
image = low_path_image,
text = "The boy squinted his eyes like he was trying to light a fire with his dusty ‘lids. He leaned further and further…and further. Finally, he leaned too far and lost his footing. He plunged into that yawning abyss and was never seen or heard from again. All that glitters is not gold.",
choices = {
restart_choice,
},
}
screens["skinwalker_ending"] = {
image = skinwalker_image,
text = "The boy hitched up his britches and marched back up the tunnel, double-time. He had suspected for some days that he was being followed and now he intended to interrogate his stalker. He bumped into something and realized the space in front of him was a richer black than the space his eyes had become accustomed to. Grasping around in front of him, he grabbed handfuls of thick strands like a rabbit skin cap he owned as a small child. He looked up and saw a cracked, yellowish coyote skull with two black chasms peering down in his direction and was never seen or heard from again. Beware things that go bump in the night.",
choices = {
restart_choice,
},
}
screens["ending"] = {
image = cave_exit_image,
text = "placeholder",
choices = {
restart_choice,
},
}
current_screen := screens["intro"]
mouse_position: rl.Vector2
for !rl.WindowShouldClose() {
rl.UpdateMusicStream(music);
mouse_position = rl.GetMousePosition()
choice1_selected: bool
choice2_selected: bool
choice3_selected: bool
if rl.CheckCollisionPointRec(mouse_position, choice_rect1) {
choice1_selected = true
} else if rl.CheckCollisionPointRec(mouse_position, choice_rect2) {
choice2_selected = true
} else if rl.CheckCollisionPointRec(mouse_position, choice_rect3) {
choice3_selected = true
}
if rl.IsMouseButtonReleased(.LEFT) {
if choice1_selected && len(current_screen.choices) > 0 {
run_side_effects(current_screen.choices[0])
current_screen = screens[current_screen.choices[0].screen_id]
} else if choice2_selected && len(current_screen.choices) >= 2 {
run_side_effects(current_screen.choices[1])
current_screen = screens[current_screen.choices[1].screen_id]
} else if choice3_selected && len(current_screen.choices) == 3 {
run_side_effects(current_screen.choices[2])
current_screen = screens[current_screen.choices[2].screen_id]
}
}
rl.BeginDrawing()
defer rl.EndDrawing()
rl.ClearBackground(rl.GRAY)
rl.DrawTexture(current_screen.image, IMAGE_X, IMAGE_Y, rl.WHITE)
rl.DrawTexture(left_banner_image, LEFT_BANNER_X, BANNER_Y, rl.WHITE)
rl.DrawTexture(right_banner_image, RIGHT_BANNER_X, BANNER_Y, rl.WHITE)
rl.DrawRectangleRec(text_area_rect, TEXT_AREA_COLOR)
rl.DrawRectangleRec(text_box_rect, TEXT_BOX_BACKGROUND_COLOR)
draw_text_boxed(font, current_screen.text, text_box_rect, TEXT_SIZE, 0, TEXT_BOX_TEXT_COLOR)
rl.DrawRectangleRec(choice_box_rect, CHOICE_BOX_COLOR)
if len(current_screen.choices) > 0 {
if choice1_selected {
rl.DrawRectangleRec(choice_rect1, SELECTED_CHOICE_BACKGROUND_COLOR)
rl.DrawTextEx(font, current_screen.choices[0].text, {choice_rect1.x, choice_rect1.y}, TEXT_SIZE, 0, SELECTED_CHOICE_TEXT_COLOR)
} else {
rl.DrawRectangleRec(choice_rect1, CHOICE_BACKGROUND_COLOR)
rl.DrawTextEx(font, current_screen.choices[0].text, {choice_rect1.x, choice_rect1.y}, TEXT_SIZE, 0, CHOICE_TEXT_COLOR)
}
}
if len(current_screen.choices) >= 2 {
if choice2_selected {
rl.DrawRectangleRec(choice_rect2, SELECTED_CHOICE_BACKGROUND_COLOR)
rl.DrawTextEx(font, current_screen.choices[1].text, {choice_rect2.x, choice_rect2.y}, TEXT_SIZE, 0, SELECTED_CHOICE_TEXT_COLOR)
} else {
rl.DrawRectangleRec(choice_rect2, CHOICE_BACKGROUND_COLOR)
rl.DrawTextEx(font, current_screen.choices[1].text, {choice_rect2.x, choice_rect2.y}, TEXT_SIZE, 0, CHOICE_TEXT_COLOR)
}
}
if len(current_screen.choices) == 3 {
if choice3_selected {
rl.DrawRectangleRec(choice_rect3, SELECTED_CHOICE_BACKGROUND_COLOR)
rl.DrawTextEx(font, current_screen.choices[2].text, {choice_rect3.x, choice_rect3.y}, TEXT_SIZE, 0, SELECTED_CHOICE_TEXT_COLOR)
} else {
rl.DrawRectangleRec(choice_rect3, CHOICE_BACKGROUND_COLOR)
rl.DrawTextEx(font, current_screen.choices[2].text, {choice_rect3.x, choice_rect3.y}, TEXT_SIZE, 0, CHOICE_TEXT_COLOR)
}
}
// Music credit https://freemusicarchive.org/music/universfield/ambient-music/quiet-moments/
rl.DrawTextEx(font, "Music is Quiet Moments by Universfield", {0, WINDOW_HEIGHT - 10}, 10, 0, rl.WHITE)
}
}
run_side_effects :: proc(choice: choice) {
for side_effect in choice.side_effects {
side_effect()
}
}
increment_self_doubt :: proc() {
self_doubt += 1
}
decrement_self_doubt :: proc() {
self_doubt -= 1
}
generate_ending :: proc() {
cave_exit_image := rl.LoadTexture("images/cave_exit.png")
screen, ok := &screens["ending"]
if ok {
if self_doubt >= AMBIVALENT_ENDING_SELF_DOUBT_THRESHOLD {
screen^ = {
image = cave_exit_image,
text = fmt.caprintf("The boy made it to the end of the cave, but his heart was no longer in it. He had had a hard time of it. Maybe it wasn't worth it to keep going on. Maybe they'd take him back if he turned around right now. But what if they were still feeling sore? He sat down to ponder it for a while."),
choices = slice.clone([]choice {
restart_choice,
}),
}
} else {
screen^ = {
image = cave_exit_image,
text = fmt.caprintf("The young man made it to the end of the cave. He was only a little harrowed by what he'd seen and thought in that deep, dark recess. He rested his feet for a while. Daylight was running out. He stood up, stretched, and got back on the road."),
choices = slice.clone([]choice {
restart_choice,
}),
}
}
}
}
reset_self_doubt :: proc() {
self_doubt = 0
}
// Adapted from an example by Vlad Adrian and Ramon Santamaria
draw_text_boxed :: proc(font: rl.Font, text: cstring, rect: rl.Rectangle, font_size: f32, spacing: f32, tint: rl.Color) {
draw_text_boxed_selectable(font, text, rect, font_size, spacing, tint, 0, 0, rl.WHITE, rl.WHITE)
}
draw_text_boxed_selectable :: proc(font: rl.Font, text: cstring, rect: rl.Rectangle, font_size: f32, spacing: f32, tint: rl.Color, select_start: int, select_length: int, select_tint: rl.Color, select_back_tint: rl.Color) {
select_start := select_start
text_as_bytes := transmute([^]u8)text
length := rl.TextLength(text)
text_offset_x: f32
text_offset_y: f32
scale_factor := font_size / f32(font.baseSize)
// Word/character wrapping mechanism variable
state := Wrap_State.Draw_State
start_line := -1
end_line := -1
last_k := -1
for i, k in 0..<length {
i := i
k := k
// Get next codepoint from byte string and glyph index in font
codepoint_byte_count: i32
codepoint := rl.GetCodepoint(cstring(&text_as_bytes[i]), &codepoint_byte_count)
index := rl.GetGlyphIndex(font, codepoint)
// NOTE: Normally we exit the decoding sequence as soon as a bad byte is found (and return 0x3f)
// but we need to draw all of the bad bytes using the '?' symbol moving one byte
if codepoint == 0x3f {
codepoint_byte_count = 1
}
i += u32(codepoint_byte_count - 1)
glyph_width: f32
if codepoint != '\n' {
glyph_width = (font.glyphs[index].advanceX == 0) ? font.recs[index].width * scale_factor : f32(font.glyphs[index].advanceX) * scale_factor
if i + 1 < length {
glyph_width = glyph_width + spacing
}
}
if state == .Measure_State {
if codepoint == ' ' || codepoint == '\t' || codepoint == '\n' {
end_line = int(i)
}
if text_offset_x + glyph_width > rect.width {
end_line = end_line < 1 ? int(i) : end_line
if i == u32(end_line) {
end_line -= int(codepoint_byte_count)
}
if start_line + int(codepoint_byte_count) == end_line {
end_line = int(i) - int(codepoint_byte_count)
}
state = toggle_wrap_state(state)
} else if i + 1 == length {
end_line = int(i)
state = toggle_wrap_state(state)
} else if codepoint == '\n' {
state = toggle_wrap_state(state)
}
if state == .Draw_State {
text_offset_x = 0
i = u32(start_line)
glyph_width = 0
// Save character position when we switch states
tmp := last_k
last_k = k - 1
k = tmp
}
} else {
if codepoint == '\n' {
text_offset_y += f32(font.baseSize + font.baseSize / 2) * scale_factor
text_offset_x = 0
} else {
if text_offset_x + glyph_width > rect.width {
text_offset_y += f32(font.baseSize + font.baseSize / 2) * scale_factor
text_offset_x = 0
}
// When text overflows rectangle height limit, just stop drawing
if text_offset_y + f32(font.baseSize) * scale_factor > rect.height {
break
}
// Draw selection background
is_glyph_selected: bool
if select_start >= 0 && k >= select_start && k < select_start + select_length {
rl.DrawRectangleRec({rect.x + text_offset_x - 1, rect.y + text_offset_y, glyph_width, f32(font.baseSize) * scale_factor}, select_back_tint)
is_glyph_selected = true
}
// Draw current character glyph
if codepoint != ' ' && codepoint != '\t' {
rl.DrawTextCodepoint(font, codepoint, {rect.x + text_offset_x, rect.y + text_offset_y}, font_size, is_glyph_selected ? select_tint : tint)
}
}
}
// Avoid leading spaces
if text_offset_x != 0 || codepoint != ' ' {
text_offset_x += glyph_width
}
}
}
toggle_wrap_state :: proc(state: Wrap_State) -> Wrap_State {
if state == .Draw_State {
return .Measure_State
} else {
return .Draw_State
}
}