/* Starshatter: The Open Source Project Copyright (c) 2021-2022, Starshatter: The Open Source Project Contributors Copyright (c) 2011-2012, Starshatter OpenSource Distribution Contributors Copyright (c) 1997-2006, Destroyer Studios LLC. AUTHOR: John DiCamillo */ #include "Game.h" #include "Mouse.h" #include "Universe.h" #include "Window.h" #include "EventDispatch.h" #include "DataLoader.h" #include "Panic.h" #include "Pcx.h" #include "Bitmap.h" #include "MachineInfo.h" #include "VideoSettings.h" #include "ContentBundle.h" #include "Clock.h" #include "WndProc.h" #include "SoundCard.h" // +--------------------------------------------------------------------+ Game* game = 0; const double MAX_FRAME_TIME_NORMAL = 1.0 / 5.0; // +--------------------------------------------------------------------+ Game::Game() : world(0), status(Game::OK), exit_code(0), game_mode(MENU_MODE) { Clock::Init(); if (!game) { game = this; active = false; paused = false; server = false; show_mouse = false; frame_number = 0; max_frame_length = MAX_FRAME_TIME_NORMAL; } else status = TOO_MANY; } Game::~Game() { if (game == this) game = 0; Clock::Close(); delete world; } // +--------------------------------------------------------------------+ bool Game::Init(HINSTANCE hi, HINSTANCE hpi, LPSTR cmdline, int nCmdShow) { status = OK; if (status == OK) { Print(" Initializing content...\n"); ContentBundle::GetInstance()->Init(); Print(" Initializing game...\n"); if (!InitGame()) { if (Panic::Panicked()) Panic::Panic("Could not initialize the game."); status = INIT_FAILED; } } return status == OK; } // +--------------------------------------------------------------------+ bool Game::InitGame() { if (server) { Print(" InitGame() - server mode.\n"); } return true; } // +--------------------------------------------------------------------+ int Game::Run() { status = RUN; Clock::GetInstance()->Set(); while (status < EXIT && !Panic::Panicked()) { GameLoop(); } return exit_code; } // +--------------------------------------------------------------------+ void Game::Exit() { Print("\n\n*** Game::Exit()\n"); status = EXIT; } // +--------------------------------------------------------------------+ void Game::Activate(bool f) { active = f; } // +--------------------------------------------------------------------+ void Game::Pause(bool f) { paused = f; } // +--------------------------------------------------------------------+ bool Game::GameLoop() { bool wait_for_windows_events = true; if (active && !paused) { if (!server) { // Route Events to EventTargets EventDispatch* ed = EventDispatch::GetInstance(); if (ed) ed->Dispatch(); } UpdateWorld(); GameState(); if (!server) { UpdateScreen(); } wait_for_windows_events = false; } else if (active && paused) { if (GetKey()=='P') Pause(false); } Clock::GetInstance()->Step(); frame_number++; Mouse::w = 0; return wait_for_windows_events; } // +--------------------------------------------------------------------+ void Game::UpdateWorld() { if (world) world->ExecFrame(Clock::GetInstance()->Delta()); } // +--------------------------------------------------------------------+ void Game::GameState() { } // +--------------------------------------------------------------------+ void Game::UpdateScreen() { } // +--------------------------------------------------------------------+ Game* Game::GetInstance() { return game; } // +--------------------------------------------------------------------+ DWORD Game::Frame() { return frame_number; }