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
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
|
/* San Angeles Observation OpenGL ES version example
* Copyright 2004-2005 Jetro Lauha
* All rights reserved.
* Web: http://iki.fi/jetro/
*
* This source is free software; you can redistribute it and/or
* modify it under the terms of EITHER:
* (1) The GNU Lesser General Public License as published by the Free
* Software Foundation; either version 2.1 of the License, or (at
* your option) any later version. The text of the GNU Lesser
* General Public License is included with this source in the
* file LICENSE-LGPL.txt.
* (2) The BSD-style license that is included with this source in
* the file LICENSE-BSD.txt.
*
* This source is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the files
* LICENSE-LGPL.txt and LICENSE-BSD.txt for more details.
*
* $Id: app-linux.c,v 1.4 2005/02/08 18:42:48 tonic Exp $
* $Revision: 1.4 $
*
* Parts of this source file is based on test/example code from
* GLESonGL implementation by David Blythe. Here is copy of the
* license notice from that source:
*
* Copyright (C) 2003 David Blythe All Rights Reserved.
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included
* in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
* DAVID BLYTHE BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
* AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
#include <stdlib.h>
#include <stdio.h>
#include <sys/time.h>
#include <EGL/egl.h>
#include <GLES/gl.h>
#include "app.h"
int gAppAlive = 1;
static const char sAppName[] =
"San Angeles Observation OpenGL ES version example (Linux)";
static int sWindowWidth = WINDOW_DEFAULT_WIDTH;
static int sWindowHeight = WINDOW_DEFAULT_HEIGHT;
static EGLDisplay sEglDisplay = EGL_NO_DISPLAY;
static EGLContext sEglContext = EGL_NO_CONTEXT;
static EGLSurface sEglSurface = EGL_NO_SURFACE;
const char *egl_strerror(unsigned err)
{
switch(err){
case EGL_SUCCESS: return "SUCCESS";
case EGL_NOT_INITIALIZED: return "NOT INITIALIZED";
case EGL_BAD_ACCESS: return "BAD ACCESS";
case EGL_BAD_ALLOC: return "BAD ALLOC";
case EGL_BAD_ATTRIBUTE: return "BAD_ATTRIBUTE";
case EGL_BAD_CONFIG: return "BAD CONFIG";
case EGL_BAD_CONTEXT: return "BAD CONTEXT";
case EGL_BAD_CURRENT_SURFACE: return "BAD CURRENT SURFACE";
case EGL_BAD_DISPLAY: return "BAD DISPLAY";
case EGL_BAD_MATCH: return "BAD MATCH";
case EGL_BAD_NATIVE_PIXMAP: return "BAD NATIVE PIXMAP";
case EGL_BAD_NATIVE_WINDOW: return "BAD NATIVE WINDOW";
case EGL_BAD_PARAMETER: return "BAD PARAMETER";
case EGL_BAD_SURFACE: return "BAD_SURFACE";
// case EGL_CONTEXT_LOST: return "CONTEXT LOST";
default: return "UNKNOWN";
}
}
void egl_error(const char *name)
{
unsigned err = eglGetError();
if(err != EGL_SUCCESS) {
fprintf(stderr,"%s(): egl error 0x%x (%s)\n",
name, err, egl_strerror(err));
}
}
static void checkGLErrors()
{
GLenum error = glGetError();
if (error != GL_NO_ERROR)
fprintf(stderr, "GL Error: 0x%04x\n", (int)error);
}
static void checkEGLErrors()
{
EGLint error = eglGetError();
// GLESonGL seems to be returning 0 when there is no errors?
if (error && error != EGL_SUCCESS)
fprintf(stderr, "EGL Error: 0x%04x\n", (int)error);
}
static int initGraphics()
{
EGLint s_configAttribs[] = {
EGL_RED_SIZE, 5,
EGL_GREEN_SIZE, 6,
EGL_BLUE_SIZE, 5,
#if 1
EGL_DEPTH_SIZE, 16,
EGL_STENCIL_SIZE, 0,
#else
EGL_ALPHA_SIZE, EGL_DONT_CARE,
EGL_DEPTH_SIZE, EGL_DONT_CARE,
EGL_STENCIL_SIZE, EGL_DONT_CARE,
EGL_SURFACE_TYPE, EGL_DONT_CARE,
#endif
EGL_NONE
};
EGLint numConfigs = -1;
EGLint n = 0;
EGLint majorVersion;
EGLint minorVersion;
EGLConfig config;
EGLContext context;
EGLSurface surface;
EGLDisplay dpy;
dpy = eglGetDisplay(EGL_DEFAULT_DISPLAY);
egl_error("eglGetDisplay");
fprintf(stderr,"dpy = 0x%08x\n", (unsigned) dpy);
eglInitialize(dpy, &majorVersion, &minorVersion);
egl_error("eglInitialize");
eglGetConfigs(dpy, NULL, 0, &numConfigs);
// Get all the "potential match" configs...
EGLConfig* const configs = malloc(sizeof(EGLConfig)*numConfigs);
eglChooseConfig(dpy, s_configAttribs, configs, numConfigs, &n);
config = configs[0];
if (n > 1) {
// if there is more than one candidate, go through the list
// and pick one that matches our framebuffer format
int fbSzA = 0; // should not hardcode
int fbSzR = 5; // should not hardcode
int fbSzG = 6; // should not hardcode
int fbSzB = 5; // should not hardcode
int i;
for (i=0 ; i<n ; i++) {
EGLint r,g,b,a;
eglGetConfigAttrib(dpy, configs[i], EGL_RED_SIZE, &r);
eglGetConfigAttrib(dpy, configs[i], EGL_GREEN_SIZE, &g);
eglGetConfigAttrib(dpy, configs[i], EGL_BLUE_SIZE, &b);
eglGetConfigAttrib(dpy, configs[i], EGL_ALPHA_SIZE, &a);
if (fbSzA == a && fbSzR == r && fbSzG == g && fbSzB == b) {
config = configs[i];
break;
}
}
}
free(configs);
//eglGetConfigs(dpy, NULL, 0, &numConfigs);
//egl_error("eglGetConfigs");
//fprintf(stderr,"num configs %d\n", numConfigs);
//eglChooseConfig(dpy, s_configAttribs, &config, 1, &numConfigs);
//egl_error("eglChooseConfig");
surface = eglCreateWindowSurface(dpy, config,
android_createDisplaySurface(), NULL);
egl_error("eglCreateWindowSurface");
fprintf(stderr,"surface = %p\n", surface);
context = eglCreateContext(dpy, config, NULL, NULL);
egl_error("eglCreateContext");
fprintf(stderr,"context = %p\n", context);
eglMakeCurrent(dpy, surface, surface, context);
egl_error("eglMakeCurrent");
eglQuerySurface(dpy, surface, EGL_WIDTH, &sWindowWidth);
eglQuerySurface(dpy, surface, EGL_HEIGHT, &sWindowHeight);
sEglDisplay = dpy;
sEglSurface = surface;
sEglContext = context;
return EGL_TRUE;
}
static void deinitGraphics()
{
eglMakeCurrent(sEglDisplay, NULL, NULL, NULL);
eglDestroyContext(sEglDisplay, sEglContext);
eglDestroySurface(sEglDisplay, sEglSurface);
eglTerminate(sEglDisplay);
}
int main(int argc, char *argv[])
{
// not referenced:
argc = argc;
argv = argv;
if (!initGraphics())
{
fprintf(stderr, "Graphics initialization failed.\n");
return EXIT_FAILURE;
}
appInit();
while (gAppAlive)
{
struct timeval timeNow;
if (gAppAlive)
{
gettimeofday(&timeNow, NULL);
appRender(timeNow.tv_sec * 1000 + timeNow.tv_usec / 1000,
sWindowWidth, sWindowHeight);
checkGLErrors();
eglSwapBuffers(sEglDisplay, sEglSurface);
checkEGLErrors();
}
}
appDeinit();
deinitGraphics();
return EXIT_SUCCESS;
}
|