Font sizes and map view now work with arbitrary map sizes.
This commit is contained in:
parent
2dccc6b17b
commit
824a384ffd
4 changed files with 30 additions and 20 deletions
13
gui.cpp
13
gui.cpp
|
@ -45,20 +45,11 @@ GUI::GUI(DinkyECS::World &world, Map& game_map) :
|
|||
$sounds.load("combat_player_hit", "combat_player_hit.mp3");
|
||||
$sounds.load("combat_enemy_hit", "combat_enemy_hit.mp3");
|
||||
$sounds.load("combat_miss", "combat_miss.mp3");
|
||||
resize_map(BASE_MAP_FONT_SIZE);
|
||||
resize_map(MAX_FONT_SIZE);
|
||||
}
|
||||
|
||||
void GUI::resize_map(int new_size) {
|
||||
if($renderer.resize_map(new_size)) {
|
||||
auto bounds = $renderer.$base_glyph.bounds;
|
||||
int view_x = std::ceil((VIDEO_X - GAME_MAP_POS) / bounds.width);
|
||||
int view_y = std::ceil(VIDEO_Y / bounds.height);
|
||||
|
||||
// don't allow resizing beyond/below game map size
|
||||
if(view_x > GAME_MAP_X || view_y > GAME_MAP_Y) return;
|
||||
|
||||
$view_port = {size_t(view_x), size_t(view_y)};
|
||||
|
||||
if($renderer.resize_map(new_size, $view_port)) {
|
||||
// set canvas to best size
|
||||
$canvas = Canvas($view_port.x * 2, $view_port.y * 4);
|
||||
$map_screen = Screen($view_port.x, $view_port.y);
|
||||
|
|
31
render.cpp
31
render.cpp
|
@ -3,6 +3,9 @@
|
|||
#include <cmath>
|
||||
#include <fmt/core.h>
|
||||
#include <array>
|
||||
#include "map.hpp"
|
||||
|
||||
using namespace fmt;
|
||||
|
||||
std::array<sf::Color, 10> VALUES{
|
||||
sf::Color{1, 4, 2}, // black
|
||||
|
@ -27,7 +30,7 @@ sf::Color SFMLRender::color(Value val) {
|
|||
|
||||
SFMLRender::SFMLRender(Canvas &canvas, Screen &map_screen, Screen &screen) :
|
||||
$window(sf::VideoMode(VIDEO_X,VIDEO_Y), "Roguish"),
|
||||
$map_font_size(BASE_MAP_FONT_SIZE),
|
||||
$map_font_size(0),
|
||||
$line_spacing(0),
|
||||
$canvas(canvas),
|
||||
$map_screen(map_screen),
|
||||
|
@ -59,12 +62,30 @@ sf::Sprite &SFMLRender::get_text_sprite(wchar_t tile) {
|
|||
return $sprites[tile];
|
||||
}
|
||||
|
||||
inline bool base_glyph_check(sf::Font &font, sf::Glyph &base_glyph, Point &view_port, int &font_size, int new_size) {
|
||||
auto glyph = font.getGlyph(BG_TILE, new_size, false);
|
||||
|
||||
bool SFMLRender::resize_map(int new_size) {
|
||||
if(MIN_FONT_SIZE < new_size && new_size < MAX_FONT_SIZE) {
|
||||
int view_x = std::ceil((VIDEO_X - GAME_MAP_POS) / glyph.bounds.width);
|
||||
int view_y = std::ceil(VIDEO_Y / glyph.bounds.height);
|
||||
|
||||
// don't allow resizing beyond/below game map size
|
||||
if(view_x <= GAME_MAP_X && view_y <= GAME_MAP_Y) {
|
||||
// looks good, set 'em all
|
||||
base_glyph = glyph;
|
||||
view_port = {size_t(view_x), size_t(view_y)};
|
||||
font_size = new_size;
|
||||
return true;
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
bool SFMLRender::resize_map(int new_size, Point &view_port) {
|
||||
if($map_font_size == new_size || new_size < MIN_FONT_SIZE || new_size > MAX_FONT_SIZE) return false;
|
||||
|
||||
if(base_glyph_check($font, $base_glyph, view_port, $map_font_size, new_size)) {
|
||||
$sprites.clear(); // need to reset the sprites for the new size
|
||||
$map_font_size = new_size;
|
||||
$base_glyph = $font.getGlyph(BG_TILE, $map_font_size, false);
|
||||
$line_spacing = $font.getLineSpacing($map_font_size);
|
||||
$bg_sprite = get_text_sprite(BG_TILE);
|
||||
$bg_bounds = $bg_sprite.getLocalBounds();
|
||||
|
|
|
@ -55,7 +55,7 @@ struct SFMLRender {
|
|||
sf::Color color(int val);
|
||||
sf::Color color(Value val);
|
||||
sf::Sprite &get_text_sprite(wchar_t tile);
|
||||
bool resize_map(int new_size);
|
||||
bool resize_map(int new_size, Point &view_port);
|
||||
void render_text(std::string &text, float x, float y);
|
||||
void draw_main_ui();
|
||||
void draw_screen(bool clear=true, float map_off_x=0.0f, float map_off_y=0.0f);
|
||||
|
|
|
@ -15,6 +15,4 @@ TODO:
|
|||
* Write a test that generates a ton of maps then confirms there's a path from one room to every other room?
|
||||
* Lua integration?
|
||||
|
||||
* BUG: If map is < 90 zome out crashes.
|
||||
|
||||
* Bring back sounds, check out SoLoud.
|
||||
* check out SoLoud.
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue