出于某种奇怪的原因,每当我尝试将较大的图像文件分割成(在我的情况下)32x32图片时,相反,我得到的整个图片似乎被压缩成32x32(图片整体)。虽然在玩了这些值之后,我意识到由于某种原因,SDL完全忽略了我使用SDL_Rect源(src)的请求。 这意味着我改变源(src)矩形的每个值在运行程序时都不会改变实际图像(即使是一个荒谬的值)。
#ifndef SPRITE_H_
#define SPRITE_H_
#pragma once
#include "Game.h"
struct Game;
struct Sprite
{
//Needs to be fixed :/
//Sprite(const std::string& filepath,int x,int y,int width, int height,Game*game);
Sprite();
~Sprite();
void Load(const std::string& filepath, int x, int y,int width,int height, Game*game);
void Draw(Game*game);
SDL_Rect*getDstRect(){ return &dst; }
SDL_Rect*getSrcRect(){ return &src; }
private:
SDL_Surface*surface = NULL;
SDL_Texture*texture = NULL;
SDL_Rect src;
SDL_Rect dst;
int img_width;
int img_height;
};
#endif // SPRITE_H_
#include "Sprite.h"
Sprite::Sprite()
{
}
Sprite::~Sprite()
{
SDL_DestroyTexture(texture);
surface = NULL;
texture = NULL;
}
void Sprite::Load(const std::string& filepath, int x, int y, int width, int height, Game*game)
{
bool success = true;
surface = IMG_Load(filepath.c_str());
if (surface == NULL)
{
game->getConsole()->Error("SPRITE::Surface Is Not Loaded");
success = false;
}
else
game->getConsole()->Text("SPRITE::Surface Is Loaded");
texture = SDL_CreateTextureFromSurface(game->getRenderer(), surface);
if (texture == NULL)
{
game->getConsole()->Error("SPRITE::Texture Is Not Loaded");
success = false;
}
else
game->getConsole()->Text("SPRITE::Texture is Loaded");
dst.x = x;
dst.y = y;
dst.w = height;
dst.h = height;
src.x = 0;
src.y = 0;
src.w = 0;
src.h = 0;
SDL_QueryTexture(texture, NULL, NULL, &src.w, &src.h);
SDL_FreeSurface(surface);
if (success)
game->getConsole()->Text("Loaded Sprite at " + filepath);
else
game->getConsole()->Error("SPRITE::Failed To Load Sprite at " + filepath);
}
void Sprite::Draw(Game*game)
{
SDL_RenderCopy(game->getRenderer(), texture, &src,&dst);
}
testPlayer.Load("bin/sprites/player.png", 200,200,32,32, game);
这是我在弄乱源(src)矩形后尝试更改目标(dst)矩形的值。
答案 0 :(得分:1)
https://wiki.libsdl.org/SDL_RenderCopy
dstrect - 目标SDL_Rect结构或整个渲染目标的NULL。 纹理将被拉伸以填充给定的矩形。
短版本 - 调整源矩形以使其与目标矩形具有相同的宽度和高度,并将{x,y}设置为要提取的片段的起始位置。