在SDL中水平减少静态图像进度条

时间:2018-06-28 13:35:17

标签: c++ user-interface progress-bar sdl

在我的项目中,显示的计时器进度条PNG图像正在减少。该图像及其背景可以在以下链接中找到: https://s3.eksiup.com/df7dd38f781.png 我的目标是使条从绿色的上侧到红色的下侧减小。我的代码如下:

    void EntityTimer::OnRender(SDL_Renderer *ren)
    {
        SDL_Rect currRect,dstRect;
        double rem=0.0;

        memcpy(&currRect,&origRect,sizeof (SDL_Rect));
        memcpy(&dstRect,&origRect,sizeof (SDL_Rect));
        if (timerObjPtr->remainingDuration>timerObjPtr->durationInMilisec)
            timerObjPtr->durationInMilisec=timerObjPtr->remainingDuration;
        rem=((double)timerObjPtr->remainingDuration/timerObjPtr->durationInMilisec);

        currRect.h=(origRect.h)*rem;
        currRect.h = currRect.h;
        dstRect.x=0;
        dstRect.y=0;
        dstRect.h=currRect.h;
        SDL_RenderCopy(ren,timerTexture,&dstRect,&currRect);
    }

由于此图像是静态的,因此我尝试通过使用“ currRect.h =(origRect.h)* rem;”来处理此纹理的height参数来实现此目的。线。但这会使进度条从红色的一侧减少到绿色的一侧(与我想要的相反)。

我试图纠正它,但是通过在进度条区域上镜像PNG图像并再次从下到上减小它来使情况更糟。

我们将不胜感激,以使该条形从顶部(绿色)减小到底部(红色)。

1 个答案:

答案 0 :(得分:0)

这很好,因为您是从同一点(x = 0,y = 0)绘制但使用较小的尺寸。您需要更新目标位置:

dstRect.y = origRect.h - origRect.h * rem;

编辑:这是另一种方法

dstRect.y = origRect.h * rem

(假设rem从0到1)

以下是您想要的示例,您可以使用此图片测试https://i.imgur.com/faKKShU.png

#include <SDL2/SDL.h>                                                        
#include <SDL2/SDL_image.h>                                                  
#include <iostream>                                                          
#include <thread>                                                            

#define HEIGHT 600                                                           
#define WIDTH  800                                                           

using namespace std;                                                         

int main() {                                                                 
    SDL_Init(SDL_INIT_VIDEO);                                                

    SDL_Window *window = SDL_CreateWindow("Red", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, WIDTH, HEIGHT, SDL_WINDOW_SHOWN);

    SDL_Renderer *renderer = SDL_CreateRenderer(window, 0, SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC);

    bool quit = false;                                                       
    SDL_Event event;                                                         

    SDL_Texture *red_part = IMG_LoadTexture(renderer, "red_image.png");      

    float multiplier = 0;                                                    

    while (!quit) {                                                          
        while (SDL_PollEvent(&event)) {                                      
            if (event.type == SDL_QUIT) {                                    
                quit = true;                                                 
            }                                                                
        }                                                                    

        int y_pos = HEIGHT * multiplier;                                     
        SDL_RenderClear(renderer);                                           
        SDL_Rect copy_rect{0, y_pos, 800, 600};                              
        SDL_RenderCopy(renderer, red_part, nullptr, &copy_rect);             

        SDL_RenderPresent(renderer);                                         

        multiplier += 0.01;                                                  
        if (multiplier >= 1.)                                                
            multiplier = 1.;                                                 
        std::this_thread::sleep_for(std::chrono::milliseconds{33});          
    }                                                                        

    SDL_DestroyWindow(window);                                               
    SDL_DestroyRenderer(renderer);                                           
    SDL_Quit();                                                              

    return 0;                                                                
}