DirectXHelper.h 1.4 KB

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