[SDL3] SDL3 Popup Window

SDL, raylib 2025. 4. 13. 13:06 |
반응형

SDL3에서 독립된 자식 윈도우가 아닌 팝업 윈도우를 생성해 보자.

부모 윈도우 영역의 일부처럼 동작한다.

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
#define SDL_MAIN_USE_CALLBACKS 1
#include <SDL3/SDL.h>
#include <SDL3/SDL_main.h>
#include <stdio.h>
#include <Windows.h>
 
static SDL_Window* window = NULL;
static SDL_Renderer* renderer = NULL;
 
static SDL_Window* popwindow = NULL;
static SDL_Renderer* poprenderer = NULL;
 
SDL_AppResult SDL_AppInit(void** appstate, int argc, char* argv[])
{
    SDL_SetAppMetadata("Example Renderer Clear""1.0""com.example.renderer-clear");
 
    if (!SDL_Init(SDL_INIT_VIDEO)) {
        SDL_Log("Couldn't initialize SDL: %s", SDL_GetError());
        return SDL_APP_FAILURE;
    }
 
    if (!SDL_CreateWindowAndRenderer("examples/renderer/clear"6404800&window, &renderer)) {
        SDL_Log("Couldn't create window/renderer: %s", SDL_GetError());
        return SDL_APP_FAILURE;
    }
 
    popwindow = SDL_CreatePopupWindow(window, 00100100, SDL_WINDOW_TOOLTIP);
    if (!popwindow) {
        SDL_Log("Couldn't create popup window: %s", SDL_GetError());
        return SDL_APP_FAILURE;
    }
 
    poprenderer = SDL_CreateRenderer(popwindow, NULL);
    if (!poprenderer) {
        SDL_Log("Couldn't create popup renderer: %s", SDL_GetError());
        return SDL_APP_FAILURE;
    }
 
    SDL_PropertiesID prop = SDL_GetWindowProperties(window);
    HWND hwnd = (HWND)SDL_GetPointerProperty(prop, SDL_PROP_WINDOW_WIN32_HWND_POINTER, NULL);
    if (hwnd)
        printf("Main window handle: %p\n", hwnd);
 
    prop = SDL_GetWindowProperties(popwindow);
    HWND hpwnd = (HWND)SDL_GetPointerProperty(prop, SDL_PROP_WINDOW_WIN32_HWND_POINTER, NULL);
    if (hpwnd)
        printf("Popup window handle: %p\n", hpwnd);
 
    return SDL_APP_CONTINUE;
}
 
SDL_AppResult SDL_AppEvent(void* appstate, SDL_Event* event)
{
    switch (event->type) {
    case SDL_EVENT_QUIT:
        return SDL_APP_SUCCESS;
    case SDL_EVENT_KEY_DOWN:
        printf("Key pressed: %s\n", SDL_GetKeyName(event->key.key));
        if (event->key.key == SDLK_ESCAPE)
            return SDL_APP_SUCCESS;
        break;
    default:
        break;
    }
 
    return SDL_APP_CONTINUE;
}
 
SDL_AppResult SDL_AppIterate(void* appstate)
{
    SDL_SetRenderDrawColor(renderer, 255255255, SDL_ALPHA_OPAQUE);
    SDL_RenderClear(renderer);
    SDL_RenderPresent(renderer);
 
    SDL_SetRenderDrawColor(poprenderer, 00255, SDL_ALPHA_OPAQUE);
    SDL_RenderClear(poprenderer);
    SDL_RenderPresent(poprenderer);
 
    return SDL_APP_CONTINUE;
}
 
void SDL_AppQuit(void* appstate, SDL_AppResult result)
{
    SDL_DestroyRenderer(poprenderer);
    SDL_DestroyWindow(popwindow);
 
    SDL_DestroyRenderer(renderer);
    SDL_DestroyWindow(window);
 
    SDL_Quit();
}
 

 

 

실행하면 팝업 윈도우 영역이 파란색으로 표시된다.

 

팝업 윈도우는 자신의 좌표를 갖는다. 위 코드를 수정한 아래 코드에서 확인해 보자.

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
...
SDL_AppResult SDL_AppInit(void** appstate, int argc, char* argv[])
{
    ...
    popwindow = SDL_CreatePopupWindow(window, 5050100100, SDL_WINDOW_TOOLTIP);
    if (!popwindow) {
        SDL_Log("Couldn't create popup window: %s", SDL_GetError());
        return SDL_APP_FAILURE;
    }
 
    poprenderer = SDL_CreateRenderer(popwindow, NULL);
    if (!poprenderer) {
    SDL_Log("Couldn't create popup renderer: %s", SDL_GetError());
    return SDL_APP_FAILURE;
    ...
}
    ...
}
...
SDL_AppResult SDL_AppIterate(void* appstate)
{
    ...
    SDL_SetRenderDrawColor(poprenderer, 00255, SDL_ALPHA_OPAQUE);
    SDL_RenderClear(poprenderer);
    SDL_SetRenderDrawColor(poprenderer, 25500, SDL_ALPHA_OPAQUE);
    SDL_RenderLine(poprenderer, 005050);
    SDL_RenderPresent(poprenderer);
 
    return SDL_APP_CONTINUE;
}
...
 

 

 

팝업 윈도우를 메인 윈도우 (50, 50) 위치에 (100, 100) 크기로 생성하고 (0, 0)부터 (50, 50)까지 빨간 직선을 그린다.

 

메인 윈도우가 아닌 팝업 윈도우의 좌상단 좌표 (0, 0)에서 (50, 50)까지 빨간 직선이 그려진다.

 

물론 (0, 0)에서 (300, 300) 까지 직선을 그려도 팝업 윈도우의 범위를 넘기 때문에 (100, 100)까지만 그려진다.

 

반응형
Posted by J-sean
: