summaryrefslogtreecommitdiff
path: root/3de.c
blob: 1e40f91d1d1b0230b4fba5bb59119cbc34c9beae (plain) (blame)
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
#include "asset.h"
#include "config.h"
#include "map.h"
#include "maths.h"
#include "memory.h"
#include "plat.h"
#include "player.h"
#include "render.h"
#include "services.h"
#include "tile.h"

#include <stdio.h>

void draw_spinny(Renderer* r, Colour c) {
	static int a = 0;
	int y; int x = y = 100 << fbits;
	x -= sin_table[a & sin_table_mask] * 10;
	y += cos_table[a & sin_table_mask] * 10;
	ren_point(r, c, x >> fbits, y >> fbits);
	a += 20;
}

void draw_gun(Renderer* r) {
	static int a = 0;
	const Mesh* m = get_mesh(asset_id_monkey);
	const Texture* tex = get_texture(asset_id_gun_texture);
	int p[] = { f1 * 7, 0, f1 * 5, f1 };
	mtx_push_trans(p);
	/*mtx_push_rot_x(a);
	mtx_push_rot_y(a);
	mtx_push_rot_z(a);*/
	ren_mesh(r, m, tex);
	mtx_popn(1);
	a++;
}

void draw_tri(Renderer* r, int x, int y) {
	const Texture* tex = get_texture(asset_id_gun_texture);
	int v0[] = { 100, 100, 0, 0,  0,    f1, f1,  f1  };
	int v1[] = { 200, 200, 0, f1, f1/2, f1,  f1, f1  };
	int v2[] = { 150, 300, 0, 0,  f1,   f1,  f1,  f1 };
	v1[0] = x;
	v1[1] = y;
	ren_tri(r, v0, v1, v2, tex);
}

void draw_line(Renderer* r, int x, int y) {
	int s[] = { 100, 100 }, e[2];
	e[0] = x;
	e[1] = y;
	ren_line(r, make_red(), s, e);
}

void draw_map(Renderer* r, Player* p) {
	int data[512] = {
		16, 16,
		1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
		1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1,
		1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1,
		1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1,
		1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1,
		1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1,
		1, 0, 0, 1, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 1,
		1, 0, 0, 1, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 1,
		1, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 1,
		1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1,
		1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1,
		1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1,
		1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1,
		1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1,
		1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1,
		1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
	};
	int pos[2];
	Map* m = (Map*)data;
	pos[0] = p->p[0];
	pos[1] = p->p[2];
	ren_map(r, m, pos, p->f, p->l);
}

int entrypoint(int argc, const char** argv, Arena* a) {
	App* app;
	Renderer r = { 0 };
	Heap h;
	FPS f;
	char buf[32];
	int* depth = 0;
	Arena preload;
	Colour blue = make_aliceblue();
	Player p;
	Services s;
	(void)argc;
	(void)argv;
	init_maths();
	init_heap(
		&h,
		arena_alloc(a, app_memory_size),
		app_memory_size
	);
	init_arena(
		&preload,
		arena_alloc(a, asset_preload_memory_size),
		asset_preload_memory_size
	);
	app = new_app(&h, 640, 480, "3D Engine");
	if (!app) return app->err;
	init_fps(&f, 20);
	depth = heap_alloc(&h, max_vp_w * max_vp_h * sizeof *depth);
	if (!depth) {
		print_err("Out of memory.\n");
		return error_out_of_memory;
	}
	preload_assets(&preload);
	s.a = app;
	s.r = &r;
	cfg_mouse(app, 0);
	init_player(&p);
	while (app->o) {
		fps_begin(&f);
		while (f.now >= f.next) {
			app_begin(app);
			update_player(&p, &s);
			ren_begin(&r, app->fb, depth, app->w, app->h);
				ren_clear(&r);
				ren_cleard(&r, f1 * 300);
/*				draw_line(&r, app->mx, app->my);*/
				push_player_cam(&p);
					draw_gun(&r);
					draw_map(&r, &p);
				pop_player_cam();
				/*draw_tri(&r, app->mx, app->my);*/
				sprintf(buf, "FPS: %d", app->fps);
				ren_texts(&r, blue, 3, 3,  buf);
				sprintf(buf, "CAP: %d", f.fps);
				ren_texts(&r, blue, 3, 8,  buf);
				sprintf(buf, "MOUSE: %d, %d", app->mx, app->my);
				ren_texts(&r, blue, 3, 13, buf);
			ren_end(&r);
			app_end(app);
			fps_update(&f);
		}
		fps_end(&f);
	}
	deinit_app(app);
	return 0;
}