• Advertisement
Sign in to follow this  

SDL2 Displays wrong image?

This topic is 823 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi guys, first time posting here,

So I am programming a little platformer to learn new things and a problem I never had before got in my way.

 

The thing is that I'm calling a constructor of a class with a string and a renderer, so it can create a texture and then pass it to the each tile, and it doesn't work, it uses the png of the character instead of the tileset png. For some reason if I create the texture in the main loop and then pass it to each tile when rendering them it works fine.

I don't know if I explained myself correctly, my english is not perfect so sorry if its not clear ;_;

 

Code:

I send the renderer in the constructor:

level p_("maps/test.txt",0(this is the type),render_);

In the constructor I check the type(in this case 0) and then create the surface:

SDL_Texture* text_;
if(tipo==0){
        text_ = IMG_LoadTexture(render_,"graphics/t.png");
 
Then I send that texture to the constructor:
tile(t_,x,y,cx,text_);  Obviously then I destroy the surface created
 
In the tile constructor I asign it to a class texture:
t_=text_;    t_ being declared in the tile class.
 
The weird thing is that I do the exact same thing for the character and it works just fine, but here, it uses the character png instead of the tileset png. Why could this happen?
 
Edit for extending stuff:

[SPOILER]

Level constructor:

nivel::nivel(char* archivo, int tipo, SDL_Renderer* r_){
    //dependiendo del tipo cada numero del tile será una cosa u otra
    int c_, t_, maxx, x=0, y=0, cx;
    SDL_Texture* text_;
    if(tipo==0){
        text_ = IMG_LoadTexture(r_,"graficos/t.png");
        std::ifstream f_(archivo);
        f_ >> c_;
        f_ >> maxx;
        for(int i=0; i<c_; i++){
            f_ >> t_;
            cx=32*t_;
            mapa_.push_back(tile(t_,x,y,cx,text_));
            x+=32;
            if(x>=maxx){
                x=0;
                y+=32;
            }
        }
        f_.close();
    }
    SDL_DestroyTexture(text_);
}

Tile constructor and render function:

tile::tile(int tipo, int posx, int posy, int clipx, SDL_Texture* text_){
    t_=text_;
 
    //check tipos y dar una textura segun el tipo y el recorte
    clip_.w=TILE_S;
    clip_.h=TILE_S;
    clip_.x=clipx;
    clip_.y=0;
 
    absolutex=posx;
    absolutey=posy;
 
    pos_.x=posx;
    pos_.y=posy;
    pos_.w=TILE_S;
    pos_.h=TILE_S;
}
 
 

void tile::render(SDL_Renderer* r_, int camx, int camy){
    pos_.x=absolutex-camx;
    pos_.y=absolutey-camy;
    SDL_RenderCopy(r_, t_, &clip_, &pos_);
}

Image of what happen, the orange tiles should be air and there should be other tiles bellow them, as you see, the air (tile id 0) is the character image, so its taking its png:

ef37744d6883261e7bf6a27ad78a569f.png

[/SPOILER]

 

I declare the level in the main loop of the game

 

Edit 2: I found something

If I create the texture in the main loop and just have it there, and then create the texture in the map again, it works fine, I don't know why, but it seems that having the image loaded into another texture in the main loop fixes it, its annoying thogh, anyone knows why could this be?

 
Again, sorry if I couldn't make myself clear, and thanks in advance.
Edited by Soka

Share this post


Link to post
Share on other sites
Advertisement

Yes, sorry I didn't want to overwhelm with code.

 

[SPOILER]

Level constructor:

nivel::nivel(char* archivo, int tipo, SDL_Renderer* r_){
    //dependiendo del tipo cada numero del tile será una cosa u otra
    int c_, t_, maxx, x=0, y=0, cx;
    SDL_Texture* text_;
    if(tipo==0){
        text_ = IMG_LoadTexture(r_,"graficos/t.png");
        std::ifstream f_(archivo);
        f_ >> c_;
        f_ >> maxx;
        for(int i=0; i<c_; i++){
            f_ >> t_;
            cx=32*t_;
            mapa_.push_back(tile(t_,x,y,cx,text_));
            x+=32;
            if(x>=maxx){
                x=0;
                y+=32;
            }
        }
        f_.close();
    }
    SDL_DestroyTexture(text_);
}

 

Tile constructor and render function:

tile::tile(int tipo, int posx, int posy, int clipx, SDL_Texture* text_){
    t_=text_;
 
    //check tipos y dar una textura segun el tipo y el recorte
    clip_.w=TILE_S;
    clip_.h=TILE_S;
    clip_.x=clipx;
    clip_.y=0;
 
    absolutex=posx;
    absolutey=posy;
 
    pos_.x=posx;
    pos_.y=posy;
    pos_.w=TILE_S;
    pos_.h=TILE_S;
}
 
 

void tile::render(SDL_Renderer* r_, int camx, int camy){
    pos_.x=absolutex-camx;
    pos_.y=absolutey-camy;
    SDL_RenderCopy(r_, t_, &clip_, &pos_);
}

[/SPOILER]

 

I declare the level in the main loop of the game

Share this post


Link to post
Share on other sites
You destroy the texture in the nivel constructor but the texture is still used by the tile objects that you created.

Share this post


Link to post
Share on other sites

You destroy the texture in the nivel constructor but the texture is still used by the tile objects that you created.

I've tried making the texture a class variable and not destroying it in the constructor, but it still doesn't work

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement