2014-09-16 20:22:43 +02:00
|
|
|
#include "RWGame.hpp"
|
|
|
|
#include "State.hpp"
|
|
|
|
#include "loadingstate.hpp"
|
2015-02-15 13:41:51 +01:00
|
|
|
#include "DrawUI.hpp"
|
2015-04-03 02:26:20 +02:00
|
|
|
#include "ingamestate.hpp"
|
|
|
|
#include "menustate.hpp"
|
2014-09-16 20:22:43 +02:00
|
|
|
|
2015-04-27 03:09:56 +02:00
|
|
|
#include <objects/GameObject.hpp>
|
2014-12-16 20:17:22 +01:00
|
|
|
#include <engine/GameState.hpp>
|
2015-05-03 06:22:03 +02:00
|
|
|
#include <engine/SaveGame.hpp>
|
2015-02-07 23:55:06 +01:00
|
|
|
#include <engine/GameWorld.hpp>
|
2014-09-16 20:22:43 +02:00
|
|
|
#include <render/GameRenderer.hpp>
|
2015-01-22 21:16:28 +01:00
|
|
|
#include <render/DebugDraw.hpp>
|
2015-04-04 04:12:28 +02:00
|
|
|
|
2014-09-16 20:22:43 +02:00
|
|
|
#include <script/ScriptMachine.hpp>
|
2015-04-04 04:12:28 +02:00
|
|
|
#include <script/modules/VMModule.hpp>
|
|
|
|
#include <script/modules/GameModule.hpp>
|
|
|
|
#include <script/modules/ObjectModule.hpp>
|
2014-09-16 20:22:43 +02:00
|
|
|
|
|
|
|
#include <data/CutsceneData.hpp>
|
|
|
|
#include <ai/PlayerController.hpp>
|
2015-01-22 21:16:28 +01:00
|
|
|
#include <objects/CharacterObject.hpp>
|
|
|
|
#include <objects/VehicleObject.hpp>
|
|
|
|
|
|
|
|
DebugDraw* debug;
|
2014-09-16 20:22:43 +02:00
|
|
|
|
2015-03-05 04:37:13 +01:00
|
|
|
StdOutReciever logPrinter;
|
|
|
|
|
2015-01-30 11:04:25 +01:00
|
|
|
RWGame::RWGame(const std::string& gamepath, int argc, char* argv[])
|
2015-04-27 16:31:39 +02:00
|
|
|
: state(nullptr), world(nullptr), renderer(nullptr), script(nullptr), inFocus(true),
|
2015-04-27 05:12:58 +02:00
|
|
|
showDebugStats(false), showDebugPaths(false), showDebugPhysics(false),
|
2015-04-20 03:19:30 +02:00
|
|
|
accum(0.f), timescale(1.f)
|
2014-09-16 20:22:43 +02:00
|
|
|
{
|
|
|
|
size_t w = GAME_WINDOW_WIDTH, h = GAME_WINDOW_HEIGHT;
|
2015-01-30 11:07:53 +01:00
|
|
|
bool fullscreen = false;
|
2015-04-03 02:26:20 +02:00
|
|
|
bool newgame = false;
|
2015-04-04 18:48:36 +02:00
|
|
|
bool test = false;
|
2015-04-04 04:12:28 +02:00
|
|
|
bool debugscript = false;
|
2014-09-16 20:22:43 +02:00
|
|
|
|
2015-01-30 11:04:25 +01:00
|
|
|
for( int i = 1; i < argc; ++i )
|
|
|
|
{
|
|
|
|
if( strcasecmp( "-w", argv[i] ) == 0 && i+1 < argc )
|
|
|
|
{
|
|
|
|
w = std::atoi(argv[i+1]);
|
|
|
|
}
|
|
|
|
if( strcasecmp( "-h", argv[i] ) == 0 && i+1 < argc )
|
|
|
|
{
|
|
|
|
h = std::atoi(argv[i+1]);
|
|
|
|
}
|
2015-01-30 11:07:53 +01:00
|
|
|
if( strcasecmp( "-f", argv[i] ) == 0 )
|
|
|
|
{
|
|
|
|
fullscreen = true;
|
|
|
|
}
|
2015-04-03 02:26:20 +02:00
|
|
|
if( strcmp( "--newgame", argv[i] ) == 0 )
|
|
|
|
{
|
|
|
|
newgame = true;
|
|
|
|
}
|
2015-04-04 18:48:36 +02:00
|
|
|
if( strcmp( "--test", argv[i] ) == 0 )
|
|
|
|
{
|
|
|
|
test = true;
|
|
|
|
}
|
2015-04-04 04:12:28 +02:00
|
|
|
if( strcmp( "--debug", argv[i] ) == 0 )
|
|
|
|
{
|
|
|
|
debugscript = true;
|
|
|
|
}
|
2015-01-30 11:07:53 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
sf::Uint32 style = sf::Style::Default;
|
|
|
|
if( fullscreen )
|
|
|
|
{
|
|
|
|
style |= sf::Style::Fullscreen;
|
2015-01-30 11:04:25 +01:00
|
|
|
}
|
|
|
|
|
2014-09-16 20:22:43 +02:00
|
|
|
sf::ContextSettings cs;
|
|
|
|
cs.depthBits = 32;
|
2015-01-30 11:07:53 +01:00
|
|
|
window.create(sf::VideoMode(w, h), "", style, cs);
|
2014-09-16 20:22:43 +02:00
|
|
|
window.setVerticalSyncEnabled(true);
|
|
|
|
window.setMouseCursorVisible(false);
|
|
|
|
|
|
|
|
glewExperimental = GL_TRUE;
|
|
|
|
glewInit();
|
2015-03-30 03:45:58 +02:00
|
|
|
|
|
|
|
log.addReciever(&logPrinter);
|
|
|
|
log.info("Game", "Game directory: " + gamepath);
|
|
|
|
|
|
|
|
if(! GameData::isValidGameDirectory(gamepath) )
|
|
|
|
{
|
|
|
|
std::string envname(ENV_GAME_PATH_NAME);
|
|
|
|
throw std::runtime_error("Invalid game directory path, is " +envname+ " set?");
|
|
|
|
}
|
2014-09-16 20:22:43 +02:00
|
|
|
|
2015-04-27 04:55:18 +02:00
|
|
|
data = new GameData(&log, &work, gamepath);
|
2015-04-18 02:11:17 +02:00
|
|
|
|
2014-09-16 20:22:43 +02:00
|
|
|
// Initalize all the archives.
|
2015-04-27 04:55:18 +02:00
|
|
|
data->loadIMG("/models/gta3");
|
2015-04-18 02:11:17 +02:00
|
|
|
//engine->data.loadIMG("/models/txd");
|
2015-04-27 04:55:18 +02:00
|
|
|
data->loadIMG("/anim/cuts");
|
2015-02-07 23:55:06 +01:00
|
|
|
|
2015-04-27 04:55:18 +02:00
|
|
|
data->load();
|
2015-03-30 03:45:58 +02:00
|
|
|
|
2015-03-05 17:36:14 +01:00
|
|
|
// Initialize renderer
|
2015-04-27 04:55:18 +02:00
|
|
|
renderer = new GameRenderer(&log, data);
|
2015-03-05 17:36:14 +01:00
|
|
|
|
2015-02-07 23:55:06 +01:00
|
|
|
// Set up text renderer
|
2015-03-05 17:36:14 +01:00
|
|
|
renderer->text.setFontTexture(0, "pager");
|
|
|
|
renderer->text.setFontTexture(1, "font1");
|
|
|
|
renderer->text.setFontTexture(2, "font2");
|
2014-09-16 20:22:43 +02:00
|
|
|
|
2015-01-22 21:16:28 +01:00
|
|
|
debug = new DebugDraw;
|
|
|
|
debug->setDebugMode(btIDebugDraw::DBG_DrawWireframe | btIDebugDraw::DBG_DrawConstraints | btIDebugDraw::DBG_DrawConstraintLimits);
|
2015-04-04 22:23:53 +02:00
|
|
|
debug->setShaderProgram(renderer->worldProg);
|
2014-09-16 20:22:43 +02:00
|
|
|
|
2015-04-27 04:55:18 +02:00
|
|
|
data->loadDynamicObjects(gamepath + "/data/object.dat");
|
2014-09-16 20:22:43 +02:00
|
|
|
|
|
|
|
/// @TODO language choices.
|
2015-04-27 04:55:18 +02:00
|
|
|
data->loadGXT("english.gxt");
|
2015-01-25 18:54:31 +01:00
|
|
|
|
2015-04-27 04:55:18 +02:00
|
|
|
getRenderer()->water.setWaterTable(data->waterHeights, 48, data->realWater, 128*128);
|
2015-03-28 14:42:29 +01:00
|
|
|
|
2015-01-25 18:54:31 +01:00
|
|
|
for(int m = 0; m < MAP_BLOCK_SIZE; ++m)
|
|
|
|
{
|
|
|
|
std::string num = (m < 10 ? "0" : "");
|
|
|
|
std::string name = "radar" + num + std::to_string(m);
|
2015-04-27 04:55:18 +02:00
|
|
|
data->loadTXD(name + ".txd");
|
2015-01-25 18:54:31 +01:00
|
|
|
}
|
2014-09-16 20:22:43 +02:00
|
|
|
|
2015-04-03 02:26:20 +02:00
|
|
|
auto loading = new LoadingState(this);
|
|
|
|
if( newgame )
|
|
|
|
{
|
2015-05-03 06:22:03 +02:00
|
|
|
loading->setNextState(new IngameState(this,true,test));
|
2015-04-03 02:26:20 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
loading->setNextState(new MenuState(this));
|
|
|
|
}
|
|
|
|
|
|
|
|
StateManager::get().enter(loading);
|
2014-09-16 20:22:43 +02:00
|
|
|
|
2015-03-30 03:45:58 +02:00
|
|
|
log.info("Game", "Started");
|
2014-09-16 20:22:43 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
RWGame::~RWGame()
|
|
|
|
{
|
2015-04-04 04:12:28 +02:00
|
|
|
delete script;
|
2015-03-05 17:36:14 +01:00
|
|
|
delete renderer;
|
2015-04-27 16:31:39 +02:00
|
|
|
delete world;
|
2015-04-27 04:55:18 +02:00
|
|
|
delete state;
|
|
|
|
}
|
|
|
|
|
|
|
|
void RWGame::newGame()
|
|
|
|
{
|
|
|
|
if( state != nullptr )
|
|
|
|
{
|
|
|
|
log.error("Game", "Cannot start a new game: game is already running.");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
state = new GameState;
|
2015-04-27 16:31:39 +02:00
|
|
|
world = new GameWorld(&log, &work, data);
|
|
|
|
world->dynamicsWorld->setDebugDrawer(debug);
|
|
|
|
|
|
|
|
// Associate the new world with the new state and vice versa
|
|
|
|
state->world = world;
|
|
|
|
world->state = state;
|
2015-05-03 06:22:03 +02:00
|
|
|
|
|
|
|
for(std::map<std::string, std::string>::iterator it = world->data->iplLocations.begin();
|
|
|
|
it != world->data->iplLocations.end();
|
|
|
|
++it) {
|
|
|
|
world->data->loadZone(it->second);
|
|
|
|
world->placeItems(it->second);
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
void RWGame::saveGame(const std::string& savename)
|
|
|
|
{
|
|
|
|
// Save games without a script don't make much sense at the moment
|
|
|
|
if( script )
|
|
|
|
{
|
|
|
|
SaveGame::writeScript(*script, savename+".script");
|
|
|
|
SaveGame::writeState(*state, savename+".state");
|
|
|
|
SaveGame::writeObjects(*world, savename+".world");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void RWGame::loadGame(const std::string& savename)
|
|
|
|
{
|
|
|
|
delete state->world;
|
2015-06-14 03:44:51 +02:00
|
|
|
delete state->script;
|
2015-05-03 06:22:03 +02:00
|
|
|
state = nullptr;
|
|
|
|
|
|
|
|
newGame();
|
|
|
|
|
|
|
|
startScript("data/main.scm");
|
|
|
|
|
2015-06-14 19:08:55 +02:00
|
|
|
if(! SaveGame::loadGame(*state, savename) )
|
2015-05-03 06:22:03 +02:00
|
|
|
{
|
2015-06-14 03:44:51 +02:00
|
|
|
log.error("Game", "Failed to load game");
|
2015-05-03 06:22:03 +02:00
|
|
|
}
|
2014-09-16 20:22:43 +02:00
|
|
|
}
|
|
|
|
|
2015-04-04 04:12:28 +02:00
|
|
|
void RWGame::startScript(const std::string& name)
|
|
|
|
{
|
2015-04-27 16:31:39 +02:00
|
|
|
SCMFile* f = world->data->loadSCM(name);
|
2015-04-04 04:12:28 +02:00
|
|
|
if( f ) {
|
2015-05-03 06:22:03 +02:00
|
|
|
if( script )
|
|
|
|
{
|
|
|
|
delete script;
|
|
|
|
}
|
2015-04-04 04:12:28 +02:00
|
|
|
|
|
|
|
SCMOpcodes* opcodes = new SCMOpcodes;
|
|
|
|
opcodes->modules.push_back(new VMModule);
|
|
|
|
opcodes->modules.push_back(new GameModule);
|
|
|
|
opcodes->modules.push_back(new ObjectModule);
|
|
|
|
|
2015-04-27 16:31:39 +02:00
|
|
|
script = new ScriptMachine(state, f, opcodes);
|
2015-04-04 04:12:28 +02:00
|
|
|
|
|
|
|
// Set up breakpoint handler
|
|
|
|
script->setBreakpointHandler(
|
|
|
|
[&](const SCMBreakpoint& bp)
|
|
|
|
{
|
|
|
|
log.info("Script", "Breakpoint hit!");
|
|
|
|
std::stringstream ss;
|
|
|
|
ss << " " << bp.function->description << ".";
|
|
|
|
ss << " Args:";
|
|
|
|
for(int a = 0; a < bp.args->getParameters().size(); a++)
|
|
|
|
{
|
|
|
|
auto& arg = bp.args->getParameters()[a];
|
|
|
|
ss << " " << arg.integerValue();
|
|
|
|
if( a != bp.args->getParameters().size()-1 )
|
|
|
|
{
|
|
|
|
ss << ",";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
log.info("Script", ss.str());
|
|
|
|
});
|
|
|
|
script->addBreakpoint(0);
|
2015-06-14 03:44:51 +02:00
|
|
|
state->script = script;
|
2015-04-04 04:12:28 +02:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
log.error("Game", "Failed to load SCM: " + name);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-05-03 06:22:03 +02:00
|
|
|
PlayerController *RWGame::getPlayer()
|
|
|
|
{
|
|
|
|
auto object = world->findObject(state->playerObject);
|
|
|
|
if( object )
|
|
|
|
{
|
|
|
|
auto controller = static_cast<CharacterObject*>(object)->controller;
|
|
|
|
return static_cast<PlayerController*>(controller);
|
|
|
|
}
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2014-09-16 20:22:43 +02:00
|
|
|
int RWGame::run()
|
|
|
|
{
|
|
|
|
clock.restart();
|
2015-04-03 16:38:24 +02:00
|
|
|
|
2014-09-16 20:22:43 +02:00
|
|
|
// Loop until the window is closed or we run out of state.
|
|
|
|
while (window.isOpen() && StateManager::get().states.size()) {
|
2015-04-03 16:38:24 +02:00
|
|
|
State* state = StateManager::get().states.back();
|
|
|
|
|
2014-09-16 20:22:43 +02:00
|
|
|
sf::Event event;
|
|
|
|
while (window.pollEvent(event)) {
|
|
|
|
switch (event.type) {
|
|
|
|
case sf::Event::GainedFocus:
|
|
|
|
inFocus = true;
|
|
|
|
break;
|
|
|
|
case sf::Event::LostFocus:
|
|
|
|
inFocus = false;
|
|
|
|
break;
|
|
|
|
case sf::Event::KeyPressed:
|
|
|
|
globalKeyEvent(event);
|
2015-02-26 03:06:43 +01:00
|
|
|
break;
|
|
|
|
case sf::Event::Closed:
|
|
|
|
return 0;
|
2014-09-16 20:22:43 +02:00
|
|
|
default: break;
|
|
|
|
}
|
|
|
|
|
2015-04-03 16:38:24 +02:00
|
|
|
state->handleEvent(event);
|
2014-09-16 20:22:43 +02:00
|
|
|
}
|
2014-12-11 19:11:36 +01:00
|
|
|
|
|
|
|
if(! window.isOpen() )
|
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
2014-09-16 20:22:43 +02:00
|
|
|
|
2015-02-16 01:44:30 +01:00
|
|
|
float timer = clock.restart().asSeconds();
|
|
|
|
accum += timer * timescale;
|
2014-09-16 20:22:43 +02:00
|
|
|
|
|
|
|
while ( accum >= GAME_TIMESTEP ) {
|
2015-04-03 16:38:24 +02:00
|
|
|
StateManager::get().tick(GAME_TIMESTEP);
|
2014-09-16 20:22:43 +02:00
|
|
|
|
2015-04-03 16:38:24 +02:00
|
|
|
tick(GAME_TIMESTEP);
|
2015-02-04 18:16:46 +01:00
|
|
|
|
2014-09-16 20:22:43 +02:00
|
|
|
accum -= GAME_TIMESTEP;
|
2015-02-16 01:39:19 +01:00
|
|
|
|
|
|
|
// Throw away time if the accumulator reaches too high.
|
2015-04-03 16:38:24 +02:00
|
|
|
if ( accum > GAME_TIMESTEP * 5.f )
|
2015-02-16 01:39:19 +01:00
|
|
|
{
|
|
|
|
accum = 0.f;
|
|
|
|
}
|
2014-09-16 20:22:43 +02:00
|
|
|
}
|
|
|
|
|
2015-02-16 01:39:19 +01:00
|
|
|
float alpha = fmod(accum, GAME_TIMESTEP) / GAME_TIMESTEP;
|
2015-04-03 16:38:24 +02:00
|
|
|
if( ! state->shouldWorldUpdate() )
|
|
|
|
{
|
|
|
|
alpha = 1.f;
|
|
|
|
}
|
2014-09-16 20:22:43 +02:00
|
|
|
|
2015-02-16 01:44:30 +01:00
|
|
|
render(alpha, timer);
|
2014-09-16 20:22:43 +02:00
|
|
|
|
2015-03-05 17:36:14 +01:00
|
|
|
StateManager::get().draw(renderer);
|
2014-09-16 20:22:43 +02:00
|
|
|
|
|
|
|
window.display();
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
void RWGame::tick(float dt)
|
|
|
|
{
|
|
|
|
// Process the Engine's background work.
|
2015-04-27 16:31:39 +02:00
|
|
|
world->_work->update();
|
2015-04-03 16:38:24 +02:00
|
|
|
|
2015-04-27 16:31:39 +02:00
|
|
|
State* currState = StateManager::get().states.back();
|
2015-04-03 16:38:24 +02:00
|
|
|
|
2014-09-16 20:22:43 +02:00
|
|
|
static float clockAccumulator = 0.f;
|
2015-05-08 18:54:11 +02:00
|
|
|
if ( currState->shouldWorldUpdate() ) {
|
2015-05-09 01:49:28 +02:00
|
|
|
// Clear out any per-tick state.
|
|
|
|
world->clearTickData();
|
|
|
|
|
2015-05-03 06:22:03 +02:00
|
|
|
state->gameTime += dt;
|
2014-09-16 20:22:43 +02:00
|
|
|
|
|
|
|
clockAccumulator += dt;
|
|
|
|
while( clockAccumulator >= 1.f ) {
|
2015-04-27 16:31:39 +02:00
|
|
|
world->state->minute ++;
|
|
|
|
while( state->minute >= 60 ) {
|
|
|
|
state->minute = 0;
|
|
|
|
state->hour ++;
|
|
|
|
while( state->hour >= 24 ) {
|
|
|
|
state->hour = 0;
|
2014-09-16 20:22:43 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
clockAccumulator -= 1.f;
|
|
|
|
}
|
2015-03-06 17:55:46 +01:00
|
|
|
|
|
|
|
// Clean up old VisualFX
|
2015-04-27 16:31:39 +02:00
|
|
|
for( int i = 0; i < world->effects.size(); ++i )
|
2015-03-06 17:55:46 +01:00
|
|
|
{
|
2015-04-27 16:31:39 +02:00
|
|
|
VisualFX* effect = world->effects[i];
|
2015-03-06 17:55:46 +01:00
|
|
|
if( effect->getType() == VisualFX::Particle )
|
|
|
|
{
|
|
|
|
auto& part = effect->particle;
|
|
|
|
if( part.lifetime < 0.f ) continue;
|
2015-05-03 06:22:03 +02:00
|
|
|
if( world->getGameTime() >= part.starttime + part.lifetime )
|
2015-03-06 17:55:46 +01:00
|
|
|
{
|
2015-04-27 16:31:39 +02:00
|
|
|
world->destroyEffect( effect );
|
2015-03-06 17:55:46 +01:00
|
|
|
--i;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2014-09-16 20:22:43 +02:00
|
|
|
|
2015-04-29 22:03:53 +02:00
|
|
|
for( auto& p : world->objects ) {
|
|
|
|
GameObject* object = p.second;
|
2014-09-16 20:22:43 +02:00
|
|
|
object->_updateLastTransform();
|
|
|
|
object->tick(dt);
|
|
|
|
}
|
2014-12-17 23:53:25 +01:00
|
|
|
|
2015-04-27 16:31:39 +02:00
|
|
|
world->destroyQueuedObjects();
|
|
|
|
state->texts.clear();
|
2014-09-16 20:22:43 +02:00
|
|
|
|
2015-04-27 16:31:39 +02:00
|
|
|
for( int i = 0; i < state->text.size(); )
|
2015-04-05 03:08:51 +02:00
|
|
|
{
|
2015-04-27 16:31:39 +02:00
|
|
|
auto& text = state->text[i];
|
2015-05-03 06:22:03 +02:00
|
|
|
if( world->getGameTime() > text.osTextStart + text.osTextTime )
|
2015-04-05 03:08:51 +02:00
|
|
|
{
|
2015-04-27 16:31:39 +02:00
|
|
|
state->text.erase(state->text.begin() + i);
|
2015-04-05 03:08:51 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
i++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-04-27 16:31:39 +02:00
|
|
|
world->dynamicsWorld->stepSimulation(dt, 2, dt);
|
2014-12-17 23:53:25 +01:00
|
|
|
|
2015-04-04 04:12:28 +02:00
|
|
|
if( script ) {
|
2014-09-16 20:22:43 +02:00
|
|
|
try {
|
2015-04-04 04:12:28 +02:00
|
|
|
script->execute(dt);
|
2014-09-16 20:22:43 +02:00
|
|
|
}
|
|
|
|
catch( SCMException& ex ) {
|
|
|
|
std::cerr << ex.what() << std::endl;
|
2015-03-30 03:45:58 +02:00
|
|
|
log.error( "Script", ex.what() );
|
2014-09-16 20:22:43 +02:00
|
|
|
throw;
|
|
|
|
}
|
|
|
|
}
|
2015-02-18 16:29:39 +01:00
|
|
|
|
2015-05-03 06:22:03 +02:00
|
|
|
if ( state->playerObject )
|
2015-02-18 16:29:39 +01:00
|
|
|
{
|
|
|
|
// Use the current camera position to spawn pedestrians.
|
|
|
|
auto p = nextCam.position;
|
2015-04-27 16:31:39 +02:00
|
|
|
world->cleanupTraffic(p);
|
|
|
|
world->createTraffic(p);
|
2015-02-18 16:29:39 +01:00
|
|
|
}
|
2014-09-16 20:22:43 +02:00
|
|
|
}
|
2015-04-03 16:38:24 +02:00
|
|
|
|
2014-09-16 20:22:43 +02:00
|
|
|
// render() needs two cameras to smoothly interpolate between ticks.
|
|
|
|
lastCam = nextCam;
|
2015-04-27 16:31:39 +02:00
|
|
|
nextCam = currState->getCamera();
|
2014-09-16 20:22:43 +02:00
|
|
|
}
|
|
|
|
|
2015-02-16 01:44:30 +01:00
|
|
|
void RWGame::render(float alpha, float time)
|
2014-09-16 20:22:43 +02:00
|
|
|
{
|
2015-03-28 14:54:28 +01:00
|
|
|
lastDraws = getRenderer()->getRenderer()->getDrawCount();
|
|
|
|
|
|
|
|
getRenderer()->getRenderer()->swap();
|
|
|
|
|
2015-02-07 23:55:06 +01:00
|
|
|
auto size = getWindow().getSize();
|
2015-03-28 14:42:29 +01:00
|
|
|
renderer->setViewport(size.x, size.y);
|
2015-02-07 23:55:06 +01:00
|
|
|
|
2014-09-16 20:22:43 +02:00
|
|
|
ViewCamera viewCam;
|
2015-03-30 02:03:22 +02:00
|
|
|
viewCam.frustum.fov = glm::radians(90.f);
|
2015-04-27 16:31:39 +02:00
|
|
|
if( state->currentCutscene != nullptr && state->cutsceneStartTime >= 0.f )
|
2014-09-16 20:22:43 +02:00
|
|
|
{
|
2015-04-27 16:31:39 +02:00
|
|
|
auto cutscene = state->currentCutscene;
|
2015-05-03 06:22:03 +02:00
|
|
|
float cutsceneTime = std::min(world->getGameTime() - state->cutsceneStartTime,
|
2014-09-16 20:22:43 +02:00
|
|
|
cutscene->tracks.duration);
|
|
|
|
cutsceneTime += GAME_TIMESTEP * alpha;
|
|
|
|
glm::vec3 cameraPos = cutscene->tracks.getPositionAt(cutsceneTime),
|
|
|
|
targetPos = cutscene->tracks.getTargetAt(cutsceneTime);
|
|
|
|
float zoom = cutscene->tracks.getZoomAt(cutsceneTime);
|
|
|
|
viewCam.frustum.fov = glm::radians(zoom);
|
|
|
|
float tilt = cutscene->tracks.getRotationAt(cutsceneTime);
|
|
|
|
|
|
|
|
auto direction = glm::normalize(targetPos - cameraPos);
|
|
|
|
auto right = glm::normalize(glm::cross(glm::vec3(0.f, 0.f, 1.f), direction));
|
|
|
|
auto up = glm::normalize(glm::cross(direction, right));
|
|
|
|
|
|
|
|
glm::mat3 m;
|
|
|
|
m[0][0] = direction.x;
|
|
|
|
m[0][1] = right.x;
|
|
|
|
m[0][2] = up.x;
|
|
|
|
|
|
|
|
m[1][0] = direction.y;
|
|
|
|
m[1][1] = right.y;
|
|
|
|
m[1][2] = up.y;
|
|
|
|
|
|
|
|
m[2][0] = direction.z;
|
|
|
|
m[2][1] = right.z;
|
|
|
|
m[2][2] = up.z;
|
|
|
|
|
|
|
|
auto qtilt = glm::angleAxis(glm::radians(tilt), direction);
|
|
|
|
|
|
|
|
cameraPos += cutscene->meta.sceneOffset;
|
|
|
|
targetPos += cutscene->meta.sceneOffset;
|
|
|
|
|
|
|
|
viewCam.position = cameraPos;
|
|
|
|
viewCam.rotation = glm::inverse(glm::quat_cast(m)) * qtilt;
|
|
|
|
}
|
2015-04-27 16:31:39 +02:00
|
|
|
else if( state->cameraFixed )
|
2014-12-16 20:17:22 +01:00
|
|
|
{
|
2015-04-27 16:31:39 +02:00
|
|
|
viewCam.position = state->cameraPosition;
|
|
|
|
viewCam.rotation = state->cameraRotation;
|
2014-12-16 20:17:22 +01:00
|
|
|
}
|
2014-09-16 20:22:43 +02:00
|
|
|
else
|
|
|
|
{
|
|
|
|
// There's no cutscene playing - use the camera returned by the State.
|
|
|
|
viewCam.position = glm::mix(lastCam.position, nextCam.position, alpha);
|
|
|
|
viewCam.rotation = glm::slerp(lastCam.rotation, nextCam.rotation, alpha);
|
|
|
|
}
|
|
|
|
|
|
|
|
viewCam.frustum.aspectRatio = window.getSize().x / (float) window.getSize().y;
|
2015-01-27 14:59:09 +01:00
|
|
|
|
2015-04-27 16:31:39 +02:00
|
|
|
if ( state->isCinematic )
|
2015-01-27 14:59:09 +01:00
|
|
|
{
|
2015-02-04 18:16:46 +01:00
|
|
|
viewCam.frustum.fov *= viewCam.frustum.aspectRatio;
|
2015-01-27 14:59:09 +01:00
|
|
|
}
|
2014-09-16 20:22:43 +02:00
|
|
|
|
|
|
|
glEnable(GL_DEPTH_TEST);
|
|
|
|
glClear(GL_DEPTH_BUFFER_BIT|GL_COLOR_BUFFER_BIT);
|
|
|
|
|
2015-04-10 00:54:56 +02:00
|
|
|
renderer->getRenderer()->pushDebugGroup("World");
|
|
|
|
|
2015-04-27 16:31:39 +02:00
|
|
|
renderer->renderWorld(world, viewCam, alpha);
|
2015-04-10 00:54:56 +02:00
|
|
|
|
|
|
|
auto rendertime = renderer->getRenderer()->popDebugGroup();
|
|
|
|
|
2015-04-20 03:19:30 +02:00
|
|
|
if( showDebugPaths )
|
|
|
|
{
|
|
|
|
renderDebugPaths(time);
|
|
|
|
}
|
|
|
|
|
2015-02-16 01:39:19 +01:00
|
|
|
if ( showDebugStats )
|
|
|
|
{
|
2015-04-10 00:54:56 +02:00
|
|
|
renderDebugStats(time, rendertime);
|
2015-02-16 01:39:19 +01:00
|
|
|
}
|
2015-04-27 05:12:58 +02:00
|
|
|
|
|
|
|
if( showDebugPhysics )
|
|
|
|
{
|
2015-04-27 16:31:39 +02:00
|
|
|
if( world )
|
2015-04-27 05:12:58 +02:00
|
|
|
{
|
2015-04-27 16:31:39 +02:00
|
|
|
world->dynamicsWorld->debugDrawWorld();
|
2015-04-27 05:12:58 +02:00
|
|
|
debug->flush(renderer);
|
|
|
|
}
|
|
|
|
}
|
2015-02-16 01:39:19 +01:00
|
|
|
|
2015-04-27 16:31:39 +02:00
|
|
|
drawOnScreenText(world, renderer);
|
2015-02-16 01:39:19 +01:00
|
|
|
}
|
|
|
|
|
2015-04-12 20:47:47 +02:00
|
|
|
void RWGame::renderDebugStats(float time, Renderer::ProfileInfo& worldRenderTime)
|
2015-02-16 01:39:19 +01:00
|
|
|
{
|
2015-04-11 03:21:15 +02:00
|
|
|
// Turn time into milliseconds
|
|
|
|
float time_ms = time * 1000.f;
|
2015-04-05 20:06:15 +02:00
|
|
|
constexpr size_t average_every_frame = 15;
|
|
|
|
static float times[average_every_frame];
|
|
|
|
static size_t times_index = 0;
|
|
|
|
static float time_average = 0;
|
2015-04-11 03:21:15 +02:00
|
|
|
times[times_index++] = time_ms;
|
2015-04-05 20:06:15 +02:00
|
|
|
if (times_index >= average_every_frame) {
|
|
|
|
times_index = 0;
|
2015-04-06 21:02:12 +02:00
|
|
|
time_average = 0;
|
2015-04-05 20:06:15 +02:00
|
|
|
|
|
|
|
for (int i = 0; i < average_every_frame; ++i) {
|
|
|
|
time_average += times[i];
|
|
|
|
}
|
|
|
|
time_average /= average_every_frame;
|
|
|
|
}
|
|
|
|
|
2015-04-12 20:47:47 +02:00
|
|
|
std::map<std::string, Renderer::ProfileInfo*> profGroups {
|
|
|
|
{"Objects", &renderer->profObjects},
|
|
|
|
{"Effects", &renderer->profEffects},
|
|
|
|
{"Sky", &renderer->profSky},
|
|
|
|
{"Water", &renderer->profWater},
|
|
|
|
};
|
|
|
|
|
2015-02-16 01:39:19 +01:00
|
|
|
std::stringstream ss;
|
2015-04-11 03:21:15 +02:00
|
|
|
ss << "Frametime: " << time_ms << " (FPS " << (1.f/time) << ")\n";
|
|
|
|
ss << "Average (per " << average_every_frame << " frames); Frametime: " << time_average << " (FPS " << (1000.f/time_average) << ")\n";
|
2015-03-28 14:54:28 +01:00
|
|
|
ss << "Draws: " << lastDraws << " (" << renderer->culled << " Culls)\n";
|
2015-04-10 00:54:56 +02:00
|
|
|
ss << " Texture binds: " << renderer->getRenderer()->getTextureCount() << "\n";
|
|
|
|
ss << " Buffer binds: " << renderer->getRenderer()->getBufferCount() << "\n";
|
2015-04-12 20:47:47 +02:00
|
|
|
ss << " World time: " << (worldRenderTime.duration/1000000) << "ms\n";
|
|
|
|
for(auto& perf : profGroups)
|
|
|
|
{
|
|
|
|
ss << " " << perf.first << ": "
|
|
|
|
<< perf.second->draws << " draws " << perf.second->primitives << " prims "
|
|
|
|
<< (perf.second->duration/1000000) << "ms\n";
|
|
|
|
}
|
2015-01-22 21:16:28 +01:00
|
|
|
|
2015-02-18 16:29:39 +01:00
|
|
|
// Count the number of interesting objects.
|
|
|
|
int peds = 0, cars = 0;
|
2015-04-29 22:03:53 +02:00
|
|
|
for( auto& p : world->objects )
|
2015-02-18 16:29:39 +01:00
|
|
|
{
|
2015-04-29 22:03:53 +02:00
|
|
|
GameObject* object = p.second;
|
2015-02-18 16:29:39 +01:00
|
|
|
switch ( object->type() )
|
|
|
|
{
|
|
|
|
case GameObject::Character: peds++; break;
|
|
|
|
case GameObject::Vehicle: cars++; break;
|
|
|
|
default: break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
ss << "P " << peds << " V " << cars << "\n";
|
|
|
|
|
2015-05-03 06:22:03 +02:00
|
|
|
if( state->playerObject ) {
|
|
|
|
ss << "Player (" << state->playerObject << ")\n";
|
|
|
|
auto object = world->findObject(state->playerObject);
|
|
|
|
auto player = static_cast<CharacterObject*>(object)->controller;
|
2015-02-16 01:39:19 +01:00
|
|
|
ss << "Player Activity: ";
|
2015-05-03 06:22:03 +02:00
|
|
|
if( player->getCurrentActivity() ) {
|
|
|
|
ss << player->getCurrentActivity()->name();
|
2014-09-16 20:22:43 +02:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
ss << "Idle";
|
|
|
|
}
|
|
|
|
ss << std::endl;
|
2015-02-16 01:39:19 +01:00
|
|
|
}
|
2015-02-07 23:55:06 +01:00
|
|
|
|
|
|
|
TextRenderer::TextInfo ti;
|
2015-02-16 01:39:19 +01:00
|
|
|
ti.text = ss.str();
|
2015-02-07 23:55:06 +01:00
|
|
|
ti.font = 2;
|
|
|
|
ti.screenPosition = glm::vec2( 10.f, 10.f );
|
2015-02-16 01:39:19 +01:00
|
|
|
ti.size = 15.f;
|
2015-03-05 17:36:14 +01:00
|
|
|
renderer->text.renderText(ti);
|
2014-09-16 20:22:43 +02:00
|
|
|
|
2015-03-05 04:37:13 +01:00
|
|
|
/*while( engine->log.size() > 0 && engine->log.front().time + 10.f < engine->gameTime ) {
|
2014-09-16 20:22:43 +02:00
|
|
|
engine->log.pop_front();
|
|
|
|
}
|
|
|
|
|
2015-02-07 23:55:06 +01:00
|
|
|
ti.screenPosition = glm::vec2( 10.f, 500.f );
|
|
|
|
ti.size = 15.f;
|
2014-09-16 20:22:43 +02:00
|
|
|
for(auto it = engine->log.begin(); it != engine->log.end(); ++it) {
|
2015-02-07 23:55:06 +01:00
|
|
|
ti.text = it->message;
|
2014-09-16 20:22:43 +02:00
|
|
|
switch(it->type) {
|
|
|
|
case GameWorld::LogEntry::Error:
|
2015-02-07 23:55:06 +01:00
|
|
|
ti.baseColour = glm::vec3(1.f, 0.f, 0.f);
|
2014-09-16 20:22:43 +02:00
|
|
|
break;
|
|
|
|
case GameWorld::LogEntry::Warning:
|
2015-02-07 23:55:06 +01:00
|
|
|
ti.baseColour = glm::vec3(1.f, 1.f, 0.f);
|
2014-09-16 20:22:43 +02:00
|
|
|
break;
|
|
|
|
default:
|
2015-02-07 23:55:06 +01:00
|
|
|
ti.baseColour = glm::vec3(1.f, 1.f, 1.f);
|
2014-09-16 20:22:43 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Interpolate the color
|
2015-02-07 23:55:06 +01:00
|
|
|
// c.a = (engine->gameTime - it->time > 5.f) ? 255 - (((engine->gameTime - it->time) - 5.f)/5.f) * 255 : 255;
|
|
|
|
// text.setColor(c);
|
2014-09-16 20:22:43 +02:00
|
|
|
|
2015-02-07 23:55:06 +01:00
|
|
|
engine->renderer.text.renderText(ti);
|
|
|
|
ti.screenPosition.y -= ti.size;
|
2015-03-05 04:37:13 +01:00
|
|
|
}*/
|
2014-09-16 20:22:43 +02:00
|
|
|
}
|
|
|
|
|
2015-04-20 03:19:30 +02:00
|
|
|
void RWGame::renderDebugPaths(float time)
|
|
|
|
{
|
|
|
|
btVector3 roadColour(1.f, 0.f, 0.f);
|
|
|
|
btVector3 pedColour(0.f, 0.f, 1.f);
|
|
|
|
|
2015-04-27 16:31:39 +02:00
|
|
|
for( AIGraphNode* n : world->aigraph.nodes )
|
2015-04-20 03:19:30 +02:00
|
|
|
{
|
|
|
|
btVector3 p( n->position.x, n->position.y, n->position.z );
|
|
|
|
auto& col = n->type == AIGraphNode::Pedestrian ? pedColour : roadColour;
|
|
|
|
debug->drawLine( p - btVector3(0.f, 0.f, 1.f), p + btVector3(0.f, 0.f, 1.f), col);
|
|
|
|
debug->drawLine( p - btVector3(1.f, 0.f, 0.f), p + btVector3(1.f, 0.f, 0.f), col);
|
|
|
|
debug->drawLine( p - btVector3(0.f, 1.f, 0.f), p + btVector3(0.f, 1.f, 0.f), col);
|
|
|
|
|
|
|
|
for( AIGraphNode* c : n->connections )
|
|
|
|
{
|
|
|
|
btVector3 f( c->position.x, c->position.y, c->position.z );
|
|
|
|
debug->drawLine( p, f, col);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-06-14 19:08:55 +02:00
|
|
|
// Draw Garage bounds
|
|
|
|
for(size_t g = 0; g < state->garages.size(); ++g) {
|
|
|
|
auto& garage = state->garages[g];
|
|
|
|
btVector3 minColor(1.f, 0.f, 0.f);
|
|
|
|
btVector3 maxColor(0.f, 1.f, 0.f);
|
|
|
|
btVector3 min(garage.min.x,garage.min.y,garage.min.z);
|
|
|
|
btVector3 max(garage.max.x,garage.max.y,garage.max.z);
|
|
|
|
debug->drawLine(min, min + btVector3(0.5f, 0.f, 0.f), minColor);
|
|
|
|
debug->drawLine(min, min + btVector3(0.f, 0.5f, 0.f), minColor);
|
|
|
|
debug->drawLine(min, min + btVector3(0.f, 0.f, 0.5f), minColor);
|
|
|
|
|
|
|
|
debug->drawLine(max, max - btVector3(0.5f, 0.f, 0.f), maxColor);
|
|
|
|
debug->drawLine(max, max - btVector3(0.f, 0.5f, 0.f), maxColor);
|
|
|
|
debug->drawLine(max, max - btVector3(0.f, 0.f, 0.5f), maxColor);
|
|
|
|
}
|
|
|
|
|
2015-04-20 03:19:30 +02:00
|
|
|
debug->flush(renderer);
|
|
|
|
}
|
|
|
|
|
2014-09-16 20:22:43 +02:00
|
|
|
void RWGame::globalKeyEvent(const sf::Event& event)
|
|
|
|
{
|
|
|
|
switch (event.key.code) {
|
|
|
|
case sf::Keyboard::LBracket:
|
2015-04-27 16:31:39 +02:00
|
|
|
state->minute -= 30.f;
|
2014-09-16 20:22:43 +02:00
|
|
|
break;
|
|
|
|
case sf::Keyboard::RBracket:
|
2015-04-27 16:31:39 +02:00
|
|
|
state->minute += 30.f;
|
2014-09-16 20:22:43 +02:00
|
|
|
break;
|
2014-12-13 01:59:09 +01:00
|
|
|
case sf::Keyboard::Num9:
|
|
|
|
timescale *= 0.5f;
|
|
|
|
break;
|
|
|
|
case sf::Keyboard::Num0:
|
|
|
|
timescale *= 2.0f;
|
|
|
|
break;
|
2015-02-16 01:39:19 +01:00
|
|
|
case sf::Keyboard::F1:
|
|
|
|
showDebugStats = ! showDebugStats;
|
|
|
|
break;
|
2015-04-20 03:19:30 +02:00
|
|
|
case sf::Keyboard::F2:
|
|
|
|
showDebugPaths = ! showDebugPaths;
|
|
|
|
break;
|
2015-04-27 05:12:58 +02:00
|
|
|
case sf::Keyboard::F3:
|
|
|
|
showDebugPhysics = ! showDebugPhysics;
|
|
|
|
break;
|
2015-02-16 01:44:30 +01:00
|
|
|
default: break;
|
2014-09-16 20:22:43 +02:00
|
|
|
}
|
|
|
|
}
|