Pastebin is 300% more awesome when you are logged in. Sign Up, it's FREE!
Guest

Mirrors of Elixia

By: a guest on Jul 23rd, 2013  |  syntax: C++  |  size: 3.32 KB  |  hits: 45  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. #include <SDL/SDL.h>
  2. #include <SDL/SDL_image.h>
  3. #include <SDL/SDL_Resize.h>
  4. #include <SDL/SDL_ttf.h>
  5. #include <string>
  6. #include <vector>
  7.  
  8. using std::string;
  9. using std::vector;
  10.  
  11. SDL_Surface* LoadImage(string);
  12. void ApplySurface(int, int, SDL_Surface*, SDL_Surface*);
  13. void Menu();
  14. char* GetOption(int);
  15.  
  16. const int ScreenWidth = 1000;
  17. const int ScreenHeight = 800;
  18. const int ScreenBPP = 32;
  19.  
  20. int State = 0;
  21.  
  22. SDL_Surface *Screen = NULL;
  23.  
  24. int main(int argc, char *argv[])
  25. {
  26.     if(SDL_Init(SDL_INIT_EVERYTHING) == -1)
  27.         return -1;
  28.  
  29.     else if(TTF_Init() == -1)
  30.         return -1;
  31.  
  32.     Screen = SDL_SetVideoMode(ScreenWidth, ScreenHeight, ScreenBPP, SDL_SWSURFACE);
  33.  
  34.     if(Screen == NULL)
  35.         return -1;
  36.  
  37.     while(true)
  38.     {
  39.         switch(State)
  40.         {
  41.             case 0:
  42.                 Menu();
  43.                 break;
  44.         }
  45.     }
  46. }
  47.  
  48. SDL_Surface* LoadImage(string Image)
  49. {
  50.     SDL_Surface* LoadedImage = NULL;
  51.     SDL_Surface* OptimizedImage = NULL;
  52.  
  53.     LoadedImage = IMG_Load(Image.c_str());
  54.  
  55.     if(LoadedImage != NULL)
  56.     {
  57.         OptimizedImage = SDL_DisplayFormat(LoadedImage);
  58.         SDL_FreeSurface(LoadedImage);
  59.     }
  60.  
  61.     return OptimizedImage;
  62. }
  63.  
  64. void ApplySurface(int XCoor, int YCoor, SDL_Surface *Source, SDL_Surface *Destination)
  65. {
  66.     SDL_Rect Offset;
  67.  
  68.     Offset.x = XCoor;
  69.     Offset.y = YCoor;
  70.  
  71.     SDL_BlitSurface(Source, NULL, Destination, &Offset);
  72. }
  73.  
  74. void Menu()
  75. {
  76.     bool Running = true;
  77.     int Displace = 0, Counter;
  78.     const int OptionCount = 4;
  79.     SDL_Event Event;
  80.     TTF_Font *Font = NULL;
  81.     SDL_Color TextColor = {192, 192, 192};
  82.     SDL_Surface *Background = NULL;
  83.     vector<SDL_Surface*> Button(OptionCount);
  84.     vector<SDL_Surface*> Message(OptionCount);
  85.  
  86.     SDL_WM_SetCaption("Mirrors of Elixia :: Menu", NULL);
  87.  
  88.     Font = TTF_OpenFont("DragonFont.ttf", 28);
  89.  
  90.     Background = LoadImage("background.jpg");
  91.     Background = SDL_Resize(Background, ScreenWidth, ScreenHeight, false, 3);
  92.     ApplySurface(0, 0, Background, Screen);
  93.  
  94.     for(Counter = 0; Counter < Button.size(); Counter++)
  95.     {
  96.         Message[Counter] = NULL;
  97.         Message[Counter] = TTF_RenderText_Solid(Font, GetOption(Counter), TextColor);
  98.  
  99.         Button[Counter] = NULL;
  100.         Button[Counter] = LoadImage("button.jpg");
  101.         Button[Counter] = SDL_Resize(Button[Counter], Button[Counter]->w / 2, Button[Counter]->h / 2, false, 3);
  102.  
  103.         ApplySurface(ScreenWidth - Button[Counter]->w - 10, 15 + Displace, Button[Counter], Screen);
  104.         ApplySurface(ScreenWidth - Button[Counter]->w + 40, 45 + Displace, Message[Counter], Screen);
  105.  
  106.         Displace += Button[Counter]->h + 20;
  107.     }
  108.  
  109.     if(SDL_Flip(Screen) == -1)
  110.         return;
  111.  
  112.     while(Running)
  113.     {
  114.         while(SDL_PollEvent(&Event))
  115.         {
  116.             if(Event.type == SDL_QUIT)
  117.                 Running = false;
  118.         }
  119.     }
  120.  
  121.     SDL_FreeSurface(Background);
  122.     SDL_FreeSurface(Screen);
  123.  
  124.     SDL_Quit();
  125.     return;
  126. }
  127.  
  128. char* GetOption(int Code)
  129. {
  130.     switch(Code)
  131.     {
  132.         case 0:
  133.             return "New Game";
  134.             break;
  135.  
  136.         case 1:
  137.             return "Saved Game";
  138.             break;
  139.  
  140.         case 2:
  141.             return "Settings";
  142.             break;
  143.  
  144.         case 3:
  145.             return "Exit";
  146.             break;
  147.     }
  148. }