Advertisement
Guest User

Untitled

a guest
Oct 13th, 2015
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.19 KB | None | 0 0
  1.  
  2. #include <stdlib.h>
  3. #include <stdio.h>
  4. #include <string.h>
  5. #include <math.h>
  6.  
  7. #include <SDL2/SDL.h>
  8. #include <SDL2/SDL_opengl.h>
  9.  
  10.  
  11. void Display_InitGL()
  12. {
  13. /* Enable smooth shading */
  14. glShadeModel( GL_SMOOTH );
  15.  
  16. /* Set the background black */
  17. glClearColor( 0.0f, 0.0f, 0.0f, 0.0f );
  18.  
  19. /* Depth buffer setup */
  20. glClearDepth( 1.0f );
  21.  
  22. /* Enables Depth Testing */
  23. glEnable( GL_DEPTH_TEST );
  24.  
  25. /* The Type Of Depth Test To Do */
  26. glDepthFunc( GL_LEQUAL );
  27.  
  28. /* Really Nice Perspective Calculations */
  29. glHint( GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST );
  30. }
  31. /* function to reset our viewport after a window resize */
  32. int Display_SetViewport( int width, int height )
  33. {
  34. /* Height / width ration */
  35. GLfloat ratio;
  36.  
  37. /* Protect against a divide by zero */
  38. if ( height == 0 ) {
  39. height = 1;
  40. }
  41.  
  42. ratio = ( GLfloat )width / ( GLfloat )height;
  43.  
  44. /* Setup our viewport. */
  45. glViewport( 0, 0, ( GLsizei )width, ( GLsizei )height );
  46.  
  47. /* change to the projection matrix and set our viewing volume. */
  48. glMatrixMode( GL_PROJECTION );
  49. glLoadIdentity( );
  50.  
  51. /* Set our perspective */
  52. gluPerspective( 45.0f, ratio, 0.1f, 100.0f );
  53.  
  54. /* Make sure we're chaning the model view and not the projection */
  55. glMatrixMode( GL_MODELVIEW );
  56.  
  57. /* Reset The View */
  58. glLoadIdentity( );
  59.  
  60. return 1;
  61. }
  62.  
  63. void Display_Render()
  64. {
  65. /* Set the background black */
  66. glClearColor( 0.0f, 0.0f, 0.0f, 0.0f );
  67. /* Clear The Screen And The Depth Buffer */
  68. glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
  69.  
  70. /* Move Left 1.5 Units And Into The Screen 6.0 */
  71. glLoadIdentity();
  72. glTranslatef( -1.5f, 0.0f, -6.0f );
  73.  
  74. glBegin( GL_TRIANGLES ); /* Drawing Using Triangles */
  75. glVertex3f( 0.0f, 1.0f, 0.0f ); /* Top */
  76. glVertex3f( -1.0f, -1.0f, 0.0f ); /* Bottom Left */
  77. glVertex3f( 1.0f, -1.0f, 0.0f ); /* Bottom Right */
  78. glEnd( ); /* Finished Drawing The Triangle */
  79.  
  80. /* Move Right 3 Units */
  81. glTranslatef( 3.0f, 0.0f, 0.0f );
  82.  
  83. glBegin( GL_QUADS ); /* Draw A Quad */
  84. glVertex3f( -1.0f, 1.0f, 0.0f ); /* Top Left */
  85. glVertex3f( 1.0f, 1.0f, 0.0f ); /* Top Right */
  86. glVertex3f( 1.0f, -1.0f, 0.0f ); /* Bottom Right */
  87. glVertex3f( -1.0f, -1.0f, 0.0f ); /* Bottom Left */
  88. glEnd( ); /* Done Drawing The Quad */
  89.  
  90. SDL_RenderPresent(displayRenderer);
  91. }
  92.  
  93.  
  94. int
  95. main(int argc, char *argv[])
  96. {
  97. SDL_Init(SDL_INIT_VIDEO);
  98. SDL_Window* displayWindow;
  99. SDL_Renderer* displayRenderer;
  100. SDL_RendererInfo displayRendererInfo;
  101. SDL_CreateWindowAndRenderer(800, 600, SDL_WINDOW_OPENGL, &displayWindow, &displayRenderer);
  102. SDL_GetRendererInfo(displayRenderer, &displayRendererInfo);
  103. /*TODO: Check that we have OpenGL */
  104. if ((displayRendererInfo.flags & SDL_RENDERER_ACCELERATED) == 0 ||
  105. (displayRendererInfo.flags & SDL_RENDERER_TARGETTEXTURE) == 0) {
  106. /*TODO: Handle this. We have no render surface and not accelerated. */
  107. }
  108.  
  109.  
  110. Display_InitGL();
  111.  
  112. Display_SetViewport(800, 600);
  113.  
  114. Display_Render();
  115.  
  116.  
  117. SDL_Delay(5000);
  118. SDL_Quit();
  119.  
  120. return 0;
  121. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement