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