Blender  V3.3
GHOST_DisplayManagerSDL.cpp
Go to the documentation of this file.
1 /* SPDX-License-Identifier: GPL-2.0-or-later
2  * Mode switching
3  * Copyright 1997-2001 Id Software, Inc.
4  * Copyright 1993-2011 Tim Riker
5  * Copyright 2012 Alex Fraser */
6 
12 #include "GHOST_SystemSDL.h"
13 
14 #include "GHOST_WindowManager.h"
15 
17  : GHOST_DisplayManager(), m_system(system)
18 {
19  memset(&m_mode, 0, sizeof(m_mode));
20 }
21 
23 {
24  numDisplays = SDL_GetNumVideoDisplays();
25  return GHOST_kSuccess;
26 }
27 
29  int32_t &numSettings) const
30 {
31  GHOST_ASSERT(display < 1, "Only single display systems are currently supported.\n");
32 
33  numSettings = SDL_GetNumDisplayModes(display - 1);
34 
35  return GHOST_kSuccess;
36 }
37 
38 static void ghost_mode_from_sdl(GHOST_DisplaySetting &setting, SDL_DisplayMode *mode)
39 {
40  setting.xPixels = mode->w;
41  setting.yPixels = mode->h;
42  setting.bpp = SDL_BYTESPERPIXEL(mode->format) * 8;
43  /* Just guess the frequency :( */
44  setting.frequency = mode->refresh_rate ? mode->refresh_rate : 60;
45 }
46 
47 static void ghost_mode_to_sdl(const GHOST_DisplaySetting &setting, SDL_DisplayMode *mode)
48 {
49  mode->w = setting.xPixels;
50  mode->h = setting.yPixels;
51  // setting.bpp = SDL_BYTESPERPIXEL(mode->format) * 8; ???
52  mode->refresh_rate = setting.frequency;
53 }
54 
56  int32_t index,
57  GHOST_DisplaySetting &setting) const
58 {
59  GHOST_ASSERT(display < 1, "Only single display systems are currently supported.\n");
60 
61  SDL_DisplayMode mode;
62  SDL_GetDisplayMode(display, index, &mode);
63 
64  ghost_mode_from_sdl(setting, &mode);
65 
66  return GHOST_kSuccess;
67 }
68 
70  uint8_t display, GHOST_DisplaySetting &setting) const
71 {
72  SDL_DisplayMode mode;
73  SDL_GetCurrentDisplayMode(display, &mode);
74 
75  ghost_mode_from_sdl(setting, &mode);
76 
77  return GHOST_kSuccess;
78 }
79 
81 {
82  mode = m_mode;
83  return GHOST_kSuccess;
84 }
85 
87  uint8_t display, const GHOST_DisplaySetting &setting)
88 {
89  /*
90  * Mode switching code ported from Quake 2 version 3.21 and BZFLAG version 2.4.0:
91  * ftp://ftp.idsoftware.com/idstuff/source/q2source-3.21.zip
92  * See linux/gl_glx.c:GLimp_SetMode
93  * http://wiki.bzflag.org/BZFlag_Source
94  * See: `src/platform/SDLDisplay.cxx:SDLDisplay` and `createWindow`.
95  */
96  SDL_DisplayMode mode;
97  const int num_modes = SDL_GetNumDisplayModes(display);
98  int best_fit, best_dist, dist, x, y;
99 
100  best_dist = 9999999;
101  best_fit = -1;
102 
103  if (num_modes == 0) {
104  /* Any mode is OK. */
105  ghost_mode_to_sdl(setting, &mode);
106  }
107  else {
108  for (int i = 0; i < num_modes; i++) {
109 
110  SDL_GetDisplayMode(display, i, &mode);
111 
112  if ((int)setting.xPixels > mode.w || (int)setting.yPixels > mode.h) {
113  continue;
114  }
115 
116  x = setting.xPixels - mode.w;
117  y = setting.yPixels - mode.h;
118  dist = (x * x) + (y * y);
119  if (dist < best_dist) {
120  best_dist = dist;
121  best_fit = i;
122  }
123  }
124 
125  if (best_fit == -1) {
126  return GHOST_kFailure;
127  }
128  SDL_GetDisplayMode(display, best_fit, &mode);
129  }
130 
131  m_mode = mode;
132 
133  /* evil, SDL2 needs a window to adjust display modes */
135 
136  if (win) {
137  SDL_Window *sdl_win = win->getSDLWindow();
138 
139  SDL_SetWindowDisplayMode(sdl_win, &mode);
140  SDL_ShowWindow(sdl_win);
141  SDL_SetWindowFullscreen(sdl_win, SDL_TRUE);
142 
143  return GHOST_kSuccess;
144  }
145  /* This is a problem for the BGE player :S, perhaps SDL2 will resolve at some point.
146  * we really need SDL_SetDisplayModeForDisplay() to become an API func! - campbell. */
147  printf("no windows available, can't fullscreen\n");
148 
149  /* do not fail, we will try again later when the window is created - wander */
150  return GHOST_kSuccess;
151 }
#define GHOST_ASSERT(x, info)
Definition: GHOST_Debug.h:54
static void ghost_mode_to_sdl(const GHOST_DisplaySetting &setting, SDL_DisplayMode *mode)
static void ghost_mode_from_sdl(GHOST_DisplaySetting &setting, SDL_DisplayMode *mode)
GHOST_TSuccess
Definition: GHOST_Types.h:74
@ GHOST_kFailure
Definition: GHOST_Types.h:74
@ GHOST_kSuccess
Definition: GHOST_Types.h:74
_GL_VOID GLfloat value _GL_VOID_RET _GL_VOID const GLuint GLboolean *residences _GL_BOOL_RET _GL_VOID GLsizei GLfloat GLfloat GLfloat GLfloat const GLubyte *bitmap _GL_VOID_RET _GL_VOID GLenum const void *lists _GL_VOID_RET _GL_VOID const GLdouble *equation _GL_VOID_RET _GL_VOID GLdouble GLdouble blue _GL_VOID_RET _GL_VOID GLfloat GLfloat blue _GL_VOID_RET _GL_VOID GLint GLint blue _GL_VOID_RET _GL_VOID GLshort GLshort blue _GL_VOID_RET _GL_VOID GLubyte GLubyte blue _GL_VOID_RET _GL_VOID GLuint GLuint blue _GL_VOID_RET _GL_VOID GLushort GLushort blue _GL_VOID_RET _GL_VOID GLbyte GLbyte GLbyte alpha _GL_VOID_RET _GL_VOID GLdouble GLdouble GLdouble alpha _GL_VOID_RET _GL_VOID GLfloat GLfloat GLfloat alpha _GL_VOID_RET _GL_VOID GLint GLint GLint alpha _GL_VOID_RET _GL_VOID GLshort GLshort GLshort alpha _GL_VOID_RET _GL_VOID GLubyte GLubyte GLubyte alpha _GL_VOID_RET _GL_VOID GLuint GLuint GLuint alpha _GL_VOID_RET _GL_VOID GLushort GLushort GLushort alpha _GL_VOID_RET _GL_VOID GLenum mode _GL_VOID_RET _GL_VOID GLint y
GHOST_TSuccess getDisplaySetting(uint8_t display, int32_t index, GHOST_DisplaySetting &setting) const
GHOST_TSuccess setCurrentDisplaySetting(uint8_t display, const GHOST_DisplaySetting &setting)
GHOST_TSuccess getCurrentDisplayModeSDL(SDL_DisplayMode &mode) const
GHOST_TSuccess getNumDisplays(uint8_t &numDisplays) const
GHOST_TSuccess getNumDisplaySettings(uint8_t display, int32_t &numSettings) const
GHOST_TSuccess getCurrentDisplaySetting(uint8_t display, GHOST_DisplaySetting &setting) const
GHOST_DisplayManagerSDL(GHOST_SystemSDL *system)
GHOST_WindowManager * getWindowManager() const
Definition: GHOST_System.h:432
GHOST_IWindow * getActiveWindow(void) const
SDL_Window * getSDLWindow()
signed int int32_t
Definition: stdint.h:77
unsigned char uint8_t
Definition: stdint.h:78