37 SDL_Rect surface::srcrect;
38 SDL_Rect surface::dstrect;
46 mask_changed_ =
false;
47 alpha_ = SDL_ALPHA_OPAQUE;
48 alpha_channel_ =
false;
75 mask_changed_ =
false;
78 SDL_Surface *s2 = SDL_CreateRGBSurfaceFrom(NULL, s1->w, s1->h,
79 32, 0, R_MASK, G_MASK, B_MASK, A_MASK);
81 Info->
Format = s2->format->format;
83 SDL_Texture *tmp = SDL_CreateTexture (screen::get_renderer(), Info->
Format, SDL_TEXTUREACCESS_STREAMING, s1->w, s1->h);
84 if (!tmp) std::cout <<
"*** surface_sdl::set_mask: " << SDL_GetError() << std::endl;
86 SDL_LockTexture(tmp, NULL, &s2->pixels, &s2->pitch);
88 SDL_SetSurfaceAlphaMod (s1, SDL_ALPHA_OPAQUE);
89 SDL_SetSurfaceBlendMode (s1, SDL_BLENDMODE_NONE);
90 SDL_BlitSurface (s1, NULL, s2, NULL);
92 SDL_UnlockTexture(tmp);
99 alpha_channel_ =
true;
108 if ((t == 255) && (alpha_ != 255) &&
Surface)
110 SDL_SetTextureAlphaMod(
Surface, t);
111 SDL_SetTextureBlendMode(
Surface, SDL_BLENDMODE_NONE);
114 else if (!alpha_channel && alpha_channel_ &&
Surface)
116 SDL_SetTextureBlendMode(
Surface, SDL_BLENDMODE_NONE);
120 alpha_channel_ = alpha_channel || is_masked_;
127 u_int32 rmask, gmask, bmask, amask;
131 SDL_PixelFormatEnumToMasks(Info->
Format, &bpp, &rmask, &gmask, &bmask, &amask);
135 bpp, Info->
Pitch, rmask, gmask, bmask, amask);
139 s = SDL_CreateRGBSurfaceFrom(Info->
Pixels, rect->w, rect->h,
140 bpp, Info->
Pitch, rmask, gmask, bmask, amask);
145 u_int32 trans_col = alpha_channel_? SDL_MapRGBA(s->format, 0xFF, 0x00, 0xFF, 0xFF) : SDL_MapRGB(s->format, 0xFF, 0x00, 0xFF);
146 SDL_SetColorKey(s, 1, trans_col);
149 if (alpha_channel_ || alpha_ != 255)
151 if (!alpha_channel_ || is_masked_) SDL_SetSurfaceAlphaMod (s, alpha_);
152 SDL_SetSurfaceBlendMode (s, SDL_BLENDMODE_BLEND);
162 setup_rects (x, y, sx, sy, sl, sh, da_opt);
164 if (!dstrect.w || !dstrect.h)
170 if (alpha_channel_ || alpha_ != 255)
172 if (!alpha_channel_ || is_masked_) SDL_SetTextureAlphaMod(
Surface, alpha_);
173 SDL_SetTextureBlendMode(
Surface, SDL_BLENDMODE_BLEND);
178 srcrect.x *=
scale();
179 srcrect.y *=
scale();
180 srcrect.w *=
scale();
181 srcrect.h *=
scale();
192 SDL_RenderCopy (screen::get_renderer(),
Surface, &srcrect, &dstrect);
204 srcrect.x *=
scale();
205 srcrect.y *=
scale();
206 srcrect.w *=
scale();
207 srcrect.h *=
scale();
210 dstrect.y *=
scale();
211 dstrect.w *=
scale();
212 dstrect.h *=
scale();
214 if (dstrect.x < 0) dstrect.x = 0;
215 if (dstrect.y < 0) dstrect.y = 0;
219 if (dstrect.x + dstrect.w > target->
length() *
scale()) dstrect.w = target->
length() *
scale() - dstrect.x;
220 if (dstrect.y + dstrect.h > target->
height() *
scale()) dstrect.h = target->
height() *
scale() - dstrect.y;
221 if (dstrect.w <= 0 || dstrect.h <= 0)
return;
223 srcrect.w = dstrect.w;
224 srcrect.h = dstrect.h;
230 SDL_BlitSurface (source_surf, &srcrect, target_surf, NULL);
234 SDL_FreeSurface(source_surf);
235 SDL_FreeSurface(target_surf);
261 unmap_color(col, r, g, b, a);
271 SDL_SetRenderDrawBlendMode(screen::get_renderer(), SDL_BLENDMODE_NONE);
272 SDL_SetRenderDrawColor(screen::get_renderer(), r, g, b, a);
273 SDL_RenderFillRect(screen::get_renderer(), &dstrect);
279 col = map_color(255, 0, 255, SDL_ALPHA_TRANSPARENT);
285 dstrect.y *=
scale();
286 dstrect.w *=
scale();
287 dstrect.h *=
scale();
289 if (dstrect.x < 0) dstrect.x = 0;
294 if (dstrect.w <= 0 || dstrect.h <= 0)
return;
299 std::fill_n(src, dstrect.w * dstrect.h, col);
301 SDL_ConvertPixels (dstrect.w, dstrect.h,
302 SDL_PIXELFORMAT_ARGB8888, (
const void*) src, dstrect.w*4,
313 return (a << 24) | (r << 16) | (g << 8) | b;
319 a = (col & 0xFF000000) >> 24;
320 r = (col & 0x00FF0000) >> 16;
321 g = (col & 0x0000FF00) >> 8;
322 b = (col & 0x000000FF);
353 unmap_color(col, r, g, b, a);
364 SDL_SetRenderDrawBlendMode(screen::get_renderer(), SDL_BLENDMODE_NONE);
365 SDL_SetRenderDrawColor(screen::get_renderer(), r, g, b, a);
366 SDL_RenderDrawPoint(screen::get_renderer(), x, y);
370 if (!Info->
Pixels)
return;
376 unmap_color(col, r, g, b, a);
380 case SDL_PIXELFORMAT_RGB24:
385 case SDL_PIXELFORMAT_BGR24:
394 #ifdef __BIG_ENDIAN__
395 unmap_color(col, b, a, g, r);
397 unmap_color(col, r, g, b, a);
402 case SDL_PIXELFORMAT_BGR888:
407 case SDL_PIXELFORMAT_RGBA8888:
413 case SDL_PIXELFORMAT_ARGB8888:
419 case SDL_PIXELFORMAT_RGB888:
424 case SDL_PIXELFORMAT_ABGR8888:
430 case SDL_PIXELFORMAT_BGRA8888:
437 std::cout <<
"*** surface::put_pix: Unsupported format " << SDL_GetPixelFormatName(Info->
Format) << std::endl;
443 if (!Info->
Pixels)
return 0;
445 u_int8 r, g, b, a = SDL_ALPHA_OPAQUE;
452 case SDL_PIXELFORMAT_RGB24:
457 case SDL_PIXELFORMAT_BGR24:
464 return map_color (r, g, b, a);
469 case SDL_PIXELFORMAT_BGR888:
474 case SDL_PIXELFORMAT_BGRA8888:
480 case SDL_PIXELFORMAT_ABGR8888:
486 case SDL_PIXELFORMAT_RGB888:
491 case SDL_PIXELFORMAT_ARGB8888:
497 case SDL_PIXELFORMAT_RGBA8888:
504 std::cout <<
"surface::get_pix: Unsupported format " << SDL_GetPixelFormatName(Info->
Format) << std::endl;
508 #ifdef __BIG_ENDIAN__
509 return map_color (g, r, a, b);
511 return map_color (r, g, b, a);
521 if (
scale() == new_scale)
532 u_int32 rmask, gmask, bmask, amask;
536 SDL_PixelFormatEnumToMasks(Info->
Format, &bpp, &rmask, &gmask, &bmask, &amask);
537 SDL_Texture *target = SDL_CreateTexture (screen::get_renderer(), Info->
Format, SDL_TEXTUREACCESS_STREAMING,
length()*new_scale,
height()*new_scale);
538 if (!target) std::cout <<
"*** surface_sdl::set_scale: " << SDL_GetError() << std::endl;
541 SDL_LockTexture(target, NULL, (
void**)&target_data, &pitch);
542 SDL_Surface *target_surf = SDL_CreateRGBSurfaceFrom(target_data,
length()*new_scale,
height()*new_scale, bpp, pitch, rmask, gmask, bmask, amask);
544 s_int32 target_line_length = target_surf->format->BytesPerPixel *
length()*new_scale;
549 for (
s_int32 src_y = 0; src_y < real_height; src_y +=
scale())
555 for (
s_int32 src_x = 0; src_x < real_length; src_x +=
scale())
558 for (target_x_end += new_scale; target_x < target_x_end; ++target_x)
566 for (
u_int32 i = 1; i < new_scale; i++)
568 u_int8 *target_next_line = target_data + target_surf->pitch;
569 memcpy (target_next_line, target_data, target_line_length);
570 target_data = target_next_line;
574 target_data += target_surf->pitch;
580 Info->
Pixels = target_surf->pixels;
584 SDL_FreeSurface(target_surf);
592 alpha_ = src.
alpha();
603 void *src_pixels, *dst_pixels;
606 Surface = SDL_CreateTexture (screen::get_renderer(), Info->
Format, SDL_TEXTUREACCESS_STREAMING, l, h);
607 if (!
Surface) std::cout <<
"*** surface_sdl::operator=: " << SDL_GetError() << std::endl;
609 SDL_LockTexture(src.
Surface, NULL, &src_pixels, &pitch);
610 SDL_LockTexture(
Surface, NULL, &dst_pixels, &pitch);
614 memcpy (dst_pixels, src_pixels, pitch);
615 src_pixels = (
u_int8*) src_pixels + pitch;
616 dst_pixels = (
u_int8*) dst_pixels + pitch;
640 if (l == 0 || h == 0)
647 if (screen::get_renderer())
649 #ifdef __BIG_ENDIAN__
654 Surface = SDL_CreateTexture (screen::get_renderer(), Info->
Format, SDL_TEXTUREACCESS_STREAMING, l *
scale(), h *
scale());
655 if (!
Surface) std::cout <<
"*** surface::resize: " << SDL_GetError() << std::endl;
660 std::cout <<
"*** surface:resize: screen surface not initialized!" << std::endl;
675 alpha_channel_ =
false;
689 if (l == 0 || h == 0)
695 alpha_channel_ = alpha_mask != 0;
697 Info->
Format = SDL_MasksToPixelFormatEnum (bytes_per_pixel * 8, red_mask, green_mask, blue_mask, alpha_mask);
698 Surface = SDL_CreateTexture (screen::get_renderer(), Info->
Format, SDL_TEXTUREACCESS_STREAMING, l, h);
699 if (!
Surface) std::cerr <<
"*** surface::set_data: " << SDL_GetError() << std::endl;
703 int pitch = bytes_per_pixel * l;
708 memcpy (Info->
Pixels, src, pitch);
709 src = (
u_int8*) src + pitch;
721 void * surface::get_data (
u_int8 bytes_per_pixel,
731 return tmp.get_data(bytes_per_pixel, red_mask, green_mask, blue_mask, alpha_mask);
736 u_int32 dst_format = SDL_MasksToPixelFormatEnum(bytes_per_pixel*8, red_mask, green_mask, blue_mask, alpha_mask);
737 void *dst_pixels = calloc (
length() *
height(), bytes_per_pixel);
738 int dst_pitch =
length() * bytes_per_pixel;
740 if (dst_format != Info->
Format)
752 dest = dest + dst_pitch;
770 im_zone.assign_drawing_area (draw_to);
772 SDL_Rect da_int = im_zone.setup_rects ();
773 dstrect.x = da_int.x;
774 dstrect.y = da_int.y;
775 dstrect.w = da_int.w;
776 dstrect.h = da_int.h;
779 srcrect.x = x < dstrect.x ? sx + dstrect.x - x : sx;
780 srcrect.y = y < dstrect.y ? sy + dstrect.y - y : sy;