void show_transient_error_message(CVideo& video , const std::string& message , bool message_use_markup) { LOG_STREAM(err, lg::general) << message << '\n'; show_transient_message(video, _("Error"), message, message_use_markup); }
void LevelLoader::loadTiles(const std::string& filepath, Level& level, TextureSheet* tileSet, TileCreator* creator) { std::ifstream ifs(filepath); //should probably do more error checking if (!ifs) LOG_STREAM(std::cerr) << "Failed to load: " << filepath; ifs >> level._levelID; int blockWidth, blockHeight; ifs >> blockWidth >> blockHeight; int numRows = blockHeight * Constants::TILES_PER_BLOCK_HEIGHT; int numCols = blockWidth * Constants::TILES_PER_BLOCK_WIDTH; level.tileArrangement.rows = numRows; level.tileArrangement.cols = numCols; level.tileArrangement.tiles = std::vector<std::vector<Tile>> (numRows, std::vector<Tile>(numCols)); for (int r = 0; r < numRows; ++r) { for (int c = 0; c < numCols; ++c) { int tileNum; ifs >> tileNum; level.tileArrangement.tiles[r][c] = creator->create(tileSet, tileNum, r, c); } } }
bool theme::set_resolution(const SDL_Rect& screen) { bool result = false; const config::child_list& resolutions = cfg_.get_children("resolution"); int current_rating = 1000000; config::child_list::const_iterator i; config::child_list::const_iterator current = resolutions.end(); for(i = resolutions.begin(); i != resolutions.end(); ++i) { const int width = atoi((**i)["width"].c_str()); const int height = atoi((**i)["height"].c_str()); LOG_DP << "comparing resolution " << screen.w << "," << screen.h << " to " << width << "," << height << "\n"; if(screen.w >= width && screen.h >= height) { LOG_DP << "loading theme: " << width << "," << height << "\n"; current = i; result = true; break; } const int rating = width*height; if(rating < current_rating) { current = i; current_rating = rating; } } if(current == resolutions.end()) { if(!resolutions.empty()) { LOG_STREAM(err, display) << "No valid resolution found\n"; } return false; } std::map<std::string,std::string> title_stash; std::vector<theme::menu>::iterator m; for (m = menus_.begin(); m != menus_.end(); ++m) { if (!m->title().empty() && !m->get_id().empty()) title_stash[m->get_id()] = m->title(); } panels_.clear(); labels_.clear(); status_.clear(); menus_.clear(); const config& cfg = **current; add_object(cfg); for (m = menus_.begin(); m != menus_.end(); ++m) { if (title_stash.find(m->get_id()) != title_stash.end()) m->set_title(title_stash[m->get_id()]); } theme_reset_.notify_observers(); return result; }
/** * Logs a message * Arg 1: (optional) Logger * Arg 2: Message */ static int intf_log(lua_State *L) { const std::string& logger = lua_isstring(L, 2) ? luaL_checkstring(L, 1) : ""; std::string msg = lua_isstring(L, 2) ? luaL_checkstring(L, 2) : luaL_checkstring(L, 1); if(msg.empty() || msg.back() != '\n') { msg += '\n'; } if(logger == "err" || logger == "error") { LOG_STREAM(err, log_user) << msg; } else if(logger == "warn" || logger == "wrn" || logger == "warning") { LOG_STREAM(warn, log_user) << msg; } else if((logger == "debug" || logger == "dbg")) { LOG_STREAM(debug, log_user) << msg; } else { LOG_STREAM(info, log_user) << msg; } return 0; }
void set(aspect_ptr a) { boost::shared_ptr< typesafe_aspect <T> > c = boost::dynamic_pointer_cast< typesafe_aspect<T> >(a); if (c) { assert (c->get_id()== this->get_name()); where_ = c; aspects_.insert(make_pair(this->get_name(),c)); } else { LOG_STREAM(debug, aspect::log()) << "typesafe_known_aspect [" << this->get_name() << "] : while setting aspect, got null. this might be caused by invalid [aspect] WML" << std::endl; } }
virtual void add_facet(const config &cfg) { boost::shared_ptr< composite_aspect <T> > c = boost::dynamic_pointer_cast< composite_aspect<T> >(where_); if (c) { assert (c->get_id()==this->get_name()); c->add_facet(-1, cfg); c->invalidate(); } else { LOG_STREAM(debug, aspect::log()) << "typesafe_known_aspect [" << this->get_name() << "] : while adding facet to aspect, got null. this might be caused by target [aspect] being not composite" << std::endl; } }
SpriteSheetInfo* SpritesheetInfoReader::info() { std::ifstream fileInfo(_infoPath); if (!fileInfo) return nullptr; std::map<std::string, int> loadedValues; std::string parameterName; int parameterValue; try { for (int i = 0; i < 6; ++i) { fileInfo >> parameterName >> parameterValue; loadedValues.insert(std::make_pair(parameterName, parameterValue)); } int frameTime; while (fileInfo >> frameTime) { info_.frameTimes.push_back(frameTime); } } catch (...) { LOG_STREAM(std::cerr) << "Failed to read '" << _infoPath << '\''; return nullptr; } bool fail = false; //this is dumb mapContains(loadedValues, "numSprites", fail); mapContains(loadedValues, "spriteWidth", fail); mapContains(loadedValues, "spriteHeight", fail); mapContains(loadedValues, "numRows", fail); mapContains(loadedValues, "numCols", fail); mapContains(loadedValues, "padding", fail); if (fail) return nullptr; else { info_.num = loadedValues["numSprites"]; info_.width = loadedValues["spriteWidth"]; info_.height = loadedValues["spriteHeight"]; info_.numRows = loadedValues["numRows"]; info_.numCols = loadedValues["numCols"]; info_.padding = loadedValues["padding"]; return &info_; } }
const language_def& get_locale() { //TODO: Add in support for querying the locale on Windows assert(known_languages.size() != 0); const std::string& prefs_locale = preferences::language(); if(prefs_locale.empty() == false) { wesnoth_setlocale(LC_MESSAGES, prefs_locale, NULL); for(language_list::const_iterator i = known_languages.begin(); i != known_languages.end(); ++i) { if (prefs_locale == i->localename) return *i; } LOG_STREAM(info, general) << "'" << prefs_locale << "' locale not found in known array; defaulting to system locale\n"; return known_languages[0]; } #if 0 const char* const locale = getenv("LANG"); #ifdef _WIN32 std::string win_locale = locale #include "language_win32.ii" return win_locale; #endif if(locale != NULL && strlen(locale) >= 2) { //we can't pass pointers into the string to the std::string //constructor because some STL implementations don't support //it (*cough* MSVC++6) std::string res(2,'z'); res[0] = tolower(locale[0]); res[1] = tolower(locale[1]); return res; } #endif LOG_STREAM(info, general) << "locale could not be determined; defaulting to system locale\n"; return known_languages[0]; }
void floating_textbox::tab(std::vector<team>& teams, const unit_map& /*units*/, game_display& gui) { if(active() == false) { return; } switch(mode_) { case gui::TEXTBOX_SEARCH: case gui::TEXTBOX_COMMAND: case gui::TEXTBOX_MESSAGE: { std::string text = box_->text(); std::vector<std::string> matches; // Add players for(size_t n = 0; n != teams.size(); ++n) { if(teams[n].is_empty()) continue; matches.push_back(teams[n].current_player()); } // Add observers const std::set<std::string>& observers = gui.observers(); for(std::set<std::string>::const_iterator i = observers.begin(); i != observers.end(); ++i) { matches.push_back(*i); } // Remove duplicates. std::sort<std::vector<std::string>::iterator> (matches.begin(), matches.end()); matches.erase(std::unique(matches.begin(), matches.end()), matches.end()); // Exclude own nick from tab-completion. if (mode_ == gui::TEXTBOX_MESSAGE) { matches.erase(std::remove(matches.begin(), matches.end(), preferences::login()), matches.end()); } const bool line_start = utils::word_completion(text, matches); if (matches.empty()) return; if (matches.size() == 1 && mode_ == gui::TEXTBOX_MESSAGE) { text.append(line_start ? ": " : " "); } else { std::string completion_list = utils::join(matches, ' '); gui.add_chat_message(time(NULL), "", 0, completion_list, game_display::MESSAGE_PRIVATE, false); } box_->set_text(text); break; } default: LOG_STREAM(err, display) << "unknown textbox mode\n"; } }
bool FileWriter::OpenFile(bool bBinary) { LOG_TRACE(__FUNCTION__); try { m_ofFileWriter.open(m_strFilePath,ios::out); return true; } catch(...) { LOG_STREAM(__FUNCTION__,ERROR,"File cannot open!"); return false; } }
void LevelLoader::loadEnemies(const std::string& filepath, Level& level) { std::ifstream ifs(filepath); if (!ifs) LOG_STREAM(std::cerr) << "Failed to load: " << filepath; std::string enemyName; Point pos; while (ifs >> enemyName >> pos.x >> pos.y) { level.addEnemyAtLocation(enemyName, pos); } }
void TestUtls(std::function<int(void)> fun, std::string name) { LOG_STREAM(LOG4Z_MAIN_LOGGER_ID, LOG_LEVEL_ALARM, nullptr, 0, ""); LOGA("begin test ----[" << name << "]----"); double now = getFloatNowTime(); int ret = fun(); if (ret == 0) { LOGA("end test ----[" << name << "](0)----" << " used second=" << getFloatNowTime() - now); } else { LOGE("end test ----[" << name << "](" << ret << ")----" << " used second=" << getFloatNowTime() - now ); exit(ret); } }
SpritesheetInfo* SpritesheetInfoReader::info() { std::ifstream fileInfo(_infoPath); if (!fileInfo) return nullptr; std::map<std::string, int> loadedValues; std::string parameterName; int parameterValue; try { while (fileInfo >> parameterName >> parameterValue) { loadedValues.insert(std::make_pair(parameterName, parameterValue)); } } catch (...) { LOG_STREAM(std::cerr) << "Failed to read '" << _infoPath << '\''; return nullptr; } bool fail = false; //this is dumb mapContains(loadedValues, "numSprites", fail); mapContains(loadedValues, "spriteWidth", fail); mapContains(loadedValues, "spriteHeight", fail); mapContains(loadedValues, "numRows", fail); mapContains(loadedValues, "numCols", fail); mapContains(loadedValues, "padding", fail); if (fail) return nullptr; else { info_ = SpritesheetInfo{ loadedValues["numSprites"], loadedValues["spriteWidth"], loadedValues["spriteHeight"], loadedValues["numRows"], loadedValues["numCols"], loadedValues["padding"] }; return &info_; } }
int NetworkImplementation::GetProtocol(Enum_protocol enumProtocol) const { LOG_TRACE(__FUNCTION__); switch(enumProtocol) { case TCP: return SOCK_STREAM; break; case UDP: return SOCK_DGRAM; break; default: LOG_STREAM(__FUNCTION__,ERROR,"unkown protocol type!"); return -1; } }
bool FileReader::OpenFile(bool bBinary) { LOG_TRACE(__FUNCTION__); try { if(!bBinary) m_ifFileReader.open(m_strFilePath,ios::in); else m_ifFileReader.open(m_strFilePath,ios::in|ios::binary); return true; } catch(...) { LOG_STREAM(__FUNCTION__,ERROR,"File cannot open!"); return false; } }
void init_textdomains(const config& cfg) { config::const_child_itors t = cfg.child_range("textdomain"); for(;t.first != t.second; ++t.first) { const std::string name = (**t.first)["name"]; const std::string path = (**t.first)["path"]; if(path.empty()) { // t_string::add_textdomain(name, get_intl_dir()); } else { std::string location = get_binary_dir_location("", path); if (location.empty()) { //if location is empty, this causes a crash on Windows, so we //disallow adding empty domains LOG_STREAM(err, general) << "no location found for '" << path << "', not adding textdomain\n"; } else { // t_string::add_textdomain(name, location); } } } }
/** * Show one tip-of-the-day in a frame on the titlescreen. * This frame has 2 buttons: Next-Tip, and Show-Help. */ static void draw_tip_of_day(CVideo& screen, config& tips_of_day, const gui::dialog_frame::style& style, const SDL_Rect* const main_dialog_area ) { if(preferences::show_tip_of_day() == false) { return; } // Restore the previous tip of day area to its old state (section of the title image). //tip_of_day_restorer.restore(); // Draw tip of the day const config* tip = get_tip_of_day(tips_of_day); if(tip != NULL) { int tip_width = main_dialog_area->w; //game_config::title_tip_width * screen.w() / 1024; try { const std::string& text = font::word_wrap_text((*tip)["text"], font::SIZE_NORMAL, tip_width); const std::string& source = font::word_wrap_text((*tip)["source"], font::SIZE_NORMAL, tip_width); // const int pad = game_config::title_tip_padding; #ifdef __IPAD__ const int pad = 10; #else const int pad = 5; #endif SDL_Rect area = font::text_area(text,font::SIZE_NORMAL); area.w = tip_width; SDL_Rect source_area = font::text_area(source, font::SIZE_NORMAL, TTF_STYLE_ITALIC); area.w = std::max<size_t>(area.w, source_area.w) + 2*pad; //area.x = main_dialog_area->x; // - (game_config::title_tip_x * screen.w() / 1024) - area.w; //area.y = main_dialog_area->y + (main_dialog_area->h - area.h)/2; SDL_Rect total_area; total_area.w = area.w; total_area.h = area.h + source_area.h + 3*pad; total_area.x = main_dialog_area->x; total_area.y = main_dialog_area->y + (main_dialog_area->h - total_area.h)/2; source_area.y = total_area.y + area.h + pad; // Note: The buttons' locations need to be set before the dialog frame is drawn. // Otherwise, when the buttons restore their area, they // draw parts of the old dialog frame at their old locations. // This way, the buttons draw a part of the title image, // because the call to restore above restored the area // of the old tip of the day to its initial state (the title image). // int button_x = area.x + area.w - next_tip_button->location().w - pad; // int button_y = area.y + area.h - pad - next_tip_button->location().h; // next_tip_button->set_location(button_x, button_y); // next_tip_button->set_dirty(); //force redraw even if location did not change. // button_x -= previous_tip_button->location().w + pad; // previous_tip_button->set_location(button_x, button_y); // previous_tip_button->set_dirty(); // button_x = area.x + pad; // if (help_tip_button != NULL) // { // help_tip_button->set_location(button_x, button_y); // help_tip_button->set_dirty(); // } gui::dialog_frame tip_frame(screen, "", gui::dialog_frame::titlescreen_style, false); SDL_Rect borderRect = total_area; tip_frame.layout(borderRect); tip_frame.draw_background(); tip_frame.draw_border(); // gui::dialog_frame f(screen, "", style, false); // tip_of_day_restorer = surface_restorer(&screen.video(), f.layout(area).exterior); // f.draw_background(); // f.draw_border(); font::draw_text(&screen, total_area, font::SIZE_NORMAL, font::NORMAL_COLOUR, text, total_area.x + pad, total_area.y + pad); font::draw_text(&screen, total_area, font::SIZE_NORMAL, font::NORMAL_COLOUR, source, total_area.x + total_area.w - source_area.w - pad, source_area.y, false, TTF_STYLE_ITALIC); } catch (utils::invalid_utf8_exception&) { LOG_STREAM(err, engine) << "Invalid utf-8 found, tips of day aren't drawn.\n"; return; } // LOG_DP << "drew tip of day\n"; } }
game_display::game_display(unit_map& units, CVideo& video, const gamemap& map, const gamestatus& status, const std::vector<team>& t, const config& theme_cfg, const config& cfg, const config& level) : display(video, &map, theme_cfg, cfg, level), units_(units), temp_unit_(NULL), temp_unit_loc_(), attack_indicator_src_(), attack_indicator_dst_(), energy_bar_rects_(), route_(), status_(status), teams_(t), level_(level), invalidateUnit_(true), displayedUnitHex_(), overlays_(), currentTeam_(0), activeTeam_(0), sidebarScaling_(1.0), first_turn_(true), in_game_(false), observers_(), chat_messages_(), tod_hex_mask1(NULL), tod_hex_mask2(NULL), reach_map_(), reach_map_old_(), reach_map_changed_(true), game_mode_(RUNNING), flags_() { singleton_ = this; // Inits the flag list and the team colors used by ~TC flags_.reserve(teams_.size()); std::vector<shared_string> side_colors; side_colors.reserve(teams_.size()); for(size_t i = 0; i != teams_.size(); ++i) { std::string side_color = team::get_side_colour_index(i+1); side_colors.push_back(side_color); std::string flag = teams_[i].flag(); std::string old_rgb = game_config::flag_rgb; std::string new_rgb = side_color; if(flag.empty()) { flag = game_config::flag_image; } LOG_STREAM(info, display) << "Adding flag for team " << i << " from animation " << flag << "\n"; // Must recolor flag image animated<image::locator> temp_anim; std::vector<std::string> items = utils::split(flag); std::vector<std::string>::const_iterator itor = items.begin(); for(; itor != items.end(); ++itor) { const std::vector<std::string>& items = utils::split(*itor, ':'); std::string str; int time; if(items.size() > 1) { str = items.front(); time = atoi(items.back().c_str()); } else { str = *itor; time = 100; } std::stringstream temp; temp << str << "~RC(" << old_rgb << ">"<< new_rgb << ")"; image::locator flag_image(temp.str()); temp_anim.add_frame(time, flag_image); } flags_.push_back(temp_anim); flags_.back().start_animation(rand()%flags_.back().get_end_time(), true); } image::set_team_colors(&side_colors); // clear_screen(); }