C语言 简单拉伸Raylib全屏选项?,

xvw2m8pv  于 2023-02-21  发布在  其他
关注(0)|答案(1)|浏览(329)

下面列出了一个简单Raylib程序的代码(基于Raylib示例shapes_logo_raylib)。运行该程序会显示Raylib徽标的版本:填充大约三分之一(800x450)窗口的黑色方形轮廓。
通过GetCurrentMonitor()SetWindowSize()GetMonitorWidth()GetMonitorHeight()SetConfigFlags(FLAG_WINDOW_RESIZABLE)ToggleFullscreen()等调用,制作一个全屏版本的程序并不难,但是,尽管黑色方块的 * 大小 * 与以前相似,它占据了(左上角)大部分的小部分(全屏)窗口。是否有一个选项可以在全屏窗口上显示原始窗口图像的更大“拉伸”版本?

#include "raylib.h"                                                              
                                                                                 
int main(void)                                                                   
{
  int screenWidth = 800, screenHeight = 450;
  InitWindow(screenWidth, screenHeight, "raylib [shapes] example - raylib logo using shapes");

  while (!WindowShouldClose())
  {
    BeginDrawing()
    ClearBackground(RAYWHITE);
    DrawRectangle(screenWidth/2 - 128, screenHeight/2 - 128, 256, 256, BLACK);
    DrawRectangle(screenWidth/2 - 112, screenHeight/2 - 112, 224, 224, RAYWHITE);
    DrawText("raylib", screenWidth/2 - 44, screenHeight/2 + 48, 50, BLACK);
    EndDrawing();
  }

  CloseWindow();
  return 0;                                                                    
}
wmomyfyw

wmomyfyw1#

我对你的代码做了如下的添加,使它可以在任何窗口大小上工作。它可以把你想画的东西画到RenderTexture2D上,然后把所说的纹理画到屏幕上。我只在可调整大小的窗口上测试过它,但是它应该可以在任何窗口模式下工作,包括独占全屏。
简而言之:
1.使用LoadRenderTexture(int, int)请求渲染纹理
1.使用BeginTextureMode(RenderTexture2D)EndTextureMode()在纹理上绘制
1.使用DrawTexturePro(Texture2D, Rectangle, Rectangle, Vector2, float, Color)绘制纹理,第一个矩形是纹理的大小,第二个矩形是屏幕的大小。如果它看起来像镜像,反转输入纹理的高度。
1.完成后卸载渲染纹理。
我对所有添加/更改添加了注解,以突出需要更改的内容。

#include "raylib.h"

int screenWidth = 800;
int screenHeight = 450;

int main(void)
{
    InitWindow(800, 450, "raylib [shapes] example - raylib logo using shapes");

    // This should use the flag FLAG_FULLSCREEN_MODE which results in a possible ToggleFullscreen() call later on
    SetWindowState(FLAG_WINDOW_RESIZABLE);

    // Request a texture to render to. The size is the screen size of the raylib example.
    RenderTexture2D renderTexture = LoadRenderTexture(screenWidth, screenHeight);

    while (!WindowShouldClose())
    {
        // Instead of using BeginDrawing() we render to the render texture. Everything else stays unchanged
        BeginTextureMode(renderTexture);
        ClearBackground(RAYWHITE);
        DrawRectangle(screenWidth / 2 - 128, screenHeight / 2 - 128, 256, 256, BLACK);
        DrawRectangle(screenWidth / 2 - 112, screenHeight / 2 - 112, 224, 224, RAYWHITE);
        DrawText("raylib", screenWidth / 2 - 44, screenHeight / 2 + 48, 50, BLACK);
        // We need to end the texture mode separately
        EndTextureMode();

        // Let's draw the texture. The source rect is the size of the texture, the destination rect is of the same size as the screen. For some reason, the texture was flipped vertically, so I had to invert the source rects "height" to flip the UV.
        BeginDrawing();
        DrawTexturePro(
            renderTexture.texture,
            Rectangle{ 0, 0, static_cast<float>(renderTexture.texture.width), static_cast<float>(-renderTexture.texture.height) },
            Rectangle{ 0, 0, static_cast<float>(GetScreenWidth()), static_cast<float>(GetScreenHeight()) },
            Vector2{ 0, 0 },
            0,
            WHITE);
        EndDrawing();
    }

    // Unload the texture handle again to make a clean exit.
    UnloadRenderTexture(renderTexture);

    CloseWindow();

    return 0;
}

我希望这能回答你的问题。

相关问题