How to create a subtitle using SDL

How to create a new window in the SDL that is inserted in the main window? Thus, it can be focused, have a separate drawing context and process events separately.

+3
source share
3 answers

Window in window

You can create a window in a window using the example below. In the example, two windows will appear where it subWindowis located inside mainWindow.. The sub window can be moved from the main window. If you want the sub window to be “stuck” inside the main window, you can use SDL_WindowEventto see when the window was moved and then fasten it in place usingSDL_SetWindowPosition()

Separate event handling

SDL2, . some, , - windowID. SDL_GetWindowID() SDL_Window, , .

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

int main()
{
    SDL_Init( SDL_INIT_EVERYTHING );

    // Set postion and size for main window
    int mainSizeX = 600;
    int mainSizeY = 600;
    int mainPosX = 100;
    int mainPosY = 100;

    // Set postion and size for sub window based on those of main window
    int subSizeX = mainSizeX / 2;
    int subSizeY = mainSizeY / 2;
    int subPosX = mainPosX + mainSizeX / 4;
    int subPosY = mainPosY + mainSizeY / 4;

    // Set up main window
    SDL_Window* mainWindow = SDL_CreateWindow( "Main Window", mainPosX, mainPosY, mainSizeX, mainSizeY, 0 );
    SDL_Renderer* mainRenderer = SDL_CreateRenderer( mainWindow, -1, SDL_RENDERER_ACCELERATED );
    SDL_SetRenderDrawColor( mainRenderer , 255, 0, 0, 255 );

    // Set up sub window
    SDL_Window* subWindow  = SDL_CreateWindow( "Sub Window" , subPosX, subPosY, subSizeX, subSizeY, 0 );
    SDL_Renderer* subRenderer  = SDL_CreateRenderer( subWindow, -1, SDL_RENDERER_ACCELERATED );
    SDL_SetRenderDrawColor( subRenderer , 0, 255, 0, 255 );

    // Render empty ( red ) background in mainWindow
    SDL_RenderClear( mainRenderer );
    SDL_RenderPresent( mainRenderer );

    // Render empty ( green ) background in subWindow
    SDL_RenderClear( subRenderer );
    SDL_RenderPresent( subRenderer );

    std::cin.ignore();
}

.

+5

, - , :

  • SDL_HINT_MOUSE_FOCUS_CLICKTHROUGH "1".
  • SDL_RiseWindow , SDL_WINDOWEVENT_FOCUS_GAINED
+1

Source: https://habr.com/ru/post/1526270/


All Articles