Browse Source

Initial commit

master
Fabien Givors 2 years ago
commit
8fba3abb56
  1. 7
      Cargo.toml
  2. 107
      src/lib.rs
  3. 39
      src/main.rs

7
Cargo.toml

@ -0,0 +1,7 @@
[package]
name = "minigame"
version = "0.1.0"
authors = ["Fabien Givors <fabien.givors@chezlefab.net>"]
[dependencies]
jclvg = { path = "../jclvg/" }

107
src/lib.rs

@ -0,0 +1,107 @@
use std::time;
use std::path::Path;
extern crate jclvg;
use jclvg::interface::{Event,Keycode};
use jclvg::renderer::{Point,Rect,Color,Renderer};
use jclvg::widgets::{Widget,Sprite,Label,TextureWidget};
use jclvg::game::{GameState,GameInitialState};
pub struct Data
{
pub width: u32,
pub height: u32,
pub pos: Point,
}
pub struct MyGameStateInit
{
data: Data,
finished: bool,
widgets: Vec<Box<dyn Widget>>,
}
impl GameInitialState<Data> for MyGameStateInit
{
fn new(data: Data)->Box<dyn GameState>
{
let finished = false;
let size = Rect::new(0,0,1024,768);
let widgets = vec![];
let mut gs_i = MyGameStateInit { data, finished, widgets };
let background = Sprite::new(Path::new("assets/intro.jpg")).unwrap();
gs_i.widgets.push(Box::new(background));
let mut weapons = Sprite::new(Path::new("assets/weapons.png")).unwrap();
weapons.set_dst(Some(Rect::new(0,100,size.w as u32, 50)));
gs_i.widgets.push(Box::new(weapons));
let mut tank1 = TextureWidget::new();
tank1.set_src(Some(Rect::new(0, 0, 50, 50)));
tank1.set_dst(Some(Rect::new(0, 150, size.w as u32, (size.h - 100) as u32)));
tank1.set_update(
Box::new(|pixel_data, pitch|
{
for pixel in pixel_data.iter_mut()
{ *pixel = 255; }
})
);
gs_i.widgets.push(Box::new(tank1));
let title_dst = Point::new(50, 50);
let title = Label::new("Label Widget", title_dst, Color::RGB(0,0, 0));
gs_i.widgets.push(Box::new(title));
Box::new(gs_i)
}
}
impl GameState for MyGameStateInit
{
fn draw(&mut self, renderer: &mut Renderer)
{
renderer.clear();
for w in self.widgets.iter_mut()
{
w.draw(renderer);
}
}
fn process(&mut self, event: &Event)
{
match event {
Event::KeyDown
{
keycode: Some(Keycode::Escape), ..
} => { self.finished = true; },
Event::KeyDown
{
keycode: Some(Keycode::Up), ..
} => { if self.data.pos.y > 10 { self.data.pos.y-= 10; }},
Event::KeyDown
{
keycode: Some(Keycode::Down), ..
} => { if self.data.pos.y < self.data.height as i32 { self.data.pos.y+= 10; }},
Event::KeyDown
{
keycode: Some(Keycode::Left), ..
} => { if self.data.pos.x > 10 { self.data.pos.x-= 10; }},
Event::KeyDown
{
keycode: Some(Keycode::Right), ..
} => { if self.data.pos.x < self.data.width as i32 { self.data.pos.x+= 10; }},
_ => (),
}
}
fn update(&mut self, _delta: time::Duration)->Option<Box<dyn GameState>>
{
None
}
fn is_final(&self)->bool
{
return self.finished;
}
}

39
src/main.rs

@ -0,0 +1,39 @@
extern crate jclvg;
extern crate minigame;
use std::env;
use std::process;
use jclvg::renderer::{Point};
use jclvg::game::Game;
use jclvg::Config;
use minigame::{Data,MyGameStateInit};
fn parse_command_line(_args: &[String]) -> Result<Config, &'static str>
{
if true
{
Ok(Config::new(800, 600, "Mini Game"))
}
else
{
Err("Invalid arguments")
}
}
fn main()
{
let args:Vec<String> = env::args().collect();
let config = parse_command_line(&args).unwrap_or_else(
|err|
{
println!("Error processing arguments: {}", err);
process::exit(1);
}
);
let data = Data { width: 400, height: 400, pos: Point::new(1,1) };
let mut game = Game::new::<MyGameStateInit>(&config,data);
game.run();
}
Loading…
Cancel
Save