32 #define LOG_DP LOG_STREAM(info, log_display) 33 #define ERR_DP LOG_STREAM(err, log_display) 35 #define MAGIC_DPI_SCALE_NUMBER 96 42 bool fake_interactive =
false;
50 : sdl_handler(auto_join)
52 draw_layers.push_back(
this);
57 draw_layers.remove(
this);
65 event.type = SDL_WINDOWEVENT;
66 event.window.event = SDL_WINDOWEVENT_RESIZED;
67 event.window.data1 = (*frameBuffer).h;
68 event.window.data2 = (*frameBuffer).w;
70 for(
const auto& layer : draw_layers) {
71 layer->handle_window_event(event);
78 drawEvent.user = data;
80 SDL_PushEvent(&drawEvent);
112 const int res = SDL_InitSubSystem(SDL_INIT_VIDEO);
115 ERR_DP <<
"Could not initialize SDL_video: " << SDL_GetError() << std::endl;
125 frameBuffer.clear_without_free();
128 LOG_DP <<
"calling SDL_Quit()\n";
132 LOG_DP <<
"called SDL_Quit()\n";
137 return fake_interactive ? false : (
window ==
nullptr);
142 if(event.type == SDL_WINDOWEVENT) {
143 switch(event.window.event) {
144 case SDL_WINDOWEVENT_RESIZED:
145 case SDL_WINDOWEVENT_RESTORED:
146 case SDL_WINDOWEVENT_SHOWN:
147 case SDL_WINDOWEVENT_EXPOSED:
154 drawEvent.user = data;
157 SDL_PushEvent(&drawEvent);
166 SDL_Rect dst{x, y, 0, 0};
168 const clip_rect_setter clip_setter(target, clip_rect, clip_rect !=
nullptr);
169 sdl_blit(surf, srcrect, target, &dst);
177 #if SDL_VERSION_ATLEAST(2, 0, 6) 178 frameBuffer = SDL_CreateRGBSurfaceWithFormat(0, 16, 16, 24, SDL_PIXELFORMAT_BGR888);
180 frameBuffer = SDL_CreateRGBSurface(0, 16, 16, 24, 0xFF0000, 0xFF00, 0xFF, 0);
188 #if SDL_VERSION_ATLEAST(2, 0, 6) 189 frameBuffer = SDL_CreateRGBSurfaceWithFormat(0, width, height, 32, SDL_PIXELFORMAT_BGR888);
191 frameBuffer = SDL_CreateRGBSurface(0, width, height, 32, 0xFF0000, 0xFF00, 0xFF, 0);
196 fake_interactive =
true;
216 frameBuffer.assign(fb);
231 uint32_t window_flags = 0;
234 window_flags |= SDL_WINDOW_RESIZABLE;
236 window_flags |= SDL_WINDOW_ALLOW_HIGHDPI;
240 window_flags |= SDL_WINDOW_FULLSCREEN_DESKTOP;
242 window_flags |= SDL_WINDOW_MAXIMIZED;
246 window.reset(
new sdl::window(
"", x, y, w, h, window_flags, SDL_RENDERER_SOFTWARE));
248 std::cerr <<
"Setting mode to " << w <<
"x" << h << std::endl;
252 SDL_DisplayMode currentDisplayMode;
253 SDL_GetCurrentDisplayMode(
window->get_display_index(), ¤tDisplayMode);
254 refresh_rate_ = currentDisplayMode.refresh_rate != 0 ? currentDisplayMode.refresh_rate : 60;
302 return {0, 0, frameBuffer->w, frameBuffer->h};
310 float scale_x, scale_y;
317 return {0, 0, size.x, size.y};
333 SDL_Delay(milliseconds);
380 window->fill(0, 0, 0, 255);
394 return (
window->get_flags() & flags) != 0;
399 std::pair<float, float> result{1.0f, 1.0f};
406 SDL_GetDisplayDPI(
window->get_display_index(),
nullptr, &hdpi, &vdpi);
416 std::vector<point> result;
422 const int display_index =
window->get_display_index();
424 const int modes = SDL_GetNumDisplayModes(display_index);
426 std::cerr <<
"No modes supported\n";
434 float scale_h, scale_v;
441 SDL_GetDisplayBounds(display_index, &bounds);
443 SDL_DisplayMode mode;
445 for(
int i = 0;
i < modes; ++
i) {
446 if(SDL_GetDisplayMode(display_index,
i, &mode) == 0) {
448 if(mode.w > bounds.w && mode.h > bounds.h) {
452 if(mode.w >= min_res.
x && mode.h >= min_res.
y) {
453 result.emplace_back(mode.w, mode.h);
458 if(
std::find(result.begin(), result.end(), min_res) == result.end()) {
459 result.push_back(min_res);
462 if(include_current) {
467 result.erase(std::unique(result.begin(), result.end()), result.end());
484 return (
window->get_flags() & SDL_WINDOW_FULLSCREEN_DESKTOP) != 0;
491 const color_t color{0, 0, 0, 0xbb};
503 const int border = 5;
548 d->redraw_everything();
575 d->redraw_everything();
void raise_resize_event()
void set_window_icon(surface &icon)
Sets the icon of the main window.
void _set_fullscreen(bool ison)
draw_layering(const bool auto_join=true)
bool update_locked() const
Whether the screen has been 'locked' or not.
point current_resolution()
static display * get_singleton()
Returns the display object if a display object exists.
const int min_window_height
std::list< events::sdl_handler * > draw_layers
void set_pixel_format(SDL_PixelFormat *format)
sets the pixel format used by the images.
void _set_maximized(bool ison)
std::pair< float, float > get_dpi_scale_factor() const
The current scale factor on High-DPI screens.
static l_noret error(LoadState *S, const char *why)
const int min_window_width
int help_string_
Curent ID of the help string.
void flip()
Renders the screen.
void lock_updates(bool value)
Stop the screen being redrawn.
void remove_floating_label(int handle)
removes the floating label given by 'handle' from the screen
void _set_resolution(const point &res)
static CVideo * singleton_
bool non_interactive() const
void set_font_size(int font_size)
void blit_surface(int x, int y, surface surf, SDL_Rect *srcrect=nullptr, SDL_Rect *clip_rect=nullptr)
Draws a surface directly onto the screen framebuffer.
surface & getSurface()
Returns a reference to the framebuffer.
void move_floating_label(int handle, double xmove, double ymove)
moves the floating label given by 'handle' by (xmove,ymove)
bool set_resolution(const unsigned width, const unsigned height)
void set_window_title(const std::string &title)
Sets the title of the main window.
void initSDL()
Initializes the SDL video subsystem.
std::size_t size(const std::string &str)
Length in characters of a UTF-8 string.
The wrapper class for the SDL_Window class.
void make_test_fake(const unsigned width=1024, const unsigned height=768)
Creates a fake frame buffer for the unit tests.
static lg::log_domain log_display("display")
void set_bg_color(const color_t &bg_color)
void set_position(double xpos, double ypos)
map_display and display: classes which take care of displaying the map and game-data on the screen...
sdl::window * get_window()
Returns a pointer to the underlying SDL window.
int get_width(bool as_pixels=true) const
Returns the window renderer width in pixels or screen coordinates.
virtual void handle_window_event(const SDL_Event &event)
void init_window()
Initializes a new SDL window instance, taking into account any preiously saved states.
int add_floating_label(const floating_label &flabel)
add a label floating on the screen above everything else.
void set_fullscreen(bool ison)
SDL_Rect get_floating_label_rect(int handle)
int get_height(bool as_pixels=true) const
Returns the window renderer height in pixels or in screen coordinates.
static int sort(lua_State *L)
version_info sdl_get_version()
void set_border_size(int border)
Represents version numbers.
int set_help_string(const std::string &str)
Displays a help string with the given text.
#define MAGIC_DPI_SCALE_NUMBER
void clear_without_free()
bool is_fullscreen() const
bool find(E event, F functor)
Tests whether an event handler is available.
void trigger_full_redraw()
Standard logging facilities (interface).
video_event_handler event_handler_
void clear_all_help_strings()
Removes all help strings.
static void delay(unsigned int milliseconds)
Waits a given number of milliseconds before returning.
void set_window_mode(const MODE_EVENT mode, const point &size)
Sets the window's mode - ie, changing it to fullscreen, maximizing, etc.
Contains a wrapper class for the SDL_Window class.
void update_framebuffer()
Updates and ensures the framebuffer surface is valid.
void sdl_blit(const surface &src, SDL_Rect *src_rect, surface &dst, SDL_Rect *dst_rect)
CVideo(const CVideo &)=delete
SDL_Rect screen_area(bool as_pixels=true) const
Returns the current window renderer area, either in pixels or screen coordinates. ...
void clear_screen()
Clear the screen contents.
std::shared_ptr< halo_record > handle
std::vector< point > get_available_resolutions(const bool include_current=false)
Returns the list of available screen resolutions.
bool window_has_flags(uint32_t flags) const
Tests whether the given flags are currently set on the SDL window.
virtual void join_global()
int line_width(const std::string &line, int font_size, int style)
Determine the width of a line of text given a certain font size.
std::unique_ptr< sdl::window > window
The SDL window object.
void clear_help_string(int handle)
Removes the help string with the given handle.