-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathandroid.cpp
180 lines (145 loc) · 4.78 KB
/
android.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
#include <jni.h>
#include <EGL/egl.h>
#include <GLES/gl.h>
#include <android_native_app_glue.h>
#include <android/window.h>
#include <sys/resource.h>
#include "canvas.hpp"
#include "network_epoll.hpp"
#ifdef USE_FREETYPE
#include "text.hpp"
#include <sstream>
#endif
struct UserData
{
struct android_app* app;
EGLDisplay display;
EGLSurface surface;
EGLContext context;
EGLint width;
EGLint height;
Canvas* canvas;
NetworkHandler* networkHandler;
};
static const float vertices[][4] = {{-1.f,-1.f,+0.f,+1.f},
{+1.f,-1.f,+1.f,+1.f},
{-1.f,+1.f,+0.f,+0.f},
{+1.f,+1.f,+1.f,+0.f}};
void init(UserData* userData)
{
ANativeActivity_setWindowFlags(userData->app->activity, AWINDOW_FLAG_KEEP_SCREEN_ON, 0);
static const EGLint attribs[] = {
EGL_BLUE_SIZE, 8,
EGL_GREEN_SIZE, 8,
EGL_RED_SIZE, 8,
EGL_NONE
};
static const EGLint attribList[] = {
EGL_CONTEXT_CLIENT_VERSION, 1,
EGL_NONE
};
userData->display = eglGetDisplay(EGL_DEFAULT_DISPLAY);
eglInitialize(userData->display, 0, 0);
EGLConfig config;
EGLint numConfigs;
eglChooseConfig(userData->display, attribs, &config, 1, &numConfigs);
EGLint format;
eglGetConfigAttrib(userData->display, config, EGL_NATIVE_VISUAL_ID, &format);
ANativeWindow_setBuffersGeometry(userData->app->window, 0, 0, format);
userData->surface = eglCreateWindowSurface(userData->display, config, userData->app->window, nullptr);
userData->context = eglCreateContext(userData->display, config, nullptr, attribList);
eglMakeCurrent(userData->display, userData->surface, userData->surface, userData->context);
eglQuerySurface(userData->display, userData->surface, EGL_WIDTH, &userData->width);
eglQuerySurface(userData->display, userData->surface, EGL_HEIGHT, &userData->height);
userData->canvas = new Canvas(userData->width, userData->height, new uint32_t[userData->width * userData->height]);
std::fill_n(userData->canvas->data, userData->canvas->width * userData->canvas->height, 0);
#ifdef USE_FREETYPE
writeInfoText(*(userData->canvas), 1234);
#endif
userData->networkHandler = new NetworkHandler(*(userData->canvas), 1234, std::thread::hardware_concurrency());
glViewport(0, 0, userData->width, userData->height);
GLuint tex;
glGenTextures(1, &tex);
glBindTexture(GL_TEXTURE_2D, tex);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glEnable(GL_TEXTURE_2D);
glVertexPointer(2, GL_FLOAT, sizeof(vertices[0]), &(vertices[0][0]));
glTexCoordPointer(2, GL_FLOAT, sizeof(vertices[0]), &(vertices[0][2]));
glEnableClientState(GL_VERTEX_ARRAY);
glEnableClientState(GL_TEXTURE_COORD_ARRAY);
}
void draw_frame(UserData* userData)
{
if (!userData->display) {
return;
}
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, userData->canvas->width, userData->canvas->height, 0, GL_RGBA, GL_UNSIGNED_BYTE, userData->canvas->data);
glDrawArrays(GL_TRIANGLE_STRIP, 0, sizeof(vertices) / sizeof(vertices[0]));
eglSwapBuffers(userData->display, userData->surface);
}
void terminate(UserData* userData)
{
if (userData->display != EGL_NO_DISPLAY) {
eglMakeCurrent(userData->display, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT);
if (userData->context != EGL_NO_CONTEXT) {
eglDestroyContext(userData->display, userData->context);
userData->context = EGL_NO_CONTEXT;
}
if (userData->surface != EGL_NO_SURFACE) {
eglDestroySurface(userData->display, userData->surface);
userData->surface = EGL_NO_SURFACE;
}
eglTerminate(userData->display);
userData->display = EGL_NO_DISPLAY;
}
if (userData->canvas) {
delete userData->networkHandler;
delete[] userData->canvas->data;
delete userData->canvas;
userData->canvas = nullptr;
}
}
void handle_cmd(struct android_app* app, int32_t cmd)
{
UserData* userData = (UserData*)app->userData;
switch (cmd) {
case APP_CMD_INIT_WINDOW:
if (userData->app->window) {
init(userData);
}
break;
case APP_CMD_TERM_WINDOW:
terminate(userData);
break;
case APP_CMD_LOST_FOCUS:
draw_frame(userData);
break;
}
}
extern "C" void android_main(struct android_app* app)
{
// Raise open files limit because Android makes it difficult to do for end users.
struct rlimit rlim;
if (getrlimit(RLIMIT_NOFILE, &rlim) == 0) {
rlim.rlim_cur = rlim.rlim_max;
setrlimit(RLIMIT_NOFILE, &rlim);
}
UserData userData;
memset(&userData, 0, sizeof(userData));
app->userData = &userData;
app->onAppCmd = handle_cmd;
userData.app = app;
// Make sure glue isn't stripped.
app_dummy();
while (!app->destroyRequested) {
int events;
struct android_poll_source* source;
while (ALooper_pollAll(0, nullptr, &events, (void**)&source) >= 0) {
if (source) {
source->process(app, source);
}
}
draw_frame(&userData);
}
}