【问题标题】:scrolling background image, can't hide seams, c++ raylib滚动背景图像,不能隐藏接缝,c ++ raylib
【发布时间】:2021-07-27 14:38:30
【问题描述】:

我正在尝试让滚动背景在背景图像小于程序窗口的情况下工作。为此,我创建了一个 for 循环来移动两组相同的复制图像。

奇怪的是,两组图像一直分开而不是保持在一起,无缝。

这是使用 C++ 和 raylib,但搜索后我不确定这是 raylib 特定的问题。

以下是它们如何分开而不是保持无缝的示例:

代码如下:

        #include "raylib.h"
    
    int main()
    {
        //preamble
        const int windowW{600};
        const int windowH{380};
        float dt{0};     // for use with delta time between frames
    
        SetTargetFPS(60);
        InitWindow(windowW, windowH, "walk");
    
        //backgrounds
        Texture2D bg = LoadTexture("textures/far-buildings.png"); // 256 X 192
        const float bgScale{0.5};
        const int bgQty{2};
        float bgX{0.0};
        float bgX2{(bg.width * bgScale * bgQty)};
    
        while (!WindowShouldClose())
        {
            //start drawing
            BeginDrawing();
            ClearBackground(WHITE);
    
            dt = GetFrameTime();
    
            bgX -= 250 * dt;
            bgX2 -= 250 * dt;
    
            if (bgX < -(bg.width * bgScale) * bgQty)
            {
                bgX = (bg.width * bgScale) * bgQty;
            }
            if (bgX2 < -(bg.width * bgScale) * bgQty)
            {
                bgX2 = (bg.width * bgScale) * bgQty;
            }
    
            for (int i = 0; i < bgQty; i++)
            {
                Vector2 bgPos{bgX + ((bg.width * bgScale) * i), 0.0};
                Vector2 bgPos2{bgX2 + ((bg.width * bgScale) * i), 0.0};
                DrawTextureEx(bg, bgPos, 0.0, bgScale, WHITE);
                DrawTextureEx(bg, bgPos2, 0.0, bgScale, WHITE);
            }
    
            DrawText(FormatText("dt: %f", dt), 3, windowH - 10, 10, GOLD);
            EndDrawing();
        }
        UnloadTexture(bg);
        CloseWindow();
    }

这是背景图片:

【问题讨论】:

    标签: c++ loops 2d parallax raylib


    【解决方案1】:

    您的代码过于复杂。要获得重复,只需制作一个大于源图像的源矩形(UV 坐标),OpenGL 将重复它。将源矩形设置为与屏幕相同的大小,并在滚动时偏移源矩形原点。

    这是一个简单的例子。 https://github.com/JeffM2501/TestFrame/discussions/12

    【讨论】:

      猜你喜欢
      • 2020-01-11
      • 1970-01-01
      • 1970-01-01
      • 2013-12-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-06-22
      • 1970-01-01
      相关资源
      最近更新 更多