Search code examples
c++sdl-2

SDL_Texture not rendering properly


I'm making a game board with a 3D array which stores the location of items (a vector of Items for each descrete x,y position on the game board). Each item type (Subclass of Item) has an associated sprite, which is stored as a Texture. The code for storing the textures is given below

void render(){
        int x = 0;
        //for each row in the array
        for(vector<vector<Item*>> row : items){
            int y = 0;
            // for each column in the row
            for(vector<Item*> col : row){
                //Graphical positons
                int pos_x = x * CELL_WIDTH;
                int pos_y = y * CELL_HEIGHT;

                //Initalise the rectangle
                SDL_Rect rect;
                rect.x = pos_x;
                rect.y = pos_y;
                rect.w = CELL_WIDTH;
                rect.h = CELL_HEIGHT;
                // if the the list of items is empty
                

                if(col.size() > 0){
                    for(Item* item: col){
                        SDL_Texture *texture = NULL; 
                        texture = textureMap[item->typestring];
                        SDL_SetRenderDrawColor(renderer, 0, 0 ,0, 0);
                        SDL_RenderCopy(renderer, texture, nullptr, &rect);
                        SDL_RenderFillRect(renderer, &rect);
                    
                    }

                }
                else{
                    SDL_SetRenderDrawColor(renderer, 0, 0, 0, 0);
                    SDL_RenderFillRect(renderer, &rect);

                }
                SDL_RenderFillRect(renderer, &rect);
                y++;
                
            }
            x++;
            
        }
        SDL_RenderPresent(renderer);
        
    }

but none of the sprites render and there is a blank screen. worth mentioning that if the code which attempts to fill rect with the sprite is changed to just show a colored square, it works :

        void render(){
        int x = 0;
        //for each row in the array
        for(vector<vector<Item*>> row : items){
            int y = 0;
            // for each column in the row
            for(vector<Item*> col : row){
                //Graphical positons
                int pos_x = x * CELL_WIDTH;
                int pos_y = y * CELL_HEIGHT;

                //Initalise the rectangle
                SDL_Rect rect;
                rect.x = pos_x;
                rect.y = pos_y;
                rect.w = CELL_WIDTH;
                rect.h = CELL_HEIGHT;
                // if the the list of items is empty
                

                if(col.size() > 0){
                    for(Item* item: col){
                        SDL_SetRenderDrawColor(renderer, 100, 100, 100, 0);
                        SDL_RenderFillRect(renderer, &rect);
                
                    }

                }
                else{
                    SDL_SetRenderDrawColor(renderer, 0, 0, 0, 0);
                    SDL_RenderFillRect(renderer, &rect);

                }
                SDL_RenderFillRect(renderer, &rect);
                y++;
                
            }
            x++;
            
        }
        SDL_RenderPresent(renderer);
        
    }

results in this game screen, with grey squares in the positions the items should be in

enter image description here

here is the code for creating textures from .png filepath string stored in each class, and storing it in a Map<String,SDL_Texture>.

            for(vector<vector<Item*>> row : items){
            for(vector<Item*> col : row){
                for(Item* item: col){
                    if(textureMap.find(item->typestring) == textureMap.end()){

                        SDL_Texture* texture = NULL;
                        SDL_Surface* temp = IMG_Load(item->imgpath);
                        
                        texture = SDL_CreateTextureFromSurface(renderer, temp);
                        SDL_FreeSurface(temp);
                        textureMap.insert({item->typestring, texture});
                        
                        
                        
                    }
                    
                }
            }

        }

I'm new to C++ and SDL2, can anyone see any obvious issues with the way the SDL_Textures are being rendered?


Solution

  • It's because you're using SDL_RenderFillRect after SDL_RenderCopy.

    SDL_RenderCopy does what you want to get a texture on screen. But SDL_RenderFillRect will fill an area with a colour. If you do the copy before the fill in the same spot, the colour will be overwritting the texture.

    Just remove SDL_RenderFillRect and the texture should appear properly.