comparison src/SDL_compat.c @ 2829:16fe3b867887

Allow resizing of SDL 1.2 compatibility video mode
author Sam Lantinga <slouken@libsdl.org>
date Thu, 04 Dec 2008 16:54:22 +0000
parents 365fe1a2aad5
children 7173fc5c7ef6
comparison
equal deleted inserted replaced
2828:7e5ff6cd05bf 2829:16fe3b867887
35 static SDL_TextureID SDL_VideoTexture = 0; 35 static SDL_TextureID SDL_VideoTexture = 0;
36 static SDL_Surface *SDL_VideoSurface = NULL; 36 static SDL_Surface *SDL_VideoSurface = NULL;
37 static SDL_Surface *SDL_ShadowSurface = NULL; 37 static SDL_Surface *SDL_ShadowSurface = NULL;
38 static SDL_Surface *SDL_PublicSurface = NULL; 38 static SDL_Surface *SDL_PublicSurface = NULL;
39 static SDL_GLContext *SDL_VideoContext = NULL; 39 static SDL_GLContext *SDL_VideoContext = NULL;
40 static Uint32 SDL_VideoFlags = 0;
41 static int SDL_VideoBPP = 0;
40 static char *wm_title = NULL; 42 static char *wm_title = NULL;
41 43
42 char * 44 char *
43 SDL_AudioDriverName(char *namebuf, int maxlen) 45 SDL_AudioDriverName(char *namebuf, int maxlen)
44 { 46 {
173 fake.type = SDL_VIDEOEXPOSE; 175 fake.type = SDL_VIDEOEXPOSE;
174 SDL_PushEvent(&fake); 176 SDL_PushEvent(&fake);
175 } 177 }
176 break; 178 break;
177 case SDL_WINDOWEVENT_RESIZED: 179 case SDL_WINDOWEVENT_RESIZED:
180 SDL_PeepEvents(&fake, 1, SDL_GETEVENT, SDL_VIDEORESIZEMASK);
178 fake.type = SDL_VIDEORESIZE; 181 fake.type = SDL_VIDEORESIZE;
179 fake.resize.w = event->window.data1; 182 fake.resize.w = event->window.data1;
180 fake.resize.h = event->window.data2; 183 fake.resize.h = event->window.data2;
181 SDL_PushEvent(&fake); 184 SDL_PushEvent(&fake);
182 break; 185 break;
358 SDL_CreateRGBSurface(0, w, h, bpp, Rmask, Gmask, Bmask, Amask); 361 SDL_CreateRGBSurface(0, w, h, bpp, Rmask, Gmask, Bmask, Amask);
359 } 362 }
360 return surface; 363 return surface;
361 } 364 }
362 365
366 static void
367 ClearVideoSurface()
368 {
369 Uint32 black;
370
371 /* Clear the surface for display */
372 black = SDL_MapRGB(SDL_PublicSurface->format, 0, 0, 0);
373 SDL_FillRect(SDL_PublicSurface, NULL, black);
374 SDL_UpdateRect(SDL_PublicSurface, 0, 0, 0, 0);
375 }
376
377 int
378 SDL_ResizeVideoMode(int width, int height, int bpp, Uint32 flags)
379 {
380 int w, h;
381 Uint32 format;
382 int access;
383 void *pixels;
384 int pitch;
385
386 /* We can't resize something we don't have... */
387 if (!SDL_VideoWindow) {
388 return -1;
389 }
390
391 /* We probably have to recreate the window in fullscreen mode */
392 if (flags & SDL_FULLSCREEN) {
393 return -1;
394 }
395
396 /* I don't think there's any change we can gracefully make in flags */
397 if (flags != SDL_VideoFlags) {
398 return -1;
399 }
400
401 /* Resize the window */
402 SDL_GetWindowSize(SDL_VideoWindow, &w, &h);
403 if (w != width || h != height) {
404 SDL_SetWindowSize(SDL_VideoWindow, width, height);
405 }
406
407 /* If we're in OpenGL mode, just resize the stub surface and we're done! */
408 if (flags & SDL_OPENGL) {
409 SDL_VideoSurface->w = width;
410 SDL_VideoSurface->h = height;
411 return 0;
412 }
413
414 /* Destroy the screen texture and recreate it */
415 SDL_QueryTexture(SDL_VideoTexture, &format, &access, &w, &h);
416 SDL_DestroyTexture(SDL_VideoTexture);
417 SDL_VideoTexture = SDL_CreateTexture(format, access, width, height);
418 if (!SDL_VideoTexture) {
419 return -1;
420 }
421
422 SDL_VideoSurface->w = width;
423 SDL_VideoSurface->h = height;
424 if (SDL_QueryTexturePixels(SDL_VideoTexture, &pixels, &pitch) == 0) {
425 SDL_VideoSurface->pixels = pixels;
426 SDL_VideoSurface->pitch = pitch;
427 } else {
428 SDL_CalculatePitch(SDL_VideoSurface);
429 SDL_VideoSurface->pixels =
430 SDL_realloc(SDL_VideoSurface->pixels,
431 SDL_VideoSurface->h * SDL_VideoSurface->pitch);
432 }
433 SDL_SetClipRect(SDL_VideoSurface, NULL);
434
435 if (SDL_ShadowSurface) {
436 SDL_ShadowSurface->w = width;
437 SDL_ShadowSurface->h = height;
438 SDL_CalculatePitch(SDL_ShadowSurface);
439 SDL_ShadowSurface->pixels =
440 SDL_realloc(SDL_ShadowSurface->pixels,
441 SDL_ShadowSurface->h * SDL_ShadowSurface->pitch);
442 SDL_SetClipRect(SDL_ShadowSurface, NULL);
443 }
444
445 ClearVideoSurface();
446
447 return 0;
448 }
449
363 SDL_Surface * 450 SDL_Surface *
364 SDL_SetVideoMode(int width, int height, int bpp, Uint32 flags) 451 SDL_SetVideoMode(int width, int height, int bpp, Uint32 flags)
365 { 452 {
366 SDL_DisplayMode desktop_mode; 453 SDL_DisplayMode desktop_mode;
367 SDL_DisplayMode mode; 454 SDL_DisplayMode mode;
369 int window_y = SDL_WINDOWPOS_UNDEFINED; 456 int window_y = SDL_WINDOWPOS_UNDEFINED;
370 Uint32 window_flags; 457 Uint32 window_flags;
371 Uint32 desktop_format; 458 Uint32 desktop_format;
372 Uint32 desired_format; 459 Uint32 desired_format;
373 Uint32 surface_flags; 460 Uint32 surface_flags;
374 Uint32 black;
375 461
376 if (!SDL_GetVideoDevice()) { 462 if (!SDL_GetVideoDevice()) {
377 if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_NOPARACHUTE) < 0) { 463 if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_NOPARACHUTE) < 0) {
378 return NULL; 464 return NULL;
379 } 465 }
466 }
467
468 /* See if we can simply resize the existing window and surface */
469 if (SDL_ResizeVideoMode(width, height, bpp, flags) == 0) {
470 return SDL_PublicSurface;
380 } 471 }
381 472
382 /* Destroy existing window */ 473 /* Destroy existing window */
383 SDL_PublicSurface = NULL; 474 SDL_PublicSurface = NULL;
384 if (SDL_ShadowSurface) { 475 if (SDL_ShadowSurface) {
531 } 622 }
532 if (!SDL_VideoTexture) { 623 if (!SDL_VideoTexture) {
533 return NULL; 624 return NULL;
534 } 625 }
535 626
536 SDL_SetTextureBlendMode(SDL_VideoTexture, SDL_TEXTUREBLENDMODE_NONE);
537
538
539 /* Create the screen surface */ 627 /* Create the screen surface */
540 SDL_VideoSurface = CreateVideoSurface(SDL_VideoTexture); 628 SDL_VideoSurface = CreateVideoSurface(SDL_VideoTexture);
541 if (!SDL_VideoSurface) { 629 if (!SDL_VideoSurface) {
542 return NULL; 630 return NULL;
543 } 631 }
578 } 666 }
579 } 667 }
580 SDL_PublicSurface = 668 SDL_PublicSurface =
581 (SDL_ShadowSurface ? SDL_ShadowSurface : SDL_VideoSurface); 669 (SDL_ShadowSurface ? SDL_ShadowSurface : SDL_VideoSurface);
582 670
583 /* Clear the surface for display */ 671 SDL_VideoFlags = flags;
584 black = SDL_MapRGB(SDL_PublicSurface->format, 0, 0, 0); 672
585 SDL_FillRect(SDL_PublicSurface, NULL, black); 673 ClearVideoSurface();
586 SDL_UpdateRect(SDL_PublicSurface, 0, 0, 0, 0);
587 674
588 /* We're finally done! */ 675 /* We're finally done! */
589 return SDL_PublicSurface; 676 return SDL_PublicSurface;
590 } 677 }
591 678