Silicon
A realtime platform for creating interactive media.
Asset.cpp
Go to the documentation of this file.
1
// BSD 2-Clause License
2
//
3
// Copyright (c) 2023, Matthew McCall
4
// All rights reserved.
5
//
6
// Redistribution and use in source and binary forms, with or without
7
// modification, are permitted provided that the following conditions are met:
8
//
9
// 1. Redistributions of source code must retain the above copyright notice, this
10
// list of conditions and the following disclaimer.
11
//
12
// 2. Redistributions in binary form must reproduce the above copyright notice,
13
// this list of conditions and the following disclaimer in the documentation
14
// and/or other materials provided with the distribution.
15
//
16
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
17
// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18
// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
19
// DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
20
// FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21
// DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
22
// SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
23
// CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
24
// OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
25
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26
27
//
28
// Created by Matthew McCall on 1/7/23.
29
//
30
31
#include <
utility
>
32
33
#include "SDL_rwops.h"
34
35
#include "
Silicon/Asset.hpp
"
36
37
namespace
Si
{
38
39
const
std::string
&
Asset::GetPath
()
const
40
{
41
return
m_path;
42
}
43
44
Vector<std::uint8_t>
&
Asset::GetBytes
()
45
{
46
return
m_data
;
47
}
48
49
Asset::Asset
(
std::string
path)
50
: m_path(
std
::move(path))
51
{
52
Engine::Trace
(
"Loading asset: {}"
, m_path);
53
54
// Load entire file into m_data using SDL.
55
SDL_RWops *file = SDL_RWFromFile(m_path.
c_str
(),
"rb"
);
56
if
(!file)
57
{
58
Engine::Error
(
"Failed to open file: {}"
, m_path);
59
throw
std::runtime_error
(
"Failed to open file: "
+ m_path);
60
}
61
62
// Get file size.
63
Sint64 size = SDL_RWsize(file);
64
if
(size < 0)
65
{
66
Engine::Error
(
"Failed to get file size: {}"
, m_path);
67
throw
std::runtime_error
(
"Failed to get file size: "
+ m_path);
68
}
69
70
// Read file into m_data.
71
m_data
.resize(
static_cast<
std::size_t
>
(size));
72
std::size_t
read = SDL_RWread(file,
m_data
.data(), 1,
static_cast<
std::size_t
>
(size));
73
74
if
(read !=
static_cast<
std::size_t
>
(size))
75
{
76
Engine::Error
(
"Failed to read file: {}"
, m_path);
77
throw
std::runtime_error
(
"Failed to read file: "
+ m_path);
78
}
79
80
SDL_RWclose(file);
81
}
82
83
Asset::~Asset
()
84
{
85
Engine::Trace
(
"Unloading asset: {}"
, m_path);
86
}
87
88
PlainTextAsset::PlainTextAsset
(
std::string
path)
89
:
Asset
(
std
::move(path))
90
{
91
// Convert m_data to a string.
92
m_text =
std::string
(
m_data
.begin(),
m_data
.end());
93
}
94
95
const
std::string
&
PlainTextAsset::GetText
()
const
96
{
97
return
m_text;
98
}
99
}
Asset.hpp
std::string
std::string::c_str
T c_str(T... args)
Si::Asset
Definition:
Asset.hpp:45
Si::Asset::GetPath
const std::string & GetPath() const
Definition:
Asset.cpp:39
Si::Asset::m_data
Vector< std::uint8_t > m_data
Definition:
Asset.hpp:83
Si::Asset::GetBytes
Vector< std::uint8_t > & GetBytes()
Definition:
Asset.cpp:44
Si::Asset::~Asset
virtual ~Asset()
Definition:
Asset.cpp:83
Si::Asset::Asset
Asset(std::string path)
Definition:
Asset.cpp:49
Si::PlainTextAsset::PlainTextAsset
PlainTextAsset(std::string path)
Definition:
Asset.cpp:88
Si::PlainTextAsset::GetText
const std::string & GetText() const
Definition:
Asset.cpp:95
Si::Engine::Error
void Error(Args &&... args)
Definition:
Log.hpp:103
Si::Engine::Trace
void Trace(Args &&... args)
Definition:
Log.hpp:79
Si
Definition:
Allocator.hpp:36
std
std::runtime_error
std::size_t
utility
std::vector
src
Asset.cpp
Generated by
1.9.4