1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445
|
#if defined __unix__ && !(defined __APPLE__)
#include "linuxstuff.h"
#endif
#ifdef _WIN32
#include "winstuff.h"
#endif
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include <unistd.h>
#include <iostream>
#include <stdexcept>
#include <time.h>
#include <sys/time.h>
#if !defined(HAVE_GLES2)
#include <GL/glew.h>
#else
#include <GLES2/gl2.h>
#include "eglport/eglport.h"
#endif
#include <SDL/SDL.h>
#include <SDL/SDL_syswm.h>
#include "debug.h"
#include "platform-dependent.h"
#include "language.h"
#include "stringy.h"
#include "sludger.h"
#include "backdrop.h"
#include "language.h"
#include "newfatal.h"
#include "people.h"
#include "floor.h"
#include "objtypes.h"
#include "talk.h"
#include "statusba.h"
#include "transition.h"
#include "specialsettings.h"
#include "timing.h"
#include "sound.h"
#include "sludger.h"
#include "graphics.h"
#include "helpers.h"
#ifdef _WIN32
#define PATHSLASH '\\'
#else
#define PATHSLASH '/'
#endif
extern bool runningFullscreen;
#ifndef MAX_PATH
#define MAX_PATH 1024 // maximum size of a path name
#endif
HWND hMainWindow = NULL;
int realWinWidth = 640, realWinHeight = 480;
extern float cameraZoom;
extern int specialSettings;
extern inputType input;
extern variableStack * noStack;
int dialogValue = 0;
char * gameName = NULL;
char * gamePath = NULL;
char *bundleFolder;
void setGameFilePath (char * f) {
char currentDir[1000];
if (! getcwd (currentDir, 998)) {
debugOut("Can't get current directory.\n");
}
int got = -1, a;
for (a = 0; f[a]; a ++) {
if (f[a] == PATHSLASH) got = a;
}
if (got != -1) {
f[got] = 0;
if (chdir (f)) {
debugOut("Error: Failed changing to directory %s\n", f);
}
f[got] = PATHSLASH;
}
gamePath = new char[400];
if (! checkNew (gamePath)) return;
if (! getcwd (gamePath, 398)) {
debugOut( "Can't get game directory.\n");
}
if (chdir (currentDir)) {
debugOut("Error: Failed changing to directory %s\n", currentDir);
}
}
void saveHSI (FILE * writer);
extern bool reallyWantToQuit;
#ifdef _WIN32
#undef main
#endif
int weAreDoneSoQuit;
void checkInput() {
static bool fakeRightclick = false;
SDL_Event event;
/* Check for events */
while ( SDL_PollEvent(&event) ) {
switch (event.type) {
case SDL_VIDEORESIZE:
realWinWidth = event.resize.w;
realWinHeight = event.resize.h;
setGraphicsWindow(false, true, true);
break;
case SDL_MOUSEMOTION:
input.justMoved = true;
input.mouseX = event.motion.x * ((float)winWidth/cameraZoom) / realWinWidth;
input.mouseY = event.motion.y * ((float)winHeight/cameraZoom) / realWinHeight;
break;
case SDL_MOUSEBUTTONDOWN:
if (event.button.button == SDL_BUTTON_LEFT)
{
if (SDL_GetModState() & KMOD_CTRL) {
input.rightClick = true;
fakeRightclick = true;
} else {
input.leftClick = true;
fakeRightclick = false;
}
}
if (event.button.button == SDL_BUTTON_RIGHT) input.rightClick = true;
input.mouseX = event.motion.x * ((float)winWidth/cameraZoom) / realWinWidth;
input.mouseY = event.motion.y * ((float)winHeight/cameraZoom) / realWinHeight;
break;
case SDL_MOUSEBUTTONUP:
if (event.button.button == SDL_BUTTON_LEFT) {
if (fakeRightclick) {
fakeRightclick = false;
input.rightRelease = true;
} else {
input.leftRelease = true;
}
}
if (event.button.button == SDL_BUTTON_RIGHT) input.rightRelease = true;
input.mouseX = event.motion.x * ((float)winWidth/cameraZoom) / realWinWidth;
input.mouseY = event.motion.y * ((float)winHeight/cameraZoom) / realWinHeight;
break;
case SDL_KEYDOWN:
// A Windows key is pressed - let's leave fullscreen.
if (runningFullscreen) {
if (event.key.keysym.sym == SDLK_LSUPER || event.key.keysym.sym == SDLK_LSUPER) {
setGraphicsWindow(! runningFullscreen);
}
}
// Ignore Command keypresses - they're for the OS to handle.
if (event.key.keysym.mod & KMOD_META) {
// Command+F - let's switch to/from full screen
if ('f' == event.key.keysym.unicode) {
setGraphicsWindow(! runningFullscreen);
}
break;
} else if (event.key.keysym.mod & KMOD_ALT) {
// Alt + Enter also switches full screen mode
if (SDLK_RETURN == event.key.keysym.sym) {
setGraphicsWindow(! runningFullscreen);
}
if (SDLK_a == event.key.keysym.sym) {
gameSettings.antiAlias = ! gameSettings.antiAlias;
break;
}
// Allow Alt+F4 to quit
if (SDLK_F4 == event.key.keysym.sym) {
SDL_Event event;
event.type = SDL_QUIT;
SDL_PushEvent(&event);
}
break;
}
switch (event.key.keysym.sym) {
case SDLK_BACKSPACE:
case SDLK_DELETE: // Ok, mapping these to the same key is weird, I admit. But good?
input.keyPressed = 127; break;
case SDLK_TAB:
input.keyPressed = 9; break;
case SDLK_RETURN:
input.keyPressed = 13; break;
case SDLK_ESCAPE:
input.keyPressed = 27; break;
case SDLK_PAGEUP:
input.keyPressed = 63276; break;
case SDLK_PAGEDOWN:
input.keyPressed = 63277; break;
case SDLK_END:
input.keyPressed = 63275; break;
case SDLK_HOME:
input.keyPressed = 63273; break;
case SDLK_LEFT:
input.keyPressed = 63234; break;
case SDLK_UP:
input.keyPressed = 63232; break;
case SDLK_RIGHT:
input.keyPressed = 63235; break;
case SDLK_DOWN:
input.keyPressed = 63233; break;
case SDLK_F1:
input.keyPressed = 63236; break;
case SDLK_F2:
input.keyPressed = 63237; break;
case SDLK_F3:
input.keyPressed = 63238; break;
case SDLK_F4:
input.keyPressed = 63239; break;
case SDLK_F5:
input.keyPressed = 63240; break;
case SDLK_F6:
input.keyPressed = 63241; break;
case SDLK_F7:
input.keyPressed = 63242; break;
case SDLK_F8:
input.keyPressed = 63243; break;
case SDLK_F9:
input.keyPressed = 63244; break;
case SDLK_F10:
input.keyPressed = 63245; break;
case SDLK_F11:
input.keyPressed = 63246; break;
case SDLK_F12:
input.keyPressed = 63247; break;
default:
input.keyPressed = event.key.keysym.unicode;
break;
}
break;
case SDL_QUIT:
if (reallyWantToQuit) {
// The game file has requested that we quit
weAreDoneSoQuit = 1;
} else {
// The request is from elsewhere - ask for confirmation.
setGraphicsWindow(false);
//fprintf (stderr, "%s %s\n", gameName, getNumberedString(2));
if (msgBoxQuestion (gameName, getNumberedString(2))) {
weAreDoneSoQuit = 1;
}
}
break;
default:
break;
}
}
}
int main(int argc, char *argv[]) try
{
/* Dimensions of our window. */
winWidth = 640;
winHeight = 480;
char * sludgeFile;
time_t t;
srand((unsigned) time(&t));
// bundleFolder is used to look for the game file
// and later to find the shader programs
#ifdef __APPLE__
// bundleFolder is set in applicationDidFinishLaunching.
#elif defined __unix__
bundleFolder = copyString(DATADIR); // DATADIR is defined in the Makefile.
#else
bundleFolder = copyString(argv[0]);
int lastSlash = -1;
for (int i = 0; bundleFolder[i]; i ++) {
if (bundleFolder[i] == PATHSLASH) lastSlash = i;
}
bundleFolder[lastSlash+1] = NULL;
#endif
if (argc > 1) {
sludgeFile = argv[argc - 1];
} else {
sludgeFile = joinStrings (bundleFolder, "gamedata.slg");
if (! ( fileExists (sludgeFile) ) ) {
delete sludgeFile;
sludgeFile = joinStrings (bundleFolder, "gamedata");
if (! ( fileExists (sludgeFile) ) ) {
sludgeFile = grabFileName ();
}
}
}
#if defined __unix__ && !(defined __APPLE__)
if (! parseCmdlineParameters(argc, argv) ) {
printCmdlineUsage();
return 0;
}
if (! fileExists(sludgeFile) ) {
fprintf(stderr, "Game file not found.\n");
printCmdlineUsage();
return 0;
}
#endif
// The player pressed cancel in the file selection dialogue,
// so we should quit now.
if (! sludgeFile) return 0;
// OK, so we DO want to start up, then...
setGameFilePath (sludgeFile);
if (! initSludge (sludgeFile)) return 0;
/* Initialize the SDL library */
if ( SDL_Init(SDL_INIT_VIDEO) < 0 ) {
msgBox("Startup Error: Couldn't initialize SDL.", SDL_GetError());
exit(1);
}
if (gameIcon) {
if (SDL_Surface * programIcon = SDL_CreateRGBSurfaceFrom(gameIcon, iconW, iconH, 32, iconW*4, 0x000000ff, 0x0000ff00, 0x00ff0000, 0xff000000)) {
SDL_WM_SetIcon(programIcon, NULL);
SDL_FreeSurface(programIcon);
}
delete gameIcon;
}
// Needed to make menu shortcuts work (on Mac), i.e. Command+Q for quit
SDL_putenv((char *)"SDL_ENABLEAPPEVENTS=1");
setupOpenGLStuff();
#ifdef _WIN32
SDL_SysWMinfo wmInfo;
SDL_VERSION(&wmInfo.version);
SDL_GetWMInfo(&wmInfo);
hMainWindow = wmInfo.window;
#endif
registerWindowForFatal ();
if (! resizeBackdrop (winWidth, winHeight)) return fatal ("Couldn't allocate memory for backdrop");
blankScreen (0, 0, winWidth, winHeight);
if (! initPeople ()) return fatal ("Couldn't initialise people stuff");
if (! initFloor ()) return fatal ("Couldn't initialise floor stuff");
if (! initObjectTypes ()) return fatal ("Couldn't initialise object type stuff");
initSpeech ();
initStatusBar ();
resetRandW ();
gameName = getNumberedString(1);
SDL_WM_SetCaption(gameName, gameName);
if ( (specialSettings & (SPECIAL_MOUSE_1 | SPECIAL_MOUSE_2)) == SPECIAL_MOUSE_1) {
// Hide the standard mouse cursor!
// This is done in a weird way because there's bugs with SDL_ShowCursor(SDL_DISABLE);
SDL_Cursor *cursor = NULL;
Uint8 data = 0;
SDL_FreeCursor(cursor);
cursor = SDL_CreateCursor(&data, &data, 1, 1, 0, 0);
SDL_SetCursor(cursor);
}
if (! (specialSettings & SPECIAL_SILENT)) {
initSoundStuff (hMainWindow);
}
startNewFunctionNum (0, 0, NULL, noStack);
Init_Timer();
SDL_EnableUNICODE(1);
weAreDoneSoQuit = 0;
while ( !weAreDoneSoQuit ) {
checkInput();
walkAllPeople ();
handleInput ();
sludgeDisplay ();
Wait_Frame();
}
debugOut( "Bye!\n\n");
delete [] gamePath;
killSoundStuff ();
#if defined(HAVE_GLES2)
EGL_Close();
#endif
/* Clean up the SDL library */
SDL_Quit();
displayFatal ();
return(0);
}
catch (std::exception & ex) //NOTE by reference, not value
{
std::cerr << "std::exception caught: " << ex.what() << std::endl;
return -1;
}
catch (...)
{
std::cerr << "Unknown exception was never caught" << std::endl;
return -2;
}
|