Skip to content

Commit

Permalink
Finalize preloader with library functions
Browse files Browse the repository at this point in the history
  • Loading branch information
VectorWolf committed Dec 6, 2019
1 parent d2bb831 commit 0145854
Show file tree
Hide file tree
Showing 10 changed files with 37 additions and 7 deletions.
3 changes: 3 additions & 0 deletions include/gameinfo.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,9 @@ namespace salmon {
unsigned get_screen_x_resolution() const;
unsigned get_screen_y_resolution() const;

void add_preload_directory(std::string);
bool preload(float seconds);

AudioManagerRef get_audio_manager();
DataBlockRef get_data();
InputCacheRef get_input_cache();
Expand Down
8 changes: 7 additions & 1 deletion src/audio/audio_manager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,12 @@
* along with the RawSalmonEngine. If not, see <http://www.gnu.org/licenses/>.
*/
#include "audio/audio_manager.hpp"

#include "util/game_types.hpp"

bool AudioManager::load_music(std::string path, bool absolute) {
if(!absolute) {path = m_music_base_path + path;}
make_path_absolute(path);
//std::cout << path << "\n";
Music temp(path);
if(temp.good()) {
m_music[path] = temp;
Expand All @@ -32,6 +34,8 @@ bool AudioManager::load_music(std::string path, bool absolute) {
}
bool AudioManager::load_sound(std::string path, bool absolute) {
if(!absolute) {path = m_sounds_base_path + path;}
make_path_absolute(path);
//std::cout << path << "\n";
SoundEffect temp(path);
if(temp.good()) {
m_sounds[path] = temp;
Expand All @@ -44,6 +48,7 @@ bool AudioManager::load_sound(std::string path, bool absolute) {

Music& AudioManager::get_music(std::string path, bool absolute) {
if(!absolute) {path = m_music_base_path + path;}
make_path_absolute(path);
if(m_music.find(path) != m_music.end() || load_music(path, true)) {
return m_music.at(path);
}
Expand All @@ -54,6 +59,7 @@ Music& AudioManager::get_music(std::string path, bool absolute) {
}
SoundEffect& AudioManager::get_sound(std::string path, bool absolute) {
if(!absolute) {path = m_sounds_base_path + path;}
make_path_absolute(path);
if(m_sounds.find(path) != m_sounds.end() || load_sound(path, true)) {
return m_sounds.at(path);
}
Expand Down
5 changes: 2 additions & 3 deletions src/core/gameinfo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,11 +44,10 @@ GameInfo::GameInfo() : m_preloader{this}, m_input_cache{this} {
Logger(Logger::error) << "Couldn't get location of executable! Probably running on currently unsupported OS";
}
m_resource_path = m_base_path + m_resource_path;
make_path_absolute(m_resource_path);
m_resource_path = m_resource_path + "/";
m_current_path = m_resource_path;

m_preloader.add_directory(m_resource_path);
m_preloader.load_recursive(300000);

m_audio_manager.set_music_path(m_resource_path);
m_audio_manager.set_sound_path(m_resource_path);
}
Expand Down
1 change: 1 addition & 0 deletions src/core/gameinfo.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ class GameInfo {

bool m_key_repeat = false; // If false, ignores automatically repeated key presses

Preloader& get_preloader() {return m_preloader;}
TextureCache& get_texture_cache() {return m_texture_cache;}
AudioManager& get_audio_manager() {return m_audio_manager;}
FontManager& get_font_manager() {return m_font_manager;}
Expand Down
8 changes: 6 additions & 2 deletions src/graphics/texture_cache.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2017-2018 Agouti Games Team (see the AUTHORS file)
* Copyright 2017-2019 Agouti Games Team (see the AUTHORS file)
*
* This file is part of the RawSalmonEngine.
*
Expand All @@ -20,13 +20,17 @@

#include <iostream>

#include "util/game_types.hpp"

Texture TextureCache::get(std::string full_path) {
make_path_absolute(full_path);
if(!load(full_path)) {return m_empty_texture;}
else {return m_textures.at(full_path);}
}
bool TextureCache::load(std::string full_path) {
std::cout << full_path << "\n";
make_path_absolute(full_path);
if(has(full_path)) {return true;}
//std::cout << full_path << "\n";

Texture temp;
if(temp.loadFromFile(m_renderer,full_path)) {
Expand Down
2 changes: 1 addition & 1 deletion src/graphics/texture_cache.hpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2017-2018 Agouti Games Team (see the AUTHORS file)
* Copyright 2017-2019 Agouti Games Team (see the AUTHORS file)
*
* This file is part of the RawSalmonEngine.
*
Expand Down
7 changes: 7 additions & 0 deletions src/include_impl/gameinfo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,13 @@ unsigned salmon::GameInfo::get_screen_y_resolution() const {return m_impl->get_s
bool salmon::GameInfo::window_minimized() const {return m_impl->window_minimized();}
bool salmon::GameInfo::window_active() const {return m_impl->window_active();}

void salmon::GameInfo::add_preload_directory(std::string dir) {
m_impl->get_preloader().add_directory(m_impl->get_resource_path() + dir);
}
bool salmon::GameInfo::preload(float seconds) {
return m_impl->get_preloader().load_recursive(static_cast<Uint32>(seconds * 1000));
}

salmon::AudioManagerRef salmon::GameInfo::get_audio_manager() {return m_impl->get_audio_manager();}
salmon::DataBlockRef salmon::GameInfo::get_data() {return m_impl->get_data();}
salmon::InputCacheRef salmon::GameInfo::get_input_cache() {return m_impl->get_input_cache();}
8 changes: 8 additions & 0 deletions src/util/game_types.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@
#include "util/game_types.hpp"
#include <iostream>

#include <experimental/filesystem>

namespace fs = std::experimental::filesystem;

/// Converts a @c string to an @c enum of @c AnimationType
salmon::AnimationType str_to_anim_type(const std::string& name) {
if(name == "IDLE") return salmon::AnimationType::idle;
Expand Down Expand Up @@ -116,3 +120,7 @@ SDL_Color str_to_color(const std::string& name) {

return color;
}

void make_path_absolute(std::string& path) {
path = fs::canonical(path).string();
}
1 change: 1 addition & 0 deletions src/util/game_types.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ Priority str_to_priority(const std::string& name);
EventSignal str_to_event_signal(const std::string& name);
Response str_to_response(const std::string& name);
SDL_Color str_to_color(const std::string& name);
void make_path_absolute(std::string& path);

std::vector<float> dir_to_mov(const salmon::Direction dir);

Expand Down
1 change: 1 addition & 0 deletions src/util/preloader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ bool Preloader::load_recursive(Uint32 milliseconds) {
m_files.push_back(p.path().string());
}
}
m_directories.clear();
}
Uint32 start = SDL_GetTicks();
while(!m_files.empty()) {
Expand Down

0 comments on commit 0145854

Please sign in to comment.