Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <GL/glew.h>
- #include <GL/gl.h>
- #include <GL/glu.h>
- #include <stdio.h>
- #include <iostream>
- #include <SDL2/SDL.h>
- #include <SDL2/SDL_version.h>
- #include <SDL2/SDL_revision.h>
- int window_width = 960;
- int window_height = 640;
- const char* window_title = "Gaem 0.0.1";
- const int GL_MAJOR_VERSION_TARGET = 3;
- const int GL_MINOR_VERSION_TARGET = 2;
- SDL_Window *window;
- SDL_GLContext glcontext;
- GLuint VertexArrayID;
- // This will identify our vertex buffer
- GLuint vertexbuffer;
- // An array of 3 vectors which represents 3 vertices
- static const GLfloat g_vertex_buffer_data[] = {
- -1.0f, -1.0f, 0.0f,
- 1.0f, -1.0f, 0.0f,
- 0.0f, 1.0f, 0.0f,
- };
- SDL_Event event;
- bool running = true;
- int Init()
- {
- //SDL_INIT_EVERYTHING because why not
- SDL_Init(SDL_INIT_EVERYTHING);
- //Checking if I have SDL2 properly installed...
- SDL_version compiled;
- SDL_version linked;
- SDL_VERSION(&compiled);
- SDL_GetVersion(&linked);
- printf("Compiled against SDL version %d.%d.%d \n", compiled.major, compiled.minor, compiled.patch);
- printf("Linking against SDL version %d.%d.%d\n", linked.major, linked.minor, linked.patch);
- //Setting GL Context attributes
- SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_CORE);
- //SDL_GL_SetAttribute(SDL_GL_ACCELERATED_VISUAL, 1);
- SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, GL_MAJOR_VERSION_TARGET);
- SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, GL_MINOR_VERSION_TARGET);
- SDL_GL_SetAttribute(SDL_GL_RED_SIZE, 8);
- SDL_GL_SetAttribute(SDL_GL_GREEN_SIZE, 8);
- SDL_GL_SetAttribute(SDL_GL_BLUE_SIZE, 8);
- SDL_GL_SetAttribute(SDL_GL_BUFFER_SIZE, 32);
- SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
- SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 24);
- //Create window
- window = SDL_CreateWindow(
- window_title, // window title
- SDL_WINDOWPOS_CENTERED, // initial x position
- SDL_WINDOWPOS_CENTERED, // initial y position
- window_width, // width, in pixels
- window_height, // height, in pixels
- SDL_WINDOW_OPENGL | SDL_WINDOW_RESIZABLE // Flags - Special Window Attributes
- );
- // Check that the window was successfully created
- if (window == NULL) {
- // In the case that the window could not be made...
- printf("%s\n", SDL_GetError());
- return 1;
- }
- //Create the OpenGL Context
- glcontext = SDL_GL_CreateContext(window);
- // Check that the glcontext was successfully created
- if (glcontext == NULL) {
- // In the case that the glcontext could not be made...
- printf("%s\n", SDL_GetError());
- return 1;
- }
- //Initialize GLEW
- glewExperimental = true; // Needed in core profile
- if (glewInit() != GLEW_OK) {
- fprintf(stderr, "Failed to initialize GLEW\n");
- return -1;
- }
- //Print Attempted GL Version of Context
- std::cout << "\nAttempting GL Version " << GL_MAJOR_VERSION_TARGET << "." << GL_MINOR_VERSION_TARGET << std::endl;
- //Print Actual GL Version of Context
- std::cout << "Actual: " << glGetString(GL_VERSION) << std::endl;
- //VAO stuff
- glGenVertexArrays(1, &VertexArrayID);
- glBindVertexArray(VertexArrayID);
- // Generate 1 buffer, put the resulting identifier in vertexbuffer
- glGenBuffers(1, &vertexbuffer);
- // The following commands will talk about our 'vertexbuffer' buffer
- glBindBuffer(GL_ARRAY_BUFFER, vertexbuffer);
- // Give our vertices to OpenGL.
- glBufferData(GL_ARRAY_BUFFER, sizeof(g_vertex_buffer_data), g_vertex_buffer_data, GL_STATIC_DRAW);
- // 1rst attribute buffer : vertices
- glEnableVertexAttribArray(0);
- glVertexAttribPointer(
- 0, // attribute 0. No particular reason for 0, but must match the layout in the shader.
- 3, // size
- GL_FLOAT, // type
- GL_FALSE, // normalized?
- 0, // stride
- (void*)0 // array buffer offset
- );
- //Clear the window
- glClearColor(0,0,0,1);
- //glClear(GL_COLOR_BUFFER_BIT);
- return 0;
- }
- void Quit()
- {
- //This kills the SDL
- SDL_GL_DeleteContext(glcontext);
- SDL_DestroyWindow(window);
- SDL_Quit();
- }
- void Render()
- {
- //Clear the window -- Maybe not necessary?
- glClear(GL_COLOR_BUFFER_BIT);
- // Draw the triangle !
- glDrawArrays(GL_TRIANGLES, 0, 3); // Starting from vertex 0; 3 vertices total -> 1 triangle
- glDisableVertexAttribArray(0);
- //Update the screen (Swap DisplayBuffers)
- SDL_GL_SwapWindow(window);
- }
- int main()
- {
- //Initialization
- if (Init() == 0)
- printf("\nInitialization complete!\n");
- while(running)
- {
- Render();
- while (SDL_PollEvent(&event)) {
- if (event.type == SDL_QUIT)
- running = false;
- }
- }
- Quit();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement