-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtestlib_rpi.c
229 lines (179 loc) · 5.8 KB
/
testlib_rpi.c
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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
/* EGL implementation of testlib targeting the raspberry pi */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include <assert.h>
#include <time.h>
#include <unistd.h>
#include "testlib.h"
#include "testlib_internal.h"
#include "joystick.h"
#include "bcm_host.h"
#include "GLES/gl.h"
#include "EGL/egl.h"
#include "EGL/eglext.h"
EGLDisplay display;
EGLSurface surface;
EGLContext context;
extern StackAllocator frame_allocator;
// include common code that is dependant on the platform variable
// location/name of the opengl headers
#include "testlib_gl.c"
void egl_assert_(int test, const char * string) {
if(!test) {
fprintf(stderr, "egl_assert: %s -> %s\n", string,
eglQueryString(display, eglGetError()));
exit(-1);
}
}
#define egl_assert(test) egl_assert_(test, "" #test)
/* GLES2
static GLbyte vShaderStr[] =
"attribute vec4 vPosition; \n"
"void main() { \n"
" gl_Position = vPosition; \n"
"}\n";
static GLbyte fShaderStr[] =
"precision mediump float;\n"
"void main() { \n"
" gl_FragColor = vec4(1.0, 1.0, 0.0, 1.0); \n"
"}\n";
GLuint load_shader(GLenum type, const char * src) {
GLuint shader;
GLint compiled;
shader = glCreateShader(type);
if(shader == 0) return 0;
glShaderSource(shader, 1, &src, NULL);
glCompilerShader(shader);
glGetShaderiv(shader, GL_COMPILE_STATUS, &compiled);
if(!compiled) {
GLint info_len = 0;
glGetShaderiv(shader, GL_INFO_LOG_LENGTH, &info_len);
if(info_len > 0) {
char* log = malloc(info_len);
glGetShaderInfoLog(shader, info_len, NULL, log);
fprintf(stderr, "Error compiling shader:\n%s\n", log);
free(log);
}
glDeleteShader(shader);
return 0;
}
return shader;
}
*/
struct timeval start_time;
js_state joystick_state;
void native_init() {
gettimeofday(&start_time, NULL);
joystick_state = joystick_open("/dev/input/js0");
}
long time_millis() {
struct timeval now_time;
gettimeofday(&now_time);
long delta_secs = now_time.tv_sec - start_time.tv_sec;
long delta_usecs = now_time.tv_usec - start_time.tv_usec;
return (delta_secs * 1000) + (delta_usecs / 1000);
}
void sleep_millis(long millis) {
usleep(millis * 1000);
}
int sign(int val) {
if(val > 0) return 1;
if(val < 0) return -1;
return 0;
}
InputState frame_inputstate() {
InputState state = stack_allocator_alloc(frame_allocator, sizeof(struct InputState_));
memset(state, 0, sizeof(struct InputState_));
joystick_update_state(joystick_state);
/*
if(joystick_state->values[4].value != state->leftright
|| joystick_state->values[5].value != state->updown) {
joystick_print_state(joystick_state);
}
joystick_print_state(joystick_state);
*/
state->leftright = ((float)joystick_state->values[1].value) / 32767.0;
state->updown = -((float)joystick_state->values[3].value) / 32767.0;
state->action1 = joystick_state->values[0].value;
return state;
}
void renderer_init(void* empty) {
int32_t success = 0;
EGLBoolean result;
EGLint num_config;
static EGL_DISPMANX_WINDOW_T nativewindow;
DISPMANX_ELEMENT_HANDLE_T dispman_element;
DISPMANX_DISPLAY_HANDLE_T dispman_display;
DISPMANX_UPDATE_HANDLE_T dispman_update;
VC_RECT_T dst_rect;
VC_RECT_T src_rect;
static const EGLint attribute_list[] =
{
EGL_RED_SIZE, 8,
EGL_GREEN_SIZE, 8,
EGL_BLUE_SIZE, 8,
EGL_ALPHA_SIZE, 8,
EGL_SURFACE_TYPE, EGL_WINDOW_BIT,
EGL_NONE
};
EGLConfig config;
bcm_host_init();
// get an EGL display connection
display = eglGetDisplay(EGL_DEFAULT_DISPLAY);
egl_assert(display!=EGL_NO_DISPLAY);
// initialize the EGL display connection
int major, minor;
result = eglInitialize(display, &major, &minor);
egl_assert(EGL_FALSE != result);
fprintf(stderr, "EGL initialzed version %d %d\n", major, minor);
// get an appropriate EGL frame buffer configuration
result = eglChooseConfig(display, attribute_list, &config, 1, &num_config);
egl_assert(EGL_FALSE != result);
// create an EGL rendering context
context = eglCreateContext(display, config, EGL_NO_CONTEXT, NULL);
egl_assert(context!=EGL_NO_CONTEXT);
// create an EGL window surface
success = graphics_get_display_size(0 /* LCD */, &screen_width, &screen_height);
fprintf(stderr, "success = %d, screen_width = %d, screen_height = %d\n", success, screen_width, screen_height);
egl_assert( success >= 0 );
dst_rect.x = 0;
dst_rect.y = 0;
dst_rect.width = screen_width;
dst_rect.height = screen_height;
src_rect.x = 0;
src_rect.y = 0;
src_rect.width = screen_width << 16;
src_rect.height = screen_height << 16;
dispman_display = vc_dispmanx_display_open( 0 /* LCD */);
dispman_update = vc_dispmanx_update_start( 0 );
dispman_element =
vc_dispmanx_element_add ( dispman_update, dispman_display,
0/*layer*/, &dst_rect, 0/*src*/,
&src_rect, DISPMANX_PROTECTION_NONE, 0 /*alpha*/, 0/*clamp*/, 0/*transform*/);
nativewindow.element = dispman_element;
nativewindow.width = screen_width;
nativewindow.height = screen_height;
vc_dispmanx_update_submit_sync( dispman_update );
surface = eglCreateWindowSurface( display, config, &nativewindow, NULL );
egl_assert(surface != EGL_NO_SURFACE);
// connect the context to the surface
result = eglMakeCurrent(display, surface, surface, context);
egl_assert(EGL_FALSE != result);
renderer_gl_init();
}
void renderer_shutdown(void* empty) {
renderer_gl_shutdown();
// Release OpenGL resources
eglMakeCurrent( display, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT );
eglDestroySurface( display, surface );
eglDestroyContext( display, context );
eglTerminate( display );
}
void at_exit() {
}
void signal_render_complete(void* empty) {
eglSwapBuffers(display, surface);
gl_check_("endframe");
}