From 4da4d43fc91d4c7116f9fd4ffda715826215f89d Mon Sep 17 00:00:00 2001 From: ghostly_zsh Date: Thu, 20 Mar 2025 00:16:59 -0500 Subject: [PATCH] well native exists but nothing is drawn --- crates/client/src/rendering/mod.rs | 93 +++++++++++++++++++++++++----- 1 file changed, 79 insertions(+), 14 deletions(-) diff --git a/crates/client/src/rendering/mod.rs b/crates/client/src/rendering/mod.rs index 55a9ad9e528cf6cb9b02efd652ae683bb9d1568f..553b9f9cca851aed4d710d39f8fa6ecbfa0055c0 100644 --- a/crates/client/src/rendering/mod.rs +++ b/crates/client/src/rendering/mod.rs @@ -1,17 +1,33 @@ +use std::num::NonZeroU32; + use glow::HasContext; +#[cfg(not(target_arch = "wasm32"))] +use glutin::surface::{Surface, WindowSurface, GlSurface, SwapInterval}; +#[cfg(not(target_arch = "wasm32"))] +use glutin::{config::{ConfigTemplateBuilder, GlConfig}, context::{ContextApi, ContextAttributesBuilder, PossiblyCurrentContext}, display::GetGlDisplay, prelude::{GlDisplay, NotCurrentGlContext}}; +#[cfg(not(target_arch = "wasm32"))] +use glutin_winit::{DisplayBuilder, GlWindow}; +#[cfg(target_arch = "wasm32")] use wasm_bindgen::{prelude::Closure, JsCast}; +#[cfg(target_arch = "wasm32")] use web_sys::{Event, HtmlCanvasElement}; -use winit::{application::ApplicationHandler, dpi::LogicalSize, event::WindowEvent, event_loop::ActiveEventLoop, platform::web::{WindowAttributesExtWebSys, WindowExtWebSys}, raw_window_handle::HasWindowHandle, window::{Window, WindowAttributes}}; +#[cfg(target_arch = "wasm32")] +use winit::platform::web::{WindowAttributesExtWebSys, WindowExtWebSys}; +use winit::{application::ApplicationHandler, dpi::LogicalSize, event::WindowEvent, event_loop::ActiveEventLoop, raw_window_handle::HasWindowHandle, window::{Window, WindowAttributes}}; pub mod init; #[derive(Default)] pub struct App { window: Option, - program: glow::Program, - vertex_array: glow::VertexArray, - vertex_buffer: glow::Buffer, - element_buffer: glow::Buffer, + program: Option, + vertex_array: Option, + vertex_buffer: Option, + element_buffer: Option, + #[cfg(not(target_arch = "wasm32"))] + gl_surface: Option>, + #[cfg(not(target_arch = "wasm32"))] + gl_context: Option, gl: Option, } @@ -42,19 +58,59 @@ impl ApplicationHandler for App { }; #[cfg(not(target_arch = "wasm32"))] let attributes = { - Window::default_attributes().with_title("StarKingdoms.TK"); + Window::default_attributes().with_transparent(true).with_title("StarKingdoms.TK") + .with_inner_size(LogicalSize::new(400, 300)) }; - self.window = Some(event_loop.create_window(attributes).unwrap()); + self.window = Some(event_loop.create_window(attributes.clone()).unwrap()); let window = self.window.as_ref().unwrap(); #[cfg(target_arch = "wasm32")] let context = window.canvas().unwrap().get_context("webgl2") .unwrap().unwrap() .dyn_into::() .unwrap(); - #[cfg(not(target_arch = "wasm32"))] - unsafe { - } + #[cfg(target_arch = "wasm32")] let (gl, shader_version) = (glow::Context::from_webgl2_context(context), "#version 300 es"); + + #[cfg(not(target_arch = "wasm32"))] + let (gl, shader_version) = unsafe { + let template = ConfigTemplateBuilder::new().with_transparency(true); + + let display_builder = DisplayBuilder::new().with_window_attributes(Some(attributes)); + + let (window, gl_config) = display_builder.build(event_loop, template, |configs| { + configs.reduce(|accum, config| { + if config.num_samples() > accum.num_samples() { + config + } else { + accum + } + }).unwrap() + }).unwrap(); + let raw_handle = window.as_ref().map(|window| window.window_handle().unwrap().window_handle().unwrap().as_raw()); + let gl_display = gl_config.display(); + let context_attributes = ContextAttributesBuilder::new() + .with_context_api(ContextApi::OpenGl(Some(glutin::context::Version { + major: 3, + minor: 0, + }))).build(raw_handle); + + let not_current_gl_context = gl_display.create_context(&gl_config, &context_attributes).unwrap(); + + let window = window.unwrap(); + let surface_attributes = window.build_surface_attributes(Default::default()).unwrap(); + let gl_surface = gl_display.create_window_surface(&gl_config, &surface_attributes).unwrap(); + + let gl_context = not_current_gl_context.make_current(&gl_surface).unwrap(); + + let gl = glow::Context::from_loader_function_cstr(|s| gl_display.get_proc_address(s)); + + gl_surface.set_swap_interval(&gl_context, SwapInterval::Wait(NonZeroU32::new(1).unwrap())).unwrap(); + + self.gl_surface = Some(gl_surface); + self.gl_context = Some(gl_context); + + (gl, "#version 300 es") + }; unsafe { let shaders = [ ("vertex", include_str!("../shaders/vertex.glsl"), glow::VERTEX_SHADER), @@ -95,10 +151,10 @@ impl ApplicationHandler for App { gl.clear_color(1.0, 1.0, 1.0, 1.0); gl.viewport(0, 0, window.inner_size().width as i32, window.inner_size().height as i32); - self.program = program; - self.vertex_array = vertex_array; - self.vertex_buffer = vertex_buffer; - self.element_buffer = element_buffer; + self.program = Some(program); + self.vertex_array = Some(vertex_array); + self.vertex_buffer = Some(vertex_buffer); + self.element_buffer = Some(element_buffer); } #[cfg(target_arch = "wasm32")] web_sys::window().unwrap().set_onresize(Some(Closure::::new(move |_| { @@ -118,7 +174,14 @@ impl ApplicationHandler for App { event: winit::event::WindowEvent, ) { match event { + WindowEvent::CloseRequested => { + event_loop.exit(); + } WindowEvent::Resized(size) => { + #[cfg(not(target_arch = "wasm32"))] + self.gl_surface.as_ref().unwrap().resize(self.gl_context.as_ref().unwrap(), + NonZeroU32::new(size.width).unwrap(), NonZeroU32::new(size.height).unwrap()); + unsafe { self.gl.as_ref().unwrap().viewport(0, 0, size.width as i32, size.height as i32); } @@ -130,6 +193,8 @@ impl ApplicationHandler for App { unsafe { gl.clear(glow::COLOR_BUFFER_BIT); gl.draw_elements(glow::TRIANGLES, 6, glow::UNSIGNED_INT, 0); + #[cfg(not(target_arch = "wasm32"))] + self.gl_surface.as_ref().unwrap().swap_buffers(self.gl_context.as_ref().unwrap()).unwrap(); } window.request_redraw();