Wenn ich das Programm laufe ich auf der Konsole dies geschrieben bekommen:SDL Fehler: ungültiger Renderers, wenn SDL_CreateTextureFromSurface mit
Unable to create texture from Textures/background.png! SDL Error: Invalid renderer
Failed to load background texture image!
Failed to load media!
So, hier ist mein volles Programm mit allen Klassen in separaten Dateien:
//main.cpp
#include <SDL.h>
#include <SDL_image.h>
#include <stdio.h>
#include <string>
class LTexture;
#include "LTexture.h"
class Engine;
#include "Engine.h"
int main(int argc, char* args[])
{
Engine start;
return 0;
}
engine.h Datei:
#ifndef ENGINE_H
#define ENGINE_H
#include "LTexture.h"
#include "v.h"
#include <SDL.h>
#include <SDL_image.h>
#include <stdio.h>
#include <string>
class Engine: public v
{
public:
Engine();
~Engine();
bool init();
bool loadMedia();
void close();
//LTexture gFooTexture;
LTexture gBackgroundTexture;
};
#endif // ENGINE_H
Engine.cpp Datei:
#include "Engine.h"
#include <SDL.h>
#include <SDL_image.h>
#include <stdio.h>
#include <string>
Engine::Engine()
{
if(!init())
{
printf("Failed to initialize!\n");
}
else
{
if(!loadMedia())
{
printf("Failed to load media!\n");
}
else
{
bool quit = false;
SDL_Event e;
while(!quit)
{
while(SDL_PollEvent(&e) != 0)
{
if(e.type == SDL_QUIT)
{
quit = true;
}
}
SDL_SetRenderDrawColor(gRenderer, 0xFF, 0xFF, 0xFF, 0xFF);
SDL_RenderClear(gRenderer);
gBackgroundTexture.render(0, 0);
gFooTexture.render(240, 190);
SDL_RenderPresent(gRenderer);
}
}
}
}
Engine::~Engine()
{
close();
}
bool Engine::init()
{
bool success = true;
if(SDL_Init(SDL_INIT_VIDEO) < 0)
{
printf("SDL could not initialize! SDL Error: %s\n", SDL_GetError());
success = false;
}
else
{
if(!SDL_SetHint(SDL_HINT_RENDER_SCALE_QUALITY, "1"))
{
printf("Warning: Linear texture filtering not enabled!");
}
gWindow = SDL_CreateWindow("SDL Tutorial", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, SCREEN_WIDTH, SCREEN_HEIGHT, SDL_WINDOW_SHOWN);
if(gWindow == NULL)
{
printf("Window could not be created! SDL Error: %s\n", SDL_GetError());
success = false;
}
else
{
gRenderer = SDL_CreateRenderer(gWindow, -1, SDL_RENDERER_ACCELERATED);
if(gRenderer == NULL)
{
printf("Renderer could not be created! SDL Error: %s\n", SDL_GetError());
success = false;
}
else
{
SDL_SetRenderDrawColor(gRenderer, 0xFF, 0xFF, 0xFF, 0xFF);
int imgFlags = IMG_INIT_PNG;
if(!(IMG_Init(imgFlags) & imgFlags))
{
printf("SDL_image could not initialize! SDL_image Error: %s\n", IMG_GetError());
success = false;
}
}
}
}
return success;
}
bool Engine::loadMedia()
{
bool success = true;
if(!gFooTexture.loadFromFile("Textures/foo.png"))
{
printf("Failed to load Foo' texture image!\n");
success = false;
}
if(!gBackgroundTexture.loadFromFile("Textures/background.png"))
{
printf("Failed to load background texture image!\n");
success = false;
}
return success;
}
void Engine::close()
{
gFooTexture.freeup();
gBackgroundTexture.freeup();
SDL_DestroyRenderer(gRenderer);
SDL_DestroyWindow(gWindow);
gWindow = NULL;
gRenderer = NULL;
IMG_Quit();
SDL_Quit();
}
LTexture.h Datei:
#ifndef LTEXTURE_H
#define LTEXTURE_H
#include "v.h"
#include <SDL.h>
#include <SDL_image.h>
#include <stdio.h>
#include <string>
class LTexture: public v
{
public:
LTexture();
~LTexture();
bool loadFromFile(std::string path);
void freeup();
void render(int x, int y);
int getWidth();
int getHeight();
private:
SDL_Texture* mTexture;
int mWidth;
int mHeight;
};
#endif // LTEXTURE_H
LTexture.cpp Datei:
#include "LTexture.h"
#include <SDL.h>
#include <SDL_image.h>
#include <stdio.h>
#include <string>
LTexture::LTexture()
{
mTexture = NULL;
mWidth = 0;
mHeight = 0;
}
LTexture::~LTexture()
{
freeup();
}
bool LTexture::loadFromFile(std::string path)
{
freeup();
SDL_Texture* newTexture = NULL;
SDL_Surface* loadedSurface = IMG_Load(path.c_str());
if(loadedSurface == NULL)
{
printf("Unable to load image %s! SDL_image Error: %s\n", path.c_str(), IMG_GetError());
}
else
{
SDL_SetColorKey(loadedSurface, SDL_TRUE, SDL_MapRGB(loadedSurface->format, 0, 0xFF, 0xFF));
newTexture = SDL_CreateTextureFromSurface(gRenderer, loadedSurface);
if(newTexture == NULL)
{
printf("Unable to create texture from %s! SDL Error: %s\n", path.c_str(), SDL_GetError());
}
else
{
mWidth = loadedSurface->w;
mHeight = loadedSurface->h;
}
SDL_FreeSurface(loadedSurface);
}
mTexture = newTexture;
return mTexture != NULL;
}
void LTexture::freeup()
{
if(mTexture != NULL)
{
SDL_DestroyTexture(mTexture);
mTexture = NULL;
mWidth = 0;
mHeight = 0;
}
}
void LTexture::render(int x, int y)
{
SDL_Rect renderQuad = { x, y, mWidth, mHeight };
SDL_RenderCopy(gRenderer, mTexture, NULL, &renderQuad);
}
int LTexture::getWidth()
{
return mWidth;
}
int LTexture::getHeight()
{
return mHeight;
}
v.h Datei:
#ifndef V_H
#define V_H
#include <SDL.h>
#include <SDL_image.h>
#include <stdio.h>
#include <string>
class v
{
public:
v();
~v();
const int SCREEN_WIDTH = 640;
const int SCREEN_HEIGHT = 480;
SDL_Window* gWindow = NULL;
SDL_Renderer* gRenderer = NULL;
};
#endif // V_H
v.cpp Datei:
#include "v.h"
v::v()
{
//ctor
}
v::~v()
{
//dtor
}
Viel zu viel Code, fast alles ist redundant. Wo ist der [MCVE], mit dem Sie dieses Problem in den letzten Tagen behoben haben? –