comparison src/video/SDL_surface.c @ 1670:eef792d31de8 SDL-1.3

Work in progress. :)
author Sam Lantinga <slouken@libsdl.org>
date Wed, 07 Jun 2006 16:10:28 +0000
parents 4da1ee79c9af
children 8e754b82cecc
comparison
equal deleted inserted replaced
1669:9857d21967bb 1670:eef792d31de8
41 { 41 {
42 SDL_VideoDevice *_this = SDL_GetVideoDevice(); 42 SDL_VideoDevice *_this = SDL_GetVideoDevice();
43 SDL_Surface *screen; 43 SDL_Surface *screen;
44 SDL_Surface *surface; 44 SDL_Surface *surface;
45 45
46 /* FIXME!! */
46 /* Make sure the size requested doesn't overflow our datatypes */ 47 /* Make sure the size requested doesn't overflow our datatypes */
47 /* Next time I write a library like SDL, I'll use int for size. :) */ 48 /* Next time I write a library like SDL, I'll use int for size. :) */
48 if (width >= 16384 || height >= 65536) { 49 if (width >= 16384 || height >= 65536) {
49 SDL_SetError("Width or height is too large"); 50 SDL_SetError("Width or height is too large");
50 return (NULL); 51 return (NULL);
51 } 52 }
52 53
53 /* Check to see if we desire the surface in video memory */
54 if (_this) {
55 screen = SDL_PublicSurface;
56 } else {
57 screen = NULL;
58 }
59 if (screen && ((screen->flags & SDL_HWSURFACE) == SDL_HWSURFACE)) {
60 if ((flags & (SDL_SRCCOLORKEY | SDL_SRCALPHA)) != 0) {
61 flags |= SDL_HWSURFACE;
62 }
63 if ((flags & SDL_SRCCOLORKEY) == SDL_SRCCOLORKEY) {
64 if (!_this->info.blit_hw_CC) {
65 flags &= ~SDL_HWSURFACE;
66 }
67 }
68 if ((flags & SDL_SRCALPHA) == SDL_SRCALPHA) {
69 if (!_this->info.blit_hw_A) {
70 flags &= ~SDL_HWSURFACE;
71 }
72 }
73 } else {
74 flags &= ~SDL_HWSURFACE;
75 }
76
77 /* Allocate the surface */ 54 /* Allocate the surface */
78 surface = (SDL_Surface *) SDL_malloc(sizeof(*surface)); 55 surface = (SDL_Surface *) SDL_malloc(sizeof(*surface));
79 if (surface == NULL) { 56 if (surface == NULL) {
80 SDL_OutOfMemory(); 57 SDL_OutOfMemory();
81 return (NULL); 58 return (NULL);
82 } 59 }
83 surface->flags = SDL_SWSURFACE; 60 surface->flags = 0;
84 if ((flags & SDL_HWSURFACE) == SDL_HWSURFACE) {
85 if ((Amask) && (_this->displayformatalphapixel)) {
86 depth = _this->displayformatalphapixel->BitsPerPixel;
87 Rmask = _this->displayformatalphapixel->Rmask;
88 Gmask = _this->displayformatalphapixel->Gmask;
89 Bmask = _this->displayformatalphapixel->Bmask;
90 Amask = _this->displayformatalphapixel->Amask;
91 } else {
92 depth = screen->format->BitsPerPixel;
93 Rmask = screen->format->Rmask;
94 Gmask = screen->format->Gmask;
95 Bmask = screen->format->Bmask;
96 Amask = screen->format->Amask;
97 }
98 }
99 surface->format = SDL_AllocFormat(depth, Rmask, Gmask, Bmask, Amask); 61 surface->format = SDL_AllocFormat(depth, Rmask, Gmask, Bmask, Amask);
100 if (surface->format == NULL) { 62 if (surface->format == NULL) {
101 SDL_free(surface); 63 SDL_free(surface);
102 return (NULL); 64 return (NULL);
103 } 65 }
106 } 68 }
107 surface->w = width; 69 surface->w = width;
108 surface->h = height; 70 surface->h = height;
109 surface->pitch = SDL_CalculatePitch(surface); 71 surface->pitch = SDL_CalculatePitch(surface);
110 surface->pixels = NULL; 72 surface->pixels = NULL;
111 surface->offset = 0;
112 surface->hwdata = NULL; 73 surface->hwdata = NULL;
113 surface->locked = 0; 74 surface->locked = 0;
114 surface->map = NULL; 75 surface->map = NULL;
115 surface->unused1 = 0;
116 SDL_SetClipRect(surface, NULL); 76 SDL_SetClipRect(surface, NULL);
117 SDL_FormatChanged(surface); 77 SDL_FormatChanged(surface);
118 78
119 /* Get the pixels */ 79 /* Get the pixels */
120 if (((flags & SDL_HWSURFACE) == SDL_SWSURFACE) || 80 if (surface->w && surface->h) {
121 (_this->AllocHWSurface(_this, surface) < 0)) { 81 surface->pixels = SDL_malloc(surface->h * surface->pitch);
122 if (surface->w && surface->h) { 82 if (surface->pixels == NULL) {
123 surface->pixels = SDL_malloc(surface->h * surface->pitch); 83 SDL_FreeSurface(surface);
124 if (surface->pixels == NULL) { 84 SDL_OutOfMemory();
125 SDL_FreeSurface(surface); 85 return NULL;
126 SDL_OutOfMemory(); 86 }
127 return (NULL); 87 /* This is important for bitmaps */
128 } 88 SDL_memset(surface->pixels, 0, surface->h * surface->pitch);
129 /* This is important for bitmaps */
130 SDL_memset(surface->pixels, 0, surface->h * surface->pitch);
131 }
132 } 89 }
133 90
134 /* Allocate an empty mapping */ 91 /* Allocate an empty mapping */
135 surface->map = SDL_AllocBlitMap(); 92 surface->map = SDL_AllocBlitMap();
136 if (surface->map == NULL) { 93 if (surface->map == NULL) {
165 surface->w = width; 122 surface->w = width;
166 surface->h = height; 123 surface->h = height;
167 surface->pitch = pitch; 124 surface->pitch = pitch;
168 SDL_SetClipRect(surface, NULL); 125 SDL_SetClipRect(surface, NULL);
169 } 126 }
170 return (surface); 127 return surface;
128 }
129
130 SDL_Surface *
131 SDL_CreateRGBSurfaceFromTexture(SDL_TextureID textureID)
132 {
133 SDL_Surface *surface;
134 Uint32 format;
135 int w, h;
136 int bpp;
137 Uint32 Rmask, Gmask, Bmask, Amask;
138
139 if (SDL_QueryTexture(textureID, &format, NULL, &w, &h) < 0) {
140 return NULL;
141 }
142
143 if (!SDL_PixelFormatEnumToMasks
144 (format, &bpp, &Rmask, &Gmask, &Bmask, &Amask)) {
145 SDL_SetError("Unknown texture format");
146 return NULL;
147 }
148
149 surface = SDL_CreateRGBSurface(SDL_SWSURFACE, 0, 0, bpp,
150 Rmask, Gmask, Bmask, Amask);
151 if (surface != NULL) {
152 surface->flags |= (SDL_HWSURFACE | SDL_PREALLOC);
153 surface->w = width;
154 surface->h = height;
155 SDL_SetClipRect(surface, NULL);
156 }
157 return surface;
158 }
159
160 /*
161 * Set the palette in a blittable surface
162 */
163 int
164 SDL_SetColors(SDL_Surface * surface, SDL_Color * colors, int firstcolor,
165 int ncolors)
166 {
167 SDL_Palette *pal;
168 int gotall;
169 int palsize;
170
171 /* Verify the parameters */
172 pal = surface->format->palette;
173 if (!pal) {
174 return 0; /* not a palettized surface */
175 }
176 gotall = 1;
177 palsize = 1 << surface->format->BitsPerPixel;
178 if (ncolors > (palsize - firstcolor)) {
179 ncolors = (palsize - firstcolor);
180 gotall = 0;
181 }
182
183 if (colors != (pal->colors + firstcolor)) {
184 SDL_memcpy(pal->colors + firstcolor, colors,
185 ncolors * sizeof(*colors));
186 }
187 SDL_FormatChanged(surface);
188
189 if (surface->flags & (SDL_SHADOW_SURFACE | SDL_SCREEN_SURFACE)) {
190 gotall &= SDL_SetScreenColors(surface, colors, firstcolor, ncolors);
191 }
192 return gotall;
171 } 193 }
172 194
173 /* 195 /*
174 * Set the color key in a blittable surface 196 * Set the color key in a blittable surface
175 */ 197 */