DirectXHelper.h 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. /*
  2. * Copyright (c) 2014, Oculus VR, Inc.
  3. * All rights reserved.
  4. *
  5. * This source code is licensed under the BSD-style license found in the
  6. * LICENSE file in the root directory of this source tree. An additional grant
  7. * of patent rights can be found in the PATENTS file in the same directory.
  8. *
  9. */
  10. #pragma once
  11. #include <wrl/client.h>
  12. #include <ppl.h>
  13. #include <ppltasks.h>
  14. namespace DX
  15. {
  16. inline void ThrowIfFailed(HRESULT hr)
  17. {
  18. if (FAILED(hr))
  19. {
  20. // Set a breakpoint on this line to catch Win32 API errors.
  21. throw Platform::Exception::CreateException(hr);
  22. }
  23. }
  24. // Function that reads from a binary file asynchronously.
  25. inline Concurrency::task<Platform::Array<byte>^> ReadDataAsync(Platform::String^ filename)
  26. {
  27. using namespace Windows::Storage;
  28. using namespace Concurrency;
  29. auto folder = Windows::ApplicationModel::Package::Current->InstalledLocation;
  30. return create_task(folder->GetFileAsync(filename)).then([] (StorageFile^ file)
  31. {
  32. return file->OpenReadAsync();
  33. }).then([] (Streams::IRandomAccessStreamWithContentType^ stream)
  34. {
  35. unsigned int bufferSize = static_cast<unsigned int>(stream->Size);
  36. auto fileBuffer = ref new Streams::Buffer(bufferSize);
  37. return stream->ReadAsync(fileBuffer, bufferSize, Streams::InputStreamOptions::None);
  38. }).then([] (Streams::IBuffer^ fileBuffer) -> Platform::Array<byte>^
  39. {
  40. auto fileData = ref new Platform::Array<byte>(fileBuffer->Length);
  41. Streams::DataReader::FromBuffer(fileBuffer)->ReadBytes(fileData);
  42. return fileData;
  43. });
  44. }
  45. }
粤ICP备19079148号