[oden] Move scaling entirely into JavaScript
Now the game controls its own resolution. We might want to further copy Love2D and generate resize events, I don't know.
This commit is contained in:
parent
994be3e493
commit
1cb30034f8
9 changed files with 176 additions and 58 deletions
|
|
@ -1,4 +1,5 @@
|
|||
use oden_js::{module, ContextRef, Error, Result, Value};
|
||||
use std::cell::RefCell;
|
||||
use std::sync::atomic::{AtomicU32, Ordering};
|
||||
use std::sync::mpsc::Sender;
|
||||
use std::sync::Arc;
|
||||
|
|
@ -23,6 +24,15 @@ pub struct CreateTextureCommand {
|
|||
pub label: Option<String>,
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct SpriteCommand {
|
||||
pub src_top_left: [f32; 2],
|
||||
pub src_dims: [f32; 2],
|
||||
|
||||
pub dest_top_left: [f32; 2],
|
||||
pub dest_dims: [f32; 2],
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct CircleCommand {
|
||||
pub center: [f32; 2],
|
||||
|
|
@ -35,9 +45,10 @@ pub enum GraphicsCommand {
|
|||
Clear(ClearCommand),
|
||||
FillColor([f32; 4]),
|
||||
StrokeColor([f32; 4]),
|
||||
Scale([f32; 2]),
|
||||
Print(PrintCommand),
|
||||
Circle(CircleCommand),
|
||||
Sprite(crate::SpriteInstance),
|
||||
Sprite(SpriteCommand),
|
||||
CreateTexture(CreateTextureCommand),
|
||||
CreateWritableTexture {
|
||||
id: u32,
|
||||
|
|
@ -53,6 +64,7 @@ pub enum GraphicsCommand {
|
|||
|
||||
struct GraphicsImpl {
|
||||
next_texture_id: AtomicU32,
|
||||
dimensions: (f32, f32),
|
||||
sender: Sender<GraphicsCommand>,
|
||||
}
|
||||
|
||||
|
|
@ -60,6 +72,7 @@ impl GraphicsImpl {
|
|||
pub fn new(sender: Sender<GraphicsCommand>) -> Self {
|
||||
GraphicsImpl {
|
||||
sender,
|
||||
dimensions: (0.0, 0.0),
|
||||
next_texture_id: AtomicU32::new(0),
|
||||
}
|
||||
}
|
||||
|
|
@ -84,15 +97,17 @@ impl GraphicsImpl {
|
|||
let _ = self.sender.send(GraphicsCommand::StrokeColor([r, g, b, a]));
|
||||
}
|
||||
|
||||
fn scale(&self, x: f32, y: f32) -> () {
|
||||
let _ = self.sender.send(GraphicsCommand::Scale([x, y]));
|
||||
}
|
||||
|
||||
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(crate::SpriteInstance {
|
||||
src_top_left: [u, v],
|
||||
src_dims: [sw, sh],
|
||||
dest_top_left: [x, y],
|
||||
dest_dims: [w, h],
|
||||
}));
|
||||
let _ = self.sender.send(GraphicsCommand::Sprite(SpriteCommand {
|
||||
src_top_left: [u, v],
|
||||
src_dims: [sw, sh],
|
||||
dest_top_left: [x, y],
|
||||
dest_dims: [w, h],
|
||||
}));
|
||||
}
|
||||
|
||||
fn circle(&self, x: f32, y: f32, r: f32, s: f32) {
|
||||
|
|
@ -155,28 +170,42 @@ impl GraphicsImpl {
|
|||
fn write_to_texture(&self, id: u32) {
|
||||
let _ = self.sender.send(GraphicsCommand::WriteToTexture(id));
|
||||
}
|
||||
|
||||
fn get_dimensions(&self, ctx: &ContextRef) -> Result<Value> {
|
||||
let width = ctx.new_f64(self.dimensions.0)?;
|
||||
let height = ctx.new_f64(self.dimensions.1)?;
|
||||
|
||||
let mut result = ctx.new_array()?;
|
||||
result.set_index(ctx, 0, &width)?;
|
||||
result.set_index(ctx, 1, &height)?;
|
||||
Ok(result)
|
||||
}
|
||||
}
|
||||
|
||||
pub struct GraphicsAPI {
|
||||
gfx: Arc<GraphicsImpl>,
|
||||
gfx: Arc<RefCell<GraphicsImpl>>,
|
||||
}
|
||||
|
||||
impl GraphicsAPI {
|
||||
pub fn define(ctx: &ContextRef, sender: Sender<GraphicsCommand>) -> oden_js::Result<Self> {
|
||||
let gfx = Arc::new(GraphicsImpl::new(sender));
|
||||
let gfx = Arc::new(RefCell::new(GraphicsImpl::new(sender)));
|
||||
let mut builder = module::native::NativeModuleBuilder::new(ctx);
|
||||
{
|
||||
let gfx = gfx.clone();
|
||||
builder.export(
|
||||
"print",
|
||||
ctx.new_fn(move |_: &ContextRef, t: String, x: f32, y: f32| gfx.print(t, x, y))?,
|
||||
ctx.new_fn(move |_: &ContextRef, t: String, x: f32, y: f32| {
|
||||
gfx.borrow().print(t, x, y)
|
||||
})?,
|
||||
)?;
|
||||
}
|
||||
{
|
||||
let gfx = gfx.clone();
|
||||
builder.export(
|
||||
"cls",
|
||||
ctx.new_fn(move |_: &ContextRef, r: f64, g: f64, b: f64| gfx.cls(r, g, b))?,
|
||||
ctx.new_fn(move |_: &ContextRef, r: f64, g: f64, b: f64| {
|
||||
gfx.borrow().cls(r, g, b)
|
||||
})?,
|
||||
)?;
|
||||
}
|
||||
{
|
||||
|
|
@ -184,7 +213,7 @@ impl GraphicsAPI {
|
|||
builder.export(
|
||||
"color",
|
||||
ctx.new_fn(move |_: &ContextRef, r: f32, g: f32, b: f32, a: f32| {
|
||||
gfx.color(r, g, b, a)
|
||||
gfx.borrow().color(r, g, b, a)
|
||||
})?,
|
||||
)?;
|
||||
}
|
||||
|
|
@ -193,10 +222,17 @@ impl GraphicsAPI {
|
|||
builder.export(
|
||||
"stroke",
|
||||
ctx.new_fn(move |_: &ContextRef, r: f32, g: f32, b: f32, a: f32| {
|
||||
gfx.stroke(r, g, b, a)
|
||||
gfx.borrow().stroke(r, g, b, a)
|
||||
})?,
|
||||
)?;
|
||||
}
|
||||
{
|
||||
let gfx = gfx.clone();
|
||||
builder.export(
|
||||
"scale",
|
||||
ctx.new_fn(move |_: &ContextRef, x: f32, y: f32| gfx.borrow().scale(x, y))?,
|
||||
)?;
|
||||
}
|
||||
{
|
||||
let gfx = gfx.clone();
|
||||
builder.export(
|
||||
|
|
@ -210,7 +246,7 @@ impl GraphicsAPI {
|
|||
u: f32,
|
||||
v: f32,
|
||||
sw: f32,
|
||||
sh: f32| gfx.spr(x, y, w, h, u, v, sw, sh),
|
||||
sh: f32| gfx.borrow().spr(x, y, w, h, u, v, sw, sh),
|
||||
)?,
|
||||
)?;
|
||||
}
|
||||
|
|
@ -219,7 +255,7 @@ impl GraphicsAPI {
|
|||
builder.export(
|
||||
"circle",
|
||||
ctx.new_fn(move |_: &ContextRef, x: f32, y: f32, r: f32, s: f32| {
|
||||
gfx.circle(x, y, r, s)
|
||||
gfx.borrow().circle(x, y, r, s)
|
||||
})?,
|
||||
)?;
|
||||
}
|
||||
|
|
@ -227,7 +263,7 @@ impl GraphicsAPI {
|
|||
let gfx = gfx.clone();
|
||||
builder.export(
|
||||
"use_texture",
|
||||
ctx.new_fn(move |_: &ContextRef, id: u32| gfx.use_texture(id))?,
|
||||
ctx.new_fn(move |_: &ContextRef, id: u32| gfx.borrow().use_texture(id))?,
|
||||
)?;
|
||||
}
|
||||
{
|
||||
|
|
@ -236,7 +272,7 @@ impl GraphicsAPI {
|
|||
"create_texture",
|
||||
ctx.new_fn(
|
||||
move |c: &ContextRef, buffer: Value, label: Option<String>| {
|
||||
gfx.create_texture(c, buffer, label)
|
||||
gfx.borrow().create_texture(c, buffer, label)
|
||||
},
|
||||
)?,
|
||||
)?;
|
||||
|
|
@ -247,7 +283,7 @@ impl GraphicsAPI {
|
|||
"create_writable_texture",
|
||||
ctx.new_fn(
|
||||
move |_: &ContextRef, width: u32, height: u32, label: Option<String>| {
|
||||
gfx.create_writable_texture(width, height, label)
|
||||
gfx.borrow().create_writable_texture(width, height, label)
|
||||
},
|
||||
)?,
|
||||
)?;
|
||||
|
|
@ -256,14 +292,21 @@ impl GraphicsAPI {
|
|||
let gfx = gfx.clone();
|
||||
builder.export(
|
||||
"write_to_screen",
|
||||
ctx.new_fn(move |_: &ContextRef| gfx.write_to_screen())?,
|
||||
ctx.new_fn(move |_: &ContextRef| gfx.borrow().write_to_screen())?,
|
||||
)?;
|
||||
}
|
||||
{
|
||||
let gfx = gfx.clone();
|
||||
builder.export(
|
||||
"write_to_texture",
|
||||
ctx.new_fn(move |_: &ContextRef, id: u32| gfx.write_to_texture(id))?,
|
||||
ctx.new_fn(move |_: &ContextRef, id: u32| gfx.borrow().write_to_texture(id))?,
|
||||
)?;
|
||||
}
|
||||
{
|
||||
let gfx = gfx.clone();
|
||||
builder.export(
|
||||
"get_dimensions",
|
||||
ctx.new_fn(move |ctx: &ContextRef| gfx.borrow().get_dimensions(ctx))?,
|
||||
)?;
|
||||
}
|
||||
|
||||
|
|
@ -271,7 +314,11 @@ impl GraphicsAPI {
|
|||
Ok(GraphicsAPI { gfx })
|
||||
}
|
||||
|
||||
pub fn set_dimensions(&mut self, dimensions: (f32, f32)) {
|
||||
self.gfx.borrow_mut().dimensions = dimensions;
|
||||
}
|
||||
|
||||
pub fn end_frame(&self) {
|
||||
let _ = self.gfx.sender.send(GraphicsCommand::EndFrame);
|
||||
let _ = self.gfx.borrow().sender.send(GraphicsCommand::EndFrame);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue