DirectXHelper.h 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. #pragma once
  2. #include <ppl.h>
  3. #include <ppltasks.h>
  4. #include <wrl/client.h>
  5. namespace DX {
  6. inline void ThrowIfFailed(HRESULT hr)
  7. {
  8. if (FAILED(hr)) {
  9. // Set a breakpoint on this line to catch Win32 API errors.
  10. throw Platform::Exception::CreateException(hr);
  11. }
  12. }
  13. // Function that reads from a binary file asynchronously.
  14. inline Concurrency::task<Platform::Array<byte> ^> ReadDataAsync(
  15. Platform::String ^ filename)
  16. {
  17. using namespace Windows::Storage;
  18. using namespace Concurrency;
  19. auto folder = Windows::ApplicationModel::Package::Current->InstalledLocation;
  20. return create_task(folder->GetFileAsync(filename))
  21. .then([](StorageFile ^ file) {
  22. #if !PHONE
  23. return FileIO::ReadBufferAsync(file);
  24. #else
  25. return file->OpenReadAsync();
  26. }).then([](Streams::IRandomAccessStreamWithContentType^ stream)
  27. {
  28. unsigned int bufferSize = static_cast<unsigned int>(stream->Size);
  29. auto fileBuffer = ref new Streams::Buffer(bufferSize);
  30. return stream->ReadAsync(fileBuffer, bufferSize, Streams::InputStreamOptions::None);
  31. #endif
  32. })
  33. .then([](Streams::IBuffer ^ fileBuffer) -> Platform::Array<byte> ^ {
  34. auto fileData = ref new Platform::Array<byte>(fileBuffer->Length);
  35. Streams::DataReader::FromBuffer(fileBuffer)->ReadBytes(fileData);
  36. return fileData;
  37. });
  38. }
  39. }