int CALLBACK WinMain(HINSTANCE instance, HINSTANCE previousInstance, LPSTR commandLine, int showCode)
{
	WNDCLASS window = {};

	ResizeDibSection(backBuffer, 1280, 720);

	window.style = CS_HREDRAW | CS_VREDRAW;
	window.lpfnWndProc = MainWindowCallback;
	window.hInstance = instance;
	//window.hIcon;
	window.lpszClassName = "Handmade Hero Take 1";
	DefineControllerFuntions();

	if (RegisterClass(&window))
	{
		HWND handle = CreateWindowEx(0, window.lpszClassName,
			"Handmade Hero", WS_VISIBLE | WS_OVERLAPPEDWINDOW, CW_USEDEFAULT,
			CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, 0, 0, instance, 0);

		if (handle)
		{
			int samplesPerSecond = 48000;
			int blueOffset = 0,
					greenOffset = 0;
			uint32 runningSampleIndex = 0;
			int hertz = 256;
			int squareWavePeriod = samplesPerSecond / hertz;
			int halfPeriod = squareWavePeriod / 2;
			int bytesPerSample = sizeof(int16) * 2;
			int audioBufferSize = samplesPerSecond * bytesPerSample;
			int16 toneVolume = 6000;

			InitializeSound(handle, samplesPerSecond, audioBufferSize);
			audioBuffer->Play(0, 0, 0);

			MSG message;
			BOOL messageResult;
			while (isRunning) {

				// GetMessageA blocks. PeekMessage does not
				while (PeekMessage(&message, 0, 0, 0, PM_REMOVE)) {

					if (message.message == WM_QUIT) {
						isRunning = false;
					}

					TranslateMessage(&message);
					DispatchMessage(&message);
				}

				for (DWORD controllerIndex = 0; controllerIndex < XUSER_MAX_COUNT; controllerIndex++) {
					XINPUT_STATE controllerState;
					if (XInputGetState(controllerIndex, &controllerState) == ERROR_SUCCESS) {
						// The controller is plugged in
						XINPUT_GAMEPAD *pad = &controllerState.Gamepad;

						if (pad->wButtons & XINPUT_GAMEPAD_DPAD_UP)
						{
							greenOffset++;
						}
						if (pad->wButtons & XINPUT_GAMEPAD_DPAD_DOWN)
						{
							greenOffset--;
						}
						if (pad->wButtons & XINPUT_GAMEPAD_DPAD_LEFT)
						{
							blueOffset -= 2;
						}
						if ((pad->wButtons & XINPUT_GAMEPAD_DPAD_RIGHT) || pad->sThumbLX < 0)
						{
							blueOffset++;
						}
						bool leftShoulderPressed = (pad->wButtons & XINPUT_GAMEPAD_LEFT_SHOULDER);
						bool rightShoulderPressed = (pad->wButtons & XINPUT_GAMEPAD_RIGHT_SHOULDER);
						bool aButtonPressed = (pad->wButtons & XINPUT_GAMEPAD_A);
						bool bButtonPressed = (pad->wButtons & XINPUT_GAMEPAD_B);
						bool xButtonPressed = (pad->wButtons & XINPUT_GAMEPAD_X);
						bool yButtonPressed = (pad->wButtons & XINPUT_GAMEPAD_Y);

					}
					else {
						// Controller not available.
					}

					XINPUT_VIBRATION goodVibrations;
					goodVibrations.wLeftMotorSpeed = 6000;
					goodVibrations.wRightMotorSpeed = 6000;
					XInputSetState(0, &goodVibrations);

				}

				renderGradient(backBuffer, blueOffset++, greenOffset);

				// DirectSound output test
				DWORD playCursor;
				DWORD writeCurser;
				if(SUCCEEDED(audioBuffer->GetCurrentPosition(&playCursor, &writeCurser)))
				{
					DWORD byteToLock = runningSampleIndex * bytesPerSample % samplesPerSecond;
					int32 bytesToWrite;
					if(byteToLock > playCursor)
					{
						bytesToWrite = (audioBufferSize - byteToLock);
						bytesToWrite += playCursor;
					}
					else{
						bytesToWrite = playCursor - byteToLock;
					}

					VOID *region1;
					DWORD region1Size;
					VOID *region2;
					DWORD region2Size;


					HRESULT  response = audioBuffer->Lock(byteToLock, bytesToWrite, &region1, &region1Size, &region2, &region2Size, 0);
					if(SUCCEEDED(response))
					{
						int16 *sampleOutput = (int16 *)region1;
						DWORD region1SampleCount = region1Size / bytesPerSample;
						for (int32 sampleIndex = 0; sampleIndex < region1SampleCount; sampleIndex++)
						{
							int16 sampleValue = ((runningSampleIndex++ > halfPeriod) % 2) ? toneVolume : -toneVolume;
							*sampleOutput++ = sampleValue;
							*sampleOutput++ = sampleValue;
						}

						DWORD region2SampleCount = region2Size / bytesPerSample;
						for (int32 sampleIndex = 0; sampleIndex < region2SampleCount; sampleIndex++)
						{
							int16 sampleValue = ((runningSampleIndex++ > halfPeriod) % 2) ? toneVolume : -toneVolume;
							*sampleOutput++ = sampleValue;
							*sampleOutput++ = sampleValue;
						}

						audioBuffer->Unlock(region1, region1Size, region2, region2Size);
					}
				}

				HDC deviceContext = GetDC(handle);
				window_dimensions updateScreen = GetWindowDimensions(handle);
				DisplayBufferInWindow(deviceContext, updateScreen, backBuffer);
				ReleaseDC(handle, deviceContext);
			}
		}
	}

	return 0;
}
Beispiel #2
0
int CALLBACK
WinMain(HINSTANCE instance,
	HINSTANCE prevInstance,
	LPSTR     cmdLine,
	int       showCode)
{
	WNDCLASS windowClass = {};

	// makes the entire client rect part of the window redraw whenever
	// moved horizontally or veritcally
	windowClass.style = CS_HREDRAW | CS_VREDRAW | CS_OWNDC;
	windowClass.lpfnWndProc = MessageHandler;
	windowClass.hInstance = instance;
	windowClass.lpszClassName = L"Gradient Renderer";

	if (RegisterClass(&windowClass))
	{
		HWND window = CreateWindowEx(
			0,
			windowClass.lpszClassName,
			L"GradientRenderer",
			WS_OVERLAPPEDWINDOW | WS_VISIBLE, //window with border and title. also shows up visible by default
			CW_USEDEFAULT,
			CW_USEDEFAULT,
			CW_USEDEFAULT,
			CW_USEDEFAULT,
			0,
			0,
			instance,
			0);

		if (window)
		{
			HDC deviceContext = GetDC(window);
			uint8 xOffset = 0;
			uint8 yOffset = 0;

			initBuffer(&buffer, 1280, 720);

			globalRunning = true;
			while (globalRunning)
			{
				MSG message;
				while (PeekMessage(&message, 0, 0, 0, PM_REMOVE))
				{
					if (message.message == WM_QUIT)
					{
						globalRunning = false;
					}

					TranslateMessage(&message);
					DispatchMessage(&message);
				}

				renderGradient(&buffer, 255, xOffset, yOffset);
				xOffset++;
				yOffset++;


				Dimension windowDimension = getWindowDimension(window);
				drawBufferToScreen(deviceContext, &buffer, windowDimension.width, windowDimension.height);
			}


		}
	}

	return 0;
}