/** ** Convert a string to SDLKey */ int Str2SdlKey(const char *str) { InitKey2Str(); std::map<int, std::string>::iterator i; for (i = Key2Str.begin(); i != Key2Str.end(); ++i) { if (!strcasecmp(str, (*i).second.c_str())) { return (*i).first; } } std::map<std::string, int>::iterator i2; for (i2 = Str2Key.begin(); i2 != Str2Key.end(); ++i2) { if (!strcasecmp(str, (*i2).first.c_str())) { return (*i2).second; } } return 0; }
/** ** Initialize the video part for SDL. */ void InitVideoSdl() { Uint32 flags = 0; if (SDL_WasInit(SDL_INIT_VIDEO) == 0) { #ifndef USE_WIN32 // Fix tablet input in full-screen mode SDL_putenv(strdup("SDL_MOUSE_RELATIVE=0")); #endif int res = SDL_Init( #ifdef DEBUG SDL_INIT_NOPARACHUTE | #endif SDL_INIT_AUDIO | SDL_INIT_VIDEO | SDL_INIT_TIMER); if (res < 0) { fprintf(stderr, "Couldn't initialize SDL: %s\n", SDL_GetError()); exit(1); } // Clean up on exit atexit(SDL_Quit); #ifdef USE_MAEMO maemo_init(); #endif // If debug is enabled, Stratagus disable SDL Parachute. // So we need gracefully handle segfaults and aborts. #if defined(DEBUG) && !defined(USE_WIN32) signal(SIGSEGV, CleanExit); signal(SIGABRT, CleanExit); #endif // Set WindowManager Title if (!FullGameName.empty()) { SDL_WM_SetCaption(FullGameName.c_str(), FullGameName.c_str()); } else if (!Parameters::Instance.applicationName.empty()) { SDL_WM_SetCaption(Parameters::Instance.applicationName.c_str(), Parameters::Instance.applicationName.c_str()); } else { SDL_WM_SetCaption("Stratagus", "Stratagus"); } #if ! defined(USE_WIN32) && ! defined(USE_MAEMO) #if defined(USE_OPENGL) || defined(USE_GLES) // Make sure, that we not create OpenGL textures (and do not call OpenGL functions), when creating icon surface bool UseOpenGL_orig = UseOpenGL; UseOpenGL = false; #endif SDL_Surface *icon = NULL; CGraphic *g = NULL; struct stat st; std::string FullGameNameL = FullGameName; for (size_t i = 0; i < FullGameNameL.size(); ++i) { FullGameNameL[i] = tolower(FullGameNameL[i]); } std::string ApplicationName = Parameters::Instance.applicationName; std::string ApplicationNameL = ApplicationName; for (size_t i = 0; i < ApplicationNameL.size(); ++i) { ApplicationNameL[i] = tolower(ApplicationNameL[i]); } std::vector <std::string> pixmaps; pixmaps.push_back(std::string() + PIXMAPS + "/" + FullGameName + ".png"); pixmaps.push_back(std::string() + PIXMAPS + "/" + FullGameNameL + ".png"); pixmaps.push_back(std::string() + "/usr/share/pixmaps" + "/" + FullGameName + ".png"); pixmaps.push_back(std::string() + "/usr/share/pixmaps" + "/" + FullGameNameL + ".png"); pixmaps.push_back(std::string() + PIXMAPS + "/" + ApplicationName + ".png"); pixmaps.push_back(std::string() + PIXMAPS + "/" + ApplicationNameL + ".png"); pixmaps.push_back(std::string() + "/usr/share/pixmaps" + "/" + ApplicationName + ".png"); pixmaps.push_back(std::string() + "/usr/share/pixmaps" + "/" + ApplicationNameL + ".png"); pixmaps.push_back(std::string() + PIXMAPS + "/" + "Stratagus" + ".png"); pixmaps.push_back(std::string() + PIXMAPS + "/" + "stratagus" + ".png"); pixmaps.push_back(std::string() + "/usr/share/pixmaps" + "/" + "Stratagus" + ".png"); pixmaps.push_back(std::string() + "/usr/share/pixmaps" + "/" + "stratagus" + ".png"); for (size_t i = 0; i < pixmaps.size(); ++i) { if (stat(pixmaps[i].c_str(), &st) == 0) { if (g) { CGraphic::Free(g); } g = CGraphic::New(pixmaps[i].c_str()); g->Load(); icon = g->Surface; if (icon) { break; } } } if (icon) { SDL_WM_SetIcon(icon, 0); } if (g) { CGraphic::Free(g); } #if defined(USE_OPENGL) || defined(USE_GLES) UseOpenGL = UseOpenGL_orig; #endif #endif #ifdef USE_WIN32 HWND hwnd = NULL; HICON hicon = NULL; SDL_SysWMinfo info; SDL_VERSION(&info.version); if (SDL_GetWMInfo(&info)) { hwnd = info.window; } if (hwnd) { hicon = ExtractIcon(GetModuleHandle(NULL), Parameters::Instance.applicationName.c_str(), 0); } if (hicon) { SendMessage(hwnd, (UINT)WM_SETICON, ICON_SMALL, (LPARAM)hicon); SendMessage(hwnd, (UINT)WM_SETICON, ICON_BIG, (LPARAM)hicon); } #endif } // Initialize the display #ifdef USE_MAEMO // TODO: Support window mode and portrait mode resolution on Maemo - Nokia N900 Video.FullScreen = 1; Video.Width = 800; Video.Height = 480; #endif #if !defined(USE_OPENGL) && !defined(USE_GLES) flags = SDL_HWSURFACE | SDL_HWPALETTE; #endif // Sam said: better for windows. /* SDL_HWSURFACE|SDL_HWPALETTE | */ if (Video.FullScreen) { flags |= SDL_FULLSCREEN; } #if defined(USE_OPENGL) || defined(USE_GLES) if (UseOpenGL) { #ifdef USE_GLES_NATIVE flags |= SDL_OPENGLES; #endif #ifdef USE_GLES_MAEMO if (SDL_GLES_Init(SDL_GLES_VERSION_1_1) < 0) { fprintf(stderr, "Couldn't initialize SDL_GLES: %s\n", SDL_GetError()); exit(1); } // Clean up GLES on exit atexit(SDL_GLES_Quit); flags |= SDL_SWSURFACE; #endif #ifdef USE_OPENGL flags |= SDL_OPENGL | SDL_GL_DOUBLEBUFFER; #endif } #endif if (!Video.Width || !Video.Height) { Video.Width = 640; Video.Height = 480; } if (!Video.Depth) { Video.Depth = 32; } #if defined(USE_OPENGL) || defined(USE_GLES) if (!Video.ViewportWidth || !Video.ViewportHeight) { Video.ViewportWidth = Video.Width; Video.ViewportHeight = Video.Height; } TheScreen = SDL_SetVideoMode(Video.ViewportWidth, Video.ViewportHeight, Video.Depth, flags); #else TheScreen = SDL_SetVideoMode(Video.Width, Video.Height, Video.Depth, flags); #endif if (TheScreen && (TheScreen->format->BitsPerPixel != 16 && TheScreen->format->BitsPerPixel != 32)) { // Only support 16 and 32 bpp, default to 16 #if defined(USE_OPENGL) || defined(USE_GLES) TheScreen = SDL_SetVideoMode(Video.ViewportWidth, Video.ViewportHeight, 16, flags); #else TheScreen = SDL_SetVideoMode(Video.Width, Video.Height, 16, flags); #endif } if (TheScreen == NULL) { fprintf(stderr, "Couldn't set %dx%dx%d video mode: %s\n", Video.Width, Video.Height, Video.Depth, SDL_GetError()); exit(1); } Video.FullScreen = (TheScreen->flags & SDL_FULLSCREEN) ? 1 : 0; Video.Depth = TheScreen->format->BitsPerPixel; #if defined(USE_TOUCHSCREEN) && defined(USE_WIN32) // Must not allow SDL to switch to relative mouse coordinates // with touchscreen when going fullscreen. So we don't hide the // cursor, but instead set a transparent 1px cursor Uint8 emptyCursor[] = {'\0'}; Video.blankCursor = SDL_CreateCursor(emptyCursor, emptyCursor, 1, 1, 0, 0); SDL_SetCursor(Video.blankCursor); #else // Turn cursor off, we use our own. SDL_ShowCursor(SDL_DISABLE); #endif // Make default character translation easier SDL_EnableUNICODE(1); #if defined(USE_OPENGL) || defined(USE_GLES) if (UseOpenGL) { #ifdef USE_GLES_MAEMO SDL_GLES_Context *context = SDL_GLES_CreateContext(); if (!context) { fprintf(stderr, "Couldn't initialize SDL_GLES_CreateContext: %s\n", SDL_GetError()); exit(1); } if (SDL_GLES_MakeCurrent(context) < 0) { fprintf(stderr, "Couldn't initialize SDL_GLES_MakeCurrent: %s\n", SDL_GetError()); exit(1); } // atexit(GLES_DeleteContext(context)); #endif #ifdef USE_GLES_EGL // Get the SDL window handle SDL_SysWMinfo sysInfo; //Will hold our Window information SDL_VERSION(&sysInfo.version); //Set SDL version if (SDL_GetWMInfo(&sysInfo) <= 0) { fprintf(stderr, "Unable to get window handle\n"); exit(1); } eglDisplay = eglGetDisplay((EGLNativeDisplayType)sysInfo.info.x11.display); if (!eglDisplay) { fprintf(stderr, "Couldn't open EGL Display\n"); exit(1); } if (!eglInitialize(eglDisplay, NULL, NULL)) { fprintf(stderr, "Couldn't initialize EGL Display\n"); exit(1); } // Find a matching config EGLint configAttribs[] = {EGL_SURFACE_TYPE, EGL_WINDOW_BIT, EGL_NONE}; EGLint numConfigsOut = 0; EGLConfig eglConfig; if (eglChooseConfig(eglDisplay, configAttribs, &eglConfig, 1, &numConfigsOut) != EGL_TRUE || numConfigsOut == 0) { fprintf(stderr, "Unable to find appropriate EGL config\n"); exit(1); } eglSurface = eglCreateWindowSurface(eglDisplay, eglConfig, (EGLNativeWindowType)sysInfo.info.x11.window, 0); if (eglSurface == EGL_NO_SURFACE) { fprintf(stderr, "Unable to create EGL surface\n"); exit(1); } // Bind GLES and create the context eglBindAPI(EGL_OPENGL_ES_API); EGLint contextParams[] = {EGL_CONTEXT_CLIENT_VERSION, 1, EGL_NONE}; EGLContext eglContext = eglCreateContext(eglDisplay, eglConfig, NULL, NULL); if (eglContext == EGL_NO_CONTEXT) { fprintf(stderr, "Unable to create GLES context\n"); exit(1); } if (eglMakeCurrent(eglDisplay, eglSurface, eglSurface, eglContext) == EGL_FALSE) { fprintf(stderr, "Unable to make GLES context current\n"); exit(1); } #endif InitOpenGL(); } #endif InitKey2Str(); ColorBlack = Video.MapRGB(TheScreen->format, 0, 0, 0); ColorDarkGreen = Video.MapRGB(TheScreen->format, 48, 100, 4); ColorLightBlue = Video.MapRGB(TheScreen->format, 52, 113, 166); ColorBlue = Video.MapRGB(TheScreen->format, 0, 0, 252); ColorOrange = Video.MapRGB(TheScreen->format, 248, 140, 20); ColorWhite = Video.MapRGB(TheScreen->format, 252, 248, 240); ColorLightGray = Video.MapRGB(TheScreen->format, 192, 192, 192); ColorGray = Video.MapRGB(TheScreen->format, 128, 128, 128); ColorDarkGray = Video.MapRGB(TheScreen->format, 64, 64, 64); ColorRed = Video.MapRGB(TheScreen->format, 252, 0, 0); ColorGreen = Video.MapRGB(TheScreen->format, 0, 252, 0); ColorYellow = Video.MapRGB(TheScreen->format, 252, 252, 0); UI.MouseWarpPos.x = UI.MouseWarpPos.y = -1; }
/** ** Initialize the video part for SDL. */ void InitVideoSdl(void) { Uint32 flags; if (SDL_WasInit(SDL_INIT_VIDEO) == 0) { #ifdef HAVE_X #define SDL_MOUSE_RELATIVE #endif #ifdef __PLAYBOOK__ //#define SDL_MOUSE_RELATIVE #endif #ifdef SDL_MOUSE_RELATIVE // Fix tablet input in full-screen mode. // // By default, SDL 1.2 on X11 uses mouse relative mode // if the mouse cursor is hidden and input is grabbed. // In this mode, SDL attempts to report the direction // in which the mouse is moving. SDL computes this // from the mouse-cursor coordinates it gets from X. // If the mouse cursor of X were to hit an edge of the // screen, SDL would not know whether the user keeps // moving the mouse in that direction. To prevent // that, SDL warps the mouse cursor of X back to the // center of the screen whenever it gets too far. // SDL then compensates for this warping in its // relative-motion computations, and uses the relative // coordinates to maintain its own idea of the mouse // cursor location, separately from what X thinks. // // All of the above works great with mice but fails // horribly with tablet or touch-screen devices, where // the coordinates reported by X depend only on the // position of the stylus and not at all on where SDL // previously warped the pointer. Because Bos Wars // doesn't actually care about relative mouse motion, // let's just disable that whole feature in SDL. The // easiest way to do that is by setting an environment // variable. Do this before SDL_Init in case that // caches the value. // // SDL 1.2 documentation warns that environment // variables are mostly for debugging and might not be // supported in future SDL releases. In that event, // Bos Wars can instead use the same trick as Barrage // does: tell SDL to show the mouse cursor, but make // all of its pixels transparent, so it's actually // invisible. However, SDL 1.3 is apparently going to // support tablet devices natively, so this might not // be needed then. static char mouseRelative0[] = "SDL_MOUSE_RELATIVE=0"; SDL_putenv(mouseRelative0); // doesn't want const #endif if (SDL_Init( #ifdef DEBUG SDL_INIT_NOPARACHUTE | #endif SDL_INIT_AUDIO | SDL_INIT_VIDEO | SDL_INIT_TIMER) < 0 ) { fprintf(stderr, "Couldn't initialize SDL: %s\n", SDL_GetError()); exit(1); } // Clean up on exit atexit(SDL_Quit); // If debug is enabled, Stratagus disable SDL Parachute. // So we need gracefully handle segfaults and aborts. #if defined(DEBUG) && !defined(USE_WIN32) signal(SIGSEGV, CleanExit); signal(SIGABRT, CleanExit); #endif // Set WindowManager Title SDL_WM_SetCaption("Bos Wars", "Bos Wars"); } // Initialize the display flags = 0; // Sam said: better for windows. /* SDL_HWSURFACE|SDL_HWPALETTE | */ if (Video.FullScreen) { flags |= SDL_FULLSCREEN; } if (UseOpenGL) { flags |= SDL_OPENGL; } if (!Video.Width || !Video.Height) { #ifdef __PLAYBOOK__ Video.Width = 1024; Video.Height = 600; #else Video.Width = 640; Video.Height = 480; #endif } TheScreen = SDL_SetVideoMode(Video.Width, Video.Height, Video.Depth, flags); if (TheScreen && (TheScreen->format->BitsPerPixel != 16 && TheScreen->format->BitsPerPixel != 32)) { // Only support 16 and 32 bpp, default to 16 TheScreen = SDL_SetVideoMode(Video.Width, Video.Height, 16, flags); } if (TheScreen == NULL) { fprintf(stderr, "Couldn't set %dx%dx%d video mode: %s\n", Video.Width, Video.Height, Video.Depth, SDL_GetError()); exit(1); } Video.FullScreen = (TheScreen->flags & SDL_FULLSCREEN) ? 1 : 0; Video.Depth = TheScreen->format->BitsPerPixel; // Turn cursor off, we use our own. SDL_ShowCursor(0); // Make default character translation easier SDL_EnableUNICODE(1); if (UseOpenGL) { InitOpenGL(); } InitKey2Str(); ColorBlack = Video.MapRGB(TheScreen->format, 0, 0, 0); ColorDarkGreen = Video.MapRGB(TheScreen->format, 48, 100, 4); ColorDarkBlue = Video.MapRGB(TheScreen->format, 0, 0, 96); ColorBlue = Video.MapRGB(TheScreen->format, 0, 0, 252); ColorCyan = Video.MapRGB(TheScreen->format, 0, 160, 208); ColorOrange = Video.MapRGB(TheScreen->format, 248, 140, 20); ColorWhite = Video.MapRGB(TheScreen->format, 252, 248, 240); ColorGray = Video.MapRGB(TheScreen->format, 128, 128, 128); ColorRed = Video.MapRGB(TheScreen->format, 252, 0, 0); ColorGreen = Video.MapRGB(TheScreen->format, 0, 252, 0); ColorYellow = Video.MapRGB(TheScreen->format, 252, 252, 0); UI.MouseWarpX = UI.MouseWarpY = -1; }
/** ** Initialize the video part for SDL. */ void InitVideoSdl(void) { Uint32 flags; if (SDL_WasInit(SDL_INIT_VIDEO) == 0) { if (SDL_Init( #ifdef DEBUG SDL_INIT_NOPARACHUTE | #endif SDL_INIT_AUDIO | SDL_INIT_VIDEO | SDL_INIT_TIMER) < 0 ) { fprintf(stderr, "Couldn't initialize SDL: %s\n", SDL_GetError()); ExitFatal(1); } // Clean up on exit atexit(SDL_Quit); // If debug is enabled, Stratagus disable SDL Parachute. // So we need gracefully handle segfaults and aborts. #if defined(DEBUG) && !defined(USE_WIN32) signal(SIGSEGV, CleanExit); signal(SIGABRT, CleanExit); #endif // Set WindowManager Title SDL_WM_SetCaption("Stratagus", "Stratagus"); } // Initialize the display flags = 0; // Sam said: better for windows. /* SDL_HWSURFACE|SDL_HWPALETTE | */ if (Video.FullScreen) { flags |= SDL_FULLSCREEN; } #ifdef USE_OPENGL flags |= SDL_OPENGL; #endif if (!Video.Width || !Video.Height) { Video.Width = 640; Video.Height = 480; } TheScreen = SDL_SetVideoMode(Video.Width, Video.Height, Video.Depth, flags); if (TheScreen && (TheScreen->format->BitsPerPixel != 16 && TheScreen->format->BitsPerPixel != 32)) { // Only support 16 and 32 bpp, default to 16 TheScreen = SDL_SetVideoMode(Video.Width, Video.Height, 16, flags); } if (TheScreen == NULL) { fprintf(stderr, "Couldn't set %dx%dx%d video mode: %s\n", Video.Width, Video.Height, Video.Depth, SDL_GetError()); ExitFatal(1); } Video.FullScreen = (TheScreen->flags & SDL_FULLSCREEN) ? 1 : 0; Video.Depth = TheScreen->format->BitsPerPixel; // Turn cursor off, we use our own. SDL_ShowCursor(0); // Make default character translation easier SDL_EnableUNICODE(1); #ifdef USE_OPENGL InitOpenGL(); #endif InitKey2Str(); ColorBlack = Video.MapRGB(TheScreen->format, 0, 0, 0); ColorDarkGreen = Video.MapRGB(TheScreen->format, 48, 100, 4); ColorBlue = Video.MapRGB(TheScreen->format, 0, 0, 252); ColorOrange = Video.MapRGB(TheScreen->format, 248, 140, 20); ColorWhite = Video.MapRGB(TheScreen->format, 252, 248, 240); ColorGray = Video.MapRGB(TheScreen->format, 128, 128, 128); ColorRed = Video.MapRGB(TheScreen->format, 252, 0, 0); ColorGreen = Video.MapRGB(TheScreen->format, 0, 252, 0); ColorYellow = Video.MapRGB(TheScreen->format, 252, 252, 0); UI.MouseWarpX = UI.MouseWarpY = -1; }