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