OLD | NEW |
1 // | 1 // |
2 // Book: OpenGL(R) ES 2.0 Programming Guide | 2 // Book: OpenGL(R) ES 2.0 Programming Guide |
3 // Authors: Aaftab Munshi, Dan Ginsburg, Dave Shreiner | 3 // Authors: Aaftab Munshi, Dan Ginsburg, Dave Shreiner |
4 // ISBN-10: 0321502795 | 4 // ISBN-10: 0321502795 |
5 // ISBN-13: 9780321502797 | 5 // ISBN-13: 9780321502797 |
6 // Publisher: Addison-Wesley Professional | 6 // Publisher: Addison-Wesley Professional |
7 // URLs: http://safari.informit.com/9780321563835 | 7 // URLs: http://safari.informit.com/9780321563835 |
8 // http://www.opengles-book.com | 8 // http://www.opengles-book.com |
9 // | 9 // |
10 | 10 |
11 // ESUtil.c | 11 // ESUtil.c |
12 // | 12 // |
13 // A utility library for OpenGL ES. This library provides a | 13 // A utility library for OpenGL ES. This library provides a |
14 // basic common framework for the example applications in the | 14 // basic common framework for the example applications in the |
15 // OpenGL ES 2.0 Programming Guide. | 15 // OpenGL ES 2.0 Programming Guide. |
16 // | 16 // |
17 | 17 |
18 /// | 18 /// |
19 // Includes | 19 // Includes |
20 // | 20 // |
21 #include <stdio.h> | 21 #include <stdio.h> |
22 #include <stdlib.h> | 22 #include <stdlib.h> |
23 #include <stdarg.h> | |
24 #include <string.h> | |
25 | |
26 #include <GLES2/gl2.h> | 23 #include <GLES2/gl2.h> |
27 | 24 #include <EGL/egl.h> |
28 #include "esUtil.h" | 25 #include "esUtil.h" |
29 #include "esUtil_win.h" | 26 #include "esUtil_win.h" |
30 | 27 |
| 28 |
| 29 |
| 30 |
| 31 /// |
| 32 // CreateEGLContext() |
| 33 // |
| 34 // Creates an EGL rendering context and all associated elements |
| 35 // |
| 36 EGLBoolean CreateEGLContext ( EGLNativeWindowType hWnd, EGLDisplay* eglDisplay, |
| 37 EGLContext* eglContext, EGLSurface* eglSurface, |
| 38 EGLint attribList[]) |
| 39 { |
| 40 EGLint numConfigs; |
| 41 EGLint majorVersion; |
| 42 EGLint minorVersion; |
| 43 EGLDisplay display; |
| 44 EGLContext context; |
| 45 EGLSurface surface; |
| 46 EGLConfig config; |
| 47 EGLint contextAttribs[] = { EGL_CONTEXT_CLIENT_VERSION, 2, EGL_NONE, EGL_NONE
}; |
| 48 |
| 49 // Get Display |
| 50 display = eglGetDisplay(GetDC(hWnd)); |
| 51 if ( display == EGL_NO_DISPLAY ) |
| 52 { |
| 53 return EGL_FALSE; |
| 54 } |
| 55 |
| 56 // Initialize EGL |
| 57 if ( !eglInitialize(display, &majorVersion, &minorVersion) ) |
| 58 { |
| 59 return EGL_FALSE; |
| 60 } |
| 61 |
| 62 // Get configs |
| 63 if ( !eglGetConfigs(display, NULL, 0, &numConfigs) ) |
| 64 { |
| 65 return EGL_FALSE; |
| 66 } |
| 67 |
| 68 // Choose config |
| 69 if ( !eglChooseConfig(display, attribList, &config, 1, &numConfigs) ) |
| 70 { |
| 71 return EGL_FALSE; |
| 72 } |
| 73 |
| 74 // Create a surface |
| 75 surface = eglCreateWindowSurface(display, config, (EGLNativeWindowType)hWnd,
NULL); |
| 76 if ( surface == EGL_NO_SURFACE ) |
| 77 { |
| 78 return EGL_FALSE; |
| 79 } |
| 80 |
| 81 // Create a GL context |
| 82 context = eglCreateContext(display, config, EGL_NO_CONTEXT, contextAttribs ); |
| 83 if ( context == EGL_NO_CONTEXT ) |
| 84 { |
| 85 return EGL_FALSE; |
| 86 } |
| 87 |
| 88 // Make the context current |
| 89 if ( !eglMakeCurrent(display, surface, surface, context) ) |
| 90 { |
| 91 return EGL_FALSE; |
| 92 } |
| 93 |
| 94 *eglDisplay = display; |
| 95 *eglSurface = surface; |
| 96 *eglContext = context; |
| 97 return EGL_TRUE; |
| 98 } |
| 99 |
| 100 ////////////////////////////////////////////////////////////////// |
| 101 // |
| 102 // Public Functions |
| 103 // |
| 104 // |
| 105 |
31 /// | 106 /// |
32 // esInitContext() | 107 // esInitContext() |
33 // | 108 // |
34 // Initialize ES utility context. This must be called before calling any o
ther | 109 // Initialize ES utility context. This must be called before calling any o
ther |
35 // functions. | 110 // functions. |
36 // | 111 // |
37 void esInitContext ( ESContext *esContext ) | 112 void ESUTIL_API esInitContext ( ESContext *esContext ) |
38 { | 113 { |
39 if ( esContext != NULL ) | 114 if ( esContext != NULL ) |
40 { | 115 { |
41 memset( esContext, 0, sizeof( ESContext) ); | 116 memset( esContext, 0, sizeof( ESContext) ); |
42 } | 117 } |
43 } | 118 } |
44 | 119 |
45 /// | 120 /// |
| 121 // esCreateWindow() |
| 122 // |
| 123 // title - name for title bar of window |
| 124 // width - width of window to create |
| 125 // height - height of window to create |
| 126 // flags - bitwise or of window creation flags |
| 127 // ES_WINDOW_ALPHA - specifies that the framebuffer should have a
lpha |
| 128 // ES_WINDOW_DEPTH - specifies that a depth buffer should be crea
ted |
| 129 // ES_WINDOW_STENCIL - specifies that a stencil buffer should be cr
eated |
| 130 // ES_WINDOW_MULTISAMPLE - specifies that a multi-sample buffer should
be created |
| 131 // |
| 132 GLboolean ESUTIL_API esCreateWindow ( ESContext *esContext, const char* title, G
Lint width, GLint height, GLuint flags ) |
| 133 { |
| 134 EGLint attribList[] = |
| 135 { |
| 136 EGL_RED_SIZE, 5, |
| 137 EGL_GREEN_SIZE, 6, |
| 138 EGL_BLUE_SIZE, 5, |
| 139 EGL_ALPHA_SIZE, (flags & ES_WINDOW_ALPHA) ? 8 : EGL_DONT_CARE, |
| 140 EGL_DEPTH_SIZE, (flags & ES_WINDOW_DEPTH) ? 8 : EGL_DONT_CARE, |
| 141 EGL_STENCIL_SIZE, (flags & ES_WINDOW_STENCIL) ? 8 : EGL_DONT_CARE, |
| 142 EGL_SAMPLE_BUFFERS, (flags & ES_WINDOW_MULTISAMPLE) ? 1 : 0, |
| 143 EGL_NONE |
| 144 }; |
| 145 |
| 146 if ( esContext == NULL ) |
| 147 { |
| 148 return GL_FALSE; |
| 149 } |
| 150 |
| 151 esContext->width = width; |
| 152 esContext->height = height; |
| 153 |
| 154 if ( !WinCreate ( esContext, title) ) |
| 155 { |
| 156 return GL_FALSE; |
| 157 } |
| 158 |
| 159 |
| 160 if ( !CreateEGLContext ( esContext->hWnd, |
| 161 &esContext->eglDisplay, |
| 162 &esContext->eglContext, |
| 163 &esContext->eglSurface, |
| 164 attribList) ) |
| 165 { |
| 166 return GL_FALSE; |
| 167 } |
| 168 |
| 169 |
| 170 return GL_TRUE; |
| 171 } |
| 172 |
| 173 /// |
| 174 // esMainLoop() |
| 175 // |
| 176 // Start the main loop for the OpenGL ES application |
| 177 // |
| 178 void ESUTIL_API esMainLoop ( ESContext *esContext ) |
| 179 { |
| 180 WinLoop ( esContext ); |
| 181 } |
| 182 |
| 183 |
| 184 /// |
| 185 // esRegisterDrawFunc() |
| 186 // |
| 187 void ESUTIL_API esRegisterDrawFunc ( ESContext *esContext, void (ESCALLBACK *dra
wFunc) (ESContext* ) ) |
| 188 { |
| 189 esContext->drawFunc = drawFunc; |
| 190 } |
| 191 |
| 192 |
| 193 /// |
| 194 // esRegisterUpdateFunc() |
| 195 // |
| 196 void ESUTIL_API esRegisterUpdateFunc ( ESContext *esContext, void (ESCALLBACK *u
pdateFunc) ( ESContext*, float ) ) |
| 197 { |
| 198 esContext->updateFunc = updateFunc; |
| 199 } |
| 200 |
| 201 |
| 202 /// |
| 203 // esRegisterKeyFunc() |
| 204 // |
| 205 void ESUTIL_API esRegisterKeyFunc ( ESContext *esContext, |
| 206 void (ESCALLBACK *keyFunc) (ESContext*, unsi
gned char, int, int ) ) |
| 207 { |
| 208 esContext->keyFunc = keyFunc; |
| 209 } |
| 210 |
| 211 |
| 212 /// |
46 // esLogMessage() | 213 // esLogMessage() |
47 // | 214 // |
48 // Log an error message to the debug output for the platform | 215 // Log an error message to the debug output for the platform |
49 // | 216 // |
50 void esLogMessage ( const char *formatStr, ... ) | 217 void ESUTIL_API esLogMessage ( const char *formatStr, ... ) |
51 { | 218 { |
52 va_list params; | 219 va_list params; |
53 char buf[BUFSIZ]; | 220 char buf[BUFSIZ]; |
54 | 221 |
55 va_start ( params, formatStr ); | 222 va_start ( params, formatStr ); |
56 vsprintf_s ( buf, sizeof(buf), formatStr, params ); | 223 vsprintf_s ( buf, sizeof(buf), formatStr, params ); |
57 | 224 |
58 printf ( "%s", buf ); | 225 printf ( "%s", buf ); |
59 | 226 |
60 va_end ( params ); | 227 va_end ( params ); |
61 } | 228 } |
62 | 229 |
| 230 |
63 /// | 231 /// |
64 // esLoadTGA() | 232 // esLoadTGA() |
65 // | 233 // |
66 // Loads a 24-bit TGA image from a file | 234 // Loads a 24-bit TGA image from a file |
67 // | 235 // |
68 char* esLoadTGA ( char *fileName, int *width, int *height ) | 236 char* ESUTIL_API esLoadTGA ( char *fileName, int *width, int *height ) |
69 { | 237 { |
70 char *buffer; | 238 char *buffer; |
71 | 239 |
72 if ( WinTGALoad ( fileName, &buffer, width, height ) ) | 240 if ( WinTGALoad ( fileName, &buffer, width, height ) ) |
73 { | 241 { |
74 return buffer; | 242 return buffer; |
75 } | 243 } |
76 | 244 |
77 return NULL; | 245 return NULL; |
78 } | 246 } |
OLD | NEW |