Cargo.tomlの[dependencies]に
[dependencies] ggez = "0.6.0-rc1"
と入力。 公式のリポジトリのREADMEには"0.6"と書かれているが、これだと
stdout : Updating crates.io index error: failed to select a version for the requirement `ggez = "^0.6"` candidate versions found which didn't match: 0.5.1, 0.5.0, 0.4.4, ... location searched: crates.io index required by package `ggez_practice1 v0.1.0 (/Users/Yuki/GgezSandBox/ggez_practice1)` stderr :
のようなエラーが出てしまうので注意。
ggezは、3つの主要部分で構成されています。 - コンピューターのハードウェアとのインターフェイスに必要なすべての状態を含むContextオブジェクト - イベントのコールバックを登録するためにユーザーが実装するEventHandlerトレイト - 機能を提供するグラフィックやオーディオなどのサブモジュール
一般的なパターンは、EventHandlerトレイトを実装するゲームのデータを保持する構造体を作成することです。 ContextBuilderまたはConfオブジェクトのデフォルトオブジェクトを使用して新しいContextオブジェクトを作成し、ContextとEventHandlerのインスタンスを使用してevent :: run()を呼び出して、ゲームのメインループを実行します。
テンプレート
use ggez::{Context, ContextBuilder, GameResult}; use ggez::graphics::{self, Color}; use ggez::event::{self, EventHandler}; fn main() { // Make a Context. let (mut ctx, event_loop) = ContextBuilder::new("my_game", "Yuki Sakai") .build() .expect("could not create ggez context!"); // Create an instance of your event handler. // Usually, you should provide it with the Context object to // use when setting your game up. let my_game = MyGame::new(&mut ctx); // Run! event::run(ctx, event_loop, my_game); } struct MyGame { // Your state here... } impl MyGame { pub fn new(_ctx: &mut Context) -> MyGame { // Load/create resources such as images here. MyGame { // ... } } } impl EventHandler for MyGame { fn update(&mut self, _ctx: &mut Context) -> GameResult<()> { // Update code here... Ok(()) } fn draw(&mut self, ctx: &mut Context) -> GameResult<()> { graphics::clear(ctx, Color::WHITE); // Draw code here... graphics::present(ctx) } }