2003-08-15 18:00:22 +00:00
|
|
|
// Residual - Virtual machine to run LucasArts' 3D adventure games
|
2004-02-24 21:09:53 +00:00
|
|
|
// Copyright (C) 2003-2004 The ScummVM-Residual Team (www.scummvm.org)
|
2003-08-15 18:00:22 +00:00
|
|
|
//
|
|
|
|
// This library is free software; you can redistribute it and/or
|
|
|
|
// modify it under the terms of the GNU Lesser General Public
|
|
|
|
// License as published by the Free Software Foundation; either
|
|
|
|
// version 2.1 of the License, or (at your option) any later version.
|
|
|
|
//
|
|
|
|
// This library is distributed in the hope that it will be useful,
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
// Lesser General Public License for more details.
|
|
|
|
//
|
|
|
|
// You should have received a copy of the GNU Lesser General Public
|
|
|
|
// License along with this library; if not, write to the Free Software
|
|
|
|
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
|
|
|
|
2003-08-24 17:56:03 +00:00
|
|
|
#include "stdafx.h"
|
2003-08-15 19:41:26 +00:00
|
|
|
#include "engine.h"
|
|
|
|
#include "scene.h"
|
|
|
|
#include "lua.h"
|
|
|
|
#include "colormap.h"
|
|
|
|
#include "actor.h"
|
2003-08-21 15:42:36 +00:00
|
|
|
#include "textobject.h"
|
2003-08-15 18:00:22 +00:00
|
|
|
#include <SDL.h>
|
|
|
|
#include <SDL_timer.h>
|
2003-08-15 23:31:28 +00:00
|
|
|
#include <assert.h>
|
2003-08-30 17:58:33 +00:00
|
|
|
#include "screen.h"
|
2004-02-24 07:04:50 +00:00
|
|
|
#include "smush.h"
|
2004-01-23 11:10:59 +00:00
|
|
|
#include "driver_gl.h"
|
2003-08-15 18:00:22 +00:00
|
|
|
|
|
|
|
Engine *Engine::instance_ = NULL;
|
|
|
|
|
|
|
|
Engine::Engine() :
|
2004-02-24 21:09:53 +00:00
|
|
|
currScene_(NULL), selectedActor_(NULL) {
|
|
|
|
for (int i = 0; i < SDLK_EXTRA_LAST; i++)
|
|
|
|
controlsEnabled_[i] = false;
|
2004-03-20 08:22:51 +00:00
|
|
|
_speechMode = 2;
|
2003-08-15 18:00:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void Engine::mainLoop() {
|
2004-03-03 21:43:34 +00:00
|
|
|
movieTime_ = 0;
|
2004-02-24 21:09:53 +00:00
|
|
|
frameTime_ = 0;
|
|
|
|
frameStart_ = SDL_GetTicks();
|
2004-04-15 20:20:04 +00:00
|
|
|
unsigned int frameCounter = 0;
|
|
|
|
unsigned int timeAccum = 0;
|
|
|
|
char fps[8] = "";
|
2004-02-24 21:09:53 +00:00
|
|
|
|
|
|
|
for (;;) {
|
|
|
|
// Process events
|
|
|
|
SDL_Event event;
|
|
|
|
while (SDL_PollEvent(&event)) {
|
|
|
|
if (event.type == SDL_KEYDOWN && controlsEnabled_[event.key.keysym.sym]) {
|
|
|
|
lua_beginblock();
|
|
|
|
lua_Object handler = getEventHandler("buttonHandler");
|
|
|
|
if (handler != LUA_NOOBJECT) {
|
|
|
|
lua_pushnumber(event.key.keysym.sym);
|
|
|
|
lua_pushnumber(1);
|
|
|
|
lua_pushnil();
|
|
|
|
lua_callfunction(handler);
|
|
|
|
}
|
|
|
|
lua_endblock();
|
|
|
|
}
|
|
|
|
if (event.type == SDL_KEYUP && controlsEnabled_[event.key.keysym.sym]) {
|
|
|
|
lua_beginblock();
|
|
|
|
lua_Object handler = getEventHandler("buttonHandler");
|
|
|
|
if (handler != LUA_NOOBJECT) {
|
|
|
|
lua_pushnumber(event.key.keysym.sym);
|
|
|
|
lua_pushnil();
|
|
|
|
lua_pushnil();
|
|
|
|
lua_callfunction(handler);
|
|
|
|
}
|
|
|
|
lua_endblock();
|
|
|
|
}
|
|
|
|
if (event.type == SDL_QUIT) {
|
|
|
|
lua_beginblock();
|
|
|
|
lua_Object handler = getEventHandler("exitHandler");
|
|
|
|
if (handler != LUA_NOOBJECT)
|
|
|
|
lua_callfunction(handler);
|
|
|
|
lua_endblock();
|
|
|
|
}
|
|
|
|
if (event.type == SDL_KEYDOWN) {
|
|
|
|
if (event.key.keysym.sym == SDLK_q)
|
|
|
|
return;
|
2004-02-29 18:38:26 +00:00
|
|
|
}
|
2004-02-24 21:09:53 +00:00
|
|
|
}
|
|
|
|
|
2004-02-29 18:38:26 +00:00
|
|
|
// Run asynchronous tasks
|
|
|
|
lua_runtasks();
|
|
|
|
|
2004-03-20 05:47:21 +00:00
|
|
|
if (_mode == ENGINE_MODE_SMUSH) {
|
|
|
|
if (g_smush->isPlaying()) {
|
|
|
|
movieTime_ = g_smush->getMovieTime();
|
|
|
|
if (g_smush->isUpdateNeeded()) {
|
|
|
|
g_driver->prepareSmushFrame(g_smush->getWidth(), g_smush->getHeight(), g_smush->getDstPtr());
|
|
|
|
g_smush->clearUpdateNeeded();
|
|
|
|
}
|
2004-03-26 00:48:07 +00:00
|
|
|
if (g_smush->getFrame() > 0)
|
|
|
|
g_driver->drawSmushFrame(g_smush->getX(), g_smush->getY());
|
2004-03-20 05:47:21 +00:00
|
|
|
}
|
|
|
|
g_driver->flipBuffer();
|
|
|
|
} else if (_mode == ENGINE_MODE_NORMAL) {
|
2004-03-24 12:20:46 +00:00
|
|
|
if (SCREENBLOCKS_GLOBAL)
|
2004-03-20 05:47:21 +00:00
|
|
|
screenBlocksReset();
|
2004-02-29 18:38:26 +00:00
|
|
|
|
2004-03-20 05:47:21 +00:00
|
|
|
// Update actor costumes
|
|
|
|
for (actor_list_type::iterator i = actors_.begin(); i != actors_.end(); i++) {
|
|
|
|
Actor *a = *i;
|
|
|
|
assert(currScene_);
|
|
|
|
if (a->inSet(currScene_->name()) && a->visible())
|
|
|
|
a->update();
|
|
|
|
}
|
2004-02-29 18:38:26 +00:00
|
|
|
|
2004-03-20 05:47:21 +00:00
|
|
|
g_driver->clearScreen();
|
2004-02-25 05:52:47 +00:00
|
|
|
|
2004-03-24 12:20:46 +00:00
|
|
|
if (SCREENBLOCKS_GLOBAL)
|
2004-03-20 05:47:21 +00:00
|
|
|
screenBlocksBlitDirtyBlocks();
|
2004-02-25 05:52:47 +00:00
|
|
|
|
2004-03-22 11:23:37 +00:00
|
|
|
if (currScene_ != NULL) {
|
2004-02-29 18:38:26 +00:00
|
|
|
currScene_->drawBackground();
|
2004-03-22 11:23:37 +00:00
|
|
|
currScene_->drawBitmaps(ObjectState::OBJSTATE_UNDERLAY);
|
|
|
|
currScene_->drawBitmaps(ObjectState::OBJSTATE_STATE);
|
|
|
|
}
|
2004-02-29 18:38:26 +00:00
|
|
|
|
2004-03-20 05:47:21 +00:00
|
|
|
if (g_smush->isPlaying()) {
|
|
|
|
movieTime_ = g_smush->getMovieTime();
|
|
|
|
if (g_smush->isUpdateNeeded()) {
|
|
|
|
g_driver->prepareSmushFrame(g_smush->getWidth(), g_smush->getHeight(), g_smush->getDstPtr());
|
|
|
|
g_smush->clearUpdateNeeded();
|
|
|
|
}
|
2004-03-26 00:48:07 +00:00
|
|
|
if (g_smush->getFrame() > 0)
|
|
|
|
g_driver->drawSmushFrame(g_smush->getX(), g_smush->getY());
|
2004-02-29 18:38:26 +00:00
|
|
|
}
|
|
|
|
|
2004-03-20 05:47:21 +00:00
|
|
|
glMatrixMode(GL_MODELVIEW);
|
2004-02-24 07:04:50 +00:00
|
|
|
|
2004-03-20 05:47:21 +00:00
|
|
|
glEnable(GL_DEPTH_TEST);
|
|
|
|
if (currScene_ != NULL)
|
|
|
|
currScene_->setupCamera();
|
|
|
|
|
|
|
|
// Draw actors
|
|
|
|
if (!g_smush->isPlaying()) {
|
|
|
|
glEnable(GL_TEXTURE_2D);
|
|
|
|
for (actor_list_type::iterator i = actors_.begin(); i != actors_.end(); i++) {
|
|
|
|
Actor *a = *i;
|
|
|
|
if (a->inSet(currScene_->name()) && a->visible())
|
|
|
|
a->draw();
|
|
|
|
}
|
|
|
|
glDisable(GL_TEXTURE_2D);
|
|
|
|
//screenBlocksDrawDebug();
|
|
|
|
}
|
2004-02-25 05:52:47 +00:00
|
|
|
|
2004-03-20 05:47:21 +00:00
|
|
|
// Draw text
|
|
|
|
for (text_list_type::iterator i = textObjects_.begin(); i != textObjects_.end(); i++) {
|
|
|
|
(*i)->draw();
|
2004-02-29 18:38:26 +00:00
|
|
|
}
|
2003-08-30 17:58:33 +00:00
|
|
|
|
2004-04-15 20:39:09 +00:00
|
|
|
if (SHOWFPS_GLOBAL)
|
2004-04-15 20:20:04 +00:00
|
|
|
g_driver->drawEmergString(550, 25, fps, Color(255, 255, 255));
|
|
|
|
|
2004-03-22 11:23:37 +00:00
|
|
|
currScene_->drawBitmaps(ObjectState::OBJSTATE_OVERLAY);
|
|
|
|
|
2004-03-20 05:47:21 +00:00
|
|
|
g_driver->flipBuffer();
|
2004-02-24 21:09:53 +00:00
|
|
|
}
|
|
|
|
|
2004-03-03 20:52:58 +00:00
|
|
|
// don't kill CPU
|
|
|
|
SDL_Delay(1);
|
|
|
|
|
2004-02-24 21:09:53 +00:00
|
|
|
// Update timing information
|
|
|
|
unsigned newStart = SDL_GetTicks();
|
|
|
|
frameTime_ = newStart - frameStart_;
|
|
|
|
frameStart_ = newStart;
|
|
|
|
|
|
|
|
lua_beginblock();
|
|
|
|
set_frameTime(frameTime_);
|
|
|
|
lua_endblock();
|
2004-03-03 21:43:34 +00:00
|
|
|
|
|
|
|
lua_beginblock();
|
|
|
|
set_movieTime(movieTime_);
|
|
|
|
lua_endblock();
|
2004-04-15 20:39:09 +00:00
|
|
|
|
|
|
|
if (SHOWFPS_GLOBAL) {
|
|
|
|
frameCounter++;
|
|
|
|
timeAccum += frameTime_;
|
|
|
|
if (timeAccum > 1000) {
|
|
|
|
sprintf(fps, "%7.2f", (double)(frameCounter * 1000) / (double)timeAccum );
|
|
|
|
frameCounter = 0;
|
|
|
|
timeAccum = 0;
|
|
|
|
}
|
|
|
|
}
|
2004-02-24 21:09:53 +00:00
|
|
|
}
|
2003-08-15 18:00:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void Engine::setScene(const char *name) {
|
2004-02-24 21:09:53 +00:00
|
|
|
Block *b = ResourceLoader::instance()->getFileBlock(name);
|
|
|
|
if (b == NULL)
|
|
|
|
warning("Could not find scene file %s\n", name);
|
|
|
|
delete currScene_;
|
|
|
|
currScene_ = new Scene(name, b->data(), b->len());
|
|
|
|
delete b;
|
2003-08-15 18:00:22 +00:00
|
|
|
}
|