-
Notifications
You must be signed in to change notification settings - Fork 7
/
main.cpp
154 lines (127 loc) · 3.76 KB
/
main.cpp
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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
// Copyright 2015 Red Blob Games <[email protected]>
// License: Apache v2.0 <http://www.apache.org/licenses/LICENSE-2.0.html>
#include "common.h"
#include "glwrappers.h"
#include "window.h"
#include "render-sprites.h"
#include "render-shapes.h"
#include "render-surface.h"
#include "render-imgui.h"
#include "font.h"
#include <SDL.h>
#ifdef __EMSCRIPTEN__
#include <emscripten.h>
#endif
#define SHOW_SPRITES 1
#define SHOW_SHAPES 0
#define SHOW_IMGUI 1
#define SHOW_OVERLAY 1
std::unique_ptr<Window> window;
std::unique_ptr<RenderSprites> sprite_layer;
std::unique_ptr<RenderShapes> shape_layer;
static bool main_loop_running = true;
Triangle tri(float x1, float y1, float x2, float y2, float x3, float y3) {
return Triangle{{x1, x2, x3}, {y1, y2, y3}};
}
void main_loop() {
SDL_Event event;
while (SDL_PollEvent(&event)) {
switch (event.type) {
case SDL_QUIT: {
main_loop_running = false;
break;
}
case SDL_KEYUP: {
int sym = event.key.keysym.sym;
if (sym == SDLK_ESCAPE) { main_loop_running = false; }
break;
}
}
window->ProcessEvent(&event);
}
if (window->visible) {
static float t = 0.0;
t += 0.01f;
#if SHOW_SPRITES
{
std::vector<Sprite> sprites;
int SIDE = 4; // Try changing to 100; can't have more than 128 though because I use GLushort somewhere
int NUM = SIDE * SIDE;
for (int j = 0; j < NUM; j++) {
sprites.emplace_back();
auto& s = sprites.back();
s.image_id = 0;
s.x = (0.5f + j % SIDE - 0.5f*SIDE + ((j/SIDE)%2) * 0.5f - 0.25f) * 2.0f / SIDE;
s.y = (0.5f + j / SIDE - 0.5f*SIDE) * 2.0f / SIDE;
s.scale = 2.0f / SIDE;
s.rotation_degrees = j * 0.03f * t * DEG_TO_RAD;
}
sprite_layer->SetSprites(sprites);
}
#endif
#if SHOW_SHAPES
{
std::vector<Shape> shapes;
Shape s;
s.r = 1.0; s.g = 0.5; s.b = 0.5; s.a = 1.0;
s.triangles = {
tri(0, -3, 1, -1, 0.5, 0),
tri(1, -1, 3, 0, 0.5, 0),
tri(3, 0, 1, 1, 0.5, 0),
tri(1, 1, 0, 3, 0.5, 0),
tri(0, 3, 0, 1, 0.5, 0),
tri(0, 1, -1, 1, 0.5, 0),
tri(-1, 1, -1, -1, 0.5, 0),
tri(-1, -1, 0, -1, 0.5, 0),
tri(0, -1, 0, -3, 0.5, 0)
};
shapes.push_back(s);
shape_layer->SetShapes(shapes);
}
#endif
window->Render();
}
}
int main(int, char**) {
if (SDL_Init(SDL_INIT_VIDEO) < 0) { FAIL("SDL_Init"); }
SDL_GL_SetSwapInterval(1);
window = std::unique_ptr<Window>(new Window(800, 600));
Font font("imgui/misc/fonts/DroidSans.ttf", 32);
SDL_Surface* overlay_surface = CreateRGBASurface(window->width, window->height);
SDL_Rect fillarea;
fillarea.x = 0;
fillarea.y = 0;
fillarea.w = 2 + font.Width("Hello world");
fillarea.h = font.Height();
SDL_FillRect(overlay_surface, &fillarea, SDL_MapRGBA(overlay_surface->format, 64, 32, 0, 192));
font.Draw(overlay_surface, 1, font.Baseline(), "Hello world");
#if SHOW_SPRITES
sprite_layer = std::unique_ptr<RenderSprites>(new RenderSprites);
window->AddLayer(sprite_layer.get());
#endif
#if SHOW_SHAPES
shape_layer = std::unique_ptr<RenderShapes>(new RenderShapes);
window->AddLayer(shape_layer.get());
#endif
#if SHOW_OVERLAY
std::unique_ptr<RenderSurface> overlay_layer(new RenderSurface(overlay_surface));
window->AddLayer(overlay_layer.get());
#endif
#if SHOW_IMGUI
std::unique_ptr<RenderImGui> ui_layer(new RenderImGui());
window->AddLayer(ui_layer.get());
#endif
#ifdef __EMSCRIPTEN__
// 0 fps means to use requestAnimationFrame; non-0 means to use setTimeout.
emscripten_set_main_loop(main_loop, 0, 1);
#else
while (main_loop_running) {
main_loop();
SDL_Delay(16);
}
#endif
sprite_layer = nullptr;
shape_layer = nullptr;
window = nullptr;
SDL_Quit();
}