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 // TODO: The video subsystem is not de-inited on errors.
15 // While it would be consistent to do so, doing it in this class is
16 // not ideal since the init doesn't happen here.
17 void SDLVisibleSurfaceBase::createSurface(int width, int height, unsigned flags)
18 {
19  if (getDisplay().getRenderSettings().getFullScreen()) {
20  flags |= SDL_WINDOW_FULLSCREEN_DESKTOP;
21  }
22  flags |= SDL_WINDOW_ALLOW_HIGHDPI;
23 
24  assert(!window);
25  window.reset(SDL_CreateWindow(
26  getDisplay().getWindowTitle().c_str(),
27  SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED,
28  width, height,
29  flags));
30  if (!window) {
31  std::string err = SDL_GetError();
32  throw InitException("Could not create window: ", err);
33  }
34 
36 
37  // prefer linear filtering (instead of nearest neighbour)
38  SDL_SetHint(SDL_HINT_RENDER_SCALE_QUALITY, "1");
39 
40  // set icon
41  if (OPENMSX_SET_WINDOW_ICON) {
42  SDLSurfacePtr iconSurf;
43  // always use 32x32 icon on Windows, for some reason you get badly scaled icons there
44 #ifndef _WIN32
45  try {
46  iconSurf = PNG::load(preferSystemFileContext().resolve("icons/openMSX-logo-256.png"), true);
47  } catch (MSXException& e) {
49  "Falling back to built in 32x32 icon, because failed to load icon: ",
50  e.getMessage());
51 #endif
52  iconSurf.reset(SDL_CreateRGBSurfaceFrom(
53  const_cast<char*>(openMSX_icon.pixel_data),
57  OPENMSX_BIGENDIAN ? 0xFF000000 : 0x000000FF,
58  OPENMSX_BIGENDIAN ? 0x00FF0000 : 0x0000FF00,
59  OPENMSX_BIGENDIAN ? 0x0000FF00 : 0x00FF0000,
60  OPENMSX_BIGENDIAN ? 0x000000FF : 0xFF000000));
61 #ifndef _WIN32
62  }
63 #endif
64  SDL_SetColorKey(iconSurf.get(), SDL_TRUE, 0);
65  SDL_SetWindowIcon(window.get(), iconSurf.get());
66  }
67 }
68 
70 {
71  assert(window);
72  SDL_SetWindowTitle(window.get(), getDisplay().getWindowTitle().c_str());
73 }
74 
76 {
77  auto flags = SDL_GetWindowFlags(window.get());
78  // Note: SDL_WINDOW_FULLSCREEN_DESKTOP also has the SDL_WINDOW_FULLSCREEN
79  // bit set.
80  bool currentState = (flags & SDL_WINDOW_FULLSCREEN) != 0;
81  if (currentState == fullscreen) {
82  // already wanted stated
83  return true;
84  }
85 
86  // in win32, toggling full screen requires opening a new SDL screen
87  // in Linux calling the SDL_WM_ToggleFullScreen usually works fine
88  // We now always create a new screen to make the code on both OSes
89  // more similar (we had a windows-only bug because of this difference)
90  return false;
91 }
92 
93 } // namespace openmsx
openmsx::SDLVisibleSurfaceBase::updateWindowTitle
void updateWindowTitle() override
Definition: SDLVisibleSurfaceBase.cc:69
Display.hh
openmsx::OpenMSX_Icon::pixel_data
const char * pixel_data
Definition: Icon.hh:11
SDLSurfacePtr
Wrapper around a SDL_Surface.
Definition: SDLSurfacePtr.hh:33
openmsx::OpenMSX_Icon::bytes_per_pixel
unsigned bytes_per_pixel
Definition: Icon.hh:10
openmsx::SDLVisibleSurfaceBase::setFullScreen
bool setFullScreen(bool fullscreen) override
Definition: SDLVisibleSurfaceBase.cc:75
openmsx::MSXException
Definition: MSXException.hh:9
openmsx::OpenMSX_Icon::width
unsigned width
Definition: Icon.hh:8
PNG.hh
Icon.hh
openmsx::CliComm::printWarning
void printWarning(std::string_view message)
Definition: CliComm.cc:10
openmsx::openMSX_icon
OpenMSX_Icon openMSX_icon
Definition: Icon.cc:17
openmsx::OpenMSX_Icon::height
unsigned height
Definition: Icon.hh:9
openmsx::Display::getWindowTitle
std::string getWindowTitle()
Definition: Display.cc:222
openmsx::InitException
Thrown when a subsystem initialisation fails.
Definition: InitException.hh:11
build-info.hh
RenderSettings.hh
FileContext.hh
openmsx::SDLVisibleSurfaceBase::createSurface
void createSurface(int width, int height, unsigned flags)
Definition: SDLVisibleSurfaceBase.cc:17
SDLSurfacePtr::get
SDL_Surface * get()
Definition: SDLSurfacePtr.hh:77
openmsx::SDLVisibleSurfaceBase::window
SDLWindowPtr window
Definition: SDLVisibleSurfaceBase.hh:23
InitException.hh
SDLVisibleSurfaceBase.hh
SDLSurfacePtr::reset
void reset(SDL_Surface *surface_=nullptr)
Definition: SDLSurfacePtr.hh:71
openmsx::PNG::load
SDLSurfacePtr load(const std::string &filename, bool want32bpp)
Load the given PNG file in a SDL_Surface.
Definition: PNG.cc:90
CliComm.hh
openmsx::MSXException::getMessage
const std::string & getMessage() const &
Definition: MSXException.hh:23
openmsx::VisibleSurface::getDisplay
Display & getDisplay() const
Definition: VisibleSurface.hh:52
openmsx
Thanks to enen for testing this on a real cartridge:
Definition: Autofire.cc:5
openmsx::VisibleSurface::getCliComm
CliComm & getCliComm() const
Definition: VisibleSurface.hh:51
openmsx::preferSystemFileContext
FileContext preferSystemFileContext()
Definition: FileContext.cc:155