4
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

大きな画像を分割して使うのにimageクレートが使えた&piston_windowで使う編

Last updated at Posted at 2018-04-11

暇な時、pistonを用いたゲーム制作について少しずつ勉強しています。ただリファレンス読んでも3Dゲームの作り方は微塵も見つからない...

閑話休題

今回は2Dゲームの基本?として、いくつかの画像をまとめた大きな画像を読み込み、分割して使用する方法をまとめてみました。
少し前までpiston_windowクレート内部だけですべて完結させようとしていたので進展のなさに諦めかけていましたが、imageクレートを使用することで解決しました。

今回はこちらの一部を使って説明します。変数名が異なっていますのでご注意ください。
https://github.com/glaceef/sub_image

piston_windowクレートとimageクレートを組み合わせる

find_folderクレートなどで画像へのPathを取得しておきます。

// image path
let image_path = ...;

// create texture
let topen(&image_path).unwrap();

// create a sub image
let sub_image = SubImage::new(
  &mut texture,
  0, 0, // offset x, y
  100, 100 // width, height
);
let sub_image_buffer = sub_image.to_image();
let new_texture = Texture::from_image(
  &mut window.factory,
  &sub_image_buffer,
  &TextureSettings::new()
).unwrap();

画像を表示するには、クロージャ内部の記述にimage()を用います。

while let Some(e) = window.next() {
  window.draw_2d(&e, |c, g|{
    clear([1.0; 4], g);
    image(&new_texture, c.transform, g)
  });
}

###全然関係ないですけど...
以前の記事でも少し触れたのですが、piston_windowのループでは描画するときとしないときがあって、滑らかに描画されない(ガクガクしてる)のですが何か解決策を知っている方はいらっしゃいませんか...?

4
1
3

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
4
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?