33 #include <SDL2/SDL_render.h>
39 #define LOG_DP LOG_STREAM(info, log_display)
40 #define ERR_DP LOG_STREAM(err, log_display)
41 #define WRN_DP LOG_STREAM(warn, log_display)
42 #define DBG_DP LOG_STREAM(debug, log_display)
47 std::unique_ptr<sdl::window> window;
53 texture current_render_target_ = {};
55 bool headless_ =
false;
56 bool testing_ =
false;
57 point test_resolution_ = {1024, 768};
58 int refresh_rate_ = 0;
59 point game_canvas_size_ = {0, 0};
61 rect input_area_ = {};
83 LOG_DP <<
"initializing video";
84 if(SDL_WasInit(SDL_INIT_VIDEO)) {
85 throw error(
"video subsystem already initialized");
87 if(SDL_InitSubSystem(SDL_INIT_VIDEO) < 0) {
88 ERR_DP <<
"Could not initialize SDL_video: " << SDL_GetError();
89 throw error(
"Video initialization failed");
106 throw error(
"unrecognized fake type passed to video::init");
112 LOG_DP <<
"deinitializing video";
116 assert(SDL_WasInit(SDL_INIT_TIMER));
122 render_texture_.reset();
123 current_render_target_.reset();
129 if(SDL_WasInit(SDL_INIT_VIDEO)) {
130 LOG_DP <<
"quitting SDL video subsystem";
131 SDL_QuitSubSystem(SDL_INIT_VIDEO);
133 if(SDL_WasInit(SDL_INIT_VIDEO)) {
135 throw error(
"video subsystem still initialized after deinit");
151 LOG_DP <<
"running headless";
154 game_canvas_size_ = {800,600};
168 throw(
"trying to update test framebuffer with no window");
171 bool changed =
false;
175 if (render_texture_) {
177 SDL_QueryTexture(render_texture_,
nullptr,
nullptr, &
w, &
h);
178 if (
w != test_resolution_.x ||
h != test_resolution_.y) {
180 LOG_DP <<
"destroying old render texture";
181 render_texture_.reset();
184 if (!render_texture_) {
185 LOG_DP <<
"creating offscreen render texture";
186 render_texture_.assign(SDL_CreateTexture(
188 window->pixel_format(),
189 SDL_TEXTUREACCESS_TARGET,
190 test_resolution_.x, test_resolution_.y
192 LOG_DP <<
"updated render target to " << test_resolution_.x
193 <<
"x" << test_resolution_.y;
198 game_canvas_size_ = test_resolution_;
199 input_area_ = {{}, test_resolution_};
210 throw error(
"trying to update framebuffer with no window");
217 bool changed =
false;
220 SDL_SetRenderTarget(*window,
nullptr);
225 SDL_RenderSetIntegerScale(*window, SDL_TRUE);
228 point osize(window->get_output_size());
229 int max_scale = std::min(
238 int def_scale = std::min(
241 scale = std::min(max_scale, def_scale);
243 int min_scale = std::min(
251 if (pixel_scale_ !=
scale) {
252 pixel_scale_ =
scale;
257 point lsize(window->get_logical_size());
258 point wsize(window->get_size());
259 if (lsize.x != osize.x /
scale || lsize.y != osize.y /
scale) {
261 LOG_DP <<
"reducing pixel scale from desired "
266 LOG_DP <<
"overriding logical size";
267 LOG_DP <<
" old lsize: " << lsize;
268 LOG_DP <<
" old wsize: " << wsize;
269 LOG_DP <<
" old osize: " << osize;
270 window->set_logical_size(osize.x /
scale, osize.y /
scale);
271 lsize = window->get_logical_size();
272 wsize = window->get_size();
273 osize = window->get_output_size();
274 LOG_DP <<
" new lsize: " << lsize;
275 LOG_DP <<
" new wsize: " << wsize;
276 LOG_DP <<
" new osize: " << osize;
278 SDL_RenderGetScale(*window, &sx, &sy);
279 LOG_DP <<
" render scale: " << sx <<
", " << sy;
282 game_canvas_size_ = lsize;
285 if (render_texture_) {
287 SDL_QueryTexture(render_texture_,
nullptr,
nullptr, &
w, &
h);
288 if (
w != osize.x ||
h != osize.y) {
290 LOG_DP <<
"destroying old render texture";
291 render_texture_.reset();
294 render_texture_.set_draw_size(lsize);
297 if (!render_texture_) {
298 LOG_DP <<
"creating offscreen render texture";
299 render_texture_.assign(SDL_CreateTexture(
301 window->pixel_format(),
302 SDL_TEXTUREACCESS_TARGET,
306 render_texture_.set_draw_size(lsize);
314 input_area_ = {{}, wsize};
317 if (active_area.
size() != osize) {
318 LOG_DP <<
"render target offset: LT " << active_area.
origin() <<
" RB "
319 << osize - active_area.
size() - active_area.
origin();
322 (active_area.
origin() * wsize) / osize,
323 (active_area.
origin() * wsize) / osize
325 LOG_DP <<
"input area: " << input_area_;
333 LOG_DP <<
"creating test window " << test_resolution_.x
334 <<
"x" << test_resolution_.y;
336 uint32_t window_flags = 0;
337 window_flags |= SDL_WINDOW_HIDDEN;
340 uint32_t renderer_flags = 0;
341 renderer_flags |= SDL_RENDERER_TARGETTEXTURE;
345 "", 0, 0, test_resolution_.x, test_resolution_.y,
346 window_flags, renderer_flags
355 const int x =
prefs::get().fullscreen() ? SDL_WINDOWPOS_UNDEFINED : SDL_WINDOWPOS_CENTERED;
356 const int y =
prefs::get().fullscreen() ? SDL_WINDOWPOS_UNDEFINED : SDL_WINDOWPOS_CENTERED;
363 uint32_t window_flags = 0;
366 window_flags |= SDL_WINDOW_RESIZABLE;
367 window_flags |= SDL_WINDOW_ALLOW_HIGHDPI;
370 window_flags |= SDL_WINDOW_FULLSCREEN_DESKTOP;
372 window_flags |= SDL_WINDOW_MAXIMIZED;
376 LOG_DP <<
"hiding main window";
377 window_flags |= SDL_WINDOW_HIDDEN;
380 uint32_t renderer_flags = SDL_RENDERER_ACCELERATED | SDL_RENDERER_TARGETTEXTURE;
384 renderer_flags |= SDL_RENDERER_PRESENTVSYNC;
388 window.reset(
new sdl::window(
"", x, y,
w,
h, window_flags, renderer_flags));
392 assert(!render_texture_);
398 SDL_DisplayMode currentDisplayMode;
399 SDL_GetCurrentDisplayMode(window->get_display_index(), ¤tDisplayMode);
400 refresh_rate_ = currentDisplayMode.refresh_rate != 0 ? currentDisplayMode.refresh_rate : 60;
413 return test_resolution_;
416 return window->get_output_size();
422 return test_resolution_;
424 return window->get_size();
429 return {0, 0, game_canvas_size_.x, game_canvas_size_.y};
434 return game_canvas_size_;
439 return current_render_target_.draw_size();
444 return {0, 0, current_render_target_.w(), current_render_target_.h()};
457 return (osize - (
scale * dsize)) / 2;
463 return {0, 0,
p.x,
p.y};
469 point dsize = current_render_target_.draw_size();
470 point osize = current_render_target_.get_raw_size();
489 return refresh_rate_;
495 ERR_DP <<
"failed to set render target to "
496 <<
static_cast<void*
>(
t.get()) <<
' '
497 <<
t.draw_size() <<
" / " <<
t.get_raw_size();
498 ERR_DP <<
"last SDL error: " << SDL_GetError();
499 throw error(
"failed to set render target");
501 current_render_target_ =
t;
510 DBG_DP <<
"rendering to window / screen";
511 window->set_logical_size(game_canvas_size_);
512 }
else if (
t == render_texture_) {
513 DBG_DP <<
"rendering to primary buffer";
514 window->set_logical_size(game_canvas_size_);
516 DBG_DP <<
"rendering to custom target "
517 <<
static_cast<void*
>(
t.get()) <<
' '
518 <<
t.draw_size() <<
" / " <<
t.get_raw_size();
519 window->set_logical_size(
t.w(),
t.h());
536 assert(current_render_target_ == SDL_GetRenderTarget(
get_renderer()));
537 return current_render_target_;
545 if(headless_ || testing_) {
551 WRN_DP <<
"trying to render with no window";
557 if(SDL_GetRenderTarget(*window) != render_texture_) {
558 ERR_DP <<
"trying to render screen, but current render texture is "
559 <<
static_cast<void*
>(SDL_GetRenderTarget(*window))
560 <<
" | " <<
static_cast<void*
>(current_render_target_.get())
561 <<
". It should be " <<
static_cast<void*
>(render_texture_.get());
562 throw error(
"tried to render screen from wrong render target");
569 SDL_RenderCopy(*window, render_texture_,
nullptr,
nullptr);
572 SDL_RenderPresent(*window);
581 WRN_DP <<
"trying to read pixels with no window";
586 texture& target = current_render_target_;
589 if (target != SDL_GetRenderTarget(*window)) {
590 SDL_Texture*
t = SDL_GetRenderTarget(*window);
591 ERR_DP <<
"render target " <<
static_cast<void*
>(target.
get())
593 <<
" doesn't match window render target "
594 <<
static_cast<void*
>(
t);
595 throw error(
"unexpected render target while reading pixels");
602 if (r_clipped != *r) {
603 DBG_DP <<
"modifying pixel read area from " << *r
604 <<
" to " << r_clipped;
614 SDL_RenderReadPixels(*window, &o,
s->format->format,
s->pixels,
s->pitch);
621 WRN_DP <<
"trying to read pixels with no window";
635 window->set_title(title);
641 window->set_icon(icon);
660 const char*
const drvname = SDL_GetCurrentVideoDriver();
661 return drvname ? drvname :
"<not initialized>";
666 std::vector<std::string> res;
667 int num_drivers = SDL_GetNumVideoDrivers();
669 for(
int n = 0;
n < num_drivers; ++
n) {
670 const char* drvname = SDL_GetVideoDriver(
n);
671 res.emplace_back(drvname ? drvname :
"<invalid driver>");
684 return window && (window->get_flags() & flags) != 0;
704 std::vector<point> result;
710 const int display_index = window->get_display_index();
712 const int modes = SDL_GetNumDisplayModes(display_index);
723 SDL_GetDisplayBounds(display_index, &bounds);
725 SDL_DisplayMode mode;
727 for(
int i = 0;
i < modes; ++
i) {
728 if(SDL_GetDisplayMode(display_index,
i, &mode) == 0) {
730 if(mode.w > bounds.w && mode.h > bounds.h) {
734 if(mode.w >= min_res.x && mode.h >= min_res.y) {
735 result.emplace_back(mode.w, mode.h);
740 if(std::find(result.begin(), result.end(), min_res) == result.end()) {
741 result.push_back(min_res);
744 if(include_current) {
748 std::sort(result.begin(), result.end());
749 result.erase(std::unique(result.begin(), result.end()), result.end());
757 return test_resolution_;
759 return point(window->get_size());
767 return (window->get_flags() & SDL_WINDOW_FULLSCREEN_DESKTOP) != 0;
772 if (headless_ || testing_) {
779 window->full_screen();
806 throw error(
"tried to set resolution with no window");
810 LOG_DP <<
"resizing test resolution to " << resolution;
811 test_resolution_ = resolution;
816 window->set_size(resolution.x, resolution.y);
822 LOG_DP <<
"updating resolution to " << resolution;
835 LOG_DP <<
"updating video buffers";
843 float hdpi = 0.0f, vdpi = 0.0f;
844 if(window && SDL_GetDisplayDPI(window->get_display_index(),
nullptr, &hdpi, &vdpi) == 0) {
853 hdpi /= scale_factor;
854 vdpi /= scale_factor;
858 return { hdpi, vdpi };
863 std::vector<std::pair<std::string, std::string>> res;
867 if(window && (rnd = *window) && SDL_GetRendererInfo(rnd, &ri) == 0) {
868 std::string renderer_name = ri.name ? ri.name :
"<unknown>";
870 if(ri.flags & SDL_RENDERER_SOFTWARE) {
871 renderer_name +=
" (sw)";
874 if(ri.flags & SDL_RENDERER_ACCELERATED) {
875 renderer_name +=
" (hw)";
878 std::string renderer_max = std::to_string(ri.max_texture_width) +
880 std::to_string(ri.max_texture_height);
882 res.emplace_back(
"Renderer", renderer_name);
883 res.emplace_back(
"Maximum texture size", renderer_max);
884 res.emplace_back(
"VSync", ri.flags & SDL_RENDERER_PRESENTVSYNC ?
"on" :
"off");
void set_resolution(const point &res)
The wrapper class for the SDL_Window class.
Wrapper class to encapsulate creation and management of an SDL_Texture.
point draw_size() const
The size of the texture in draw-space.
point get_raw_size() const
The raw internal texture size.
SDL_Texture * get() const
Represents version numbers.
Interfaces for manipulating version numbers of engine, add-ons, etc.
Standard logging facilities (interface).
CGFloat get_scale_factor(int display_index)
void invalidate_all()
Mark the entire screen as requiring redraw.
void point(int x, int y)
Draw a single point.
void flush_texture_cache()
Flush the rendered text cache.
void flush_cache()
Purges all image caches.
const int min_window_height
const int max_pixel_scale
const int def_window_width
const int min_window_width
const int max_window_height
const int max_window_width
const int def_window_height
version_info get_version()
Returns the runtime SDL version.
std::size_t size(const std::string &str)
Length in characters of a UTF-8 string.
bool window_has_mouse_focus()
True iff the window has mouse focus.
bool headless()
The game is running headless.
rect draw_area()
The current drawable area.
rect output_area()
{0, 0, output_size().x, output_size().y}
void set_window_title(const std::string &title)
Sets the title of the main window.
void clear_render_target()
Reset the render target to the main window / screen.
rect to_output(const rect &r)
Convert coordinates in draw space to coordinates in render space.
std::vector< std::pair< std::string, std::string > > renderer_report()
Provides diagnostic information about the current renderer for the build_info API.
static bool window_has_flags(uint32_t flags)
Tests whether the given flags are currently set on the SDL window.
static bool update_framebuffer()
void reset_render_target()
Reset the render target to the primary render buffer.
point output_size()
Returns the size of the final render target.
std::vector< point > get_available_resolutions(const bool include_current)
Returns the list of available screen resolutions.
static bool update_test_framebuffer()
Returns true if the buffer was changed.
void set_window_icon(surface &icon)
Sets the icon of the main window.
bool window_is_visible()
True iff the window is not hidden.
surface read_pixels_low_res(SDL_Rect *r)
The same as read_pixels, but returns a low-resolution surface suitable for use with the old drawing s...
surface read_pixels(SDL_Rect *r)
Copy back a portion of the render target that is already drawn.
void force_render_target(const texture &t)
Set the render target, without any provided way of setting it back.
point game_canvas_size()
The size of the game canvas, in drawing coordinates / game pixels.
SDL_Window * get_window()
bool window_has_focus()
True iff the window has mouse or input focus.
point window_size()
Returns the size of the window in display units / screen coordinates.
bool has_window()
Whether the game has set up a window to render into.
bool testing()
The game is running unit tests.
bool is_fullscreen()
Whether we are currently in fullscreen mode.
std::vector< std::string > enumerate_drivers()
A list of available video drivers.
rect game_canvas()
The game canvas area, in drawing coordinates.
std::pair< float, float > get_dpi()
Retrieves the current game screen DPI for the build_info API.
static point draw_offset()
int get_pixel_scale()
Get the current active pixel scale multiplier.
static void init_window(bool hidden=false)
point current_resolution()
The current window size in desktop coordinates.
void init(fake type)
Initialize the video subsystem.
void set_fullscreen(bool fullscreen)
Set the fullscreen state.
void deinit()
Deinitialize the video subsystem.
bool set_resolution(const point &resolution)
Set the window resolution.
int current_refresh_rate()
The refresh rate of the screen.
std::string current_driver()
The current video driver in use, or else "<not initialized>".
void toggle_fullscreen()
Toggle fullscreen mode.
SDL_Renderer * get_renderer()
void update_buffers(bool autoupdate)
Update buffers to match current resolution and pixel scale settings.
texture get_render_target()
Get the current render target.
fake
For describing the type of faked display, if any.
static void init_test_window()
point draw_size()
The size of the current render target in drawing coordinates.
rect input_area()
Returns the input area of the window, in display coordinates.
void scale(size_t factor, const uint32_t *src, uint32_t *trg, int srcWidth, int srcHeight, ColorFormat colFmt, const ScalerCfg &cfg=ScalerCfg(), int yFirst=0, int yLast=std::numeric_limits< int >::max())
Contains a wrapper class for the SDL_Window class.
An abstract description of a rectangle with integer coordinates.
constexpr point origin() const
void clip(const SDL_Rect &r)
Clip this rectangle by the given rectangle.
constexpr point size() const
An error specifically indicating video subsystem problems.
static map_location::direction n
static map_location::direction s
surface scale_surface(const surface &surf, int w, int h)
Scale a surface using alpha-weighted modified bilinear filtering Note: causes artifacts with alpha gr...
static lg::log_domain log_display("display")