parent
009b1e63a7
commit
9397af2a11
@ -0,0 +1,144 @@ |
||||
#include "render.hpp" |
||||
#include <cmath> |
||||
|
||||
std::array<sf::Color, 10> VALUES{ |
||||
sf::Color{1, 4, 2}, // black
|
||||
sf::Color{9, 29, 16}, // dark dark
|
||||
sf::Color{14, 50, 26}, // dark mid
|
||||
sf::Color{0, 109, 44}, // dark light
|
||||
sf::Color{63, 171, 92}, // mid
|
||||
sf::Color{161, 217, 155}, // light dark
|
||||
sf::Color{199, 233, 192}, // light mid
|
||||
sf::Color{229, 245, 224}, // light light
|
||||
sf::Color{255, 255, 255}, // white
|
||||
sf::Color::Transparent, // white
|
||||
}; |
||||
|
||||
sf::Color SFMLRender::color(int val) { |
||||
return VALUES[size_t(val)]; |
||||
} |
||||
|
||||
sf::Color SFMLRender::color(Value val) { |
||||
return VALUES[size_t(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), |
||||
$line_spacing(0), |
||||
$canvas(canvas), |
||||
$map_screen(map_screen), |
||||
$screen(screen) |
||||
{ |
||||
$font.loadFromFile("./assets/text.otf"); |
||||
$ui_text.setFont($font); |
||||
$ui_text.setPosition(0,0); |
||||
$ui_text.setCharacterSize(UI_FONT_SIZE); |
||||
$ui_text.setFillColor(color(Value::LIGHT_LIGHT)); |
||||
} |
||||
|
||||
sf::Sprite &SFMLRender::get_text_sprite(wchar_t tile) { |
||||
if(!$sprites.contains(tile)) { |
||||
sf::Glyph glyph = $font.getGlyph(tile, $map_font_size, false); |
||||
// WARNING! we actually have to do this here because SFML caches
|
||||
// the glyphs on the font texture, so this gets loaded each time
|
||||
// we get a new glyph from the font.
|
||||
$font_texture = $font.getTexture($map_font_size); |
||||
sf::Sprite sprite($font_texture); |
||||
sprite.setTextureRect(glyph.textureRect); |
||||
$sprites[tile] = sprite; |
||||
} |
||||
|
||||
return $sprites[tile]; |
||||
} |
||||
|
||||
|
||||
bool SFMLRender::resize_map(int new_size) { |
||||
if(MIN_FONT_SIZE < new_size && new_size < MAX_FONT_SIZE) { |
||||
$sprites.clear(); // need to reset the sprites for the new size
|
||||
$map_font_size = new_size; |
||||
$base_glyph = $font.getGlyph(L'█', $map_font_size, false); |
||||
$line_spacing = $font.getLineSpacing($map_font_size); |
||||
return true; |
||||
} else { |
||||
// something else here
|
||||
return false; |
||||
} |
||||
} |
||||
|
||||
|
||||
void SFMLRender::draw_screen(bool clear, float map_off_x, float map_off_y) { |
||||
if(clear) $window.clear(); |
||||
std::string screenout = $screen.ToString(); |
||||
std::wstring main_screen_utf8 = $converter.from_bytes(screenout); |
||||
$ui_text.setString(main_screen_utf8); |
||||
$window.draw($ui_text); |
||||
|
||||
std::string map_screenout = $map_screen.ToString(); |
||||
std::wstring map_screen_utf8 = $converter.from_bytes(map_screenout); |
||||
|
||||
float y = 0.0f; |
||||
float x = GAME_MAP_POS; |
||||
// make a copy so we don't modify the cached one
|
||||
auto bg_sprite = get_text_sprite(L'█'); |
||||
auto bg_bounds = bg_sprite.getLocalBounds(); |
||||
bg_sprite.setColor(sf::Color(20,20,20)); |
||||
auto add_sprite = get_text_sprite(L'!'); |
||||
bool has_add = false; |
||||
|
||||
for(size_t i = 0; i < map_screen_utf8.size(); i++) { |
||||
wchar_t tile = map_screen_utf8[i]; |
||||
|
||||
if(tile == L'\n') { |
||||
// don't bother processing newlines, just skip
|
||||
y += $line_spacing; |
||||
x = GAME_MAP_POS; |
||||
} else if(tile == L'\r') { |
||||
continue; // skip these, just windows junk
|
||||
} else { |
||||
// it's a visual cell
|
||||
bg_sprite.setPosition({x+map_off_x, y+map_off_y}); |
||||
sf::Sprite &sprite = get_text_sprite(tile); |
||||
|
||||
// should look into caching all this instead of calcing it each time
|
||||
auto sp_bounds = sprite.getLocalBounds(); |
||||
|
||||
// calculate where to center the sprite, but only if it's smaller
|
||||
auto width_delta = bg_bounds.width > sp_bounds.width ? (bg_bounds.width - sp_bounds.width) / 2 : 0; |
||||
auto height_delta = bg_bounds.height > sp_bounds.width ? (bg_bounds.height - sp_bounds.height) / 2 : 0; |
||||
|
||||
// TODO: need to center it inside the bg_sprite
|
||||
sprite.setPosition({x+width_delta+map_off_x, y+height_delta+map_off_y}); |
||||
|
||||
// get the entity combat and make them light gray if dead
|
||||
if(tile == L'█') { |
||||
sprite.setColor(sf::Color(80,80,80)); |
||||
} else if(tile == L'☺') { |
||||
sprite.setColor(sf::Color::Blue); |
||||
} else if(tile == L'Ω') { |
||||
sprite.setColor(sf::Color::Red); |
||||
// HACK: just playing with adding multiple characters for drawing
|
||||
add_sprite.setColor(sf::Color::Red); |
||||
add_sprite.setPosition({x-3,y-3}); |
||||
has_add = true; |
||||
} else if(tile == L'#') { |
||||
sprite.setColor(sf::Color(5,5,5)); |
||||
} else { |
||||
sprite.setColor(color(Value::MID)); |
||||
} |
||||
|
||||
// now draw the background sprite and sprite
|
||||
// TODO: this can become a standard sprite description
|
||||
$window.draw(bg_sprite); |
||||
$window.draw(sprite); |
||||
if(has_add) { |
||||
$window.draw(add_sprite); |
||||
has_add = false; |
||||
} |
||||
// next cell
|
||||
x += $base_glyph.advance; |
||||
} |
||||
} |
||||
|
||||
$window.display(); |
||||
} |
@ -0,0 +1,53 @@ |
||||
#pragma once |
||||
|
||||
#include <ftxui/screen/screen.hpp> |
||||
#include <ftxui/dom/canvas.hpp> |
||||
#include <SFML/Window.hpp> |
||||
#include <SFML/System.hpp> |
||||
#include <SFML/Graphics.hpp> |
||||
#include "point.hpp" |
||||
#include <codecvt> |
||||
|
||||
using ftxui::Canvas, ftxui::Screen; |
||||
|
||||
constexpr int VIDEO_X = 1600; |
||||
constexpr int VIDEO_Y = 900; |
||||
constexpr int MIN_FONT_SIZE = 20; |
||||
constexpr int MAX_FONT_SIZE = 140; |
||||
constexpr int GAME_MAP_X = 90; |
||||
constexpr int GAME_MAP_Y = 90; |
||||
constexpr int GAME_MAP_POS = 600; |
||||
constexpr int UI_FONT_SIZE=30; |
||||
constexpr int BASE_MAP_FONT_SIZE=90; |
||||
|
||||
enum class Value { |
||||
BLACK=0, DARK_DARK, DARK_MID, |
||||
DARK_LIGHT, MID, LIGHT_DARK, LIGHT_MID, |
||||
LIGHT_LIGHT, WHITE, TRANSPARENT |
||||
}; |
||||
|
||||
struct SFMLRender { |
||||
sf::RenderWindow $window; |
||||
int $map_font_size; |
||||
float $line_spacing; |
||||
std::unordered_map<wchar_t, sf::Sprite> $sprites; |
||||
sf::Font $font; |
||||
sf::Texture $font_texture; |
||||
sf::Glyph $base_glyph; |
||||
Canvas& $canvas; |
||||
Screen& $map_screen; |
||||
Screen& $screen; |
||||
sf::Text $ui_text; |
||||
std::wstring_convert<std::codecvt_utf8_utf16<wchar_t>> $converter; |
||||
|
||||
SFMLRender(Canvas &canvas, Screen &map_screen, Screen &screen); |
||||
|
||||
// disable copy
|
||||
SFMLRender(SFMLRender &other) = delete; |
||||
|
||||
sf::Color color(int val); |
||||
sf::Color color(Value val); |
||||
sf::Sprite &get_text_sprite(wchar_t tile); |
||||
bool resize_map(int new_size); |
||||
void draw_screen(bool clear=true, float map_off_x=0.0f, float map_off_y=0.0f); |
||||
}; |
Loading…
Reference in new issue