Created
July 27, 2017 22:55
-
-
Save henriquerichter/9b010c440e88936dea503145362526a0 to your computer and use it in GitHub Desktop.
Simple SDL Window + OpenGL
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Window window = Window(); | |
| window.openWindow("Main Window", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, w, h); | |
| ################################ | |
| #include "Window.h" | |
| #include <glew.h> | |
| Window::Window() | |
| { | |
| // Initializes SDL | |
| if (SDL_Init(SDL_INIT_VIDEO) < 0) | |
| { | |
| fprintf(stderr, "Window initialization failed: %s\n", SDL_GetError()); | |
| ErrorHandler::show("Window initialization failed.", window); | |
| exit(-1); | |
| } | |
| } | |
| Window::~Window() | |
| { | |
| this->beforeClose(); | |
| SDL_DestroyWindow(this->window); | |
| } | |
| // Runs after the window is open, before the main loop | |
| void Window::afterInit() | |
| { | |
| } | |
| // Runs before destroying the object | |
| void Window::beforeClose() | |
| { | |
| } | |
| // Opens the the window and runs the main loop | |
| void Window::openWindow(char *window_name, int pos_x, int pos_y, int width, int height, bool full_screen) | |
| { | |
| this->setWindowName(window_name); | |
| this->setSize(width, height); | |
| this->setPos(pos_x, pos_y); | |
| this->setFullScreen(full_screen); | |
| this->createWindow(); | |
| this->initOpenGL(); | |
| this->updateViewport(); | |
| this->afterInit(); | |
| this->mainLoop(); | |
| } | |
| // Creates/Opens the window. Creates the context | |
| void Window::createWindow() | |
| { | |
| if (this->getIsFullScreen()) | |
| { | |
| window = SDL_CreateWindow(this->_window_name, this->_pos_x, this->_pos_y, this->_width, this->_height, SDL_WINDOW_OPENGL | SDL_WINDOW_RESIZABLE | SDL_WINDOW_FULLSCREEN); | |
| } | |
| else | |
| { | |
| window = SDL_CreateWindow(this->_window_name, this->_pos_x, this->_pos_y, this->_width, this->_height, SDL_WINDOW_OPENGL | SDL_WINDOW_RESIZABLE); | |
| } | |
| if (window == NULL) | |
| { | |
| fprintf(stderr, "Window creation failed: %s\n", SDL_GetError()); | |
| ErrorHandler::show("Window creation failed.", window); | |
| exit(-1); | |
| } | |
| // Creates the OpenGL context | |
| glContext = SDL_GL_CreateContext(window); | |
| if (glContext == NULL) | |
| { | |
| fprintf(stderr, "OpenGL initialization failed: %s\n", SDL_GetError()); | |
| ErrorHandler::show("OpenGL initialization failed.", window); | |
| exit(-1); | |
| } | |
| SDL_GL_MakeCurrent(window, glContext); | |
| } | |
| // Window main loop | |
| void Window::mainLoop() | |
| { | |
| bool running = true; | |
| while (running) | |
| { | |
| SDL_Event event; | |
| while (SDL_PollEvent(&event)) | |
| { | |
| switch (event.type) | |
| { | |
| case SDL_QUIT: | |
| running = false; | |
| break; | |
| case SDL_WINDOWEVENT: | |
| windowEvents(event.window.event); | |
| break; | |
| default: | |
| break; | |
| } | |
| } | |
| this->updateWindow(); | |
| } | |
| } | |
| // Handles window events | |
| void Window::windowEvents(Uint8 event) | |
| { | |
| switch (event) | |
| { | |
| case SDL_WINDOWEVENT_SHOWN: | |
| // logging("Window shown"); | |
| break; | |
| case SDL_WINDOWEVENT_HIDDEN: | |
| // logging("Window hidden"); | |
| break; | |
| case SDL_WINDOWEVENT_EXPOSED: | |
| // logging("Window exposed"); | |
| break; | |
| case SDL_WINDOWEVENT_MOVED: | |
| // logging("Window moved"); | |
| break; | |
| case SDL_WINDOWEVENT_RESIZED: | |
| // logging("Window resized"); | |
| break; | |
| case SDL_WINDOWEVENT_SIZE_CHANGED: | |
| // logging("Window size changed"); | |
| break; | |
| case SDL_WINDOWEVENT_MINIMIZED: | |
| // logging("Window minimized"); | |
| break; | |
| case SDL_WINDOWEVENT_MAXIMIZED: | |
| // logging("Window maximized"); | |
| break; | |
| case SDL_WINDOWEVENT_RESTORED: | |
| // logging("Window restored"); | |
| break; | |
| case SDL_WINDOWEVENT_ENTER: | |
| // logging("Mouse entered window"); | |
| break; | |
| case SDL_WINDOWEVENT_LEAVE: | |
| // logging("Mouse left window"); | |
| break; | |
| case SDL_WINDOWEVENT_FOCUS_GAINED: | |
| // logging("Window gained keyboard focus"); | |
| break; | |
| case SDL_WINDOWEVENT_FOCUS_LOST: | |
| // logging("Window lost keyboard focus"); | |
| break; | |
| case SDL_WINDOWEVENT_CLOSE: | |
| // logging("Window closed"); | |
| break; | |
| #if SDL_VERSION_ATLEAST(2, 0, 5) | |
| case SDL_WINDOWEVENT_TAKE_FOCUS: | |
| // logging("Window is offered a focus"); | |
| break; | |
| case SDL_WINDOWEVENT_HIT_TEST: | |
| // logging("Window has a special hit test"); | |
| break; | |
| #endif | |
| default: | |
| // logging("Window got unknown event", event); | |
| break; | |
| } | |
| } | |
| void Window::updateWindow() | |
| { | |
| glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); | |
| glColor4f(0.0f, 0.0f, 1.0f, 1.0f); | |
| /* Move Left 1.5 Units And Into The Screen 6.0 */ | |
| glLoadIdentity(); | |
| glTranslatef(-1.5f, 0.0f, -6.0f); | |
| glBegin(GL_TRIANGLES); /* Drawing Using Triangles */ | |
| glVertex3f(0.0f, 1.0f, 0.0f); /* Top */ | |
| glVertex3f(-1.0f, -1.0f, 0.0f); /* Bottom Left */ | |
| glVertex3f(1.0f, -1.0f, 0.0f); /* Bottom Right */ | |
| glEnd(); /* Finished Drawing The Triangle */ | |
| /* Move Right 3 Units */ | |
| glTranslatef(3.0f, 0.0f, 0.0f); | |
| glBegin(GL_QUADS); /* Draw A Quad */ | |
| glVertex3f(-1.0f, 1.0f, 0.0f); /* Top Left */ | |
| glVertex3f(1.0f, 1.0f, 0.0f); /* Top Right */ | |
| glVertex3f(1.0f, -1.0f, 0.0f); /* Bottom Right */ | |
| glVertex3f(-1.0f, -1.0f, 0.0f); /* Bottom Left */ | |
| glEnd(); /* Done Drawing The Quad */ | |
| SDL_GL_SwapWindow(this->getWindow()); /* Done Drawing The Quad */ | |
| } | |
| void Window::initOpenGL() | |
| { | |
| /* Enable smooth shading */ | |
| glShadeModel(GL_SMOOTH); | |
| /* Set the background red */ | |
| glClearColor(0.0f, 0.0f, 0.0f, 0.0f); | |
| /* Depth buffer setup */ | |
| glClearDepth(1.0f); | |
| /* Enables Depth Testing */ | |
| glEnable(GL_DEPTH_TEST); | |
| /* The Type Of Depth Test To Do */ | |
| glDepthFunc(GL_LEQUAL); | |
| /* Really Nice Perspective Calculations */ | |
| glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST); | |
| } | |
| void Window::updateViewport() | |
| { | |
| /* Height / width ratio */ | |
| //GLfloat ratio; | |
| //ratio = (GLfloat)800 / (GLfloat)600; | |
| /* Setup our viewport. */ | |
| glViewport(0, 0, (GLsizei)_width, (GLsizei)_height); | |
| /* change to the projection matrix and set our viewing volume. */ | |
| glMatrixMode(GL_PROJECTION); | |
| glLoadIdentity(); | |
| /* Set our perspective */ | |
| gluPerspective(45.0f, 1, 0.1f, 100.0f); | |
| /* Make sure we're chaning the model view and not the projection */ | |
| glMatrixMode(GL_MODELVIEW); | |
| /* Reset The View */ | |
| glLoadIdentity(); | |
| } | |
| // Setters | |
| void Window::setWindowName(char *window_name) | |
| { | |
| this->_window_name = window_name; | |
| } | |
| void Window::setSize(int width, int height) | |
| { | |
| this->_width = width; | |
| this->_height = height; | |
| } | |
| void Window::setPos(int pos_x, int pos_y) | |
| { | |
| this->_pos_x = pos_x; | |
| this->_pos_y = pos_y; | |
| } | |
| void Window::setFullScreen(bool full_screen) | |
| { | |
| this->_full_screen = full_screen; | |
| } | |
| // Getters | |
| SDL_Window *Window::getWindow() { return this->window; } | |
| SDL_GLContext Window::getGLContext() { return this->glContext; } | |
| char *Window::getWindowName() { return this->_window_name; } | |
| int Window::getWidth() { return this->_width; } | |
| int Window::getHeight() { return this->_height; } | |
| int Window::getPosX() { return this->_pos_x; } | |
| int Window::getPosY() { return this->_pos_y; } | |
| bool Window::getIsFullScreen() { return this->_full_screen; } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment