Browse Source

use SDL2 for opengl example.

This makes Fullscreen handling work much better, in addition to bing more future proof
James Sarrett 8 years ago
parent
commit
4669e70231

+ 1 - 1
CMakeLists.txt

@@ -104,7 +104,7 @@ if (OPENHMD_EXAMPLE_SIMPLE)
 endif(OPENHMD_EXAMPLE_SIMPLE)
 
 if (OPENHMD_EXAMPLE_SDL)
-	find_package(SDL REQUIRED)
+	find_package(SDL2 REQUIRED)
 	find_package(GLEW REQUIRED)
 	find_package(OpenGL REQUIRED)
 	add_subdirectory(./examples/opengl)

+ 1 - 1
configure.ac

@@ -112,7 +112,7 @@ AM_CONDITIONAL([BUILD_OPENGL_EXAMPLE], [test "x$openglexample_enabled" != "xno"]
 
 # Libs required by OpenGL test
 AS_IF([test "x$openglexample_enabled" != "xno"], [
-	PKG_CHECK_MODULES([sdl], [sdl])
+	PKG_CHECK_MODULES([sdl2], [sdl2])
 
 	# Try to find OpenGL with pkg-config
 	PKG_CHECK_MODULES([GL], [gl], [],

+ 3 - 3
examples/opengl/CMakeLists.txt

@@ -1,5 +1,5 @@
 project (openglexample)
-include_directories(${CMAKE_BINARY_DIR}/include ${SDL_INCLUDE_DIR} ${GLEW_INCLUDE_DIRS} ${OPENGL_INCLUDE_DIR})
+include_directories(${CMAKE_BINARY_DIR}/include ${SDL2_INCLUDE_DIRS} ${GLEW_INCLUDE_DIRS} ${OPENGL_INCLUDE_DIR})
 link_directories(${CMAKE_BINARY_DIR})
-link_libraries (openhmd m ${SDL_LIBRARY} ${GLEW_LIBRARIES} ${OPENGL_LIBRARIES})
-add_executable(openglexample gl.c main.c)
+link_libraries (openhmd m ${SDL2_LIBRARIES} ${GLEW_LIBRARIES} ${OPENGL_LIBRARIES})
+add_executable(openglexample gl.c main.c)

+ 2 - 2
examples/opengl/Makefile.am

@@ -1,5 +1,5 @@
 bin_PROGRAMS = openglexample
-AM_CPPFLAGS = -Wall -Werror -I$(top_srcdir)/include -DOHMD_STATIC $(sdl_CFLAGS) $(GLEW_CFLAGS)
+AM_CPPFLAGS = -Wall -Werror -I$(top_srcdir)/include -DOHMD_STATIC $(sdl2_CFLAGS) $(GLEW_CFLAGS)
 openglexample_SOURCES = gl.c main.c
 openglexample_LDADD = $(top_builddir)/src/libopenhmd.la -lm
-openglexample_LDFLAGS = -static-libtool-libs $(sdl_LIBS) $(GLEW_LIBS) $(GL_LIBS)
+openglexample_LDFLAGS = -static-libtool-libs $(sdl2_LIBS) $(GLEW_LIBS) $(GL_LIBS)

+ 21 - 8
examples/opengl/gl.c

@@ -31,18 +31,31 @@ void init_gl(gl_ctx* ctx, int w, int h)
 	}
 
 	SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
-	SDL_GL_SetAttribute(SDL_GL_SWAP_CONTROL, 1);
 
-	ctx->screen = SDL_SetVideoMode(w, h, 0, SDL_OPENGL | SDL_GL_DOUBLEBUFFER);
-	if(ctx->screen == NULL){
-		printf("SDL_SetVideoMode failed\n");
+	ctx->window = SDL_CreateWindow("OpenHMD opengl example",
+			SDL_WINDOWPOS_UNDEFINED,
+			SDL_WINDOWPOS_UNDEFINED,
+			w, h, SDL_WINDOW_OPENGL );
+	if(ctx->window == NULL) {
+		printf("SDL_CreateWindow failed\n");
 		exit(-1);
 	}
+	ctx->w = w;
+	ctx->h = h;
+	ctx->is_fullscreen = 0;
+
+	ctx->glcontext = SDL_GL_CreateContext(ctx->window);
+	if(ctx->glcontext == NULL){
+		printf("SDL_GL_CreateContext\n");
+		exit(-1);
+	}
+
+	SDL_GL_SetSwapInterval(1);
 
 	// Disable ctrl-c catching on linux (and OS X?) 
-	#ifdef __unix
+#ifdef __unix
 	signal(SIGINT, SIG_DFL);
-	#endif
+#endif
 
 	// Load extensions.
 	glewInit();
@@ -69,7 +82,7 @@ void init_gl(gl_ctx* ctx, int w, int h)
 	glEnable(GL_POLYGON_SMOOTH); 
 	glLoadIdentity();
 
-	glViewport(0, 0, ctx->screen->w, ctx->screen->h);
+	glViewport(0, 0, ctx->w, ctx->h);
 }
 
 void ortho(gl_ctx* ctx)
@@ -78,7 +91,7 @@ void ortho(gl_ctx* ctx)
 	//glPushMatrix();
 	glLoadIdentity();
 
-	glOrtho(0.0f, ctx->screen->w, ctx->screen->h, 0.0f, -1.0f, 1.0f);
+	glOrtho(0.0f, ctx->w, ctx->h, 0.0f, -1.0f, 1.0f);
 	glMatrixMode(GL_MODELVIEW);
 	//glPushMatrix();
 	glLoadIdentity();

+ 3 - 1
examples/opengl/gl.h

@@ -17,7 +17,9 @@
 
 typedef struct {
 	int w, h;
-	SDL_Surface* screen;
+	SDL_Window* window;
+	SDL_GLContext glcontext;
+	int is_fullscreen;
 } gl_ctx;
 
 void ortho(gl_ctx* ctx);

+ 5 - 2
examples/opengl/main.c

@@ -206,7 +206,10 @@ int main(int argc, char** argv)
 					done = true;
 					break;
 				case SDLK_F1:
-					SDL_WM_ToggleFullScreen(gl.screen);
+					{
+						gl.is_fullscreen = !gl.is_fullscreen;
+						SDL_SetWindowFullscreen(gl.window, gl.is_fullscreen ? SDL_WINDOW_FULLSCREEN : 0);
+					}
 					break;
 				case SDLK_F2:
 					{
@@ -390,7 +393,7 @@ int main(int argc, char** argv)
 		glUseProgram(0);
 
 		// Da swap-dawup!
-		SDL_GL_SwapBuffers();
+		SDL_GL_SwapWindow(gl.window);
 		SDL_Delay(10);
 	}