Skip to main content
GameDev.net gamedev.net
🔒 Locked

D3D10 with SDL

Started by Trenki Dec 23, 2007 at 10:58 AM 0 replies 1.4k views
Original Post
Trenki
Trenki
Hello all! While learning DX10 i put together a demo program which can be viewed as a tutorial that shows how to initialize D3D10 from an SDL application. It's actually quite easy to use D3D10 together with SDL. The below source code shows how to initialize D3D10 and clear the screen and may be usefull for beginners in D3D10.

#include "SDL.h"
#include "SDL_syswm.h"

#include <d3d10.h>
#include <d3dx10.h>

ID3D10Device * d3ddevice;
IDXGISwapChain * swap_chain;
ID3D10RenderTargetView * render_target_view;

int main(int argc, char *argv[])
{
	// initialize sdl. normally one should check for errors here
	SDL_Init(SDL_INIT_VIDEO);
	SDL_SetVideoMode(640, 480, 32, 0);

	// this retrieves the HWND of the window required later
	SDL_SysWMinfo info;
	SDL_VERSION(&info.version);
	SDL_GetWMInfo(&info);

	// initialize the swap chain structure to some reasonable defaults
	DXGI_SWAP_CHAIN_DESC swap_chain_desc;
	ZeroMemory(&swap_chain_desc, sizeof(swap_chain_desc));

	swap_chain_desc.BufferDesc.Width = 640;
	swap_chain_desc.BufferDesc.Height = 480;
	swap_chain_desc.BufferDesc.Format = DXGI_FORMAT_R8G8B8A8_UNORM;
	swap_chain_desc.BufferDesc.RefreshRate.Denominator = 60;
	swap_chain_desc.BufferDesc.RefreshRate.Denominator = 1;
	swap_chain_desc.BufferCount = 1;
	swap_chain_desc.BufferUsage = DXGI_USAGE_RENDER_TARGET_OUTPUT;
	swap_chain_desc.SampleDesc.Count = 1;
	swap_chain_desc.SampleDesc.Quality = 0;
	swap_chain_desc.OutputWindow = info.window;
	swap_chain_desc.Windowed = TRUE;

	// create device and swap chain. could fail, so in production quality code 
	// you would have to test for errors.
	D3D10CreateDeviceAndSwapChain(
		0,
		D3D10_DRIVER_TYPE_HARDWARE, 
		0,
		D3D10_CREATE_DEVICE_DEBUG,
		D3D10_SDK_VERSION, 
		&swap_chain_desc,
		&swap_chain,
		&d3ddevice
	);

	// create a render target view for the swap chain to actually render to.
	ID3D10Texture2D * back_buffer;
	swap_chain->GetBuffer(
		0,
		IID_ID3D10Texture2D, // could use __uuidof() but that isn't portable
		(LPVOID*)&back_buffer);
	d3ddevice->CreateRenderTargetView(back_buffer, 0, &render_target_view);
	back_buffer->Release();

	// we will render to render_target_view
	d3ddevice->OMSetRenderTargets(1, &render_target_view, 0);

	// we have to define a viewport
	D3D10_VIEWPORT viewport;
	viewport.Width = 640;
	viewport.Height = 480;
	viewport.MinDepth = 0.0f;
	viewport.MaxDepth = 1.0f;
	viewport.TopLeftX = 0;
	viewport.TopLeftY = 0;
	d3ddevice->RSSetViewports(1, &viewport);

	// clear with blue color an show the result
	d3ddevice->ClearRenderTargetView(render_target_view, 
		D3DXCOLOR(0.0f, 0.0f, 1.0f, 0.0f));
	swap_chain->Present(0, 0);
	
	// wait for the quit event
	SDL_Event e;
	while (SDL_WaitEvent(&e) && e.type != SDL_QUIT);

	// unbind render targets before deletion
	d3ddevice->OMSetRenderTargets(0, 0, 0);

	// clean everything up
	render_target_view->Release();
	swap_chain->Release();
	d3ddevice->Release();

	// shutdown
	SDL_Quit();
	return 0;
}

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.