Skip to content

Commit 56c955c

Browse files
committed
Make more progress on editor revival
1 parent ed7f9f5 commit 56c955c

14 files changed

Lines changed: 495 additions & 330 deletions

File tree

Cargo.lock

Lines changed: 9 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

crates/spaceman-dmm/Cargo.toml

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,7 @@ lodepng = "3.10.7"
1717
ndarray = "0.15.6"
1818
divrem = "1.0.0"
1919
sdl3 = "0.14.42"
20-
serde = "1.0.213"
21-
serde_derive = "1.0.213"
20+
serde = { version = "1.0.213", features = ["derive"] }
2221
toml = "0.5.11"
2322
petgraph = { version = "0.6.0", default-features = false }
2423
weak-table = "0.3.2"

crates/spaceman-dmm/build.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@ extern crate git2;
44
use std::env;
55
use std::fs::File;
66
use std::io::Write;
7-
use std::process::Command;
87
use std::path::{Path, PathBuf};
8+
use std::process::Command;
99

1010
fn main() {
1111
// build info
@@ -21,7 +21,7 @@ fn main() {
2121
if cfg!(windows) {
2222
let out_dir = env::var("OUT_DIR").ok().expect("can't find out_dir");
2323

24-
if cfg!(target_env="msvc") {
24+
if cfg!(target_env = "msvc") {
2525
if let Err(e) = Command::new("windres")
2626
.args(&["res/editor.rc", "-o"])
2727
.arg(&format!("{}/editor_rc.lib", out_dir))

crates/spaceman-dmm/src/config.rs

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
1-
use std::{io, env, fs};
21
use std::path::{Path, PathBuf};
2+
use std::{env, fs, io};
3+
4+
use serde::{Deserialize, Serialize};
35

46
#[derive(Serialize, Deserialize, Debug, Default)]
57
pub struct Config {
@@ -20,7 +22,7 @@ impl Config {
2022
pub fn make_recent(&mut self, recent: &Path) {
2123
if let Some(first) = self.recent.first() {
2224
if first == recent {
23-
return; // no work to do
25+
return; // no work to do
2426
}
2527
}
2628

@@ -54,21 +56,22 @@ fn load(path: &Path) -> io::Result<Config> {
5456
Err(e) => {
5557
println!("Config read error:\n{}", e);
5658
Ok(Config::default())
57-
}
59+
},
5860
}
5961
}
6062

6163
fn save(cfg: &Config, path: &Path) -> io::Result<()> {
62-
use std::io::Write;
6364
use serde::Serialize;
65+
use std::io::Write;
6466

6567
let mut buffer = String::new();
6668
cfg.serialize(
6769
toml::ser::Serializer::new(&mut buffer)
6870
.pretty_string(true)
6971
.pretty_string_literal(false)
7072
.pretty_array(true),
71-
).unwrap();
73+
)
74+
.unwrap();
7275

7376
if let Some(parent) = path.parent() {
7477
fs::create_dir_all(parent)?;

crates/spaceman-dmm/src/dmi.rs

Lines changed: 58 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,20 @@
11
//! Editor-environment specific DMI (texture) handling.
22
3+
use std::collections::hash_map::HashMap;
34
use std::io;
45
use std::path::{Path, PathBuf};
5-
use std::collections::hash_map::HashMap;
66
use std::sync::{Arc, RwLock};
77

8-
use lodepng::{self, RGBA, Decoder, ColorType};
8+
use lodepng::{self, ColorType, Decoder, RGBA};
9+
use sdl3::pixels::{PixelFormat, PixelFormatEnum};
10+
use sdl3::render::Texture;
11+
use sdl3::surface::Surface;
912

10-
use gfx::{self, Factory as FactoryTrait};
11-
use crate::{Factory, Texture};
13+
use crate::support::TextureCreator;
1214

1315
type Rect = (u32, u32, u32, u32);
1416

15-
pub use dm::dmi::*;
17+
pub use dreammaker::dmi::*;
1618

1719
// ----------------------------------------------------------------------------
1820
// Icon file and metadata handling
@@ -23,8 +25,8 @@ pub struct IconCache {
2325
}
2426

2527
#[derive(Default)]
26-
pub struct TextureCache {
27-
textures: Vec<Option<Texture>>,
28+
pub struct TextureCache<'r> {
29+
textures: Vec<Option<Texture<'r>>>,
2830
}
2931

3032
#[derive(Default)]
@@ -61,12 +63,12 @@ impl IconCache {
6163
lock.icons.push(arc);
6264
lock.paths.insert(relative_file_path.to_owned(), Some(i));
6365
Some(i)
64-
}
66+
},
6567
None => {
6668
let mut lock = self.lock.write().expect("IconCache poisoned");
6769
lock.paths.insert(relative_file_path.to_owned(), None);
6870
None
69-
}
71+
},
7072
},
7173
}
7274
}
@@ -84,13 +86,17 @@ impl IconCache {
8486
}*/
8587
}
8688

87-
impl TextureCache {
88-
pub fn retrieve(&mut self, factory: &mut Factory, icons: &IconCache, id: usize) -> &Texture {
89-
use crate::Fulfill;
89+
impl<'r> TextureCache<'r> {
90+
pub fn retrieve(
91+
&mut self,
92+
factory: &'r TextureCreator,
93+
icons: &IconCache,
94+
id: usize,
95+
) -> &Texture {
9096
if id >= self.textures.len() {
91-
self.textures.resize(id + 1, None);
97+
self.textures.resize_with(id + 1, Default::default);
9298
}
93-
self.textures[id].fulfill(|| load_texture(factory, &icons.get_icon(id).bitmap))
99+
self.textures[id].get_or_insert_with(|| load_texture(factory, &icons.get_icon(id).bitmap))
94100
}
95101

96102
pub fn clear(&mut self) {
@@ -104,7 +110,7 @@ fn load(path: &Path) -> Option<IconFile> {
104110
Err(err) => {
105111
eprintln!("error loading icon: {}\n {}", path.display(), err);
106112
None
107-
}
113+
},
108114
}
109115
}
110116

@@ -130,21 +136,27 @@ impl IconFile {
130136
}
131137

132138
pub fn uv_of(&self, icon_state: &str, dir: Dir) -> Option<[f32; 4]> {
133-
self.rect_of(icon_state, dir).map(|(x1, y1, w, h)| [
134-
x1 as f32 / self.width as f32,
135-
y1 as f32 / self.height as f32,
136-
(x1 + w) as f32 / self.width as f32,
137-
(y1 + h) as f32 / self.height as f32,
138-
])
139+
self.rect_of(icon_state, dir).map(|(x1, y1, w, h)| {
140+
[
141+
x1 as f32 / self.width as f32,
142+
y1 as f32 / self.height as f32,
143+
(x1 + w) as f32 / self.width as f32,
144+
(y1 + h) as f32 / self.height as f32,
145+
]
146+
})
139147
}
140148

141149
#[inline]
142150
pub fn rect_of(&self, icon_state: &str, dir: Dir) -> Option<Rect> {
143-
self.metadata.rect_of(self.width, icon_state, dir, 0)
151+
self.metadata
152+
.rect_of(self.width, &StateIndex::from(icon_state), dir, 0)
144153
}
145154
}
146155

147-
pub fn texture_from_bytes(factory: &mut Factory, bytes: &[u8]) -> io::Result<Texture> {
156+
pub fn texture_from_bytes<'r>(
157+
factory: &'r TextureCreator,
158+
bytes: &[u8],
159+
) -> io::Result<Texture<'r>> {
148160
let mut decoder = Decoder::new();
149161
decoder.info_raw_mut().colortype = ColorType::RGBA;
150162
decoder.info_raw_mut().set_bitdepth(8);
@@ -157,22 +169,30 @@ pub fn texture_from_bytes(factory: &mut Factory, bytes: &[u8]) -> io::Result<Tex
157169
Ok(load_texture(factory, &bitmap))
158170
}
159171

160-
pub fn load_texture(factory: &mut Factory, bitmap: &lodepng::Bitmap<RGBA>) -> Texture {
172+
pub fn load_texture<'r>(
173+
factory: &'r TextureCreator,
174+
bitmap: &lodepng::Bitmap<RGBA>,
175+
) -> Texture<'r> {
161176
let width = bitmap.width;
162177
let height = bitmap.height;
163-
let mut new_buffer = Vec::with_capacity(4 * width * height);
164-
for pixel in &bitmap.buffer {
165-
new_buffer.push(pixel.r);
166-
new_buffer.push(pixel.g);
167-
new_buffer.push(pixel.b);
168-
new_buffer.push(pixel.a);
169-
}
170178

171-
let kind = gfx::texture::Kind::D2(width as u16, height as u16, gfx::texture::AaMode::Single);
172-
let (_, view) = factory.create_texture_immutable_u8::<crate::ColorFormat>(
173-
kind,
174-
gfx::texture::Mipmap::Provided,
175-
&[&new_buffer[..]]
176-
).expect("create_texture_immutable_u8");
177-
view
179+
let mut surface = Surface::new(
180+
width as u32,
181+
height as u32,
182+
PixelFormat::from(PixelFormatEnum::RGBA8888),
183+
)
184+
.expect("Surface::new");
185+
surface.with_lock_mut(|dest| {
186+
let mut dest = dest.iter_mut();
187+
for pixel in &bitmap.buffer {
188+
*dest.next().unwrap() = pixel.r;
189+
*dest.next().unwrap() = pixel.g;
190+
*dest.next().unwrap() = pixel.b;
191+
*dest.next().unwrap() = pixel.a;
192+
}
193+
});
194+
195+
factory
196+
.create_texture_from_surface(surface)
197+
.expect("create_texture_from_surface")
178198
}

0 commit comments

Comments
 (0)