Expand description
core
declares the relationship between any Canvas
(an object that can be drawn to) and the library’s primitives and anything else that can be drawn to the screen.
§Quick Start
Let’s get started with a simple program to demonstrate how Gemini works:
//! The quick start example
use gemini_engine::{
core::{ColChar, Vec2D},
gameloop,
primitives::Pixel,
view::{View, WrappingMode},
};
const FPS: f32 = 30.0;
fn main() {
let mut view = View::new(40, 8, ColChar::BACKGROUND).with_wrapping_mode(WrappingMode::Wrap);
let mut pixel = Pixel::new(Vec2D::new(10, 5), ColChar::SOLID);
loop {
view.clear();
pixel.pos.x += 1;
view.draw(&pixel);
let _ = view.display_render();
let _ = gameloop::sleep_fps(FPS, None);
}
}
Ok, let’s go over this and see what’s going on. We start by creating a View
and Pixel
. View::new
takes a width and height parameter, as well as a ColChar
which is used as the default character when the screen is cleared. We also set the WrappingMode
to Wrap
, which means that any pixel drawn outside the screen will be wrapped back around to the opposite edge.
We use ColChar
to define the text character and colour used to represent each pixel. Here we used the ColChar::BACKGROUND
and ColChar::SOLID
constants, which appear as ░
and █
respectively.
Vec2D
is an alias to glam::I64Vec2
. We used it here to define the Pixel
’s starting position, before the game loop.
Now that we’ve got initialisation out of the way, let’s get on to the main loop. In Gemini the main loop generally goes as follows:
- Logical processing (physics, input, etc.)
- Clear the
View
- Draw all the
CanDraw
elements to theView
- Call
View.display_render
- Wait until the next frame
In our case, we want to move our Pixel
one unit to the right every frame, so we do so with pixel.pos.x += 1;
. Next we draw the Pixel
to the View
and call display_render()
, which prints the render to stdout
(make sure your terminal is large enough to fit the whole image!). The last line of our code sleeps for 1/FPS
seconds. We pass None
to the elapsed parameter here, but we could instead pass the amount of time taken for our gameloop to run (as a Duration
), which would be subtracted from the total sleep time.
There you have it! You’ve written your first program with Gemini! This is still a work in progress, so any feedback or issue requests would be appreciated :)
Structs§
- ColChar
- A coloured character. Made up of
text_char
, a single ascii character used as the “pixel” when drawn to aCanvas
, andmodifier
, which gives that pixel a colour or makes it bold/italic - Colour
- A struct to store colour values. Can be created from RGB, HSV or greyscale values, but is ultimately stored as RGB.
Enums§
- Modifier
- The
Modifier
enum is used for adding modifications to text such as colour, bold/italic/underline and others.Modifier
should be used throughColChar
.
Traits§
- CanDraw
- A struct that can draw to a
Canvas
- Canvas
- A struct that can be drawn to by elements which implement
CanDraw