1. // Beginning Game Programming, Second Edition
  2. // winmain.cpp - Windows framework source code file
  3. #include <time.h>
  4. #include <stdio.h>
  5. #include "game.h"
  6.  
  7. // Cross platform compatible
  8. #include "CPTimer.h"
  9.  
  10. // Mouse state information.
  11. bool LMBDown = false;
  12. int mouseX = 0, mouseY = 0;
  13.  
  14. //timer
  15. CPTimer timer;
  16. double times;
  17. unsigned long startTick;
  18.  
  19. // flag to show the FPS on title bar
  20. bool show_FPS = true;
  21. // flag to enable/disable the FRAME RATE cap
  22. bool cap = true;
  23.  
  24. //function prototypes
  25. bool InitializeObjects();
  26.  
  27. //window event callback function
  28. LRESULT WINAPI WinProc( HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam )
  29. {
  30.     switch( msg )
  31.     {
  32.         case WM_DESTROY:
  33.             //call the "front-end" shutdown function
  34.             Game_End(hWnd);
  35.             //tell Windows to kill this program
  36.             PostQuitMessage(0);
  37.             return 0;
  38.             break;
  39.  
  40.         case WM_LBUTTONDOWN:
  41.             LMBDown = true;
  42.             break;
  43.  
  44.          case WM_LBUTTONUP:
  45.             LMBDown = false;
  46.             break;
  47.  
  48.          case WM_MOUSEMOVE:
  49.             mouseX = LOWORD (lParam);
  50.             mouseY = HIWORD (lParam);
  51.             break;
  52.     }
  53.  
  54.     return DefWindowProc( hWnd, msg, wParam, lParam );
  55. }
  56.  
  57.  
  58. //helper function to set up the window properties
  59. ATOM MyRegisterClass(HINSTANCE hInstance)
  60. {
  61.     //create the window class structure
  62.     WNDCLASSEX wc;
  63.     wc.cbSize = sizeof(WNDCLASSEX);
  64.  
  65.     //fill the struct with info
  66.     //wc.style         = CS_HREDRAW | CS_VREDRAW;
  67.     wc.style         = CS_CLASSDC;
  68.     wc.lpfnWndProc   = (WNDPROC)WinProc;
  69.     wc.cbClsExtra    = 0L;
  70.     wc.cbWndExtra    = 0L;
  71.     wc.hInstance     = hInstance;
  72.     wc.hIcon         = LoadIcon(NULL, IDI_APPLICATION);
  73.     wc.hCursor       = LoadCursor(NULL, IDC_ARROW);
  74.     wc.hbrBackground = (HBRUSH)GetStockObject(BLACK_BRUSH);
  75.     wc.lpszMenuName  = NULL;
  76.     wc.lpszClassName = APPTITLE;
  77.     wc.hIconSm       = LoadIcon(NULL, IDI_APPLICATION);
  78.     //set up the window with the class info
  79.     return RegisterClassEx(&wc);
  80. }
  81.  
  82. //
  83. static int win_screen_x = 0;
  84. static int win_screen_y = 0;
  85. static int win_fullscreen = 0; // false
  86. void setVar(string token, string value)
  87. {
  88.      int iValue = atoi(value.c_str());
  89.  
  90.      if (token == "SCREEN_X")        
  91.           win_screen_x = iValue;
  92.      if (token == "SCREEN_Y")        
  93.           win_screen_y = iValue;
  94.      if (token == "SCREEN_FULL")        
  95.           win_fullscreen = iValue;
  96. }
  97. bool loadVars(string filename)
  98. {
  99.      // open the file
  100.      ifstream input_file(filename.c_str());
  101.  
  102.      if (! input_file.is_open())
  103.      {
  104.           cout << "Unable to open file: " << filename << endl;
  105.           return false;
  106.      }
  107.  
  108.   //   cout << endl << "Loading variables from config file ..." << endl;
  109.  
  110.      // setup variables
  111.      string inputText;
  112.      string tToken = "" , tValue = "";
  113.      string lastToken;    
  114.  
  115.      //
  116.      const int MAX_TOKENS = 99;
  117.      string tokens[MAX_TOKENS];
  118.      string values[MAX_TOKENS]; // string (int) value of each token
  119.  
  120.      int currChar = 0;
  121.      int tokensFound = 0;
  122.      int tLoc = 0, tVLoc = 0;
  123.      int strLen;
  124.      char tChar;
  125.      int lineCnt = 0;
  126.      bool blankLine = true;
  127.      bool declaringVar = false;
  128.  
  129.      // 2-D array for variables
  130.      // read from file
  131.      while (! input_file.eof() )
  132.      {
  133.           // get it one line at a time
  134.           getline (input_file, inputText);
  135.           lineCnt++;
  136.  
  137.           currChar = 0;
  138.           tLoc = 0;
  139.           strLen = inputText.length();
  140.           blankLine = true;
  141.  
  142.           // get tokens
  143.           //token   value          
  144.  
  145.           // while not at end of the string
  146.           while ( currChar < strLen )
  147.           {
  148.                tChar = inputText[currChar];
  149.                // parse string
  150.  
  151.                // is it an expression ? then check for prior declaration of variable name
  152.                if ( tChar == '/' )
  153.                     currChar = strLen; // exit loop
  154.  
  155.                // if it is a letter or _ character.
  156.                if (isLetter(tChar) || (tChar == '_'))
  157.                {
  158.                     // its a letter, so we assume its a token string
  159.                     tToken += tChar;
  160.                     tLoc++;
  161.                     blankLine = false;
  162.                }
  163.                else if (isNumber(tChar))
  164.                {
  165.                     // its a number, so we assume its a value
  166.                     tValue += tChar;
  167.                     tVLoc++;
  168.                     blankLine = false;
  169.                }
  170.                else
  171.                {
  172.                     // white space or special tokens
  173.  
  174.                     // handle special characters
  175.                     if ( tLoc > 0 )
  176.                     {
  177.                          // close out the token
  178.                          tokens[tokensFound] = tToken;
  179.                          cout << tToken;
  180.                          lastToken = tToken;
  181.                          tToken = ""; // reset for next token
  182.                          tLoc = 0;
  183.                          blankLine = false;
  184.                     }
  185.                     if ( tVLoc > 0 )
  186.                     {
  187.                          // close out the value token
  188.                          values[tokensFound] = tValue;
  189.                          cout << " = " << tValue << endl;
  190.                          setVar(tokens[tokensFound],values[tokensFound]); // override default
  191.  
  192.                          tokensFound++; // increment for next token
  193.                          tValue = ""; // reset for next token
  194.                          tVLoc = 0;
  195.                          blankLine = false;
  196.                     }
  197.                }
  198.                // next character
  199.                currChar++;
  200.           } // while ... parsing each line
  201.      } // while ... not end of file
  202.  
  203.      input_file.close();
  204.  
  205.      return true;
  206. }
  207. //
  208.  
  209. //entry point for a Windows program
  210. int WINAPI WinMain(HINSTANCE hInstance,
  211.                    HINSTANCE hPrevInstance,
  212.                    LPSTR     lpCmdLine,
  213.                    int       nCmdShow)
  214. {
  215.      MSG msg;
  216.      HWND hWnd;
  217.  
  218.      // Load the Config data from file
  219.      bool read_from_config = loadVars("setup.cfg");
  220. //     int screen_x = getToken("SCREEN_WIDTH");  // this should be the value from the config file
  221. //     int screen_y = getToken("SCREEN_HEIGHT");
  222.  
  223.      // register the class
  224.     MyRegisterClass(hInstance);
  225.  
  226.     //set up the screen in windowed or fullscreen mode?
  227.     DWORD style;
  228.     if (FULLSCREEN)
  229.         style = WS_EX_TOPMOST | WS_VISIBLE | WS_POPUP;
  230.     else
  231.         style = WS_OVERLAPPED;
  232. /*
  233.     // debug, set app tile to current value of SCREEN_HEIGHT/WIDTH variables
  234.      // int to string
  235.      ostringstream buffer;
  236.      
  237. //     buffer << SCREEN_WIDTH << "x" << SCREEN_HEIGHT << "," << FULLSCREEN << " " << read_from_config << ":" << screen_x << "x" << screen_y;
  238.      buffer << win_screen_x << "x" << win_screen_y << "," << win_fullscreen << " " << read_from_config << ":" << screen_x << "x" << screen_y;
  239.      string screen_res = buffer.str();
  240.      LPCSTR AppTitle = screen_res.c_str();    
  241. */
  242.     //create a new window
  243.     hWnd = CreateWindow(
  244.        APPTITLE,              //window class
  245.        APPTITLE,    // AppTitle, //APPTITLE,              //title bar
  246.        style,                 //window style
  247.        //CW_USEDEFAULT,         //x position of window
  248.        //CW_USEDEFAULT,         //y position of window
  249.        0,
  250.       0,
  251.       win_screen_x, //SCREEN_WIDTH,          //width of the window
  252.        win_screen_y, //SCREEN_HEIGHT,         //height of the window
  253.        GetDesktopWindow(),    //parent window
  254.        NULL,                  //menu
  255.        hInstance,             //application instance
  256.        NULL);                 //window parameters
  257.  
  258.     //was there an error creating the window?
  259.     if (!hWnd)
  260.       return FALSE;
  261.  
  262.     //display the window
  263.     ShowWindow(hWnd, nCmdShow);
  264.     UpdateWindow(hWnd);
  265.                          
  266.     //initialize the game
  267.     if (!Game_Init(hWnd))
  268.     {
  269.         MessageBox(hWnd, "Error initializing the game", "Error", MB_OK);
  270.         return 0;
  271.     }
  272.  
  273.     if(!InitializeObjects()) return false;
  274.  
  275.     //SetCursor(LoadCursor(NULL, IDC_ARROW));
  276.     ShowCursor(TRUE);
  277.  
  278.     CPTimer fps,update;
  279.  
  280.     // keep track of the current frame
  281.     int frame = 0,c_frame = 0; 
  282.  
  283.     //start the frame timer  
  284.      fps.start();
  285.      double start = fps.get_ticks();
  286.      update.start();
  287.  
  288.     // main message loop
  289.     int done = 0;
  290.     while (!done)
  291.     {
  292.  
  293.         if (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
  294.         {
  295.             //look for quit message
  296.             if (msg.message == WM_QUIT)
  297.                 done = 1;
  298.  
  299.             //decode and pass messages on to WndProc
  300.             TranslateMessage(&msg);
  301.             DispatchMessage(&msg);
  302.         }
  303.         else
  304.         {
  305.              // if we want to cap the frame rate
  306.         if ( cap == true )
  307.         {
  308.             if ((fps.get_ticks() - start) >= (1000 / FRAMES_PER_SECOND))
  309.             {
  310.                 start = fps.get_ticks();
  311.                 // increment the frame counter
  312.                 frame++;
  313.                 //process game loop (else prevents running after window is closed)
  314.                 Game_Run(hWnd);
  315.             }
  316.         }
  317.         else
  318.         {
  319.             // increment the frame counter
  320.             frame++;
  321.             //process game loop (else prevents running after window is closed)
  322.             Game_Run(hWnd);
  323.         }
  324.         }
  325.  
  326.         if (show_FPS)
  327.         if ( update.get_ticks() > 1000 )
  328.         {
  329.             //The frame rate as a string
  330.             std::stringstream caption;
  331.  
  332.             //Calculate the frames per second and create the string
  333.             caption <<  "BugHunt 2942AD  (DirectX) ~Avg FPS: " << frame / ( fps.get_ticks() / 1000.f );
  334.  
  335.             //Reset the caption
  336.                SetWindowText(hWnd,caption.str().c_str());
  337.  
  338.             // reset the counter for updates
  339.             update.start();
  340.         }
  341.     }
  342.  
  343.     return msg.wParam;
  344. }
  345.  
  346.  
  347. bool InitializeObjects()
  348. {
  349.     timer.start();
  350.  
  351.     return true;
  352. }