Working on editor

This commit is contained in:
2019-06-22 18:12:02 +02:00
parent cdd7a35a04
commit 1ae9635fe8
12 changed files with 338 additions and 105 deletions
+73
View File
@@ -0,0 +1,73 @@
use sfml::graphics::Font as SfFont;
macro_rules! make_fonts {
( $(
$enum_name: ident,
$font_name: ident,
$function_name: ident,
$font_path: tt,)
*) => {
/// Describes all the fonts that will be used in this game.
#[derive(Copy, Clone)]
pub enum Font {
$(
/// A font.
$enum_name,
)*
}
/// A struct that holds all the fonts.
pub struct FontManager {
$(
/// A font.
$font_name: SfFont,
)*
}
impl FontManager {
/// Creates a new font manager and initializes all fonts.
pub fn new() -> FontManager {
FontManager {
$(
$font_name: FontManager::$function_name(),
)*
}
}
$(
/// Creates the font.
fn $function_name() -> SfFont {
SfFont::from_file("assets/fonts/sansation.ttf").unwrap()
// let bytes = include_bytes!($font_path).to_vec();
// FontManager::make_font_from_bytes(bytes)
}
)*
/// Returns the real font from the font id.
pub fn get(&self, id: Font) -> &SfFont {
match id {
$(
Font::$enum_name => &self.$font_name,
)*
}
}
}
}
}
make_fonts!(
Sansation,
sansation,
make_sansation_font,
"../../../assets/fonts/sansation.ttf",
);
impl FontManager {
/// Creates a fonts from an array of bytes.
fn _make_font_from_bytes(bytes: Vec<u8>) -> SfFont {
SfFont::from_memory(&bytes)
.expect("Failed to create font")
}
}
+3
View File
@@ -10,6 +10,9 @@ pub mod controls;
/// This module helps us dealing with textures.
pub mod texture;
/// This module helps us dealing with fonts.
pub mod font;
/// This module contains the math tools that will be used.
pub mod math;
+13 -1
View File
@@ -1,6 +1,6 @@
use std::time::Instant;
use sfml::graphics::{Color, IntRect, RenderTarget, RenderWindow, Sprite, View};
use sfml::graphics::{Color, IntRect, RenderTarget, RenderWindow, Sprite, View, Text};
use sfml::window::{Event, Style};
@@ -8,6 +8,7 @@ use sfml::system::Vector2;
use crate::engine::scene::Scene;
use crate::engine::texture::{Texture, TextureManager};
use crate::engine::font::{Font, FontManager};
/// Our custom drawable trait.
pub trait Drawable {
@@ -29,6 +30,9 @@ pub struct Renderer {
/// The texture manager needed by the renderer.
texture_manager: TextureManager,
/// The font manager needed by the renderer.
font_manager: FontManager,
/// The global timer of the renderer.
started: Instant,
}
@@ -52,6 +56,7 @@ impl Renderer {
Renderer {
window,
texture_manager: TextureManager::new(),
font_manager: FontManager::new(),
started: Instant::now(),
}
}
@@ -132,4 +137,11 @@ impl Renderer {
pub fn window_mut(&mut self) -> &mut RenderWindow {
&mut self.window
}
/// Draws text on the screen.
pub fn draw_text(&mut self, text: &str, font: Font, size: u32, color: &Color) {
let mut text = Text::new(text, self.font_manager.get(font), size);
text.set_fill_color(color);
self.window.draw(&text);
}
}