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 |
23 #include <GLES2/gl2.h> | 26 #include <GLES2/gl2.h> |
24 #include <EGL/egl.h> | 27 |
25 #include "esUtil.h" | 28 #include "esUtil.h" |
26 #include "esUtil_win.h" | 29 #include "esUtil_win.h" |
27 | 30 |
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 | |
106 /// | 31 /// |
107 // esInitContext() | 32 // esInitContext() |
108 // | 33 // |
109 // Initialize ES utility context. This must be called before calling any o
ther | 34 // Initialize ES utility context. This must be called before calling any o
ther |
110 // functions. | 35 // functions. |
111 // | 36 // |
112 void ESUTIL_API esInitContext ( ESContext *esContext ) | 37 void esInitContext ( ESContext *esContext ) |
113 { | 38 { |
114 if ( esContext != NULL ) | 39 if ( esContext != NULL ) |
115 { | 40 { |
116 memset( esContext, 0, sizeof( ESContext) ); | 41 memset( esContext, 0, sizeof( ESContext) ); |
117 } | 42 } |
118 } | 43 } |
119 | 44 |
120 /// | 45 /// |
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 /// | |
213 // esLogMessage() | 46 // esLogMessage() |
214 // | 47 // |
215 // Log an error message to the debug output for the platform | 48 // Log an error message to the debug output for the platform |
216 // | 49 // |
217 void ESUTIL_API esLogMessage ( const char *formatStr, ... ) | 50 void esLogMessage ( const char *formatStr, ... ) |
218 { | 51 { |
219 va_list params; | 52 va_list params; |
220 char buf[BUFSIZ]; | 53 char buf[BUFSIZ]; |
221 | 54 |
222 va_start ( params, formatStr ); | 55 va_start ( params, formatStr ); |
223 vsprintf_s ( buf, sizeof(buf), formatStr, params ); | 56 vsprintf_s ( buf, sizeof(buf), formatStr, params ); |
224 | 57 |
225 printf ( "%s", buf ); | 58 printf ( "%s", buf ); |
226 | 59 |
227 va_end ( params ); | 60 va_end ( params ); |
228 } | 61 } |
229 | 62 |
230 | |
231 /// | 63 /// |
232 // esLoadTGA() | 64 // esLoadTGA() |
233 // | 65 // |
234 // Loads a 24-bit TGA image from a file | 66 // Loads a 24-bit TGA image from a file |
235 // | 67 // |
236 char* ESUTIL_API esLoadTGA ( char *fileName, int *width, int *height ) | 68 char* esLoadTGA ( char *fileName, int *width, int *height ) |
237 { | 69 { |
238 char *buffer; | 70 char *buffer; |
239 | 71 |
240 if ( WinTGALoad ( fileName, &buffer, width, height ) ) | 72 if ( WinTGALoad ( fileName, &buffer, width, height ) ) |
241 { | 73 { |
242 return buffer; | 74 return buffer; |
243 } | 75 } |
244 | 76 |
245 return NULL; | 77 return NULL; |
246 } | 78 } |
OLD | NEW |