Advertisement
Guest User

Untitled

a guest
Feb 11th, 2016
52
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.04 KB | None | 0 0
  1. #include <GL/glew.h>
  2. #include <GL/gl.h>
  3. #include <GL/glu.h>
  4. #include <stdio.h>
  5. #include <iostream>
  6. #include <SDL2/SDL.h>
  7. #include <SDL2/SDL_version.h>
  8. #include <SDL2/SDL_revision.h>
  9.  
  10. int window_width = 960;
  11. int window_height = 640;
  12. const char* window_title = "Gaem 0.0.1";
  13.  
  14. const int GL_MAJOR_VERSION_TARGET = 3;
  15. const int GL_MINOR_VERSION_TARGET = 2;
  16.  
  17. SDL_Window *window;
  18. SDL_GLContext glcontext;
  19.  
  20. GLuint VertexArrayID;
  21.  
  22. // This will identify our vertex buffer
  23. GLuint vertexbuffer;
  24.  
  25. // An array of 3 vectors which represents 3 vertices
  26. static const GLfloat g_vertex_buffer_data[] = {
  27. -1.0f, -1.0f, 0.0f,
  28. 1.0f, -1.0f, 0.0f,
  29. 0.0f, 1.0f, 0.0f,
  30. };
  31.  
  32. SDL_Event event;
  33. bool running = true;
  34.  
  35.  
  36. int Init()
  37. {
  38.  
  39. //SDL_INIT_EVERYTHING because why not
  40. SDL_Init(SDL_INIT_EVERYTHING);
  41.  
  42. //Checking if I have SDL2 properly installed...
  43. SDL_version compiled;
  44. SDL_version linked;
  45. SDL_VERSION(&compiled);
  46. SDL_GetVersion(&linked);
  47. printf("Compiled against SDL version %d.%d.%d \n", compiled.major, compiled.minor, compiled.patch);
  48. printf("Linking against SDL version %d.%d.%d\n", linked.major, linked.minor, linked.patch);
  49.  
  50. //Setting GL Context attributes
  51. SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_CORE);
  52. //SDL_GL_SetAttribute(SDL_GL_ACCELERATED_VISUAL, 1);
  53. SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, GL_MAJOR_VERSION_TARGET);
  54. SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, GL_MINOR_VERSION_TARGET);
  55.  
  56. SDL_GL_SetAttribute(SDL_GL_RED_SIZE, 8);
  57. SDL_GL_SetAttribute(SDL_GL_GREEN_SIZE, 8);
  58. SDL_GL_SetAttribute(SDL_GL_BLUE_SIZE, 8);
  59.  
  60. SDL_GL_SetAttribute(SDL_GL_BUFFER_SIZE, 32);
  61. SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
  62. SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 24);
  63.  
  64. //Create window
  65. window = SDL_CreateWindow(
  66. window_title, // window title
  67. SDL_WINDOWPOS_CENTERED, // initial x position
  68. SDL_WINDOWPOS_CENTERED, // initial y position
  69. window_width, // width, in pixels
  70. window_height, // height, in pixels
  71. SDL_WINDOW_OPENGL | SDL_WINDOW_RESIZABLE // Flags - Special Window Attributes
  72. );
  73.  
  74. // Check that the window was successfully created
  75. if (window == NULL) {
  76. // In the case that the window could not be made...
  77. printf("%s\n", SDL_GetError());
  78. return 1;
  79. }
  80.  
  81. //Create the OpenGL Context
  82. glcontext = SDL_GL_CreateContext(window);
  83.  
  84. // Check that the glcontext was successfully created
  85. if (glcontext == NULL) {
  86. // In the case that the glcontext could not be made...
  87. printf("%s\n", SDL_GetError());
  88. return 1;
  89. }
  90.  
  91. //Initialize GLEW
  92. glewExperimental = true; // Needed in core profile
  93. if (glewInit() != GLEW_OK) {
  94. fprintf(stderr, "Failed to initialize GLEW\n");
  95. return -1;
  96. }
  97.  
  98. //Print Attempted GL Version of Context
  99. std::cout << "\nAttempting GL Version " << GL_MAJOR_VERSION_TARGET << "." << GL_MINOR_VERSION_TARGET << std::endl;
  100. //Print Actual GL Version of Context
  101. std::cout << "Actual: " << glGetString(GL_VERSION) << std::endl;
  102.  
  103. //VAO stuff
  104. glGenVertexArrays(1, &VertexArrayID);
  105. glBindVertexArray(VertexArrayID);
  106.  
  107. // Generate 1 buffer, put the resulting identifier in vertexbuffer
  108. glGenBuffers(1, &vertexbuffer);
  109. // The following commands will talk about our 'vertexbuffer' buffer
  110. glBindBuffer(GL_ARRAY_BUFFER, vertexbuffer);
  111. // Give our vertices to OpenGL.
  112. glBufferData(GL_ARRAY_BUFFER, sizeof(g_vertex_buffer_data), g_vertex_buffer_data, GL_STATIC_DRAW);
  113.  
  114. // 1rst attribute buffer : vertices
  115. glEnableVertexAttribArray(0);
  116. glVertexAttribPointer(
  117. 0, // attribute 0. No particular reason for 0, but must match the layout in the shader.
  118. 3, // size
  119. GL_FLOAT, // type
  120. GL_FALSE, // normalized?
  121. 0, // stride
  122. (void*)0 // array buffer offset
  123. );
  124.  
  125. //Clear the window
  126. glClearColor(0,0,0,1);
  127. //glClear(GL_COLOR_BUFFER_BIT);
  128.  
  129. return 0;
  130. }
  131.  
  132. void Quit()
  133. {
  134. //This kills the SDL
  135. SDL_GL_DeleteContext(glcontext);
  136. SDL_DestroyWindow(window);
  137. SDL_Quit();
  138. }
  139.  
  140. void Render()
  141. {
  142. //Clear the window -- Maybe not necessary?
  143. glClear(GL_COLOR_BUFFER_BIT);
  144.  
  145. // Draw the triangle !
  146. glDrawArrays(GL_TRIANGLES, 0, 3); // Starting from vertex 0; 3 vertices total -> 1 triangle
  147. glDisableVertexAttribArray(0);
  148.  
  149. //Update the screen (Swap DisplayBuffers)
  150. SDL_GL_SwapWindow(window);
  151. }
  152.  
  153. int main()
  154. {
  155. //Initialization
  156. if (Init() == 0)
  157. printf("\nInitialization complete!\n");
  158.  
  159.  
  160. while(running)
  161. {
  162. Render();
  163.  
  164. while (SDL_PollEvent(&event)) {
  165. if (event.type == SDL_QUIT)
  166. running = false;
  167. }
  168. }
  169.  
  170. Quit();
  171. return 0;
  172. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement