[oden] Garbage assets, need to rewrite to IO
This commit is contained in:
parent
75fcc427ac
commit
17805fa4a6
17 changed files with 240 additions and 94 deletions
56
src/script/assets.rs
Normal file
56
src/script/assets.rs
Normal file
|
|
@ -0,0 +1,56 @@
|
|||
use crate::script::graphics::{CreateTextureCommand, GraphicsCommand};
|
||||
use oden_js::{module::native::NativeModuleBuilder, ContextRef, Error, Result};
|
||||
use std::sync::atomic::{AtomicU32, Ordering};
|
||||
use std::sync::mpsc::Sender;
|
||||
use std::sync::Arc;
|
||||
|
||||
struct AssetsImpl {
|
||||
next_texture_id: AtomicU32,
|
||||
gfx_sender: Sender<GraphicsCommand>,
|
||||
}
|
||||
|
||||
impl AssetsImpl {
|
||||
fn new(sender: Sender<GraphicsCommand>) -> Self {
|
||||
AssetsImpl {
|
||||
gfx_sender: sender,
|
||||
next_texture_id: AtomicU32::new(0),
|
||||
}
|
||||
}
|
||||
|
||||
fn load_texture(&self, path: &str) -> Result<u32> {
|
||||
let bytes = std::fs::read(path)?;
|
||||
let image = match image::load_from_memory(&bytes) {
|
||||
Ok(i) => i,
|
||||
Err(e) => return Err(Error::RustFunctionError(format!("{e}"))),
|
||||
};
|
||||
|
||||
let id = self.next_texture_id.fetch_add(1, Ordering::SeqCst);
|
||||
let _ = self
|
||||
.gfx_sender
|
||||
.send(GraphicsCommand::CreateTexture(CreateTextureCommand {
|
||||
id,
|
||||
image,
|
||||
label: path.into(),
|
||||
}));
|
||||
|
||||
Ok(id)
|
||||
}
|
||||
}
|
||||
|
||||
pub struct AssetsAPI {}
|
||||
|
||||
impl AssetsAPI {
|
||||
pub fn define(ctx: &ContextRef, sender: Sender<GraphicsCommand>) -> oden_js::Result<Self> {
|
||||
let assets = Arc::new(AssetsImpl::new(sender));
|
||||
let mut builder = NativeModuleBuilder::new(ctx);
|
||||
{
|
||||
let assets = assets.clone();
|
||||
builder.export(
|
||||
"load_texture",
|
||||
ctx.new_fn(move |_ctx: &ContextRef, p: String| assets.load_texture(&p))?,
|
||||
)?;
|
||||
}
|
||||
builder.build("asset-core")?;
|
||||
Ok(AssetsAPI {})
|
||||
}
|
||||
}
|
||||
|
|
@ -1,4 +1,4 @@
|
|||
use oden_js::{module, ContextRef, Value, ValueResult};
|
||||
use oden_js::{module, ContextRef};
|
||||
use std::sync::mpsc::Sender;
|
||||
use std::sync::Arc;
|
||||
|
||||
|
|
@ -24,11 +24,20 @@ pub struct SpriteCommand {
|
|||
pub sh: f32,
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct CreateTextureCommand {
|
||||
pub id: u32,
|
||||
pub image: image::DynamicImage,
|
||||
pub label: String,
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
pub enum GraphicsCommand {
|
||||
Clear(ClearCommand),
|
||||
Print(PrintCommand),
|
||||
Sprite(SpriteCommand),
|
||||
CreateTexture(CreateTextureCommand),
|
||||
UseTexture(u32),
|
||||
EndFrame,
|
||||
}
|
||||
|
||||
|
|
@ -41,33 +50,19 @@ impl GraphicsImpl {
|
|||
GraphicsImpl { sender }
|
||||
}
|
||||
|
||||
fn print_fn(&self, ctx: &ContextRef, text: String) -> ValueResult {
|
||||
fn print(&self, text: String) -> () {
|
||||
let _ = self
|
||||
.sender
|
||||
.send(GraphicsCommand::Print(PrintCommand { text }));
|
||||
|
||||
Ok(Value::undefined(ctx))
|
||||
}
|
||||
|
||||
fn cls_fn(&self, ctx: &ContextRef, r: f64, g: f64, b: f64) -> ValueResult {
|
||||
fn cls(&self, r: f64, g: f64, b: f64) -> () {
|
||||
let _ = self.sender.send(GraphicsCommand::Clear(ClearCommand {
|
||||
color: [r, g, b, 1.0],
|
||||
}));
|
||||
Ok(Value::undefined(ctx))
|
||||
}
|
||||
|
||||
fn spr_fn(
|
||||
&self,
|
||||
ctx: &ContextRef,
|
||||
x: f32,
|
||||
y: f32,
|
||||
w: f32,
|
||||
h: f32,
|
||||
u: f32,
|
||||
v: f32,
|
||||
sw: f32,
|
||||
sh: f32,
|
||||
) -> ValueResult {
|
||||
fn spr(&self, x: f32, y: f32, w: f32, h: f32, u: f32, v: f32, sw: f32, sh: f32) {
|
||||
let _ = self.sender.send(GraphicsCommand::Sprite(SpriteCommand {
|
||||
x,
|
||||
y,
|
||||
|
|
@ -78,7 +73,10 @@ impl GraphicsImpl {
|
|||
sw,
|
||||
sh,
|
||||
}));
|
||||
Ok(Value::undefined(ctx))
|
||||
}
|
||||
|
||||
fn use_texture(&self, id: u32) {
|
||||
let _ = self.sender.send(GraphicsCommand::UseTexture(id));
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -94,16 +92,14 @@ impl GraphicsAPI {
|
|||
let gfx = gfx.clone();
|
||||
builder.export(
|
||||
"print",
|
||||
ctx.new_fn(move |ctx: &ContextRef, t: String| gfx.print_fn(ctx, t))?,
|
||||
ctx.new_fn(move |_: &ContextRef, t: String| gfx.print(t))?,
|
||||
)?;
|
||||
}
|
||||
{
|
||||
let gfx = gfx.clone();
|
||||
builder.export(
|
||||
"cls",
|
||||
ctx.new_fn(move |ctx: &ContextRef, r: f64, g: f64, b: f64| {
|
||||
gfx.cls_fn(ctx, r, g, b)
|
||||
})?,
|
||||
ctx.new_fn(move |_: &ContextRef, r: f64, g: f64, b: f64| gfx.cls(r, g, b))?,
|
||||
)?;
|
||||
}
|
||||
{
|
||||
|
|
@ -111,7 +107,7 @@ impl GraphicsAPI {
|
|||
builder.export(
|
||||
"spr",
|
||||
ctx.new_fn(
|
||||
move |ctx: &ContextRef,
|
||||
move |_: &ContextRef,
|
||||
x: f32,
|
||||
y: f32,
|
||||
w: f32,
|
||||
|
|
@ -119,12 +115,17 @@ impl GraphicsAPI {
|
|||
u: f32,
|
||||
v: f32,
|
||||
sw: f32,
|
||||
sh: f32| {
|
||||
gfx.spr_fn(ctx, x, y, w, h, u, v, sw, sh)
|
||||
},
|
||||
sh: f32| gfx.spr(x, y, w, h, u, v, sw, sh),
|
||||
)?,
|
||||
)?;
|
||||
}
|
||||
{
|
||||
let gfx = gfx.clone();
|
||||
builder.export(
|
||||
"use_texture",
|
||||
ctx.new_fn(move |_: &ContextRef, id: u32| gfx.use_texture(id))?,
|
||||
)?;
|
||||
}
|
||||
builder.build("graphics-core")?;
|
||||
Ok(GraphicsAPI { gfx })
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue