Advertisement
Guest User

Untitled

a guest
Oct 5th, 2016
221
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 11.04 KB | None | 0 0
  1. /*
  2. * SDL OpenGL Tutorial.
  3. * (c) Michael Vance, 2000
  4. * briareos@lokigames.com
  5. *
  6. * Distributed under terms of the LGPL.
  7. */
  8. #ifdef WIN32
  9. #include <windows.h>
  10. #endif
  11.  
  12. #include <iostream>
  13. #include <sstream>
  14.  
  15. #include <SDL.h>
  16.  
  17. #ifdef USE_OPENGL_ES
  18. #include <GLES/gl.h>
  19. #endif
  20.  
  21. #ifdef USE_OPENGL_DESKTOP
  22. //why the hell this naming inconsistency exists is well beyond me
  23. #include <SDL_opengl.h>
  24. #include <GL/glu.h>
  25. #endif
  26.  
  27. #include <stdio.h>
  28. #include <stdlib.h>
  29.  
  30. static GLboolean should_rotate = GL_TRUE;
  31. SDL_Window* pWnd = NULL;
  32.  
  33. static void quit_tutorial( int code )
  34. {
  35. /*
  36. * Quit SDL so we can release the fullscreen
  37. * mode and restore the previous video settings,
  38. * etc.
  39. */
  40. SDL_Quit( );
  41.  
  42. /* Exit program. */
  43. exit( code );
  44. }
  45.  
  46. static void handle_key_down(SDL_KeyboardEvent* keyEv )
  47. {
  48.  
  49. /*
  50. * We're only interested if 'Esc' has
  51. * been presssed.
  52. *
  53. * EXERCISE:
  54. * Handle the arrow keys and have that change the
  55. * viewing position/angle.
  56. */
  57. switch(keyEv->keysym.sym) {
  58. case SDLK_ESCAPE:
  59. quit_tutorial( 0 );
  60. break;
  61. case SDLK_SPACE:
  62. should_rotate = !should_rotate;
  63. break;
  64. default:
  65. break;
  66. }
  67.  
  68. }
  69.  
  70. static void process_events( void )
  71. {
  72. /* Our SDL event placeholder. */
  73. SDL_Event event;
  74.  
  75. /* Grab all the events off the queue. */
  76. while( SDL_PollEvent( &event ) ) {
  77.  
  78. switch( event.type ) {
  79. case SDL_KEYDOWN:
  80. /* Handle key presses. */
  81. handle_key_down( &event.key );
  82. break;
  83. case SDL_QUIT:
  84. /* Handle quit requests (like Ctrl-c). */
  85. quit_tutorial( 0 );
  86. break;
  87. }
  88.  
  89. }
  90.  
  91. }
  92.  
  93. Uint32 profileStartTime = 0u;
  94. void profileStart() {
  95. profileStartTime = SDL_GetTicks();
  96. }
  97.  
  98. void profileEnd(const std::string& name) {
  99. Uint32 end = SDL_GetTicks();
  100. //std::cout << "Profile - " << name << ": " << end - profileStartTime << "\n";
  101. }
  102.  
  103. static void draw_screen( void )
  104. {
  105. /* Our angle of rotation. */
  106. static float angle = 0.0f;
  107.  
  108. /*
  109. * EXERCISE:
  110. * Replace this awful mess with vertex
  111. * arrays and a call to glDrawElements.
  112. *
  113. * EXERCISE:
  114. * After completing the above, change
  115. * it to use compiled vertex arrays.
  116. *
  117. * EXERCISE:
  118. * Verify my windings are correct here ;).
  119. */
  120. static GLfloat v0[] = { -1.0f, -1.0f, 1.0f };
  121. static GLfloat v1[] = { 1.0f, -1.0f, 1.0f };
  122. static GLfloat v2[] = { 1.0f, 1.0f, 1.0f };
  123. static GLfloat v3[] = { -1.0f, 1.0f, 1.0f };
  124. static GLfloat v4[] = { -1.0f, -1.0f, -1.0f };
  125. static GLfloat v5[] = { 1.0f, -1.0f, -1.0f };
  126. static GLfloat v6[] = { 1.0f, 1.0f, -1.0f };
  127. static GLfloat v7[] = { -1.0f, 1.0f, -1.0f };
  128. static GLubyte red[] = { 255, 0, 0, 255 };
  129. static GLubyte green[] = { 0, 255, 0, 255 };
  130. static GLubyte blue[] = { 0, 0, 255, 255 };
  131. static GLubyte white[] = { 255, 255, 255, 255 };
  132. static GLubyte yellow[] = { 0, 255, 255, 255 };
  133. static GLubyte black[] = { 0, 0, 0, 255 };
  134. static GLubyte orange[] = { 255, 255, 0, 255 };
  135. static GLubyte purple[] = { 255, 0, 255, 0 };
  136.  
  137. /* Clear the color and depth buffers. */
  138. glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
  139.  
  140. /* We don't want to modify the projection matrix. */
  141. glMatrixMode( GL_MODELVIEW );
  142. glLoadIdentity( );
  143.  
  144. /* Move down the z-axis. */
  145. glTranslatef( 0.0, 0.0, -5.0 );
  146.  
  147. /* Rotate. */
  148. glRotatef( angle, 0.0, 1.0, 0.0 );
  149.  
  150. if( should_rotate ) {
  151.  
  152. if( ++angle > 360.0f ) {
  153. angle = 0.0f;
  154. }
  155.  
  156. }
  157.  
  158. /* Send our triangle data to the pipeline. */
  159. glBegin( GL_TRIANGLES );
  160.  
  161. glColor4ubv( red );
  162. glVertex3fv( v0 );
  163. glColor4ubv( green );
  164. glVertex3fv( v1 );
  165. glColor4ubv( blue );
  166. glVertex3fv( v2 );
  167.  
  168. glColor4ubv( red );
  169. glVertex3fv( v0 );
  170. glColor4ubv( blue );
  171. glVertex3fv( v2 );
  172. glColor4ubv( white );
  173. glVertex3fv( v3 );
  174.  
  175. glColor4ubv( green );
  176. glVertex3fv( v1 );
  177. glColor4ubv( black );
  178. glVertex3fv( v5 );
  179. glColor4ubv( orange );
  180. glVertex3fv( v6 );
  181.  
  182. glColor4ubv( green );
  183. glVertex3fv( v1 );
  184. glColor4ubv( orange );
  185. glVertex3fv( v6 );
  186. glColor4ubv( blue );
  187. glVertex3fv( v2 );
  188.  
  189. glColor4ubv( black );
  190. glVertex3fv( v5 );
  191. glColor4ubv( yellow );
  192. glVertex3fv( v4 );
  193. glColor4ubv( purple );
  194. glVertex3fv( v7 );
  195.  
  196. glColor4ubv( black );
  197. glVertex3fv( v5 );
  198. glColor4ubv( purple );
  199. glVertex3fv( v7 );
  200. glColor4ubv( orange );
  201. glVertex3fv( v6 );
  202.  
  203. glColor4ubv( yellow );
  204. glVertex3fv( v4 );
  205. glColor4ubv( red );
  206. glVertex3fv( v0 );
  207. glColor4ubv( white );
  208. glVertex3fv( v3 );
  209.  
  210. glColor4ubv( yellow );
  211. glVertex3fv( v4 );
  212. glColor4ubv( white );
  213. glVertex3fv( v3 );
  214. glColor4ubv( purple );
  215. glVertex3fv( v7 );
  216.  
  217. glColor4ubv( white );
  218. glVertex3fv( v3 );
  219. glColor4ubv( blue );
  220. glVertex3fv( v2 );
  221. glColor4ubv( orange );
  222. glVertex3fv( v6 );
  223.  
  224. glColor4ubv( white );
  225. glVertex3fv( v3 );
  226. glColor4ubv( orange );
  227. glVertex3fv( v6 );
  228. glColor4ubv( purple );
  229. glVertex3fv( v7 );
  230.  
  231. glColor4ubv( green );
  232. glVertex3fv( v1 );
  233. glColor4ubv( red );
  234. glVertex3fv( v0 );
  235. glColor4ubv( yellow );
  236. glVertex3fv( v4 );
  237.  
  238. glColor4ubv( green );
  239. glVertex3fv( v1 );
  240. glColor4ubv( yellow );
  241. glVertex3fv( v4 );
  242. glColor4ubv( black );
  243. glVertex3fv( v5 );
  244.  
  245. glEnd( );
  246.  
  247. /*
  248. * EXERCISE:
  249. * Draw text telling the user that 'Spc'
  250. * pauses the rotation and 'Esc' quits.
  251. * Do it using vetors and textured quads.
  252. */
  253.  
  254. /*
  255. * Swap the buffers. This this tells the driver to
  256. * render the next frame from the contents of the
  257. * back-buffer, and to set all rendering operations
  258. * to occur on what was the front-buffer.
  259. *
  260. * Double buffering prevents nasty visual tearing
  261. * from the application drawing on areas of the
  262. * screen that are being updated at the same time.
  263. */
  264. profileEnd("Rest");
  265. profileStart();
  266. SDL_GL_SwapWindow(pWnd);
  267. profileEnd("Swap");
  268. profileStart();
  269. }
  270.  
  271. static void setup_opengl( int width, int height )
  272. {
  273. float ratio = (float) width / (float) height;
  274.  
  275. /* Our shading model--Gouraud (smooth). */
  276. glShadeModel( GL_SMOOTH );
  277.  
  278. /* Culling. */
  279. glCullFace( GL_BACK );
  280. glFrontFace( GL_CCW );
  281. glEnable( GL_CULL_FACE );
  282.  
  283. /* Set the clear color. */
  284. glClearColor( 0, 0, 0, 0 );
  285.  
  286. /* Setup our viewport. */
  287. glViewport( 0, 0, width, height );
  288.  
  289. /*
  290. * Change to the projection matrix and set
  291. * our viewing volume.
  292. */
  293. glMatrixMode( GL_PROJECTION );
  294. glLoadIdentity( );
  295. /*
  296. * EXERCISE:
  297. * Replace this with a call to glFrustum.
  298. */
  299. gluPerspective( 60.0, ratio, 1.0, 1024.0 );
  300. }
  301.  
  302. bool vsync = false;
  303. int main( int argc, char* argv[] )
  304. {
  305. for(int i=1; i < argc; ++i)
  306. {
  307. if (strcmp(argv[i], "vsync") == 0)
  308. vsync = true;
  309. }
  310.  
  311. /* Information about the current video settings. */
  312. //const SDL_VideoInfo* info = NULL;
  313. /* Dimensions of our window. */
  314. int width = 0;
  315. int height = 0;
  316. /* Color depth in bits of our window. */
  317. int bpp = 0;
  318. /* Flags we will pass into SDL_SetVideoMode. */
  319. int flags = 0;
  320.  
  321. /* First, initialize SDL's video subsystem. */
  322. if( SDL_Init( SDL_INIT_VIDEO ) < 0 ) {
  323. /* Failed, exit. */
  324. fprintf( stderr, "Video initialization failed: %s\n",
  325. SDL_GetError( ) );
  326. quit_tutorial( 1 );
  327. }
  328. // SDL_SetVideoMode(640, 480, 16, SDL_DOUBLEBUF);//|SDL_FULLSCREEN);
  329.  
  330. /*
  331. * Set our width/height to 640/480 (you would
  332. * of course let the user decide this in a normal
  333. * app). We get the bpp we will request from
  334. * the display. On X11, VidMode can't change
  335. * resolution, so this is probably being overly
  336. * safe. Under Win32, ChangeDisplaySettings
  337. * can change the bpp.
  338. */
  339. width = 1920;
  340. height = 1080;
  341. //bpp = info->vfmt->BitsPerPixel;
  342.  
  343. /*
  344. * Now, we want to setup our requested
  345. * window attributes for our OpenGL window.
  346. * We want *at least* 5 bits of red, green
  347. * and blue. We also want at least a 16-bit
  348. * depth buffer.
  349. *
  350. * The last thing we do is request a double
  351. * buffered window. '1' turns on double
  352. * buffering, '0' turns it off.
  353. *
  354. * Note that we do not use SDL_DOUBLEBUF in
  355. * the flags to SDL_SetVideoMode. That does
  356. * not affect the GL attribute state, only
  357. * the standard 2D blitting setup.
  358. */
  359. SDL_GL_SetAttribute( SDL_GL_RED_SIZE, 5 );
  360. SDL_GL_SetAttribute( SDL_GL_GREEN_SIZE, 5 );
  361. SDL_GL_SetAttribute( SDL_GL_BLUE_SIZE, 5 );
  362. SDL_GL_SetAttribute( SDL_GL_DEPTH_SIZE, 16 );
  363. SDL_GL_SetAttribute( SDL_GL_DOUBLEBUFFER, 1 );
  364.  
  365. /*
  366. * We want to request that SDL provide us
  367. * with an OpenGL window, in a fullscreen
  368. * video mode.
  369. *
  370. * EXERCISE:
  371. * Make starting windowed an option, and
  372. * handle the resize events properly with
  373. * glViewport.
  374. */
  375. // flags = SDL_OPENGL | SDL_RESIZABLE;// | SDL_FULLSCREEN;
  376.  
  377. pWnd = SDL_CreateWindow("Playground", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, width, height, SDL_WINDOW_OPENGL | SDL_WINDOW_FULLSCREEN);
  378. if (!pWnd)
  379. {
  380. fprintf( stderr, "SDL_CreateWindow failed: %s\n",
  381. SDL_GetError( ) );
  382. quit_tutorial( 1 );
  383. }
  384.  
  385.  
  386. SDL_GLContext context = SDL_GL_CreateContext(pWnd);
  387.  
  388. int res = SDL_GL_SetSwapInterval(vsync ? 1 : 0);
  389. /* if (res != 0)
  390. res = SDL_GL_SetSwapInterval(-1);*/
  391. /*
  392. * At this point, we should have a properly setup
  393. * double-buffered window for use with OpenGL.
  394. */
  395. setup_opengl( width, height );
  396.  
  397. /*
  398. * Now we want to begin our normal app process--
  399. * an event loop with a lot of redrawing.
  400. */
  401. unsigned long frameCount = 0ul;
  402. unsigned long frameCountTotal = 0ul;
  403. Uint32 prevTicks = 0u;
  404. unsigned long fpsInterval = 1000u;
  405. Uint32 startTime = SDL_GetTicks();
  406. while( 1 ) {
  407.  
  408. Uint32 curTicks = SDL_GetTicks();
  409.  
  410. if (curTicks - prevTicks > fpsInterval)
  411. {
  412. float fps = frameCount / static_cast<float>(fpsInterval) * 1000.0f;
  413. prevTicks = curTicks;
  414. frameCount = 0ul;
  415.  
  416.  
  417. std::cout << "FPS: " << fps << "\n";
  418. }
  419.  
  420. /* Process incoming events. */
  421. // process_events( );
  422. /* Draw the screen. */
  423. draw_screen( );
  424.  
  425. ++frameCount;
  426. ++frameCountTotal;
  427.  
  428. if (frameCountTotal == 3000)
  429. break;
  430. }
  431.  
  432. /*
  433. * EXERCISE:
  434. * Record timings using SDL_GetTicks() and
  435. * and print out frames per second at program
  436. * end.
  437. */
  438.  
  439. /* Never reached. */
  440. Uint32 endTime = SDL_GetTicks();
  441. std::cout << "Runtime: " << (endTime - startTime) / 1000.0f << " s\n";
  442.  
  443. return 0;
  444. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement