summaryrefslogtreecommitdiff
path: root/src/main.c
blob: 4645ad7c370684d3669ebc7825812761bcf06607 (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
#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"
#include "extend/scene.h"

#define TITLE "Soft Shade Room"

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

static char* instruction = 
"/*********************************************/\n"
"/*  SoftShadeRoom                            */\n"
"/*                                      chai */\n"
"/*********************************************/\n"
"Instructions:\n"
"RightMouseButton+MouseMove:  look around\n"
"MiddleMouseButton+MouseMove: move around\n"
"MouseScroll:                 zoom in\\zoom out\n"
"Shift+Above Operation:       speed up\n"
"key-g:                       show\\hide grid\n"
"----------------------------------------------\n";

typedef void(*F)(void*);

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

#define SET_EXAMPLE_FUNC(f, e) \
	f = f##_##e;

#define SET_EXAMPLE(i) \
	SET_EXAMPLE_FUNC(onload, i)\
	SET_EXAMPLE_FUNC(ondraw, i)\
	SET_EXAMPLE_FUNC(onevent, i)\
	SET_EXAMPLE_FUNC(onupdate, i)

int main(int argc, char* argv[]) {
	printf(instruction);

	SET_EXAMPLE(CURRENT_EXAMPLE);
	wog_Window* wnd = wog_createWindow(TITLE, SCREEN_WIDTH, SCREEN_HEIGHT, 500, 500, 0);
	wog_Surface* surface = wog_getsurface(wnd); // ARGB format
	/* init ssr */
	ssr_Config config = {
		SCREEN_WIDTH, SCREEN_HEIGHT,
		0,
		surface->buffer
	};
	ssr_init(&config);
	/*set up global camera*/
	CameraConfig cam_config = {
		{0, 10, 800},
		{0, 0, 0},
		60, SCREEN_WIDTH / SCREEN_HEIGHT, 0.1, 100000,
		{5, 5}, {150, 150},
		4000,
	};
	onload(&cam_config);
	scene.main_camera = camera_create(wnd, &cam_config);
	gizmo_init();
	wog_show(wnd);

	_screen_params.x = SCREEN_WIDTH;
	_screen_params.y = SCREEN_HEIGHT;
	_screen_params.z = 1 / SCREEN_WIDTH;
	_screen_params.w = 1 / SCREEN_HEIGHT;

	/* 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(scene.main_camera, &e, _dt);
			gizmo_onevent(&e, _dt);
			if (e.type == WOG_ECLOSE) {
				goto quit;
			}
			else if (e.type == WOG_EKEYDOWN) {
				if (e.key == VK_ESCAPE) {
					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(scene.main_camera, _dt);
		gizmo_onupdate(_dt);
		onupdate(&_dt);
		
		/*draw*/
		camera_ondraw(scene.main_camera);
		ondraw(NULL);
		gizmo_ondraw();

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

	return 0;
}