Advertisement
Guest User

JeZ-l-Lee

a guest
Feb 12th, 2010
421
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 13.97 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. TTF_Font* Font = NULL;
  24. SDL_Color Color;
  25. SDL_Rect Position;
  26.  
  27. bool DrawText = true;
  28. Uint32 DelayKeyboardInput = 0;
  29.  
  30. #define NumberOfHappyFaces      200
  31. Uint32 randomX[NumberOfHappyFaces];
  32. Uint32 randomY[NumberOfHappyFaces];
  33.  
  34. GLuint Texture;         // This is a handle to our texture object
  35. SDL_Surface *Surface;   // This surface will tell us the details of the image
  36. GLenum texture_format;
  37. GLint  nOfColors;
  38.  
  39. bool BMP_Dimensions_OK = true;
  40. bool BMP_To_Texture_OK = true;
  41.  
  42. int X_Offset = 0;
  43. int Y_Offset = 0;
  44.  
  45. //-------------------------------------------------------------------------------------------------
  46. ///int round(double x)
  47. ///{
  48. ///     return (int)(x + 0.5);
  49. ///}
  50. //-------------------------------------------------------------------------------------------------
  51. int nextpoweroftwo(int x)
  52. {
  53.     double logbase2 = log(x) / log(2);
  54.     return round(pow(2,ceil(logbase2)));
  55. }
  56. //-------------------------------------------------------------------------------------------------
  57. void SDL_GL_RenderText(const char *text, TTF_Font *font, SDL_Color color, SDL_Rect *location)
  58. {
  59. SDL_Surface *initial;
  60. SDL_Surface *initialOutline;
  61. SDL_Surface *intermediary;
  62. SDL_Rect rect;
  63. int w,h;
  64. GLuint textureTwo;
  65. SDL_Color outline;
  66.  
  67.     outline.r = 255;
  68.     outline.g = 0;
  69.     outline.b = 0;
  70.  
  71.     // Use SDL_TTF to render our text
  72.     initial = TTF_RenderText_Solid(font, text, color);
  73.     initialOutline = TTF_RenderText_Solid(font, text, outline);
  74.  
  75.     // Convert the rendered text to a known format
  76.     w = nextpoweroftwo(5+initial->w);
  77.     h = nextpoweroftwo(5+initial->h);
  78.  
  79.     /* SDL interprets each pixel as a 32-bit number, so our masks must depend
  80.        on the endianness (byte order) of the machine */
  81.     Uint32 rmask, gmask, bmask, amask;
  82.     bool colorIsRGBA = true;
  83.     #if SDL_BYTEORDER == SDL_BIG_ENDIAN
  84.         rmask = 0xff000000;
  85.         gmask = 0x00ff0000;
  86.         bmask = 0x0000ff00;
  87.         amask = 0x000000ff;
  88.         colorIsRGBA = false;
  89.     #else
  90.         rmask = 0x000000ff;
  91.         gmask = 0x0000ff00;
  92.         bmask = 0x00ff0000;
  93.         amask = 0xff000000;
  94.         colorIsRGBA = true;
  95.     #endif
  96.     intermediary = SDL_CreateRGBSurface(0, w, h, 32, rmask, gmask, bmask, amask);
  97.  
  98.     for (int outlineY = 0; outlineY < 5; outlineY++)
  99.         for (int outlineX = 0; outlineX < 5; outlineX++)
  100.         {
  101.             rect.x = outlineX;
  102.             rect.y = outlineY;
  103.             SDL_BlitSurface(initialOutline, 0, intermediary, &rect);
  104.         }
  105.  
  106.     rect.x = 2;
  107.     rect.y = 2;
  108.     SDL_BlitSurface(initial, 0, intermediary, &rect);
  109.  
  110.     // Tell GL about our new texture
  111.     glGenTextures(1, &textureTwo);
  112.     glBindTexture(GL_TEXTURE_2D, textureTwo);
  113.     if (colorIsRGBA == true)
  114.         glTexImage2D(GL_TEXTURE_2D, 0, 4, w, h, 0, GL_RGBA,
  115.         GL_UNSIGNED_BYTE, intermediary->pixels );
  116.     else
  117.         glTexImage2D(GL_TEXTURE_2D, 0, 4, w, h, 0, GL_BGRA,
  118.         GL_UNSIGNED_BYTE, intermediary->pixels );
  119.  
  120.  
  121.     // GL_NEAREST looks horrible, if scaled...
  122.     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
  123.     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
  124.  
  125.     // prepare to render our texture
  126.     glEnable(GL_TEXTURE_2D);
  127.     glBindTexture(GL_TEXTURE_2D, textureTwo);
  128.     glColor3f(1.0f, 1.0f, 1.0f);
  129.  
  130.     // Draw a quad at location
  131.     glBegin(GL_QUADS);
  132.         // Recall that the origin is in the lower-left corner
  133.         // That is why the TexCoords specify different corners
  134.         // than the Vertex coors seem to.
  135.         glTexCoord2f(0.0f, 1.0f);
  136.         glVertex2f(location->x    , location->y + h);
  137.  
  138.         glTexCoord2f(1.0f, 1.0f);
  139.         glVertex2f(location->x + w, location->y + h);
  140.  
  141.         glTexCoord2f(1.0f, 0.0f);
  142.         glVertex2f(location->x + w, location->y    );
  143.  
  144.         glTexCoord2f(0.0f, 0.0f);
  145.         glVertex2f(location->x    , location->y    );
  146.     glEnd();
  147.  
  148.     // Bad things happen if we delete the texture before it finishes
  149.     glFinish();
  150.  
  151.     // return the deltas in the unused w,h part of the rect
  152.     location->w = initial->w;
  153.     location->h = initial->h;
  154.  
  155.     // Clean up
  156.     SDL_FreeSurface(initial);
  157.     SDL_FreeSurface(intermediary);
  158.     glDeleteTextures(1, &textureTwo);
  159. }
  160. //-------------------------------------------------------------------------------------------------
  161. void Calculate100RandomPositions(void)
  162. {
  163.     for (int index = 0; index < NumberOfHappyFaces; index++)
  164.     {
  165.         randomX[index] = rand()%1040;
  166.         randomX[index]-=200;
  167.         randomY[index] = rand()%880;
  168.         randomY[index]-=200;
  169.     }
  170. }
  171. //-------------------------------------------------------------------------------------------------
  172. void Draw100TestTextures(void)
  173. {
  174.     if (BMP_To_Texture_OK == true)
  175.     {
  176.         for (int index = 0; index < NumberOfHappyFaces;  index++)
  177.         {
  178.             // Bind the texture to which subsequent calls refer to
  179.             glBindTexture( GL_TEXTURE_2D, Texture );
  180.  
  181.             glEnable(GL_BLEND);
  182.             glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
  183.  
  184.             glBegin( GL_QUADS ); // Draw texture with no rotation
  185.                 //Top-left vertex (corner)
  186.                 glTexCoord2i( 0, 0 );
  187.                 glVertex3f( randomY[index], randomX[index], 0.0f );
  188.  
  189.                 //Bottom-left vertex (corner)
  190.                 glTexCoord2i( 1, 0 );
  191.                 glVertex3f( randomY[index]+100, randomX[index], 0 );
  192.  
  193.                 //Bottom-right vertex (corner)
  194.                 glTexCoord2i( 1, 1 );
  195.                 glVertex3f( randomY[index]+100, randomX[index]+100, 0 );
  196.  
  197.                 //Top-right vertex (corner)
  198.                 glTexCoord2i( 0, 1 );
  199.                 glVertex3f( randomY[index], randomX[index]+100, 0 );
  200.             glEnd();
  201.         }
  202.     }
  203. }
  204. //-------------------------------------------------------------------------------------------------
  205. void DrawMovableTestTexture(void)
  206. {
  207.     if (BMP_To_Texture_OK == true)
  208.     {
  209.         // Bind the texture to which subsequent calls refer to
  210.         glBindTexture( GL_TEXTURE_2D, Texture );
  211.  
  212.         glEnable(GL_BLEND);
  213.         glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
  214.  
  215.         glBegin( GL_QUADS ); // Draw texture with some rotation
  216.             //Top-left vertex (corner)
  217.             glTexCoord2i( 0, 0 );
  218.             glVertex3f( 100+X_Offset, 300+Y_Offset, 0.0f );
  219.  
  220.             //Bottom-left vertex (corner)
  221.             glTexCoord2i( 1, 0 );
  222.             glVertex3f( 200+X_Offset, 100+Y_Offset, 0 );
  223.  
  224.             //Bottom-right vertex (corner)
  225.             glTexCoord2i( 1, 1 );
  226.             glVertex3f( 300+X_Offset, 300+Y_Offset, 0 );
  227.  
  228.             //Top-right vertex (corner)
  229.             glTexCoord2i( 0, 1 );
  230.             glVertex3f( 200+X_Offset, 500+Y_Offset, 0 );
  231.         glEnd();
  232.     }
  233. }
  234. //-------------------------------------------------------------------------------------------------
  235. int main( int argc, char* args[] )
  236. {
  237.     if ( SDL_Init(SDL_INIT_EVERYTHING) != 0 )
  238.     {
  239.         printf("ERROR: Unable to initialize SDL: %s\n", SDL_GetError());
  240.         CoreFAILURE = true;
  241.     }
  242.     else  printf("SDL (Everything) initialized\n");
  243.  
  244.     if ( TTF_Init() == -1 )
  245.     {
  246.         fprintf(stderr, "ERROR: TTF_Init: %s\n", TTF_GetError());
  247.         CoreFAILURE = true;
  248.     }
  249.     else fprintf(stdout, "SDL_TTF initialized\n");
  250.  
  251.     if( !(Font = TTF_OpenFont("data/fonts/default.ttf", 50)) )
  252.     {
  253.         printf("ERROR: loading font: %s\n", TTF_GetError());
  254.         CoreFAILURE = true;
  255.     }
  256.  
  257.     SDL_GL_SetAttribute( SDL_GL_DOUBLEBUFFER, 1 );
  258.  
  259.     putenv(strdup("SDL_VIDEO_CENTERED=1"));
  260.     SDL_Surface* screen = SDL_SetVideoMode( 640, 480, 0, SDL_OPENGL);
  261.     if (!screen)
  262.     {
  263.         printf("ERROR: SDL 640x480 window init failed\n");
  264.         CoreFAILURE = true;
  265.     }
  266.  
  267.     glEnable( GL_TEXTURE_2D );
  268.     glClearColor( 0.0f, 0.0f, 0.0f, 0.0f );
  269.     glViewport( 0, 0, 640, 480 );
  270.     glClear( GL_COLOR_BUFFER_BIT );
  271.     glMatrixMode( GL_PROJECTION );
  272.     glLoadIdentity();
  273.     glOrtho(0.0f, 640, 480, 0.0f, -1.0f, 1.0f);
  274.     glMatrixMode( GL_MODELVIEW );
  275.     glLoadIdentity();
  276.     printf("OpenGL initialized\n");
  277.  
  278.     if ( (Surface = IMG_Load("data/graphics/image.png")) )
  279.     {
  280.         // Check that the image's width is a power of 2
  281.         if ( (Surface->w & (Surface->w - 1)) != 0 )
  282.         {
  283.             printf("ERROR: image.bmp's width is not a power of 2\n");
  284.             BMP_Dimensions_OK = false;
  285.             CoreFAILURE = true;
  286.         }
  287.  
  288.         // Also check if the height is a power of 2
  289.         if ( (Surface->h & (Surface->h - 1)) != 0 )
  290.         {
  291.             printf("ERROR: image.bmp's height is not a power of 2\n");
  292.             BMP_Dimensions_OK = false;
  293.             CoreFAILURE = true;
  294.         }
  295.  
  296.         if (BMP_Dimensions_OK == true)
  297.         {
  298.             // get the number of channels in the SDL surface
  299.             nOfColors = Surface->format->BytesPerPixel;
  300.             if (nOfColors == 4) // contains an alpha channel
  301.             {
  302.                 if (Surface->format->Rmask == 0x000000ff)
  303.                     texture_format = GL_RGBA;
  304.                 else
  305.                     texture_format = GL_BGRA;
  306.             }
  307.             else if (nOfColors == 3) // no alpha channel
  308.             {
  309.                 if (Surface->format->Rmask == 0x000000ff)
  310.                     texture_format = GL_RGB;
  311.                 else
  312.                     texture_format = GL_BGR;
  313.             }
  314.             else
  315.             {
  316.                 printf("ERROR: the image is not truecolor\n");
  317.                 BMP_To_Texture_OK = false;
  318.                 CoreFAILURE = true;
  319.             }
  320.  
  321.             if (BMP_To_Texture_OK == true)
  322.             {
  323.                 // Have OpenGL generate a texture object handle for us
  324.                 glGenTextures( 1, &Texture );
  325.  
  326.                 // Bind the texture object
  327.                 glBindTexture( GL_TEXTURE_2D, Texture );
  328.  
  329.                 // Set the texture's stretching properties
  330.                 glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR );
  331.                 glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR );
  332.  
  333.                 // Edit the texture object's image data using the information SDL_Surface gives us
  334.                 glTexImage2D( GL_TEXTURE_2D, 0, nOfColors, Surface->w, Surface->h, 0,
  335.                     texture_format, GL_UNSIGNED_BYTE, Surface->pixels );
  336.  
  337.                 // Free the SDL_Surface only if it was successfully created
  338.                 if ( Surface )
  339.                 {
  340.                     SDL_FreeSurface( Surface );
  341.                 }
  342.             }
  343.         }
  344.     }
  345.     else
  346.     {
  347.         printf("ERROR: SDL could not load image.bmp: %s\n", SDL_GetError());
  348.         BMP_To_Texture_OK = false;
  349.     }
  350.  
  351.     srand( (unsigned)time( NULL ) );
  352.  
  353.     Calculate100RandomPositions();
  354.  
  355.     printf("Main Loop executed!\n");
  356.  
  357. //-------------------------------------------------------------------------------
  358. //  MAIN LOOP
  359.  
  360.     while (CoreFAILURE != true && EXITgame == false)
  361.     {
  362.         while( SDL_PollEvent( &Event ) )
  363.         {
  364.  
  365.         }
  366.  
  367.         switch( Event.type )
  368.         {
  369.             case SDL_QUIT:
  370.                 EXITgame = true;
  371.                 break;
  372.  
  373.             case SDL_KEYDOWN:
  374.                 switch( Event.key.keysym.sym )
  375.                 {
  376.                     case SDLK_ESCAPE:
  377.                         EXITgame = true;
  378.                         break;
  379.  
  380.                     case SDLK_t:
  381.                         if (DelayKeyboardInput == 0)
  382.                         {
  383.                             if (DrawText == false)  DrawText = true;
  384.                             else  DrawText = false;
  385.                             DelayKeyboardInput = 40;
  386.                         }
  387.                         break;
  388.  
  389.                     default:
  390.                         break;
  391.                 }
  392.         }
  393.  
  394.         if (DelayKeyboardInput > 0)  DelayKeyboardInput--;
  395.  
  396.         Uint8 *keystates = SDL_GetKeyState( NULL );
  397.  
  398.         if      ( keystates[ SDLK_UP    ] )  Y_Offset--;
  399.         else if ( keystates[ SDLK_DOWN  ] )  Y_Offset++;
  400.         else if ( keystates[ SDLK_LEFT  ] )  X_Offset--;
  401.         else if ( keystates[ SDLK_RIGHT ] )  X_Offset++;
  402.  
  403.         glClearColor( 0.0f, 1.0f, 0.0f, 0.0f ); // Green OGL clear color
  404.         glClear( GL_COLOR_BUFFER_BIT );
  405.  
  406.         Draw100TestTextures();
  407.  
  408.         Color.r = 255;
  409.         Color.g = 255;
  410.         Color.b = 255;
  411.         /// A quick note about position.
  412.         /// * Enable2D puts the origin in the lower-left corner of the
  413.         /// * screen, which is different from the normal coordinate
  414.         /// * space of most 2D api's. position, therefore,
  415.         /// * gives the X,Y coordinates of the lower-left corner of the
  416.         /// * rectangle
  417.         Position.x = 10;
  418.         Position.y = 10;
  419.         if (DrawText == true)  SDL_GL_RenderText("Hello, World!", Font, Color, &Position);
  420.  
  421.         DrawMovableTestTexture();
  422.  
  423.         SDL_GL_SwapBuffers();
  424.         SDL_Delay(16.7); // 60 Frames Per Second!
  425.     }
  426. //                                                                    MAIN LOOP
  427. //-------------------------------------------------------------------------------
  428.  
  429.     if (BMP_To_Texture_OK == true)  glDeleteTextures( 1, &Texture );
  430.     if (Font != NULL)  TTF_CloseFont(Font);
  431.     TTF_Quit();
  432.     SDL_Quit();
  433.  
  434.     printf("SDL+OGL application successfully closed\n");
  435.     return 0;
  436. }
  437. //                  TM
  438. // "A 100% By JeZ+Lee" of Silent Hero Productions(R)
  439.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement