Free games for graphing calculators: NumWorks, TI-Nspire CX, Casio fx-CG50 and soon TI-84 Plus CE. Balatro, Celeste, Hollow Knight, Minecraft and more.
C calculatorcalculator-gamescasiochesscrossy-roaddrive-madfx-cg50gamesgeometry-dashgraphing-calculatorndlessnumworksnumworks-appsnumworks-calculatornumworks-gamenumworks-gamestetristi-84-plus-ceti-nspire
/// Here are some useful functions that should not have to be remade every time.
/// There are also some useful constants, that some would argue should be in the graphical file.
use heapless::String;
use crate::eadk::display::{self, SCREEN_HEIGHT, SCREEN_WIDTH};
use crate::eadk::{keyboard, random, Point};
// Those constants have been found by hand and seem to not be perfect.
/// Width in pixels of a large character
pub const LARGE_CHAR_WIDTH: u16 = 10;
/// Width in pixels of a small character
pub const SMALL_CHAR_WIDTH: u16 = 7;
/// Height in pixels of a large character
pub const LARGE_CHAR_HEIGHT: u16 = 18;
/// Height in pixels of a small character
pub const SMALL_CHAR_HEIGHT: u16 = 14;
/// This center is the upper left corner of the 4 pixels that make the center
pub const CENTER: Point = Point::new(SCREEN_WIDTH / 2, SCREEN_HEIGHT / 2);
/// Returns the left x coordinate that centers the given string
pub fn get_centered_text_x_coordo(string: &str, large: bool) -> u16 {
match (display::SCREEN_WIDTH).checked_sub(get_string_pixel_size(string, large)) {
x @ Some(1_u16..=u16::MAX) => x.unwrap() / 2,
None | Some(0) => 0,
}
}
/// The cells a text takes in the calculator's fixed-width font. In a language build (see build.rs),
/// an accented letter takes one and a Chinese one two (NumPlay draws those in two cells).
pub fn text_cells(string: &str) -> u16 {
if cfg!(np_text_extra) {
string
.chars()
.filter(|c| *c >= ' ')
.map(|c| if c as u32 >= 0x2E80 { 2 } else { 1 })
.sum()
} else {
string
.chars()
.filter(|p| p.is_ascii_alphanumeric() || *p == ' ' || p.is_ascii_punctuation())
.count() as u16
}
}
/// Returns the size IN PIXELS, on the screen, of the given string
pub fn get_string_pixel_size(string: &str, large: bool) -> u16 {
text_cells(string)
* (if large {
LARGE_CHAR_WIDTH
} else {
SMALL_CHAR_WIDTH
})
}
#[inline]
/// Gives a [random] integer between a and b (excluded)
pub fn randint(a: u32, b: u32) -> u32 {
if a == b {
0
} else {
random() % (b - a) + a
}
}
/// Blocks the program until *nothing* is pressed.
pub fn wait_for_no_keydown() {
loop {
let keyboard_state = keyboard::scan();
if !keyboard_state.any_down() {
break;
}
}
}
#[inline]
/// Takes a decimal (from [include_bytes!]) and returns its value as a ascii number.
/// Used to read images parameters.
pub fn decimal_to_ascii_number(number: u8) -> Option<u8> {
if !((48..=57).contains(&number)) {
// not (48 <= number <= 57) == not an ascii number
None
} else {
Some(number - 48)
}
}
// Reads a text and returns the u32 associated with it. WARNING : does not care about overflow.
// If the first character isn't a number, returns None. Else, stops at the first non-number character.
pub fn text_to_u32(text: &str) -> Option<u32> {
let mut chars = text.chars().skip_while(|p| p.is_whitespace());
let c = chars.next()?;
let mut res = decimal_to_ascii_number(c as u8)? as u32;
let mut c2 = chars.next();
if c2.is_none() {
return Some(res);
}
while let Some(x) = decimal_to_ascii_number(c2.unwrap() as u8) {
res = res * 10 + x as u32;
c2 = chars.next();
if c2.is_none() {
return Some(res);
}
}
Some(res)
}
/// Returns the string corresponding to the decimal number given.
pub fn string_from_u16(number: u16) -> String<6> {
let mut text: String<6> = String::new();
if number == 0 {
text.push('0').unwrap();
text.truncate(1);
text.push('\0').unwrap();
return text;
}
let mut n = number;
let mut cpt = 0;
while n != 0 {
text.push(((n % 10) as u8 + b'0') as char).unwrap();
n /= 10;
cpt += 1;
}
text.truncate(cpt);
let mut second_text: String<6> = String::new();
for i in (0..cpt).rev() {
second_text.push(text.chars().nth(i).unwrap()).unwrap();
}
second_text.truncate(cpt);
second_text.push('\0').unwrap();
second_text
}
/// Returns the string corresponding to the decimal number given.
pub fn string_from_u32(number: u32) -> String<11> {
let mut text: String<11> = String::new();
if number == 0 {
text.push('0').unwrap();
text.truncate(1);
text.push('\0').unwrap();
return text;
}
let mut n = number;
let mut cpt = 0;
while n != 0 {
text.push(((n % 10) as u8 + b'0') as char).unwrap();
n /= 10;
cpt += 1;
}
text.truncate(cpt);
let mut second_text: String<11> = String::new();
for i in (0..cpt).rev() {
second_text.push(text.chars().nth(i).unwrap()).unwrap();
}
second_text.truncate(cpt);
second_text.push('\0').unwrap();
second_text
}