Facebook
From Stained Lechwe, 9 Years ago, written in C.
Embed
Download Paste or View Raw
Hits: 621
  1. // Mini Game Template by g.c.
  2. // win32: g++ szesciany.cpp -o szesciany.exe -Wall -pedantic -lSDL -lopengl32 -lglu32 -lSDL_image -lm
  3. // linux: g++ szesciany.c -o szesciany -Wall -pedantic -lSDL -lGL -lGLU -lSDL_image -lm
  4.  
  5. #include <stdio.h>
  6. #include <stdlib.h>
  7. #include <string.h>
  8. #include <stdint.h>
  9. #include <stdbool.h>
  10.  
  11. #include <GL/gl.h>
  12. #include <GL/glu.h>
  13.  
  14. // Sometimes this needs to be changed to just <SDL.h> and <SDL_image.h>
  15. // When in doubt, use sdl-config to find out
  16. #include <SDL/SDL.h>
  17. #include <SDL/SDL_image.h>
  18.  
  19. #ifdef _WIN32
  20. #  include <windows.h>
  21. #endif
  22.  
  23. //using namespace std;
  24.  
  25. // Remove SDL main hook on Windows
  26. #ifdef _WIN32
  27. #  undef main
  28. #endif
  29.  
  30. // Macros.
  31. #define UNUSED(a) ((void)(a))
  32.  
  33. // Globals.
  34. static int screen_width;
  35. static int screen_height;
  36. bool keys[SDLK_LAST];
  37. float ratio;
  38.  
  39.  
  40. // Functions.
  41. static bool InitSDL(bool fullscreen, int width, int height);
  42. static bool InitOpenGL();
  43.  
  44. unsigned int ImgToTexture(const char *filename);
  45. void DrawQuad(float x, float y, float w, float h);
  46. void DrawQuadRGBA(float x, float y, float w, float h, float r, float g, float b, float a);
  47. void DrawQuadTexture(float x, float y, float w, float h, unsigned int texture_id);
  48. void DrawCube(float x, float y, float z, float w, float h, float d);
  49. void DrawCubeRGBA(float x, float y, float z, float w, float h, float d, float r, float g, float b, float a);
  50.  
  51. static bool Events();
  52. static void Logic();
  53. static void Scene();
  54.  
  55. // ------------------------------------------------------------------
  56. // EVENTS
  57. // ------------------------------------------------------------------
  58.  
  59. // Events
  60. static bool
  61. Events() {
  62.   SDL_Event ev;
  63.   memset(&ev, 0, sizeof(ev));
  64.  
  65.   while(SDL_PollEvent(&ev)) {
  66.     switch(ev.type) {
  67.       case SDL_KEYUP:
  68.                                 keys[ev.key.keysym.sym] = false;      
  69.                                 break;
  70.      
  71.       case SDL_KEYDOWN:
  72.         if(ev.key.keysym.sym == SDLK_ESCAPE)
  73.           return false;
  74.                                 keys[ev.key.keysym.sym] = true;
  75.                                 break;
  76.        
  77.       case SDL_QUIT:
  78.         return false;      
  79.     }
  80.   }
  81.  
  82.   return true;
  83. }
  84.  
  85.  
  86. // ------------------------------------------------------------------
  87. // LOGIC
  88. // ------------------------------------------------------------------
  89.  
  90. // Logic
  91. static void
  92. Logic() {
  93. }
  94.  
  95. // ------------------------------------------------------------------
  96. // SCENE
  97. // ------------------------------------------------------------------
  98.  
  99. // Scene
  100. static void
  101. Scene() {
  102.   glEnable(GL_DEPTH_TEST);
  103.   glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  104.   glMatrixMode(GL_MODELVIEW);
  105.   glLoadIdentity();
  106.         glScalef(1.0f, -1.0f, 1.0f);
  107.  
  108.   glTranslatef(0.0f, 0.0f, -15.0f);
  109.         glRotatef(25.0f, -1.0f, 0.0f, 0.0f);
  110.  
  111.   // draw sth here
  112.   // -- EXAMPLE --
  113.   static float rotx;
  114.   //DrawQuadRGBA(0.0f, 0.0f, 1.0f, 2.0f, 1.0f, 1.0f, 0.0f, 0.0f);
  115.  
  116.         //DrawQuadTexture(0.0f, 0.0f, 1.0f, 1.0f, textures[TEX_TLO]);
  117.   // -- EXAMPLE --
  118.         // for(int z=-1; z<2; z++)
  119.                 // for(int x=-1; x<2; x++)
  120.                         // for(int y=-1; y<2; y++)
  121.                                 // {
  122.                                         // glPushMatrix();
  123.                                         // glRotatef(rotx -= 1.0f*ratio, 0.0f, 1.0f, 0.0f);
  124.                                         // DrawCubeRGBA(3.0f * x, 3.0f * y, 3.0f * z, 1.5f, 1.5f, 1.5f, 0.5f, 1.0f, 1.0f, 0.5f);
  125.                                         // glPopMatrix();
  126.                                 // }
  127.                                                                
  128.                 for(int z=-1; z<2; z++)
  129.                 for(int x=-1; x<2; x++)
  130.                         for(int y=-1; y<2; y++)
  131.                                 {
  132.                                                                         if(z == -1)
  133.                                                                         {
  134.                                                                                 switch(x)
  135.                                                                                 {
  136.                                                                                         case -1:
  137.                                                                                         {
  138.                                                                                                 glPushMatrix();
  139.                                                                                                 glTranslatef(-3.0, -1.5, -3.0);
  140.                                                                                                 glRotatef(rotx -= 1.0f*ratio, 0.0f, -1.0f, 0.0f);
  141.                                                                                                 glTranslatef(3.0f, 1.5, 3.0f);
  142.                                                                                                 DrawCubeRGBA(3.0f * x, 3.0f * y, 3.0f * z, 1.5f, 1.5f, 1.5f, 0.5f, 1.0f, 1.0f, 0.5f);
  143.                                                                                                 glPopMatrix();
  144.                                                                                                 break;
  145.                                                                                         }
  146.                                                                                         case 0:
  147.                                                                                         {
  148.                                                                                                 glPushMatrix();
  149.                                                                                                 glTranslatef(0,0,-3.0);
  150.                                                                                                 glRotatef(rotx -= 1.0f*ratio, 0.0f, -1.0f, 0.0f);
  151.                                                                                                 glTranslatef(0,0,3.0);
  152.                                                                                                 DrawCubeRGBA(3.0f * x, 3.0f * y, 3.0f * z, 1.5f, 1.5f, 1.5f, 0.5f, 1.0f, 1.0f, 0.5f);
  153.                                                                                                 glPopMatrix();
  154.                                                                                                 break;
  155.                                                                                         }
  156.                                                                                         case 1:
  157.                                                                                         {
  158.                                                                                                 glPushMatrix();
  159.                                                                                                 glTranslatef(3,0,-3.0);
  160.                                                                                                 glRotatef(rotx -= 1.0f*ratio, 0.0f, -1.0f, 0.0f);
  161.                                                                                                 glTranslatef(-3,0,3.0);
  162.                                                                                                 DrawCubeRGBA(3.0f * x, 3.0f * y, 3.0f * z, 1.5f, 1.5f, 1.5f, 0.5f, 1.0f, 1.0f, 0.5f);
  163.                                                                                                 glPopMatrix();
  164.                                                                                                 break;
  165.                                                                                         }
  166.                                                                                 }
  167.                                                                         }
  168.                                                                         else if (z == 0)
  169.                                                                         {
  170.                                                                                         switch(x)
  171.                                                                                 {
  172.                                                                                         case -1:
  173.                                                                                         {
  174.                                                                                                 glPushMatrix();
  175.                                                                                                 glTranslatef(-3.0, -1.5, 0.0);
  176.                                                                                                 glRotatef(rotx -= 1.0f*ratio, 0.0f, -1.0f, 0.0f);
  177.                                                                                                 glTranslatef(3.0f, 1.5, 0.0f);
  178.                                                                                                 DrawCubeRGBA(3.0f * x, 3.0f * y, 3.0f * z, 1.5f, 1.5f, 1.5f, 0.5f, 1.0f, 1.0f, 0.5f);
  179.                                                                                                 glPopMatrix();
  180.                                                                                                 break;
  181.                                                                                         }
  182.                                                                                         case 0:
  183.                                                                                         {
  184.                                                                                                 glPushMatrix();
  185.                                                                                                 glTranslatef(0,0,0);
  186.                                                                                                 glRotatef(rotx -= 1.0f*ratio, 0.0f, -1.0f, 0.0f);
  187.                                                                                                 glTranslatef(0,0,0);
  188.                                                                                                 DrawCubeRGBA(3.0f * x, 3.0f * y, 3.0f * z, 1.5f, 1.5f, 1.5f, 0.5f, 1.0f, 1.0f, 0.5f);
  189.                                                                                                 glPopMatrix();
  190.                                                                                                 break;
  191.                                                                                         }
  192.                                                                                         case 1:
  193.                                                                                         {
  194.                                                                                                 glPushMatrix();
  195.                                                                                                 glTranslatef(3,0,0);
  196.                                                                                                 glRotatef(rotx -= 1.0f*ratio, 0.0f, -1.0f, 0.0f);
  197.                                                                                                 glTranslatef(-3,0,0);
  198.                                                                                                 DrawCubeRGBA(3.0f * x, 3.0f * y, 3.0f * z, 1.5f, 1.5f, 1.5f, 0.5f, 1.0f, 1.0f, 0.5f);
  199.                                                                                                 glPopMatrix();
  200.                                                                                                 break;
  201.                                                                                         }
  202.                                                                                 }
  203.                                                                         }
  204.                                                                         else if (z == 1)
  205.                                                                         {
  206.                                                                                         switch(x)
  207.                                                                                 {
  208.                                                                                         case -1:
  209.                                                                                         {
  210.                                                                                                 glPushMatrix();
  211.                                                                                                 glTranslatef(-3.0, -1.5, 3.0);
  212.                                                                                                 glRotatef(rotx -= 1.0f*ratio, 0.0f, -1.0f, 0.0f);
  213.                                                                                                 glTranslatef(3.0f, 1.5, -3.0f);
  214.                                                                                                 DrawCubeRGBA(3.0f * x, 3.0f * y, 3.0f * z, 1.5f, 1.5f, 1.5f, 0.5f, 1.0f, 1.0f, 0.5f);
  215.                                                                                                 glPopMatrix();
  216.                                                                                                 break;
  217.                                                                                         }
  218.                                                                                         case 0:
  219.                                                                                         {
  220.                                                                                                 glPushMatrix();
  221.                                                                                                 glTranslatef(0,0,3);
  222.                                                                                                 glRotatef(rotx -= 1.0f*ratio, 0.0f, -1.0f, 0.0f);
  223.                                                                                                 glTranslatef(0,0,-3);
  224.                                                                                                 DrawCubeRGBA(3.0f * x, 3.0f * y, 3.0f * z, 1.5f, 1.5f, 1.5f, 0.5f, 1.0f, 1.0f, 0.5f);
  225.                                                                                                 glPopMatrix();
  226.                                                                                                 break;
  227.                                                                                         }
  228.                                                                                         case 1:
  229.                                                                                         {
  230.                                                                                                 glPushMatrix();
  231.                                                                                                 glTranslatef(3,0,3);
  232.                                                                                                 glRotatef(rotx -= 1.0f*ratio, 0.0f, -1.0f, 0.0f);
  233.                                                                                                 glTranslatef(-3,0,-3);
  234.                                                                                                 DrawCubeRGBA(3.0f * x, 3.0f * y, 3.0f * z, 1.5f, 1.5f, 1.5f, 0.5f, 1.0f, 1.0f, 0.5f);
  235.                                                                                                 glPopMatrix();
  236.                                                                                                 break;
  237.                                                                                         }
  238.                                                                                 }
  239.                                                                         }
  240.  
  241.                                 }
  242.                                                                
  243.        
  244.   /* Use depth buffering for hidden surface elimination. */
  245.  
  246.   SDL_GL_SwapBuffers();
  247. }
  248.  
  249. // ------------------------------------------------------------------
  250. // Init & utils
  251. // ------------------------------------------------------------------
  252.  
  253. // main
  254. int
  255. main(int argc, char **argv, char **envp) {
  256.   // Unused.
  257.   UNUSED(argc);
  258.   UNUSED(argv);
  259.   UNUSED(envp);
  260.  
  261.   // Init SDL.
  262.   if(!InitSDL(false, 640, 480))    
  263.     return 1;
  264.  
  265.   // Init OpenGL.
  266.   if(!InitOpenGL())
  267.     return 2;
  268.  
  269.   // Title
  270.   SDL_WM_SetCaption("Szesciany", "Szesciany");
  271.  
  272.   // Main loop:
  273.   unsigned int last_time = SDL_GetTicks();
  274.   ratio = 0.0f;
  275.  
  276.   for(;;) {
  277.  
  278.     // Main stuff.
  279.     if(!Events())
  280.       break;
  281.  
  282.     Logic();
  283.     Scene();
  284.  
  285.     // Calc time.
  286.     unsigned int curr_time = SDL_GetTicks();
  287.     ratio = (float)(curr_time - last_time) / 1000.0f;
  288.     last_time = curr_time;
  289.   }
  290.  
  291.   // Done.
  292.   SDL_Quit();
  293.   return 0;
  294. }
  295.  
  296. // InitSDL
  297. static bool
  298. InitSDL(bool fullscreen, int width, int height) {
  299.   int screen_bpp;
  300.   int screen_flag = SDL_OPENGL;
  301.   if(fullscreen) screen_flag |= SDL_FULLSCREEN;
  302.  
  303.   screen_width = width;
  304.   screen_height = height;
  305.  
  306.   const SDL_VideoInfo *info;
  307.   SDL_Surface *surface;  
  308.  
  309.   if(SDL_Init(SDL_INIT_VIDEO) < 0)
  310.     return false;
  311.  
  312.   info = SDL_GetVideoInfo();
  313.   if(info == NULL)
  314.     goto err;
  315.  
  316.   screen_bpp = info->vfmt->BitsPerPixel;
  317.   SDL_GL_SetAttribute(SDL_GL_RED_SIZE,   8);
  318.   SDL_GL_SetAttribute(SDL_GL_GREEN_SIZE, 8);
  319.   SDL_GL_SetAttribute(SDL_GL_BLUE_SIZE,  8);
  320.   SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 16);
  321.   SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
  322.  
  323.   SDL_putenv("SDL_VIDEO_CENTERED=center");
  324.  
  325.   surface = SDL_SetVideoMode(
  326.             screen_width, screen_height,
  327.             screen_bpp, screen_flag);
  328.  
  329.   if(surface == NULL)
  330.     goto err;
  331.  
  332.   SDL_EnableKeyRepeat(SDL_DEFAULT_REPEAT_DELAY, SDL_DEFAULT_REPEAT_INTERVAL);
  333.  
  334.   return true;
  335.  
  336. err:
  337.   SDL_Quit();
  338.   return false;
  339. }
  340.  
  341. // InitOpenGL
  342. static bool
  343. InitOpenGL()
  344. {
  345.   float ratio;
  346.   ratio = (float)screen_width / (float)screen_height;
  347.  
  348.   glShadeModel(GL_SMOOTH);  
  349.   glClearColor(0, 0, 0, 0);  
  350.   glViewport(0, 0, screen_width, screen_height);  
  351.   glMatrixMode(GL_PROJECTION);
  352.   glLoadIdentity();
  353.   gluPerspective( 60.0, ratio, 1.0, 1024.0 );
  354.   glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);
  355.  
  356.  
  357.   glEnable(GL_DEPTH_TEST);
  358.   glEnable(GL_LIGHTING);
  359.   glEnable(GL_LIGHT0);
  360.  
  361.   return true;
  362. }
  363.  
  364.  
  365. // Note: older version of opengl required image width and height
  366. // to be a power of 2 (e.g. 16x32 or 512x128 or 256x256, etc).
  367. unsigned int
  368. ImgToTexture(const char *filename) {
  369.   // Load image.
  370.   SDL_Surface *img = IMG_Load(filename);
  371.   if(!img) {
  372.     fprintf(stderr, "error: ImgToTexture could not load image \"%s\"\n",
  373.             filename);
  374.     return ~0;
  375.   }
  376.  
  377.   // Image type? Only 24 and 32 supported, rest must be converted.
  378.   unsigned int img_type = 0;
  379.  
  380.   if(img->format->BitsPerPixel == 32) {
  381.     img_type = GL_RGBA;
  382.   } else if(img->format->BitsPerPixel == 24) {
  383.     img_type = GL_RGB;
  384.   } else {
  385.  
  386.     // Convert to 32 bits.
  387.     SDL_PixelFormat fmt = {
  388.       NULL, 32, 4,
  389.       0, 0, 0, 0,
  390.       0, 8, 16, 24,
  391.       0xff, 0xff00, 0xff0000, 0xff000000,
  392.       0,
  393.       0xff
  394.     };
  395.  
  396.     SDL_Surface *nimg = SDL_ConvertSurface(img, &fmt, SDL_SWSURFACE);
  397.     SDL_FreeSurface(img);
  398.  
  399.     if(!nimg) {
  400.       fprintf(stderr, "error: ImgToTexture could not convert image \"%s\" to 32-bit\n",
  401.               filename);
  402.       return ~0;
  403.     }
  404.  
  405.     // Done converting.
  406.     img = nimg;
  407.     img_type = GL_RGBA;
  408.   }
  409.  
  410.   // Create texture.
  411.   unsigned int texture_id = ~0;
  412.   glGenTextures(1, &texture_id);
  413.   glBindTexture(GL_TEXTURE_2D, texture_id);
  414.   glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
  415.  
  416.   // You might want to play with these parameters.
  417.   glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP);
  418.   glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP);
  419.   glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
  420.   glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
  421.   glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
  422.  
  423.   // Gen texture.
  424.   glTexImage2D(GL_TEXTURE_2D, 0, img_type, img->w, img->h,
  425.     0, img_type, GL_UNSIGNED_BYTE, img->pixels);
  426.  
  427.   printf("info: ImgToTexture loaded texture \"%s\" as %u\n",
  428.       filename, texture_id);
  429.  
  430.   // Done
  431.   SDL_FreeSurface(img);
  432.   return texture_id;
  433. }
  434.  
  435. void
  436. DrawQuad(float x, float y, float w, float h)
  437. {
  438.   w /= 2.0f; h /= 2.0f;
  439.  
  440.   glBegin(GL_QUADS);
  441.   glTexCoord2f(0.0f, 0.0f); glVertex2f(x - w, y - h);
  442.   glTexCoord2f(1.0f, 0.0f); glVertex2f(x + w, y - h);
  443.   glTexCoord2f(1.0f, 1.0f); glVertex2f(x + w, y + h);
  444.   glTexCoord2f(0.0f, 1.0f); glVertex2f(x - w, y + h);
  445.   glEnd();
  446. }
  447.  
  448. void
  449. DrawQuadRGBA(float x, float y, float w, float h, float r, float g, float b, float a)
  450. {
  451.   // Get old color.
  452.   float current_color[4];
  453.   glGetFloatv(GL_CURRENT_COLOR, current_color);
  454.  
  455.   // Set new color and draw quad.
  456.   glColor4f(r, g, b, a);
  457.   DrawQuad(x, y, w, h);
  458.  
  459.   // Set old color.
  460.   glColor4fv(current_color);
  461. }
  462.  
  463. void
  464. DrawQuadTexture(float x, float y, float w, float h, unsigned int texture_id)
  465. {
  466.   // Enable texturing if needed.
  467.   bool texturing_enabled = glIsEnabled(GL_TEXTURE_2D);
  468.   if(!texturing_enabled)
  469.     glEnable(GL_TEXTURE_2D);
  470.  
  471.   // Bind texture and draw.
  472.   glBindTexture(GL_TEXTURE_2D, texture_id);
  473.   DrawQuad(x, y, w, h);
  474.  
  475.   // Disable if was disable.
  476.   if(!texturing_enabled)
  477.     glDisable(GL_TEXTURE_2D);
  478. }
  479.  
  480. void
  481. DrawCube(float x, float y, float z, float w, float h, float d)
  482. {
  483.   w /= 2.0f; h /= 2.0f; d /= 2.0f;
  484.   glBegin(GL_QUADS); //top
  485.                 glNormal3f( 0.0F, 1.0F, 0.0F);
  486.                 glVertex3f(x + w, y + h, z - d);
  487.                 glVertex3f(x - w, y + h, z - d);
  488.                 glVertex3f(x - w, y + h, z + d);
  489.                 glVertex3f(x + w, y + h, z + d);              
  490.  
  491.                 glNormal3f( 0.0F, -1.0F, 0.0F);
  492.                 glVertex3f(x + w, y - h, z + d);          // Top Right Of The Quad (Bottom)
  493.                 glVertex3f(x - w, y - h, z + d);          // Top Left Of The Quad (Bottom)
  494.                 glVertex3f(x - w, y - h, z - d);          // Bottom Left Of The Quad (Bottom)
  495.                 glVertex3f(x + w, y - h, z - d);          // Bottom Right Of The Quad (Bottom)
  496.  
  497.                 glNormal3f( 0.0F, 0.0F, 1.0F);
  498.                 glVertex3f(x + w, y + h, z + d); //front
  499.                 glVertex3f(x - w, y + h, z + d);
  500.                 glVertex3f(x - w, y - h, z + d);
  501.                 glVertex3f(x + w, y - h, z + d);
  502.  
  503.                 glNormal3f( 0.0F, 0.0F, -1.0F);
  504.                 glVertex3f(x + w, y - h, z - d); //back
  505.                 glVertex3f(x - w, y - h, z - d);
  506.                 glVertex3f(x - w, y + h, z - d);
  507.                 glVertex3f(x + w, y + h, z - d);
  508.  
  509.                 glNormal3f( -1.0F, 0.0F, 0.0F);
  510.                 glVertex3f(x - w, y + h, z + d); //left
  511.                 glVertex3f(x - w, y + h, z - d);
  512.                 glVertex3f(x - w, y - h, z - d);
  513.                 glVertex3f(x - w, y - h, z + d);
  514.  
  515.                 glNormal3f( 1.0F, 0.0F, 0.0F);
  516.                 glVertex3f(x + w, y + h, z - d); //right
  517.                 glVertex3f(x + w, y + h, z + d);
  518.                 glVertex3f(x + w, y - h, z + d);
  519.                 glVertex3f(x + w, y - h, z - d);
  520.   glEnd();
  521. }
  522.  
  523. void
  524. DrawCubeRGBA(float x, float y, float z, float w, float h, float d, float r, float g, float b, float a)
  525. {
  526.           // Get old color.
  527.   float current_color[4];
  528.   glGetFloatv(GL_CURRENT_COLOR, current_color);
  529.  
  530.   // Set new color and draw quad.
  531.   glColor4f(r, g, b, a);
  532.   DrawCube(x, y, z, w, h, d);
  533.  
  534.   // Set old color.
  535.   glColor4fv(current_color);
  536. }
  537.