Created
February 14, 2021 19:48
-
-
Save danielytics/bec9f6acd9317ea64bb653ef1d3da416 to your computer and use it in GitHub Desktop.
OpenGL with SDL and GLFW
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
// Code taken from GLFW documentation: https://www.glfw.org/documentation.html | |
#include <GLFW/glfw3.h> | |
int main(void) | |
{ | |
GLFWwindow* window; | |
/* Initialize the library */ | |
if (!glfwInit()) | |
return -1; | |
/* Create a windowed mode window and its OpenGL context */ | |
window = glfwCreateWindow(640, 480, "Hello World", NULL, NULL); | |
if (!window) | |
{ | |
glfwTerminate(); | |
return -1; | |
} | |
/* Make the window's context current */ | |
glfwMakeContextCurrent(window); | |
/* Loop until the user closes the window */ | |
while (!glfwWindowShouldClose(window)) | |
{ | |
/* Render here */ | |
glClear(GL_COLOR_BUFFER_BIT); | |
// OpenGL rendering goes here | |
/* Swap front and back buffers */ | |
glfwSwapBuffers(window); | |
/* Poll for and process events */ | |
glfwPollEvents(); | |
} | |
glfwTerminate(); | |
return 0; | |
} |
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
// Code taken from https://gist.github.com/jordandee/94b187bcc51df9528a2f and modified very slightly | |
#include <SDL2/SDL.h> | |
#include <SDL2/SDL_opengl.h> | |
#include <GL/gl.h> | |
int main (int argc, char** argv) | |
{ | |
if (SDL_Init(SDL_INIT_VIDEO) < 0) { | |
return -1; | |
} | |
// Open Window | |
SDL_Window* window = SDL_CreateWindow("OpenGL Test", 0, 0, 640, 480, SDL_WINDOW_OPENGL); | |
if (!window) { | |
return -1; | |
} | |
// OpenGL attributes | |
SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_CORE); | |
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 4); | |
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 1); | |
SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1); | |
// Create OpenGL context | |
SDL_GLContext Context = SDL_GL_CreateContext(window); | |
bool running = true; | |
while (running) | |
{ | |
SDL_Event event; | |
while (SDL_PollEvent(&event)) | |
{ | |
if (event.type == SDL_KEYDOWN) | |
{ | |
switch (event.key.keysym.sym) | |
{ | |
case SDLK_ESCAPE: | |
running = false; | |
break; | |
// Other input here | |
default: | |
break; | |
} | |
} | |
else if (Event.type == SDL_QUIT) | |
{ | |
running = 0; | |
} | |
} | |
glViewport(0, 0, 640, 480); | |
glClearColor(1.f, 0.f, 1.f, 0.f); | |
glClear(GL_COLOR_BUFFER_BIT); | |
// OpenGL rendering goes here | |
SDL_GL_SwapWindow(window); | |
} | |
SDL_Quit(); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment