Struct nze_game_sdl::Colour
source · pub struct Colour {
pub r: u8,
pub g: u8,
pub b: u8,
pub a: u8,
}
Expand description
An RGBA colour with values from 0
to 255
for each channel
Fields§
§r: u8
§g: u8
§b: u8
§a: u8
Implementations§
source§impl Colour
impl Colour
sourcepub fn new(r: u8, g: u8, b: u8, a: u8) -> Colour
pub fn new(r: u8, g: u8, b: u8, a: u8) -> Colour
Examples found in repository?
examples/main.rs (line 47)
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119
pub fn main() -> Result<(), Error> {
let (mut cam, drawing_area, context) = DrawingArea::new(
"Game Template", //window name
Rect::new(0.0, 0.0, 240.0, 160.0),
Vec2::new(240.0 * 4.0, 160.0 * 4.0)
)?;
let mut render = Render::new(drawing_area, &context)?;
let mut controls = Controls::new(&context)?;
let mono_font = render.font_manager.load_font(
Path::new("resources/textures/fonts/FiraCode-Light.ttf"))?;
let map = Map::new(
Path::new("resources/map/test.tmx"), &mut render.texture_manager,
Path::new("resources/textures/fonts"), &mut render.font_manager)?;
let mut audio = AudioManager::new()?;
let music = audio.music.load(Path::new("resources/audio/test.wav"))?;
audio.music.play(music, -1)?;
let sfx = audio.sfx.load(Path::new("resources/audio/test.mp3"))?;
audio.sfx.set_volume(sfx, 0.4)?;
//checking resource loading/unloading
let mut is_gaia = true;
let mut ephemeral_obj = GameObject::new_from_tex(
render.texture_manager.load(
Path::new("resources/textures/gaia.png"))?);
let mut text = render
.font_manager
.load_text_obj(
&mono_font,
"The Planet Earth",
Colour::new(100, 200, 70, 255),
Vec2::new(0.0, 0.0), 10.0,
Vec2::new(0.0, 0.0)
)?;
while !controls.should_close {
cam_controls(&mut render, &mut controls, &mut cam)?;
controls.update(&cam);
// load/unload resources
if controls.kb.press(Key::L) ||
controls.c.press(0, controller::Button::A)
{
render.texture_manager.unload_from_gameobject(ephemeral_obj);
render.font_manager.unload_text_obj(text);
if is_gaia {
ephemeral_obj = GameObject::new_from_tex(
render.texture_manager.load(
Path::new("resources/textures/error.png"))?);
text = render.font_manager.load_text_obj(
&mono_font,
"Error Text",
Colour::new(200, 100, 70, 255),
Vec2::new(100.0, 0.0),
10.0,
Vec2::new(0.0, 0.0))?;
text.parallax = Vec2::new(1.0, 1.0);
} else {
ephemeral_obj = GameObject::new_from_tex(
render.texture_manager.load(
Path::new("resources/textures/gaia.png"))?);
text = render.font_manager.load_text_obj(
&mono_font,
"The Planet Earth",
Colour::new(100, 200, 70, 255),
Vec2::new(0.0, 0.0),
10.0,
Vec2::new(0.0, 0.0))?;
}
is_gaia = !is_gaia;
}
ephemeral_obj.rotate += controls.frame_elapsed * 30.0;
if controls.kb.press(Key::F) {
ephemeral_obj.flip_horizontal = !ephemeral_obj.flip_horizontal;
}
if controls.kb.press(Key::P) {
audio.sfx.play(sfx)?;
}
if controls.c.press(0, controller::Button::DPadUp) {
controls.c.rumble(0, 10000, 20000, 1000);
}
render.start_draw();
map.draw(&mut cam);
cam.draw(&ephemeral_obj);
cam.draw_text(&text);
cam.draw_disposable_text(
&mono_font,
format!("Wheel: {}", controls.m.wheel()),
40,
controls.m.pos(),
Colour::white(),
Vec2::new(1.0, 1.0));
render.end_draw(&mut cam)?;
}
Ok(())
}
pub fn new_from_floats(r: f64, g: f64, b: f64, a: f64) -> Colour
sourcepub fn white() -> Colour
pub fn white() -> Colour
Examples found in repository?
examples/main.rs (line 113)
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119
pub fn main() -> Result<(), Error> {
let (mut cam, drawing_area, context) = DrawingArea::new(
"Game Template", //window name
Rect::new(0.0, 0.0, 240.0, 160.0),
Vec2::new(240.0 * 4.0, 160.0 * 4.0)
)?;
let mut render = Render::new(drawing_area, &context)?;
let mut controls = Controls::new(&context)?;
let mono_font = render.font_manager.load_font(
Path::new("resources/textures/fonts/FiraCode-Light.ttf"))?;
let map = Map::new(
Path::new("resources/map/test.tmx"), &mut render.texture_manager,
Path::new("resources/textures/fonts"), &mut render.font_manager)?;
let mut audio = AudioManager::new()?;
let music = audio.music.load(Path::new("resources/audio/test.wav"))?;
audio.music.play(music, -1)?;
let sfx = audio.sfx.load(Path::new("resources/audio/test.mp3"))?;
audio.sfx.set_volume(sfx, 0.4)?;
//checking resource loading/unloading
let mut is_gaia = true;
let mut ephemeral_obj = GameObject::new_from_tex(
render.texture_manager.load(
Path::new("resources/textures/gaia.png"))?);
let mut text = render
.font_manager
.load_text_obj(
&mono_font,
"The Planet Earth",
Colour::new(100, 200, 70, 255),
Vec2::new(0.0, 0.0), 10.0,
Vec2::new(0.0, 0.0)
)?;
while !controls.should_close {
cam_controls(&mut render, &mut controls, &mut cam)?;
controls.update(&cam);
// load/unload resources
if controls.kb.press(Key::L) ||
controls.c.press(0, controller::Button::A)
{
render.texture_manager.unload_from_gameobject(ephemeral_obj);
render.font_manager.unload_text_obj(text);
if is_gaia {
ephemeral_obj = GameObject::new_from_tex(
render.texture_manager.load(
Path::new("resources/textures/error.png"))?);
text = render.font_manager.load_text_obj(
&mono_font,
"Error Text",
Colour::new(200, 100, 70, 255),
Vec2::new(100.0, 0.0),
10.0,
Vec2::new(0.0, 0.0))?;
text.parallax = Vec2::new(1.0, 1.0);
} else {
ephemeral_obj = GameObject::new_from_tex(
render.texture_manager.load(
Path::new("resources/textures/gaia.png"))?);
text = render.font_manager.load_text_obj(
&mono_font,
"The Planet Earth",
Colour::new(100, 200, 70, 255),
Vec2::new(0.0, 0.0),
10.0,
Vec2::new(0.0, 0.0))?;
}
is_gaia = !is_gaia;
}
ephemeral_obj.rotate += controls.frame_elapsed * 30.0;
if controls.kb.press(Key::F) {
ephemeral_obj.flip_horizontal = !ephemeral_obj.flip_horizontal;
}
if controls.kb.press(Key::P) {
audio.sfx.play(sfx)?;
}
if controls.c.press(0, controller::Button::DPadUp) {
controls.c.rumble(0, 10000, 20000, 1000);
}
render.start_draw();
map.draw(&mut cam);
cam.draw(&ephemeral_obj);
cam.draw_text(&text);
cam.draw_disposable_text(
&mono_font,
format!("Wheel: {}", controls.m.wheel()),
40,
controls.m.pos(),
Colour::white(),
Vec2::new(1.0, 1.0));
render.end_draw(&mut cam)?;
}
Ok(())
}