comparison src/video/SDL_video.c @ 1683:396a35389351 SDL-1.3

Finished palettized display handling. Added support for surface palette sharing.
author Sam Lantinga <slouken@libsdl.org>
date Sat, 17 Jun 2006 06:45:14 +0000
parents 7ae8018b2e5d
children c4aa1a2f48f1
comparison
equal deleted inserted replaced
1682:7ae8018b2e5d 1683:396a35389351
515 int 515 int
516 SDL_SetDisplayMode(const SDL_DisplayMode * mode) 516 SDL_SetDisplayMode(const SDL_DisplayMode * mode)
517 { 517 {
518 SDL_VideoDisplay *display; 518 SDL_VideoDisplay *display;
519 SDL_DisplayMode display_mode; 519 SDL_DisplayMode display_mode;
520 int i; 520 int i, ncolors;
521 521
522 if (!_this) { 522 if (!_this) {
523 SDL_SetError("Video subsystem has not been initialized"); 523 SDL_SetError("Video subsystem has not been initialized");
524 return -1; 524 return -1;
525 } 525 }
558 (&display_mode, SDL_GetCurrentDisplayMode(), 558 (&display_mode, SDL_GetCurrentDisplayMode(),
559 sizeof(display_mode)) == 0) { 559 sizeof(display_mode)) == 0) {
560 return 0; 560 return 0;
561 } 561 }
562 562
563 /* Set up a palette, if necessary */
563 if (SDL_ISPIXELFORMAT_INDEXED(display_mode.format)) { 564 if (SDL_ISPIXELFORMAT_INDEXED(display_mode.format)) {
564 display->palette.ncolors = 565 ncolors = (1 << SDL_BITSPERPIXEL(display_mode.format));
565 (1 << SDL_BITSPERPIXEL(display_mode.format));
566 display->palette.colors =
567 (SDL_Color *) SDL_realloc(display->palette.colors,
568 display->palette.ncolors *
569 sizeof(*display->palette.colors));
570 if (!display->palette.colors) {
571 SDL_OutOfMemory();
572 return -1;
573 }
574 SDL_memset(display->palette.colors, 0xff,
575 display->palette.ncolors *
576 sizeof(*display->palette.colors));
577 } else { 566 } else {
578 if (display->palette.colors) { 567 ncolors = 0;
579 SDL_free(display->palette.colors); 568 }
580 } 569 if ((!ncolors && display->palette) || (ncolors && !display->palette)
581 display->palette.colors = NULL; 570 || (ncolors != display->palette->ncolors)) {
582 display->palette.ncolors = 0; 571 if (display->palette) {
572 SDL_FreePalette(display->palette);
573 display->palette = NULL;
574 }
575 if (ncolors) {
576 display->palette = SDL_AllocPalette(ncolors);
577 if (!display->palette) {
578 return -1;
579 }
580 }
583 } 581 }
584 582
585 return _this->SetDisplayMode(_this, &display_mode); 583 return _this->SetDisplayMode(_this, &display_mode);
586 } 584 }
587 585
588 int 586 int
589 SDL_SetDisplayPalette(const SDL_Color * colors, int firstcolor, int ncolors) 587 SDL_SetDisplayPalette(const SDL_Color * colors, int firstcolor, int ncolors)
590 { 588 {
591 SDL_Palette *palette; 589 SDL_Palette *palette;
590 int status = 0;
592 591
593 if (!_this) { 592 if (!_this) {
594 SDL_SetError("Video subsystem has not been initialized"); 593 SDL_SetError("Video subsystem has not been initialized");
595 return -1; 594 return -1;
596 } 595 }
597 596 if (!SDL_CurrentDisplay.palette) {
598 palette = &SDL_CurrentDisplay.palette;
599 if (!palette->ncolors) {
600 SDL_SetError("Display mode does not have a palette"); 597 SDL_SetError("Display mode does not have a palette");
601 return -1; 598 return -1;
602 } 599 }
603 600
604 if (firstcolor < 0 || (firstcolor + ncolors) > palette->ncolors) { 601 status =
605 SDL_SetError("Palette indices are out of range"); 602 SDL_SetPaletteColors(SDL_CurrentDisplay.palette, colors, firstcolor,
606 return -1; 603 ncolors);
607 }
608
609 SDL_memcpy(&palette->colors[firstcolor], colors,
610 ncolors * sizeof(*colors));
611 604
612 if (_this->SetDisplayPalette) { 605 if (_this->SetDisplayPalette) {
613 return _this->SetDisplayPalette(_this, palette); 606 if (_this->SetDisplayPalette(_this, palette) < 0) {
614 } else { 607 status = -1;
615 return 0; 608 }
616 } 609 }
610 return status;
617 } 611 }
618 612
619 int 613 int
620 SDL_GetDisplayPalette(SDL_Color * colors, int firstcolor, int ncolors) 614 SDL_GetDisplayPalette(SDL_Color * colors, int firstcolor, int ncolors)
621 { 615 {
624 if (!_this) { 618 if (!_this) {
625 SDL_SetError("Video subsystem has not been initialized"); 619 SDL_SetError("Video subsystem has not been initialized");
626 return -1; 620 return -1;
627 } 621 }
628 622
629 palette = &SDL_CurrentDisplay.palette; 623 palette = SDL_CurrentDisplay.palette;
630 if (!palette->ncolors) { 624 if (!palette->ncolors) {
631 SDL_SetError("Display mode does not have a palette"); 625 SDL_SetError("Display mode does not have a palette");
632 return -1; 626 return -1;
633 } 627 }
634 628
1715 } 1709 }
1716 if (display->windows) { 1710 if (display->windows) {
1717 SDL_free(display->windows); 1711 SDL_free(display->windows);
1718 display->windows = NULL; 1712 display->windows = NULL;
1719 } 1713 }
1720 if (display->palette.colors) { 1714 if (display->palette) {
1721 SDL_free(display->palette.colors); 1715 SDL_FreePalette(display->palette);
1722 display->palette.colors = NULL; 1716 display->palette = NULL;
1723 display->palette.ncolors = 0;
1724 } 1717 }
1725 } 1718 }
1726 _this->VideoQuit(_this); 1719 _this->VideoQuit(_this);
1727 if (_this->displays) { 1720 if (_this->displays) {
1728 SDL_free(_this->displays); 1721 SDL_free(_this->displays);