| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268 |
- #include "anki/core/App.h"
- #include "anki/core/Logger.h"
- #include "anki/core/Globals.h"
- #include "anki/util/Exception.h"
- #include "anki/util/Platform.h"
- #include <GL/glew.h>
- #include <sstream>
- #include <SDL/SDL.h>
- #include <iostream>
- #include <iomanip>
- #include <boost/filesystem.hpp>
- #include <boost/algorithm/string.hpp>
- namespace anki {
- //==============================================================================
- // handleMessageHanlderMsgs =
- //==============================================================================
- void App::handleMessageHanlderMsgs(const char* file, int line,
- const char* func, Logger::MessageType type, const char* msg)
- {
- std::ostream* out = NULL;
- const char* x = NULL;
- switch(type)
- {
- case Logger::MT_NORMAL:
- out = &std::cout;
- x = "Info";
- break;
- case Logger::MT_ERROR:
- out = &std::cerr;
- x = "Error";
- break;
- case Logger::MT_WARNING:
- out = &std::cerr;
- x = "Warn";
- break;
- }
- (*out) << "(" << file << ":" << line << " "<< func <<
- ") " << x << ": " << 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::get().connect(&App::handleMessageHanlderMsgs, this);
- parseCommandLineArgs(argc, argv);
- printAppInfo();
- initDirs();
- initWindow();
- // other
- activeCam = NULL;
- timerTick = 1.0 / 60.0; // in sec. 1.0 / period
- }
- //==============================================================================
- // initWindow =
- //==============================================================================
- void App::initWindow()
- {
- ANKI_INFO("SDL window initializing...");
- if(SDL_Init(SDL_INIT_VIDEO) < 0)
- {
- throw ANKI_EXCEPTION("Failed to init SDL_VIDEO");
- }
- // print driver name
- const char* driverName = SDL_GetCurrentVideoDriver();
- if(driverName != NULL)
- {
- ANKI_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);
- // WARNING: Set this low only in deferred shading
- SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 8);
- 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 ANKI_EXCEPTION("Cannot create main window");
- }
- glContext = SDL_GL_CreateContext(windowId);
- // the icon
- iconImage = SDL_LoadBMP("gfx/icon.bmp");
- if(iconImage == NULL)
- {
- ANKI_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);
- }
- ANKI_INFO("SDL window initialization ends");
- }
- //==============================================================================
- // initDirs =
- //==============================================================================
- void App::initDirs()
- {
- settingsPath = boost::filesystem::path(getenv("HOME")) / ".anki";
- if(!boost::filesystem::exists(settingsPath))
- {
- ANKI_INFO("Creating settings dir \"" << settingsPath.string() << "\"");
- boost::filesystem::create_directory(settingsPath);
- }
- cachePath = settingsPath / "cache";
- if(boost::filesystem::exists(cachePath))
- {
- ANKI_INFO("Deleting dir \"" << cachePath.string() << "\"");
- boost::filesystem::remove_all(cachePath);
- }
- ANKI_INFO("Creating cache dir \"" << cachePath.string() << "\"");
- boost::filesystem::create_directory(cachePath);
- }
- //==============================================================================
- // 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(ANKI_REVISION)
- # define ANKI_REVISION "unknown"
- #endif
- void App::printAppInfo()
- {
- std::stringstream msg;
- msg << "App info: ";
- #if defined(NDEBUG)
- msg << "Release";
- #else
- msg << "Debug";
- #endif
- msg << " build, ";
- msg << "platform ID " << ANKI_PLATFORM << ", ";
- msg << "compiler ID " << ANKI_COMPILER << ", ";
- msg << "GLEW " << glewGetString(GLEW_VERSION) << ", ";
- const SDL_version* v = SDL_Linked_Version();
- msg << "SDL " << int(v->major) << '.' << int(v->minor) << '.' <<
- int(v->patch) << ", " << "build date " __DATE__ << ", " <<
- "rev " << ANKI_REVISION;
- ANKI_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;
- }
- } // end namespace
|