summaryrefslogtreecommitdiff
path: root/src/main.c
diff options
context:
space:
mode:
authorchai <chaifix@163.com>2019-12-04 00:07:32 +0800
committerchai <chaifix@163.com>2019-12-04 00:07:32 +0800
commit2e82e2ddd0852b8063a3d6645366f53ee844e273 (patch)
tree41ec10760f2d2c9f1f782a918f48e1287da2a4b4 /src/main.c
+init
Diffstat (limited to 'src/main.c')
-rw-r--r--src/main.c74
1 files changed, 74 insertions, 0 deletions
diff --git a/src/main.c b/src/main.c
new file mode 100644
index 0000000..d06803a
--- /dev/null
+++ b/src/main.c
@@ -0,0 +1,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 500
+#define SCREEN_HEIGHT 400
+
+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_VIDEO) < 0)
+ return 1;
+ SDL_Window* wnd = SDL_CreateWindow("Test", 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;
+}