I'm trying to make a simple game and when I try rendering my SDL_Texture
, I get an inexplicable error. I've set up everything right, I'm able to successfully clear the screen with SDL_RenderClear
, and my texture isn't null, so it should have been created properly. But when I try calling the render()
function I get an error, and SDL_GetError()
returns "Invalid texture".
Edit: I have now created an MCVE as requested, and I tested it to verify that it reproduces the error. It should display the image at the path "gfx/grid.bmp" in the window, but instead it gives me the error. Here is the full MCVE:
#include <SDL.h>
#include <string>
#include <iostream>
#undef main
class Texture{
private:
SDL_Texture* texture;
SDL_Renderer* renderer;
int width;
int height;
public:
Texture(){
texture = nullptr;
}
Texture (SDL_Renderer* ren, std::string path){
texture = nullptr;
SDL_Surface* surface = nullptr;
surface = SDL_LoadBMP(path.c_str());
if(surface == nullptr) return;
renderer = ren;
texture = SDL_CreateTextureFromSurface( renderer, surface );
if(texture == 0) return;
width = surface->w;
height = surface->h;
SDL_FreeSurface(surface);
std::cout << "Texture '" << path << "' successfully loaded\n";
}
bool loaded(){
return texture != 0;
}
bool render(int x = 0, int y = 0){
SDL_Rect dest;
dest.x = x;
dest.y = y;
dest.w = width;
dest.h = height;
return SDL_RenderCopy(renderer, texture, nullptr, &dest) == 0;
}
void unload(){
if(loaded()){
SDL_DestroyTexture(texture);
texture = nullptr;
renderer = nullptr;
width = 0;
height = 0;
}
}
~Texture(){
unload();
}
};
class CApp{
private:
bool running = true;
SDL_Window* window = nullptr;
SDL_Renderer* renderer = nullptr;
Texture graphic_grid;
bool render = true;
public:
int OnExecute();
bool OnInit();
void OnEvent(SDL_Event* event);
void OnRender();
void OnCleanup();
};
bool CApp::OnInit(){
if(SDL_Init(SDL_INIT_EVERYTHING) < 0){
return false;
}
if((window = SDL_CreateWindow("Simple Tic-Tac-Toe", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, 600, 600, SDL_WINDOW_SHOWN)) == nullptr){
return false;
}
renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED);
if(renderer == nullptr){
std::cout << "Renderer failed to load! Error: " << SDL_GetError();
return false;
}
SDL_SetRenderDrawColor( renderer, 0xFF, 0xFF, 0xFF, 0xFF );
graphic_grid = Texture(renderer, "gfx/grid.bmp");
if(!graphic_grid.loaded()){
std::cout << "Image failed to load! Error: " << SDL_GetError();
return false;
}
std::cout << "Initialized fully\n";
return true;
}
void CApp::OnEvent(SDL_Event* event){
switch(event->type == SDL_QUIT){
running = false;
}
}
void CApp::OnRender(){
if(!render) return;
if(SDL_RenderClear(renderer) == 0){
std::cout << "Successfully cleared screen\n";
}
if(!graphic_grid.loaded()){
std::cout << "Texture not loaded!\n";
}else{
std::cout << "Texture not null!\n";
}
//This is the place where I get the "Invalid texture" error, everything else works fine
if(!graphic_grid.render()){
std::cout << "Failed to render image! Error: " << SDL_GetError() << '\n';
}
SDL_RenderPresent(renderer);
render = false;
}
void CApp::OnCleanup(){
graphic_grid.unload();
SDL_DestroyRenderer(renderer);
SDL_DestroyWindow(window);
renderer = nullptr;
window = nullptr;
SDL_Quit();
}
int CApp::OnExecute(){
if(OnInit() == false){
return -1;
}
SDL_Event event;
while(running){
while(SDL_PollEvent(&event)){
OnEvent(&event);
}
OnRender();
}
OnCleanup();
return 0;
}
int main(){
CApp theApp;
return theApp.OnExecute();
}
I have searched around and can't find anything that explains what my error could be. The closest thing I could find was this question, but that error was caused by the renderer being destroyed before the texture, which isn't my case.
I really hope someone can tell me what might be causing this error, any suggestions are appreciated!