feat: added simple_text example

This commit is contained in:
lisk77 2025-05-03 21:56:56 +02:00
parent d4a1bde4dc
commit 05249f2a6d
6 changed files with 85 additions and 296 deletions

View file

@ -11,4 +11,5 @@ cargo run --example <example_name>
|------------------------------------|----------------------------------------------------------------------------------------|
| [hello_world](hello_world) | A simple boilerplate example to show how to properly start creating a Comet App. |
| [textured_entity](textured_entity) | This covers the basics on how to create a camera and your first entity with a texture. |
| [simple_move_2d](simple_move_2d) | Simple demonstration of a hypothetical movement system in 2D. |
| [simple_move_2d](simple_move_2d) | A simple demonstration of a hypothetical movement system in 2D. |
| [simple_text](simple_text) | A simple demonstration of how to write some text in Comet. |

42
examples/simple_text.rs Normal file
View file

@ -0,0 +1,42 @@
use comet::prelude::*;
fn setup(app: &mut App, renderer: &mut Renderer2D) {
// Loading the font from the resources/fonts directory with a rendered size of 77px
renderer.load_font("./resources/fonts/PressStart2P-Regular.ttf", 77.0);
// Setting up camera
let camera = app.new_entity();
app.add_component(camera, Transform2D::new());
app.add_component(camera, Camera2D::new(v2::new(2.0, 2.0), 1.0, 1));
// Creating the text entity
let text = app.new_entity();
app.add_component(text, Transform2D::new());
app.add_component(text, Text::new(
"comet", // The content of the text
"./resources/fonts/PressStart2P-Regular.ttf", // The used font (right now exact to the font path)
77.0, // Pixel size at which the font will be drawn
true, // Should the text be visible
sRgba::<f32>::from_hex("#abb2bfff") // Color of the text
));
}
fn update(app: &mut App, renderer: &mut Renderer2D, dt: f32) {
// Getting the windows size
let size = renderer.size();
// Recalculating the position of the text every frame to ensure the same relative position
let transform = app.get_component_mut::<Transform2D>(1).unwrap();
transform.position_mut().set_x(-((size.width-50) as f32));
transform.position_mut().set_y((size.height-100) as f32);
renderer.render_scene_2d(app.scene());
}
fn main() {
App::new()
.with_preset(App2D)
.with_title("Simple Text")
.run::<Renderer2D>(setup, update);
}