I've been writing my own game engine using SDL3 and DX11. I've been able to get the HWND and set it to swapChainDesc.OutputWindow
and set the color to pink:
#define SDL_MAIN_USE_CALLBACKS 1
#include "SDL3/SDL.h"
#include "SDL3/SDL_main.h"
#include "SDL3/SDL_events.h"
#pragma comment(lib, "d3d11.lib")
#pragma comment(lib, "dxgi.lib")
#pragma comment(lib, "d3dcompiler.lib")
#include <d3d11.h>
#include <DirectXMath.h>
#include <windows.h>
SDL_Window* window;
ID3D11Device* pD3D11Dev = NULL;
ID3D11DeviceContext* pD3D11DevContext = NULL;
IDXGISwapChain *pSwapChain = NULL;
ID3D11Texture2D *pRenderTargetResource = NULL;
ID3D11RenderTargetView *pBackBuffer = NULL;
SDL_AppResult SDL_AppInit(void** appstate, int argc, char* argv[]) {
if (!SDL_Init(SDL_INIT_VIDEO))
{
return SDL_APP_FAILURE;
}
window = SDL_CreateWindow("Doctrina Editor", 640, 480, SDL_WINDOW_RESIZABLE);
HWND hwnd = reinterpret_cast<HWND>(SDL_GetWindowFromID(SDL_GetWindowID(window)));
int windowWidth, windowHeight;
SDL_GetWindowSize(window, &windowWidth, &windowHeight);
D3D_FEATURE_LEVEL featureLevelsRequested = D3D_FEATURE_LEVEL_11_0;
D3D_FEATURE_LEVEL featureLevelsSupported;
DXGI_SWAP_CHAIN_DESC swapChainDesc = {};
swapChainDesc.BufferCount = 1;
swapChainDesc.BufferDesc.Width = windowWidth;
swapChainDesc.BufferDesc.Height = windowHeight;
swapChainDesc.BufferDesc.Format = DXGI_FORMAT_R8G8B8A8_UNORM;
swapChainDesc.BufferDesc.RefreshRate.Numerator = 60;
swapChainDesc.BufferDesc.RefreshRate.Denominator = 1;
swapChainDesc.BufferUsage = DXGI_USAGE_RENDER_TARGET_OUTPUT;
swapChainDesc.OutputWindow = hwnd;
swapChainDesc.SampleDesc.Count = 1;
swapChainDesc.SampleDesc.Quality = 0;
swapChainDesc.Windowed = TRUE;
HRESULT d3dHR = D3D11CreateDeviceAndSwapChain(
NULL,
D3D_DRIVER_TYPE_HARDWARE,
NULL,
D3D11_CREATE_DEVICE_DEBUG,
&featureLevelsRequested,
1,
D3D11_SDK_VERSION,
&swapChainDesc,
&pSwapChain,
&pD3D11Dev,
&featureLevelsSupported,
&pD3D11DevContext
);
pSwapChain->GetBuffer(0, __uuidof(ID3D11Texture2D), (LPVOID*)&pRenderTargetResource);
if (pRenderTargetResource != NULL) {
pD3D11Dev->CreateRenderTargetView(pRenderTargetResource, NULL, &pBackBuffer);
pRenderTargetResource->Release();
pD3D11DevContext->OMSetRenderTargets(1, &pBackBuffer, NULL);
}
D3D11_VIEWPORT viewPort = {};
viewPort.TopLeftX = 0;
viewPort.TopLeftY = 0;
viewPort.Width = windowWidth;
viewPort.Height = windowHeight;
pD3D11DevContext->RSSetViewports(1, &viewPort);
return SDL_APP_CONTINUE;
}
SDL_AppResult SDL_AppEvent(void* appstate, SDL_Event* event) {
if (event->type == SDL_EVENT_QUIT)
{
return SDL_APP_SUCCESS;
}
if (event->type == SDL_EVENT_WINDOW_RESIZED)
{
int newWidth = event->window.data1;
int newHeight = event->window.data2;
}
return SDL_APP_CONTINUE;
}
SDL_AppResult SDL_AppIterate(void* appstate)
{
float color[4];
color[0] = 255.0f;
color[1] = 0.0f;
color[2] = 255.0f;
color[3] = 1.0f;
pD3D11DevContext->ClearRenderTargetView(pBackBuffer, color);
pSwapChain->Present(1, 0);
return SDL_APP_CONTINUE;
}
void SDL_AppQuit(void* appstate, SDL_AppResult result)
{
pSwapChain->Release();
pBackBuffer->Release();
pD3D11Dev->Release();
pD3D11DevContext->Release();
SDL_Quit();
}
But, the window is black, even though it is supposed to be pink?
The code on github: https://github.com/ADoseOfCodeYT/DoctrinaSDK
HWND hwnd = reinterpret_cast<HWND>(SDL_GetWindowFromID(SDL_GetWindowID(window)));
this line is wrong, this is not how you get a HWND
in SDL3, or even SDL2, not sure where you got this from, please follow the SDL3 migration guide
this is the correct way to get the HWND
from SDL3 docs
HWND hwnd = (HWND)SDL_GetPointerProperty(SDL_GetWindowProperties(window), SDL_PROP_WINDOW_WIN32_HWND_POINTER, NULL);
doing this, the window is correctly pink.
You should always check if any function in DX11 fails, like D3D11CreateDeviceAndSwapChain
fails but you never check its return for errors, check for errors even if you just log that the function fails, this helps you debug.
assert(d3dHR == S_OK);
if (d3dHR != S_OK)
{ std::cout << "D3D11CreateDeviceAndSwapChain failed!\n"; }
Also see if you can enable debug layers during development as they save a ton of time catching bugs.