I am trying to render an image using SDL_image v2.0.0, in SDL2.
I have an image called Red.png in my res/img/ folder. When I try to load the texture, and use SDL_QueryTexture() it gets the size and everything just fine. But when it comes to rendering the actual image, Ive put a rectangle outline to know where the image is, but there is no image in the box.
The class I use to load and render the texture:
class LTexture
{
public:
~LTexture()
{
SDL_DestroyTexture(image_);
renderer_ = nullptr;
image_ = nullptr;
}
void init(SDL_Renderer* renderer)
{
printf("init texture\n");
renderer_ = renderer;
}
void loadBMP(std::string filename)
{
printf("load texture\n");
image_ = IMG_LoadTexture(renderer_, ("res/img/"+filename).c_str());
SDL_QueryTexture(image_, NULL, NULL, &imgrect.w, &imgrect.h);
}
void render(int x, int y)
{
imgrect.x = x;
imgrect.y = y;
SDL_SetRenderDrawColor(renderer_, 128, 128, 128, 255);
if (image_ != nullptr && renderer_ != nullptr)
{
printf("%i, %i\n", imgrect.x, imgrect.y);
SDL_RenderDrawRect(renderer_, &imgrect);
SDL_RenderCopy(renderer_, image_, &imgrect, &imgrect);
}
}
bool isLoaded()
{
return image_ != nullptr;
}
private:
SDL_Renderer* renderer_ = nullptr;
SDL_Texture* image_ = nullptr;
SDL_Rect imgrect;
};
I know it correctly gets the renderer and loads the image because the DrawRect function works, and if you didn't guess by the name, Red.png is a red rectangle.
Pass
nullptr
in forsrcrect
in yourSDL_RenderCopy()
call:Right now if
x
and/ory
are larger thanimage_
SDL will clipsrcrect
to the extents ofimage_
, end up with an empty rect, and do nothing.Example:
red.png
for reference: