0

The code for a simple Raylib program is listed below (based on the Raylib example shapes_logo_raylib). Running the program shows a version of the Raylib logo: a black square outline which fills about a third of the (800x450) window.

It's not hard to make a fullscreen version of the program, with calls such as GetCurrentMonitor(), SetWindowSize(), GetMonitorWidth(), GetMonitorHeight() SetConfigFlags(FLAG_WINDOW_RESIZABLE), or ToggleFullscreen(). But then, while the black square remains a similar size as before, it occupies (top left) a much smaller proportion of the larger (fullscreen) window. Is there an option to display a larger "stretched" version of the original windowed image on the fullscreen window?

#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;                                                                    
}                  
user2023370
  • 10,488
  • 6
  • 50
  • 83
  • Have you updated the `screenWidth` and `screenHeight` values? Because as I see it, everything about the logo is pretty hard coded and only looks good when rendering on a 800x450 window. – Max Play Feb 20 '23 at 08:59
  • Yes, I did. While the black square remains a similar size as before, it occupies (top left) a much smaller proportion of the larger (fullscreen) window. I understand I can rewrite everything proportionally for a larger screen size; or also generically, for an arbitrary screen size. I'm asking though about whether there is a way to *stretch* a window (here 800x450), to cover the full screen. – user2023370 Feb 20 '23 at 09:46
  • You could render it onto a 800x450 `RenderTexture2D` and then draw it fullscreen, but it will probably look not as crisp. – Max Play Feb 20 '23 at 10:11
  • Yes, it will not look as crisp, but I would be happy to accept such an answer. How many lines do you think it would add to the ~20 lines above? – user2023370 Feb 20 '23 at 14:47
  • I can try to make it work by myself and then post an answer. – Max Play Feb 20 '23 at 17:15

1 Answers1

1

I made the following additions to your code to make it work on any window size. It draws the stuff you want to draw onto a RenderTexture2D and then draws said texture onto the screen. I've only tested it with resizable windows, but it should work in any window mode, including exclusive fullscreen.

In short:

  1. Request render texture using LoadRenderTexture(int, int)
  2. Use BeginTextureMode(RenderTexture2D) and EndTextureMode() to draw onto the texture
  3. Draw the texture using DrawTexturePro(Texture2D, Rectangle, Rectangle, Vector2, float, Color), the first rectangle is the size of the texture, the second the size of the screen. If it looks mirrored, invert the height of the input texture.
  4. Unload the render texture when done.

I added comments to all my additions/changes to highlight what needs to be changed.

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

I hope this answers your question.

Max Play
  • 3,717
  • 1
  • 22
  • 39
  • Wonderful !! It certainly does answer the question. You've made the internet a better place today :) Thankyou. (I also updated my question with the two vars you spotted I'd mistakenly culled.) – user2023370 Feb 20 '23 at 20:49
  • 1
    Glad I could help. I removed the comment from the `screenHeight` and `screemWidth` definitions, so it matches your code. – Max Play Feb 20 '23 at 21:48