Advertisement
Archon

Impacts - main.cpp

Jan 30th, 2011
213
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 7.57 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.                             while ((outcome = (GameJam::GameOutcome)game()) == GameJam::RESTART)
  123.                                 ;
  124.                             if (outcome == GameJam::ALT_F4)
  125.                                 loop = false;
  126.                         }
  127.                             break;
  128.                     }
  129.                 }
  130.                 while (loop);
  131.  
  132.             }
  133.             else {
  134.                 while (game() == GameJam::RESTART);
  135.             }
  136.  
  137.         }
  138.  
  139.         //Quit
  140.         //TTF_Quit() happens at debug dtor
  141.         Mix_CloseAudio();
  142.         SDL_Quit();
  143.         //TODO GameJam::Surface::quit();
  144.         GameJam::Sound::quit();
  145.     }
  146.     catch (std::string& e) {
  147.         std::cerr << e << std::endl;
  148.     }
  149.     catch (...) {
  150.         std::cerr << "Unknown exception" << std::endl;
  151.     }
  152.    
  153.     return 0;
  154. }
  155.  
  156. namespace GameJam {
  157.  
  158. void buildScreens(Screen &mainMenu,
  159.                   Screen &credits) {
  160.  
  161.     //Main menu
  162.     //mainMenu.addElement(
  163.     //    ScreenElement(ELEM_LABEL,
  164.     //                  GAME_TITLE,
  165.     //                  ANCHOR_CENTER,
  166.     //                  Point(0, -150))
  167.     //);
  168.  
  169.     mainMenu.addElement(
  170.         ScreenElement(ELEM_BUTTON,
  171.                       "Begin",
  172.                       ANCHOR_BOTTOM_LEFT,
  173.                       Point(885, -125),
  174.                       BUTTON_NEW)
  175.     );
  176.  
  177.     mainMenu.addElement(
  178.         ScreenElement(ELEM_BUTTON,
  179.                       "Credits",
  180.                       ANCHOR_BOTTOM_LEFT,
  181.                       Point(885, -65),
  182.                       BUTTON_CREDITS)
  183.     );
  184.    
  185.     mainMenu.addElement(
  186.         ScreenElement(ELEM_BUTTON,
  187.                       "Quit",
  188.                       ANCHOR_BOTTOM_LEFT,
  189.                       Point(885, -5),
  190.                       BUTTON_QUIT)
  191.     );
  192.  
  193.     //Credits
  194.     credits.addElement(
  195.         ScreenElement(ELEM_BUTTON,
  196.                       "Back",
  197.                       ANCHOR_TOP_RIGHT,
  198.                       Point(-30, 30),
  199.                       BUTTON_QUIT)
  200.     );
  201.  
  202.     int yPos = CREDITS_OFFSET;
  203.  
  204.  
  205.     addCreditTitle(credits, yPos, "Programming");
  206.     addCredit(credits, yPos, "Tim Gurto");
  207.     addCredit(credits, yPos, "Jacqui Hawkins");
  208.     addCreditGap(yPos);
  209.  
  210.     addCreditTitle(credits, yPos, "Art and Graphic Design");
  211.     addCredit(credits, yPos, "Katherine Primrose");
  212.     addCreditGap(yPos);
  213.  
  214.     addCreditTitle(credits, yPos, "Sound and Music");
  215.     addCredit(credits, yPos, "Wade Clarke");
  216.     addCreditGap(yPos);
  217.  
  218.     addCreditTitle(credits, yPos, "Game Design");
  219.     addCredit(credits, yPos, "All of the above");
  220.     addCreditGap(yPos);
  221.     addCreditGap(yPos);
  222.  
  223.     addCredit(credits, yPos, "Created in 48 hours during the 2011 Sydney Game Jam");
  224. }
  225.  
  226. void addCreditTitle(Screen &creditsScreen,
  227.                     int &yPos,
  228.                     std::string text) {
  229.  
  230.     creditsScreen.addElement(
  231.         ScreenElement(ELEM_LABEL,
  232.                            text,
  233.                            ANCHOR_CENTER,
  234.                            Point(CREDITS_X_OFFSET, yPos),
  235.                            ScreenElement::NO_ID,
  236.                            0,
  237.                            0,
  238.                            22,
  239.                            CREDITS_HEADING_COLOR)
  240.     );
  241.     yPos += CREDITS_INCREMENT;
  242. }
  243.  
  244. void addCredit(Screen &creditsScreen,
  245.                int &yPos,
  246.                std::string text) {
  247.  
  248.     creditsScreen.addElement(
  249.         ScreenElement(ELEM_LABEL,
  250.                            text,
  251.                            ANCHOR_CENTER,
  252.                            Point(CREDITS_X_OFFSET, yPos),
  253.                            ScreenElement::NO_ID,
  254.                            0,
  255.                            0,
  256.                            18)
  257.     );
  258.     yPos += CREDITS_INCREMENT;
  259. }
  260.  
  261. void addCreditGap(int &yPos) {
  262.     yPos += CREDITS_GAP;
  263. }
  264.  
  265. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement