2 Copyright (C) 2003, 2010 - Wolfire Games
3 Copyright (C) 2010-2017 - Lugaru contributors (see AUTHORS file)
5 This file is part of Lugaru.
7 Lugaru is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
12 Lugaru is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with Lugaru. If not, see <http://www.gnu.org/licenses/>.
23 #include "Audio/openal_wrapper.hpp"
24 #include "Graphic/gamegl.hpp"
25 #include "Platform/Platform.hpp"
26 #include "User/Settings.hpp"
27 #include "Version.hpp"
45 extern float multiplier;
46 extern float realmultiplier;
48 extern bool cellophane;
49 extern float texdetail;
52 extern bool stillloading;
55 extern float slomospeed;
56 extern float slomofreq;
58 extern int difficulty;
60 extern SDL_Window* sdlwindow;
64 set<pair<int, int>> resolutions;
66 // statics/globals (internal only) ------------------------------------------
73 //-----------------------------------------------------------------------------------------------------------------------
79 glClear(GL_COLOR_BUFFER_BIT);
83 glDisable(GL_ALPHA_TEST);
85 glDisable(GL_DEPTH_TEST);
87 glDisable(GL_LIGHTING);
88 glDisable(GL_LOGIC_OP);
89 glDisable(GL_TEXTURE_1D);
90 glDisable(GL_TEXTURE_2D);
91 glPixelTransferi(GL_MAP_COLOR, GL_FALSE);
92 glPixelTransferi(GL_RED_SCALE, 1);
93 glPixelTransferi(GL_RED_BIAS, 0);
94 glPixelTransferi(GL_GREEN_SCALE, 1);
95 glPixelTransferi(GL_GREEN_BIAS, 0);
96 glPixelTransferi(GL_BLUE_SCALE, 1);
97 glPixelTransferi(GL_BLUE_BIAS, 0);
98 glPixelTransferi(GL_ALPHA_SCALE, 1);
99 glPixelTransferi(GL_ALPHA_BIAS, 0);
101 // set initial rendering states
102 glShadeModel(GL_SMOOTH);
104 glDepthFunc(GL_LEQUAL);
105 glDepthMask(GL_TRUE);
106 glEnable(GL_DEPTH_TEST);
107 glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);
108 glCullFace(GL_FRONT);
109 glEnable(GL_CULL_FACE);
110 glEnable(GL_LIGHTING);
112 glEnable(GL_COLOR_MATERIAL);
113 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
114 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
115 glAlphaFunc(GL_GREATER, 0.5f);
117 if (CanInitStereo(stereomode)) {
118 InitStereo(stereomode);
120 fprintf(stderr, "Failed to initialize stereo, disabling.\n");
121 stereomode = stereoNone;
125 void toggleFullscreen()
127 fullscreen = !fullscreen;
128 Uint32 flags = SDL_GetWindowFlags(sdlwindow);
129 if (flags & SDL_WINDOW_FULLSCREEN) {
130 flags &= ~SDL_WINDOW_FULLSCREEN;
132 flags |= SDL_WINDOW_FULLSCREEN;
134 SDL_SetWindowFullscreen(sdlwindow, flags);
137 SDL_bool sdlEventProc(const SDL_Event& e)
143 case SDL_WINDOWEVENT:
144 if (e.window.event == SDL_WINDOWEVENT_CLOSE) {
149 case SDL_MOUSEMOTION:
150 deltah += e.motion.xrel;
151 deltav += e.motion.yrel;
155 if ((e.key.keysym.scancode == SDL_SCANCODE_G) &&
156 (e.key.keysym.mod & KMOD_CTRL)) {
157 SDL_bool mode = SDL_TRUE;
158 if ((SDL_GetWindowFlags(sdlwindow) & SDL_WINDOW_FULLSCREEN) == 0) {
159 mode = (SDL_GetWindowGrab(sdlwindow) ? SDL_FALSE : SDL_TRUE);
161 SDL_SetWindowGrab(sdlwindow, mode);
162 SDL_SetRelativeMouseMode(mode);
163 } else if ((e.key.keysym.scancode == SDL_SCANCODE_RETURN) && (e.key.keysym.mod & KMOD_ALT)) {
171 // --------------------------------------------------------------------------
173 static Point gMidPoint;
186 if (!SDL_WasInit(SDL_INIT_VIDEO)) {
187 if (SDL_Init(SDL_INIT_VIDEO) == -1) {
188 fprintf(stderr, "SDL_Init() failed: %s\n", SDL_GetError());
192 if (!LoadSettings()) {
193 fprintf(stderr, "Failed to load config, creating default\n");
197 if (SDL_GL_LoadLibrary(NULL) == -1) {
198 fprintf(stderr, "SDL_GL_LoadLibrary() failed: %s\n", SDL_GetError());
203 for (int displayIdx = 0; displayIdx < SDL_GetNumVideoDisplays(); ++displayIdx) {
204 for (int i = 0; i < SDL_GetNumDisplayModes(displayIdx); ++i) {
205 SDL_DisplayMode mode;
206 if (SDL_GetDisplayMode(displayIdx, i, &mode) == -1) {
209 if ((mode.w < 640) || (mode.h < 480)) {
210 continue; // sane lower limit.
212 pair<int, int> resolution(mode.w, mode.h);
213 resolutions.insert(resolution);
217 if (resolutions.empty()) {
218 const std::string error = "No suitable video resolutions found.";
219 cerr << error << endl;
220 SDL_ShowSimpleMessageBox(SDL_MESSAGEBOX_ERROR, "Lugaru init failed!", error.c_str(), NULL);
225 if (commandLineOptions[SHOWRESOLUTIONS]) {
226 printf("Available resolutions:\n");
227 for (auto resolution = resolutions.begin(); resolution != resolutions.end(); resolution++) {
228 printf(" %d x %d\n", (int)resolution->first, (int)resolution->second);
232 SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
233 SDL_GL_SetAttribute(SDL_GL_STENCIL_SIZE, 1);
235 Uint32 sdlflags = SDL_WINDOW_OPENGL | SDL_WINDOW_SHOWN;
236 if (commandLineOptions[FULLSCREEN]) {
237 fullscreen = commandLineOptions[FULLSCREEN].last()->type();
240 sdlflags |= SDL_WINDOW_FULLSCREEN;
242 if (!commandLineOptions[NOMOUSEGRAB].last()->type()) {
243 sdlflags |= SDL_WINDOW_INPUT_GRABBED;
246 sdlwindow = SDL_CreateWindow("Lugaru", SDL_WINDOWPOS_CENTERED_DISPLAY(0), SDL_WINDOWPOS_CENTERED_DISPLAY(0),
247 kContextWidth, kContextHeight, sdlflags);
250 fprintf(stderr, "SDL_CreateWindow() failed: %s\n", SDL_GetError());
251 fprintf(stderr, "forcing 640x480...\n");
253 kContextHeight = 480;
254 sdlwindow = SDL_CreateWindow("Lugaru", SDL_WINDOWPOS_CENTERED_DISPLAY(0), SDL_WINDOWPOS_CENTERED_DISPLAY(0),
255 kContextWidth, kContextHeight, sdlflags);
257 fprintf(stderr, "SDL_CreateWindow() failed: %s\n", SDL_GetError());
258 fprintf(stderr, "forcing 640x480 windowed mode...\n");
259 sdlflags &= ~SDL_WINDOW_FULLSCREEN;
260 sdlwindow = SDL_CreateWindow("Lugaru", SDL_WINDOWPOS_CENTERED_DISPLAY(0), SDL_WINDOWPOS_CENTERED_DISPLAY(0),
261 kContextWidth, kContextHeight, sdlflags);
264 fprintf(stderr, "SDL_CreateWindow() failed: %s\n", SDL_GetError());
270 SDL_GLContext glctx = SDL_GL_CreateContext(sdlwindow);
272 fprintf(stderr, "SDL_GL_CreateContext() failed: %s\n", SDL_GetError());
277 SDL_GL_MakeCurrent(sdlwindow, glctx);
280 if ((SDL_GL_GetAttribute(SDL_GL_DOUBLEBUFFER, &dblbuf) == -1) || (!dblbuf)) {
281 fprintf(stderr, "Failed to get a double-buffered context.\n");
286 if (SDL_GL_SetSwapInterval(-1) == -1) { // try swap_tear first.
287 SDL_GL_SetSwapInterval(1);
291 if (!commandLineOptions[NOMOUSEGRAB].last()->type()) {
292 SDL_SetRelativeMouseMode(SDL_TRUE);
297 GLint width = kContextWidth;
298 GLint height = kContextHeight;
299 gMidPoint.h = width / 2;
300 gMidPoint.v = height / 2;
302 screenheight = height;
305 newscreenwidth = screenwidth;
306 newscreenheight = screenheight;
308 /* If saved resolution is not in the list, add it to the list (so that it’s selectable in the options) */
309 pair<int, int> startresolution(width, height);
310 if (resolutions.find(startresolution) == resolutions.end()) {
311 resolutions.insert(startresolution);
319 static void DoMouse()
322 if (mainmenu || ((abs(deltah) < 10 * realmultiplier * 1000) && (abs(deltav) < 10 * realmultiplier * 1000))) {
323 deltah *= usermousesensitivity;
324 deltav *= usermousesensitivity;
325 mousecoordh += deltah;
326 mousecoordv += deltav;
327 if (mousecoordh < 0) {
329 } else if (mousecoordh >= kContextWidth) {
330 mousecoordh = kContextWidth - 1;
332 if (mousecoordv < 0) {
334 } else if (mousecoordv >= kContextHeight) {
335 mousecoordv = kContextHeight - 1;
340 void DoFrameRate(int update)
342 static long frames = 0;
344 static AbsoluteTime time = { 0, 0 };
345 static AbsoluteTime frametime = { 0, 0 };
346 AbsoluteTime currTime = UpTime();
347 double deltaTime = (float)AbsoluteDeltaToDuration(currTime, frametime);
349 if (0 > deltaTime) { // if negative microseconds
350 deltaTime /= -1000000.0;
351 } else { // else milliseconds
355 multiplier = deltaTime;
356 if (multiplier < .001) {
359 if (multiplier > 10) {
363 frametime = currTime; // reset for next time interval
366 deltaTime = (float)AbsoluteDeltaToDuration(currTime, time);
368 if (0 > deltaTime) { // if negative microseconds
369 deltaTime /= -1000000.0;
370 } else { // else milliseconds
374 if (0.001 <= deltaTime) { // has update interval passed
376 time = currTime; // reset for next time interval
384 static float sps = 200;
386 static float oldmult;
389 if (multiplier > .6) {
393 fps = 1 / multiplier;
395 count = multiplier * sps;
400 realmultiplier = multiplier;
401 multiplier *= gamespeed;
402 if (difficulty == 1) {
405 if (difficulty == 0) {
410 multiplier *= .00001;
412 if (slomo && !mainmenu) {
413 multiplier *= slomospeed;
415 oldmult = multiplier;
416 multiplier /= (float)count;
422 for (int i = 0; i < count; i++) {
425 multiplier = oldmult;
428 /* - Debug code to test how many channels were active on average per frame
429 static long frames = 0;
431 static AbsoluteTime start = {0,0};
432 AbsoluteTime currTime = UpTime ();
433 static int num_channels = 0;
435 num_channels += OPENAL_GetChannelsPlaying();
436 double deltaTime = (float) AbsoluteDeltaToDuration (currTime, start);
438 if (0 > deltaTime) // if negative microseconds
439 deltaTime /= -1000000.0;
440 else // else milliseconds
448 float avg_channels = (float)num_channels / (float)frames;
450 ofstream opstream("log.txt",ios::app);
451 opstream << "Average frame count: ";
453 opstream << " frames - ";
454 opstream << avg_channels;
455 opstream << " per frame.\n";
462 if (stereomode == stereoNone) {
463 DrawGLScene(stereoCenter);
465 DrawGLScene(stereoLeft);
466 DrawGLScene(stereoRight);
470 // --------------------------------------------------------------------------
476 delete[] commandLineOptionsBuffer;
481 // --------------------------------------------------------------------------
483 static bool IsFocused()
485 return ((SDL_GetWindowFlags(sdlwindow) & SDL_WINDOW_INPUT_FOCUS) != 0);
489 // (code lifted from physfs: http://icculus.org/physfs/ ... zlib license.)
490 static char* findBinaryInPath(const char* bin, char* envr)
492 size_t alloc_size = 0;
499 ptr = strchr(start, ':'); /* find next $PATH separator. */
503 size = strlen(start) + strlen(bin) + 2;
504 if (size > alloc_size) {
505 char* x = (char*)realloc(exe, size);
517 /* build full binary path... */
519 if ((exe[0] == '\0') || (exe[strlen(exe) - 1] != '/')) {
524 if (access(exe, X_OK) == 0) { /* Exists as executable? We're done. */
525 strcpy(exe, start); /* i'm lazy. piss off. */
529 start = ptr + 1; /* start points to beginning of next element. */
530 } while (ptr != NULL);
536 return (NULL); /* doesn't exist in path. */
537 } /* findBinaryInPath */
539 char* calcBaseDir(const char* argv0)
541 /* If there isn't a path on argv0, then look through the $PATH for it. */
545 if (strchr(argv0, '/')) {
546 retval = strdup(argv0);
548 *((char*)strrchr(retval, '/')) = '\0';
553 envr = getenv("PATH");
561 retval = findBinaryInPath(argv0, envr);
566 static inline void chdirToAppPath(const char* argv0)
568 char* dir = calcBaseDir(argv0);
570 #if (defined(__APPLE__) && defined(__MACH__))
571 // Chop off /Contents/MacOS if it's at the end of the string, so we
572 // land in the base of the app bundle.
573 const size_t len = strlen(dir);
574 const char* bundledirs = "/Contents/MacOS";
575 const size_t bundledirslen = strlen(bundledirs);
576 if (len > bundledirslen) {
577 char* ptr = (dir + len) - bundledirslen;
578 if (strcasecmp(ptr, bundledirs) == 0)
583 if (chdir(dir) != 0) {
584 printf("Error changing dir to '%s' (%s).\n", dir, strerror(errno));
591 const option::Descriptor usage[] =
593 { UNKNOWN, 0, "", "", option::Arg::None, "USAGE: lugaru [options]\n\n"
595 { VERSION, 0, "v", "version", option::Arg::None, " -v, --version Print version and exit." },
596 { HELP, 0, "h", "help", option::Arg::None, " -h, --help Print usage and exit." },
597 { FULLSCREEN, 1, "f", "fullscreen", option::Arg::None, " -f, --fullscreen Start the game in fullscreen mode." },
598 { FULLSCREEN, 0, "w", "windowed", option::Arg::None, " -w, --windowed Start the game in windowed mode (default)." },
599 { NOMOUSEGRAB, 1, "", "nomousegrab", option::Arg::None, " --nomousegrab Disable mousegrab." },
600 { NOMOUSEGRAB, 0, "", "mousegrab", option::Arg::None, " --mousegrab Enable mousegrab (default)." },
601 { SOUND, 1, "", "nosound", option::Arg::None, " --nosound Disable sound." },
602 { OPENALINFO, 0, "", "openal-info", option::Arg::None, " --openal-info Print info about OpenAL at launch." },
603 { SHOWRESOLUTIONS, 0, "", "showresolutions", option::Arg::None, " --showresolutions List the resolutions found by SDL at launch." },
604 { DEVTOOLS, 0, "d", "devtools", option::Arg::None, " -d, --devtools Enable dev tools: console, level editor and debug info." },
608 option::Option commandLineOptions[commandLineOptionsNumber];
609 option::Option* commandLineOptionsBuffer;
611 int main(int argc, char** argv)
614 argv += (argc > 0); // skip program name argv[0] if present
615 option::Stats stats(true, usage, argc, argv);
616 if (commandLineOptionsNumber != stats.options_max) {
617 std::cerr << "Found incorrect command line option number" << std::endl;
620 commandLineOptionsBuffer = new option::Option[stats.buffer_max];
621 option::Parser parse(true, usage, argc, argv, commandLineOptions, commandLineOptionsBuffer);
624 delete[] commandLineOptionsBuffer;
628 // Always start by printing the version and info to the stdout
629 std::cout << "--------------------------------------------------------------------------\n"
630 << "Lugaru HD: The Rabbit's Foot, by Wolfire Games and the OSS Lugaru project.\n\n"
631 << "Licensed under the GPL 2.0+ and CC-BY-SA 3.0 and 4.0 licenses.\n"
632 << "More information, updates and bug reports at http://osslugaru.gitlab.io\n"
635 std::cout << "Version " + VERSION_STRING + " -- " + VERSION_BUILD_TYPE + " build\n"
636 << "--------------------------------------------------------------------------\n"
639 if (commandLineOptions[VERSION]) {
640 // That was enough, quit.
641 delete[] commandLineOptionsBuffer;
645 if (commandLineOptions[HELP]) {
646 option::printUsage(std::cout, usage);
647 delete[] commandLineOptionsBuffer;
651 if (option::Option* opt = commandLineOptions[UNKNOWN]) {
652 std::cerr << "Unknown option: " << opt->name << "\n";
653 option::printUsage(std::cerr, usage);
654 delete[] commandLineOptionsBuffer;
658 // !!! FIXME: we could use a Win32 API for this. --ryan.
660 chdirToAppPath(argv[0]);
672 delete[] commandLineOptionsBuffer;
676 if (commandLineOptions[DEVTOOLS]) {
680 bool gameDone = false;
681 bool gameFocused = true;
683 while (!gameDone && !tryquit) {
687 // check windows messages
694 while (SDL_PollEvent(&e)) {
695 if (!sdlEventProc(e)) {
706 // allow game chance to pause
711 // game is not in focus, give CPU time to other apps by waiting for messages instead of 'peeking'
723 } catch (const std::exception& error) {
726 std::string e = "Caught exception: ";
731 SDL_ShowSimpleMessageBox(SDL_MESSAGEBOX_ERROR, "Exception catched", error.what(), NULL);