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