c++ - SDL(简单媒体直接层)尝试缩放我的 Sprite (角色)

标签 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);

最佳答案

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

例如,如果您希望原始分辨率为 600x400,则可以创建一个 600x400 表面,将角色和其他任何内容传输到该表面,然后最终将该表面缩放到窗口。如果将窗口大小调整为 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);

关于c++ - SDL(简单媒体直接层)尝试缩放我的 Sprite (角色),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25860443/

相关文章:

python - 使用 python c api 获取共享对象的完整路径

C++11:非成员 rbegin()/rend() 函数

c - 从 If 语句切换到 Switch 语句

python - 从 C 调用 python hello world 函数,解析字符串参数

c++ - 如何在Linux中编译隐藏源文件的C++代码?

linux - 为什么 binutils 或 gcc 安装在两个位置?

c++ - 无法从 MFC DLL 创建模式对话框

c++ - 复制指针列表没有循环

c++ - 在VTK下保存一个Mesh文件

c - 对多组求和