Open1
Pistonでローグライク制作
Conrodを動かす
Piston上でConrodを動かすのに手間取ったのでメモ。
環境
- Windows 10
- rustc 1.52.1
-
opengl_graphics
環境 (OpenGL 3.2)
依存関係 (`Cargo.toml`)
[dependencies]
piston = "0.53.0"
piston2d-opengl_graphics = "0.78.0"
piston2d-graphics = "0.40.0"
pistoncore-glutin_window = "0.69.0"
vecmath = "1.0.0"
pathfinding = "2.1.5"
serde = { version = "1.0.126", features = ["derive"] }
serde_json = "1.0.59"
chrono = "0.4"
anyhow = "1.0"
dev_menu = "0.36.0"
conrod_core = "0.74"
conrod_piston = "0.74"
piston_window = "0.120"
find_folder = "0.3"
実装
サンプルコードは gfx_graphics
で実装されているので、これを opengl_graphics
に移植する。
gfx_graphics
と opengl_graphics
の違い
gfx_graphics |
opengl_graphics |
---|---|
piston_window::G2dTexture |
opengl_texture::Texture |
GfxGraphics |
GlGraphics |
-
texture_context
が不要-
G2dTexture::from_memory_alpha()
と違ってopengl_graphics::Texture::from_memory_alpha()
ではtexture_context
を使用しない -
piston_window::texture::UpdateTexture::update()
のformat
には&mut ()
を渡しているけどこれで合ってるのかは不明
-
移植部分のソースコード
晒せない依存箇所あるのでそのままでは動かないですが、雰囲気だけでも。
menu.rs
use super::constants::{WINDOW_HEIGHT, WINDOW_WIDTH};
use conrod_core;
use conrod_core::theme::Theme;
use piston_window::{PistonWindow, TextureSettings};
pub struct Menu<'a> {
ui: conrod_core::Ui,
text_texture_cache: opengl_graphics::Texture,
image_map: conrod_core::image::Map<opengl_graphics::Texture>,
glyph_cache: conrod_core::text::GlyphCache<'a>,
text_vertex_data: Vec<u8>,
}
fn theme() -> Theme {
use conrod_core::position::{Align, Direction, Padding, Position, Relative};
conrod_core::Theme {
name: "Demo Theme".to_string(),
padding: Padding::none(),
x_position: Position::Relative(Relative::Align(Align::Start), None),
y_position: Position::Relative(Relative::Direction(Direction::Backwards, 20.0), None),
background_color: conrod_core::color::DARK_CHARCOAL,
shape_color: conrod_core::color::LIGHT_CHARCOAL,
border_color: conrod_core::color::BLACK,
border_width: 0.0,
label_color: conrod_core::color::WHITE,
font_id: None,
font_size_large: 26,
font_size_medium: 18,
font_size_small: 12,
widget_styling: conrod_core::theme::StyleMap::default(),
mouse_drag_threshold: 0.0,
double_click_threshold: std::time::Duration::from_millis(500),
}
}
impl Menu<'_> {
pub fn new() -> Self {
let mut ui = conrod_core::UiBuilder::new([WINDOW_WIDTH as f64, WINDOW_HEIGHT as f64])
.theme(theme())
.build();
let assets = find_folder::Search::KidsThenParents(3, 5)
.for_folder("assets")
.unwrap();
let font_path = assets.join("PixelMplus10-Regular.ttf");
ui.fonts
.insert_from_file(font_path)
.expect("Could not load font");
let (glyph_cache, text_texture_cache) = {
const SCALE_TOLERANCE: f32 = 0.1;
const POSITION_TOLERANCE: f32 = 0.1;
let cache = conrod_core::text::GlyphCache::builder()
.dimensions(WINDOW_WIDTH as u32, WINDOW_HEIGHT as u32)
.scale_tolerance(SCALE_TOLERANCE)
.position_tolerance(POSITION_TOLERANCE)
.build();
let buffer_len = WINDOW_WIDTH as usize * WINDOW_HEIGHT as usize;
let init = vec![128; buffer_len];
let settings = TextureSettings::new();
let texture = opengl_graphics::Texture::from_memory_alpha(
&init,
WINDOW_WIDTH as u32,
WINDOW_HEIGHT as u32,
&settings,
)
.unwrap();
(cache, texture)
};
Menu {
ui: ui,
text_texture_cache: text_texture_cache,
image_map: conrod_core::image::Map::<opengl_graphics::Texture>::new(),
glyph_cache: glyph_cache,
text_vertex_data: Vec::<u8>::new(),
}
}
pub fn render(&mut self, c: &graphics::Context, g: &mut opengl_graphics::GlGraphics) {
let ui = self.ui.draw();
graphics::Rectangle::new(super::constants::color::BLACK).draw(
[
super::constants::WINDOW_SIZE as f64 / 8.,
super::constants::WINDOW_HEIGHT as f64 / 8.,
super::constants::WINDOW_SIZE as f64 * 6. / 8.,
super::constants::WINDOW_HEIGHT as f64 * 6. / 8.,
],
&c.draw_state,
c.transform,
g,
);
let text_vertex_data = &mut self.text_vertex_data;
// A function used for caching glyphs to the texture cache.
let cache_queued_glyphs = |_graphics: &mut opengl_graphics::GlGraphics,
cache: &mut opengl_graphics::Texture,
rect: conrod_core::text::rt::Rect<u32>,
data: &[u8]| {
let offset = [rect.min.x, rect.min.y];
let size = [rect.width(), rect.height()];
let format = piston_window::texture::Format::Rgba8;
text_vertex_data.clear();
text_vertex_data.extend(data.iter().flat_map(|&b| vec![255, 255, 255, b]));
piston_window::texture::UpdateTexture::update(
cache,
&mut (),
format,
&text_vertex_data[..],
offset,
size,
)
.expect("failed to update texture")
};
fn texture_from_image<T>(img: &T) -> &T {
img
}
conrod_piston::draw::primitives(
ui,
*c,
g,
&mut self.text_texture_cache,
&mut self.glyph_cache,
&self.image_map,
cache_queued_glyphs,
texture_from_image,
);
// glyphs.character(size: FontSize, ch: char)
}
}
The MIT License (MIT)
Copyright (c) 2014 PistonDevelopers
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.