Example #1
0
/**
 * Initializes the rendering using the appropriate rendering method.
 */
bool InitializeRendering()
{
	DestroyWaveMesh();

	DeinitRendering();

	D3DPRESENT_PARAMETERS params;
	memset(&params, 0, sizeof(D3DPRESENT_PARAMETERS));
	params.Windowed = !gFullscreen;
	params.SwapEffect = D3DSWAPEFFECT_DISCARD;
	params.hDeviceWindow = gWindowHandle;
	params.BackBufferCount = 2;
	if (gFullscreen)
	{
		GetScreenSize(gWindowWidth, gWindowHeight);

		params.BackBufferFormat = D3DFMT_A8R8G8B8;
		params.BackBufferWidth = gWindowWidth;
		params.BackBufferHeight = gWindowHeight;
	}
	else
	{
		gWindowWidth = 1280;
		gWindowHeight = 1024;

		unsigned int width, height;
		DetermineWindowSize(width, height);

		SetWindowPos(gWindowHandle, HWND_TOP, 0, 0, width, height, SWP_NOMOVE);
	}

	HRESULT hr = gGraphicsDevice->Reset(&params);
	if (FAILED(hr))
	{
		ReportError("Failed to initialize device");
		return false;
	}

	InitRendering(gWindowWidth, gWindowHeight, gBroadcastWidth, gBroadcastHeight);
	
	// Set the viewport
	D3DVIEWPORT9 vp;
	vp.X = 0;
	vp.Y = 0;
	vp.MinZ = 0.0f;
	vp.MaxZ = 1.0f;
	vp.Width = gWindowWidth;
	vp.Height = gWindowHeight;
	gGraphicsDevice->SetViewport(&vp);

	// Setup the projection matrix
	D3DXMatrixPerspectiveFovLH(&gProjectionMatrix, D3DXToRadian(60), (FLOAT)gWindowWidth/(FLOAT)gWindowHeight, 1, 1000);

	// Create the mesh that will be rendered
	CreateWaveMesh(64, 20);

	InitializeChatRenderer(gWindowWidth, gWindowHeight);

	return true;
}
Example #2
0
void VRRender::Shutdown()
{
	DeinitRendering();
}
Example #3
0
/**
 * The main entry point for the application.
 */
int APIENTRY _tWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPTSTR lpCmdLine, int nCmdShow)
{
	UNREFERENCED_PARAMETER(hPrevInstance);
	UNREFERENCED_PARAMETER(lpCmdLine);

	// Initialize global strings
	LoadString(hInstance, IDS_APP_TITLE, gWindowTitle, sizeof(gWindowTitle));
	LoadString(hInstance, IDC_INTEGRATION, gWindowClass, sizeof(gWindowClass));

	// Register the window class
	RegisterWindowClass(hInstance);

	// Perform application initialization:
	if ( !InitInstance(hInstance, nCmdShow) )
	{
		return FALSE;
	}

	// Set the view to the default position
	ResetView();

	// Cache the last mouse position
	GetCursorPos(&gLastMousePos);

	// Initialize the Twitch SDK
	std::string channelName = "<username>";
	InitializeStreaming("<username>", "<password>", "<clientId>", "<clientSecret>", GetIntelDllPath());

	// Main message loop
	MSG msg;
	while (true)
	{
		// Check to see if any messages are waiting in the queue
		while (PeekMessage(&msg, nullptr, 0, 0, PM_REMOVE))
		{
			// Process window messages
			TranslateMessage(&msg);
			DispatchMessage(&msg);

			// Received a quit message
			if (msg.message == WM_QUIT)
			{
				break;
			}
		}

		// Received a quit message so exit the app
		if (msg.message == WM_QUIT)
		{
			break;
		}

		if (gReinitializeRequired)
		{
			gReinitializeRequired = false;
			InitializeRendering();
		}

		// Draw the scene
		Render();

		UpdateWaveMesh();

		// Process user input independent of the event queue
		if (gFocused && !AcceptingChatInput())
		{
			HandleInput();
		}

		// Record the frame time
		float curTime = GetSystemTimeMs();

		// Begin streaming when ready
		if (gStreamingDesired && 
			!IsStreaming() &&
			IsReadyToStream())
		{
			StartStreaming(gBroadcastWidth, gBroadcastHeight, gBroadcastFramesPerSecond);

			gLastCaptureTime = 0;
		}

		// If you send frames too quickly to the SDK (based on the broadcast FPS you configured) it will not be able 
		// to make use of them all.  In that case, it will simply release buffers without using them which means the
		// game wasted time doing the capture.  To mitigate this, the app should pace the captures to the broadcast FPS.
		float captureDelta = curTime - gLastCaptureTime;
		bool isTimeForNextCapture = (captureDelta / 1000.0f) >= (1.0f / gBroadcastFramesPerSecond);

		// streaming is in progress so try and capture a frame
		if (IsStreaming() && 
			!gPaused &&
			isTimeForNextCapture)
		{
			// capture a snapshot of the back buffer
			unsigned char* pBgraFrame = nullptr;
			int width = 0;
			int height = 0;
			bool gotFrame = false;

			gotFrame = CaptureFrame(gBroadcastWidth, gBroadcastHeight, pBgraFrame, width, height);

			// send a frame to the stream
			if (gotFrame)
			{
				SubmitFrame(pBgraFrame);
			}
		}

		// The SDK may generate events that need to be handled by the main thread so we should handle them
		FlushStreamingEvents();

		#undef CHAT_STATE
		#undef STREAM_STATE
		#define CHAT_STATE(__state__) CS_##__state__
		#define STREAM_STATE(__state__) SS_##__state__

		// initialize chat after we have authenticated
		if (GetChatState() == CHAT_STATE(Uninitialized) && 
			GetStreamState() >= STREAM_STATE(Authenticated))
		{
			InitializeChat(channelName.c_str());
		}

		if (GetChatState() != CHAT_STATE(Uninitialized))
		{
			FlushChatEvents();
		}

		#undef CHAT_STATE
		#undef STREAM_STATE

		gLastFrameTime = curTime;

		// Update the window title to show the state
		#undef STREAM_STATE
		#define STREAM_STATE(__state__) #__state__,
		const char* streamStates[] = 
		{
			STREAM_STATE_LIST
		};
		#undef STREAM_STATE

		#undef CHAT_STATE
		#define CHAT_STATE(__state__) #__state__,
		const char* chatStates[] = 
		{
			CHAT_STATE_LIST
		};
		#undef CHAT_STATE

		char buffer[256];
		sprintf_s(buffer, sizeof(buffer), "Twitch Direct3D Integration Sample - %s - Stream:%s Chat:%s", GetUsername().c_str(), streamStates[GetStreamState()], chatStates[GetChatState()]);
		SetWindowTextA(gWindowHandle, buffer);
	}

	StopStreaming();

	DeinitChatRenderer();

	// Shutdown the Twitch SDK
	ShutdownChat();
	ShutdownStreaming();

	DeinitRendering();

	// Shutdown the app
	gGraphicsDevice->Release();
	gDirect3D->Release();

	// Cleanup the mesh
	DestroyWaveMesh();

	return (int)msg.wParam;
}