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");
tile(t_,x,y,cx,text_); Obviously then I destroy the surface created
t_=text_; t_ being declared in the tile class.
[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:
[/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?