annotate python/src/lib.rs @ 772:7492d384d122 default tip

Rust: Add a Glide renderer (2D only for now) This is an experiment for a Rust renderer, iterating over the Python data using pyo3. It requires --feature=glide to be passed to cargo build, doesn’t support NPOT textures, text rendering, the background, or even msg faces, some of that may come in a future changeset.
author Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
date Mon, 05 Sep 2022 17:53:36 +0200
parents f6c287745a67
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
770
f6c287745a67 Rust: Add a libtouhou Python wrapper using pyo3
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
1 use pyo3::prelude::*;
f6c287745a67 Rust: Add a libtouhou Python wrapper using pyo3
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
2 use pyo3::types::PyBytes;
f6c287745a67 Rust: Add a libtouhou Python wrapper using pyo3
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
3 use touhou_formats::th06::pbg3;
f6c287745a67 Rust: Add a libtouhou Python wrapper using pyo3
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
4 use std::fs::File;
f6c287745a67 Rust: Add a libtouhou Python wrapper using pyo3
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
5 use std::io::BufReader;
f6c287745a67 Rust: Add a libtouhou Python wrapper using pyo3
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
6
772
7492d384d122 Rust: Add a Glide renderer (2D only for now)
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 770
diff changeset
7 #[cfg(feature = "glide")]
7492d384d122 Rust: Add a Glide renderer (2D only for now)
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 770
diff changeset
8 mod glide;
7492d384d122 Rust: Add a Glide renderer (2D only for now)
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 770
diff changeset
9
770
f6c287745a67 Rust: Add a libtouhou Python wrapper using pyo3
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
10 #[pyclass]
f6c287745a67 Rust: Add a libtouhou Python wrapper using pyo3
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
11 struct PBG3 {
f6c287745a67 Rust: Add a libtouhou Python wrapper using pyo3
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
12 inner: pbg3::PBG3<BufReader<File>>,
f6c287745a67 Rust: Add a libtouhou Python wrapper using pyo3
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
13 }
f6c287745a67 Rust: Add a libtouhou Python wrapper using pyo3
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
14
f6c287745a67 Rust: Add a libtouhou Python wrapper using pyo3
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
15 #[pymethods]
f6c287745a67 Rust: Add a libtouhou Python wrapper using pyo3
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
16 impl PBG3 {
f6c287745a67 Rust: Add a libtouhou Python wrapper using pyo3
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
17 #[staticmethod]
f6c287745a67 Rust: Add a libtouhou Python wrapper using pyo3
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
18 fn from_filename(filename: &str) -> PyResult<PBG3> {
f6c287745a67 Rust: Add a libtouhou Python wrapper using pyo3
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
19 let inner = pbg3::from_path_buffered(filename)?;
f6c287745a67 Rust: Add a libtouhou Python wrapper using pyo3
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
20 Ok(PBG3 {
f6c287745a67 Rust: Add a libtouhou Python wrapper using pyo3
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
21 inner
f6c287745a67 Rust: Add a libtouhou Python wrapper using pyo3
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
22 })
f6c287745a67 Rust: Add a libtouhou Python wrapper using pyo3
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
23 }
f6c287745a67 Rust: Add a libtouhou Python wrapper using pyo3
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
24
f6c287745a67 Rust: Add a libtouhou Python wrapper using pyo3
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
25 #[getter]
f6c287745a67 Rust: Add a libtouhou Python wrapper using pyo3
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
26 fn file_list(&self) -> Vec<String> {
f6c287745a67 Rust: Add a libtouhou Python wrapper using pyo3
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
27 self.inner.list_files().cloned().collect()
f6c287745a67 Rust: Add a libtouhou Python wrapper using pyo3
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
28 }
f6c287745a67 Rust: Add a libtouhou Python wrapper using pyo3
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
29
f6c287745a67 Rust: Add a libtouhou Python wrapper using pyo3
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
30 fn list_files(&self) -> Vec<String> {
f6c287745a67 Rust: Add a libtouhou Python wrapper using pyo3
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
31 self.inner.list_files().cloned().collect()
f6c287745a67 Rust: Add a libtouhou Python wrapper using pyo3
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
32 }
f6c287745a67 Rust: Add a libtouhou Python wrapper using pyo3
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
33
f6c287745a67 Rust: Add a libtouhou Python wrapper using pyo3
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
34 fn get_file(&mut self, py: Python, name: &str) -> PyObject {
f6c287745a67 Rust: Add a libtouhou Python wrapper using pyo3
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
35 let data = self.inner.get_file(name, true).unwrap();
f6c287745a67 Rust: Add a libtouhou Python wrapper using pyo3
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
36 PyBytes::new(py, &data).into_py(py)
f6c287745a67 Rust: Add a libtouhou Python wrapper using pyo3
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
37 }
f6c287745a67 Rust: Add a libtouhou Python wrapper using pyo3
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
38 }
f6c287745a67 Rust: Add a libtouhou Python wrapper using pyo3
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
39
f6c287745a67 Rust: Add a libtouhou Python wrapper using pyo3
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
40 #[pymodule]
772
7492d384d122 Rust: Add a Glide renderer (2D only for now)
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 770
diff changeset
41 fn libtouhou(py: Python, m: &PyModule) -> PyResult<()> {
770
f6c287745a67 Rust: Add a libtouhou Python wrapper using pyo3
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
42 m.add_class::<PBG3>()?;
772
7492d384d122 Rust: Add a Glide renderer (2D only for now)
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 770
diff changeset
43 #[cfg(feature = "glide")]
7492d384d122 Rust: Add a Glide renderer (2D only for now)
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 770
diff changeset
44 m.add_submodule(glide::module(py)?)?;
770
f6c287745a67 Rust: Add a libtouhou Python wrapper using pyo3
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
45 Ok(())
f6c287745a67 Rust: Add a libtouhou Python wrapper using pyo3
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
46 }