繁体   English   中英

SDL_Texture 未正确渲染

[英]SDL_Texture not rendering properly

我正在制作一个带有 3D 数组的游戏板,用于存储项目的位置(游戏板上每个离散x,y 位置的项目向量)。 每个项目类型(项目的子类)都有一个关联的精灵,它存储为纹理。 下面给出了存储纹理的代码

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

但是没有一个精灵渲染并且有一个空白屏幕。 值得一提的是,如果尝试用精灵填充 rect 的代码更改为仅显示彩色方块,则它可以工作:

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

结果在这个游戏画面中,物品应该在的位置有灰色方块

在此处输入图片说明

这是从每个类中存储的 .png 文件路径字符串创建纹理并将其存储在 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});
                        
                        
                        
                    }
                    
                }
            }

        }

我是 C++ 和 SDL2 的新手,谁能看到 SDL_Textures 的渲染方式有任何明显的问题?

这是因为您在SDL_RenderFillRect之后使用SDL_RenderCopy

SDL_RenderCopy执行您想要在屏幕上获得纹理的操作。 但是SDL_RenderFillRect会用颜色填充一个区域。 如果您在填充同一位置之前进行复制,则颜色将覆盖纹理。

只需删除SDL_RenderFillRect ,纹理就会正确显示。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM