SDL_BlitScaled doesn't work

余生颓废 提交于 2019-12-08 04:33:45

问题


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.


回答1:


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!