0

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!

user3820248
  • 84
  • 2
  • 10
  • Are you sure this source is correct? `tex` seems coming out of nothing... – 6502 Feb 18 '15 at 06:56
  • @6502 The problem is fixed! It is just a mistake here, sorry! But can you figure out that why it is not working? If objGame is public and I compare obj.objGame->getRenderer() with scr.getRenderer(), its the same address. Then, why its not working! – user3820248 Feb 18 '15 at 07:15
  • This is not the source code behaving strangely: it's the part **you think** is having problems also edited the way **you think** is pertinent. However if there was no problem in your thinking this program would be working. Is there anything else **you think** is irrelevant omitted (for example destructors, copy constructors, assigment operators)? – 6502 Feb 18 '15 at 07:25
  • Is there some kind of problem related with **Shallow copy** and **Deep copy** thing. I really know nothing about it. – user3820248 Feb 18 '15 at 07:27
  • Ok, my thinking is free from problems now! Tell me the solution! I am eager to know! – user3820248 Feb 18 '15 at 07:29
  • Its a duplicate, thats ok! But can somebody make this code run in correct way (correct this code)? – user3820248 Feb 18 '15 at 11:26

0 Answers0