Skip to content

Commit

Permalink
remove window from app
Browse files Browse the repository at this point in the history
  • Loading branch information
geeklint committed Feb 10, 2024
1 parent 07fec06 commit b1eed6a
Show file tree
Hide file tree
Showing 16 changed files with 51 additions and 166 deletions.
27 changes: 1 addition & 26 deletions suzy/src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ use std::{cell::RefCell, rc::Rc, time};

use crate::{
dims::{Padding2d, Rect, SimpleRect},
graphics::PlatformDrawContext,
platform::Platform,
pointer::{PointerEvent, PointerEventData},
widget::{self, Widget},
Expand Down Expand Up @@ -48,7 +47,6 @@ mod app_struct {
P: ?Sized + Platform,
{
pub(crate) watch_ctx: WatchContext<'static>,
pub(crate) window: P::Window,
pub(super) roots: Vec<RootHolder<P::Renderer>>,
pub(super) pointer_grab_map: HashMap<PointerId, UniqueHandleId>,
pub(crate) state: Rc<super::AppState>,
Expand Down Expand Up @@ -84,7 +82,7 @@ pub fn coarse_time() -> time::Instant {
}

impl<P: Platform> App<P> {
pub fn from_window(window: P::Window) -> Self {
pub fn from_window(window: &P::Window) -> Self {
use std::collections::HashMap;

use crate::watch::WatchContext;
Expand All @@ -96,7 +94,6 @@ impl<P: Platform> App<P> {

Self {
watch_ctx,
window,
roots: Vec::new(),
pointer_grab_map: HashMap::new(),
state,
Expand Down Expand Up @@ -144,28 +141,6 @@ impl<P: Platform> App<P> {
self.needs_draw = true;
}

pub fn loop_draw(&mut self) {
let mut loop_count: u32 = 0;
let mut pass_arg = None;
loop {
let mut ctx = self.window.prepare_draw(pass_arg);
for root in self.roots.iter_mut() {
root.borrow_mut().draw(&mut ctx);
}
pass_arg = ctx.finish();
if pass_arg.is_none() {
break;
}
debug_assert!(
loop_count < 1024,
"render exceeded its loop count (possible infinite loop)",
);
self.watch_ctx.update();
loop_count += 1;
}
self.needs_draw = false;
}

pub fn draw(
&mut self,
ctx: &mut crate::graphics::DrawContext<'_, P::Renderer>,
Expand Down
24 changes: 1 addition & 23 deletions suzy/src/app/tester.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use std::time;

use drying_paint::WatchedValueCore;

use crate::{platform::Platform, pointer::PointerEventData, window::Window};
use crate::{platform::Platform, pointer::PointerEventData};

use super::App;

Expand All @@ -27,8 +27,6 @@ pub trait AppTestingExt {
let frame_time = time::Duration::from_nanos(16666667);
self.next_frame(frame_time);
}

fn screenshot_tmp(&self) -> ScreenshotTaker;
}

impl<P: Platform> AppTestingExt for App<P> {
Expand All @@ -54,24 +52,4 @@ impl<P: Platform> AppTestingExt for App<P> {
y: py,
});
}

fn screenshot_tmp(&self) -> ScreenshotTaker {
ScreenshotTaker
}
}

pub struct ScreenshotTaker;

impl ScreenshotTaker {
pub fn draw_and_take_screenshot<P>(
&mut self,
app: &mut App<P>,
) -> Box<[u8]>
where
P: Platform,
{
app.update_watches();
app.loop_draw();
app.window.take_screenshot()
}
}
19 changes: 7 additions & 12 deletions suzy/src/platforms.rs
Original file line number Diff line number Diff line change
Expand Up @@ -99,23 +99,18 @@ impl TestEnvWindow {
gl_win,
}
}
}

#[cfg(feature = "platform_opengl")]
impl crate::window::Window<opengl::OpenGlRenderPlatform> for TestEnvWindow {
fn prepare_draw(
pub fn draw_and_take_screenshot(
&mut self,
pass_arg: Option<<crate::platforms::opengl::OpenGlRenderPlatform as crate::platform::RenderPlatform>::DrawPassInfo>,
) -> crate::graphics::DrawContext<'_, opengl::OpenGlRenderPlatform> {
let first_pass = pass_arg.is_none();
app: &mut crate::app::App<TestPlatform>,
) -> Box<[u8]> {
let gl_win = (*self.gl_win).as_mut();
if first_pass {
gl_win.clear();
}
let size = [self.width.into(), self.height.into()];
gl_win.prepare_draw(size, first_pass)
gl_win.draw_and_take_screenshot(app)
}
}

#[cfg(feature = "platform_opengl")]
impl crate::window::Window<opengl::OpenGlRenderPlatform> for TestEnvWindow {
fn take_screenshot(&mut self) -> Box<[u8]> {
let gl_win = (*self.gl_win).as_mut();
gl_win.take_screenshot()
Expand Down
7 changes: 0 additions & 7 deletions suzy/src/platforms/no_graphics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,13 +43,6 @@ impl crate::graphics::PlatformDrawContext<()> for NoGraphics {
}

impl crate::window::Window<NoGraphics> for Window {
fn prepare_draw(
&mut self,
_pass_arg: Option<()>,
) -> crate::graphics::DrawContext<'_, NoGraphics> {
NoGraphics
}

fn take_screenshot(&mut self) -> Box<[u8]> {
unimplemented!("Can't take screenshot with a NoGraphics window");
}
Expand Down
21 changes: 6 additions & 15 deletions suzy/src/platforms/opengl/graphics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,31 +12,22 @@ use super::{
Mat4,
};

enum DrawPass {
enum DrawPass<'a> {
GatherTextures,
Main {
masking: BatchMasking,
batch_pool: BatchPool,
batch_pool: &'a mut BatchPool,
},
}

pub struct DrawContext<'a> {
context: &'a mut super::context::OpenGlContext,
pass: DrawPass,
pass: DrawPass<'a>,
}

impl<'a> crate::graphics::PlatformDrawContext<()> for DrawContext<'a> {
fn finish(self) -> Option<()> {
match self.pass {
DrawPass::GatherTextures => {
self.context.run_texture_populators();
Some(())
}
DrawPass::Main { batch_pool, .. } => {
super::renderer::render(self.context, batch_pool);
None
}
}
unimplemented!()
}
}

Expand All @@ -52,7 +43,7 @@ impl<'a> DrawContext<'a> {

pub(crate) fn main_draw_pass(
context: &'a mut super::context::OpenGlContext,
batch_pool: BatchPool,
batch_pool: &'a mut BatchPool,
) -> Self {
Self {
context,
Expand Down Expand Up @@ -141,7 +132,7 @@ impl<'a> DrawContext<'a> {
DrawPass::GatherTextures => (),
DrawPass::Main { batch_pool, .. } => {
let new_pool = BatchPool::new(f(batch_pool.matrix));
let old_pool = std::mem::replace(batch_pool, new_pool);
let old_pool = std::mem::replace(*batch_pool, new_pool);
super::renderer::render(self.context, old_pool);
}
}
Expand Down
29 changes: 5 additions & 24 deletions suzy/src/platforms/opengl/window.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

#![allow(missing_docs)]

use crate::graphics::{Color, DrawContext};
use crate::graphics::Color;

use super::{
context::bindings::types::*,
Expand Down Expand Up @@ -62,26 +62,6 @@ impl Window {
}
}

pub fn prepare_draw(
&mut self,
screen_size: [f32; 2],
first_pass: bool,
) -> DrawContext<'_, OpenGlRenderPlatform> {
unsafe {
self.ctx.bindings.Enable(BLEND);
self.ctx.bindings.BlendFunc(SRC_ALPHA, ONE_MINUS_SRC_ALPHA);
}
if first_pass {
super::DrawContext::gather_textures(&mut self.ctx)
} else {
let [screen_width, screen_height] = screen_size;
let matrix = Mat4::translate(-1.0, -1.0)
* Mat4::scale(2.0 / screen_width, 2.0 / screen_height);
let batch_pool = super::renderer::BatchPool::new(matrix);
super::DrawContext::main_draw_pass(&mut self.ctx, batch_pool)
}
}

pub fn draw_app<P>(&mut self, app: &mut crate::app::App<P>)
where
P: crate::platform::Platform<Renderer = OpenGlRenderPlatform>,
Expand All @@ -97,12 +77,12 @@ impl Window {
* Mat4::scale(2.0 / screen_width, 2.0 / screen_height);
app.draw(&mut super::DrawContext::gather_textures(&mut self.ctx));
self.ctx.run_texture_populators();
let current_batches = super::renderer::BatchPool::new(matrix);
let mut current_batches = super::renderer::BatchPool::new(matrix);
app.draw(&mut super::DrawContext::main_draw_pass(
&mut self.ctx,
current_batches,
&mut current_batches,
));
//super::renderer::render(&mut self.ctx, current_batches);
super::renderer::render(&mut self.ctx, current_batches);
}

/// Issue opengl call to clear the screen.
Expand Down Expand Up @@ -147,6 +127,7 @@ impl Window {
P: crate::platform::Platform<Renderer = OpenGlRenderPlatform>,
{
app.update_watches();
self.clear();
self.draw_app(app);
self.take_screenshot()
}
Expand Down
11 changes: 1 addition & 10 deletions suzy/src/platforms/osmesa/window.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/* SPDX-License-Identifier: (Apache-2.0 OR MIT OR Zlib) */
/* Copyright © 2021 Violet Leonard */

use crate::{graphics::Color, graphics::DrawContext, window::Window};
use crate::{graphics::Color, window::Window};

use crate::platforms::opengl;

Expand Down Expand Up @@ -68,15 +68,6 @@ impl OsMesaWindow {
}

impl Window<opengl::OpenGlRenderPlatform> for OsMesaWindow {
fn prepare_draw(
&mut self,
frame_arg: Option<()>,
) -> DrawContext<'_, opengl::OpenGlRenderPlatform> {
let size = [self.width.into(), self.height.into()];
self.gl_win.clear();
self.gl_win.prepare_draw(size, frame_arg.is_none())
}

fn take_screenshot(&mut self) -> Box<[u8]> {
self.gl_win.take_screenshot()
}
Expand Down
21 changes: 13 additions & 8 deletions suzy/src/platforms/sdl2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,11 @@ impl SdlPlatform {
}
}

pub fn run(self, app: &mut crate::app::App<Self>) -> Result<(), String> {
pub fn run(
self,
window: &mut window::Window,
app: &mut crate::app::App<Self>,
) -> Result<(), String> {
let mut event_pump = self.sdl.event_pump()?;
loop {
use sdl2::event::{Event, WindowEvent};
Expand All @@ -44,16 +48,15 @@ impl SdlPlatform {
} => {
return Ok(());
}
event => app.handle_event(event, || {
event => app.handle_event(window, event, || {
let state = event_pump.mouse_state();
(state.x() as f32, state.y() as f32)
}),
}
}
app.update_watches();
app.loop_draw();
//app.window.gl_win.draw_app(&mut app);
app.window.flip();
window.gl_win.draw_app(app);
window.flip();
}
}
}
Expand All @@ -67,6 +70,7 @@ impl Default for SdlPlatform {
pub trait AppHandleSdlEvent {
fn handle_event(
&mut self,
window: &mut window::Window,
event: sdl2::event::Event,
mouse_pos: impl FnOnce() -> (f32, f32),
);
Expand All @@ -75,6 +79,7 @@ pub trait AppHandleSdlEvent {
impl AppHandleSdlEvent for crate::app::App<SdlPlatform> {
fn handle_event(
&mut self,
window: &mut window::Window,
event: sdl2::event::Event,
mouse_pos: impl FnOnce() -> (f32, f32),
) {
Expand All @@ -84,10 +89,10 @@ impl AppHandleSdlEvent for crate::app::App<SdlPlatform> {
match win_event {
WindowEvent::SizeChanged(_, _)
| WindowEvent::Moved { .. } => {
let [width, height] = self.window.logical_size();
let [width, height] = window.logical_size();
self.resize(width, height);
self.update_dpi(self.window.dpi());
self.window.recalculate_viewport();
self.update_dpi(window.dpi());
window.recalculate_viewport();
}
WindowEvent::Leave => {
self.pointer_event(PointerEventData {
Expand Down
13 changes: 1 addition & 12 deletions suzy/src/platforms/sdl2/window.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use std::convert::TryInto;
use sdl2::video::WindowBuildError;

use crate::{
graphics::{Color, DrawContext},
graphics::Color,
platforms::opengl,
window::{self},
};
Expand Down Expand Up @@ -137,17 +137,6 @@ impl Window {
}

impl window::Window<opengl::OpenGlRenderPlatform> for Window {
fn prepare_draw(
&mut self,
pass_arg: Option<()>,
) -> DrawContext<'_, opengl::OpenGlRenderPlatform> {
let first_pass = pass_arg.is_none();
if first_pass {
self.gl_win.clear();
}
self.gl_win.prepare_draw(self.logical_size(), first_pass)
}

fn take_screenshot(&mut self) -> Box<[u8]> {
(*self).take_screenshot()
}
Expand Down
6 changes: 3 additions & 3 deletions suzy/src/widget/content.rs
Original file line number Diff line number Diff line change
Expand Up @@ -160,17 +160,17 @@ where
let platform = DefaultPlatform {
sdl: sdl2::init().expect("Failed to initialize SDL2"),
};
let window = Window::new_window(
let mut window = Window::new_window(
&platform.sdl,
WindowSettings {
title: &T::app_title(),
..WindowSettings::default()
},
)
.expect("Failed to open window");
let mut app = App::<DefaultPlatform>::from_window(window);
let mut app = App::<DefaultPlatform>::from_window(&window);
app.add_root(widget::Widget::<T>::default());
let code: i32 = match platform.run(&mut app) {
let code: i32 = match platform.run(&mut window, &mut app) {
Ok(()) => 0,
Err(_) => 1,
};
Expand Down
Loading

0 comments on commit b1eed6a

Please sign in to comment.