I am making a game with SDL and C++. I am trying to render simple image to the screen but I can't. I don't know why, I am not getting any ERROR but I see only black screen.
Here is my code:
game.h
#ifndef GAME_H
#define GAME_H
#include
#include
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"< 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"<
crop.x = 0;
crop.y = 0;
crop.w = 32;
crop.h = 32;
cout<<"pos x :"<pos.x< cout<<"pos y : "<pos.y<
cout<<"pos w : "<pos.w< cout<<"pos h : "<pos.h< cout<<"crop x :"<crop.x< cout<<"crop y :"<crop.y< cout<<"crop w :"<crop.w< cout<<"crop h : "<crop.h< }
void player::tick()
{
}
void player::render(SDL_Renderer *renderer)
{
/*
cout<<"pos x :"<pos.x< cout<<"pos y : "<pos.y< cout<<"pos w : "<pos.w< cout<<"pos h : "<pos.h< cout<<"crop x :"<crop.x<
cout<<"crop y :"<crop.y< cout<<"crop w :"<crop.w< cout<<"crop h : "<crop.h< */
SDL_RenderCopy(renderer,playertex,&crop,&pos);
}
player::~player(){};
here is my files
No comments:
Post a Comment