Skip to content

Instantly share code, notes, and snippets.

@blast007
Last active May 30, 2020 07:22
Show Gist options
  • Save blast007/57f6d95522932035ed57db27b6db7070 to your computer and use it in GitHub Desktop.
Save blast007/57f6d95522932035ed57db27b6db7070 to your computer and use it in GitHub Desktop.
SDL2/GLFW OpenGL Fullscreen Test
For SDL2:
g++ -Wall -Wpedantic -Wshadow -lSDL2 -lGL -lGLU -o sdl2test main.cxx
For GLFW:
g++ -Wall -Wpedantic -Wshadow -DUSE_GLFW -lglfw -lGL -lGLU -o glfwtest main.cxx
For custom SDL:
hg clone http://hg.libsdl.org/SDL
cd SDL
./configure --disable-rpath --enable-sdl-dlopen --disable-nas --disable-esd --disable-arts --disable-alsa-shared --disable-pulseaudio-shared --enable-ibus --disable-x11-shared --disable-video-directfb --enable-video-opengles --enable-video-wayland --disable-wayland-shared --prefix $HOME/local/
make
make install
cd ../SDL2_GLFW_Test/
g++ -Wall -Wpedantic -Wshadow -Wl,-rpath,$HOME/local/lib `$HOME/local/bin/sdl2-config --cflags` `$HOME/local/bin/sdl2-config --libs` -lGL -lGLU -o sdl2test main.cxx
#include <iostream>
#include <sstream>
#ifdef USE_GLFW
# include <GLFW/glfw3.h>
#else
# include <SDL2/SDL.h>
#endif
#include <GL/gl.h>
#include <GL/glu.h>
// 0 = Windowed mode at the window size below
// 1 = Start windowed and then change to a fullscreen resolution mode at (roughly) the window size defined below
// 2 = Create fullscreen window at a resolution (roughly) the window size defined below
// 3 = Go fullscreen at the desktop resolution (no resolution change)
#define FULLSCREEN_MODE 2
// Window size or resolution
#define WINDOW_WIDTH 800
#define WINDOW_HEIGHT 600
// Do the OpenGL stuff (1 = On, 0 = Off)
#define DO_GL 1
using namespace std;
#ifdef USE_GLFW
void key_callback(GLFWwindow* window, int key, int scancode, int action, int mods) {
if (key == GLFW_KEY_ESCAPE && action == GLFW_PRESS)
glfwSetWindowShouldClose(window, GLFW_TRUE);
}
#endif
int main(int argc, char *argv[])
{
// Initialize library
#ifdef USE_GLFW
GLFWwindow* window = nullptr;
// Initialize GLFW
if (!glfwInit()) {
cerr << "Unable to initialize GLFW" << endl;
return 1;
}
#else
SDL_Window* window = nullptr;
SDL_GLContext context;
// Initialize SDL video subsystem
if (SDL_Init(SDL_INIT_VIDEO) < 0) {
cerr << "Unable to initialize SDL2 video subsystem: " << SDL_GetError() << endl;
return 1;
}
#endif
// Write out the SDL version we are linked to
SDL_version version;
SDL_GetVersion(&version);
cout << "Using SDL version " << (int)version.major << "." << (int)version.minor << "." << (int)version.patch << " (" << SDL_GetRevision() << ")" << endl;
// Create window
#ifdef USE_GLFW
if (FULLSCREEN_MODE == 0 || FULLSCREEN_MODE == 1) {
window = glfwCreateWindow(WINDOW_WIDTH, WINDOW_HEIGHT, "GLTest", nullptr, nullptr);
}
if (window != nullptr && FULLSCREEN_MODE == 1) {
glfwSetWindowMonitor(window, glfwGetPrimaryMonitor(), 0, 0, WINDOW_WIDTH, WINDOW_HEIGHT, GLFW_DONT_CARE);
}
if (FULLSCREEN_MODE == 2) {
window = glfwCreateWindow(WINDOW_WIDTH, WINDOW_HEIGHT, "GLTest", glfwGetPrimaryMonitor(), nullptr);
}
if (FULLSCREEN_MODE == 3) {
GLFWmonitor* monitor = glfwGetPrimaryMonitor();
const GLFWvidmode* mode = glfwGetVideoMode(monitor);
glfwWindowHint(GLFW_RED_BITS, mode->redBits);
glfwWindowHint(GLFW_GREEN_BITS, mode->greenBits);
glfwWindowHint(GLFW_BLUE_BITS, mode->blueBits);
glfwWindowHint(GLFW_REFRESH_RATE, mode->refreshRate);
window = glfwCreateWindow(mode->width, mode->height, "GLTest", monitor, nullptr);
}
if (window == nullptr) {
cerr << "Unable to create window" << endl;
return 1;
}
#else
Uint32 flags = SDL_WINDOW_OPENGL;
SDL_DisplayMode closest;
int width, height;
if (FULLSCREEN_MODE == 1 || FULLSCREEN_MODE == 2) {
// Get closest fullscreen resolution
SDL_DisplayMode target;
target.w = WINDOW_WIDTH;
target.h = WINDOW_HEIGHT;
target.format = 0;
target.refresh_rate = 0;
target.driverdata = nullptr;
if (SDL_GetClosestDisplayMode(0, &target, &closest) == nullptr) {
cerr << "Unable to determine closest display mode: " << SDL_GetError() << endl;
return 1;
}
// Set the window width/height to match
width = closest.w;
height = closest.h;
// If mode 2, create the window fullscreen
if (FULLSCREEN_MODE == 2) {
flags |= SDL_WINDOW_FULLSCREEN;
}
} else if (FULLSCREEN_MODE == 3) {
// Using the desktop resolution
width = 0;
height = 0;
flags |= SDL_WINDOW_FULLSCREEN_DESKTOP;
} else {
width = WINDOW_WIDTH;
height = WINDOW_HEIGHT;
}
window = SDL_CreateWindow("SDL2 Test", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, width, height, flags);
// Change from windowed to fullscreen
if (FULLSCREEN_MODE == 1) {
if (SDL_SetWindowDisplayMode(window, &closest) < 0) {
cerr << "Unable to set display mode: " << SDL_GetError() << endl;
return 1;
}
cout << "Display Resolution: " << closest.w << " x " << closest.h << endl;
SDL_SetWindowFullscreen(window, SDL_WINDOW_FULLSCREEN);
}
if (window == nullptr) {
cerr << "Unable to create window: " << SDL_GetError() << endl;
return 1;
}
#endif
#if DO_GL
# ifdef USE_GLFW
glfwMakeContextCurrent(window);
glfwSetKeyCallback(window, key_callback);
# else
context = SDL_GL_CreateContext(window);
SDL_GL_MakeCurrent(window, context);
# endif
// Code ripped from NeHe Lession 3 Linux/SDL port
/*
* This code was created by Jeff Molofee '99
* (ported to Linux/SDL by Ti Leggett '01)
*
* If you've found this code useful, please let me know.
*
* Visit Jeff at http://nehe.gamedev.net/
*
* or for port-specific comments, questions, bugreports etc.
* email to leggett@eecs.tulane.edu
*/
# ifdef USE_GLFW
while (!glfwWindowShouldClose(window)) {
# else
bool running = true;
while (running) {
# endif
glShadeModel( GL_SMOOTH );
glClearColor( 0.0f, 0.0f, 0.0f, 0.0f );
glClearDepth( 1.0 );
glEnable( GL_DEPTH_TEST );
glDepthFunc( GL_LEQUAL );
glHint( GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST );
GLdouble ratio;
int w = 0, h = 0;
# ifdef USE_GLFW
glfwGetFramebufferSize(window, &w, &h);
# else
SDL_GL_GetDrawableSize(window, &w, &h);
# endif
if (h == 0) {
cerr << "Unable to get window dimensions." << endl;
# ifdef USE_GLFW
glfwDestroyWindow(window);
glfwTerminate();
# else
SDL_DestroyWindow(window);
SDL_Quit();
# endif
return -1;
}
ratio = (GLdouble) w / (GLdouble) h;
glViewport( 0, 0, ( GLsizei )w, ( GLsizei )h );
glMatrixMode( GL_PROJECTION );
glLoadIdentity( );
gluPerspective( 45.0, ratio, 0.1, 100.0 );
glMatrixMode( GL_MODELVIEW );
glLoadIdentity( );
glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
glLoadIdentity();
glTranslatef(0.0f, 0.0f, -3.0f);
glBegin( GL_TRIANGLES ); /* Drawing Using Triangles */
glColor3f( 1.0f, 0.0f, 0.0f ); /* Red */
glVertex3f( 0.0f, 1.0f, 0.0f ); /* Top Of Triangle */
glColor3f( 0.0f, 1.0f, 0.0f ); /* Green */
glVertex3f( -1.0f, -1.0f, 0.0f ); /* Left Of Triangle */
glColor3f( 0.0f, 0.0f, 1.0f ); /* Blue */
glVertex3f( 1.0f, -1.0f, 0.0f ); /* Right Of Triangle */
glEnd( );
# ifdef USE_GLFW
glfwSwapBuffers(window);
glfwPollEvents();
# else
SDL_GL_SwapWindow(window);
SDL_Event event;
while (SDL_PollEvent(&event)) {
switch (event.type) {
case SDL_KEYDOWN:
if (event.key.keysym.sym == SDLK_ESCAPE)
running = false;
break;
case SDL_WINDOWEVENT:
switch (event.window.event) {
case SDL_WINDOWEVENT_RESIZED:
printf("Received resize event. Width: %d Height: %d\n", event.window.data1, event.window.data2);
break;
default:
break;
}
break;
default:
break;
}
}
#endif
}
#endif
# ifdef USE_GLFW
glfwDestroyWindow(window);
glfwTerminate();
# else
SDL_DestroyWindow(window);
SDL_Quit();
# endif
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment