반응형

부모 윈도우와 자식 윈도우의 입력을 구분해 처리해 보자.

 

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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
#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* childWindow = NULL;
static SDL_Renderer* childRenderer = NULL;
 
SDL_AppResult SDL_AppInit(void** appstate, int argc, char* argv[])
{
    SDL_SetAppMetadata("Example""1.0""Sean");
 
    // Initialize SDL with video subsystem
    if (!SDL_Init(SDL_INIT_VIDEO)) {
        SDL_Log("Couldn't initialize SDL: %s", SDL_GetError());
        return SDL_APP_FAILURE;
    }
 
    // 메인 윈도우 및 렌더러 생성
    if (!SDL_CreateWindowAndRenderer("Parent Window"6404800&window, &renderer)) {
        SDL_Log("Couldn't create window/renderer: %s", SDL_GetError());
        return SDL_APP_FAILURE;
    }
 
    printf("Main window ID: %d\n", SDL_GetWindowID(window));
 
    // 자식 윈도우 및 렌더러 생성
    if (!SDL_CreateWindowAndRenderer("Child Window"6404800&childWindow, &childRenderer)) {
        SDL_Log("Couldn't create child window/renderer: %s", SDL_GetError());
        return SDL_APP_FAILURE;
    }
 
    printf("Child window ID: %d\n", SDL_GetWindowID(childWindow));
 
    // 메인 윈도우를 자식 윈도우의 부모 윈도우로 설정
    if (!SDL_SetWindowParent(childWindow, window)) {
        SDL_Log("Couldn't set child window parent: %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(childWindow);
    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 window: %d, Key: %s\n", event->key.windowID, SDL_GetKeyName(event->key.key));
        if (event->key.key == SDLK_ESCAPE)
            if (event->key.windowID == SDL_GetWindowID(window))
                return SDL_APP_SUCCESS; // Exit on Escape key in main window
            else if (event->key.windowID == SDL_GetWindowID(childWindow)) {
                // 자식 윈도우에서 Escape 키를 누르면 자식 윈도우 닫음
                SDL_DestroyRenderer(childRenderer);
                childRenderer = NULL;
                SDL_DestroyWindow(childWindow);
                childWindow = NULL;
            }
        break;
    case SDL_EVENT_MOUSE_BUTTON_DOWN:
        printf("Mouse button pressed window: %d, Button index: %d\n", event->button.windowID, event->button.button);
        printf("Mouse position: (%d, %d)\n", (int)event->button.x, (int)event->button.y);
        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);
 
    if (childRenderer)
    {
        SDL_SetRenderDrawColor(childRenderer, 00255, SDL_ALPHA_OPAQUE);
        SDL_RenderClear(childRenderer);
        SDL_RenderPresent(childRenderer);
    }
 
    return SDL_APP_CONTINUE;
}
 
void SDL_AppQuit(void* appstate, SDL_AppResult result)
{
    if (childRenderer)
        SDL_DestroyRenderer(childRenderer);
    if (childWindow)
        SDL_DestroyWindow(childWindow);
 
    SDL_DestroyRenderer(renderer);
    SDL_DestroyWindow(window);
 
    SDL_Quit();
}
 

 

자식 윈도우에서 ESC키를 누르면 자식 윈도우만 종료된다.

 

반응형
Posted by J-sean
: