I'm starting to learn SDL3 and I'm getting this weird error while trying to render a texture using SDL_RenderTexture
The error messages are:
Exception produced at 0x00007FFCA9067788 in A Fairy Tail.exe: Microsoft C++ exception: _com_error in memory location 0x000000F86C0FED60
And the SDL_GetError()
one:
FUNCTION, ID3D11Device1::CreatePixelShader: Parameter is not correct
Apparently they only pop up when I use the function SDL_GetError() to check if the texture has been loaded propperly, specifically these lines of code:
if(SDL_RenderTexture(renderer, fairy, NULL, NULL)
{
SDL_Log("IMG Error: %s", SDL_GetError());
}
Strangely, they don't pop up when I check the same condition but for a false value, only for true values as in the lines above, meaning (I guess) the texture is being rendered, but it simply doesn't show up at the window and indstead the console sends these error messages.
Here's the complete code for better understanding:
//Header file mygame.h
#pragma once
#include <iostream>
#include "SDL3/SDL.h"
#include "SDL_image.h"
class Game
{
public:
Game();
~Game();
void init(const char*, int xpos, int ypos, int width, int height, bool fullscreen);
void handle_events();
void update();
void render();
void clean();
bool running();
private:
bool is_running = NULL;
SDL_Window* window = NULL;
SDL_Renderer* renderer = NULL;
//Source File for the function definitions of the above header file mygame.cpp
#include "mygame.h"
SDL_Texture* fairy = NULL;
Game::Game() {}
Game::~Game() {}
//Initialization function
void Game::init(const char* title, int xpos, int ypos, int width, int height, bool fullscreen)
{
int flags = 0;
if (fullscreen)
{
flags = SDL_WINDOW_FULLSCREEN;
}
if(SDL_Init(SDL_INIT_VIDEO) < 0)
{
std::cout << "Error: Couldn't initialize SDL!" << std::endl;
is_running = false;
return;
}
//Creating Window
window = SDL_CreateWindow(title, width, height, flags);
if(window)
{
std::cout << "Window Created!";
}
//Creating Renderer
renderer = SDL_CreateRenderer(window, NULL);
if(renderer)
{
std::cout << "Renderer Created" << std::endl;
}
is_running = true;
fairy = IMG_LoadTexture(renderer, "assets/hadita.png");
}
void Game::handle_events()
{
SDL_Event event;
SDL_PollEvent(&event);
switch(event.type)
{
case SDL_EVENT_QUIT:
is_running = false;
default:
break;
}
}
void Game::update(){}
void Game::render()
{
SDL_RenderClear(renderer);
if(SDL_RenderTexture(renderer, fairy))
{
SDL_Log("IMG Render Error: %s", SDL_GetError());
}
SDL_RenderPresent(renderer);
}
void Game:clean()
{
SDL_DestroyWindow(window);
SDL_DestroyRenderer(renderer);
SDL_DestroyTexture(fairy);
SDL_Quit();
std::cout << "Game cleaned";
}
bool Game::running()
{
return is_running;
}
//Now finally the main.cpp file where the game loop is
#include "mygame.h"
Game* game = nullptr;
int main(int argc, const char* argv[])
{
game = new Game();
game->init("A Fairy Tale", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, 800, 600, false);
while (game->running())
{
game->handle_events();
game->update();
game->render();
}
game -> clean();
return 0;
}
I'm starting to learn SDL3 and I'm getting this weird error while trying to render a texture using SDL_RenderTexture
The error messages are:
Exception produced at 0x00007FFCA9067788 in A Fairy Tail.exe: Microsoft C++ exception: _com_error in memory location 0x000000F86C0FED60
And the SDL_GetError()
one:
FUNCTION, ID3D11Device1::CreatePixelShader: Parameter is not correct
Apparently they only pop up when I use the function SDL_GetError() to check if the texture has been loaded propperly, specifically these lines of code:
if(SDL_RenderTexture(renderer, fairy, NULL, NULL)
{
SDL_Log("IMG Error: %s", SDL_GetError());
}
Strangely, they don't pop up when I check the same condition but for a false value, only for true values as in the lines above, meaning (I guess) the texture is being rendered, but it simply doesn't show up at the window and indstead the console sends these error messages.
Here's the complete code for better understanding:
//Header file mygame.h
#pragma once
#include <iostream>
#include "SDL3/SDL.h"
#include "SDL_image.h"
class Game
{
public:
Game();
~Game();
void init(const char*, int xpos, int ypos, int width, int height, bool fullscreen);
void handle_events();
void update();
void render();
void clean();
bool running();
private:
bool is_running = NULL;
SDL_Window* window = NULL;
SDL_Renderer* renderer = NULL;
//Source File for the function definitions of the above header file mygame.cpp
#include "mygame.h"
SDL_Texture* fairy = NULL;
Game::Game() {}
Game::~Game() {}
//Initialization function
void Game::init(const char* title, int xpos, int ypos, int width, int height, bool fullscreen)
{
int flags = 0;
if (fullscreen)
{
flags = SDL_WINDOW_FULLSCREEN;
}
if(SDL_Init(SDL_INIT_VIDEO) < 0)
{
std::cout << "Error: Couldn't initialize SDL!" << std::endl;
is_running = false;
return;
}
//Creating Window
window = SDL_CreateWindow(title, width, height, flags);
if(window)
{
std::cout << "Window Created!";
}
//Creating Renderer
renderer = SDL_CreateRenderer(window, NULL);
if(renderer)
{
std::cout << "Renderer Created" << std::endl;
}
is_running = true;
fairy = IMG_LoadTexture(renderer, "assets/hadita.png");
}
void Game::handle_events()
{
SDL_Event event;
SDL_PollEvent(&event);
switch(event.type)
{
case SDL_EVENT_QUIT:
is_running = false;
default:
break;
}
}
void Game::update(){}
void Game::render()
{
SDL_RenderClear(renderer);
if(SDL_RenderTexture(renderer, fairy))
{
SDL_Log("IMG Render Error: %s", SDL_GetError());
}
SDL_RenderPresent(renderer);
}
void Game:clean()
{
SDL_DestroyWindow(window);
SDL_DestroyRenderer(renderer);
SDL_DestroyTexture(fairy);
SDL_Quit();
std::cout << "Game cleaned";
}
bool Game::running()
{
return is_running;
}
//Now finally the main.cpp file where the game loop is
#include "mygame.h"
Game* game = nullptr;
int main(int argc, const char* argv[])
{
game = new Game();
game->init("A Fairy Tale", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, 800, 600, false);
while (game->running())
{
game->handle_events();
game->update();
game->render();
}
game -> clean();
return 0;
}
Always use error checks only for getting a error means when something is not returning correctly.
if (!SDL_RenderTexture(renderer, fairy)) { }
Just think like that if SDL_RenderTexture()
is returning correctly why would there be an error. SDL_GetError()
gives you the latest error at that point.
render
method really destroy all the SDL3 state? Your call toSDL_RenderTexture
is nowhere to be seen... I think you screwed up copy pasting. – Botje Commented Jan 31 at 12:03Game::render
looks like it should beGame::clean
, and there'sbooll
... Please post real code. – molbdnilo Commented Jan 31 at 12:05IMG_LoadTexture
returns NULL on failure. Check both calls please. – Botje Commented Jan 31 at 12:24