0
\$\begingroup\$

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 <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(){};

here is my files

enter image description here

\$\endgroup\$
  • \$\begingroup\$ You're not doing any error checking whatsoever, which means that you don't know if the player texture, and even the SDL renderer, is successfully created. Check the state of the renderer, the texture, that the pos and crop variables are correctly set etc. \$\endgroup\$ – Daniel_1985 Nov 10 '19 at 12:10
  • \$\begingroup\$ can you tell me how also the pos and crop are fine i printed them \$\endgroup\$ – younlok Nov 10 '19 at 12:11
  • \$\begingroup\$ Well, you can check if an object, your texture in this case, is nullptr. If the texture is nullptr after IMG_LoadTexture() is executed, it means something failed, for example that the image file wasn't found. If that's the case, if the texture is nullptr, you can execute IMG_GetError() after the creation to find out what caused the error. \$\endgroup\$ – Daniel_1985 Nov 10 '19 at 15:23
  • \$\begingroup\$ but iam not getting any error and can u give me the function that test the texture \$\endgroup\$ – younlok Nov 10 '19 at 15:24
  • \$\begingroup\$ You will have to debug your code by yourself. The comment section is not for extended discussion and answers are supposed to solve the problem. Since I don't know what the problem is with your code, I can'/won't post an answer (yet). Set out some breakpoints in your code and check the values of all the variables and objects. \$\endgroup\$ – Daniel_1985 Nov 10 '19 at 15:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.