Change resource manager to be more closely tied to the opengl context instance rather than the game instance

This commit is contained in:
rexy712 2022-01-18 12:03:46 -08:00
parent 6580fdd4d5
commit 52808c3826
16 changed files with 226 additions and 211 deletions

View File

@ -22,7 +22,6 @@
#include "observable.hpp"
#include "game_state.hpp"
#include "graphics/window.hpp"
#include "resource_manager.hpp"
#include "input.hpp"
#include "math/vec.hpp"
@ -35,7 +34,6 @@ namespace egn{
protected:
gfx::window m_window;
game_state_manager m_states_manager;
resource_manager m_res_manager;
std::queue<input_event> m_event_queue;
double m_last_time;
@ -54,8 +52,8 @@ namespace egn{
void on_notify(const game_state_event& e)override;
resource_manager& resource_man(void);
const resource_manager& resource_man(void)const;
gfx::resource_manager& gfx_resource_manager(void);
const gfx::resource_manager& gfx_resource_manager(void)const;
void push_state(std::unique_ptr<game_state_iface>&&);
void pop_state(void);

View File

@ -1,109 +0,0 @@
/**
This file is a part of our_dick
Copyright (C) 2022 rexy712
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef OUR_DICK_ENGINE_RESOURCE_MANAGER_HPP
#define OUR_DICK_ENGINE_RESOURCE_MANAGER_HPP
#include "engine/image.hpp"
#include "graphics/material.hpp"
#include "graphics/model.hpp"
#include "graphics/shader_program.hpp"
#include <map>
#include <string>
#include <utility> //pair, forward, piecewise_construct
#include <tuple>
namespace egn{
class resource_manager
{
private:
std::map<std::string,gfx::texture_array> m_array_textures;
std::map<std::string,gfx::material> m_textures;
std::map<std::string,gfx::model> m_models;
std::map<std::string,gfx::shader_program> m_shaders;
public:
resource_manager(void) = default;
resource_manager(const resource_manager&) = default;
resource_manager(resource_manager&&) = default;
~resource_manager(void) = default;
resource_manager& operator=(const resource_manager&) = default;
resource_manager& operator=(resource_manager&&) = default;
bool has_array_texture(const char* key)const;
template<class... Args>
std::pair<gfx::texture_array*,bool> emplace_array_texture(const char* key, Args&&... args);
gfx::texture_array* get_array_texture(const char* file);
bool erase_array_texture(const char* key);
bool has_material(const char* key)const;
template<class... Args>
std::pair<gfx::material*,bool> emplace_material(const char* key, Args&&... args);
gfx::material* get_material(const char* file);
bool erase_material(const char* key);
bool has_model(const char* key)const;
template<class... Args>
std::pair<gfx::model*,bool> emplace_model(const char* key, Args&&... args);
gfx::model* get_model(const char* file);
bool erase_model(const char* key);
bool has_shader(const char* key)const;
template<class... Args>
std::pair<gfx::shader_program*,bool> emplace_shader(const char* key, Args&&... args);
gfx::shader_program* get_shader(const char* key);
bool erase_shader(const char* key);
};
template<class... Args>
std::pair<gfx::texture_array*,bool> resource_manager::emplace_array_texture(const char* key, Args&&... args){
if(auto it = m_array_textures.find(key);it != m_array_textures.end())
return {&(*it).second, false};
auto ret = m_array_textures.emplace(std::piecewise_construct, std::tuple(key), std::tuple(std::forward<Args>(args)...));
return {&(*(ret.first)).second, true};
}
template<class... Args>
std::pair<gfx::material*,bool> resource_manager::emplace_material(const char* key, Args&&... args){
if(auto it = m_textures.find(key);it != m_textures.end())
return {&(*it).second, false};
auto ret = m_textures.emplace(std::piecewise_construct, std::tuple(key), std::tuple(std::forward<Args>(args)...));
return {&(*(ret.first)).second, true};
}
template<class... Args>
std::pair<gfx::model*,bool> resource_manager::emplace_model(const char* key, Args&&... args){
if(auto it = m_models.find(key);it != m_models.end())
return {&(*it).second, false};
auto ret = m_models.emplace(std::piecewise_construct, std::tuple(key), std::tuple(std::forward<Args>(args)...));
return {&(*(ret.first)).second, true};
}
template<class... Args>
std::pair<gfx::shader_program*,bool> resource_manager::emplace_shader(const char* key, Args&&... args){
if(auto it = m_shaders.find(key);it != m_shaders.end())
return {&(*it).second, false};
auto ret = m_shaders.emplace(std::piecewise_construct, std::tuple(key), std::tuple(std::forward<Args>(args)...));
return {&(*(ret.first)).second, true};
}
}
#endif

View File

@ -0,0 +1,142 @@
/**
This file is a part of our_dick
Copyright (C) 2022 rexy712
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef OUR_DICK_GRAPHICS_RESOURCE_MANAGER_HPP
#define OUR_DICK_GRAPHICS_RESOURCE_MANAGER_HPP
#include <string>
#include <map>
#include <utility> //pair, forward
#include <tuple>
#include "graphics/texture.hpp"
#include "graphics/shader_program.hpp"
namespace gfx{
namespace detail{
template<class T>
class resource_manager
{
public:
using value_type = T;
using key_type = std::string;
using reference = T&;
using const_reference = const T&;
using pointer = T*;
using const_pointer = const T*;
private:
std::map<key_type,value_type> m_map;
public:
resource_manager(void) = default;
resource_manager(const resource_manager&) = default;
resource_manager(resource_manager&&) = default;
~resource_manager(void) = default;
resource_manager& operator=(const resource_manager&) = default;
resource_manager& operator=(resource_manager&&) = default;
bool has_value(const char* key)const;
template<class... Args>
std::pair<pointer,bool> emplace_value(const char* key, Args&&... args);
pointer get_value(const char* file);
bool erase_value(const char* key);
};
}
class resource_manager
{
private:
detail::resource_manager<texture> m_textures;
detail::resource_manager<texture_array> m_texture_arrays;
detail::resource_manager<shader_program> m_shaders;
public:
resource_manager(void) = default;
resource_manager(const resource_manager&) = default;
resource_manager(resource_manager&&) = default;
~resource_manager(void) = default;
resource_manager& operator=(const resource_manager&) = default;
resource_manager& operator=(resource_manager&&) = default;
bool has_texture_array(const char* key)const;
template<class... Args>
auto emplace_texture_array(const char* key, Args&&... args);
gfx::texture_array* get_texture_array(const char* file);
bool erase_texture_array(const char* key);
bool has_texture(const char* key)const;
template<class... Args>
auto emplace_texture(const char* key, Args&&... args);
gfx::texture* get_texture(const char* file);
bool erase_texture(const char* key);
bool has_shader(const char* key)const;
template<class... Args>
auto emplace_shader(const char* key, Args&&... args);
gfx::shader_program* get_shader(const char* key);
bool erase_shader(const char* key);
};
namespace detail{
template<class T>
bool resource_manager<T>::has_value(const char* key)const{
if(auto it = m_map.find(key);it != m_map.end())
return true;
return false;
}
template<class T>
template<class... Args>
auto resource_manager<T>::emplace_value(const char* key, Args&&... args) -> std::pair<pointer,bool>{
if(auto it = m_map.find(key);it != m_map.end())
return {&(*it).second, false};
auto ret = m_map.emplace(std::piecewise_construct, std::tuple(key), std::tuple(std::forward<Args>(args)...));
return {&(*(ret.first)).second, true};
}
template<class T>
auto resource_manager<T>::get_value(const char* key) -> pointer{
if(auto it = m_map.find(key);it != m_map.end())
return &(*it).second;
return nullptr;
}
template<class T>
bool resource_manager<T>::erase_value(const char* key){
return m_map.erase(key) == 1;
}
}
template<class... Args>
auto resource_manager::emplace_texture_array(const char* key, Args&&... args){
return m_texture_arrays.emplace_value(key, std::forward<Args>(args)...);
}
template<class... Args>
auto resource_manager::emplace_texture(const char* key, Args&&... args){
return m_textures.emplace_value(key, std::forward<Args>(args)...);
}
template<class... Args>
auto resource_manager::emplace_shader(const char* key, Args&&... args){
return m_shaders.emplace_value(key, std::forward<Args>(args)...);
}
}
#endif

View File

@ -24,6 +24,7 @@
#include "math/math.hpp"
#include "util/init_constants.hpp"
#include "fbo.hpp"
#include "resource_manager.hpp"
namespace gfx{
@ -47,6 +48,7 @@ namespace gfx{
GLFWwindow* m_window = nullptr;
glfw_system m_glfw_handle;
fbo m_root_fbo{util::no_initialize};
resource_manager m_resources;
char* m_title = nullptr;
int m_antialias_level = 0;
int m_refresh_rate = GLFW_DONT_CARE;
@ -73,6 +75,9 @@ namespace gfx{
void destroy();
resource_manager& resource_man(void);
const resource_manager& resource_man(void)const;
fbo& framebuffer();
const fbo& framebuffer()const;

View File

@ -22,7 +22,7 @@
#include "graphics/shader_program.hpp"
#include "renderable.hpp"
#include "engine/resource_manager.hpp"
#include "graphics/resource_manager.hpp"
#include "math/vec.hpp"
#include "config.hpp"
@ -62,7 +62,7 @@ private:
std::unique_ptr<board_gfx> m_gfx;
public:
board(egn::resource_manager& r);
board(gfx::resource_manager& r);
~board(void)override = default;
tile::value check_winner(void)const;

View File

@ -24,7 +24,7 @@
#include "graphics/vbo.hpp"
#include "math/math.hpp"
#include "scene.hpp"
#include "engine/resource_manager.hpp"
#include "graphics/resource_manager.hpp"
#include "basic_framebuffer.hpp"
class main_renderer
@ -53,7 +53,7 @@ private:
gfx::vbo m_board_vbo;
public:
main_renderer(egn::resource_manager& res, int width, int height);
main_renderer(gfx::resource_manager& res, int width, int height);
void set_vp_matrix(const math::mat4<GLfloat>& vp);
void render(scene&);

View File

@ -24,7 +24,7 @@
#include "graphics/texture.hpp"
#include "math/math.hpp"
#include "graphics/shader_program.hpp"
#include "engine/resource_manager.hpp"
#include "graphics/resource_manager.hpp"
#include "scene.hpp"
class screen_renderer
@ -50,7 +50,7 @@ private:
int m_screen_width, m_screen_height;
public:
screen_renderer(egn::resource_manager& res, int width, int height, gfx::texture& base_tex);
screen_renderer(gfx::resource_manager& res, int width, int height, gfx::texture& base_tex);
void render(scene&);
void resize_viewport(int width, int height);

View File

@ -101,11 +101,11 @@ namespace egn{
void game::on_notify(const game_state_event&){}
resource_manager& game::resource_man(void){
return m_res_manager;
gfx::resource_manager& game::gfx_resource_manager(void){
return m_window.resource_man();
}
const resource_manager& game::resource_man(void)const{
return m_res_manager;
const gfx::resource_manager& game::gfx_resource_manager(void)const{
return m_window.resource_man();
}
void game::push_state(std::unique_ptr<game_state_iface>&& state){
m_states_manager.push_state(std::move(state));

View File

@ -1,81 +0,0 @@
/**
This file is a part of our_dick
Copyright (C) 2022 rexy712
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "engine/resource_manager.hpp"
namespace egn{
bool resource_manager::has_array_texture(const char* key)const{
if(auto it = m_array_textures.find(key);it != m_array_textures.end())
return true;
return false;
}
gfx::texture_array* resource_manager::get_array_texture(const char* file){
if(auto it = m_array_textures.find(file);it != m_array_textures.end())
return &(*it).second;
return nullptr;
}
bool resource_manager::erase_array_texture(const char* key){
return m_array_textures.erase(key) == 1;
}
bool resource_manager::has_material(const char* key)const{
if(auto it = m_textures.find(key);it != m_textures.end())
return true;
return false;
}
gfx::material* resource_manager::get_material(const char* file){
if(auto it = m_textures.find(file);it != m_textures.end())
return &(*it).second;
return nullptr;
}
bool resource_manager::erase_material(const char* key){
return m_textures.erase(key) == 1;
}
bool resource_manager::has_model(const char* key)const{
if(auto it = m_models.find(key);it != m_models.end())
return true;
return false;
}
gfx::model* resource_manager::get_model(const char* file){
if(auto it = m_models.find(file);it != m_models.end())
return &(*it).second;
return nullptr;
}
bool resource_manager::erase_model(const char* key){
return m_models.erase(key) == 1;
}
bool resource_manager::has_shader(const char* key)const{
if(auto it = m_shaders.find(key);it != m_shaders.end())
return true;
return false;
}
gfx::shader_program* resource_manager::get_shader(const char* file){
if(auto it = m_shaders.find(file);it != m_shaders.end())
return &(*it).second;
return nullptr;
}
bool resource_manager::erase_shader(const char* key){
return m_shaders.erase(key) == 1;
}
}

View File

@ -0,0 +1,53 @@
/**
This file is a part of our_dick
Copyright (C) 2022 rexy712
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "graphics/resource_manager.hpp"
namespace gfx{
bool resource_manager::has_texture_array(const char* key)const{
return m_texture_arrays.has_value(key);
}
gfx::texture_array* resource_manager::get_texture_array(const char* file){
return m_texture_arrays.get_value(file);
}
bool resource_manager::erase_texture_array(const char* key){
return m_texture_arrays.erase_value(key);
}
bool resource_manager::has_texture(const char* key)const{
return m_textures.has_value(key);
}
gfx::texture* resource_manager::get_texture(const char* file){
return m_textures.get_value(file);
}
bool resource_manager::erase_texture(const char* key){
return m_textures.erase_value(key);
}
bool resource_manager::has_shader(const char* key)const{
return m_shaders.has_value(key);
}
gfx::shader_program* resource_manager::get_shader(const char* key){
return m_shaders.get_value(key);
}
bool resource_manager::erase_shader(const char* key){
return m_shaders.erase_value(key);
}
}

View File

@ -327,6 +327,13 @@ static void enable_opengl_debug_context(){
delete[] m_title;
m_title = nullptr;
}
resource_manager& window::resource_man(void){
return m_resources;
}
const resource_manager& window::resource_man(void)const{
return m_resources;
}
fbo& window::framebuffer(){
return m_root_fbo;
}

View File

@ -34,9 +34,9 @@ const math::vec4<float>& tile::get_color(void)const{
return m_color_filter;
}
board::board(egn::resource_manager& resman):
board::board(gfx::resource_manager& resman):
m_tiles(new tile[9]),
m_gfx(new board_gfx(resman.emplace_array_texture("board_textures", 3)))
m_gfx(new board_gfx(resman.emplace_texture_array("board_textures", 3)))
{
m_tiles[0].set_position({-2.125, 2.125, -1});
m_tiles[1].set_position({-2.125, 0, -1});

View File

@ -27,7 +27,7 @@
#define MAX_WIDTH 3840
#define MAX_HEIGHT 2160
main_renderer::main_renderer(egn::resource_manager& res, int width, int height):
main_renderer::main_renderer(gfx::resource_manager& res, int width, int height):
m_fb(MAX_WIDTH, MAX_HEIGHT),
m_primary_fb(util::no_initialize),
m_vbo(s_vertices, sizeof(s_vertices), gfx::vbo::usage::DYNAMIC_DRAW),

View File

@ -27,7 +27,7 @@
pause_state::pause_state(egn::game& owner, egn::game_state_iface* under):
game_state_iface(&owner),
m_under_state(under),
m_screen_renderer(owner.resource_man(), owner.get_width(), owner.get_height(), *owner.resource_man().emplace_material("pause_screen", util::make_deferred<egn::image>("assets/images/pause.png", true)).first){}
m_screen_renderer(owner.gfx_resource_manager(), owner.get_width(), owner.get_height(), *owner.gfx_resource_manager().emplace_texture("pause_screen", util::make_deferred<egn::image>("assets/images/pause.png", true)).first){}
void pause_state::enter(){
m_screen_renderer.resize_viewport(m_owner->get_width(), m_owner->get_height());

View File

@ -29,12 +29,12 @@
play_state::play_state(egn::game& owner, int width, int height):
game_state_iface(&owner),
m_main_renderer(owner.resource_man(), width, height),
m_main_renderer(owner.gfx_resource_manager(), width, height),
m_current_player(rand() % 2)
{
debug_print("First player chosen is %c\n", m_current_player ? 'X' : 'O');
m_scene.objects.emplace_back(new board(m_owner->resource_man()));
m_scene.objects.emplace_back(new board(m_owner->gfx_resource_manager()));
m_scene.renderables.emplace_back(static_cast<board*>(m_scene.objects[0].get()));
m_scene.cameras.emplace_back(new egn::ortho_camera(10 * ((float)width / height), 10, 0.1, 50));

View File

@ -23,7 +23,7 @@
#include <tuple>
#include <utility> //piecewise_construct
screen_renderer::screen_renderer(egn::resource_manager& res, int width, int height, gfx::texture& base_tex):
screen_renderer::screen_renderer(gfx::resource_manager& res, int width, int height, gfx::texture& base_tex):
m_vbo(s_vertices, sizeof(s_vertices), gfx::vbo::usage::STATIC_DRAW),
m_texture(&base_tex),
m_screen_width(width), m_screen_height(height)