Change image resource type from sf::Image to sf::Texture

This includes an added optimization to the resource manager - it now uses unordered (hash) maps instead of ordered (tree) maps to keep track of loaded resources and paths, for the average constant lookup time.
This commit is contained in:
2015-10-05 23:57:42 -04:00
parent 17095e1760
commit 7f00d0c775
30 changed files with 261 additions and 406 deletions

View File

@@ -27,15 +27,11 @@
#include "restypes.hpp"
bool cur_scen_is_mac = true;
extern sf::Texture items_gworld,tiny_obj_gworld,fields_gworld,roads_gworld,boom_gworld,missiles_gworld;
extern sf::Texture dlogpics_gworld,monst_gworld[],terrain_gworld[],anim_gworld,talkfaces_gworld,pc_gworld;
extern sf::Texture status_gworld, vehicle_gworld, small_ter_gworld;
extern cCustomGraphics spec_scen_g;
extern fs::path tempDir;
void load_spec_graphics_v1(fs::path scen_file);
void load_spec_graphics_v2(int num_sheets);
void reload_core_graphics();
// Load old scenarios (town talk is handled by the town loading function)
static bool load_scenario_v1(fs::path file_to_load, cScenario& scenario, bool only_header);
static bool load_outdoors_v1(fs::path scen_file, location which_out,cOutdoors& the_out, legacy::scenario_data_type& scenario);
@@ -2434,7 +2430,6 @@ void load_spec_graphics_v1(fs::path scen_file) {
}
}
}
reload_core_graphics();
}
void load_spec_graphics_v2(int num_sheets) {
@@ -2446,37 +2441,6 @@ void load_spec_graphics_v2(int num_sheets) {
while(num_sheets-- > 0) {
std::string name = "sheet" + std::to_string(num_sheets);
ResMgr::free<ImageRsrc>(name);
spec_scen_g.sheets[num_sheets].loadFromImage(*ResMgr::get<ImageRsrc>(name));
spec_scen_g.sheets[num_sheets] = *ResMgr::get<ImageRsrc>(name);
}
reload_core_graphics();
}
void reload_core_graphics() {
// TODO: This should really reload ALL textures...
// Now load regular graphics
items_gworld.loadFromImage(*ResMgr::get<ImageRsrc>("objects"));
tiny_obj_gworld.loadFromImage(*ResMgr::get<ImageRsrc>("tinyobj"));
fields_gworld.loadFromImage(*ResMgr::get<ImageRsrc>("fields"));
roads_gworld.loadFromImage(*ResMgr::get<ImageRsrc>("trim"));
boom_gworld.loadFromImage(*ResMgr::get<ImageRsrc>("booms"));
missiles_gworld.loadFromImage(*ResMgr::get<ImageRsrc>("missiles"));
dlogpics_gworld.loadFromImage(*ResMgr::get<ImageRsrc>("dlogpics"));
status_gworld.loadFromImage(*ResMgr::get<ImageRsrc>("staticons"));
for(int i = 0; i < NUM_MONST_SHEETS; i++){
std::ostringstream sout;
sout << "monst" << i + 1;
monst_gworld[i].loadFromImage(*ResMgr::get<ImageRsrc>(sout.str()));
}
for(int i = 0; i < NUM_TER_SHEETS; i++){
std::ostringstream sout;
sout << "ter" << i + 1;
terrain_gworld[i].loadFromImage(*ResMgr::get<ImageRsrc>(sout.str()));
}
anim_gworld.loadFromImage(*ResMgr::get<ImageRsrc>("teranim"));
talkfaces_gworld.loadFromImage(*ResMgr::get<ImageRsrc>("talkportraits"));
pc_gworld.loadFromImage(*ResMgr::get<ImageRsrc>("pcs"));
vehicle_gworld.loadFromImage(*ResMgr::get<ImageRsrc>("vehicle"));
small_ter_gworld.loadFromImage(*ResMgr::get<ImageRsrc>("termap"));
// TODO: Scenario icons ...
}

View File

@@ -33,7 +33,6 @@ using boost::math::constants::pi;
rectangle bg_rects[21];
tessel_ref_t bg[21];
tessel_ref_t bw_pats[6];
sf::Texture bg_gworld, bw_gworld;
bool use_win_graphics = false;
sf::Shader maskShader;
extern fs::path progDir;
@@ -108,8 +107,6 @@ void init_graph_tool(){
bg_rects[18].top += 32;
bg_rects[7].left += 32;
bg_rects[7].top += 32;
bg_gworld.loadFromImage(*ResMgr::get<ImageRsrc>("pixpats"));
bw_gworld.loadFromImage(*ResMgr::get<ImageRsrc>("bwpats"));
register_main_patterns();
}
@@ -391,13 +388,17 @@ rectangle calc_rect(short i, short j){
return base_rect;
}
extern sf::Texture fields_gworld;
graf_pos cCustomGraphics::find_graphic(pic_num_t which_rect, bool party) {
static graf_pos dummy = {&fields_gworld, {72,140,108,28}};
if(party && !party_sheet) return dummy;
bool valid = true;
if(party && !party_sheet) valid = false;
else if(!party && !is_old && (which_rect / 100) >= numSheets)
return dummy;
else if(numSheets == 0) return dummy;
valid = false;
else if(numSheets == 0) valid = false;
if(!valid) {
INVALID:
sf::Texture* blank = ResMgr::get<ImageRsrc>("blank").get();
return {blank, {0,0,36,28}};
}
short sheet = which_rect / 100;
if(is_old || party) sheet = 0;
else which_rect %= 100;
@@ -406,7 +407,7 @@ graf_pos cCustomGraphics::find_graphic(pic_num_t which_rect, bool party) {
store_rect.offset(28 * (which_rect % 10),36 * (which_rect / 10));
sf::Texture* the_sheet = party ? party_sheet.get() : &sheets[sheet];
rectangle test(*the_sheet);
if((store_rect & test) != store_rect) return dummy;
if((store_rect & test) != store_rect) goto INVALID; // FIXME: HACK
return std::make_pair(the_sheet,store_rect);
}
@@ -989,20 +990,11 @@ tessel_ref_t prepareForTiling(sf::Texture& srcImg, rectangle srcRect) {
return ref;
}
void flushTessels(sf::Texture& alteredImg) {
erase_if(tiling_reservoir, [&alteredImg](std::pair<const tessel_ref_t,tessel_t>& kv) -> bool {
if(kv.second.img == &alteredImg) {
delete kv.second.tessel;
return true;
}
return false;
});
if(&alteredImg == &bg_gworld)
register_main_patterns();
}
static void register_main_patterns() {
rectangle bw_rect = {0,0,8,8};
sf::Texture& bg_gworld = *ResMgr::get<ImageRsrc>("pixpats");
sf::Texture& bw_gworld = *ResMgr::get<ImageRsrc>("bwpats");
for(int i = 0; i < 21; i++) {
if(i < 6) {
bw_pats[i] = prepareForTiling(bw_gworld, bw_rect);

View File

@@ -21,8 +21,6 @@
namespace fs = boost::filesystem;
#define LINES_IN_TEXT_WIN 11
#define NUM_TER_SHEETS 14
#define NUM_MONST_SHEETS 10
struct m_pic_index_t {
unsigned char i, x, y;
@@ -118,7 +116,6 @@ void win_draw_string(sf::RenderTarget& dest_window,rectangle dest_rect,std::stri
size_t string_length(std::string str, TextStyle style, short* height = nullptr);
rectangle calc_rect(short i, short j);
void setActiveRenderTarget(sf::RenderTarget& where);
void flushTessels(sf::Texture& alteredImg);
tessel_ref_t prepareForTiling(sf::Texture& srcImg, rectangle srcRect);
void tileImage(sf::RenderTarget& target, rectangle area, tessel_ref_t tessel, sf::BlendMode mode = sf::BlendNone);
void tileImage(sf::RenderWindow& target, Region& rgn, tessel_ref_t tessel, sf::BlendMode mode = sf::BlendNone);

View File

@@ -10,14 +10,23 @@
#define BOE_RESMGR_H
#include <string>
#include <map>
#include <unordered_map>
#include <stack>
#include <exception>
#include <memory>
#include <boost/filesystem.hpp>
#include <boost/functional/hash.hpp>
#include <functional>
#include <iostream>
namespace std {
template<> struct hash<boost::filesystem::path> {
size_t operator()(const boost::filesystem::path& p) const {
return boost::filesystem::hash_value(p);
}
};
}
/// A simple resource manager.
/// Handles loading, retaining, and releasing of resources as necessary.
/// Resources include sounds, images, fonts, and cursors.
@@ -35,8 +44,8 @@ namespace ResMgr {
/// @tparam type The type of resource that this pool manages.
template<typename type> struct resPool {
/// Get the map of all currently-loaded resources from this resource pool.
static std::map<std::string,std::shared_ptr<type> >& resources() {
static std::map<std::string,std::shared_ptr<type> > data;
static std::unordered_map<std::string,std::shared_ptr<type> >& resources() {
static std::unordered_map<std::string,std::shared_ptr<type> > data;
return data;
}
/// Get the current search path stack for this resource pool.
@@ -51,8 +60,8 @@ namespace ResMgr {
}
/// Get the map of past path resolutions.
/// @return A map of relative paths to the absolute path they most recently resolved to.
static std::map<fs::path,fs::path>& pathFound() {
static std::map<fs::path,fs::path> data;
static std::unordered_map<fs::path,fs::path>& pathFound() {
static std::unordered_map<fs::path,fs::path> data;
return data;
}
/// Convert a relative path to an absolute path by checking the current search path stack.

View File

@@ -22,7 +22,7 @@
#include "cursors.hpp"
#include "location.hpp"
using ImageRsrc = sf::Image;
using ImageRsrc = sf::Texture;
using CursorRsrc = Cursor;
using FontRsrc = sf::Font;
using StringRsrc = std::vector<std::string>;