Advertisement
Guest User

JeZ-l-Lee

a guest
Feb 15th, 2010
136
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 25.95 KB | None | 0 0
  1. /*
  2. -------------------------------------------------------------------------------------------
  3.         S.D.L.(R) 1.2.x GTR Twin Turbo(OpenGL)
  4.  
  5. Source (C)opyright 2010, by Silent Hero Productions(R)
  6. -------------------------------------------------------------------------------------------
  7. */
  8.  
  9. #include "SDL/SDL.h"
  10. #include "SDL/SDL_image.h"
  11. #include "SDL/SDL_ttf.h"
  12. #include "SDL/SDL_opengl.h"
  13.  
  14. #include <iostream>
  15. #include <stdio.h>
  16. #include <stdlib.h>
  17. #include <math.h>
  18.  
  19. bool CoreFAILURE = false;
  20. SDL_Event Event;
  21. bool EXITgame = false;
  22.  
  23. Uint32 SystemTicks = 0;
  24. Uint32 NextFrameTicks = 0;
  25. Uint32 NextSecondTick = 0;
  26. Uint32 NumberOfFramesPerSecond = 0;
  27. Uint32 FPSindex = 0;
  28. Uint32 FramesPerSecondArray[10] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
  29. Uint32 AverageFPS = 0;
  30.  
  31. struct PreLoadTexts
  32. {
  33.     GLuint OGL_TextTexture;
  34.     int TextWidth;
  35.     int TextHeight;
  36.     int TextureWidth;
  37.     int TextureHeight;
  38. } PreLoadTextsArray[1];
  39.  
  40. #define JustifyLeft             0
  41. #define JustifyCenter           1
  42. #define JustifyRight            2
  43. #define JustifyCenterOnPoint    3
  44. Uint32 rmask, gmask, bmask, amask;
  45. bool ColorIsRGBA = true;
  46. TTF_Font* Font = NULL;
  47. SDL_Color Color;
  48. char VariableText[64];
  49. int DrawText = 0;
  50. bool KeyUp = false;
  51. bool T_WasPressed = false;
  52. bool Q_IsPressed = false;
  53. bool W_IsPressed = false;
  54. bool A_IsPressed = false;
  55. bool S_IsPressed = false;
  56.  
  57. #define NumberOfHappyFaces      200
  58. Sint32 randomX[NumberOfHappyFaces];
  59. Sint32 randomY[NumberOfHappyFaces];
  60. #define NumberOfTexts           100
  61. Sint32 randomTextX[NumberOfTexts];
  62. Sint32 randomTextY[NumberOfTexts];
  63.  
  64. struct Sprite
  65. {
  66.     GLuint OGL_Texture;
  67.     int Width;
  68.     int Height;
  69.     int CenterX;
  70.     int CenterY;
  71.     GLfloat RotationDegree;
  72. } Sprites[2];
  73.  
  74. SDL_Surface *Surface;   // This surface will tell us the details of the image
  75. GLenum TextureFormat;
  76. GLint  NumberOfColors;
  77.  
  78. bool BMP_Dimensions_OK = true;
  79. bool BMP_To_Texture_OK = true;
  80.  
  81. int X_Offset = 320;
  82. int Y_Offset = 240;
  83.  
  84. //-------------------------------------------------------------------------------------------------
  85. int nextpoweroftwo(int x)
  86. {
  87.     double logbase2 = log(x) / log(2);
  88.     return round(pow(2,ceil(logbase2)));
  89. }
  90. //-------------------------------------------------------------------------------------------------
  91. void PreloadTestText()
  92. {
  93. SDL_Surface *initial;
  94. SDL_Surface *initialOutline;
  95. SDL_Surface *intermediary;
  96. SDL_Rect rect;
  97. int w,h;
  98. SDL_Color outline;
  99. SDL_Color color;
  100.  
  101.     PreLoadTextsArray[0].OGL_TextTexture = NULL;
  102.  
  103.     color.r = 255;
  104.     color.g = 0;
  105.     color.b = 0;
  106.  
  107.     outline.r = 1;
  108.     outline.g = 1;
  109.     outline.b = 1;
  110.  
  111.     // Use SDL_TTF to render our text
  112.     initial = TTF_RenderText_Solid(Font, "Hello, World!", color);
  113.     initialOutline = TTF_RenderText_Solid(Font, "Hello, World!", outline);
  114.  
  115.     PreLoadTextsArray[0].TextWidth = initial->w;
  116.     PreLoadTextsArray[0].TextHeight = initial->h;
  117.  
  118.     // Convert the rendered text to a known format
  119.     w = nextpoweroftwo(7+initial->w);
  120.     h = nextpoweroftwo(7+initial->h);
  121.  
  122.     PreLoadTextsArray[0].TextureWidth = w;
  123.     PreLoadTextsArray[0].TextureHeight = h;
  124.  
  125.     intermediary = SDL_CreateRGBSurface(0, w, h, 32, rmask, gmask, bmask, amask);
  126.  
  127.     rect.x = 3; rect.y = 0;
  128.     SDL_BlitSurface(initialOutline, 0, intermediary, &rect);
  129.     rect.x = 0; rect.y = 3;
  130.     SDL_BlitSurface(initialOutline, 0, intermediary, &rect);
  131.     rect.x = 6; rect.y = 3;
  132.     SDL_BlitSurface(initialOutline, 0, intermediary, &rect);
  133.     rect.x = 3; rect.y = 6;
  134.     SDL_BlitSurface(initialOutline, 0, intermediary, &rect);
  135.  
  136.     rect.x = 3;
  137.     rect.y = 3;
  138.     SDL_BlitSurface(initial, 0, intermediary, &rect);
  139.  
  140.     // Tell GL about our new texture
  141.     glGenTextures(1, &PreLoadTextsArray[0].OGL_TextTexture);
  142.     glBindTexture(GL_TEXTURE_2D, PreLoadTextsArray[0].OGL_TextTexture);
  143.     if (ColorIsRGBA == true)
  144.         glTexImage2D(GL_TEXTURE_2D, 0, 4, w, h, 0, GL_RGBA,
  145.         GL_UNSIGNED_BYTE, intermediary->pixels );
  146.     else
  147.         glTexImage2D(GL_TEXTURE_2D, 0, 4, w, h, 0, GL_BGRA,
  148.         GL_UNSIGNED_BYTE, intermediary->pixels );
  149.  
  150.     // Clean up
  151.     SDL_FreeSurface(initial);
  152.     SDL_FreeSurface(initialOutline);
  153.     SDL_FreeSurface(intermediary);
  154. }
  155. //-------------------------------------------------------------------------------------------------
  156. void DrawPreloadedTestText(int XJustification, Sint32 posX, Sint32 posY)
  157. {
  158.     if (XJustification == JustifyLeft)
  159.     {
  160.     }
  161.     else if (XJustification == JustifyCenter)
  162.     {
  163.         posX = 320 - (PreLoadTextsArray[0].TextWidth / 2) - 2;
  164.     }
  165.     else if (XJustification == JustifyRight)
  166.     {
  167.         posX = (640 - posX) - PreLoadTextsArray[0].TextWidth - 2;
  168.     }
  169.     else if (XJustification == JustifyCenterOnPoint)
  170.     {
  171.         posX = posX - (PreLoadTextsArray[0].TextWidth / 2) - 2;
  172.     }
  173.  
  174.     glBindTexture(GL_TEXTURE_2D, PreLoadTextsArray[0].OGL_TextTexture);
  175.  
  176.     // GL_NEAREST looks horrible, if scaled...
  177.     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
  178.     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
  179.  
  180.     // prepare to render our texture
  181.     glEnable(GL_TEXTURE_2D);
  182.     glBindTexture(GL_TEXTURE_2D, PreLoadTextsArray[0].OGL_TextTexture);
  183.     glColor3f(1.0f, 1.0f, 1.0f);
  184.  
  185.     // Draw a quad at location
  186.     glBegin(GL_QUADS);
  187.         // Recall that the origin is in the lower-left corner
  188.         // That is why the TexCoords specify different corners
  189.         // than the Vertex coors seem to.
  190.         glTexCoord2f(0.0f, 1.0f);
  191.         glVertex2f(posX    , posY + PreLoadTextsArray[0].TextureHeight);
  192.  
  193.         glTexCoord2f(1.0f, 1.0f);
  194.         glVertex2f(posX + PreLoadTextsArray[0].TextureWidth, posY + PreLoadTextsArray[0].TextureHeight);
  195.  
  196.         glTexCoord2f(1.0f, 0.0f);
  197.         glVertex2f(posX + PreLoadTextsArray[0].TextureWidth, posY    );
  198.  
  199.         glTexCoord2f(0.0f, 0.0f);
  200.         glVertex2f(posX    , posY    );
  201.     glEnd();
  202. }
  203. //-------------------------------------------------------------------------------------------------
  204. void SDL_GL_RenderText(const char *text, TTF_Font *font, SDL_Color color, int XJustification,
  205.                        Sint32 posX, Sint32 posY)
  206. {
  207. SDL_Surface *initial;
  208. SDL_Surface *initialOutline;
  209. SDL_Surface *intermediary;
  210. SDL_Rect rect;
  211. int w,h;
  212. GLuint textureThree;
  213. SDL_Color outline;
  214.  
  215.     if (XJustification == JustifyLeft)
  216.     {
  217.     }
  218.     else if (XJustification == JustifyCenter)
  219.     {
  220.         posX = 320 - (PreLoadTextsArray[0].TextWidth / 2) - 2;
  221.     }
  222.     else if (XJustification == JustifyRight)
  223.     {
  224.         posX = (640 - posX) - PreLoadTextsArray[0].TextWidth - 2;
  225.     }
  226.     else if (XJustification == JustifyCenterOnPoint)
  227.     {
  228.         posX = posX - (PreLoadTextsArray[0].TextWidth / 2) - 2;
  229.     }
  230.  
  231.     outline.r = 1;
  232.     outline.g = 1;
  233.     outline.b = 1;
  234.  
  235.     // Use SDL_TTF to render our text
  236.     initial = TTF_RenderText_Solid(font, text, color);
  237.     initialOutline = TTF_RenderText_Solid(font, text, outline);
  238.  
  239.     // Convert the rendered text to a known format
  240.     w = nextpoweroftwo(7+initial->w);
  241.     h = nextpoweroftwo(7+initial->h);
  242.  
  243.     intermediary = SDL_CreateRGBSurface(0, w, h, 32, rmask, gmask, bmask, amask);
  244.  
  245.     rect.x = 3; rect.y = 0;
  246.     SDL_BlitSurface(initialOutline, 0, intermediary, &rect);
  247.     rect.x = 0; rect.y = 3;
  248.     SDL_BlitSurface(initialOutline, 0, intermediary, &rect);
  249.     rect.x = 6; rect.y = 3;
  250.     SDL_BlitSurface(initialOutline, 0, intermediary, &rect);
  251.     rect.x = 3; rect.y = 6;
  252.     SDL_BlitSurface(initialOutline, 0, intermediary, &rect);
  253.  
  254.     rect.x = 3;
  255.     rect.y = 3;
  256.     SDL_BlitSurface(initial, 0, intermediary, &rect);
  257.  
  258.     // Tell GL about our new texture
  259.     glGenTextures(1, &textureThree);
  260.     glBindTexture(GL_TEXTURE_2D, textureThree);
  261.     if (ColorIsRGBA == true)
  262.         glTexImage2D(GL_TEXTURE_2D, 0, 4, w, h, 0, GL_RGBA,
  263.         GL_UNSIGNED_BYTE, intermediary->pixels );
  264.     else
  265.         glTexImage2D(GL_TEXTURE_2D, 0, 4, w, h, 0, GL_BGRA,
  266.         GL_UNSIGNED_BYTE, intermediary->pixels );
  267.  
  268.     // GL_NEAREST looks horrible, if scaled...
  269.     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
  270.     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
  271.  
  272.     // prepare to render our texture
  273.     glEnable(GL_TEXTURE_2D);
  274.     glBindTexture(GL_TEXTURE_2D, textureThree);
  275.     glColor3f(1.0f, 1.0f, 1.0f);
  276.  
  277.     // Draw a quad at location
  278.     glBegin(GL_QUADS);
  279.         // Recall that the origin is in the lower-left corner
  280.         // That is why the TexCoords specify different corners
  281.         // than the Vertex coors seem to.
  282.         glTexCoord2f(0.0f, 1.0f);
  283.         glVertex2f(posX    , posY + h);
  284.  
  285.         glTexCoord2f(1.0f, 1.0f);
  286.         glVertex2f(posX + w, posY + h);
  287.  
  288.         glTexCoord2f(1.0f, 0.0f);
  289.         glVertex2f(posX + w, posY    );
  290.  
  291.         glTexCoord2f(0.0f, 0.0f);
  292.         glVertex2f(posX    , posY    );
  293.     glEnd();
  294.  
  295.     // Bad things happen if we delete the texture before it finishes
  296.     glFinish();
  297.  
  298.     // return the deltas in the unused w,h part of the rect
  299. ///    location->w = initial->w;
  300. ///    location->h = initial->h;
  301.  
  302.     // Clean up
  303.     SDL_FreeSurface(initial);
  304.     SDL_FreeSurface(initialOutline);
  305.     SDL_FreeSurface(intermediary);
  306.     glDeleteTextures(1, &textureThree);
  307. }
  308. //-------------------------------------------------------------------------------------------------
  309. void CalculateRandomPositions(void)
  310. {
  311.     for (int index = 0; index < NumberOfHappyFaces; index++)
  312.     {
  313.         randomX[index] = rand()%1040;
  314.         randomX[index]-=200;
  315.         randomY[index] = rand()%880;
  316.         randomY[index]-=200;
  317.     }
  318.  
  319.     for (int index = 0; index < NumberOfTexts; index++)
  320.     {
  321.         randomTextX[index] = rand()%1040;
  322.         randomTextX[index]-=200;
  323.         randomTextY[index] = rand()%880;
  324.         randomTextY[index]-=200;
  325.     }
  326. }
  327. //-------------------------------------------------------------------------------------------------
  328. void MoveRandomPositions(void)
  329. {
  330.     for (int index = 0; index < NumberOfHappyFaces; index++)
  331.     {
  332.         randomY[index]-=1;
  333.         if (randomY[index] < -200)  randomY[index] = 680;
  334.     }
  335. }
  336. //-------------------------------------------------------------------------------------------------
  337. void DrawTestTextures(void)
  338. {
  339.     if (BMP_To_Texture_OK == true)
  340.     {
  341.         for (int index = 0; index < NumberOfHappyFaces;  index++)
  342.         {
  343.             // Bind the texture to which subsequent calls refer to
  344.             glBindTexture( GL_TEXTURE_2D, Sprites[0].OGL_Texture );
  345.  
  346.             glEnable(GL_BLEND);
  347.             glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
  348.  
  349.             glBegin( GL_QUADS ); // Draw textures
  350.                 //Top-left vertex (corner)
  351.                 glTexCoord2i( 0, 0 );
  352.                 glVertex3f( randomX[index]+Sprites[0].CenterX, randomY[index]+Sprites[0].CenterY, 0.0f );
  353.  
  354.                 //Bottom-left vertex (corner)
  355.                 glTexCoord2i( 1, 0 );
  356.                 glVertex3f( randomX[index]+Sprites[0].CenterX+Sprites[0].Width, randomY[index]+Sprites[0].CenterY, 0 );
  357.  
  358.                 //Bottom-right vertex (corner)
  359.                 glTexCoord2i( 1, 1 );
  360.                 glVertex3f( randomX[index]+Sprites[0].CenterX+Sprites[0].Width, randomY[index]+Sprites[0].CenterY+Sprites[0].Height, 0 );
  361.  
  362.                 //Top-right vertex (corner)
  363.                 glTexCoord2i( 0, 1 );
  364.                 glVertex3f( randomX[index]+Sprites[0].CenterX, randomY[index]+Sprites[0].CenterY+Sprites[0].Height, 0 );
  365.             glEnd();
  366.         }
  367.     }
  368. }
  369. //-------------------------------------------------------------------------------------------------
  370. void DrawMovableScalableTestTexture(void)
  371. {
  372.     if (BMP_To_Texture_OK == true)
  373.     {
  374.         // Bind the texture to which subsequent calls refer to
  375.         glBindTexture( GL_TEXTURE_2D, Sprites[1].OGL_Texture );
  376.  
  377.         glEnable(GL_BLEND);
  378.         glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
  379.  
  380.         glBegin( GL_QUADS ); // Draw texture with scaling
  381.                 //Top-left vertex (corner)
  382.                 glTexCoord2i( 0, 0 );
  383.                 glVertex3f( X_Offset-Sprites[1].CenterX, Y_Offset-Sprites[1].CenterY, 0.0f );
  384.  
  385.                 //Bottom-left vertex (corner)
  386.                 glTexCoord2i( 1, 0 );
  387.                 glVertex3f( X_Offset-Sprites[1].CenterX+Sprites[1].Width, Y_Offset-Sprites[1].CenterY, 0 );
  388.  
  389.                 //Bottom-right vertex (corner)
  390.                 glTexCoord2i( 1, 1 );
  391.                 glVertex3f( X_Offset-Sprites[1].CenterX+Sprites[1].Width, Y_Offset-Sprites[1].CenterY+Sprites[1].Height, 0 );
  392.  
  393.                 //Top-right vertex (corner)
  394.                 glTexCoord2i( 0, 1 );
  395.                 glVertex3f( X_Offset-Sprites[1].CenterX, Y_Offset-Sprites[1].CenterY+Sprites[1].Height, 0 );
  396.             glEnd();
  397.         glEnd();
  398.     }
  399. }
  400. //-------------------------------------------------------------------------------------------------
  401. int main( int argc, char* args[] )
  402. {
  403.     if ( SDL_Init(SDL_INIT_EVERYTHING) != 0 )
  404.     {
  405.         printf("ERROR: Unable to initialize SDL: %s\n", SDL_GetError());
  406.         CoreFAILURE = true;
  407.     }
  408.     else  printf("SDL (Everything) initialized\n");
  409.  
  410.     if ( TTF_Init() == -1 )
  411.     {
  412.         fprintf(stderr, "ERROR: TTF_Init: %s\n", TTF_GetError());
  413.         CoreFAILURE = true;
  414.     }
  415.     else fprintf(stdout, "SDL_TTF initialized\n");
  416.  
  417.     Font = NULL;
  418.     if( !(Font = TTF_OpenFont("data/fonts/default.ttf", 26)) )
  419.     {
  420.         printf("ERROR: loading font: %s\n", TTF_GetError());
  421.         CoreFAILURE = true;
  422.     }
  423.  
  424.     SDL_GL_SetAttribute( SDL_GL_DOUBLEBUFFER, 1 );
  425.  
  426.     putenv(strdup("SDL_VIDEO_CENTERED=1"));
  427.     SDL_WM_SetCaption("SDL+OpenGL(GTR Twin Turbo)", NULL);
  428.     SDL_Surface* screen = SDL_SetVideoMode( 640, 480, 0, SDL_OPENGL);
  429.     if (!screen)
  430.     {
  431.         printf("ERROR: SDL 640x480 window init failed\n");
  432.         CoreFAILURE = true;
  433.     }
  434.  
  435.     glEnable( GL_TEXTURE_2D );
  436.     glClearColor( 0.0f, 0.0f, 0.0f, 0.0f );
  437.     glViewport( 0, 0, 640, 480 );
  438.     glClear( GL_COLOR_BUFFER_BIT );
  439.     glMatrixMode( GL_PROJECTION );
  440.     glLoadIdentity();
  441.     glOrtho(0.0f, 640, 480, 0.0f, -1.0f, 1.0f);
  442.     glMatrixMode( GL_MODELVIEW );
  443.     glLoadIdentity();
  444.     printf("OpenGL initialized\n");
  445.  
  446.     Surface = NULL;
  447.     if ( (Surface = IMG_Load("data/graphics/image.png")) )
  448.     {
  449.         // Check that the image's width is a power of 2
  450.         if ( (Surface->w & (Surface->w - 1)) != 0 )
  451.         {
  452.             printf("ERROR: image.bmp's width is not a power of 2\n");
  453.             BMP_Dimensions_OK = false;
  454.             CoreFAILURE = true;
  455.         }
  456.  
  457.         // Also check if the height is a power of 2
  458.         if ( (Surface->h & (Surface->h - 1)) != 0 )
  459.         {
  460.             printf("ERROR: image.bmp's height is not a power of 2\n");
  461.             BMP_Dimensions_OK = false;
  462.             CoreFAILURE = true;
  463.         }
  464.  
  465.         Sprites[0].Width = Surface->w;
  466.         Sprites[0].Height = Surface->h;
  467.         Sprites[0].CenterX = Sprites[0].Width / 2;
  468.         Sprites[0].CenterY = Sprites[0].Height / 2;
  469.         Sprites[0].RotationDegree = 0;
  470.  
  471.         Sprites[1].Width = Surface->w;
  472.         Sprites[1].Height = Surface->h;
  473.         Sprites[1].CenterX = Sprites[1].Width / 2;
  474.         Sprites[1].CenterY = Sprites[1].Height / 2;
  475.         Sprites[1].RotationDegree = 0;
  476.  
  477.         if (BMP_Dimensions_OK == true)
  478.         {
  479.             // get the number of channels in the SDL surface
  480.             NumberOfColors = Surface->format->BytesPerPixel;
  481.             if (NumberOfColors == 4) // contains an alpha channel
  482.             {
  483.                 //if (Surface->format->Rmask == 0x000000ff)
  484.                     TextureFormat = GL_RGBA;
  485.                 //else
  486.                 //    TextureFormat = GL_BGRA;
  487.             }
  488.             else if (NumberOfColors == 3) // no alpha channel
  489.             {
  490.                 //if (Surface->format->Rmask == 0x000000ff)
  491.                     TextureFormat = GL_RGB;
  492.                 //else
  493.                 //    TextureFormat = GL_BGR;
  494.             }
  495.             else
  496.             {
  497.                 printf("ERROR: the image is not truecolor\n");
  498.                 BMP_To_Texture_OK = false;
  499.                 CoreFAILURE = true;
  500.             }
  501.  
  502.             if (BMP_To_Texture_OK == true)
  503.             {
  504.                 // Have OpenGL generate a texture object handle for us
  505.                 glGenTextures( 1, &Sprites[0].OGL_Texture );
  506.  
  507.                 // Bind the texture object
  508.                 glBindTexture( GL_TEXTURE_2D, Sprites[0].OGL_Texture );
  509.  
  510.                 // Set the texture's stretching properties
  511.                 glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR );
  512.                 glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR );
  513.  
  514.                 // Edit the texture object's image data using the information SDL_Surface gives us
  515.                 glTexImage2D( GL_TEXTURE_2D, 0, NumberOfColors, Surface->w, Surface->h, 0,
  516.                     TextureFormat, GL_UNSIGNED_BYTE, Surface->pixels );
  517.  
  518.                // Have OpenGL generate a texture object handle for us
  519.                 glGenTextures( 1, &Sprites[1].OGL_Texture );
  520.  
  521.                 // Bind the texture object
  522.                 glBindTexture( GL_TEXTURE_2D, Sprites[1].OGL_Texture );
  523.  
  524.                 // Set the texture's stretching properties
  525.                 glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR );
  526.                 glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR );
  527.  
  528.                 // Edit the texture object's image data using the information SDL_Surface gives us
  529.                 glTexImage2D( GL_TEXTURE_2D, 0, NumberOfColors, Surface->w, Surface->h, 0,
  530.                     TextureFormat, GL_UNSIGNED_BYTE, Surface->pixels );
  531.  
  532.                 // Free the SDL_Surface only if it was successfully created
  533.                 if ( Surface )
  534.                 {
  535.                     SDL_FreeSurface( Surface );
  536.                 }
  537.             }
  538.         }
  539.     }
  540.     else
  541.     {
  542.         printf("ERROR: SDL could not load image.png: %s\n", SDL_GetError());
  543.         BMP_To_Texture_OK = false;
  544.     }
  545.  
  546.     /* SDL interprets each pixel as a 32-bit number, so our masks must depend
  547.        on the endianness (byte order) of the machine */
  548.     #if SDL_BYTEORDER == SDL_BIG_ENDIAN
  549.         rmask = 0xff000000;
  550.         gmask = 0x00ff0000;
  551.         bmask = 0x0000ff00;
  552.         amask = 0x000000ff;
  553.         ColorIsRGBA = true;
  554.     #else
  555.         rmask = 0x00ff0000;
  556.         gmask = 0x0000ff00;
  557.         bmask = 0x000000ff;
  558.         amask = 0xff000000;
  559.         ColorIsRGBA = false;
  560.     #endif
  561.  
  562.     srand( (unsigned)time( NULL ) );
  563.  
  564.     CalculateRandomPositions();
  565.  
  566.     SystemTicks = SDL_GetTicks();
  567.     NextSecondTick = SystemTicks+1000;
  568.     NumberOfFramesPerSecond = 0;
  569.     AverageFPS = 0;
  570.  
  571.     PreloadTestText();
  572.  
  573.     printf("Main Loop executed!\n");
  574. //-------------------------------------------------------------------------------
  575. //  MAIN LOOP
  576.  
  577.     while (CoreFAILURE != true && EXITgame == false)
  578.     {
  579.         SystemTicks = SDL_GetTicks();
  580.         NextFrameTicks = SystemTicks + 17; // About 60 Frames Per Second!
  581.  
  582.         NumberOfFramesPerSecond++;
  583.         if (SystemTicks > NextSecondTick)
  584.         {
  585.             FPSindex++;
  586.             if (FPSindex > 9)
  587.             {
  588.                 FPSindex = 0;
  589.             }
  590.  
  591.             FramesPerSecondArray[FPSindex] = NumberOfFramesPerSecond;
  592.  
  593.             AverageFPS = 0;
  594.             for (int index = 0; index < 10; index++)
  595.             {
  596.                 AverageFPS += FramesPerSecondArray[index];
  597.             }
  598.             AverageFPS = AverageFPS / 10;
  599.  
  600.             NumberOfFramesPerSecond = 0;
  601.             NextSecondTick = SystemTicks+1000;
  602.         }
  603.  
  604.         while( SDL_PollEvent( &Event ) )
  605.         {
  606.  
  607.         }
  608.  
  609.         KeyUp = false;
  610.         switch( Event.type )
  611.         {
  612.             case SDL_QUIT:
  613.                 EXITgame = true;
  614.                 break;
  615.  
  616.             case SDL_KEYUP:
  617.                 switch ( Event.key.keysym.sym )
  618.                 {
  619.                     case SDLK_t:
  620.                         T_WasPressed = false;
  621.                         KeyUp = true;
  622.                         break;
  623.  
  624.                     case SDLK_q:
  625.                         Q_IsPressed = false;
  626.                         W_IsPressed = false;
  627.                         KeyUp = true;
  628.                         break;
  629.  
  630.                     case SDLK_w:
  631.                         W_IsPressed = false;
  632.                         Q_IsPressed = false;
  633.                         KeyUp = true;
  634.                         break;
  635.  
  636.                     case SDLK_a:
  637.                         A_IsPressed = false;
  638.                         S_IsPressed = false;
  639.                         KeyUp = true;
  640.                         break;
  641.  
  642.                     case SDLK_s:
  643.                         S_IsPressed = false;
  644.                         A_IsPressed = false;
  645.                         KeyUp = true;
  646.                         break;
  647.  
  648.                     default:
  649.                         break;
  650.                 }
  651.  
  652.             case SDL_KEYDOWN:
  653.                 switch( Event.key.keysym.sym )
  654.                 {
  655.                     case SDLK_ESCAPE:
  656.                         EXITgame = true;
  657.                         break;
  658.  
  659.                     case SDLK_t:
  660.                         if (KeyUp == false && T_WasPressed == false)
  661.                         {
  662.                             if (DrawText == 0)  DrawText = 1;
  663.                             else if (DrawText == 1)  DrawText = 2;
  664.                             else if (DrawText == 2)  DrawText = 3;
  665.                             else if (DrawText == 3)  DrawText = 0;
  666.                             T_WasPressed = true;
  667.                         }
  668.                         break;
  669.  
  670.                     case SDLK_q:
  671.                         if (KeyUp == false)
  672.                         {
  673.                             Q_IsPressed = true;
  674.                         }
  675.                         break;
  676.  
  677.                     case SDLK_w:
  678.                         if (KeyUp == false)
  679.                         {
  680.                             W_IsPressed = true;
  681.                         }
  682.                         break;
  683.                         break;
  684.  
  685.                     case SDLK_a:
  686.                         if (KeyUp == false)
  687.                         {
  688.                             A_IsPressed = true;
  689.                         }
  690.                         break;
  691.  
  692.                     case SDLK_s:
  693.                         if (KeyUp == false)
  694.                         {
  695.                             S_IsPressed = true;
  696.                         }
  697.                         break;
  698.  
  699.                     default:
  700.                         break;
  701.                 }
  702.         }
  703.  
  704.         if (Q_IsPressed == true)
  705.         {
  706.             if (Sprites[1].Width > 0)  Sprites[1].Width--;
  707.             Sprites[1].CenterX = Sprites[1].Width / 2;
  708.             if (Sprites[1].Height > 0)  Sprites[1].Height--;
  709.             Sprites[1].CenterY = Sprites[1].Height / 2;
  710.         }
  711.         else if (W_IsPressed == true)
  712.         {
  713.             if (Sprites[1].Width < 1000)  Sprites[1].Width++;
  714.             Sprites[1].CenterX = Sprites[1].Width / 2;
  715.             if (Sprites[1].Height < 1000)  Sprites[1].Height++;
  716.             Sprites[1].CenterY = Sprites[1].Height / 2;
  717.         }
  718.  
  719.         if (A_IsPressed == true)
  720.         {
  721.             if (Sprites[1].RotationDegree > 0)  Sprites[1].RotationDegree--;
  722.             else  Sprites[1].RotationDegree = 359;
  723.         }
  724.         else if (S_IsPressed == true)
  725.         {
  726.             if (Sprites[1].RotationDegree < 359)  Sprites[1].RotationDegree++;
  727.             else  Sprites[1].RotationDegree = 0;
  728.         }
  729.  
  730.         Uint8 *keystates = SDL_GetKeyState( NULL );
  731.  
  732.         if      ( keystates[ SDLK_UP    ] && Y_Offset > -300)  Y_Offset--;
  733.         else if ( keystates[ SDLK_DOWN  ] && Y_Offset <  780)  Y_Offset++;
  734.         else if ( keystates[ SDLK_LEFT  ] && X_Offset > -300)  X_Offset--;
  735.         else if ( keystates[ SDLK_RIGHT ] && X_Offset <  940)  X_Offset++;
  736.  
  737.         glClearColor( 0.0f, 1.0f, 0.0f, 0.0f ); // Green OGL clear color
  738.         glClear( GL_COLOR_BUFFER_BIT );
  739.  
  740.         MoveRandomPositions();
  741.         DrawTestTextures();
  742.  
  743.         Color.r = 255;
  744.         Color.g = 0;
  745.         Color.b = 0;
  746.         /// A quick note about position.
  747.         /// * Enable2D puts the origin in the lower-left corner of the
  748.         /// * screen, which is different from the normal coordinate
  749.         /// * space of most 2D api's. position, therefore,
  750.         /// * gives the X,Y coordinates of the lower-left corner of the
  751.         /// * rectangle
  752.  
  753.         for (int index = 0; index < NumberOfTexts; index++)
  754.         {
  755.             if (DrawText < 1 || DrawText == 3)  DrawPreloadedTestText(JustifyLeft, randomTextX[index], randomTextY[index]);
  756.         }
  757.  
  758.         DrawMovableScalableTestTexture();
  759.  
  760.         char temp[256];
  761.         strcpy(VariableText, "F.P.S.:");
  762.         sprintf(temp, "%d", AverageFPS);
  763.         strcat(VariableText, temp);
  764.         strcat(VariableText," ~60");
  765.         Color.r = 0;
  766.         Color.g = 0;
  767.         Color.b = 255;
  768.         if (DrawText < 2)  SDL_GL_RenderText(VariableText, Font, Color, JustifyCenter, 5, 5);
  769.  
  770.         SDL_GL_SwapBuffers();
  771.  
  772.         SystemTicks = SDL_GetTicks();
  773.         if (NextFrameTicks > SystemTicks)  SDL_Delay(NextFrameTicks-SystemTicks);
  774.     }
  775. //                                                                    MAIN LOOP
  776. //-------------------------------------------------------------------------------
  777.  
  778.     if (PreLoadTextsArray[0].OGL_TextTexture)  glDeleteTextures( 1, &PreLoadTextsArray[0].OGL_TextTexture );
  779.     if (PreLoadTextsArray[1].OGL_TextTexture)  glDeleteTextures( 1, &PreLoadTextsArray[1].OGL_TextTexture );
  780.     if (BMP_To_Texture_OK == true)  glDeleteTextures( 1, &Sprites[0].OGL_Texture );
  781.     if (Font != NULL)  TTF_CloseFont(Font);
  782.     TTF_Quit();
  783.     SDL_Quit();
  784.  
  785.     printf("SDL+OGL application successfully closed\n");
  786.     return 0;
  787. }
  788. //                  TM
  789. // "A 100% By JeZ+Lee" of Silent Hero Productions(R)
  790.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement