summaryrefslogtreecommitdiff
path: root/sim.cpp
blob: 6636ac9808826ee4be53215c8ab390c5b2754d77 (plain)
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
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
// @TODO: test read/write setups
#include "gui.hpp"
#ifdef _WIN32
#include <windows.h>
#include "lib/glcorearb.h"
#endif
#include <GL/gl.h>
#include <time.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <limits.h>
#include <assert.h>
#include <ctype.h>

#define MATH_GL
#include "math.cpp"
#include "sim.hpp"
#include "time.cpp"
#include "util.cpp"
#include "base.cpp"
#include "text.cpp"

#define BALL_STARTING_X 3.0f
#define BALL_STARTING_POS V2(BALL_STARTING_X, 10.0f)

static b2Vec2 v2_to_b2(v2 v) {
	return b2Vec2(v.x, v.y);
}

static v2 b2_to_v2(b2Vec2 v) {
	return V2(v.x, v.y);
}

// converts Box2D coordinates to GL coordinates
static v2 b2_to_gl(State const *state, v2 box2d_coordinates) {
	v3 v = m4_mul_v3(state->transform, v3_from_v2(box2d_coordinates));
	return V2(v.x, v.y);
}

static v2 pixels_to_gl_coords(State *state, u32 x, u32 y) {
	return V2((float)x / state->win_width * 2 - 1,
		(1 - (float)y / state->win_height) * 2 - 1);
}

#include "shaders.cpp"
#include "platforms.cpp"

static void simulate_time(State *state, float dt) {
	Ball *ball = &state->ball;
	if (!ball->body) return; // we're done simulating
	float time_step = 0.01f; // fixed time step
	dt += state->time_residue;
	while (dt >= time_step) {
		b2World *world = state->world;

		world->Step(time_step, 8, 3); // step using recommended parameters

		{ // update ball
			state->stuck_time += time_step;
			state->total_time += time_step;
			b2Vec2 ball_pos = ball->body->GetPosition();

			assert(!(isnan(ball_pos.x) || isnan(ball_pos.y))); // there used to be a problem with NaN but it should be fixed now

			bool reached_bottom = ball_pos.y - ball->radius < state->bottom_y; // ball reached bottom line
			float max_stuck_time = 10;
			bool stuck = state->stuck_time > max_stuck_time; // ball hasn't gotten any further in a while. it's over
			if (reached_bottom || stuck) {
				world->DestroyBody(ball->body);
				ball->body = NULL;
				if (reached_bottom) {
					// place ball on ground
					ball->pos.y = state->bottom_y + ball->radius;
				}
				if (stuck)
					state->stuck_time = max_stuck_time;
				return; // done simulating
			} else {
				ball->pos = b2_to_v2(ball_pos);
				float rounded_pos = 0.01f * roundf(ball->pos.x * 100);
				float rounded_record = 0.01f * roundf(state->furthest_ball_x_pos * 100);
				if (rounded_pos > rounded_record) { // only update record if centimeter reading will change
					state->furthest_ball_x_pos = ball->pos.x;
					state->stuck_time = 0;
				}
			}
		}

		for (Platform *platform = state->platforms, *end = platform + state->nplatforms; platform != end; ++platform) {
			platform->angle = platform->body->GetAngle();
			v2 pos = b2_to_v2(platform->body->GetPosition());
			platform->center = pos;

			if (platform->moves) {
				// check if the platform has reached the other endpoint; if so, set it going in the other direction
				v2 p1 = platform->move_p1, p2 = platform->move_p2;
				v2 vel = b2_to_v2(platform->body->GetLinearVelocity());
				bool switch_direction = false;

				if (vel.x > 0) {
					if (pos.x > maxf(p1.x, p2.x))
						switch_direction = true;
				} else if (vel.x < 0) {
					if (pos.x < minf(p1.x, p2.x))
						switch_direction = true;
				}
				if (vel.y > 0) {
					if (pos.y > maxf(p1.y, p2.y))
						switch_direction = true;
				} else if (vel.y < 0) {
					if (pos.y < minf(p1.y, p2.y))
						switch_direction = true;
				}

				if (switch_direction) {
					v2 new_vel = v2_scale(vel, -1); // flip the velocity
					platform->body->SetLinearVelocity(v2_to_b2(new_vel));
				}
				
			}
		}


		dt -= time_step;
	}
	state->time_residue = dt;
}

// render the ball
static void ball_render(State *state) {
	GL *gl = &state->gl;
	Ball *ball = &state->ball;
	float ball_x = ball->pos.x, ball_y = ball->pos.y;
	float ball_r = ball->radius;
	ShaderBall *shader = &state->shader_ball;
	
	shader_start_using(gl, &shader->base);
	
	gl->UniformMatrix4fv(shader->uniform_transform, 1, GL_FALSE, state->transform.e);
	gl->Uniform2f(shader->uniform_center, ball_x, ball_y);
	gl->Uniform1f(shader->uniform_radius, ball_r);

	glBegin(GL_QUADS);
	glColor3f(1,1,1);
	glVertex2f(ball_x-ball_r, ball_y-ball_r);
	glVertex2f(ball_x-ball_r, ball_y+ball_r);
	glVertex2f(ball_x+ball_r, ball_y+ball_r);
	glVertex2f(ball_x+ball_r, ball_y-ball_r);
	glEnd();
	shader_stop_using(gl);
}

#include "setup.cpp"

static void correct_mouse_button(State *state, u8 *button) {
	if (*button == MOUSE_LEFT) {
		if (state->shift) {
			// shift+left mouse = right mouse
			*button = MOUSE_RIGHT;
		} else if (state->ctrl) {
			// ctrl+left mouse = middle mouse
			*button = MOUSE_MIDDLE;
		}
	}
}

static void start_evolution(State *state) {
	for (size_t i = 0; i < arr_count(state->setups); ++i) {
		// randomize initial setups
		Setup *setup = &state->setups[i];
		setup_random(state, setup);
		setup_score(state, setup);
	}
	setups_sort(state);
	state->evolve_menu = true;
}

// make sure you call start_evolution before you call this function for the first time
static void start_generation(State *state) {
	state->scoring_next = 0;
	state->evolving = true;
}

static void finish_generation(State *state) {
	setups_sort(state);
	for (size_t i = 0; i < TOP_KEPT; ++i) {
		Setup *setup = &state->setups[i];
		char filename[64] = {0};
		snprintf(filename, sizeof filename - 1, "setups/%03zu.b2s", i);
	#if 0
		printf("%zu. %f - mutated %llu times\n", 
			i, setup->score, (ullong)setup->mutations);
	#endif
		setup_write_to_file(setup, filename);
	}
	
	++state->generation;
}

// returns true if this is the last one in the generation
static bool score_one(State *state) {
	u32 i = state->scoring_next++;
	// create new generation from TOP_KEPT
	Setup *setup = &state->setups[i + TOP_KEPT];
	*setup = state->setups[rand() % TOP_KEPT]; // select one of the top setups to mutate from
	++setup->mutations;
	switch (i / 20) {
	case 0: setup_mutate(state, setup, 0.05f); break; // 5% mutation rate group
	case 1: setup_mutate(state, setup, 0.10f); break; // 10% mutation rate group
	case 2: setup_mutate(state, setup, 0.20f); break; // 20% mutation rate group
	case 3: setup_mutate(state, setup, 0.30f); break; // 30% mutation rate group
	case 4: // completely random group
		memset(setup, 0, sizeof *setup);
		setup_random(state, setup);
		break;
	}
	setup_score(state, setup);
	if (state->scoring_next >= GENERATION_SIZE) {
		finish_generation(state);
		state->scoring_next = 0;
		return true;
	}
	return false;
}

#ifdef __cplusplus
extern "C"
#endif
#ifdef _WIN32
__declspec(dllexport)
#endif
void sim_frame(Frame *frame) {
	if (frame->memory_size < sizeof(State)) {
		printf("Not enough memory (got %lu, require %lu).\n", (ulong)frame->memory_size, (ulong)sizeof(State));
		frame->close = true;
		return;
	}
	i32 width = frame->width, height = frame->height;
	Input *input = &frame->input;
	maybe_unused u8 *keys_pressed = input->keys_pressed;
	maybe_unused bool *keys_down = input->keys_down;
	State *state = (State *)frame->memory;
#if DEBUG
	if (state->magic_number != MAGIC_NUMBER || keys_pressed[KEY_F5]) {
		memset(state, 0, sizeof *state);
	}
#endif
	Ball *ball = &state->ball;
	GL *gl = &state->gl;
	state->ctrl = input->keys_down[KEY_LCTRL] || input->keys_down[KEY_RCTRL];
	state->shift = input->keys_down[KEY_LSHIFT] || input->keys_down[KEY_RSHIFT];

	for (u32 i = 0; i < input->nmouse_presses; ++i) {
		MousePress *p = &input->mouse_presses[i];
		correct_mouse_button(state, &p->button);
	}
	for (u32 i = 0; i < input->nmouse_releases; ++i) {
		MouseRelease *r = &input->mouse_releases[i];
		correct_mouse_button(state, &r->button);
	}

	state->win_width  = (float)width;
	state->win_height = (float)height;
	state->aspect_ratio = state->win_width / state->win_height;
	state->dt = (float)frame->dt;
	
	if (width == 0 || height == 0) return;

	// set up GL
	glEnable(GL_BLEND);
	glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
	glViewport(0, 0, width, height);
	glMatrixMode(GL_MODELVIEW);
	glLoadIdentity();
	glClearColor(0, 0, 0, 1);
	glClear(GL_COLOR_BUFFER_BIT);
	

	if (!state->initialized) {
		logln("Initializing...");
		strcpy(frame->title, "Boxcatapult2D");

		void (*(*get_gl_proc)(char const *))(void) = frame->get_gl_proc;

		// get GL functions
	#define required_gl_proc(name) if (!(gl->name = (GL ## name)get_gl_proc("gl" #name))) { printf("Couldn't get GL proc: %s.\n", #name); exit(-1); }
	#define optional_gl_proc(name) gl->name = (GL ## name)get_gl_proc("gl" #name)
		required_gl_proc(AttachShader);
		required_gl_proc(CompileShader);
		required_gl_proc(CreateProgram);
		required_gl_proc(CreateShader);
		required_gl_proc(DeleteProgram);
		required_gl_proc(DeleteShader);
		required_gl_proc(GetAttribLocation);
		required_gl_proc(GetProgramInfoLog);
		required_gl_proc(GetProgramiv);
		required_gl_proc(GetShaderInfoLog);
		required_gl_proc(GetShaderiv);
		required_gl_proc(GetUniformLocation);
		required_gl_proc(LinkProgram);
		required_gl_proc(ShaderSource);
		required_gl_proc(Uniform1f);
		required_gl_proc(Uniform2f);
		required_gl_proc(Uniform3f);
		required_gl_proc(Uniform4f);
		required_gl_proc(Uniform1i);
		required_gl_proc(Uniform2i);
		required_gl_proc(Uniform3i);
		required_gl_proc(Uniform4i);
		required_gl_proc(UniformMatrix4fv);
		required_gl_proc(UseProgram);
		required_gl_proc(VertexAttrib1f);
		required_gl_proc(VertexAttrib2f);
		required_gl_proc(VertexAttrib3f);
		required_gl_proc(VertexAttrib4f);
	#undef optional_gl_proc
	#undef required_gl_proc

		make_directory("setups");

		shaders_load(state);
		
		state->platform_thickness = 0.05f;
		state->bottom_y = 0.1f;
		state->left_x   = 0;

		text_font_load(state, &state->font, "assets/font.ttf", 36.0f);
		text_font_load(state, &state->small_font, "assets/font.ttf", 18.0f);
		text_font_load(state, &state->large_font, "assets/font.ttf", 72.0f);

		b2Vec2 gravity(0, -9.81f);
		b2World *world = state->world = new b2World(gravity);
			
		// create ground
		b2BodyDef ground_body_def;
		ground_body_def.position.Set(0.0f, -1000.0f);
		b2Body *ground_body = world->CreateBody(&ground_body_def);

		b2PolygonShape ground_shape;
		ground_shape.SetAsBox(50.0f, 10.0f);
		ground_body->CreateFixture(&ground_shape, 0.0f);

		// create left wall
		b2BodyDef left_wall_def;
		left_wall_def.position.Set(state->left_x - 0.5f, 0);
		b2Body *left_wall_body = world->CreateBody(&left_wall_def);
		b2PolygonShape left_wall_shape;
		left_wall_shape.SetAsBox(0.5f, 1000);
		left_wall_body->CreateFixture(&left_wall_shape, 0);


		#if 0
		// make a bunch of random setups and pick the best one
		Setup *best_setup = &state->generation[0];
		for (size_t i = 0; i < GENERATION_SIZE; ++i) {
			Setup *setup = &state->generation[i];
			setup_random(state, setup);
			char filename[64] = {0};
			snprintf(filename, sizeof filename-1, "setups/%03zu.b2s", i);
			setup_write_to_file(setup, filename);
			setup_score(state, setup);
			printf("Setup %zu: %.2f m\n", i, setup->score);
			if (setup->score > best_setup->score) {
				best_setup = setup;
			}
		}
		printf("Best: setup %ld\n", (long)(best_setup - state->generation));
		setup_use(state, best_setup);
		assert(state->nplatforms == best_setup->nplatforms);
		#endif

		{
			Platform *b = &state->platform_building;
			b->radius = 1.5f;
			b->color = 0xFF00FF7F;
		}

		state->pan = BALL_STARTING_POS;

		state->start_menu = true;
		input->nkey_presses = 0; // discard any key presses on the first frame
		state->initialized = true;
	#if DEBUG
		state->magic_number = MAGIC_NUMBER;
	#endif
	}

#if DEBUG
	shaders_reload_if_necessary(state);
#endif


	Font *font = &state->font;
	Font *small_font = &state->small_font;
	Font *large_font = &state->large_font;

	Platform *mouse_platform = platform_at_mouse_pos(state);

	if (state->simulating) {
		// simulate physics
		float dt = state->dt;
		if (dt > 100) dt = 100; // prevent floating-point problems for very large dt's
		simulate_time(state, dt);
		if (keys_pressed[KEY_SPACE]) {
			// edit this setup
			state->building = true;
			state->simulating = false;
			keys_pressed[KEY_SPACE] = 0;
			setup_reset(state);
		}
		if (keys_pressed[KEY_ESCAPE]) {
			// back to evolve menu
			state->simulating = false;
			state->evolve_menu = true;
			keys_pressed[KEY_ESCAPE] = 0;
		}
	}


	{
		float half_height = 10.0f;
		float half_width = half_height * state->win_width / state->win_height;
		float view_x = ball->pos.x, view_y = ball->pos.y;
		if (state->building) {
			// pan = center of view
			view_x = state->pan.x;
			view_y = state->pan.y;
		}
		// center view around (view_x, view_y)
		state->transform = m4_ortho(view_x - half_width, view_x + half_width, view_y - half_height, view_y + half_height, -1, +1);
		state->inv_transform = m4_inv(state->transform);
	}

	{ // calculate mouse position in Box2D coordinates
		state->mouse_pos_gl = pixels_to_gl_coords(state, input->mouse_x, input->mouse_y);
		v3 mouse_gl_coords = v3_from_v2(state->mouse_pos_gl);
		state->mouse_pos = v3_xy(m4_mul_v3(state->inv_transform, mouse_gl_coords));
	}

	if (state->building) {
		Platform *platform_building = &state->platform_building;

		if (keys_pressed[KEY_R]) {
			// toggle rotating platform
			bool rotates = platform_building->rotates = !platform_building->rotates;
			if (rotates) {
				if (platform_building->rotate_speed == 0) {
					platform_building->rotate_speed = 1;
				}
			} else {
				platform_building->rotate_speed = 0;
			}
		}

		if (keys_pressed[KEY_M]) {
			// toggle moving platform
			bool moves = platform_building->moves = !platform_building->moves;
			if (moves) {
				platform_building->move_p1 = platform_building->center;
				platform_building->move_speed = 1.0f;
				state->setting_move_p2 = true;
			} else {
				state->setting_move_p2 = false;
			}
		}

		if (keys_pressed[KEY_B]) {
			// pan back to ball
			state->pan = ball->pos;
		}

		if (state->setting_move_p2) {
			platform_building->move_p2 = state->mouse_pos;
		} else {
			platform_building->center = state->mouse_pos;
			platform_building->move_p1 = platform_building->center;
		}
		float dt = state->dt;
		float rotate_amount = 2.0f * dt;
		float radius_change_amount = 2.0f * dt;
		// rotate platform using left/right
		if (keys_down[KEY_LEFT]) {
			platform_building->angle += rotate_amount;
		}
		if (keys_down[KEY_RIGHT]) {
			platform_building->angle -= rotate_amount;
		}

		// change size of platform using up/down
		if (keys_down[KEY_UP])
			platform_building->radius += radius_change_amount;
		if (keys_down[KEY_DOWN])
			platform_building->radius -= radius_change_amount;

		// pan
		float pan_amount = 5.0f * dt;
		v2 *pan = &state->pan;
		if (keys_down[KEY_A])
			pan->x -= pan_amount;
		if (keys_down[KEY_D])
			pan->x += pan_amount;
		if (keys_down[KEY_S])
			pan->y -= pan_amount;
		if (keys_down[KEY_W])
			pan->y += pan_amount;


		// change rotation speed
		float rotate_change_amount = 2.0f * dt;
		if (keys_down[KEY_Q]) {
			if (!platform_building->rotates) {
				platform_building->rotates = true;
				platform_building->rotate_speed = 0;
			}
			platform_building->rotate_speed += rotate_change_amount;
		} 
		if (keys_down[KEY_E]) {
			if (!platform_building->rotates) {
				platform_building->rotates = true;
				platform_building->rotate_speed = 0;
			}
			platform_building->rotate_speed -= rotate_change_amount;
		}

		if (platform_building->moves) {
			// change move speed
			float speed_change_amount = 2.0f * dt;
			if (keys_down[KEY_Z])
				platform_building->move_speed -= speed_change_amount;
			if (keys_down[KEY_X])
				platform_building->move_speed += speed_change_amount;
		}

		platform_building->rotate_speed = clampf(platform_building->rotate_speed,
			PLATFORM_ROTATE_SPEED_MIN, PLATFORM_ROTATE_SPEED_MAX);
		platform_building->move_speed = clampf(platform_building->move_speed,
			PLATFORM_MOVE_SPEED_MIN, PLATFORM_MOVE_SPEED_MAX);
		platform_building->radius = clampf(platform_building->radius,
			PLATFORM_RADIUS_MIN, PLATFORM_RADIUS_MAX);
		platform_building->angle = fmodf(platform_building->angle, TAUf);

		for (u32 i = 0; i < input->nmouse_presses; ++i) {
			MousePress *press = &input->mouse_presses[i];
			u8 button = press->button;

			if (button == MOUSE_LEFT) {
				if (mouse_platform) {
					// edit platform
					*platform_building = *mouse_platform;
					platform_building->body = NULL;
					platform_delete(state, mouse_platform);
					platform_building->color = (platform_building->color & 0xFFFFFF00) | 0x7F;
				} else {
					// left-click to build platform
					if (state->nplatforms < MAX_PLATFORMS) {
						platform_building->start_angle = platform_building->angle;
						Platform *p = &state->platforms[state->nplatforms++];
						*p = *platform_building;
						p->color |= 0xFF; // set alpha to 255
						platform_make_body(state, p, state->nplatforms - 1);
						state->setting_move_p2 = false;
						platform_building->moves = false;
					}
				}
			} else if (button == MOUSE_RIGHT) {
				if (mouse_platform) {
					// right-click to delete platform
					platform_delete(state, mouse_platform);
				}
			}
		}

		if (keys_pressed[KEY_SPACE]) {
			state->building = false;
			state->simulating = true;
			setup_reset(state);
		}
	}


	if (state->start_menu) {
		if (state->pressed_any_key_to_begin) {
			// if any key was pressed last frame, show evolve menu now
			state->evolve_menu = true;
			state->start_menu = false;
			start_evolution(state);
		}

		if (input->nkey_presses) {
			state->pressed_any_key_to_begin = true;
		}

		glColor3f(1,0,1);
		char text[128] = {};
		snprintf(text, sizeof text - 1, "Boxcatapult2D");
		v2 size = text_get_size(state, large_font, text);
		v2 pos = V2(-size.x * 0.5f, 0.98f);
		pos.y -= size.y;
		text_render(state, large_font, text, pos);
		
		{ // platform underline
			ShaderPlatform *shader = &state->shader_platform;
			float thickness = 0.005f;
			float radius = size.x * 0.5f + 0.05f;
			shader_start_using(gl, &shader->base);
			gl->Uniform1f(shader->uniform_thickness, thickness);
			gl->UniformMatrix4fv(shader->uniform_transform, 1, GL_FALSE, m4_identity.e);
			glBegin(GL_QUADS);
			float e1x = -radius, e1y = pos.y - 0.01f;
			float e2x = +radius, e2y = e1y;
			gl->VertexAttrib2f(shader->vertex_p1, e1x, e1y);
			gl->VertexAttrib2f(shader->vertex_p2, e2x, e2y);
			glVertex2f(e1x, e1y - thickness * 0.5f);
			glVertex2f(e2x, e2y - thickness * 0.5f);
			glVertex2f(e2x, e2y + thickness * 0.5f);
			glVertex2f(e1x, e1y + thickness * 0.5f);
			glEnd();
			shader_stop_using(gl);
		}
		
		glColor3f(1,1,1);
		snprintf(text, sizeof text - 1, "%s", state->pressed_any_key_to_begin ? "Loading..." : "Press any key to begin.");
		size = text_get_size(state, font, text);
		pos = V2(-size.x * 0.5f, -size.y * 0.5f);
		text_render(state, font, text, pos);
	} else if (state->evolve_menu) {
		// handle input
		if (!state->evolving && keys_pressed[KEY_SPACE]) {
			start_generation(state);
			state->run_one_generation = true;
		}

		if (keys_pressed[KEY_P]) {
			if (state->evolving) {
				state->evolving = false;
			} else {
				start_generation(state);
				state->run_one_generation = false;
			}
		}

		char text[128] = {};
		// show generation
		snprintf(text, sizeof text - 1, "Generation %llu", (ullong)state->generation);
		v2 size = text_get_size(state, font, text);
		v2 pos = V2(-size.x * 0.5f, 0.98f);
		pos.y -= size.y * 1.5f;
		if (state->evolving)	
			glColor3f(0.5f,1,0.5f);
		else
			glColor3f(1,1,1);
		text_render(state, font, text, pos);

		if (state->evolving) {
			snprintf(text, sizeof text - 1, "(running %u/%u)", 
				(uint)state->scoring_next, (uint)GENERATION_SIZE);
		} else {
			snprintf(text, sizeof text - 1, "(stopped)");
		}
		size = text_get_size(state, font, text);
		pos.y -= size.y * 1.5f;
		pos.x = -size.x * 0.5f;
		text_render(state, font, text, pos);

		pos.y -= 0.1f;
		for (int i = 0; i < 9; ++i) {
			Setup *setup = &state->setups[i];
			snprintf(text, sizeof text - 1, "%d. %.2fm in %.1fs (mutated %llu times)",
				i+1, setup->score, setup->total_time, (ullong)setup->mutations);
			size = text_get_size(state, font, text);
			pos.x = -size.x * 0.5f;
			pos.y -= size.y * 1.5f;

			Rect r = rect(pos, size);
			if (rect_contains_point(r, state->mouse_pos_gl))
				glColor3f(0.5f, 0.5f, 1);
			else
				glColor3f(1.0f, 0.8f, 1.0f);

			for (MousePress *press = input->mouse_presses, *end = press + input->nmouse_presses; press != end; ++press) {
				if (rect_contains_point(r, pixels_to_gl_coords(state, press->x, press->y))) {
					// clicked on this setup
					setup_use(state, &state->setups[i]);
					state->evolve_menu = false;
					state->evolving = false;
					state->simulating = true;
				}
			}
			text_render(state, font, text, pos);
		}

		gl_color1f(0.8f);
		if (!state->evolving) {
			snprintf(text, sizeof text - 1, "Press space to run a single generation.");
			size = text_get_size(state, font, text);
			pos.x = -size.x * 0.5f; pos.y -= size.y * 1.5f;
			text_render(state, font, text, pos);
		}

		snprintf(text, sizeof text - 1, "Press P to %s running generations automatically.", state->evolving ? "stop" : "start");
		size = text_get_size(state, font, text);
		pos.x = -size.x * 0.5f; pos.y -= size.y * 1.5f;
		text_render(state, font, text, pos);

		snprintf(text, sizeof text - 1, "Press a number key/click to view the corresponding catapult.");
		size = text_get_size(state, font, text);
		pos.x = -size.x * 0.5f; pos.y -= size.y * 1.5f;
		text_render(state, font, text, pos);

		for (int i = 0; i < 9; ++i) {
			if (keys_pressed[KEY_1 + i]) {
				setup_use(state, &state->setups[i]);
				state->evolve_menu = false;
				state->evolving = false;
				state->simulating = true;
			}
		}
		
		if (state->evolving) {
			// score some setups!
			struct timespec start_time = time_get();
			do {
				bool new_generation = score_one(state);
				if (new_generation) {
					if (state->run_one_generation) {
						state->evolving = false;
					}
				}
			} while (timespec_sub(time_get(), start_time) < 0.02f); // after 20ms, stop.
		}

	} else {
		u32 prev_mouse_platform_color = mouse_platform ? mouse_platform->color : 0;
		if (state->building) {
			// turn platform under mouse blue
			if (mouse_platform) mouse_platform->color = 0x007FFFFF;
		}
		platforms_render(state, state->platforms, state->nplatforms);
		if (state->building) {
			if (mouse_platform) {
				mouse_platform->color = prev_mouse_platform_color;
			} else {
				platforms_render(state, &state->platform_building, 1);
			#if 1
				{ // show rightmost x coordinate of platform
					glBegin(GL_LINES);
					v2 line_pos = V2(platform_rightmost_x(&state->platform_building), 0);
					float x = b2_to_gl(state, line_pos).x;
					glVertex2f(x, -1);
					glVertex2f(x, +1);
					glEnd();
				}
			#endif
			}
		}

		ball_render(state);

		{
			float bottom_y = m4_mul_v3(state->transform, V3(0, state->bottom_y, 0)).y;
			float left_x = m4_mul_v3(state->transform, V3(state->left_x, 0, 0)).x;

			glBegin(GL_LINES);
			glColor3f(1,0,0);
			// render floor line
			glVertex2f(-1, bottom_y);
			glVertex2f(+1, bottom_y);

			// render left wall line
			glColor3f(0.5f,0.5f,0.5f);
			glVertex2f(left_x, bottom_y);
			glVertex2f(left_x, +1);
			glEnd();

			glBegin(GL_QUADS);
			// floor area
			glColor4f(1,0,0,0.2f);
			glVertex2f(-1, bottom_y);
			glVertex2f(-1, -1);
			glVertex2f(+1, -1);
			glVertex2f(+1, bottom_y);
			// left wall area
			glColor4f(0.5f, 0.5f, 0.5f, 0.2f);
			glVertex2f(-1, +1);
			glVertex2f(left_x, +1);
			glVertex2f(left_x, bottom_y);
			glVertex2f(-1, bottom_y);
			glEnd();

			if (state->simulating) { // starting line & distance traveled
				float starting_line = platforms_starting_line(state->platforms, state->nplatforms);
				float starting_line_gl = b2_to_gl(state, V2(starting_line, 0)).x;
				glBegin(GL_LINES);
				glColor3f(1,1,0);
				glVertex2f(starting_line_gl, bottom_y);
				glVertex2f(starting_line_gl, +1);
				glEnd();

				char dist_text[64] = {0};
				if (ball->body)
					glColor4f(0.8f,0.8f,0.8f,0.8f); // still going
				else
					glColor4f(0.5f,1,0.5f,1.0f); // done
				snprintf(dist_text, sizeof dist_text - 1, "Distance: %.2f m", ball->pos.x - starting_line);
				v2 dist_size = text_get_size(state, font, dist_text);

				char best_text[64] = {0};
				snprintf(best_text, sizeof best_text - 1, "Best distance: %.2f m", state->furthest_ball_x_pos - starting_line);
				v2 best_size = text_get_size(state, font, best_text);


				v2 pos = V2(0.98f - maxf(dist_size.x, best_size.x), 0.98f);
				pos.y -= dist_size.y;
				text_render(state, font, dist_text, pos);
				pos.y -= best_size.y;
				text_render(state, font, best_text, pos);
				
				if (!ball->body) {
					// done simulating, show instructions for what to do next
					char text1[64] = {}, text2[64] = {};
					glColor3f(1,1,1);
					snprintf(text1, sizeof text1-1, "Press escape to go back to evolution.");
					snprintf(text2, sizeof text2-1, "Press space to edit this catapult.");
					v2 size1 = text_get_size(state, font, text1);
					v2 size2 = text_get_size(state, font, text2);
					pos = V2(-size1.x * 0.5f, -(size1.y + size2.y) * 1.5f * 0.5f);
					pos.y -= size1.y * 1.5f;
					text_render(state, font, text1, pos);
					pos.x = -size2.x * 0.5f;
					pos.y -= size2.y * 1.5f;
					text_render(state, font, text2, pos);
				}

			}

		}

		{ // details in the bottom right
			char text[64] = {0};
			glColor3f(0.5f,0.5f,0.5f);
			// position of ball
			snprintf(text, sizeof text - 1, "(%.2f, %.2f)", ball->pos.x, ball->pos.y);
			v2 size = text_get_size(state, small_font, text);
			v2 pos = V2(1 - size.x, -1 + size.y);
			text_render(state, small_font, text, pos);
			// stuck time
			snprintf(text, sizeof text - 1, "Last record: %.1fs ago", state->stuck_time);
			size = text_get_size(state, small_font, text);
			pos.x = 1 - size.x;
			pos.y += size.y * 1.5f;
			text_render(state, small_font, text, pos);
			// total time
			snprintf(text, sizeof text - 1, "Total time: %.1fs", state->total_time);
			size = text_get_size(state, small_font, text);
			pos.x = 1 - size.x;
			pos.y += size.y * 1.5f;
			text_render(state, small_font, text, pos);
		}

	}

	#if DEBUG
	GLuint error = glGetError();
	if (error) {
		printf("!!! GL ERROR: %u\n", error);
	}
	#endif
}