Skip to main content

c++ sdl2 : how How to pass renderer through classes?

iamI am making a game with SDL and c++ iamC++. I am trying to render simple image to the screen but iI can't. iamI am not getting any ERRORerror but iI see only black screen here is my code.

iI think because the renderer in playerplayer is not the same in gamegame they do not have the same memory adressaddress.

**my question **  : is how to pass the renderer?

iI have tried passing it through init()init() in playerplayer directly but it didn't work then how.

#ifndef GAME_H
#define GAME_H

#include <SDL2/SDL.h>
 
#include <iostream>
 

using namespace std;


 

class game
{
 
public:

    game();
    ~game();

    void init(char *title,int xpos, int ypos ,int xsize ,int ysize ,bool fullscreen);
    void loop();
    void input();
    void update();
    void render();
    SDL_Renderer* renderer;
    SDL_Window* window;

 

private:
 

    const int target_Fps = 60;
    const float TFEF = 1000/60; // time for each frame
    int delta;
    long int start_time;
    bool isrunning = true;
    int fps_counter = 0;
    unsigned int counter = 1;


 
};

#endif

#include "game.h"
 
#include "player.h"

player* pla;

game::game()
{
    cout << "something"<<endl;
    if (SDL_Init(SDL_INIT_EVERYTHING) == 0)
    {


 
        cout << "another thing" << endl;
 
        init("game",10,15,500,500,false);
    }

    loop();
}

game::~game()
{
 
}

void game::init(char *title,int xpos, int ypos ,int xsize ,int ysize ,bool fullscreen)
{
 
    window = SDL_CreateWindow(title, xpos,  ypos , xsize , ysize , fullscreen);
    renderer = SDL_CreateRenderer(window,-1,0);
    //SDL_SetRenderDrawColor(renderer, 0, 0,0, 255);
    SDL_RenderClear(renderer);
    SDL_RenderPresent(renderer);

    cout << "another another something" << endl;
 

    pla = new player(renderer,32,32);
    pla->init();

 

    //SDL_Texture* tex = IMG_LoadTexture(renderer,"res/pss.png");


 
}

void game::loop()
{




 
    while(isrunning)
    {
        start_time = SDL_GetTicks();

        input();
 
        update();
 
        render();

        delta = SDL_GetTicks() - start_time;
        if (delta < TFEF)
            SDL_Delay(TFEF - delta);

        fps_counter++;

        if (start_time >= 1000 * counter)
        {
            //cout <<"the FPS is : "<< fps_counter << endl;
            counter++;
            fps_counter = 0;
 
        }





 
    }
 
}

void game::input()
{
    SDL_Event e;
    SDL_PollEvent(&e);
    if (e.type == SDL_QUIT)
        SDL_Quit();
 
}

void game::update(){}

void game::render()
{
 
    SDL_RenderClear(renderer);
 
    pla->render(renderer);
 
    SDL_RenderPresent(renderer);
}
 

gameobject::~gameobject(){};


#ifndef PLAYER_H
#define PLAYER_H

#include "gameobject.h"

 

class gameobject;

class player : public gameobject
{

 
public:

    void init() override;
    void tick() override;
    void render(SDL_Renderer* renderer) override;
    SDL_Renderer* renderer;
    player(SDL_Renderer* renderer,int pwidth,int pheight);
private:

    ~player() override;
    SDL_Texture* playertex;



 
};



 

#endif // PLAYER_H

#include "player.h"

player::player(SDL_Renderer* renderer,int pwidth, int pheight)
{
 
   this->pos.w = pwidth;
   this->pos.h = pheight;
   this->pos.x = 0;
   this->pos.y = 0;
 

   this->renderer = renderer;



 
}

void player::init()
{
 
    this->playertex= IMG_LoadTexture(renderer,"res/pss.png");
    cout<<"int in player \n"<<endl;

    crop.x = 0;
    crop.y = 0;
    crop.w = 32;
    crop.h = 32;

    cout<<"pos x :"<<this->pos.x<<endl;
    cout<<"pos y : "<<this->pos.y<<endl;
    cout<<"pos w : "<<this->pos.w<<endl;
    cout<<"pos h : "<<this->pos.h<<endl;
    cout<<"crop x :"<<this->crop.x<<endl;
    cout<<"crop y :"<<this->crop.y<<endl;
    cout<<"crop w :"<<this->crop.w<<endl;
    cout<<"crop h : "<<this->crop.h<<endl;

 
}

void player::tick()
{
    
}

void player::render(SDL_Renderer *renderer)
{
 
/*
    cout<<"pos x :"<<this->pos.x<<endl;
    cout<<"pos y : "<<this->pos.y<<endl;
    cout<<"pos w : "<<this->pos.w<<endl;
    cout<<"pos h : "<<this->pos.h<<endl;
    cout<<"crop x :"<<this->crop.x<<endl;
    cout<<"crop y :"<<this->crop.y<<endl;
    cout<<"crop w :"<<this->crop.w<<endl;
    cout<<"crop h : "<<this->crop.h<<endl;
*/
    SDL_RenderCopy(renderer,playertex,&crop,&pos);
}
 

player::~player(){};

thank you

c++ sdl2 : how to pass renderer through classes

iam making a game with SDL and c++ iam trying to render simple image to the screen but i can't iam not getting any ERROR but i see only black screen here is my code

i think because the renderer in player is not the same in game they do not have the same memory adress

**my question **  : is how to pass the renderer

i have tried passing it through init() in player directly but it didn't work then how

#ifndef GAME_H
#define GAME_H

#include <SDL2/SDL.h>
 
#include <iostream>
 

using namespace std;


 

class game
{
 
public:

    game();
    ~game();

    void init(char *title,int xpos, int ypos ,int xsize ,int ysize ,bool fullscreen);
    void loop();
    void input();
    void update();
    void render();
    SDL_Renderer* renderer;
    SDL_Window* window;

 

private:
 

    const int target_Fps = 60;
    const float TFEF = 1000/60; // time for each frame
    int delta;
    long int start_time;
    bool isrunning = true;
    int fps_counter = 0;
    unsigned int counter = 1;


 
};

#endif

#include "game.h"
 
#include "player.h"

player* pla;

game::game()
{
    cout << "something"<<endl;
    if (SDL_Init(SDL_INIT_EVERYTHING) == 0)
    {


 
     cout << "another thing" << endl;
 
        init("game",10,15,500,500,false);
    }

loop();
}

game::~game()
{
 
}

void game::init(char *title,int xpos, int ypos ,int xsize ,int ysize ,bool fullscreen)
{
 
    window = SDL_CreateWindow(title, xpos,  ypos , xsize , ysize , fullscreen);
    renderer = SDL_CreateRenderer(window,-1,0);
    //SDL_SetRenderDrawColor(renderer, 0, 0,0, 255);
    SDL_RenderClear(renderer);
    SDL_RenderPresent(renderer);

    cout << "another another something" << endl;
 

    pla = new player(renderer,32,32);
    pla->init();

 

    //SDL_Texture* tex = IMG_LoadTexture(renderer,"res/pss.png");


 
}

void game::loop()
{




 
while(isrunning)
{
    start_time = SDL_GetTicks();

    input();
 
    update();
 
    render();

    delta = SDL_GetTicks() - start_time;
    if (delta < TFEF)
        SDL_Delay(TFEF - delta);

    fps_counter++;

    if (start_time >= 1000 * counter)
    {
        //cout <<"the FPS is : "<< fps_counter << endl;
        counter++;
        fps_counter = 0;
 
    }





 
}
 
}

void game::input()
{
    SDL_Event e;
    SDL_PollEvent(&e);
    if (e.type == SDL_QUIT)
        SDL_Quit();
 
}

void game::update(){}

void game::render()
{
 
    SDL_RenderClear(renderer);
 
    pla->render(renderer);
 
    SDL_RenderPresent(renderer);
}
 

gameobject::~gameobject(){};


#ifndef PLAYER_H
#define PLAYER_H

#include "gameobject.h"

 

class gameobject;

class player : public gameobject
{

 
public:

    void init() override;
    void tick() override;
    void render(SDL_Renderer* renderer) override;
    SDL_Renderer* renderer;
    player(SDL_Renderer* renderer,int pwidth,int pheight);
private:

    ~player() override;
    SDL_Texture* playertex;



 
};



 

#endif // PLAYER_H

#include "player.h"

player::player(SDL_Renderer* renderer,int pwidth, int pheight)
{
 
 this->pos.w = pwidth;
 this->pos.h = pheight;
 this->pos.x = 0;
 this->pos.y = 0;
 

 this->renderer = renderer;



 
}

void player::init()
{
 
    this->playertex= IMG_LoadTexture(renderer,"res/pss.png");
    cout<<"int in player \n"<<endl;

    crop.x = 0;
    crop.y = 0;
    crop.w = 32;
    crop.h = 32;

    cout<<"pos x :"<<this->pos.x<<endl;
    cout<<"pos y : "<<this->pos.y<<endl;
    cout<<"pos w : "<<this->pos.w<<endl;
    cout<<"pos h : "<<this->pos.h<<endl;
    cout<<"crop x :"<<this->crop.x<<endl;
    cout<<"crop y :"<<this->crop.y<<endl;
    cout<<"crop w :"<<this->crop.w<<endl;
    cout<<"crop h : "<<this->crop.h<<endl;

 
}

void player::tick()
{
    
}

void player::render(SDL_Renderer *renderer)
{
 
/*
    cout<<"pos x :"<<this->pos.x<<endl;
    cout<<"pos y : "<<this->pos.y<<endl;
    cout<<"pos w : "<<this->pos.w<<endl;
    cout<<"pos h : "<<this->pos.h<<endl;
    cout<<"crop x :"<<this->crop.x<<endl;
    cout<<"crop y :"<<this->crop.y<<endl;
    cout<<"crop w :"<<this->crop.w<<endl;
    cout<<"crop h : "<<this->crop.h<<endl;
*/
 SDL_RenderCopy(renderer,playertex,&crop,&pos);
}
 

player::~player(){};

thank you

How to pass renderer through classes?

I am making a game with SDL and C++. I am trying to render simple image to the screen but I can't. I am not getting any error but I see only black screen.

I think because the renderer in player is not the same in game they do not have the same memory address.

**my question **: is how to pass the renderer?

I have tried passing it through init() in player directly but it didn't work then.

#ifndef GAME_H
#define GAME_H

#include <SDL2/SDL.h>
#include <iostream>

using namespace std;

class game
{
public:

    game();
    ~game();

    void init(char *title,int xpos, int ypos ,int xsize ,int ysize ,bool fullscreen);
    void loop();
    void input();
    void update();
    void render();
    SDL_Renderer* renderer;
    SDL_Window* window;

private:

    const int target_Fps = 60;
    const float TFEF = 1000/60; // time for each frame
    int delta;
    long int start_time;
    bool isrunning = true;
    int fps_counter = 0;
    unsigned int counter = 1;
};

#endif

#include "game.h"
#include "player.h"

player* pla;

game::game()
{
    cout << "something"<<endl;
    if (SDL_Init(SDL_INIT_EVERYTHING) == 0)
    {
        cout << "another thing" << endl;
        init("game",10,15,500,500,false);
    }

    loop();
}

game::~game()
{
}

void game::init(char *title,int xpos, int ypos ,int xsize ,int ysize ,bool fullscreen)
{
    window = SDL_CreateWindow(title, xpos,  ypos , xsize , ysize , fullscreen);
    renderer = SDL_CreateRenderer(window,-1,0);
    //SDL_SetRenderDrawColor(renderer, 0, 0,0, 255);
    SDL_RenderClear(renderer);
    SDL_RenderPresent(renderer);

    cout << "another another something" << endl;

    pla = new player(renderer,32,32);
    pla->init();

    //SDL_Texture* tex = IMG_LoadTexture(renderer,"res/pss.png");
}

void game::loop()
{
    while(isrunning)
    {
        start_time = SDL_GetTicks();

        input();
        update();
        render();

        delta = SDL_GetTicks() - start_time;
        if (delta < TFEF)
            SDL_Delay(TFEF - delta);

        fps_counter++;

        if (start_time >= 1000 * counter)
        {
            //cout <<"the FPS is : "<< fps_counter << endl;
            counter++;
            fps_counter = 0;
        }
    }
}

void game::input()
{
    SDL_Event e;
    SDL_PollEvent(&e);
    if (e.type == SDL_QUIT)
        SDL_Quit();
}

void game::update(){}

void game::render()
{
    SDL_RenderClear(renderer);
    pla->render(renderer);
    SDL_RenderPresent(renderer);
}

gameobject::~gameobject(){};


#ifndef PLAYER_H
#define PLAYER_H

#include "gameobject.h"

class gameobject;

class player : public gameobject
{
public:

    void init() override;
    void tick() override;
    void render(SDL_Renderer* renderer) override;
    SDL_Renderer* renderer;
    player(SDL_Renderer* renderer,int pwidth,int pheight);
private:

    ~player() override;
    SDL_Texture* playertex;
};

#endif // PLAYER_H

#include "player.h"

player::player(SDL_Renderer* renderer,int pwidth, int pheight)
{
   this->pos.w = pwidth;
   this->pos.h = pheight;
   this->pos.x = 0;
   this->pos.y = 0;

   this->renderer = renderer;
}

void player::init()
{
    this->playertex= IMG_LoadTexture(renderer,"res/pss.png");
    cout<<"int in player \n"<<endl;

    crop.x = 0;
    crop.y = 0;
    crop.w = 32;
    crop.h = 32;

    cout<<"pos x :"<<this->pos.x<<endl;
    cout<<"pos y : "<<this->pos.y<<endl;
    cout<<"pos w : "<<this->pos.w<<endl;
    cout<<"pos h : "<<this->pos.h<<endl;
    cout<<"crop x :"<<this->crop.x<<endl;
    cout<<"crop y :"<<this->crop.y<<endl;
    cout<<"crop w :"<<this->crop.w<<endl;
    cout<<"crop h : "<<this->crop.h<<endl;
}

void player::tick()
{
}

void player::render(SDL_Renderer *renderer)
{
/*
    cout<<"pos x :"<<this->pos.x<<endl;
    cout<<"pos y : "<<this->pos.y<<endl;
    cout<<"pos w : "<<this->pos.w<<endl;
    cout<<"pos h : "<<this->pos.h<<endl;
    cout<<"crop x :"<<this->crop.x<<endl;
    cout<<"crop y :"<<this->crop.y<<endl;
    cout<<"crop w :"<<this->crop.w<<endl;
    cout<<"crop h : "<<this->crop.h<<endl;
*/
    SDL_RenderCopy(renderer,playertex,&crop,&pos);
}

player::~player(){};

Source Link

c++ sdl2 : how to pass renderer through classes

iam making a game with SDL and c++ iam trying to render simple image to the screen but i can't iam not getting any ERROR but i see only black screen here is my code

i think because the renderer in player is not the same in game they do not have the same memory adress

**my question ** : is how to pass the renderer

i have tried passing it through init() in player directly but it didn't work then how

game.h

#ifndef GAME_H
#define GAME_H

#include <SDL2/SDL.h>

#include <iostream>


using namespace std;




class game
{

public:

    game();
    ~game();

    void init(char *title,int xpos, int ypos ,int xsize ,int ysize ,bool fullscreen);
    void loop();
    void input();
    void update();
    void render();
    SDL_Renderer* renderer;
    SDL_Window* window;



private:


    const int target_Fps = 60;
    const float TFEF = 1000/60; // time for each frame
    int delta;
    long int start_time;
    bool isrunning = true;
    int fps_counter = 0;
    unsigned int counter = 1;



};

#endif

game.cpp


#include "game.h"

#include "player.h"

player* pla;

game::game()
{
    cout << "something"<<endl;
    if (SDL_Init(SDL_INIT_EVERYTHING) == 0)
    {



     cout << "another thing" << endl;

        init("game",10,15,500,500,false);
    }

loop();
}

game::~game()
{

}

void game::init(char *title,int xpos, int ypos ,int xsize ,int ysize ,bool fullscreen)
{

    window = SDL_CreateWindow(title, xpos,  ypos , xsize , ysize , fullscreen);
    renderer = SDL_CreateRenderer(window,-1,0);
    //SDL_SetRenderDrawColor(renderer, 0, 0,0, 255);
    SDL_RenderClear(renderer);
    SDL_RenderPresent(renderer);

    cout << "another another something" << endl;


    pla = new player(renderer,32,32);
    pla->init();



    //SDL_Texture* tex = IMG_LoadTexture(renderer,"res/pss.png");



}

void game::loop()
{





while(isrunning)
{
    start_time = SDL_GetTicks();

    input();

    update();

    render();

    delta = SDL_GetTicks() - start_time;
    if (delta < TFEF)
        SDL_Delay(TFEF - delta);

    fps_counter++;

    if (start_time >= 1000 * counter)
    {
        //cout <<"the FPS is : "<< fps_counter << endl;
        counter++;
        fps_counter = 0;

    }






}

}

void game::input()
{
    SDL_Event e;
    SDL_PollEvent(&e);
    if (e.type == SDL_QUIT)
        SDL_Quit();

}

void game::update(){}

void game::render()
{

    SDL_RenderClear(renderer);

    pla->render(renderer);

    SDL_RenderPresent(renderer);
}


gameobject::~gameobject(){};

player.h


#ifndef PLAYER_H
#define PLAYER_H

#include "gameobject.h"



class gameobject;

class player : public gameobject
{


public:

    void init() override;
    void tick() override;
    void render(SDL_Renderer* renderer) override;
    SDL_Renderer* renderer;
    player(SDL_Renderer* renderer,int pwidth,int pheight);
private:

    ~player() override;
    SDL_Texture* playertex;




};





#endif // PLAYER_H

player.cpp


#include "player.h"

player::player(SDL_Renderer* renderer,int pwidth, int pheight)
{

 this->pos.w = pwidth;
 this->pos.h = pheight;
 this->pos.x = 0;
 this->pos.y = 0;


 this->renderer = renderer;




}

void player::init()
{

    this->playertex= IMG_LoadTexture(renderer,"res/pss.png");
    cout<<"int in player \n"<<endl;

    crop.x = 0;
    crop.y = 0;
    crop.w = 32;
    crop.h = 32;

    cout<<"pos x :"<<this->pos.x<<endl;
    cout<<"pos y : "<<this->pos.y<<endl;
    cout<<"pos w : "<<this->pos.w<<endl;
    cout<<"pos h : "<<this->pos.h<<endl;
    cout<<"crop x :"<<this->crop.x<<endl;
    cout<<"crop y :"<<this->crop.y<<endl;
    cout<<"crop w :"<<this->crop.w<<endl;
    cout<<"crop h : "<<this->crop.h<<endl;


}

void player::tick()
{
    
}

void player::render(SDL_Renderer *renderer)
{

/*
    cout<<"pos x :"<<this->pos.x<<endl;
    cout<<"pos y : "<<this->pos.y<<endl;
    cout<<"pos w : "<<this->pos.w<<endl;
    cout<<"pos h : "<<this->pos.h<<endl;
    cout<<"crop x :"<<this->crop.x<<endl;
    cout<<"crop y :"<<this->crop.y<<endl;
    cout<<"crop w :"<<this->crop.w<<endl;
    cout<<"crop h : "<<this->crop.h<<endl;
*/
 SDL_RenderCopy(renderer,playertex,&crop,&pos);
}


player::~player(){};

thank you