I made two classes: Screen and Object. They are using SDL2. There is no problem with any other thing other than the one written below the codes. Following are the codes:
In Screen.h:
class Screen
{
public:
Screen( const char* title );
bool initialize(int xpos, int ypos, int w, int h, int wFlags, int rFlags);
SDL_Renderer* getRenderer();
void setRenderColor(int r, int g, int b, int a);
void clear();
void updateScreen();
private:
SDL_Renderer *renderer;
SDL_Window *gWindow;
std::string gTitle;
};
In Screen.cpp:
Screen::Screen( const char* title )
{
gWindow = NULL;
renderer = NULL;
gTitle = title;
quit = false;
}
bool Screen::initialize( int xpos, int ypos, int w, int h, int wFlags, int rFlags )
{
if( SDL_Init( SDL_INIT_EVERYTHING ) >= 0 )
{
gWindow = SDL_CreateWindow( gTitle.c_str(), xpos, ypos, w, h, wFlags );
if(gWindow != NULL)
{
renderer = SDL_CreateRenderer( gWindow, -1, rFlags );
if(renderer == NULL)
{
std::cerr << "Failed to start renderer! Error: " << SDL_GetError() << std::endl;
return false;
}
if(IMG_Init( IMG_INIT_PNG ) < 0 )
{
std::cerr << "Failed to initialize .png image loader! Error: " << IMG_GetError() << std::endl;
return false;
}
}
else
{
std::cerr << "Failed to create window! Error: " << SDL_GetError() << std::endl;
return false;
}
}
else
{
std::cerr << "Failed to initialize! Error: " << SDL_GetError() << std::endl;
return false;
}
return true;
}
SDL_Renderer* Screen::getRenderer()
{
return renderer;
}
void Screen::setRenderColor( int r, int g, int b, int a )
{
renderDrawR = r;
renderDrawG = g;
renderDrawB = b;
renderDrawA = a;
SDL_SetRenderDrawColor( renderer, r, g, b, a );
}
void Screen::clear()
{
SDL_RenderClear( renderer );
}
void Screen::updateScreen()
{
SDL_RenderPresent( renderer );
}
In Object.h:
Screen* systemForObjects;
class Object
{
public:
Object( SDL_Rect outClip, SDL_Rect inClip, const char *img, Screen *game = systemForObjects );
SDL_Rect inClip, outClip;
void putToScreen();
private:
Screen *objGame;
SDL_Texture tex;
};
In Object.cpp:
Object::Object( SDL_Rect outClip, SDL_Rect inClip, const char* img, Screen *game )
{
objGame = game;
this->inClip = inClip;
this->outClip = outClip;
SDL_Surface *tSurf = IMG_Load( img );
tex = SDL_CreateTextureFromSurface( objGame->getRenderer(), tSurf );
SDL_FreeSurface( tSurf );
SDL_QueryTexture(tex, NULL, NULL,
&inClip.w, &inClip.h);
}
Object::putToScreen()
{
SDL_RenderCopy(objGame->getRenderer(), tex, &inClip, &outClip);
}
In main function:
#include <...> //No problem here
#include "..." //No problem here
int main( int argc, char* args )
{
Screen scr("Window");
systemForObjects = &scr;
scr.initialize(...); //There's no problem at that part!
SDL_Rect out, in;
out.x = 0;
out.y = 0;
out.w = 100;
out.h = 100;
in = out;
Object obj( out, in, "ball0.png" );
while(true)
{
scr.clear();
obj.putToScreen();
scr.updateScreen();
}
}
So, the problem is that the output is a complete black screen. If I use renderer* of Screen as Global Variable(not a part of class but outside), then it shows the wanted image on the screen. This means that there is no problems with the codes having a tag "//No problem".
Can you please tell me what's the deal here? Thanks, pal!
EDIT1: Added tex! And it is just a mistake here! Sorry!