sdlSplashScreen.cpp 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. //-----------------------------------------------------------------------------
  2. // Copyright (c) 2012 GarageGames, LLC
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to
  6. // deal in the Software without restriction, including without limitation the
  7. // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  8. // sell copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  19. // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  20. // IN THE SOFTWARE.
  21. //-----------------------------------------------------------------------------
  22. #include "platform/platform.h"
  23. #include "console/console.h"
  24. #include "gfx/bitmap/gBitmap.h"
  25. #include "SDL.h"
  26. #include "windowManager/sdl/sdlWindow.h"
  27. static SDL_Window* gSplashWindow = nullptr;
  28. static SDL_Surface* gSplashImage = nullptr;
  29. static SDL_Texture* gSplashTexture = nullptr;
  30. static SDL_Renderer* gSplashRenderer = nullptr;
  31. bool Platform::displaySplashWindow( String path )
  32. {
  33. if(path.isEmpty())
  34. return false;
  35. Torque::Path iconPath = Torque::Path(path);
  36. if (iconPath.getExtension() == String("bmp"))
  37. {
  38. Con::errorf("Unable to use bmp format images for the splash screen. Please use a different format.");
  39. return false;
  40. }
  41. Resource<GBitmap> img = GBitmap::load(iconPath);
  42. if (img != NULL)
  43. {
  44. U32 pitch;
  45. U32 width = img->getWidth();
  46. bool hasAlpha = img->getHasTransparency();
  47. U32 depth;
  48. if (hasAlpha)
  49. {
  50. pitch = 4 * width;
  51. depth = 32;
  52. }
  53. else
  54. {
  55. pitch = 3 * width;
  56. depth = 24;
  57. }
  58. Uint32 rmask, gmask, bmask, amask;
  59. if (SDL_BYTEORDER == SDL_BIG_ENDIAN)
  60. {
  61. S32 shift = hasAlpha ? 8 : 0;
  62. rmask = 0xff000000 >> shift;
  63. gmask = 0x00ff0000 >> shift;
  64. bmask = 0x0000ff00 >> shift;
  65. amask = 0x000000ff >> shift;
  66. }
  67. else
  68. {
  69. rmask = 0x000000ff;
  70. gmask = 0x0000ff00;
  71. bmask = 0x00ff0000;
  72. amask = hasAlpha ? 0xff000000 : 0;
  73. }
  74. gSplashImage = SDL_CreateRGBSurfaceFrom(img->getAddress(0, 0), img->getWidth(), img->getHeight(), depth, pitch, rmask, gmask, bmask, amask);
  75. }
  76. //now the pop-up window
  77. if (gSplashImage)
  78. {
  79. gSplashWindow = SDL_CreateWindow("", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED,
  80. gSplashImage->w, gSplashImage->h, SDL_WINDOW_BORDERLESS | SDL_WINDOW_SHOWN);
  81. gSplashRenderer = SDL_CreateRenderer(gSplashWindow, -1, SDL_RENDERER_ACCELERATED);
  82. gSplashTexture = SDL_CreateTextureFromSurface(gSplashRenderer, gSplashImage);
  83. SDL_RenderCopy(gSplashRenderer, gSplashTexture, NULL, NULL);
  84. SDL_RenderPresent(gSplashRenderer);
  85. SDL_DestroyTexture(gSplashTexture);
  86. gSplashTexture = nullptr;
  87. SDL_DestroyRenderer(gSplashRenderer);
  88. gSplashRenderer = nullptr;
  89. }
  90. return true;
  91. }
  92. bool Platform::closeSplashWindow()
  93. {
  94. if (gSplashTexture != nullptr)
  95. {
  96. SDL_DestroyTexture(gSplashTexture);
  97. gSplashTexture = nullptr;
  98. }
  99. if (gSplashImage != nullptr)
  100. {
  101. SDL_FreeSurface(gSplashImage);
  102. gSplashImage = nullptr;
  103. }
  104. if (gSplashRenderer != nullptr)
  105. {
  106. SDL_DestroyRenderer(gSplashRenderer);
  107. gSplashRenderer = nullptr;
  108. }
  109. if (gSplashWindow != nullptr)
  110. {
  111. SDL_DestroyWindow(gSplashWindow);
  112. gSplashWindow = nullptr;
  113. }
  114. return true;
  115. }