Skip to content
This repository has been archived by the owner on Jun 19, 2021. It is now read-only.

Commit

Permalink
Merge pull request #49 from jonas-schievink/master
Browse files Browse the repository at this point in the history
Refactor game::run into a Game struct
  • Loading branch information
LukasKalbertodt authored Jul 22, 2016
2 parents 696330c + 07f3802 commit 424facb
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 27 deletions.
75 changes: 52 additions & 23 deletions client/src/game.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,36 +7,65 @@ use render::Renderer;
use super::Config;
use world::WorldView;
use base::math::*;
use base::gen::WorldGenerator;

/// Main game function: contains the mai render loop and owns all important
/// components. This function should remain rather small, all heavy lifting
/// should be done in other functions.
pub fn run(config: &Config, provider: &ChunkProvider) -> Result<(), ()> {
// Create OpenGL context, the renderer and the event manager
let context = try!(create_context(config));
let renderer = Renderer::new(context.clone());
let event_manager = EventManager::new(context.clone());
let mut world = World::empty();
for i in 0..5 {
for j in 0..5 {
world.add_chunk(ChunkIndex(AxialPoint::new(i, j)), provider).unwrap();
}
}
pub struct Game {
#[allow(dead_code)]
provider: Box<ChunkProvider>, // Needed when we dynamically load chunks
renderer: Renderer,
event_manager: EventManager,
world_view: WorldView,
#[allow(dead_code)]
world: World, // Needed for physics and updated as chunk updates arrive
player: Ghost,
}

let world_view = WorldView::from_world(&world, &context);
impl Game {
pub fn new(config: Config) -> Result<Self, ()> {
let context = try!(create_context(&config));
let mut world = World::empty();
let provider = create_chunk_provider(&config);
pregenerate_world(&mut world, &*provider);

let mut ghost = Ghost::new(context.clone());
Ok(Game {
provider: provider,
renderer: Renderer::new(context.clone()),
event_manager: EventManager::new(context.clone()),
world_view: WorldView::from_world(&world, &context),
world: world,
player: Ghost::new(context.clone()),
})
}

loop {
try!(renderer.render(&world_view, &ghost.get_camera()));
let event_resp = event_manager.poll_events(vec![&mut CloseHandler, &mut ghost]);
if event_resp == EventResponse::Quit {
break;
/// Main game function: contains the main render loop and owns all important
/// components. This function should remain rather small, all heavy lifting
/// should be done in other functions.
pub fn run(mut self) -> Result<(), ()> {
loop {
try!(self.renderer.render(&self.world_view, &self.player.get_camera()));
let event_resp = self.event_manager
.poll_events(vec![&mut CloseHandler, &mut self.player]);
if event_resp == EventResponse::Quit {
break;
}
self.player.update();
}
ghost.update();

Ok(())
}
}

fn create_chunk_provider(config: &Config) -> Box<ChunkProvider> {
Box::new(WorldGenerator::with_seed(config.seed))
}

Ok(())
fn pregenerate_world(world: &mut World, provider: &ChunkProvider) {
// FIXME temporary worldgen invoker, replace with dynamic gen
for i in 0..5 {
for j in 0..5 {
world.add_chunk(ChunkIndex(AxialPoint::new(i, j)), provider).unwrap();
}
}
}

/// Creates the OpenGL context and prints useful information about the
Expand Down
6 changes: 4 additions & 2 deletions client/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,9 @@ mod world;
pub use config::Config;
pub use camera::Camera;

use game::Game;

pub fn start_game(config: Config, world_provider: &base::world::ChunkProvider) -> Result<(), ()> {
game::run(&config, world_provider)
pub fn start_game(config: Config) -> Result<(), ()> {
let game = try!(Game::new(config));
game.run()
}
3 changes: 1 addition & 2 deletions plantex/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,7 @@ fn main() {
}
};

let generator = base::gen::WorldGenerator::with_seed(conf.seed);
let res = client::start_game(conf, &generator);
let res = client::start_game(conf);

// Check if any error occured
if res.is_err() {
Expand Down

0 comments on commit 424facb

Please sign in to comment.