aboutsummaryrefslogtreecommitdiff
path: root/sim.c
blob: b696692e717385bce5ddab472e62785538cd1d5c (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
/* #include <stdio.h> */
#include <math.h>
#include <SDL2/SDL.h>

#include "sim.h"
#include "draw.h"
#include "types.h"

#define MOUSE_LOCK 0

float hfov, vfov;
float PI;
Camera cam;
uint fps = 30;

SDL_Window *win;
void *ren;

typedef struct{
	SDL_Keymod mod;
	SDL_Keycode sym;
	void (*func)(const Arg *);
	const Arg arg;
} Key;

int
main_init()
{
	if (SDL_Init(SDL_INIT_VIDEO) < 0) {
		fprintf(stderr, "%s:%d:main_init() error %s", __FILE__, __LINE__, SDL_GetError());
		return 1;
	}

	win = SDL_CreateWindow("3D Sim",
			SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED,
			2*w, 2*h, SDL_WINDOW_RESIZABLE);
	if (win == NULL) {
		fprintf(stderr, "%s:%d:main_init() error %s", __FILE__, __LINE__, SDL_GetError());
		return 1;
	}

	ren = SDL_CreateRenderer(win, -1, 0);
	if (ren == NULL) {
		fprintf(stderr, "%s:%d:main_init() error %s", __FILE__, __LINE__, SDL_GetError());
		return 1;
	}
	SDL_RenderSetLogicalSize(ren, w, h);
	/* SDL_RenderSetScale(ren, 2, 2); */

	if (draw_init())
		return 1;

	return 0;
}

int
main_quit()
{
	SDL_DestroyWindow(win);
	SDL_Quit();
	return 1;
}

int
main_event()
{
	static SDL_Event event;
	static uint inp = MOUSE_LOCK * 1024;
	static double s = 0.5;
	uint8 q = 0;

	while(SDL_PollEvent(&event)) {
		switch (event.type) {
		case SDL_QUIT:
			q = 1;
			break;
		case SDL_KEYDOWN:
			if (event.key.keysym.sym == SDLK_q)
				q = 1;
			switch (event.key.keysym.sym) {
			case SDLK_w:     inp |= 1; break;
			case SDLK_s:     inp |= 2; break;
			case SDLK_a:     inp |= 4; break;
			case SDLK_d:     inp |= 8; break;
			case SDLK_RIGHT: inp |= 16; break;
			case SDLK_LEFT:  inp |= 32; break;
			case SDLK_UP:    inp |= 64; break;
			case SDLK_DOWN:  inp |= 128; break;
			case SDLK_j:     inp |= 256; break;
			case SDLK_k:     inp |= 512; break;
			case SDLK_m:     inp ^= 1024; break;
			case SDLK_i:     add_obj_interactive(); break;
			case SDLK_r:     redo_obj(); break;
			case SDLK_t:     drawlines ^= 1; break;
			case SDLK_EQUALS: s += 0.1; break;
			case SDLK_MINUS: s -= 0.1; break;
			}
			break;
		case SDL_KEYUP:
			switch (event.key.keysym.sym) {
			case SDLK_w:     inp &= (inp^1); break;
			case SDLK_s:     inp &= (inp^2); break;
			case SDLK_a:     inp &= (inp^4); break;
			case SDLK_d:     inp &= (inp^8); break;
			case SDLK_RIGHT: inp &= (inp^16); break;
			case SDLK_LEFT:  inp &= (inp^32); break;
			case SDLK_UP:    inp &= (inp^64); break;
			case SDLK_DOWN:  inp &= (inp^128); break;
			case SDLK_j:     inp &= (inp^256); break;
			case SDLK_k:     inp &= (inp^512); break;
			}
			break;
		case SDL_MOUSEMOTION:
			if (~inp & 1024) break;
			cam.azim -= ((float)event.motion.yrel)/25.0;
			cam.rad  += ((float)event.motion.xrel)/25.0;
			break;
		}
	}

	if (inp&1024)
		SDL_SetRelativeMouseMode(SDL_TRUE);
	else
		SDL_SetRelativeMouseMode(SDL_FALSE);

	if (inp&1) {
		cam.pos.x += cos(cam.rad) * s;
		cam.pos.z += sin(cam.rad) * s;
	}
	if (inp&2) {
		cam.pos.x -= cos(cam.rad) * s;
		cam.pos.z -= sin(cam.rad) * s;
	}
	if (inp&4) {
		cam.pos.x += sin(cam.rad) * s;
		cam.pos.z -= cos(cam.rad) * s;
	}
	if (inp&8) {
		cam.pos.x -= sin(cam.rad) * s;
		cam.pos.z += cos(cam.rad) * s;
	}

	if (inp&256) cam.pos.y -= s;
	if (inp&512) cam.pos.y += s;
	if (inp&16)
		cam.rad += 0.05;
	if (inp&32)
		cam.rad -= 0.05;

	if (inp&64)
		cam.azim += 0.05;
	if (inp&128)
		cam.azim -= 0.05;

	if (cam.azim < 0)
		cam.azim += 2.0 * PI;
	if (cam.azim > 2.0 * PI)
		cam.azim -= 2.0 * PI;

	if (cam.rad < 0)
		cam.rad += 2.0 * PI;
	if (cam.rad > 2.0 * PI)
		cam.rad -= 2.0 * PI;

	if (q)
		return main_quit();

	if (draw_screen())
		return 1;

	return 0;
}

int main(int argc, char *argv[])
{
	Uint32 ltick, fdif;
	cam.pos.x = cam.pos.z = 0;
	cam.pos.y = 10;
	cam.rad = cam.azim = 0;
	PI = 4.0 * atan(1);
	h = 288;
	w = 384;
	hfov = 1;

	if (main_init())
		return 1;

	vfov = 0.75;

	fdif = 1000 / fps;
	while (1) {
		ltick = SDL_GetTicks();
		if (main_event())
			break;
		ltick = SDL_GetTicks() - ltick;

		if (ltick < fdif)
			SDL_Delay(fdif - ltick);
	}

	return 0;
}