1
0
mirror of https://github.com/rwengine/openrw.git synced 2024-11-07 11:22:45 +01:00
openrw/rwgame/main.cpp

514 lines
12 KiB
C++
Raw Normal View History

#define GLEW_STATIC
#include <GL/glew.h>
2013-12-20 17:02:46 +01:00
#include <engine/GameWorld.hpp>
#include <loaders/LoaderDFF.hpp>
#include <render/DebugDraw.hpp>
2013-12-20 16:45:41 +01:00
#include <render/Model.hpp>
2014-06-06 16:22:26 +02:00
#include <objects/VehicleObject.hpp>
#include <objects/CharacterObject.hpp>
#include <objects/InstanceObject.hpp>
2014-06-06 18:04:00 +02:00
#include <ai/CharacterController.hpp>
#include <script/ScriptMachine.hpp>
#include <glm/glm.hpp>
#include <glm/gtc/matrix_transform.hpp>
#include <glm/gtc/type_ptr.hpp>
#include <data/CutsceneData.hpp>
2014-06-06 13:18:32 +02:00
#include "loadingstate.hpp"
#include <SFML/Graphics.hpp>
#include <memory>
2013-07-02 12:48:01 +02:00
#include <sstream>
2013-12-27 23:57:11 +01:00
#include <iomanip>
2013-07-04 01:35:03 +02:00
#include <getopt.h>
2014-05-26 06:34:49 +02:00
#include "game.hpp"
2014-05-25 23:47:10 +02:00
#define ENV_GAME_PATH_NAME ("OPENRW_GAME_PATH")
#define GAME_TIMESTEP (1.f/20.f)
2014-05-25 23:47:10 +02:00
constexpr int WIDTH = 800,
HEIGHT = 600;
2013-07-05 20:50:04 +02:00
sf::RenderWindow window;
2013-12-20 17:02:46 +01:00
GameWorld* gta = nullptr;
2014-06-06 16:22:26 +02:00
CharacterObject* player = nullptr;
DebugDraw* debugDrawer = nullptr;
bool inFocus = true;
2013-07-30 17:59:44 +02:00
int debugMode = 0;
float accum = 0.f;
2013-07-05 20:50:04 +02:00
sf::Font font;
glm::vec3 viewPosition { -260.f, -151.5f, 9.f }, lastViewPosition;
glm::vec2 viewAngles { -0.3f, 0.05f }, lastViewAngles;
2014-05-26 06:34:49 +02:00
void setViewParameters(const glm::vec3 &center, const glm::vec2 &angles)
{
lastViewPosition = viewPosition;
lastViewAngles = viewAngles;
2014-05-26 06:34:49 +02:00
viewPosition = center;
viewAngles = angles;
}
2014-06-08 23:18:03 +02:00
glm::vec3& getViewPosition()
{
return viewPosition;
}
glm::vec2& getViewAngles()
{
return viewAngles;
}
2014-05-26 06:34:49 +02:00
sf::Window& getWindow()
{
return window;
}
GameWorld* getWorld()
{
return gta;
}
sf::Font& getFont()
{
return font;
}
2014-06-06 16:22:26 +02:00
void setPlayerCharacter(CharacterObject *playerCharacter)
{
player = playerCharacter;
}
2014-06-06 16:22:26 +02:00
CharacterObject* getPlayerCharacter()
{
return player;
}
void skipTime(float time)
{
accum += time;
}
bool hitWorldRay(glm::vec3 &hit, glm::vec3 &normal, GameObject** object)
2013-12-05 13:45:20 +01:00
{
glm::mat4 view;
view = glm::rotate(view, -90.f, glm::vec3(1, 0, 0));
2014-05-26 06:34:49 +02:00
view = glm::rotate(view, viewAngles.y, glm::vec3(1, 0, 0));
view = glm::rotate(view, viewAngles.x, glm::vec3(0, 0, 1));
2013-12-05 13:45:20 +01:00
glm::vec3 dir = glm::inverse(glm::mat3(view)) * glm::vec3(0.f, 0.f, 1.f) * -50.f;
2014-05-26 06:34:49 +02:00
auto from = btVector3(viewPosition.x, viewPosition.y, viewPosition.z);
auto to = btVector3(viewPosition.x+dir.x, viewPosition.y+dir.y, viewPosition.z+dir.z);
2013-12-05 13:45:20 +01:00
btCollisionWorld::ClosestRayResultCallback ray(from, to);
gta->dynamicsWorld->rayTest(from, to, ray);
if( ray.hasHit() )
{
hit = glm::vec3(ray.m_hitPointWorld.x(), ray.m_hitPointWorld.y(),
ray.m_hitPointWorld.z());
normal = glm::vec3(ray.m_hitNormalWorld.x(), ray.m_hitNormalWorld.y(),
ray.m_hitNormalWorld.z());
2013-12-07 00:19:17 +01:00
if(object) {
*object = static_cast<GameObject*>(ray.m_collisionObject->getUserPointer());
2013-12-07 00:19:17 +01:00
}
2013-12-05 13:45:20 +01:00
return true;
}
return false;
}
2013-09-27 17:49:10 +02:00
bool hitWorldRay(const glm::vec3 &start, const glm::vec3 &direction, glm::vec3 &hit, glm::vec3 &normal, GameObject **object)
{
auto from = btVector3(start.x, start.y, start.z);
auto to = btVector3(start.x+direction.x, start.y+direction.y, start.z+direction.z);
btCollisionWorld::ClosestRayResultCallback ray(from, to);
gta->dynamicsWorld->rayTest(from, to, ray);
if( ray.hasHit() )
{
hit = glm::vec3(ray.m_hitPointWorld.x(), ray.m_hitPointWorld.y(),
ray.m_hitPointWorld.z());
normal = glm::vec3(ray.m_hitNormalWorld.x(), ray.m_hitNormalWorld.y(),
ray.m_hitNormalWorld.z());
if(object) {
*object = static_cast<GameObject*>(ray.m_collisionObject->getUserPointer());
}
return true;
}
return false;
}
2013-09-27 17:49:10 +02:00
void handleGlobalEvent(sf::Event &event)
{
switch (event.type) {
case sf::Event::KeyPressed:
2013-09-27 17:49:10 +02:00
break;
case sf::Event::GainedFocus:
inFocus = true;
break;
case sf::Event::LostFocus:
inFocus = false;
break;
default: break;
}
}
void handleInputEvent(sf::Event &event)
{
switch(event.type) {
case sf::Event::KeyPressed:
switch (event.key.code) {
case sf::Keyboard::P:
2013-10-02 14:49:45 +02:00
debugMode+=1;
2013-07-30 17:59:44 +02:00
while(debugMode > 2) debugMode -= 3;
break;
2013-10-02 14:49:45 +02:00
default: break;
}
break;
case sf::Event::KeyReleased:
switch(event.key.code) {
2013-10-02 14:49:45 +02:00
default: break;
}
break;
2013-10-02 14:49:45 +02:00
default: break;
2013-09-27 17:49:10 +02:00
}
}
void handleCommandEvent(sf::Event &event)
{
switch(event.type) {
case sf::Event::KeyPressed:
2013-12-05 13:45:20 +01:00
switch (event.key.code) {
2013-12-28 01:51:15 +01:00
case sf::Keyboard::LBracket:
gta->state.minute -= 30.f;
2013-12-28 01:51:15 +01:00
break;
case sf::Keyboard::RBracket:
gta->state.minute += 30.f;
2013-12-28 01:51:15 +01:00
break;
2013-12-05 13:45:20 +01:00
break;
2013-12-27 00:18:55 +01:00
default: break;
2013-12-05 14:46:40 +01:00
}
2013-12-27 00:18:55 +01:00
default: break;
}
}
void init(std::string gtapath, bool loadWorld)
{
2013-07-02 08:06:03 +02:00
// GTA GET
2013-12-20 17:02:46 +01:00
gta = new GameWorld(gtapath);
2013-07-02 08:06:03 +02:00
// This is harcoded in GTA III for some reason
gta->gameData.loadIMG("/models/gta3");
2014-06-01 18:41:09 +02:00
gta->gameData.loadIMG("/models/txd");
gta->gameData.loadIMG("/anim/cuts");
2013-07-02 08:06:03 +02:00
gta->load();
// Load dynamic object data
gta->gameData.loadDynamicObjects(gtapath + "/data/object.dat");
gta->gameData.loadGXT("english.gxt");
gta->gameTime = 0.f;
debugDrawer = new DebugDraw;
debugDrawer->setShaderProgram(gta->renderer.worldProgram);
debugDrawer->setDebugMode(btIDebugDraw::DBG_DrawWireframe);
gta->dynamicsWorld->setDebugDrawer(debugDrawer);
2013-09-11 13:10:42 +02:00
setViewParameters( { -260.f, -151.5f, 9.f }, { -0.3f, 0.05f } );
2013-09-11 13:10:42 +02:00
std::cout << "Loaded "
<< gta->gameData.models.size() << " models, "
2013-09-11 20:23:31 +02:00
<< gta->gameData.textures.size() << " textures" << std::endl;
}
void update(float dt)
{
static float clockAccumulator = 0.f;
if (inFocus) {
2013-07-05 03:15:29 +02:00
gta->gameTime += dt;
clockAccumulator += dt;
while( clockAccumulator >= 1.f ) {
gta->state.minute ++;
if( gta->state.minute >= 60 ) {
gta->state.minute = 0;
gta->state.hour ++;
if( gta->state.hour >= 24 ) {
gta->state.hour = 0;
}
}
clockAccumulator -= 1.f;
}
for( GameObject* object : gta->objects ) {
object->_updateLastTransform();
object->tick(dt);
}
2013-09-15 03:42:35 +02:00
gta->destroyQueuedObjects();
2014-07-30 16:48:51 +02:00
gta->state.texts.clear();
2013-10-02 02:27:48 +02:00
gta->dynamicsWorld->stepSimulation(dt, 2, dt);
if( getWorld()->script ) {
try {
getWorld()->script->execute(dt);
}
catch( SCMException& ex ) {
std::cerr << ex.what() << std::endl;
getWorld()->logError( ex.what() );
throw;
}
}
}
}
void render(float alpha)
{
2014-06-06 13:18:32 +02:00
gta->_work->update();
float qpi = glm::half_pi<float>();
glm::mat4 view;
/// @todo this probably doesn't belong in main.cpp
if( gta->state.currentCutscene == nullptr || gta->state.cutsceneStartTime <= 0.f ) {
view = glm::translate(view, glm::mix(lastViewPosition, viewPosition, alpha));
auto va = glm::mix(lastViewAngles, viewAngles, alpha);
view = glm::rotate(view, va.x, glm::vec3(0, 0, 1));
view = glm::rotate(view, va.y - qpi, glm::vec3(1, 0, 0));
view = glm::inverse(view);
gta->renderer.camera.worldPos = viewPosition;
}
else {
auto cutscene = gta->state.currentCutscene;
float cutsceneTime = std::min(gta->gameTime - gta->state.cutsceneStartTime,
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);
gta->renderer.camera.frustum.fov = glm::radians(-zoom);
float tilt = cutscene->tracks.getRotationAt(cutsceneTime);
auto d = glm::normalize(targetPos-cameraPos);
auto qtilt = glm::rotate(glm::quat(), glm::radians(tilt), d);
cameraPos += cutscene->meta.sceneOffset;
targetPos += cutscene->meta.sceneOffset;
2014-08-01 23:22:49 +02:00
// Update the internal values
lastViewPosition = viewPosition = cameraPos;
view = glm::lookAt(cameraPos, targetPos, qtilt * glm::vec3(0.f, 0.f, -1.f));
gta->renderer.camera.worldPos = cameraPos;
}
gta->renderer.camera.frustum.view = view;
// Update aspect ratio..
gta->renderer.camera.frustum.aspectRatio = window.getSize().x / (float) window.getSize().y;
2013-07-02 10:58:01 +02:00
glEnable(GL_DEPTH_TEST);
2013-08-18 21:31:37 +02:00
glClear(GL_DEPTH_BUFFER_BIT|GL_COLOR_BUFFER_BIT);
//glEnable(GL_CULL_FACE);
gta->renderer.renderWorld(alpha);
2013-07-30 17:59:44 +02:00
switch( debugMode ) {
case 0: break;
2013-07-30 17:59:44 +02:00
case 1: {
2013-07-30 17:59:44 +02:00
glUseProgram(gta->renderer.worldProgram);
gta->renderer.uploadUBO<ObjectUniformData>(
gta->renderer.uboObject, {
glm::mat4(),
glm::vec4(1.f),
1.f, 1.f
});
2014-06-17 22:46:54 +02:00
gta->renderer.renderPaths();
2013-07-30 17:59:44 +02:00
break;
}
case 2: {
glUseProgram(gta->renderer.worldProgram);
gta->renderer.uploadUBO<ObjectUniformData>(
gta->renderer.uboObject, {
glm::mat4(),
glm::vec4(1.f),
1.f, 1.f
});
2014-06-17 22:46:54 +02:00
gta->dynamicsWorld->debugDrawWorld();
2013-07-30 17:59:44 +02:00
debugDrawer->drawAllLines();
break;
}
}
2013-07-05 20:50:04 +02:00
window.resetGLStates();
2013-07-05 20:50:04 +02:00
std::stringstream ss;
2013-12-27 23:57:11 +01:00
ss << std::setfill('0') << "Time: " << std::setw(2) << gta->getHour()
2014-08-01 23:22:49 +02:00
<< ":" << std::setw(2) << gta->getMinute() << " (" << gta->gameTime << "s)\n";
ss << "View: " << viewPosition.x << " " << viewPosition.y << " " << viewPosition.z << "\n";
ss << "Drawn " << gta->renderer.rendered << " / " << gta->renderer.culled << " Culled\n";
2014-06-02 23:18:13 +02:00
if( player ) {
ss << "Activity: ";
if( player->controller->getCurrentActivity() ) {
ss << player->controller->getCurrentActivity()->name();
}
else {
ss << "Idle";
}
ss << std::endl;
}
2013-09-27 17:49:10 +02:00
2014-08-01 23:22:49 +02:00
sf::Text text(ss.str(), font, 14);
2013-12-07 00:19:17 +01:00
text.setPosition(10, 10);
window.draw(text);
while( gta->log.size() > 0 && gta->log.front().time + 10.f < gta->gameTime ) {
gta->log.pop_front();
}
2013-12-27 00:26:45 +01:00
sf::Vector2f tpos(10.f, window.getSize().y - 30.f);
2014-08-01 23:22:49 +02:00
text.setCharacterSize(14);
for(auto it = gta->log.begin(); it != gta->log.end(); ++it) {
text.setString(it->message);
switch(it->type) {
2013-12-20 17:02:46 +01:00
case GameWorld::LogEntry::Error:
text.setColor(sf::Color::Red);
break;
2013-12-20 17:02:46 +01:00
case GameWorld::LogEntry::Warning:
text.setColor(sf::Color::Yellow);
break;
default:
text.setColor(sf::Color::White);
break;
}
// Interpolate the color
auto c = text.getColor();
c.a = (gta->gameTime - it->time > 5.f) ? 255 - (((gta->gameTime - it->time) - 5.f)/5.f) * 255 : 255;
text.setColor(c);
text.setPosition(tpos);
window.draw(text);
2013-12-27 00:26:45 +01:00
tpos.y -= text.getLocalBounds().height;
}
/// @todo this should be done by GameRenderer? but it doesn't have any font support yet
if( gta->gameTime < gta->state.osTextStart + gta->state.osTextTime ) {
sf::Text messageText(gta->state.osTextString, font, 15);
auto sz = window.getSize();
auto b = messageText.getLocalBounds();
float lowerBar = sz.y - sz.y * 0.1f;
messageText.setPosition(sz.x / 2.f - std::round(b.width / 2.f), lowerBar - std::round(b.height / 2.f));
window.draw(messageText);
}
2014-07-30 16:48:51 +02:00
for(auto& t : gta->state.texts) {
sf::Text messageText(t.text, font, 15);
glm::vec2 scpos(t.position.x, t.position.y);
auto s = window.getSize();
scpos /= glm::vec2(640.f, 480.f);
scpos *= glm::vec2(s.x, s.y);
messageText.setPosition(scpos.x, scpos.y);
window.draw(messageText);
}
}
2014-05-25 23:47:10 +02:00
std::string getGamePath()
{
auto v = getenv(ENV_GAME_PATH_NAME);
return v ? v : "";
}
int main(int argc, char *argv[])
{
if(! font.loadFromFile(getGamePath() + "/DejaVuSansMono.ttf")) {
std::cerr << "Failed to load font" << std::endl;
2013-07-05 20:50:04 +02:00
}
glewExperimental = GL_TRUE;
glewInit();
2014-05-25 23:47:10 +02:00
bool loadWorld = true;
2013-07-04 01:35:03 +02:00
size_t w = WIDTH, h = HEIGHT;
int c;
while( (c = getopt(argc, argv, "w:h:l")) != -1) {
2013-07-04 01:35:03 +02:00
switch(c) {
case 'w':
w = atoi(optarg);
break;
case 'h':
h = atoi(optarg);
break;
case 'l':
loadWorld = false;
break;
2013-07-04 01:35:03 +02:00
}
}
2013-08-18 21:31:37 +02:00
sf::ContextSettings cs;
cs.depthBits = 32;
2013-12-27 00:18:55 +01:00
window.create(sf::VideoMode(w, h), "", sf::Style::Default, cs);
window.setVerticalSyncEnabled(true);
2013-12-24 20:47:04 +01:00
window.setMouseCursorVisible(false);
2014-05-25 23:47:10 +02:00
init(getGamePath(), loadWorld);
sf::Clock clock;
2014-05-26 06:34:49 +02:00
2014-06-06 13:18:32 +02:00
StateManager::get().enter(new LoadingState);
float ts = GAME_TIMESTEP;
float timescale = 1.f;
2013-08-15 18:53:11 +02:00
2013-12-27 00:18:55 +01:00
// Loop until the window is closed or we run out of state.
while (window.isOpen() && StateManager::get().states.size()) {
sf::Event event;
while (window.pollEvent(event)) {
2013-09-27 17:49:10 +02:00
handleGlobalEvent(event);
2013-12-05 13:45:20 +01:00
handleCommandEvent(event);
handleInputEvent(event);
2013-12-25 20:54:22 +01:00
2013-12-27 00:18:55 +01:00
StateManager::get().states.back()->handleEvent(event);
}
2013-09-15 03:42:35 +02:00
accum += clock.restart().asSeconds() * timescale;
2013-08-15 18:53:11 +02:00
while ( accum >= ts ) {
2014-05-26 06:34:49 +02:00
StateManager::get().tick(ts);
2013-08-15 18:53:11 +02:00
update(ts);
accum -= ts;
}
float alpha = accum / ts;
render(alpha);
2013-12-27 00:18:55 +01:00
StateManager::get().draw(window);
window.display();
2013-08-15 18:53:11 +02:00
}
delete gta;
return 0;
}