SDL(简单媒体直接层)试图缩放我的精灵(角色)



我只是想根据窗口的重量和高度来缩放(按比例放大)我的角色。我该怎么做?我尝试了SDL_BlitScaled(newssurface,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);

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

例如,如果您希望您的原生分辨率为600x400,则需要创建一个600x400曲面,将角色和其他任何对象blit到该曲面,然后最终将该曲面缩放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);

最新更新