SDL(Simple MediaDirect Layer)试图扩展我的精灵(角色)

时间:2014-09-16 04:02:25

标签: c++ c gcc 2d sdl

我只是想根据窗户的重量和高度来缩放(按比例缩小)我的角色。我怎样才能做到这一点? 我试过SDL_BlitScaled(newsurface,NULL,screen,NULL);但它不起作用。 我的代码:

SDL_Surface* screen = SDL_GetWindowSurface(win);
SDL_Surface* mySprite = IMG_Load( SPRITE_PNG_PATH);

SDL_Rect rcSprite;
rcSprite.x = 250;
rcSprite.y = 100;

SDL_BlitSurface(mySprite, NULL, screen, &rcSprite);

1 个答案:

答案 0 :(得分:0)

实现这一目标的最佳方法是拥有一个中间曲面,其宽度和高度是游戏的原始分辨率。然后,将整个帧渲染到此曲面,并使用SDL_BlitScaled函数将该曲面渲染到窗口。

例如,如果你想要你的原始分辨率是600x400,那么你将创建一个600x400的表面,将你的角色和其他任何东西放到那个表面上,然后最终缩放到窗口的blit。如果您将窗口大小调整为1200x800,那么一切看起来都要大两倍。

SDL_Surface* screen = SDL_GetWindowSurface(win);

// Create a surface with our native resolution
int NATIVE_WIDTH = 600;
int NATIVE_HEIGHT = 400;
SDL_Surface* frame = SDL_CreateRGBSurface(0, NATIVE_WIDTH, NATIVE_HEIGHT, 
    screen->format->BitsPerPixel, screen->format->Rmask, 
    screen->format->Gmask, screen->format->Bmask, screen->format->Amask);
assert(frameSurface);

SDL_Surface* mySprite = IMG_Load( SPRITE_PNG_PATH);

SDL_Rect rcSprite;
rcSprite.x = 250;
rcSprite.y = 100;

// Blit everything to the intermediate surface, instead of the screen.
SDL_BlitSurface(mySprite, NULL, frame, &rcSprite);

// Once we've drawn the entire frame, we blit the intermediate surface to
// the window, using BlitScaled to ensure it gets scaled to fit the window.
SDL_BlitScaled(frame, NULL, screen, NULL);