scummvm/main.cpp

287 lines
6.6 KiB
C++
Raw Normal View History

2003-08-15 18:00:22 +00:00
// Residual - Virtual machine to run LucasArts' 3D adventure games
2005-01-01 10:23:18 +00:00
// Copyright (C) 2003-2005 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
#include "stdafx.h"
2005-01-01 12:27:57 +00:00
#include "debug.h"
#include "bitmap.h"
#include "resource.h"
#include "lua.h"
#include "registry.h"
#include "localize.h"
#include "engine.h"
2003-12-12 21:38:12 +00:00
#include "timer.h"
#include "smush.h"
2005-01-01 12:27:57 +00:00
#include "driver_gl.h"
#include "driver_tinygl.h"
2005-01-01 12:27:57 +00:00
#include "mixer/mixer.h"
2004-04-20 07:40:14 +00:00
2005-01-01 12:27:57 +00:00
#include "imuse/imuse.h"
2005-01-01 12:27:57 +00:00
#include <SDL.h>
#include <SDL_video.h>
// Hacky global toggles for experimental/debug code
bool ZBUFFER_GLOBAL, SHOWFPS_GLOBAL, TINYGL_GLOBAL;
enDebugLevels debugLevel = DEBUG_NONE;
#ifdef __MINGW32__
int PASCAL WinMain(HINSTANCE /*hInst*/, HINSTANCE /*hPrevInst*/, LPSTR /*lpCmdLine*/, int /*iShowCmd*/) {
return main(0, NULL);
}
#endif
2004-12-31 22:05:26 +00:00
static bool g_lua_initialized = false;
Driver *g_driver = NULL;
static bool parseBoolStr(const char *val) {
if (val == NULL || val[0] == 0)
return false;
2004-04-20 07:40:14 +00:00
switch (val[0]) {
case 'y': case 'Y': // yes
case 't': case 'T': // true
case '1':
return true;
case 'n': case 'N': // no
case 'f': case 'F': // false
case '0':
return false;
case 'o':
switch (val[1]) {
case 'n': case 'N': // on
return true;
case 'f': case 'F': // off
return false;
}
}
2004-04-20 07:40:14 +00:00
error("Unrecognized boolean value %s\n", val);
2004-04-20 07:40:14 +00:00
return false;
}
void quit();
#ifdef _MSC_VER
#undef main
#endif
int main(int argc, char *argv[]) {
int i;
2004-12-31 22:05:26 +00:00
g_registry = new Registry();
// Parse command line
ZBUFFER_GLOBAL = parseBoolStr(g_registry->get("zbuffer", "TRUE"));
SHOWFPS_GLOBAL = parseBoolStr(g_registry->get("fps", "FALSE"));
TINYGL_GLOBAL = parseBoolStr(g_registry->get("soft", "FALSE"));
bool fullscreen = parseBoolStr(g_registry->get("fullscreen", "FALSE"));
2004-12-09 23:55:43 +00:00
for (i = 1; i < argc; i++) {
if (strcmp(argv[i], "-zbuffer") == 0)
ZBUFFER_GLOBAL = true;
else if (strcmp(argv[i], "-nozbuffer") == 0)
ZBUFFER_GLOBAL = false;
2004-04-15 20:20:04 +00:00
else if (strcmp(argv[i], "-fps") == 0)
SHOWFPS_GLOBAL = true;
else if (strcmp(argv[i], "-nofps") == 0)
SHOWFPS_GLOBAL = false;
2005-03-28 01:54:21 +00:00
else if (strcmp(argv[i], "-fullscreen") == 0)
fullscreen = true;
else if (strcmp(argv[i], "-nofullscreen") == 0)
fullscreen = false;
else if (strcmp(argv[i], "-soft") == 0)
TINYGL_GLOBAL = true;
else if (strcmp(argv[i], "-nosoft") == 0)
TINYGL_GLOBAL = false;
else if (strncmp(argv[i], "-debug=", 7) == 0) {
bool numeric = true;
char debugtxt[20];
unsigned int j;
int level;
sscanf(argv[i], "%*[^=]%*1s%s", debugtxt);
for(j=0;j<strlen(debugtxt);j++) {
if(!isdigit(debugtxt[j]))
numeric = false;
}
if(numeric) {
sscanf(debugtxt, "%d", &level);
if(level < 0 || level > DEBUG_ALL)
goto needshelp;
} else {
level = -1;
for(j=0;j<=DEBUG_ALL;j++)
{
if(!strcasecmp(debugtxt, debug_levels[j])) {
level = j;
break;
}
}
if(level == -1)
goto needshelp;
}
debugLevel = (enDebugLevels) level;
printf("Debug level set to: %s\n", debug_levels[debugLevel]);
} else {
int j;
needshelp:
printf("Residual CVS Version\n");
printf("--------------------\n");
printf("Recognised options:\n");
printf("\t-[no]zbuffer\t\tEnable/disable ZBuffers (Very slow on older cards)\n");
2004-04-16 10:56:27 +00:00
printf("\t-[no]fps\t\tEnable/disable fps display in upper right corner\n");
printf("\t-[no]fullscreen\t\tEnable/disable fullscreen mode at startup\n");
printf("\t-[no]soft\t\tEnable/disable software renderer\n");
printf("\t-debug=[level]\t\tSet debug to [level], valid levels:\n");
for(j=0;j<=DEBUG_ALL;j++)
printf("\t\t%-8s (%d): %s.\n", debug_levels[j], j, debug_descriptions[j]);
exit(-1);
}
}
if (SDL_Init(SDL_INIT_EVERYTHING) < 0)
return 1;
atexit(quit);
g_engine = new Engine();
g_resourceloader = new ResourceLoader();
g_localizer = new Localizer();
g_mixer = new SoundMixer();
g_mixer->setVolume(255);
g_timer = new Timer();
g_smush = new Smush();
if (TINYGL_GLOBAL)
2005-03-28 01:54:21 +00:00
g_driver = new DriverTinyGL(640, 480, 16, fullscreen);
else
2005-03-28 01:54:21 +00:00
g_driver = new DriverGL(640, 480, 24, fullscreen);
g_imuse = new Imuse(20);
Bitmap *splash_bm = g_resourceloader->loadBitmap("splash.bm");
SDL_Event event;
// For some reason we don't get the SDL_VIDEOEXPOSE event on OSX, so just don't wait for it.
#ifndef MACOSX
while (SDL_PollEvent(&event)) {
if (event.type == SDL_VIDEOEXPOSE) {
#else
SDL_PollEvent(&event);
#endif
g_driver->clearScreen();
splash_bm->draw();
2003-08-15 18:00:22 +00:00
g_driver->flipBuffer();
#ifndef MACOSX
}
}
#endif
lua_iolibopen();
lua_strlibopen();
lua_mathlibopen();
2003-08-15 18:00:22 +00:00
register_lua();
2004-12-31 22:05:26 +00:00
g_lua_initialized = true;
bundle_dofile("_system.lua");
2003-08-15 18:00:22 +00:00
lua_pushnil(); // resumeSave
2004-10-31 09:37:06 +00:00
lua_pushnil(); // bootParam
// lua_pushnumber(0); // bootParam
lua_call("BOOT");
2003-08-15 18:00:22 +00:00
g_engine->setMode(ENGINE_MODE_NORMAL);
g_engine->mainLoop();
2003-08-15 18:00:22 +00:00
quit();
return 0;
}
void quit() {
2004-12-31 22:05:26 +00:00
if (g_lua_initialized) {
lua_removelibslists();
lua_close();
g_lua_initialized = false;
}
if (g_registry) {
g_registry->save();
delete g_registry;
g_registry = NULL;
}
if (g_smush) {
delete g_smush;
g_smush = NULL;
}
if (g_imuse) {
delete g_imuse;
g_imuse = NULL;
}
if (g_localizer) {
delete g_localizer;
g_localizer = NULL;
}
if (g_resourceloader) {
delete g_resourceloader;
g_resourceloader = NULL;
}
if (g_engine) {
delete g_engine;
g_engine = NULL;
}
if (g_timer) {
delete g_timer;
g_timer = NULL;
}
if (g_mixer) {
delete g_mixer;
g_mixer = NULL;
}
if (g_driver) {
delete g_driver;
g_driver = NULL;
}
SDL_Quit();
2003-08-15 18:00:22 +00:00
}
2003-12-12 21:38:12 +00:00
2004-12-09 23:55:43 +00:00
StackLock::StackLock(MutexRef mutex) :
_mutex(mutex) {
lockMutex(_mutex);
2003-12-12 21:38:12 +00:00
}
StackLock::~StackLock() {
unlockMutex(_mutex);
2003-12-12 21:38:12 +00:00
}
MutexRef createMutex() {
2004-12-09 23:55:43 +00:00
return (MutexRef)SDL_CreateMutex();
2003-12-12 21:38:12 +00:00
}
void lockMutex(MutexRef mutex) {
2004-12-09 23:55:43 +00:00
SDL_mutexP((SDL_mutex *)mutex);
2003-12-12 21:38:12 +00:00
}
void unlockMutex(MutexRef mutex) {
2004-12-09 23:55:43 +00:00
SDL_mutexV((SDL_mutex *)mutex);
2003-12-12 21:38:12 +00:00
}
void deleteMutex(MutexRef mutex) {
2004-12-09 23:55:43 +00:00
SDL_DestroyMutex((SDL_mutex *)mutex);
2003-12-12 21:38:12 +00:00
}