-
Notifications
You must be signed in to change notification settings - Fork 0
/
ui-arlib.cpp
209 lines (178 loc) · 5.96 KB
/
ui-arlib.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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
#include "game.h"
int main(int argc, char** argv)
{
static uint32_t pixels[640*480]; // static to keep the stack frame small
image out; // initialized up here so perf-ui can use it too
out.init_ptr(pixels, 640, 480, sizeof(uint32_t)*640, ifmt_xrgb8888); // xrgb is faster than 0rgb
#ifndef STDOUT_ERROR
argparse args;
bool do_bench_solv = false;
args.add("perf-solv", &do_bench_solv);
bool do_bench_ui = false;
args.add("perf-ui", &do_bench_ui);
args.parse(argv);
if (do_bench_solv && do_bench_ui)
{
puts("can't benchmark both solver and UI");
return 1;
}
if (do_bench_solv)
{
g_rand.seed(0);
bool vg = RUNNING_ON_VALGRIND;
uint64_t start = time_ms_ne();
int nsolv = 0;
int nunsolv = 0;
int nmultisolv = 0;
for (int side=3;side<10;side++)
{
for (int i=0;i<(vg ? 1000 : 10000);i++) // ignore benchmark, we want iteration count constant between runs
{
if (i%(vg ? 10 : 100) == 0) { printf("%i %i \r", side, i); fflush(stdout); }
gamemap::genparams par = {};
par.width = side;
par.height = side;
par.density = 0.4;
par.dense = true;
par.use_reef = false;
par.use_large = false;
par.use_castle = false;
par.allow_ambiguous = true;
par.difficulty = 0.0;
par.quality = 1;
gamemap map;
map.generate(par);
map.reset();
int result = 0;
if (map.solve())
{
result = 1;
if (map.solve_another()) result = 2;
}
if (result == 0)
{
puts("ERROR");
puts(map.serialize());
exit(1);
}
if (result == 0) nunsolv++;
if (result == 1) nsolv++;
if (result == 2) nmultisolv++;
}
}
uint64_t end = time_ms_ne();
//expected (with side = 3..9, i = 0..9999, generate.cpp md5 f32cac429a654b96cc327086541aac4b):
//41535 solvable, 0 unsolvable, 28465 multiple solutions, took 7266 ms
printf("%i solvable, %i unsolvable, %i multiple solutions, took %u ms\n",
nsolv, nunsolv, nmultisolv, (unsigned)(end-start));
return 0;
}
if (do_bench_ui)
{
game* g = game::create();
game::input in = {};
game::input in_press = {};
in_press.keys = 1 << game::k_confirm;
using (benchmark b)
{
while (b) g->run(in, out);
printf("title screen: %u frames in %ums = %ffps\n", b.iterations, b.us/1000, b.per_second());
}
g->run(in_press, out);
using (benchmark b)
{
while (b) g->run(in, out);
printf("level select: %u frames in %ums = %ffps\n", b.iterations, b.us/1000, b.per_second());
}
g->run(in_press, out);
using (benchmark b)
{
while (b) g->run(in, out);
printf("ingame: %u frames in %ums = %ffps\n", b.iterations, b.us/1000, b.per_second());
}
return 0;
}
#endif
aropengl gl;
autoptr<gameview> gv = gameview::create(gl, 640, 480, aropengl::t_ver_1_0/*|aropengl::t_direct3d_vsync*/, "bridges");
gl.swapInterval(1);
gl.MatrixMode(GL_PROJECTION);
gl.LoadIdentity();
gl.Ortho(0, 640, 0, 480, -1.0, 1.0);
gl.Viewport(0, 0, 640, 480);
gl.MatrixMode(GL_MODELVIEW);
gl.LoadIdentity();
gl.Enable(GL_TEXTURE_2D);
GLuint tex;
gl.GenTextures(1, &tex);
gl.BindTexture(GL_TEXTURE_2D, tex);
gl.TexImage2D(GL_TEXTURE_2D, 0, GL_RGB8, 1024, 512, 0, GL_BGRA, GL_UNSIGNED_INT_8_8_8_8_REV, NULL);
gl.TexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
gl.TexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
// save support not implemented here, just hardcode something reasonable
// the long-term plan has always been making this game a libretro core, this frontend is mostly a far-too-long-lived placeholder
static const uint8_t save[] = { 31,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 };
autoptr<game> g = game::create(save);
game::input in = {};
gv->keys_cb([&in](int scancode, gameview::key_t key, bool down) {
//printf("key %d,%d,%d\n",scancode,key,down);
int bit;
if(0);
else if (key == gameview::K_RIGHT || key == gameview::K_d) bit = game::k_right;
else if (key == gameview::K_UP || key == gameview::K_w) bit = game::k_up;
else if (key == gameview::K_LEFT || key == gameview::K_a) bit = game::k_left;
else if (key == gameview::K_DOWN || key == gameview::K_s) bit = game::k_down;
else if (key == gameview::K_RETURN || key == gameview::K_SPACE) bit = game::k_confirm;
else if (key == gameview::K_ESCAPE || key == gameview::K_BACKSPACE) bit = game::k_cancel;
else return;
in.keys = (in.keys & ~(1<<bit)) | (down << bit);
});
gv->mouse_cb([&in](int x, int y, uint8_t buttons) {
//printf("mouse %d,%d,%d\n",x,y,buttons);
in.mousex = x;
in.mousey = y;
in.lmousedown = (buttons&1);
in.rmousedown = (buttons&2);
});
bool first = true;
bool active = true;
//time_t t=0;
//int fps=0;
while (gv->running())
{
//fps++;
//if (t != time(NULL))
//{
//printf("%dfps\n",fps);
//fps = 0;
//t = time(NULL);
//}
if (active)
{
#if defined(ARLIB_OPT) && !defined(STDOUT_ERROR)
timer t;
#endif
int do_upload = g->run(in, out, !first);
first = false;
#if defined(ARLIB_OPT) && !defined(STDOUT_ERROR)
if (t.us() >= 2500)
printf("rendered in %uus\n", (unsigned)t.us());
#endif
if (do_upload > 0) gl.TexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, 640, 480, GL_BGRA, GL_UNSIGNED_INT_8_8_8_8_REV, pixels);
active = (do_upload >= 0);
}
//redraw the texture, to avoid glitches if compositor discarded our pixels
//no point reuploading, and no point checking why gv->tmp_step returned, just redraw
gl.Clear(GL_COLOR_BUFFER_BIT); // does nothing, but docs say it makes things faster
gl.Begin(GL_TRIANGLE_STRIP);
gl.TexCoord2f(0, 0 ); gl.Vertex3i(0, 480, 0);
gl.TexCoord2f(640.0/1024.0, 0 ); gl.Vertex3i(640, 480, 0);
gl.TexCoord2f(0, 480.0/512.0); gl.Vertex3i(0, 0, 0);
gl.TexCoord2f(640.0/1024.0, 480.0/512.0); gl.Vertex3i(640, 0, 0);
gl.End();
gl.swapBuffers();
gv->step(!active);
active = (gv->focused()); // keep them in this order, so the pause-if-inactive implementation works
}
return 0;
}