openMSX
SDLVisibleSurfaceBase.cc
Go to the documentation of this file.
2 #include "InitException.hh"
3 #include "Icon.hh"
4 #include "Display.hh"
5 #include "RenderSettings.hh"
6 #include "PNG.hh"
7 #include "FileContext.hh"
8 #include "CliComm.hh"
9 #include "build-info.hh"
10 #include "endian.hh"
11 #include <cassert>
12 
13 namespace openmsx {
14 
15 int SDLVisibleSurfaceBase::windowPosX = SDL_WINDOWPOS_UNDEFINED;
16 int SDLVisibleSurfaceBase::windowPosY = SDL_WINDOWPOS_UNDEFINED;
17 
19 {
20  // store last known position for when we recreate it
21  // the window gets recreated when changing renderers, for instance.
22  // Do not store if we're fullscreen, the location is the top-left
23  if ((SDL_GetWindowFlags(window.get()) & SDL_WINDOW_FULLSCREEN) == 0) {
24  SDL_GetWindowPosition(window.get(), &windowPosX, &windowPosY);
25  }
26 }
27 
28 // TODO: The video subsystem is not de-initialized on errors.
29 // While it would be consistent to do so, doing it in this class is
30 // not ideal since the init doesn't happen here.
31 void SDLVisibleSurfaceBase::createSurface(int width, int height, unsigned flags)
32 {
33  if (getDisplay().getRenderSettings().getFullScreen()) {
34  flags |= SDL_WINDOW_FULLSCREEN_DESKTOP;
35  }
36 #ifdef __APPLE__
37  // See SDLGLVisibleSurface::setViewPort() for why only macos (for now).
38  flags |= SDL_WINDOW_ALLOW_HIGHDPI;
39 #endif
40 
41  assert(!window);
42  window.reset(SDL_CreateWindow(
43  getDisplay().getWindowTitle().c_str(),
44  windowPosX, windowPosY,
45  width, height,
46  flags));
47  if (!window) {
48  std::string err = SDL_GetError();
49  throw InitException("Could not create window: ", err);
50  }
51 
53 
54  // prefer linear filtering (instead of nearest neighbour)
55  SDL_SetHint(SDL_HINT_RENDER_SCALE_QUALITY, "1");
56 
57  // set icon
58  if constexpr (OPENMSX_SET_WINDOW_ICON) {
59  SDLSurfacePtr iconSurf;
60  // always use 32x32 icon on Windows, for some reason you get badly scaled icons there
61 #ifndef _WIN32
62  try {
63  iconSurf = PNG::load(preferSystemFileContext().resolve("icons/openMSX-logo-256.png"), true);
64  } catch (MSXException& e) {
66  "Falling back to built in 32x32 icon, because failed to load icon: ",
67  e.getMessage());
68 #endif
69  iconSurf.reset(SDL_CreateRGBSurfaceFrom(
70  const_cast<char*>(openMSX_icon.pixel_data),
74  Endian::BIG ? 0xFF000000 : 0x000000FF,
75  Endian::BIG ? 0x00FF0000 : 0x0000FF00,
76  Endian::BIG ? 0x0000FF00 : 0x00FF0000,
77  Endian::BIG ? 0x000000FF : 0xFF000000));
78 #ifndef _WIN32
79  }
80 #endif
81  SDL_SetColorKey(iconSurf.get(), SDL_TRUE, 0);
82  SDL_SetWindowIcon(window.get(), iconSurf.get());
83  }
84 }
85 
87 {
88  assert(window);
89  SDL_SetWindowTitle(window.get(), getDisplay().getWindowTitle().c_str());
90 }
91 
93 {
94  auto flags = SDL_GetWindowFlags(window.get());
95  // Note: SDL_WINDOW_FULLSCREEN_DESKTOP also has the SDL_WINDOW_FULLSCREEN
96  // bit set.
97  bool currentState = (flags & SDL_WINDOW_FULLSCREEN) != 0;
98  if (currentState == fullscreen) {
99  // already wanted stated
100  return true;
101  }
102 
103  if (SDL_SetWindowFullscreen(window.get(),
104  fullscreen ? SDL_WINDOW_FULLSCREEN_DESKTOP : 0) != 0) {
105  return false; // error, try re-creating the window
106  }
107  fullScreenUpdated(fullscreen);
108  return true; // success
109 }
110 
111 } // namespace openmsx
Wrapper around a SDL_Surface.
void reset(SDL_Surface *surface_=nullptr)
SDL_Surface * get()
void printWarning(std::string_view message)
Definition: CliComm.cc:10
Thrown when a subsystem initialisation fails.
void createSurface(int width, int height, unsigned flags)
virtual void fullScreenUpdated(bool fullscreen)=0
bool setFullScreen(bool fullscreen) override
Display & getDisplay() const
CliComm & getCliComm() const
constexpr bool BIG
Definition: endian.hh:13
constexpr double e
Definition: Math.hh:18
SDLSurfacePtr load(const std::string &filename, bool want32bpp)
Load the given PNG file in a SDL_Surface.
Definition: PNG.cc:95
This file implemented 3 utility functions:
Definition: Autofire.cc:9
OpenMSX_Icon openMSX_icon
Definition: Icon.cc:17
const FileContext & preferSystemFileContext()
Definition: FileContext.cc:163
const char * pixel_data
Definition: Icon.hh:11
unsigned width
Definition: Icon.hh:8
unsigned height
Definition: Icon.hh:9
unsigned bytes_per_pixel
Definition: Icon.hh:10