summaryrefslogtreecommitdiff
path: root/src/main.c
blob: b0ee5dfa25d9dbfcab5eaa0ec94e366174f9e0e1 (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
#include <stdio.h>
#include <windows.h>
#include "math/math.h"
#include "util/assert.h"
#include "ssr.h"
#include "example/example.h"
#include "extern/wog.h"
#include "shaders/common.h"
#include "extend/camera.h"

#define SCREEN_WIDTH 600.f
#define SCREEN_HEIGHT 480.f

typedef void(*F)(void*);

F onload;
F onupdate;
F onevent;
F ondraw;

/*https://stackoverflow.com/questions/1489932/how-to-concatenate-twice-with-the-c-preprocessor-and-expand-a-macro-as-in-arg*/
#define SETEXAMPLEF(f, e) \
	f = f##_##e;

#define SETEXAMPLE(i) \
	SETEXAMPLEF(onload, i)\
	SETEXAMPLEF(ondraw, i)\
	SETEXAMPLEF(onevent, i)\
	SETEXAMPLEF(onupdate, i)

int main(int argc, char* argv[]) {
	wog_Window* wnd = wog_createWindow("Soft Shade Room", SCREEN_WIDTH, SCREEN_HEIGHT, 500, 500, 0);
	wog_Surface* surface = wog_getsurface(wnd); // ARGB format
	Camera* camera;
	/* init ssr */
	ssr_Config config = {
		SCREEN_WIDTH, SCREEN_HEIGHT,
		0,
		surface->buffer
	};
	ssr_init(&config);
	SETEXAMPLE(EXAMPLECUR);
	/*set up global camera*/
	CameraConfig cam_config = { /*default camera setting*/
		{0, 0, 800},
		{0, 0, 0},
		60, SCREEN_WIDTH / SCREEN_HEIGHT, 0.1, 1500,
		{5, 5}, {150, 150},
		4000,
	};
	onload(&cam_config);
	camera = camera_create(wnd, &cam_config);
	wog_show(wnd);

	/* main loop */
	uint prev = wog_tick();
	uint dt = 0;
	float _dt = 0, _duration;
	uint frame_count = 0;
	uint time_stamp = 0;
	wog_Event e;
	while (1) {
		/*handle events*/
		while (wog_pollEvent(wnd, &e)) {
			camera_onevent(camera, &e, _dt);
			if (e.type == WOG_ECLOSE) {
				goto quit;
			} else {
				onevent(&e);
			}
		}
	
		/*frame count*/
		dt = wog_tick() - prev;
		prev += dt;
		time_stamp += dt;
		++frame_count;
		if (time_stamp >= 1000) {
			printf("%3d fps\n", frame_count);
			time_stamp -= 1000;
			frame_count = 0;
		}

		_time.x = _dt;
		_time.y = prev / 1000.f;
	
		/*update*/
		_dt = dt / 1000.f;
		camera_onupdate(camera, _dt);
		onupdate(&_dt);
		
		/*draw*/
		camera_ondraw(camera);
		ondraw(NULL);

		ssr_present();
		wog_updateSurface(wnd);
		//Sleep(1); /*reduce cpu using*/
	}
	
	quit:
	camera_destroy(camera);
	wog_destroyWindow(wnd);

	return 0;
}