1.1 --- a/src/video/SDL_video.c Sun Jan 24 19:47:17 2010 +0000
1.2 +++ b/src/video/SDL_video.c Sun Jan 24 20:21:51 2010 +0000
1.3 @@ -105,6 +105,26 @@
1.4
1.5 static SDL_VideoDevice *_this = NULL;
1.6
1.7 +#define CHECK_WINDOW_MAGIC(window, retval) \
1.8 + if (!_this) { \
1.9 + SDL_UninitializedVideo(); \
1.10 + return retval; \
1.11 + } \
1.12 + if (!window || window->magic != &_this->window_magic) { \
1.13 + SDL_SetError("Invalid window"); \
1.14 + return retval; \
1.15 + }
1.16 +
1.17 +#define CHECK_TEXTURE_MAGIC(texture, retval) \
1.18 + if (!_this) { \
1.19 + SDL_UninitializedVideo(); \
1.20 + return retval; \
1.21 + } \
1.22 + if (!texture || texture->magic != &_this->texture_magic) { \
1.23 + SDL_SetError("Invalid texture"); \
1.24 + return retval; \
1.25 + }
1.26 +
1.27 /* Various local functions */
1.28 static void SDL_UpdateWindowGrab(SDL_Window * window);
1.29
1.30 @@ -710,9 +730,7 @@
1.31 int
1.32 SDL_SetWindowDisplayMode(SDL_Window * window, const SDL_DisplayMode * mode)
1.33 {
1.34 - if (!window) {
1.35 - return -1;
1.36 - }
1.37 + CHECK_WINDOW_MAGIC(window, -1);
1.38
1.39 if (mode) {
1.40 window->fullscreen_mode = *mode;
1.41 @@ -727,9 +745,7 @@
1.42 {
1.43 SDL_DisplayMode fullscreen_mode;
1.44
1.45 - if (!window) {
1.46 - return -1;
1.47 - }
1.48 + CHECK_WINDOW_MAGIC(window, -1);
1.49
1.50 fullscreen_mode = window->fullscreen_mode;
1.51 if (!fullscreen_mode.w) {
1.52 @@ -897,6 +913,7 @@
1.53 }
1.54 display = SDL_CurrentDisplay;
1.55 window = (SDL_Window *)SDL_calloc(1, sizeof(*window));
1.56 + window->magic = &_this->window_magic;
1.57 window->id = _this->next_object_id++;
1.58 window->x = x;
1.59 window->y = y;
1.60 @@ -944,6 +961,7 @@
1.61 }
1.62 display = SDL_CurrentDisplay;
1.63 window = (SDL_Window *)SDL_calloc(1, sizeof(*window));
1.64 + window->magic = &_this->window_magic;
1.65 window->id = _this->next_object_id++;
1.66 window->flags = SDL_WINDOW_FOREIGN;
1.67 window->display = display;
1.68 @@ -1047,9 +1065,8 @@
1.69 Uint32
1.70 SDL_GetWindowID(SDL_Window * window)
1.71 {
1.72 - if (!window) {
1.73 - return 0;
1.74 - }
1.75 + CHECK_WINDOW_MAGIC(window, 0);
1.76 +
1.77 return window->id;
1.78 }
1.79
1.80 @@ -1077,16 +1094,17 @@
1.81 Uint32
1.82 SDL_GetWindowFlags(SDL_Window * window)
1.83 {
1.84 - if (!window) {
1.85 - return 0;
1.86 - }
1.87 + CHECK_WINDOW_MAGIC(window, 0);
1.88 +
1.89 return window->flags;
1.90 }
1.91
1.92 void
1.93 SDL_SetWindowTitle(SDL_Window * window, const char *title)
1.94 {
1.95 - if (!window || title == window->title) {
1.96 + CHECK_WINDOW_MAGIC(window, );
1.97 +
1.98 + if (title == window->title) {
1.99 return;
1.100 }
1.101 if (window->title) {
1.102 @@ -1106,18 +1124,16 @@
1.103 const char *
1.104 SDL_GetWindowTitle(SDL_Window * window)
1.105 {
1.106 - if (!window) {
1.107 - return NULL;
1.108 - }
1.109 + CHECK_WINDOW_MAGIC(window, NULL);
1.110 +
1.111 return window->title;
1.112 }
1.113
1.114 void
1.115 SDL_SetWindowIcon(SDL_Window * window, SDL_Surface * icon)
1.116 {
1.117 - if (!window) {
1.118 - return;
1.119 - }
1.120 + CHECK_WINDOW_MAGIC(window, );
1.121 +
1.122 if (_this->SetWindowIcon) {
1.123 _this->SetWindowIcon(_this, window, icon);
1.124 }
1.125 @@ -1126,27 +1142,24 @@
1.126 void
1.127 SDL_SetWindowData(SDL_Window * window, void *userdata)
1.128 {
1.129 - if (!window) {
1.130 - return;
1.131 - }
1.132 + CHECK_WINDOW_MAGIC(window, );
1.133 +
1.134 window->userdata = userdata;
1.135 }
1.136
1.137 void *
1.138 SDL_GetWindowData(SDL_Window * window)
1.139 {
1.140 - if (!window) {
1.141 - return NULL;
1.142 - }
1.143 + CHECK_WINDOW_MAGIC(window, NULL);
1.144 +
1.145 return window->userdata;
1.146 }
1.147
1.148 void
1.149 SDL_SetWindowPosition(SDL_Window * window, int x, int y)
1.150 {
1.151 - if (!window) {
1.152 - return;
1.153 - }
1.154 + CHECK_WINDOW_MAGIC(window, );
1.155 +
1.156 if (x != SDL_WINDOWPOS_UNDEFINED) {
1.157 window->x = x;
1.158 }
1.159 @@ -1162,9 +1175,8 @@
1.160 void
1.161 SDL_GetWindowPosition(SDL_Window * window, int *x, int *y)
1.162 {
1.163 - if (!window) {
1.164 - return;
1.165 - }
1.166 + CHECK_WINDOW_MAGIC(window, );
1.167 +
1.168 if (x) {
1.169 *x = window->x;
1.170 }
1.171 @@ -1176,9 +1188,8 @@
1.172 void
1.173 SDL_SetWindowSize(SDL_Window * window, int w, int h)
1.174 {
1.175 - if (!window) {
1.176 - return;
1.177 - }
1.178 + CHECK_WINDOW_MAGIC(window, );
1.179 +
1.180 window->w = w;
1.181 window->h = h;
1.182
1.183 @@ -1211,7 +1222,9 @@
1.184 void
1.185 SDL_ShowWindow(SDL_Window * window)
1.186 {
1.187 - if (!window || (window->flags & SDL_WINDOW_SHOWN)) {
1.188 + CHECK_WINDOW_MAGIC(window, );
1.189 +
1.190 + if (window->flags & SDL_WINDOW_SHOWN) {
1.191 return;
1.192 }
1.193
1.194 @@ -1224,7 +1237,9 @@
1.195 void
1.196 SDL_HideWindow(SDL_Window * window)
1.197 {
1.198 - if (!window || !(window->flags & SDL_WINDOW_SHOWN)) {
1.199 + CHECK_WINDOW_MAGIC(window, );
1.200 +
1.201 + if (!(window->flags & SDL_WINDOW_SHOWN)) {
1.202 return;
1.203 }
1.204
1.205 @@ -1237,7 +1252,9 @@
1.206 void
1.207 SDL_RaiseWindow(SDL_Window * window)
1.208 {
1.209 - if (!window || !(window->flags & SDL_WINDOW_SHOWN)) {
1.210 + CHECK_WINDOW_MAGIC(window, );
1.211 +
1.212 + if (!(window->flags & SDL_WINDOW_SHOWN)) {
1.213 return;
1.214 }
1.215 if (_this->RaiseWindow) {
1.216 @@ -1251,7 +1268,9 @@
1.217 void
1.218 SDL_MaximizeWindow(SDL_Window * window)
1.219 {
1.220 - if (!window || (window->flags & SDL_WINDOW_MAXIMIZED)) {
1.221 + CHECK_WINDOW_MAGIC(window, );
1.222 +
1.223 + if (window->flags & SDL_WINDOW_MAXIMIZED) {
1.224 return;
1.225 }
1.226
1.227 @@ -1264,7 +1283,9 @@
1.228 void
1.229 SDL_MinimizeWindow(SDL_Window * window)
1.230 {
1.231 - if (!window || (window->flags & SDL_WINDOW_MINIMIZED)) {
1.232 + CHECK_WINDOW_MAGIC(window, );
1.233 +
1.234 + if (window->flags & SDL_WINDOW_MINIMIZED) {
1.235 return;
1.236 }
1.237
1.238 @@ -1277,8 +1298,9 @@
1.239 void
1.240 SDL_RestoreWindow(SDL_Window * window)
1.241 {
1.242 - if (!window
1.243 - || !(window->flags & (SDL_WINDOW_MAXIMIZED | SDL_WINDOW_MINIMIZED))) {
1.244 + CHECK_WINDOW_MAGIC(window, );
1.245 +
1.246 + if (!(window->flags & (SDL_WINDOW_MAXIMIZED | SDL_WINDOW_MINIMIZED))) {
1.247 return;
1.248 }
1.249
1.250 @@ -1291,9 +1313,8 @@
1.251 int
1.252 SDL_SetWindowFullscreen(SDL_Window * window, int fullscreen)
1.253 {
1.254 - if (!window) {
1.255 - return -1;
1.256 - }
1.257 + CHECK_WINDOW_MAGIC(window, -1);
1.258 +
1.259 if (fullscreen) {
1.260 fullscreen = SDL_WINDOW_FULLSCREEN;
1.261 }
1.262 @@ -1315,7 +1336,9 @@
1.263 void
1.264 SDL_SetWindowGrab(SDL_Window * window, int mode)
1.265 {
1.266 - if (!window || (!!mode == !!(window->flags & SDL_WINDOW_INPUT_GRABBED))) {
1.267 + CHECK_WINDOW_MAGIC(window, );
1.268 +
1.269 + if ((!!mode == !!(window->flags & SDL_WINDOW_INPUT_GRABBED))) {
1.270 return;
1.271 }
1.272 if (mode) {
1.273 @@ -1337,9 +1360,8 @@
1.274 int
1.275 SDL_GetWindowGrab(SDL_Window * window)
1.276 {
1.277 - if (!window) {
1.278 - return 0;
1.279 - }
1.280 + CHECK_WINDOW_MAGIC(window, 0);
1.281 +
1.282 return ((window->flags & SDL_WINDOW_INPUT_GRABBED) != 0);
1.283 }
1.284
1.285 @@ -1436,10 +1458,8 @@
1.286 {
1.287 SDL_VideoDisplay *display;
1.288
1.289 - if (!_this || !window || !window->id) {
1.290 - SDL_SetError("Invalid window");
1.291 - return;
1.292 - }
1.293 + CHECK_WINDOW_MAGIC(window, );
1.294 + window->magic = NULL;
1.295
1.296 if (window->title) {
1.297 SDL_free(window->title);
1.298 @@ -1469,9 +1489,6 @@
1.299 display->windows = window->next;
1.300 }
1.301
1.302 - /* Clear the ID so we know it was destroyed */
1.303 - window->id = 0;
1.304 -
1.305 SDL_free(window);
1.306 }
1.307
1.308 @@ -1519,10 +1536,7 @@
1.309 int
1.310 SDL_CreateRenderer(SDL_Window * window, int index, Uint32 flags)
1.311 {
1.312 - if (!window) {
1.313 - SDL_SetError("Invalid window");
1.314 - return -1;
1.315 - }
1.316 + CHECK_WINDOW_MAGIC(window, -1);
1.317
1.318 /* Free any existing renderer */
1.319 SDL_DestroyRenderer(window);
1.320 @@ -1596,10 +1610,8 @@
1.321 {
1.322 SDL_Renderer *renderer;
1.323
1.324 - if (!window) {
1.325 - SDL_SetError("Invalid window");
1.326 - return -1;
1.327 - }
1.328 + CHECK_WINDOW_MAGIC(window, -1);
1.329 +
1.330 renderer = window->renderer;
1.331 if (!renderer) {
1.332 SDL_SetError("Use SDL_CreateRenderer() to create a renderer");
1.333 @@ -1644,6 +1656,7 @@
1.334 SDL_OutOfMemory();
1.335 return 0;
1.336 }
1.337 + texture->magic = &_this->texture_magic;
1.338 texture->format = format;
1.339 texture->access = access;
1.340 texture->w = w;
1.341 @@ -1972,9 +1985,8 @@
1.342 SDL_QueryTexture(SDL_Texture * texture, Uint32 * format, int *access,
1.343 int *w, int *h)
1.344 {
1.345 - if (!texture) {
1.346 - return -1;
1.347 - }
1.348 + CHECK_TEXTURE_MAGIC(texture, -1);
1.349 +
1.350 if (format) {
1.351 *format = texture->format;
1.352 }
1.353 @@ -1995,9 +2007,8 @@
1.354 {
1.355 SDL_Renderer *renderer;
1.356
1.357 - if (!texture) {
1.358 - return -1;
1.359 - }
1.360 + CHECK_TEXTURE_MAGIC(texture, -1);
1.361 +
1.362 renderer = texture->renderer;
1.363 if (!renderer->QueryTexturePixels) {
1.364 SDL_Unsupported();
1.365 @@ -2012,9 +2023,8 @@
1.366 {
1.367 SDL_Renderer *renderer;
1.368
1.369 - if (!texture) {
1.370 - return -1;
1.371 - }
1.372 + CHECK_TEXTURE_MAGIC(texture, -1);
1.373 +
1.374 renderer = texture->renderer;
1.375 if (!renderer->SetTexturePalette) {
1.376 SDL_Unsupported();
1.377 @@ -2030,9 +2040,8 @@
1.378 {
1.379 SDL_Renderer *renderer;
1.380
1.381 - if (!texture) {
1.382 - return -1;
1.383 - }
1.384 + CHECK_TEXTURE_MAGIC(texture, -1);
1.385 +
1.386 renderer = texture->renderer;
1.387 if (!renderer->GetTexturePalette) {
1.388 SDL_Unsupported();
1.389 @@ -2047,9 +2056,8 @@
1.390 {
1.391 SDL_Renderer *renderer;
1.392
1.393 - if (!texture) {
1.394 - return -1;
1.395 - }
1.396 + CHECK_TEXTURE_MAGIC(texture, -1);
1.397 +
1.398 renderer = texture->renderer;
1.399 if (!renderer->SetTextureColorMod) {
1.400 SDL_Unsupported();
1.401 @@ -2072,9 +2080,8 @@
1.402 {
1.403 SDL_Renderer *renderer;
1.404
1.405 - if (!texture) {
1.406 - return -1;
1.407 - }
1.408 + CHECK_TEXTURE_MAGIC(texture, -1);
1.409 +
1.410 renderer = texture->renderer;
1.411 if (r) {
1.412 *r = texture->r;
1.413 @@ -2093,9 +2100,8 @@
1.414 {
1.415 SDL_Renderer *renderer;
1.416
1.417 - if (!texture) {
1.418 - return -1;
1.419 - }
1.420 + CHECK_TEXTURE_MAGIC(texture, -1);
1.421 +
1.422 renderer = texture->renderer;
1.423 if (!renderer->SetTextureAlphaMod) {
1.424 SDL_Unsupported();
1.425 @@ -2113,9 +2119,8 @@
1.426 int
1.427 SDL_GetTextureAlphaMod(SDL_Texture * texture, Uint8 * alpha)
1.428 {
1.429 - if (!texture) {
1.430 - return -1;
1.431 - }
1.432 + CHECK_TEXTURE_MAGIC(texture, -1);
1.433 +
1.434 if (alpha) {
1.435 *alpha = texture->a;
1.436 }
1.437 @@ -2127,9 +2132,8 @@
1.438 {
1.439 SDL_Renderer *renderer;
1.440
1.441 - if (!texture) {
1.442 - return -1;
1.443 - }
1.444 + CHECK_TEXTURE_MAGIC(texture, -1);
1.445 +
1.446 renderer = texture->renderer;
1.447 if (!renderer->SetTextureBlendMode) {
1.448 SDL_Unsupported();
1.449 @@ -2142,9 +2146,8 @@
1.450 int
1.451 SDL_GetTextureBlendMode(SDL_Texture * texture, int *blendMode)
1.452 {
1.453 - if (!texture) {
1.454 - return -1;
1.455 - }
1.456 + CHECK_TEXTURE_MAGIC(texture, -1);
1.457 +
1.458 if (blendMode) {
1.459 *blendMode = texture->blendMode;
1.460 }
1.461 @@ -2156,9 +2159,8 @@
1.462 {
1.463 SDL_Renderer *renderer;
1.464
1.465 - if (!texture) {
1.466 - return -1;
1.467 - }
1.468 + CHECK_TEXTURE_MAGIC(texture, -1);
1.469 +
1.470 renderer = texture->renderer;
1.471 if (!renderer->SetTextureScaleMode) {
1.472 SDL_Unsupported();
1.473 @@ -2171,9 +2173,8 @@
1.474 int
1.475 SDL_GetTextureScaleMode(SDL_Texture * texture, int *scaleMode)
1.476 {
1.477 - if (!texture) {
1.478 - return -1;
1.479 - }
1.480 + CHECK_TEXTURE_MAGIC(texture, -1);
1.481 +
1.482 if (scaleMode) {
1.483 *scaleMode = texture->scaleMode;
1.484 }
1.485 @@ -2187,9 +2188,8 @@
1.486 SDL_Renderer *renderer;
1.487 SDL_Rect full_rect;
1.488
1.489 - if (!texture) {
1.490 - return -1;
1.491 - }
1.492 + CHECK_TEXTURE_MAGIC(texture, -1);
1.493 +
1.494 renderer = texture->renderer;
1.495 if (!renderer->UpdateTexture) {
1.496 SDL_Unsupported();
1.497 @@ -2212,9 +2212,8 @@
1.498 SDL_Renderer *renderer;
1.499 SDL_Rect full_rect;
1.500
1.501 - if (!texture) {
1.502 - return -1;
1.503 - }
1.504 + CHECK_TEXTURE_MAGIC(texture, -1);
1.505 +
1.506 if (texture->access != SDL_TEXTUREACCESS_STREAMING) {
1.507 SDL_SetError("SDL_LockTexture(): texture must be streaming");
1.508 return -1;
1.509 @@ -2240,9 +2239,8 @@
1.510 {
1.511 SDL_Renderer *renderer;
1.512
1.513 - if (!texture) {
1.514 - return;
1.515 - }
1.516 + CHECK_TEXTURE_MAGIC(texture, );
1.517 +
1.518 if (texture->access != SDL_TEXTUREACCESS_STREAMING) {
1.519 return;
1.520 }
1.521 @@ -2259,9 +2257,8 @@
1.522 {
1.523 SDL_Renderer *renderer;
1.524
1.525 - if (!texture) {
1.526 - return;
1.527 - }
1.528 + CHECK_TEXTURE_MAGIC(texture, );
1.529 +
1.530 if (texture->access != SDL_TEXTUREACCESS_STREAMING) {
1.531 return;
1.532 }
1.533 @@ -2544,14 +2541,12 @@
1.534 SDL_Rect real_srcrect;
1.535 SDL_Rect real_dstrect;
1.536
1.537 + CHECK_TEXTURE_MAGIC(texture, -1);
1.538 +
1.539 renderer = SDL_GetCurrentRenderer(SDL_TRUE);
1.540 if (!renderer) {
1.541 return -1;
1.542 }
1.543 - if (!texture) {
1.544 - SDL_SetError("Texture not found");
1.545 - return -1;
1.546 - }
1.547 if (texture->renderer != renderer) {
1.548 SDL_SetError("Texture was not created with this renderer");
1.549 return -1;
1.550 @@ -2704,10 +2699,8 @@
1.551 {
1.552 SDL_Renderer *renderer;
1.553
1.554 - if (!texture || !texture->renderer) {
1.555 - SDL_SetError("Invalid texture");
1.556 - return;
1.557 - }
1.558 + CHECK_TEXTURE_MAGIC(texture, );
1.559 + texture->magic = NULL;
1.560
1.561 renderer = texture->renderer;
1.562 if (texture->next) {
1.563 @@ -2718,7 +2711,6 @@
1.564 } else {
1.565 renderer->textures = texture->next;
1.566 }
1.567 - texture->renderer = NULL;
1.568
1.569 renderer->DestroyTexture(renderer, texture);
1.570 SDL_free(texture);
1.571 @@ -2729,9 +2721,8 @@
1.572 {
1.573 SDL_Renderer *renderer;
1.574
1.575 - if (!window) {
1.576 - return;
1.577 - }
1.578 + CHECK_WINDOW_MAGIC(window, );
1.579 +
1.580 renderer = window->renderer;
1.581 if (!renderer) {
1.582 return;
1.583 @@ -3215,9 +3206,8 @@
1.584 SDL_GLContext
1.585 SDL_GL_CreateContext(SDL_Window * window)
1.586 {
1.587 - if (!window) {
1.588 - return NULL;
1.589 - }
1.590 + CHECK_WINDOW_MAGIC(window, NULL);
1.591 +
1.592 if (!(window->flags & SDL_WINDOW_OPENGL)) {
1.593 SDL_SetError("The specified window isn't an OpenGL window");
1.594 return NULL;
1.595 @@ -3228,7 +3218,9 @@
1.596 int
1.597 SDL_GL_MakeCurrent(SDL_Window * window, SDL_GLContext context)
1.598 {
1.599 - if (window && !(window->flags & SDL_WINDOW_OPENGL)) {
1.600 + CHECK_WINDOW_MAGIC(window, -1);
1.601 +
1.602 + if (!(window->flags & SDL_WINDOW_OPENGL)) {
1.603 SDL_SetError("The specified window isn't an OpenGL window");
1.604 return -1;
1.605 }
1.606 @@ -3271,9 +3263,8 @@
1.607 void
1.608 SDL_GL_SwapWindow(SDL_Window * window)
1.609 {
1.610 - if (!window) {
1.611 - return;
1.612 - }
1.613 + CHECK_WINDOW_MAGIC(window, );
1.614 +
1.615 if (!(window->flags & SDL_WINDOW_OPENGL)) {
1.616 SDL_SetError("The specified window isn't an OpenGL window");
1.617 return;
1.618 @@ -3393,7 +3384,9 @@
1.619 SDL_bool
1.620 SDL_GetWindowWMInfo(SDL_Window * window, struct SDL_SysWMinfo *info)
1.621 {
1.622 - if (!window || !_this->GetWindowWMInfo) {
1.623 + CHECK_WINDOW_MAGIC(window, SDL_FALSE);
1.624 +
1.625 + if (!_this->GetWindowWMInfo) {
1.626 return SDL_FALSE;
1.627 }
1.628 return (_this->GetWindowWMInfo(_this, window, info));