The Android Open Source Project | f013e1a | 2008-12-17 18:05:43 -0800 | [diff] [blame] | 1 | /* San Angeles Observation OpenGL ES version example |
| 2 | * Copyright 2004-2005 Jetro Lauha |
| 3 | * All rights reserved. |
| 4 | * Web: http://iki.fi/jetro/ |
| 5 | * |
| 6 | * This source is free software; you can redistribute it and/or |
| 7 | * modify it under the terms of EITHER: |
| 8 | * (1) The GNU Lesser General Public License as published by the Free |
| 9 | * Software Foundation; either version 2.1 of the License, or (at |
| 10 | * your option) any later version. The text of the GNU Lesser |
| 11 | * General Public License is included with this source in the |
| 12 | * file LICENSE-LGPL.txt. |
| 13 | * (2) The BSD-style license that is included with this source in |
| 14 | * the file LICENSE-BSD.txt. |
| 15 | * |
| 16 | * This source is distributed in the hope that it will be useful, |
| 17 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 18 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the files |
| 19 | * LICENSE-LGPL.txt and LICENSE-BSD.txt for more details. |
| 20 | * |
| 21 | * $Id: app-linux.c,v 1.4 2005/02/08 18:42:48 tonic Exp $ |
| 22 | * $Revision: 1.4 $ |
| 23 | * |
| 24 | * Parts of this source file is based on test/example code from |
| 25 | * GLESonGL implementation by David Blythe. Here is copy of the |
| 26 | * license notice from that source: |
| 27 | * |
| 28 | * Copyright (C) 2003 David Blythe All Rights Reserved. |
| 29 | * |
| 30 | * Permission is hereby granted, free of charge, to any person obtaining a |
| 31 | * copy of this software and associated documentation files (the "Software"), |
| 32 | * to deal in the Software without restriction, including without limitation |
| 33 | * the rights to use, copy, modify, merge, publish, distribute, sublicense, |
| 34 | * and/or sell copies of the Software, and to permit persons to whom the |
| 35 | * Software is furnished to do so, subject to the following conditions: |
| 36 | * |
| 37 | * The above copyright notice and this permission notice shall be included |
| 38 | * in all copies or substantial portions of the Software. |
| 39 | * |
| 40 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS |
| 41 | * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 42 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL |
| 43 | * DAVID BLYTHE BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN |
| 44 | * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN |
| 45 | * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
| 46 | */ |
| 47 | |
| 48 | #include <stdlib.h> |
| 49 | #include <stdio.h> |
| 50 | #include <sys/time.h> |
| 51 | |
The Android Open Source Project | d24b818 | 2009-02-10 15:44:00 -0800 | [diff] [blame^] | 52 | #include <EGL/egl.h> |
| 53 | #include <GLES/gl.h> |
The Android Open Source Project | f013e1a | 2008-12-17 18:05:43 -0800 | [diff] [blame] | 54 | |
| 55 | #include "app.h" |
| 56 | |
| 57 | |
| 58 | int gAppAlive = 1; |
| 59 | |
| 60 | static const char sAppName[] = |
| 61 | "San Angeles Observation OpenGL ES version example (Linux)"; |
| 62 | |
| 63 | static int sWindowWidth = WINDOW_DEFAULT_WIDTH; |
| 64 | static int sWindowHeight = WINDOW_DEFAULT_HEIGHT; |
| 65 | static EGLDisplay sEglDisplay = EGL_NO_DISPLAY; |
| 66 | static EGLContext sEglContext = EGL_NO_CONTEXT; |
| 67 | static EGLSurface sEglSurface = EGL_NO_SURFACE; |
| 68 | |
| 69 | const char *egl_strerror(unsigned err) |
| 70 | { |
| 71 | switch(err){ |
| 72 | case EGL_SUCCESS: return "SUCCESS"; |
| 73 | case EGL_NOT_INITIALIZED: return "NOT INITIALIZED"; |
| 74 | case EGL_BAD_ACCESS: return "BAD ACCESS"; |
| 75 | case EGL_BAD_ALLOC: return "BAD ALLOC"; |
| 76 | case EGL_BAD_ATTRIBUTE: return "BAD_ATTRIBUTE"; |
| 77 | case EGL_BAD_CONFIG: return "BAD CONFIG"; |
| 78 | case EGL_BAD_CONTEXT: return "BAD CONTEXT"; |
| 79 | case EGL_BAD_CURRENT_SURFACE: return "BAD CURRENT SURFACE"; |
| 80 | case EGL_BAD_DISPLAY: return "BAD DISPLAY"; |
| 81 | case EGL_BAD_MATCH: return "BAD MATCH"; |
| 82 | case EGL_BAD_NATIVE_PIXMAP: return "BAD NATIVE PIXMAP"; |
| 83 | case EGL_BAD_NATIVE_WINDOW: return "BAD NATIVE WINDOW"; |
| 84 | case EGL_BAD_PARAMETER: return "BAD PARAMETER"; |
| 85 | case EGL_BAD_SURFACE: return "BAD_SURFACE"; |
| 86 | // case EGL_CONTEXT_LOST: return "CONTEXT LOST"; |
| 87 | default: return "UNKNOWN"; |
| 88 | } |
| 89 | } |
| 90 | |
| 91 | void egl_error(const char *name) |
| 92 | { |
| 93 | unsigned err = eglGetError(); |
| 94 | if(err != EGL_SUCCESS) { |
| 95 | fprintf(stderr,"%s(): egl error 0x%x (%s)\n", |
| 96 | name, err, egl_strerror(err)); |
| 97 | } |
| 98 | } |
| 99 | |
| 100 | static void checkGLErrors() |
| 101 | { |
| 102 | GLenum error = glGetError(); |
| 103 | if (error != GL_NO_ERROR) |
| 104 | fprintf(stderr, "GL Error: 0x%04x\n", (int)error); |
| 105 | } |
| 106 | |
| 107 | |
| 108 | static void checkEGLErrors() |
| 109 | { |
| 110 | EGLint error = eglGetError(); |
| 111 | // GLESonGL seems to be returning 0 when there is no errors? |
| 112 | if (error && error != EGL_SUCCESS) |
| 113 | fprintf(stderr, "EGL Error: 0x%04x\n", (int)error); |
| 114 | } |
| 115 | |
| 116 | static int initGraphics() |
| 117 | { |
| 118 | EGLint s_configAttribs[] = { |
| 119 | EGL_RED_SIZE, 5, |
| 120 | EGL_GREEN_SIZE, 6, |
| 121 | EGL_BLUE_SIZE, 5, |
| 122 | #if 1 |
| 123 | EGL_DEPTH_SIZE, 16, |
| 124 | EGL_STENCIL_SIZE, 0, |
| 125 | #else |
| 126 | EGL_ALPHA_SIZE, EGL_DONT_CARE, |
| 127 | EGL_DEPTH_SIZE, EGL_DONT_CARE, |
| 128 | EGL_STENCIL_SIZE, EGL_DONT_CARE, |
| 129 | EGL_SURFACE_TYPE, EGL_DONT_CARE, |
| 130 | #endif |
| 131 | EGL_NONE |
| 132 | }; |
| 133 | |
| 134 | EGLint numConfigs = -1; |
| 135 | EGLint majorVersion; |
| 136 | EGLint minorVersion; |
| 137 | EGLConfig config; |
| 138 | EGLContext context; |
| 139 | EGLSurface surface; |
| 140 | |
| 141 | EGLDisplay dpy; |
| 142 | |
| 143 | dpy = eglGetDisplay(EGL_DEFAULT_DISPLAY); |
| 144 | egl_error("eglGetDisplay"); |
| 145 | fprintf(stderr,"dpy = 0x%08x\n", (unsigned) dpy); |
| 146 | |
| 147 | eglInitialize(dpy, &majorVersion, &minorVersion); |
| 148 | egl_error("eglInitialize"); |
| 149 | |
| 150 | eglGetConfigs(dpy, NULL, 0, &numConfigs); |
| 151 | egl_error("eglGetConfigs"); |
| 152 | fprintf(stderr,"num configs %d\n", numConfigs); |
| 153 | |
| 154 | eglChooseConfig(dpy, s_configAttribs, &config, 1, &numConfigs); |
| 155 | egl_error("eglChooseConfig"); |
| 156 | |
| 157 | surface = eglCreateWindowSurface(dpy, config, |
| 158 | android_createDisplaySurface(), NULL); |
| 159 | egl_error("eglMapWindowSurface"); |
| 160 | |
| 161 | fprintf(stderr,"surface = %p\n", surface); |
| 162 | |
| 163 | context = eglCreateContext(dpy, config, NULL, NULL); |
| 164 | egl_error("eglCreateContext"); |
| 165 | fprintf(stderr,"context = %p\n", context); |
| 166 | |
| 167 | eglMakeCurrent(dpy, surface, surface, context); |
| 168 | egl_error("eglMakeCurrent"); |
| 169 | |
| 170 | eglQuerySurface(dpy, surface, EGL_WIDTH, &sWindowWidth); |
| 171 | eglQuerySurface(dpy, surface, EGL_HEIGHT, &sWindowHeight); |
| 172 | |
| 173 | sEglDisplay = dpy; |
| 174 | sEglSurface = surface; |
| 175 | sEglContext = context; |
| 176 | |
| 177 | return EGL_TRUE; |
| 178 | } |
| 179 | |
| 180 | |
| 181 | static void deinitGraphics() |
| 182 | { |
| 183 | eglMakeCurrent(sEglDisplay, NULL, NULL, NULL); |
| 184 | eglDestroyContext(sEglDisplay, sEglContext); |
| 185 | eglDestroySurface(sEglDisplay, sEglSurface); |
| 186 | eglTerminate(sEglDisplay); |
| 187 | } |
| 188 | |
| 189 | |
| 190 | int main(int argc, char *argv[]) |
| 191 | { |
| 192 | // not referenced: |
| 193 | argc = argc; |
| 194 | argv = argv; |
| 195 | |
| 196 | if (!initGraphics()) |
| 197 | { |
| 198 | fprintf(stderr, "Graphics initialization failed.\n"); |
| 199 | return EXIT_FAILURE; |
| 200 | } |
| 201 | |
| 202 | appInit(); |
| 203 | |
| 204 | while (gAppAlive) |
| 205 | { |
| 206 | struct timeval timeNow; |
| 207 | |
| 208 | if (gAppAlive) |
| 209 | { |
| 210 | gettimeofday(&timeNow, NULL); |
| 211 | appRender(timeNow.tv_sec * 1000 + timeNow.tv_usec / 1000, |
| 212 | sWindowWidth, sWindowHeight); |
| 213 | checkGLErrors(); |
| 214 | eglSwapBuffers(sEglDisplay, sEglSurface); |
| 215 | checkEGLErrors(); |
| 216 | } |
| 217 | } |
| 218 | |
| 219 | appDeinit(); |
| 220 | deinitGraphics(); |
| 221 | |
| 222 | return EXIT_SUCCESS; |
| 223 | } |