Use loadIntoMemory

This commit is contained in:
Chris Thrasher 2024-04-11 18:13:19 -06:00
parent 8ccf534993
commit 30fcb3523c
5 changed files with 23 additions and 32 deletions

View File

@ -91,18 +91,8 @@ TEST_CASE("[Audio] sf::InputSoundFile")
SECTION("openFromMemory()") SECTION("openFromMemory()")
{ {
const auto memory = loadIntoMemory("Audio/killdeer.wav");
sf::InputSoundFile inputSoundFile; sf::InputSoundFile inputSoundFile;
const auto memory = []()
{
std::ifstream file("Audio/killdeer.wav", std::ios::binary | std::ios::ate);
REQUIRE(file);
const auto size = file.tellg();
file.seekg(0, std::ios::beg);
std::vector<char> buffer(static_cast<std::size_t>(size));
REQUIRE(file.read(buffer.data(), size));
return buffer;
}();
REQUIRE(inputSoundFile.openFromMemory(memory.data(), memory.size())); REQUIRE(inputSoundFile.openFromMemory(memory.data(), memory.size()));
CHECK(inputSoundFile.getSampleCount() == 112'941); CHECK(inputSoundFile.getSampleCount() == 112'941);
CHECK(inputSoundFile.getChannelCount() == 1); CHECK(inputSoundFile.getChannelCount() == 1);

View File

@ -6,12 +6,9 @@
#include <GraphicsUtil.hpp> #include <GraphicsUtil.hpp>
#include <SystemUtil.hpp> #include <SystemUtil.hpp>
#include <fstream>
#include <limits> #include <limits>
#include <ostream> #include <ostream>
#include <cassert>
namespace sf namespace sf
{ {
std::ostream& operator<<(std::ostream& os, const BlendMode& blendMode) std::ostream& operator<<(std::ostream& os, const BlendMode& blendMode)
@ -116,15 +113,3 @@ bool operator==(const sf::Transform& lhs, const Approx<sf::Transform>& rhs)
lhs.getMatrix()[7] == Approx(rhs.value.getMatrix()[7]) && lhs.getMatrix()[7] == Approx(rhs.value.getMatrix()[7]) &&
lhs.getMatrix()[15] == Approx(rhs.value.getMatrix()[15]); lhs.getMatrix()[15] == Approx(rhs.value.getMatrix()[15]);
} }
std::vector<std::byte> loadIntoMemory(const std::filesystem::path& path)
{
std::ifstream file(path, std::ios::binary | std::ios::ate);
assert(file);
const auto size = file.tellg();
file.seekg(0, std::ios::beg);
std::vector<std::byte> buffer(static_cast<std::size_t>(size));
[[maybe_unused]] const auto& result = file.read(reinterpret_cast<char*>(buffer.data()), size);
assert(result);
return buffer;
}

View File

@ -6,11 +6,7 @@
#pragma once #pragma once
#include <SystemUtil.hpp> #include <SystemUtil.hpp>
#include <filesystem>
#include <iosfwd> #include <iosfwd>
#include <vector>
#include <cstddef>
namespace sf namespace sf
{ {
@ -39,5 +35,3 @@ bool operator==(const sf::Rect<T>& lhs, const Approx<sf::Rect<T>>& rhs)
return lhs.left == Approx(rhs.value.left) && lhs.top == Approx(rhs.value.top) && return lhs.left == Approx(rhs.value.left) && lhs.top == Approx(rhs.value.top) &&
lhs.width == Approx(rhs.value.width) && lhs.height == Approx(rhs.value.height); lhs.width == Approx(rhs.value.width) && lhs.height == Approx(rhs.value.height);
} }
[[nodiscard]] std::vector<std::byte> loadIntoMemory(const std::filesystem::path& path);

View File

@ -7,9 +7,13 @@
#include <catch2/catch_approx.hpp> #include <catch2/catch_approx.hpp>
#include <SystemUtil.hpp> #include <SystemUtil.hpp>
#include <fstream>
#include <iomanip> #include <iomanip>
#include <limits> #include <limits>
#include <cassert>
namespace sf namespace sf
{ {
void setStreamPrecision(std::ostream& os, int maxDigits10) void setStreamPrecision(std::ostream& os, int maxDigits10)
@ -75,3 +79,15 @@ bool operator==(const sf::Angle& lhs, const Approx<sf::Angle>& rhs)
{ {
return lhs.asDegrees() == Approx(rhs.value.asDegrees()); return lhs.asDegrees() == Approx(rhs.value.asDegrees());
} }
std::vector<std::byte> loadIntoMemory(const std::filesystem::path& path)
{
std::ifstream file(path, std::ios::binary | std::ios::ate);
assert(file);
const auto size = file.tellg();
file.seekg(0, std::ios::beg);
std::vector<std::byte> buffer(static_cast<std::size_t>(size));
[[maybe_unused]] const auto& result = file.read(reinterpret_cast<char*>(buffer.data()), size);
assert(result);
return buffer;
}

View File

@ -5,7 +5,11 @@
#pragma once #pragma once
#include <filesystem>
#include <iosfwd> #include <iosfwd>
#include <vector>
#include <cstddef>
// String conversions for Catch2 // String conversions for Catch2
namespace sf namespace sf
@ -58,3 +62,5 @@ std::ostream& operator<<(std::ostream& os, const Approx<T>& approx)
{ {
return os << approx.value; return os << approx.value;
} }
[[nodiscard]] std::vector<std::byte> loadIntoMemory(const std::filesystem::path& path);