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
|
#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 "extend/camera.h"
#define SCREEN_WIDTH 600
#define SCREEN_HEIGHT 500
typedef void(*F)(void*);
F onload;
F onupdate;
F onevent;
F ondraw;
/*macro这里需要绕一下弯*/
/*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 Shader Room", SCREEN_WIDTH, SCREEN_HEIGHT, 500, 500, 0);
wog_show(wnd);
wog_Surface* surface = wog_getsurface(wnd); // ARGB format
/* init ssr */
ssr_Config config = {
SCREEN_WIDTH, SCREEN_HEIGHT,
0,
surface->buffer
};
ssr_init(&config);
SETEXAMPLE(EXAMPLECUR);
onload(NULL);
/*set up global camera*/
Camera cam;
camera_init(&cam, wnd);
/* main loop */
uint prev = wog_tick();
uint dt = 0;
float _dt = 0;
uint frame_count = 0;
uint time_stamp = 0;
wog_Event e;
while (1) {
/*handle events*/
while (wog_pollEvent(wnd, &e)) {
camera_onevent(&cam, &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;
}
/*update*/
_dt = dt / 1000.f;
camera_onupdate(&cam, _dt);
onupdate(&_dt);
/*set vp matrix*/
ssr_matrixmode(MATRIX_PROJECTION);
camera_getprojmatrix(&cam, NULL);
ssr_loadmatrix(&cam.proj_matrix);
ssr_matrixmode(MATRIX_VIEW);
camera_getviewmatrix(&cam, NULL);
ssr_loadmatrix(&cam.view_matrix);
ssr_matrixmode(MATRIX_MODEL);
/*draw*/
ondraw(NULL);
ssr_present();
wog_updateSurface(wnd);
//Sleep(1); /*reduce cpu using*/
}
quit:
wog_destroyWindow(wnd);
return 0;
}
|