| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344 |
- #include <GL/glew.h>
- #include <sstream>
- #include <SDL/SDL.h>
- #include <iostream>
- #include <iomanip>
- #include <boost/filesystem.hpp>
- #include <boost/algorithm/string.hpp>
- #include "App.h"
- #include "RendererInitializer.h"
- #include "MainRenderer.h"
- #include "ScriptingEngine.h"
- #include "StdinListener.h"
- #include "Input.h"
- #include "Logger.h"
- #include "Globals.h"
- //======================================================================================================================
- // handleMessageHanlderMsgs =
- //======================================================================================================================
- void App::handleMessageHanlderMsgs(const char* file, int line, const char* func, const char* msg)
- {
- if(boost::find_first(msg, "Warning") || boost::find_first(msg, "Error"))
- {
- std::cerr << "(" << file << ":" << line << " "<< func << ") " << msg << std::flush;
- }
- else
- {
- std::cout << "(" << file << ":" << line << " "<< func << ") " << msg << std::flush;
- }
- }
- //======================================================================================================================
- // parseCommandLineArgs =
- //======================================================================================================================
- void App::parseCommandLineArgs(int argc, char* argv[])
- {
- for(int i = 1; i < argc; i++)
- {
- char* arg = argv[i];
- if(strcmp(arg, "--terminal-coloring") == 0)
- {
- terminalColoringEnabled = true;
- }
- else if(strcmp(arg, "--no-terminal-coloring") == 0)
- {
- terminalColoringEnabled = false;
- }
- else
- {
- std::cerr << "Incorrect command line argument \"" << arg << "\"" << std::endl;
- abort();
- }
- }
- }
- //======================================================================================================================
- // init =
- //======================================================================================================================
- void App::init(int argc, char* argv[])
- {
- windowW = 1280;
- windowH = 720;
- terminalColoringEnabled = true,
- fullScreenFlag = false;
- // send output to handleMessageHanlderMsgs
- LoggerSingleton::getInstance().getSignal().connect(boost::bind(&App::handleMessageHanlderMsgs,
- this, _1, _2, _3, _4));
- INFO("Initializing the engine...");
- parseCommandLineArgs(argc, argv);
- printAppInfo();
- // dirs
- initDirs();
- // create the subsystems. WATCH THE ORDER
- const char* commonPythonCode =
- "import sys\n"
- "from Anki import *\n"
- "\n"
- "class StdoutCatcher:\n"
- "\tdef write(self, str_):\n"
- "\t\tif str_ == \"\\n\": return\n"
- "\t\tline = sys._getframe(1).f_lineno\n"
- "\t\tfile = sys._getframe(1).f_code.co_filename\n"
- "\t\tfunc = sys._getframe(1).f_code.co_name\n"
- "\t\tLoggerSingleton.getInstance().write(file, line, func, str_ + \"\\n\")\n"
- "\n"
- "class StderrCatcher:\n"
- "\tdef write(self, str_):\n"
- "\t\tline = sys._getframe(1).f_lineno\n"
- "\t\tfile = sys._getframe(1).f_code.co_filename\n"
- "\t\tfunc = sys._getframe(1).f_code.co_name\n"
- "\t\tLoggerSingleton.getInstance().write(file, line, func, str_)\n"
- "\n"
- "sys.stdout = StdoutCatcher()\n"
- "sys.stderr = StderrCatcher()\n";
- ScriptingEngineSingleton::getInstance().execScript(commonPythonCode);
- StdinListenerSingleton::getInstance().start();
- initWindow();
- initRenderer();
- // other
- activeCam = NULL;
- timerTick = 1000 / 40; // in ms. 1000/Hz
- time = 0;
- INFO("Engine initialization ends");
- }
- //======================================================================================================================
- // initWindow =
- //======================================================================================================================
- void App::initWindow()
- {
- INFO("SDL window initializing...");
- if(SDL_Init(SDL_INIT_VIDEO) < 0)
- {
- throw EXCEPTION("Failed to init SDL_VIDEO");
- }
- // print driver name
- const char* driverName = SDL_GetCurrentVideoDriver();
- if(driverName != NULL)
- {
- INFO("Video driver name: " << driverName);
- }
- // set GL attribs
- SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 3);
- SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 3);
- SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 8); // WARNING: Set this low only in deferred shading
- SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
- SDL_GL_SetAttribute(SDL_GL_ACCELERATED_VISUAL, 1);
- // OpenWindow
- windowId = SDL_CreateWindow("AnKi 3D Engine", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, windowW, windowH,
- SDL_WINDOW_OPENGL | SDL_WINDOW_SHOWN);
- if(!windowId)
- {
- throw EXCEPTION("Cannot create main window");
- }
- glContext = SDL_GL_CreateContext(windowId);
- // the icon
- iconImage = SDL_LoadBMP("gfx/icon.bmp");
- if(iconImage == NULL)
- {
- WARNING("Cannot load window icon");
- }
- else
- {
- Uint32 colorkey = SDL_MapRGB(iconImage->format, 255, 0, 255);
- SDL_SetColorKey(iconImage, SDL_SRCCOLORKEY, colorkey);
- //SDL_WM_SetIcon(iconImage, NULL);
- SDL_SetWindowIcon(windowId, iconImage);
- }
- INFO("SDL window initialization ends");
- }
- //======================================================================================================================
- // initDirs =
- //======================================================================================================================
- void App::initDirs()
- {
- settingsPath = boost::filesystem::path(getenv("HOME")) / ".anki";
- if(!boost::filesystem::exists(settingsPath))
- {
- INFO("Creating settings dir \"" << settingsPath.string() << "\"");
- boost::filesystem::create_directory(settingsPath);
- }
- cachePath = settingsPath / "cache";
- if(boost::filesystem::exists(cachePath))
- {
- INFO("Deleting dir \"" << cachePath.string() << "\"");
- boost::filesystem::remove_all(cachePath);
- }
- INFO("Creating cache dir \"" << cachePath.string() << "\"");
- boost::filesystem::create_directory(cachePath);
- }
- //======================================================================================================================
- // initRenderer =
- //======================================================================================================================
- void App::initRenderer()
- {
- RendererInitializer initializer;
- initializer.ms.ez.enabled = true;
- initializer.dbg.enabled = true;
- initializer.is.sm.bilinearEnabled = true;
- initializer.is.sm.enabled = true;
- initializer.is.sm.pcfEnabled = true;
- initializer.is.sm.resolution = 1024;
- initializer.is.sm.level0Distance = 3.0;
- initializer.pps.hdr.enabled = true;
- initializer.pps.hdr.renderingQuality = 0.25;
- initializer.pps.hdr.blurringDist = 1.0;
- initializer.pps.hdr.blurringIterationsNum = 2;
- initializer.pps.hdr.exposure = 4.0;
- initializer.pps.ssao.blurringIterationsNum = 4;
- initializer.pps.ssao.enabled = true;
- initializer.pps.ssao.renderingQuality = 0.3;
- initializer.pps.bl.enabled = true;
- initializer.pps.bl.blurringIterationsNum = 2;
- initializer.pps.bl.sideBlurFactor = 1.0;
- initializer.mainRendererQuality = 1.0;
- MainRendererSingleton::getInstance().init(initializer);
- }
- //======================================================================================================================
- // togleFullScreen =
- //======================================================================================================================
- void App::togleFullScreen()
- {
- //SDL_WM_ToggleFullScreen(mainSurf);
- SDL_SetWindowFullscreen(windowId, fullScreenFlag ? SDL_TRUE : SDL_FALSE);
- fullScreenFlag = !fullScreenFlag;
- }
- //======================================================================================================================
- // swapBuffers =
- //======================================================================================================================
- void App::swapBuffers()
- {
- //SDL_GL_SwapBuffers();
- SDL_GL_SwapWindow(windowId);
- }
- //======================================================================================================================
- // quit =
- //======================================================================================================================
- void App::quit(int code)
- {
- SDL_FreeSurface(iconImage);
- SDL_GL_DeleteContext(glContext);
- SDL_DestroyWindow(windowId);
- SDL_Quit();
- exit(code);
- }
- //======================================================================================================================
- // printAppInfo =
- //======================================================================================================================
- #if !defined(REVISION)
- #define REVISION "unknown"
- #endif
- void App::printAppInfo()
- {
- std::stringstream msg;
- msg << "App info: Build: ";
- #if defined(NDEBUG)
- msg << "release, ";
- #else
- msg << "debug, ";
- #endif
- msg << "platform ";
- #if defined(PLATFORM_LINUX)
- msg << "Linux, ";
- #elif defined(PLATFORM_WIN)
- msg << "Windows, ";
- #else
- #error "See file"
- #endif
- msg << "GLEW " << glewGetString(GLEW_VERSION) << ", ";
- const SDL_version* v = SDL_Linked_Version();
- msg << "SDL " << int(v->major) << '.' << int(v->minor) << '.' << int(v->patch) << ", ";
- msg << "build date " __DATE__ << ", ";
- msg << "rev " << REVISION;
- INFO(msg.str());
- }
- //======================================================================================================================
- // getDesktopWidth =
- //======================================================================================================================
- uint App::getDesktopWidth() const
- {
- SDL_DisplayMode mode;
- /// @todo re-enable it
- //SDL_GetDesktopDisplayMode(&mode);
- return mode.w;
- }
- //======================================================================================================================
- // getDesktopHeight =
- //======================================================================================================================
- uint App::getDesktopHeight() const
- {
- SDL_DisplayMode mode;
- /// @todo re-enable it
- //SDL_GetDesktopDisplayMode(&mode);
- return mode.h;
- }
- //======================================================================================================================
- // execStdinScpripts =
- //======================================================================================================================
- void App::execStdinScpripts()
- {
- while(1)
- {
- std::string cmd = StdinListenerSingleton::getInstance().getLine();
- if(cmd.length() < 1)
- {
- break;
- }
- try
- {
- ScriptingEngineSingleton::getInstance().execScript(cmd.c_str(), "command line input");
- }
- catch(Exception& e)
- {
- ERROR(e.what());
- }
- }
- }
|