【问题标题】:SDL on blit, loses transparency and turns blackSDL 在 blit 上,失去透明度并变黑
【发布时间】:2012-02-24 00:17:53
【问题描述】:

几个星期以来,我和我的朋友们正在研究一个项目。准确的说是游戏。我们遇到了一个破坏游戏玩法的大问题。播放器应该透明的部分是黑色的。

MergeSurfaces 函数是 blit。 rects 本身被写入 SDL_Rect,并执行 blit

void MergeSurfaces(SDL_Surface *From, SDL_Surface *To, int FromX, int FromY, int FromWidth, int FromLenght, int ToX, int ToY){




            SDL_Rect srcRect;    
            srcRect.x = FromX;    
            srcRect.y = FromY;
            srcRect.w = FromWidth;   
            srcRect.h = FromLenght;

            SDL_Rect dstRect;    
            dstRect.x = ToX;    
            dstRect.y = ToY;    

            SDL_BlitSurface(From, &srcRect, To, &dstRect);
        }

这是玩家形成功能。

//------------------------------------------------------------------------------------------
//----MAIN LOAD FUNCTION
//------------------------------------------------------------------------------------------    

    void LoadPlayerGraphics(SDL_Surface* BodyID[], int PlayerHeight, int PlayerWidth, long EquipmentID[], int MovementAmountX, int MovementAmountY){
        SDL_Surface* Image;
        SDL_Surface* EquipmentColorization;
        std::string FileName;
        int ID;


        Clean(BodyID,MovementAmountX*MovementAmountY,PlayerWidth,PlayerHeight);


        for(int i = -1; i < 8; i++){
            ID = 0;
            //here we put a small exception to firstly load the player. And only then dress Him
            if(i == -1){
                FileName = "resource/images/Player/WhiteMaleBody.png";
                goto playerbody;
            }
            if(EquipmentID[i] != 0){
                GetFileNameByID(EquipmentID[i],FileName);
            playerbody:
                Image = IMG_Load(FileName.c_str());
                if(Image == NULL){
                    exit(1);
                }

                //Needed for equipment coloring. At this point we will put RGB masks in order to color the armor by it's type
                EquipmentColorization = SDL_CreateRGBSurface(SDL_HWSURFACE | SDL_SRCALPHA, MovementAmountX*PlayerWidth, MovementAmountY*PlayerHeight, 32, 0, 0, 0, 0);


                GraphicsFunctions.MergeSurfaces(Image,EquipmentColorization,0,0,MovementAmountX*PlayerWidth,MovementAmountY*PlayerHeight,0,0);

                for(int i = 0; i < MovementAmountY; i++){
                    for(int j = 0; j < MovementAmountX; j++){
                        ID++;   
                        //We put the graphics on and on on top. So we dress the frames. BodyID[ID] are frames by motion ID. We just fill this up.
                        GraphicsFunctions.MergeSurfaces(    EquipmentColorization,BodyID[ID],
                                                            (j * PlayerWidth),
                                                            (i * PlayerHeight),
                                                            PlayerWidth,PlayerHeight,
                                                            0,0);

                        if(BodyID[i] == NULL){
                            exit(2);
                        }
                    }
                }
            }

        }
    }

如果您想知道它的作用,请使用 Clean 功能。我还没有释放这里的表面。由于我在程序结束时执行此操作,并且到目前为止已加载一次。所以基本上它只是为了创建这些表面以填充它们。

void Clean(SDL_Surface* TheSurface[], int MovementAmount, int PlayerWidth, int PlayerHeight){
        GraphicsFunctions.Setrgba();
        for(int i = 0; i <= MovementAmount; i++){
            TheSurface[i] = SDL_CreateRGBSurface(SDL_HWSURFACE | SDL_SRCALPHA, PlayerWidth, PlayerHeight, 32, 0, 0, 0, 0);
        }
    }

接下来是框架部分,或者使角色看起来在移动。它是在别处调用的,所以我可以轻松控制速度。

void Variate(SDL_Surface* Graphical_Output){
        GraphicsFunctions.MergeSurfaces(BodyID[MovementVariationID[MovementID][Variation]], Graphical_Output, 0, 0, PlayerWidth, PlayerHeight, 0, 0);
        Variation++;
        if(Variation == MovementVariationIn[MovementID]){
            Variation = 0;
        }
    }

这里是主线程控件。主系统线程,Blits 和 Flips 显示您在此处看到的内容。

//------------------------------------------------------------------------------------------
//----MAIN Thread Function (As Thread Repeat to infinity LOL)
//------------------------------------------------------------------------------------------
    int Player_Main(void *unused){

        GraphicsFunctions.Setrgba();
        PlayerGraphics = SDL_CreateRGBSurface(SDL_HWSURFACE | SDL_SRCALPHA, 1024, 768, 32, GraphicsFunctions.r, GraphicsFunctions.g, GraphicsFunctions.b, GraphicsFunctions.a);
        while(!EndProgram){

            PlayerMovementGraphics::Variate(PlayerGraphics);

            SDL_Delay(200);
        }
        return 0;
    }

当然,这里需要进行改进。但自从我几周前开始从事 SDL 工作以来。我还有很多东西要学。这就是现在图形的全部内容。因此,也许您可​​以检测到为什么播放器本身在应该透明的地方有黑色。

【问题讨论】:

    标签: c++ visual-c++ sdl


    【解决方案1】:

    你写:

    SDL_CreateRGBSurface( SDL_HWSURFACE | SDL_SRCALPHA,
                          PlayerWidth, PlayerHeight, 
                          32, 0, 0, 0, 0 );
    

    来自documentation:

    对 RGB 蒙版使用零设置基于深度的默认值。但是,对 Amask 使用零会导致 Amask 为 0。

    如果你想要一个 alpha 通道,你需要明确指定你的掩码:

    SDL_CreateRGBSurface( SDL_HWSURFACE | SDL_SRCALPHA,
                          PlayerWidth, PlayerHeight, 32,
                          0xFF000000, 0x00FF0000, 0x0000FF00, 0x000000FF );
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-11-12
      • 2018-09-23
      • 2018-03-30
      • 2011-04-03
      相关资源
      最近更新 更多