summaryrefslogtreecommitdiff
path: root/src/main.c
blob: 1dd4663b0a5984e3f8a5162f2cd4890cd736892a (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
#include <SDL2/SDL.h>
#include <stdio.h>
#include <windows.h>
#include "math/math.h"
#include "util/assert.h"
#include "ssr.h"
#include "example/example.h"

SDL_Surface* suf;

#define SCREEN_WIDTH 600 /*800*/
#define SCREEN_HEIGHT 500 /*600*/

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[])
{
	if (SDL_Init(SDL_INIT_EVERYTHING) < 0)
		return 1;
	SDL_Window* wnd = SDL_CreateWindow("Soft Shade Room", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, SCREEN_WIDTH, SCREEN_HEIGHT, SDL_WINDOW_SHOWN);
	SDL_Event e;
	suf = SDL_GetWindowSurface(wnd);
	/* ARGB format */
	ssr_assert(suf->format->BitsPerPixel == 32);
	ssr_assert(suf->format->Rshift == 16);
	ssr_assert(suf->format->Gshift == 8);
	ssr_assert(suf->format->Bshift == 0);
	/* init ssr */
	ssr_Config config = {
		SCREEN_WIDTH, SCREEN_HEIGHT,/* screen size */
		0, /* double buffer */
		suf->pixels /* screen buffer */ 
	};
	ssr_init(&config);
	SETEXAMPLE(EXAMPLECUR);
	onload(0);
	/* main loop */
	uint previous = SDL_GetTicks();
	uint dt = 0;
	while (1) {
		while (SDL_PollEvent(&e)) {
			if (e.type == SDL_QUIT) {
				goto quit;
			} else {
				onevent(&e);
			}
		}
		dt = SDL_GetTicks() - previous;
		previous = dt + previous;
		onupdate(&dt);
		ondraw(0);
		ssr_present();
		SDL_UpdateWindowSurface(wnd);
		Sleep(10); /*100fps limit*/
	}
	quit:
	SDL_Quit();
	return 0;
}