here is my c++ code:
#include <SDL2/SDL.h>
SDL_Window* gWindow = NULL;
SDL_Renderer* gRenderer = NULL;
int main( int argc, char* args[] )
{
SDL_Init( SDL_INIT_VIDEO );
gWindow = SDL_CreateWindow( "test", 0,0,640,480, SDL_WINDOW_SHOWN );
gRenderer = SDL_CreateRenderer(gWindow,-1,SDL_RENDERER_ACCELERATED);
SDL_Surface* x = SDL_CreateRGBSurface(0,50,50,32,0xFF,0xFF00,0xFF0000,0XFF000000);
SDL_Surface* y = SDL_CreateRGBSurface(0,640,480,32,0xFF,0xFF00,0xFF0000,0XFF000000);
SDL_Rect a = {0,0,50,50};
SDL_RenderClear(gRenderer);
SDL_FillRect(x,&a,SDL_MapRGBA(x->format,255,255,0,255));
SDL_Rect dest = {0,0,100,100};
SDL_BlitScaled( x, &a, y, &dest ); //Does nothing
dest.x = 200;
SDL_BlitSurface( x, &a, y, &dest );
SDL_Texture* t;
t = SDL_CreateTextureFromSurface(gRenderer,y);
SDL_RenderCopy(gRenderer,t,NULL,NULL);
SDL_RenderPresent(gRenderer);
SDL_Delay(2000);
return 0;
}
well,when you compile this code,you will see there is ONLY ONE rect on the screen.The function SDL_BlitScaled
does nothing.
I'm working with Archlinux,gcc 4.8.2,SDL 2.0.1
According to Retired Ninja's answer,I changed my code:
#include <SDL2/SDL.h>
SDL_Window* gWindow = NULL;
SDL_Renderer* gRenderer = NULL;
int main( int argc, char* args[] )
{
SDL_Init( SDL_INIT_VIDEO );
gWindow = SDL_CreateWindow( "test", 0,0,640,480, SDL_WINDOW_SHOWN );
gRenderer = SDL_CreateRenderer(gWindow,-1,SDL_RENDERER_ACCELERATED);
SDL_Surface* x = SDL_CreateRGBSurface(0,50,50,32,0xFF,0xFF00,0xFF0000,0XFF000000);
SDL_Surface* y = SDL_CreateRGBSurface(0,640,480,32,0xFF,0xFF00,0xFF0000,0XFF000000);
SDL_SetRenderDrawColor(gRenderer,255,255,255,255);
SDL_RenderClear(gRenderer);
SDL_Rect a = {0,0,25,25};
SDL_FillRect(x,&a,SDL_MapRGBA(x->format,255,255,0,255));
a.x = 25;a.y = 25;
SDL_FillRect(x,&a,SDL_MapRGBA(x->format,255,0,0,255));
SDL_FillRect(y,&y->clip_rect,SDL_MapRGBA(y->format,255,0,255,128));
SDL_Rect dest = {0,0,100,100};
SDL_Rect x_rect = {0,0,50,50};
SDL_SetSurfaceBlendMode(x,SDL_BLENDMODE_NONE);
SDL_BlitScaled( x, &x_rect, y, &dest );
SDL_Texture* t;
t = SDL_CreateTextureFromSurface(gRenderer,y);
SDL_RenderCopy(gRenderer,t,NULL,NULL);
SDL_RenderPresent(gRenderer);
SDL_Delay(2000);
return 0;
}
there is another problem appeared.I try to copy x
into y
,The x
covered y
entirely,even through there are transparent area in x
.But if I don't use SDL_BLENDMODE_NONE,the alpha of the area copied into y
will be set into 128,which is not my purpose.
Filling y
with a color makes it work.
I added:
SDL_FillRect(y, &y->clip_rect, SDL_MapRGBA(x->format, 255, 0, 0, 255));
after the SDL_FillRect
for x
and it worked.
Some further experimentation of just filling a portion of y
with alpha leads me to believe that SDL_BlitScaled
isn't copying the alpha from the source surface so unless the dest surface has alpha where the destination rect is you won't see the result.
Some more experimentation shows that the default blend mode for a surface is SDL_BLENDMODE_BLEND
and changing the mode for y
to SDL_BLENDMODE_NONE
also makes it work.
来源:https://stackoverflow.com/questions/20587999/sdl-blitscaled-doesnt-work