Created
May 29, 2018 10:05
-
-
Save fu5ha/7d3f98acc346af2f9b60e9c2afc71e37 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| extern crate ggez; | |
| use ggez::*; | |
| use ggez::graphics::{DrawMode, Point}; | |
| use std::time::Duration; | |
| struct MainState { | |
| imagebuffer: Vec<u8>, // represents an image, groupings of 4 values (R,G,B,A) representing one pixel. | |
| width: usize, | |
| height: usize, | |
| i: u8, | |
| } | |
| impl MainState { | |
| fn new(_ctx: &mut Context) -> GameResult<MainState> { | |
| // an 800x600 image, times 4 for each component | |
| let mut v = Vec::with_capacity(800*600*4); | |
| v.resize(800*600*4, 0); | |
| let state = MainState { | |
| imagebuffer: v, | |
| width: 800, | |
| height: 600, | |
| i: 0, | |
| }; | |
| Ok(state) | |
| } | |
| } | |
| impl event::EventHandler for MainState { | |
| fn update(&mut self, _ctx: &mut Context, _dt: Duration) -> GameResult<()> { | |
| for i in 0..(self.framebuffer.len() / 4) { | |
| // white or pink checkerboard, obviously you can do whatever here | |
| if i % 2 == 0 { | |
| self.framebuffer[i * 4] = 255; | |
| self.framebuffer[i * 4 + 1] = 255; | |
| self.framebuffer[i * 4 + 2] = 255; | |
| self.framebuffer[i * 4 + 3] = 255; | |
| } else { | |
| self.framebuffer[i * 4] = 255; | |
| self.framebuffer[i * 4 + 1] = 135; | |
| self.framebuffer[i * 4 + 2] = 220; | |
| self.framebuffer[i * 4 + 3] = 255; | |
| } | |
| } | |
| Ok(()) | |
| } | |
| fn draw(&mut self, ctx: &mut Context) -> GameResult<()> { | |
| graphics::clear(ctx); | |
| // creates an image from the raw buffer | |
| let img = graphics::Image::from_rgba8(ctx, self.width as u16, self.height as u16, &self.framebuffer)?; | |
| // draws the image with a center at 400, 300 | |
| graphics::draw(ctx, &img, Point::new(400.0, 300.0), 0.0)?; | |
| // actually presents draws to the screen | |
| graphics::present(ctx); | |
| Ok(()) | |
| } | |
| } | |
| pub fn main() { | |
| let c = conf::Conf::new(); | |
| let ctx = &mut Context::load_from_conf("super_simple", "ggez", c).unwrap(); | |
| let state = &mut MainState::new(ctx).unwrap(); | |
| event::run(ctx, state).unwrap(); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment