int SDL_GLES_SetVideoMode()
{
SDL_SysWMinfo info;
+ EGLBoolean res;
SDL_VERSION(&info.version);
if (SDL_GetWMInfo(&info) != 1) {
/* Destroy previous surface, if any. */
if (egl_surface != EGL_NO_SURFACE) {
+ /* Ensure the surface is not the current one,
+ * thus freeing memory earlier. */
+ eglMakeCurrent(egl_display, EGL_NO_SURFACE, EGL_NO_SURFACE,
+ EGL_NO_CONTEXT);
eglDestroySurface(egl_display, egl_surface);
egl_surface = EGL_NO_SURFACE;
}
- /* No current context? Quietly defer surface creation. */
+ /* No current context? Quietly defer surface creation.
+ * Surface will be created on the call to MakeCurrent. */
if (!cur_context) {
return 0;
}
+ /* Create the new window surface. */
egl_surface = eglCreateWindowSurface(egl_display, cur_context->egl_config,
(EGLNativeWindowType)info.info.x11.window, NULL);
if (egl_surface == EGL_NO_SURFACE) {
return -2;
}
+ /* New surface created. Make it active. */
+ assert(cur_context && cur_context->egl_context != EGL_NO_CONTEXT);
+ res = eglMakeCurrent(egl_display, egl_surface, egl_surface,
+ cur_context->egl_context);
+
+ if (!res) {
+ SDL_SetError("EGL failed to change current surface: %s",
+ get_error_string(eglGetError()));
+ cur_context = NULL;
+ return -2;
+ }
+
return 0;
}
SDL_GLES_ContextPriv *context = (SDL_GLES_ContextPriv*)c;
int res;
- if (context) {
- cur_context = context;
-
- res = SDL_GLES_SetVideoMode();
- if (res != 0) return res; /* Surface (re-)creation failed. */
-
- res = eglMakeCurrent(egl_display, egl_surface, egl_surface,
- context->egl_context);
+ cur_context = context;
- if (!res) {
- SDL_SetError("EGL failed to make current: %s",
- get_error_string(eglGetError()));
- cur_context = NULL;
- return -2;
- }
- } else {
- eglMakeCurrent(egl_display, EGL_NO_SURFACE, EGL_NO_SURFACE,
- EGL_NO_CONTEXT);
- cur_context = NULL;
- SDL_GLES_SetVideoMode(); /* Will clear current surface. */
- }
+ /* SDL_GLES_SetVideoMode() will appropiately clear the current context
+ * (and surface), then create a new surface matching the selected context
+ * config and make both the surface and the context the active ones. */
+ res = SDL_GLES_SetVideoMode();
+ if (res != 0) return res; /* Surface (re-)creation failed. */
- // TODO Update attrib_list
+ /* TODO Update attrib_list. Make SDL_GLES_GetAttribute work. */
switch (gl_version) {
case SDL_GLES_VERSION_1_1:
*/
extern DECLSPEC void SDLCALL SDL_GLES_Quit();
-/* Equivalent to SDL/GL ones. Untested. */
/** Call before calling GetProcAddress. Dynamically loads a GLES library.
* @param path full path to the library to load, or leave as NULL to load
- * the default GL ES library to the version specified in SDL_GLES_Init().
+ * the default GL ES library (version as specified in SDL_GLES_Init()).
* @return 0 if everything went OK.
*/
extern DECLSPEC int SDLCALL SDL_GLES_LoadLibrary(const char *path);
static SDL_Surface *screen;
static SDL_GLES_Context *context;
-static float box_step = 0.1f;
+static float box_step = 0.05f;
static const float w = 0.28f, h = 0.4f;
-static float x = -1.0f, y = 0.0f;
+static float x = -0.5f, y = 0.0f;
static float box_v[4*3];
+static bool fullscreen = false;
static Uint32 tick(Uint32 interval, void* param)
{
e.type = SDL_VIDEOEXPOSE;
x += box_step;
- if (x >= 1.0f || x <= -1.0f) {
+ if ((x + w/2) >= 1.0f || (x - w/2) <= -1.0f) {
box_step *= -1.0f;
}
box_v[6] = x1; box_v[7] = y2; box_v[8] = z;
box_v[9] = x2; box_v[10] = y2; box_v[11] = z;
- SDL_PushEvent(&e);
+ SDL_PushEvent(&e); /* Since SDL calls timers in another thread, we cannot
+ call rendering functions from here. */
return interval;
}
+static void toggle_fullscreen()
+{
+ int res;
+
+ fullscreen = !fullscreen;
+
+ screen = SDL_SetVideoMode(0, 0, 16, SDL_SWSURFACE |
+ (fullscreen ? SDL_FULLSCREEN : 0));
+ assert(screen);
+
+ res = SDL_GLES_SetVideoMode();
+ if (res != 0) puts(SDL_GetError());
+ assert(res == 0);
+}
+
int main()
{
int res;
screen = SDL_SetVideoMode(0, 0, 16, SDL_SWSURFACE);
assert(screen);
- SDL_TimerID timer = SDL_AddTimer(10, tick, NULL);
+ SDL_ShowCursor(SDL_DISABLE);
+
+ SDL_TimerID timer = SDL_AddTimer(50, tick, NULL);
assert(timer != NULL);
context = SDL_GLES_CreateContext();
assert(context);
- SDL_GLES_MakeCurrent(context);
-
+ res = SDL_GLES_MakeCurrent(context);
+ assert(res == 0);
+
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
SDL_GLES_SwapBuffers();
break;
+ case SDL_MOUSEBUTTONDOWN:
+ toggle_fullscreen();
+ break;
}
}