| 1 |
# Copyright 2014 Patrick Dawson <[email protected]> |
| 2 |
# Copyright 2014 Tom Rothamel <[email protected]> |
| 3 |
# |
| 4 |
# This software is provided 'as-is', without any express or implied |
| 5 |
# warranty. In no event will the authors be held liable for any damages |
| 6 |
# arising from the use of this software. |
| 7 |
# |
| 8 |
# Permission is granted to anyone to use this software for any purpose, |
| 9 |
# including commercial applications, and to alter it and redistribute it |
| 10 |
# freely, subject to the following restrictions: |
| 11 |
# |
| 12 |
# 1. The origin of this software must not be misrepresented; you must not |
| 13 |
# claim that you wrote the original software. If you use this software |
| 14 |
# in a product, an acknowledgment in the product documentation would be |
| 15 |
# appreciated but is not required. |
| 16 |
# 2. Altered source versions must be plainly marked as such, and must not be |
| 17 |
# misrepresented as being the original software. |
| 18 |
# 3. This notice may not be removed or altered from any source distribution. |
| 19 |
|
| 20 |
from sdl2 cimport * |
| 21 |
from pygame_sdl2.surface cimport * |
| 22 |
from pygame_sdl2.rect cimport to_sdl_rect |
| 23 |
|
| 24 |
from libc.stdlib cimport calloc, free |
| 25 |
from pygame_sdl2.locals import SRCALPHA, GL_SWAP_CONTROL |
| 26 |
from pygame_sdl2.error import error |
| 27 |
import pygame_sdl2 |
| 28 |
|
| 29 |
import warnings |
| 30 |
import os |
| 31 |
|
| 32 |
# True if we are on ios. |
| 33 |
ios = ("PYGAME_IOS" in os.environ) |
| 34 |
|
| 35 |
# This inits SDL proper, and should be called by the other init methods. |
| 36 |
|
| 37 |
# A map from a PYGAME_SDL2 hint to what it was set to. |
| 38 |
_pygame_hints = { } |
| 39 |
|
| 40 |
def hint(hint, value, priority=1): |
| 41 |
|
| 42 |
if str(hint).startswith("PYGAME_SDL2"): |
| 43 |
_pygame_hints[str(hint)] = str(value) |
| 44 |
return |
| 45 |
|
| 46 |
if not isinstance(hint, bytes): |
| 47 |
hint = hint.encode("utf-8") |
| 48 |
|
| 49 |
if not isinstance(value, bytes): |
| 50 |
value = value.encode("utf-8") |
| 51 |
|
| 52 |
SDL_SetHintWithPriority(hint, value, priority) |
| 53 |
|
| 54 |
def _get_hint(hint, default): |
| 55 |
hint = str(hint) |
| 56 |
|
| 57 |
if hint in _pygame_hints: |
| 58 |
return _pygame_hints[hint] |
| 59 |
|
| 60 |
if hint in os.environ: |
| 61 |
return os.environ[hint] |
| 62 |
|
| 63 |
return default |
| 64 |
|
| 65 |
|
| 66 |
main_done = False |
| 67 |
|
| 68 |
def sdl_main_init(): |
| 69 |
global main_done |
| 70 |
|
| 71 |
if main_done: |
| 72 |
return |
| 73 |
|
| 74 |
SDL_SetMainReady() |
| 75 |
|
| 76 |
if SDL_Init(0): |
| 77 |
raise error() |
| 78 |
|
| 79 |
main_done = True |
| 80 |
|
| 81 |
# True if init has been called without quit being called. |
| 82 |
init_done = False |
| 83 |
|
| 84 |
@pygame_sdl2.register_init |
| 85 |
def init(): |
| 86 |
|
| 87 |
if init_done: |
| 88 |
return |
| 89 |
|
| 90 |
sdl_main_init() |
| 91 |
|
| 92 |
if SDL_InitSubSystem(SDL_INIT_VIDEO): |
| 93 |
raise error() |
| 94 |
|
| 95 |
pygame_sdl2.event.init() |
| 96 |
|
| 97 |
global init_done |
| 98 |
init_done = True |
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
@pygame_sdl2.register_quit |
| 103 |
def quit(): # @ReservedAssignment |
| 104 |
|
| 105 |
global init_done |
| 106 |
global main_window |
| 107 |
|
| 108 |
if main_window: |
| 109 |
main_window.destroy() |
| 110 |
main_window = None |
| 111 |
|
| 112 |
|
| 113 |
SDL_QuitSubSystem(SDL_INIT_VIDEO) |
| 114 |
|
| 115 |
init_done = False |
| 116 |
|
| 117 |
def get_init(): |
| 118 |
return init_done |
| 119 |
|
| 120 |
|
| 121 |
# The window that is used by the various module globals. |
| 122 |
main_window = None |
| 123 |
|
| 124 |
try: |
| 125 |
import androidembed |
| 126 |
except ImportError: |
| 127 |
androidembed = None |
| 128 |
|
| 129 |
|
| 130 |
cdef class Window: |
| 131 |
def __init__(self, title, resolution=(0, 0), flags=0, depth=0, pos=(SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED), Surface shape=None): |
| 132 |
cdef SDL_WindowShapeMode shape_mode |
| 133 |
|
| 134 |
if not isinstance(title, bytes): |
| 135 |
title = title.encode("utf-8") |
| 136 |
|
| 137 |
self.create_flags = flags |
| 138 |
|
| 139 |
# If we do not get the AVOID_GL hint, we always create a GL-compatible |
| 140 |
# window. This lets us change the OPENGL flag later on. |
| 141 |
if int(_get_hint("PYGAME_SDL2_AVOID_GL", "0")): |
| 142 |
gl_flag = 0 |
| 143 |
else: |
| 144 |
gl_flag = SDL_WINDOW_OPENGL |
| 145 |
|
| 146 |
self.window = NULL |
| 147 |
|
| 148 |
if androidembed is not None: |
| 149 |
self.window = SDL_GL_GetCurrentWindow() |
| 150 |
|
| 151 |
if self.window: |
| 152 |
|
| 153 |
# Android bug - a RGB_565 format is likely to mean the window |
| 154 |
# wasn't created properly, so re-make it. |
| 155 |
if SDL_GetWindowPixelFormat(self.window) == SDL_PIXELFORMAT_RGB565: |
| 156 |
SDL_DestroyWindow(self.window) |
| 157 |
self.window = NULL |
| 158 |
else: |
| 159 |
SDL_SetWindowTitle(self.window, title) |
| 160 |
|
| 161 |
if not self.window: |
| 162 |
|
| 163 |
flags |= SDL_WINDOW_HIDDEN |
| 164 |
|
| 165 |
if shape is not None: |
| 166 |
|
| 167 |
shape_mode.mode = ShapeModeDefault |
| 168 |
|
| 169 |
self.window = SDL_CreateShapedWindow( |
| 170 |
title, |
| 171 |
pos[0], pos[1], |
| 172 |
resolution[0], resolution[1], flags | gl_flag) |
| 173 |
|
| 174 |
SDL_SetWindowShape(self.window, shape.surface, &shape_mode) |
| 175 |
|
| 176 |
else: |
| 177 |
|
| 178 |
self.window = SDL_CreateWindow( |
| 179 |
title, |
| 180 |
pos[0], pos[1], |
| 181 |
resolution[0], resolution[1], flags | gl_flag) |
| 182 |
|
| 183 |
if pos != (SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED): |
| 184 |
SDL_SetWindowPosition(self.window, pos[0], pos[1]) |
| 185 |
|
| 186 |
SDL_ShowWindow(self.window) |
| 187 |
|
| 188 |
if not self.window: |
| 189 |
raise error() |
| 190 |
|
| 191 |
# From here on, the window exists. So we have to call self.destroy if |
| 192 |
# an exception occurs. |
| 193 |
|
| 194 |
try: |
| 195 |
|
| 196 |
if flags & SDL_WINDOW_OPENGL: |
| 197 |
|
| 198 |
self.gl_context = SDL_GL_CreateContext(self.window) |
| 199 |
|
| 200 |
if self.gl_context == NULL: |
| 201 |
raise error() |
| 202 |
|
| 203 |
SDL_GL_MakeCurrent(self.window, self.gl_context) |
| 204 |
|
| 205 |
if not ios: |
| 206 |
# Try setting the swap interval - first positive, then negated |
| 207 |
# to deal with the case where the negative interval isn't |
| 208 |
# supported. Then give up and carry on. |
| 209 |
if SDL_GL_SetSwapInterval(default_swap_control): |
| 210 |
if default_swap_control < 0: |
| 211 |
SDL_GL_SetSwapInterval(-default_swap_control) |
| 212 |
|
| 213 |
self.create_surface() |
| 214 |
|
| 215 |
except: |
| 216 |
self.destroy() |
| 217 |
raise |
| 218 |
|
| 219 |
def create_surface(self): |
| 220 |
""" |
| 221 |
Creates the surface associated with this window. |
| 222 |
""" |
| 223 |
|
| 224 |
cdef int w, h |
| 225 |
SDL_GetWindowSize(self.window, &w, &h) |
| 226 |
|
| 227 |
if self.gl_context: |
| 228 |
|
| 229 |
# For now, make this the size of the window so get_size() works. |
| 230 |
# TODO: Make this a bit less wasteful of memory, even if it means |
| 231 |
# we lie about the actual size of the pixel array. |
| 232 |
self.surface = Surface((w, h), SRCALPHA, 32) |
| 233 |
|
| 234 |
else: |
| 235 |
|
| 236 |
self.window_surface = SDL_GetWindowSurface(self.window) |
| 237 |
|
| 238 |
# If the surface is 32-bit, we can use it directly. Otherwise, |
| 239 |
# we need to create a 32-bit proxy surface. |
| 240 |
if self.window_surface.format.BitsPerPixel == 32: |
| 241 |
|
| 242 |
self.surface = Surface(()) |
| 243 |
self.surface.surface = self.window_surface |
| 244 |
self.surface.owns_surface = False |
| 245 |
self.surface.window_surface = True |
| 246 |
|
| 247 |
else: |
| 248 |
self.surface = Surface((w, h), 0, 32) |
| 249 |
|
| 250 |
self.surface.get_window_flags = self.get_window_flags |
| 251 |
|
| 252 |
|
| 253 |
def destroy(self): |
| 254 |
""" |
| 255 |
This should be called before the window is deleted. |
| 256 |
""" |
| 257 |
|
| 258 |
if self.gl_context != NULL: |
| 259 |
SDL_GL_DeleteContext(self.gl_context) |
| 260 |
|
| 261 |
if self.surface: |
| 262 |
|
| 263 |
# Break the cycle that prevents refcounting from collecting this |
| 264 |
# object. |
| 265 |
self.surface.get_window_flags = None |
| 266 |
|
| 267 |
# Necessary to collect the GL surface, doesn't hurt the window surface. |
| 268 |
self.surface = None |
| 269 |
|
| 270 |
SDL_DestroyWindow(self.window) |
| 271 |
|
| 272 |
def resize(self, size, opengl=False, fullscreen=None, maximized=None): |
| 273 |
""" |
| 274 |
Resizes the window to `size`, which must be a width, height tuple. If opengl |
| 275 |
is true, adds an OpenGL context, if it's missing. Otherwise, removes the |
| 276 |
opengl context if present. |
| 277 |
""" |
| 278 |
|
| 279 |
flags = SDL_GetWindowFlags(self.window) |
| 280 |
|
| 281 |
if fullscreen is None: |
| 282 |
fullscreen = flags & SDL_WINDOW_FULLSCREEN_DESKTOP |
| 283 |
|
| 284 |
if maximized is None: |
| 285 |
maximized = flags & SDL_WINDOW_MAXIMIZED |
| 286 |
|
| 287 |
if fullscreen: |
| 288 |
maximized = False |
| 289 |
|
| 290 |
# Prevents a loop between the surface and this object. |
| 291 |
self.surface.get_window_flags = None |
| 292 |
|
| 293 |
if self.gl_context and not opengl: |
| 294 |
SDL_GL_DeleteContext(self.gl_context) |
| 295 |
self.gl_context = NULL |
| 296 |
|
| 297 |
cdef int cur_width = 0 |
| 298 |
cdef int cur_height = 0 |
| 299 |
|
| 300 |
if (not fullscreen) and (not maximized) and (flags & SDL_WINDOW_MAXIMIZED): |
| 301 |
SDL_RestoreWindow(self.window) |
| 302 |
|
| 303 |
if fullscreen: |
| 304 |
|
| 305 |
if SDL_SetWindowFullscreen(self.window, SDL_WINDOW_FULLSCREEN_DESKTOP): |
| 306 |
fullscreen = False |
| 307 |
|
| 308 |
if not fullscreen: |
| 309 |
SDL_SetWindowFullscreen(self.window, 0) |
| 310 |
|
| 311 |
if (not fullscreen) and (not maximized): |
| 312 |
|
| 313 |
width, height = size |
| 314 |
|
| 315 |
SDL_GetWindowSize(self.window, &cur_width, &cur_height) |
| 316 |
|
| 317 |
if (cur_width != width) or (cur_height != height): |
| 318 |
SDL_SetWindowSize(self.window, width, height) |
| 319 |
|
| 320 |
if maximized: |
| 321 |
SDL_MaximizeWindow(self.window) |
| 322 |
|
| 323 |
# Create a missing GL context. |
| 324 |
if opengl and not self.gl_context: |
| 325 |
self.gl_context = SDL_GL_CreateContext(self.window) |
| 326 |
|
| 327 |
if self.gl_context == NULL: |
| 328 |
raise error() |
| 329 |
|
| 330 |
self.create_surface() |
| 331 |
|
| 332 |
def recreate_gl_context(self, always=False): |
| 333 |
""" |
| 334 |
Check to see if the GL context was lost, and re-create it if it was. |
| 335 |
""" |
| 336 |
|
| 337 |
if not always: |
| 338 |
if <unsigned long> SDL_GL_GetCurrentContext(): |
| 339 |
return False |
| 340 |
|
| 341 |
self.gl_context = SDL_GL_CreateContext(self.window) |
| 342 |
|
| 343 |
if self.gl_context == NULL: |
| 344 |
raise error() |
| 345 |
|
| 346 |
SDL_GL_MakeCurrent(self.window, self.gl_context) |
| 347 |
|
| 348 |
return True |
| 349 |
|
| 350 |
def get_window_flags(self): |
| 351 |
rv = SDL_GetWindowFlags(self.window) |
| 352 |
|
| 353 |
if self.gl_context: |
| 354 |
rv |= SDL_WINDOW_OPENGL |
| 355 |
else: |
| 356 |
rv &= ~SDL_WINDOW_OPENGL |
| 357 |
|
| 358 |
return rv |
| 359 |
|
| 360 |
def proxy_window_surface(self): |
| 361 |
SDL_UpperBlit(self.surface.surface, NULL, self.window_surface, NULL) |
| 362 |
|
| 363 |
def flip(self): |
| 364 |
cdef const char *err |
| 365 |
|
| 366 |
if self.gl_context != NULL: |
| 367 |
with nogil: |
| 368 |
SDL_ClearError(); |
| 369 |
|
| 370 |
SDL_GL_SwapWindow(self.window) |
| 371 |
|
| 372 |
err = SDL_GetError() |
| 373 |
|
| 374 |
if err[0]: |
| 375 |
raise error(err) |
| 376 |
|
| 377 |
else: |
| 378 |
|
| 379 |
if self.surface.surface != self.window_surface: |
| 380 |
self.proxy_window_surface() |
| 381 |
|
| 382 |
with nogil: |
| 383 |
SDL_UpdateWindowSurface(self.window) |
| 384 |
|
| 385 |
def get_surface(self): |
| 386 |
return self.surface |
| 387 |
|
| 388 |
def update(self, rectangles=None): |
| 389 |
|
| 390 |
cdef SDL_Rect *rects |
| 391 |
cdef int count = 0 |
| 392 |
|
| 393 |
if rectangles is None: |
| 394 |
self.flip() |
| 395 |
return |
| 396 |
|
| 397 |
if self.surface.surface != self.window_surface: |
| 398 |
self.proxy_window_surface() |
| 399 |
|
| 400 |
if not isinstance(rectangles, list): |
| 401 |
rectangles = [ rectangles ] |
| 402 |
|
| 403 |
rects = <SDL_Rect *> calloc(len(rectangles), sizeof(SDL_Rect)) |
| 404 |
if rects == NULL: |
| 405 |
raise MemoryError("Couldn't allocate rectangles.") |
| 406 |
|
| 407 |
try: |
| 408 |
|
| 409 |
for i in rectangles: |
| 410 |
if i is None: |
| 411 |
continue |
| 412 |
|
| 413 |
to_sdl_rect(i, &rects[count]) |
| 414 |
count += 1 |
| 415 |
|
| 416 |
SDL_UpdateWindowSurfaceRects(self.window, rects, count) |
| 417 |
|
| 418 |
finally: |
| 419 |
free(rects) |
| 420 |
|
| 421 |
def get_wm_info(self): |
| 422 |
return { } |
| 423 |
|
| 424 |
def get_active(self): |
| 425 |
if SDL_GetWindowFlags(self.window) & (SDL_WINDOW_HIDDEN | SDL_WINDOW_MINIMIZED): |
| 426 |
return False |
| 427 |
else: |
| 428 |
return True |
| 429 |
|
| 430 |
def iconify(self): |
| 431 |
SDL_MinimizeWindow(self.window) |
| 432 |
return True |
| 433 |
|
| 434 |
def toggle_fullscreen(self): |
| 435 |
if SDL_GetWindowFlags(self.window) & (SDL_WINDOW_FULLSCREEN_DESKTOP): |
| 436 |
if SDL_SetWindowFullscreen(self.window, 0): |
| 437 |
raise error() |
| 438 |
else: |
| 439 |
if SDL_SetWindowFullscreen(self.window, SDL_WINDOW_FULLSCREEN_DESKTOP): |
| 440 |
raise error() |
| 441 |
|
| 442 |
return True |
| 443 |
|
| 444 |
def set_gamma(self, red, green=None, blue=None): |
| 445 |
if green is None: |
| 446 |
green = red |
| 447 |
if blue is None: |
| 448 |
blue = red |
| 449 |
|
| 450 |
cdef Uint16 red_gamma[256] |
| 451 |
cdef Uint16 green_gamma[256] |
| 452 |
cdef Uint16 blue_gamma[256] |
| 453 |
|
| 454 |
SDL_CalculateGammaRamp(red, red_gamma) |
| 455 |
SDL_CalculateGammaRamp(green, green_gamma) |
| 456 |
SDL_CalculateGammaRamp(blue, blue_gamma) |
| 457 |
|
| 458 |
if SDL_SetWindowGammaRamp(self.window, red_gamma, green_gamma, blue_gamma): |
| 459 |
return False |
| 460 |
|
| 461 |
return True |
| 462 |
|
| 463 |
def set_gamma_ramp(self, red, green, blue): |
| 464 |
|
| 465 |
cdef Uint16 red_gamma[256] |
| 466 |
cdef Uint16 green_gamma[256] |
| 467 |
cdef Uint16 blue_gamma[256] |
| 468 |
|
| 469 |
for i in range(256): |
| 470 |
red_gamma[i] = red[i] |
| 471 |
green_gamma[i] = green[i] |
| 472 |
blue_gamma[i] = blue[i] |
| 473 |
|
| 474 |
if SDL_SetWindowGammaRamp(self.window, red_gamma, green_gamma, blue_gamma): |
| 475 |
return False |
| 476 |
|
| 477 |
return True |
| 478 |
|
| 479 |
def set_icon(self, Surface surface): |
| 480 |
SDL_SetWindowIcon(self.window, surface.surface) |
| 481 |
|
| 482 |
def set_caption(self, title): |
| 483 |
|
| 484 |
if not isinstance(title, bytes): |
| 485 |
title = title.encode("utf-8") |
| 486 |
|
| 487 |
SDL_SetWindowTitle(self.window, title) |
| 488 |
|
| 489 |
def get_drawable_size(self): |
| 490 |
cdef int w, h |
| 491 |
|
| 492 |
SDL_GL_GetDrawableSize(self.window, &w, &h) |
| 493 |
return w, h |
| 494 |
|
| 495 |
|
| 496 |
def get_size(self): |
| 497 |
cdef int w, h |
| 498 |
|
| 499 |
SDL_GetWindowSize(self.window, &w, &h) |
| 500 |
return w, h |
| 501 |
|
| 502 |
def restore(self): |
| 503 |
SDL_RestoreWindow(self.window) |
| 504 |
|
| 505 |
def maximize(self): |
| 506 |
SDL_MaximizeWindow(self.window) |
| 507 |
|
| 508 |
def minimize(self): |
| 509 |
SDL_MinimizeWindow(self.window) |
| 510 |
|
| 511 |
def get_sdl_window_pointer(self): |
| 512 |
""" |
| 513 |
Returns the pointer to the SDL_Window corresponding to this window. |
| 514 |
""" |
| 515 |
|
| 516 |
import ctypes |
| 517 |
return ctypes.c_void_p(<unsigned long> self.window) |
| 518 |
|
| 519 |
def get_position(self): |
| 520 |
cdef int x, y |
| 521 |
|
| 522 |
SDL_GetWindowPosition(self.window, &x, &y) |
| 523 |
return x, y |
| 524 |
|
| 525 |
def set_position(self, pos): |
| 526 |
SDL_SetWindowPosition(self.window, pos[0], pos[1]) |
| 527 |
|
| 528 |
|
| 529 |
|
| 530 |
# The icon that's used for new windows. |
| 531 |
default_icon = None |
| 532 |
|
| 533 |
# The title that's used for new windows. |
| 534 |
default_title = "pygame window" |
| 535 |
|
| 536 |
# The default gl_swap_control |
| 537 |
default_swap_control = 1 |
| 538 |
|
| 539 |
def set_mode(resolution=(0, 0), flags=0, depth=0, pos=(SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED)): |
| 540 |
global main_window |
| 541 |
|
| 542 |
RESIZE_FLAGS = SDL_WINDOW_OPENGL | SDL_WINDOW_FULLSCREEN_DESKTOP |
| 543 |
|
| 544 |
if main_window: |
| 545 |
|
| 546 |
if (flags & ~RESIZE_FLAGS) == (main_window.create_flags & ~RESIZE_FLAGS): |
| 547 |
main_window.resize(resolution, flags & SDL_WINDOW_OPENGL, flags & SDL_WINDOW_FULLSCREEN_DESKTOP) |
| 548 |
return main_window.surface |
| 549 |
|
| 550 |
else: |
| 551 |
main_window.destroy() |
| 552 |
|
| 553 |
main_window = Window(default_title, resolution, flags, depth, pos=pos) |
| 554 |
|
| 555 |
if default_icon is not None: |
| 556 |
main_window.set_icon(default_icon) |
| 557 |
|
| 558 |
return main_window.surface |
| 559 |
|
| 560 |
def destroy(): |
| 561 |
global main_window |
| 562 |
|
| 563 |
if main_window is not None: |
| 564 |
main_window.destroy() |
| 565 |
main_window = None |
| 566 |
|
| 567 |
def get_surface(): |
| 568 |
if main_window is None: |
| 569 |
return None |
| 570 |
|
| 571 |
return main_window.get_surface() |
| 572 |
|
| 573 |
def get_window(): |
| 574 |
""" |
| 575 |
Returns the Window created by set_mode, or None if no such window |
| 576 |
exists. |
| 577 |
""" |
| 578 |
|
| 579 |
return main_window |
| 580 |
|
| 581 |
def flip(): |
| 582 |
if main_window: |
| 583 |
main_window.flip() |
| 584 |
|
| 585 |
def update(rectangles=None): |
| 586 |
if main_window: |
| 587 |
main_window.update(rectangles) |
| 588 |
|
| 589 |
def get_driver(): |
| 590 |
cdef const char *driver = SDL_GetCurrentVideoDriver() |
| 591 |
|
| 592 |
if driver == NULL: |
| 593 |
raise error() |
| 594 |
|
| 595 |
return driver |
| 596 |
|
| 597 |
class Info(object): |
| 598 |
|
| 599 |
def __init__(self): |
| 600 |
cdef SDL_DisplayMode dm |
| 601 |
cdef SDL_PixelFormat *format |
| 602 |
|
| 603 |
if SDL_GetCurrentDisplayMode(0, &dm): |
| 604 |
raise error() |
| 605 |
|
| 606 |
format = SDL_AllocFormat(dm.format) |
| 607 |
if format == NULL: |
| 608 |
raise error() |
| 609 |
|
| 610 |
self.bitsize = format.BitsPerPixel |
| 611 |
self.bytesize = format.BytesPerPixel |
| 612 |
|
| 613 |
self.masks = ( |
| 614 |
format.Rmask, |
| 615 |
format.Gmask, |
| 616 |
format.Bmask, |
| 617 |
format.Amask, |
| 618 |
) |
| 619 |
|
| 620 |
self.shifts = ( |
| 621 |
format.Rshift, |
| 622 |
format.Gshift, |
| 623 |
format.Bshift, |
| 624 |
format.Ashift, |
| 625 |
) |
| 626 |
|
| 627 |
self.losses = ( |
| 628 |
format.Rloss, |
| 629 |
format.Gloss, |
| 630 |
format.Bloss, |
| 631 |
format.Aloss, |
| 632 |
) |
| 633 |
|
| 634 |
SDL_FreeFormat(format) |
| 635 |
|
| 636 |
if main_window: |
| 637 |
self.current_w, self.current_h = main_window.surface.get_size() |
| 638 |
else: |
| 639 |
|
| 640 |
self.current_w = dm.w |
| 641 |
self.current_h = dm.h |
| 642 |
|
| 643 |
self.refresh_rate = dm.refresh_rate |
| 644 |
|
| 645 |
# The rest of these are just guesses. |
| 646 |
self.hw = False |
| 647 |
self.wm = True |
| 648 |
self.video_mem = 256 * 1024 * 1024 |
| 649 |
|
| 650 |
self.blit_hw = False |
| 651 |
self.blit_hw_CC = False |
| 652 |
self.blit_hw_A = False |
| 653 |
|
| 654 |
self.blit_sw = False |
| 655 |
self.blit_sw_CC = False |
| 656 |
self.blit_sw_A = False |
| 657 |
|
| 658 |
def __repr__(self): |
| 659 |
return "<Info({!r})>".format(self.__dict__) |
| 660 |
|
| 661 |
|
| 662 |
def get_wm_info(): |
| 663 |
if main_window: |
| 664 |
return main_window.get_wm_info() |
| 665 |
|
| 666 |
return {} |
| 667 |
|
| 668 |
|
| 669 |
def get_num_video_displays(): |
| 670 |
""" |
| 671 |
Returns the number of video displays connected to the system. |
| 672 |
""" |
| 673 |
|
| 674 |
rv = SDL_GetNumVideoDisplays() |
| 675 |
|
| 676 |
if rv < 0: |
| 677 |
raise error() |
| 678 |
|
| 679 |
return rv |
| 680 |
|
| 681 |
|
| 682 |
def list_modes(depth=0, flags=0, display=0): |
| 683 |
""" |
| 684 |
Returns a list of possible display modes for the display `display`. |
| 685 |
|
| 686 |
`depth` and `flags` are ignored. |
| 687 |
""" |
| 688 |
|
| 689 |
cdef int num_modes, i |
| 690 |
cdef SDL_DisplayMode mode |
| 691 |
|
| 692 |
rv = [ ] |
| 693 |
|
| 694 |
num_modes = SDL_GetNumDisplayModes(display) |
| 695 |
if num_modes < 0: |
| 696 |
raise error() |
| 697 |
|
| 698 |
for 0 <= i < num_modes: |
| 699 |
if SDL_GetDisplayMode(display, i, &mode) == 0: |
| 700 |
t = (mode.w, mode.h) |
| 701 |
if t not in rv: |
| 702 |
rv.append(t) |
| 703 |
|
| 704 |
return rv |
| 705 |
|
| 706 |
|
| 707 |
def mode_ok(size, flags=0, depth=0): |
| 708 |
""" |
| 709 |
Returns true if size is in the result of list_modes(). |
| 710 |
""" |
| 711 |
|
| 712 |
return tuple(size) in list_modes() |
| 713 |
|
| 714 |
def gl_reset_attributes(): |
| 715 |
SDL_GL_ResetAttributes() |
| 716 |
|
| 717 |
def gl_set_attribute(flag, value): |
| 718 |
|
| 719 |
if flag == GL_SWAP_CONTROL: |
| 720 |
if ios: |
| 721 |
return |
| 722 |
|
| 723 |
global default_swap_control |
| 724 |
|
| 725 |
# Try setting the swap interval - first positive, then negated |
| 726 |
# to deal with the case where the negative interval isn't |
| 727 |
# supported. Then give up and carry on. |
| 728 |
if SDL_GL_SetSwapInterval(value): |
| 729 |
SDL_GL_SetSwapInterval(-value) |
| 730 |
|
| 731 |
default_swap_control = value |
| 732 |
return |
| 733 |
|
| 734 |
if SDL_GL_SetAttribute(flag, value): |
| 735 |
raise error() |
| 736 |
|
| 737 |
def gl_get_attribute(flag): |
| 738 |
cdef int rv |
| 739 |
|
| 740 |
if flag == GL_SWAP_CONTROL: |
| 741 |
return SDL_GL_GetSwapInterval() |
| 742 |
|
| 743 |
if SDL_GL_GetAttribute(flag, &rv): |
| 744 |
raise error() |
| 745 |
|
| 746 |
return rv |
| 747 |
|
| 748 |
def gl_load_library(path): |
| 749 |
if path is None: |
| 750 |
if SDL_GL_LoadLibrary(NULL): |
| 751 |
raise error() |
| 752 |
else: |
| 753 |
if SDL_GL_LoadLibrary(path): |
| 754 |
raise error() |
| 755 |
|
| 756 |
def gl_unload_library(): |
| 757 |
SDL_GL_UnloadLibrary() |
| 758 |
|
| 759 |
def get_active(): |
| 760 |
if main_window: |
| 761 |
return main_window.get_active() |
| 762 |
return False |
| 763 |
|
| 764 |
def iconify(): |
| 765 |
if main_window: |
| 766 |
return main_window.iconify() |
| 767 |
|
| 768 |
return False |
| 769 |
|
| 770 |
def toggle_fullscreen(): |
| 771 |
if main_window: |
| 772 |
return main_window.toggle_fullscreen() |
| 773 |
|
| 774 |
return True |
| 775 |
|
| 776 |
def set_gamma(red, green=None, blue=None): |
| 777 |
if main_window: |
| 778 |
return main_window.set_gamma(red, green, blue) |
| 779 |
return False |
| 780 |
|
| 781 |
def set_gamma_ramp(red, green, blue): |
| 782 |
if main_window: |
| 783 |
return main_window.set_gamma_ramp(red, green, blue) |
| 784 |
return False |
| 785 |
|
| 786 |
def set_icon(surface): |
| 787 |
global default_icon |
| 788 |
|
| 789 |
default_icon = surface.copy() |
| 790 |
|
| 791 |
if main_window is not None: |
| 792 |
main_window.set_icon(default_icon) |
| 793 |
|
| 794 |
def set_caption(title, icontitle = None): |
| 795 |
global default_title |
| 796 |
|
| 797 |
default_title = title |
| 798 |
|
| 799 |
if main_window: |
| 800 |
main_window.set_caption(default_title) |
| 801 |
|
| 802 |
def get_caption(): |
| 803 |
return default_title |
| 804 |
|
| 805 |
def get_drawable_size(): |
| 806 |
if main_window: |
| 807 |
return main_window.get_drawable_size() |
| 808 |
return None |
| 809 |
|
| 810 |
def get_size(): |
| 811 |
if main_window: |
| 812 |
return main_window.get_size() |
| 813 |
return None |
| 814 |
|
| 815 |
def get_position(): |
| 816 |
if main_window: |
| 817 |
return main_window.get_position() |
| 818 |
return None |
| 819 |
|
| 820 |
def set_position(pos): |
| 821 |
if main_window: |
| 822 |
return main_window.set_position(pos) |
| 823 |
return False |
| 824 |
|
| 825 |
|
| 826 |
def get_num_video_displays(): |
| 827 |
rv = SDL_GetNumVideoDisplays() |
| 828 |
if rv < 0: |
| 829 |
raise error() |
| 830 |
|
| 831 |
return rv |
| 832 |
|
| 833 |
def get_display_bounds(index): |
| 834 |
cdef SDL_Rect rect |
| 835 |
rv = SDL_GetDisplayBounds(index, &rect) |
| 836 |
|
| 837 |
return (rect.x, rect.y, rect.w, rect.h) |
| 838 |
|
| 839 |
def set_screensaver(state): |
| 840 |
""" |
| 841 |
Sets the screenslaver to `state`. |
| 842 |
""" |
| 843 |
|
| 844 |
if state: |
| 845 |
SDL_EnableScreenSaver() |
| 846 |
else: |
| 847 |
SDL_DisableScreenSaver() |
| 848 |
|
| 849 |
def get_platform(): |
| 850 |
return SDL_GetPlatform().decode("utf-8") |
| 851 |
|
| 852 |
cdef api SDL_Window *PyWindow_AsWindow(window): |
| 853 |
""" |
| 854 |
Returns a pointer to the SDL_Window corresponding to `window`. If `window` |
| 855 |
is None, a pointer to the main window is returned. NULL is returned if |
| 856 |
there is no main window. |
| 857 |
""" |
| 858 |
|
| 859 |
if window is None: |
| 860 |
window = main_window |
| 861 |
|
| 862 |
if window is None: |
| 863 |
return NULL |
| 864 |
|
| 865 |
return (<Window> window).window |