Advertisement
Archon

Impacts main.cpp

Jan 30th, 2011
232
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 7.77 KB | None | 0 0
  1. // (C) 2009-2011 Tim Gurto, Jacqui Hawkins
  2.  
  3. #include <cassert>
  4. #include <fstream>
  5. #include <ctime>
  6. #include <cstdlib>
  7. #include <iostream>
  8. #include <string>
  9. #include <cmath>
  10.  
  11. #ifdef __APPLE__
  12. #include "SDLMain.h"
  13. #endif
  14.  
  15. #include "SDL.h"
  16. #include "SDL_ttf.h"
  17. #include "SDL_mixer.h"
  18.  
  19. #include "Building.h"
  20. #include "Cursor.h"
  21. #include "game.h"
  22. #include "globals.h"
  23. #include "Debug.h"
  24. #include "Screen.h"
  25. #include "Surface.h"
  26. #include "Point.h"
  27. #include "ScreenElement.h"
  28. #include "Sound.h"
  29. #include "Surface.h"
  30. #include "Human.h"
  31. #include "Flower.h"
  32. #include "Tree.h"
  33. #include "Bunny.h"
  34. #include "main.h"
  35. #include "globals.h"
  36. #include "util.h"
  37. #include "misc.h"
  38.  
  39. namespace GameJam {
  40.  
  41. //global screen buffer
  42. Surface screenBuf; //uninitialized
  43.  
  44. //general debug messages
  45. Debug debug(YELLOW, 0, 0, 59);
  46.  
  47. //can be reused for whichever screens; the useful bit is the name
  48.  
  49. enum ButtonID {
  50.     //ScreenElement::NO_ID = -1
  51.     BUTTON_QUIT = 0,
  52.     BUTTON_NEW,
  53.     BUTTON_CREDITS,
  54. };
  55.  
  56. }
  57.  
  58. int main(int argc, char *argv[]) {
  59.  
  60.     try {
  61.  
  62.         //seed random generator
  63.         srand(unsigned(time(0)));
  64.  
  65.         //SDL initialization
  66.         int sdlInit(SDL_Init(SDL_INIT_VIDEO));
  67.         assert(sdlInit == 0);
  68.         int ttfInit(TTF_Init());
  69.         assert(ttfInit >= 0);
  70.         int mixInit(Mix_OpenAudio(22050, MIX_DEFAULT_FORMAT, 2, 256)); //256));
  71.         assert(mixInit >= 0);
  72.  
  73.         GameJam::debug.initFont(GameJam::FONT_DEBUG, 0);
  74.  
  75.  
  76.         //initialize screen buffer
  77.         GameJam::screenBuf = GameJam::Surface(GameJam::SUR_SCREEN);
  78.  
  79.         {//new scope for surfaces
  80.  
  81.             //init with surfaces
  82.             GameJam::Screen::init();
  83.             GameJam::Screen mainMenu(GameJam::BUTTON_NEW, GameJam::BUTTON_QUIT);
  84.            
  85.             GameJam::Surface background(GameJam::IMAGE_PATH + "titlescreen2.png", false);
  86.             mainMenu.setBackground(background);
  87.  
  88.             GameJam::Screen game(&GameJam::gameMode);
  89.             GameJam::Screen credits(GameJam::BUTTON_QUIT, GameJam::BUTTON_QUIT);
  90.             GameJam::buildScreens(mainMenu, credits);
  91.  
  92.             //statics
  93.             GameJam::Cursor::surface = GameJam::Surface(GameJam::IMAGE_PATH + "cursor.png", true);
  94.             GameJam::Flower::surface = GameJam::Surface(GameJam::IMAGE_PATH + "flowers.png", true);
  95.             GameJam::Flower::futureSurface = GameJam::Surface(GameJam::IMAGE_PATH + "flowerclusters_scale.png", true);
  96.             GameJam::Human::surface = GameJam::Surface(GameJam::IMAGE_PATH + "human.png", true);
  97.             GameJam::Building::surface = GameJam::Surface(GameJam::IMAGE_PATH + "hut.png", true);
  98.             GameJam::Building::futureSurface = GameJam::Surface(GameJam::IMAGE_PATH + "skyscraper.png", true);
  99.             GameJam::Tree::surface = GameJam::Surface(GameJam::IMAGE_PATH + "tree.png", true);
  100.             GameJam::Tree::futureSurface = GameJam::Surface(GameJam::IMAGE_PATH + "treeclump_scale.png", true);
  101.             GameJam::Bunny::surface = GameJam::Surface(GameJam::IMAGE_PATH + "bunny.png", true);
  102.             GameJam::Bunny::futureSurface = GameJam::Surface(GameJam::IMAGE_PATH + "bunny_scale.png", true);
  103.  
  104.             // skip main menu
  105.             if (!DEBUG) {
  106.  
  107.                 //main menu
  108.                 bool loop = true;
  109.                 do {
  110.                     switch (mainMenu()) {
  111.                         case GameJam::BUTTON_QUIT:
  112.                             loop = false;
  113.                             break;
  114.  
  115.                         case GameJam::BUTTON_CREDITS:
  116.                             credits();
  117.                             break;
  118.  
  119.                         case GameJam::BUTTON_NEW:
  120.                         {
  121.                             GameJam::GameOutcome outcome;
  122.  ========BEFORE===================================================================================
  123.                             while ((outcome = (GameJam::GameOutcome)game()) == GameJam::RESTART)
  124.                                 ;
  125.                             if (outcome == GameJam::ALT_F4)
  126. =========AFTER====================================================================================
  127. game();
  128.                                 loop = false;
  129.                         }
  130.                             break;
  131.                     }
  132.                 }
  133.                 while (loop);
  134.  
  135.             }
  136.             else {
  137.                 while (game() == GameJam::RESTART);
  138.             }
  139.  
  140.         }
  141.  
  142.         //Quit
  143.         //TTF_Quit() happens at debug dtor
  144.         Mix_CloseAudio();
  145.         SDL_Quit();
  146.         //TODO GameJam::Surface::quit();
  147.         GameJam::Sound::quit();
  148.     }
  149.     catch (std::string& e) {
  150.         std::cerr << e << std::endl;
  151.     }
  152.     catch (...) {
  153.         std::cerr << "Unknown exception" << std::endl;
  154.     }
  155.    
  156.     return 0;
  157. }
  158.  
  159. namespace GameJam {
  160.  
  161. void buildScreens(Screen &mainMenu,
  162.                   Screen &credits) {
  163.  
  164.     //Main menu
  165.     //mainMenu.addElement(
  166.     //    ScreenElement(ELEM_LABEL,
  167.     //                  GAME_TITLE,
  168.     //                  ANCHOR_CENTER,
  169.     //                  Point(0, -150))
  170.     //);
  171.  
  172.     mainMenu.addElement(
  173.         ScreenElement(ELEM_BUTTON,
  174.                       "Begin",
  175.                       ANCHOR_BOTTOM_LEFT,
  176.                       Point(885, -125),
  177.                       BUTTON_NEW)
  178.     );
  179.  
  180.     mainMenu.addElement(
  181.         ScreenElement(ELEM_BUTTON,
  182.                       "Credits",
  183.                       ANCHOR_BOTTOM_LEFT,
  184.                       Point(885, -65),
  185.                       BUTTON_CREDITS)
  186.     );
  187.    
  188.     mainMenu.addElement(
  189.         ScreenElement(ELEM_BUTTON,
  190.                       "Quit",
  191.                       ANCHOR_BOTTOM_LEFT,
  192.                       Point(885, -5),
  193.                       BUTTON_QUIT)
  194.     );
  195.  
  196.     //Credits
  197.     credits.addElement(
  198.         ScreenElement(ELEM_BUTTON,
  199.                       "Back",
  200.                       ANCHOR_TOP_RIGHT,
  201.                       Point(-30, 30),
  202.                       BUTTON_QUIT)
  203.     );
  204.  
  205.     int yPos = CREDITS_OFFSET;
  206.  
  207.  
  208.     addCreditTitle(credits, yPos, "Programming");
  209.     addCredit(credits, yPos, "Tim Gurto");
  210.     addCredit(credits, yPos, "Jacqui Hawkins");
  211.     addCreditGap(yPos);
  212.  
  213.     addCreditTitle(credits, yPos, "Art and Graphic Design");
  214.     addCredit(credits, yPos, "Katherine Primrose");
  215.     addCreditGap(yPos);
  216.  
  217.     addCreditTitle(credits, yPos, "Sound and Music");
  218.     addCredit(credits, yPos, "Wade Clarke");
  219.     addCreditGap(yPos);
  220.  
  221.     addCreditTitle(credits, yPos, "Game Design");
  222.     addCredit(credits, yPos, "All of the above");
  223.     addCreditGap(yPos);
  224.     addCreditGap(yPos);
  225.  
  226.     addCredit(credits, yPos, "Created in 48 hours during the 2011 Sydney Game Jam");
  227. }
  228.  
  229. void addCreditTitle(Screen &creditsScreen,
  230.                     int &yPos,
  231.                     std::string text) {
  232.  
  233.     creditsScreen.addElement(
  234.         ScreenElement(ELEM_LABEL,
  235.                            text,
  236.                            ANCHOR_CENTER,
  237.                            Point(CREDITS_X_OFFSET, yPos),
  238.                            ScreenElement::NO_ID,
  239.                            0,
  240.                            0,
  241.                            22,
  242.                            CREDITS_HEADING_COLOR)
  243.     );
  244.     yPos += CREDITS_INCREMENT;
  245. }
  246.  
  247. void addCredit(Screen &creditsScreen,
  248.                int &yPos,
  249.                std::string text) {
  250.  
  251.     creditsScreen.addElement(
  252.         ScreenElement(ELEM_LABEL,
  253.                            text,
  254.                            ANCHOR_CENTER,
  255.                            Point(CREDITS_X_OFFSET, yPos),
  256.                            ScreenElement::NO_ID,
  257.                            0,
  258.                            0,
  259.                            18)
  260.     );
  261.     yPos += CREDITS_INCREMENT;
  262. }
  263.  
  264. void addCreditGap(int &yPos) {
  265.     yPos += CREDITS_GAP;
  266. }
  267.  
  268. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement