# SDL2 window as child of Win32 GUI

I'm building an editor. Is it possible to create a child window of SDL2 within a Win32 gui? (The most obvious page from SDLTutorials works for earlier versions of SDL)

• Just a guess, since I learned working with DirectX and not OpenGL. Can't you just initialize your win32 gui and do stuff with it and inside that window initialize an SDL2 viewport (in DirectX the window you're working with is called the viewport). Instead of giving it the whole window just initialize it with a portion. Something like this maybe: lazyfoo.net/tutorials/SDL/09_the_viewport/index.php – Dries Jun 2 '14 at 11:22

Yes it is SDL 2 allows you to tie your window directly to a window handle of a parent window. You can also plug DirectX and/or OpenGL directly into an SDL window. (This is, I think, how SDL2's OpenGL layer does it)

EDIT
Below is the code for the SDL portion of the program. You would create the win32 window as normal and it looks like you still need to use the win32 event system. I have not tried the SDL's in conjunction with it however.

SDL_InitSubSystem(SDL_INIT_VIDEO);
SDL_Window* sdlWnd = SDL_CreateWindowFrom(hWnd);
SDL_SetWindowTitle(sdlWnd, "SDL Window - Set by SDL");
SDL_Surface* s = SDL_GetWindowSurface(sdlWnd);
SDL_FillRect(s, &s->clip_rect, 0xffff00ff);
SDL_UpdateWindowSurface(sdlWnd);


This will open the window from hWnd and set the title and color the window magenta.

EDIT 2
Events do work and you will use the event loop inside of a main loop (or however you choose to do it) as normal. You do not need to use the WndProc callback. But, it must be present for window creation (it may look like below):

LRESULT CALLBACK WndProc(HWND h, UINT m, WPARAM w, LPARAM l) { return DefWindowProc(h, m, w, l); }


EDIT 3
It looks like events are a bit wonky in SDL when using system events. You will basically have to fall back on system events either by using SDL_SYSWMEVENT (get the WM_EVENT from Ev.syswm.msg->msg.win.msg) or use the WndProc.

For example, the events stop working if you resize the window or do anything of that nature.

• Thanks for the answer. I have one problem though. When I run the program, my child window is created and is magenta as you said, but a moment later it changes to the black. So in my render function, I used glClear to change the background to red. When I did this, the same as above happened, but instead of black, it turned red. Any ideas as to what is happening? Window's swapbuffer vs SDL swapbuffer? – ChocoMan Jun 4 '14 at 7:37
• Well you have to enable OpenGL to use the gl functions. I do not know how to do that after the fact. In terms of clearing, it could be due to the window redrawing so if you put that section in a loop it should work, but I have not personally experienced that problem. – CobaltHex Jun 4 '14 at 12:11