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
37namespace Si {
38
40{
41 return m_path;
42}
43
45{
46 return m_data;
47}
48
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
84{
85 Engine::Trace("Unloading asset: {}", m_path);
86}
87
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
96{
97 return m_text;
98}
99}
T c_str(T... args)
const std::string & GetPath() const
Definition: Asset.cpp:39
Vector< std::uint8_t > m_data
Definition: Asset.hpp:83
Vector< std::uint8_t > & GetBytes()
Definition: Asset.cpp:44
virtual ~Asset()
Definition: Asset.cpp:83
Asset(std::string path)
Definition: Asset.cpp:49
PlainTextAsset(std::string path)
Definition: Asset.cpp:88
const std::string & GetText() const
Definition: Asset.cpp:95
void Error(Args &&... args)
Definition: Log.hpp:103
void Trace(Args &&... args)
Definition: Log.hpp:79
Definition: Allocator.hpp:36