summaryrefslogtreecommitdiff
path: root/engine
diff options
context:
space:
mode:
authorHampusM <hampus@hampusmat.com>2023-10-13 23:32:00 +0200
committerHampusM <hampus@hampusmat.com>2023-10-13 23:32:00 +0200
commit12f48046b2606fc77a1312a6d5e5fc7ff9feff88 (patch)
tree644f4abd6785a5f0c066c7fbadcfc5e820a41ebf /engine
parentcfa73b1ea42fa491ff9e00bb5efb5e5a5d860578 (diff)
refactor(engine): move uses of OpenGL to OpenGL module
Diffstat (limited to 'engine')
-rw-r--r--engine/Cargo.toml1
-rw-r--r--engine/src/lib.rs3
-rw-r--r--engine/src/object.rs13
-rw-r--r--engine/src/opengl/currently_bound.rs (renamed from engine/src/currently_bound.rs)0
-rw-r--r--engine/src/opengl/mod.rs38
-rw-r--r--engine/src/opengl/shader.rs (renamed from engine/src/shader.rs)0
-rw-r--r--engine/src/opengl/vertex_array.rs (renamed from engine/src/renderer/vertex_array.rs)4
-rw-r--r--engine/src/opengl/vertex_buffer.rs (renamed from engine/src/renderer/vertex_buffer.rs)2
-rw-r--r--engine/src/renderer/mod.rs35
9 files changed, 64 insertions, 32 deletions
diff --git a/engine/Cargo.toml b/engine/Cargo.toml
index 7b42cab..40b2e9f 100644
--- a/engine/Cargo.toml
+++ b/engine/Cargo.toml
@@ -7,3 +7,4 @@ edition = "2021"
glfw = { path = "../glfw" }
thiserror = "1.0.49"
gl = "0.14.0"
+bitflags = "2.4.0"
diff --git a/engine/src/lib.rs b/engine/src/lib.rs
index 5b313ec..e036c38 100644
--- a/engine/src/lib.rs
+++ b/engine/src/lib.rs
@@ -6,11 +6,10 @@ pub use glfw::WindowSize;
use crate::object::Object;
use crate::vector::Vec2;
+mod opengl;
mod renderer;
-mod shader;
pub mod color;
-pub mod currently_bound;
pub mod object;
pub mod vector;
pub mod vertex;
diff --git a/engine/src/object.rs b/engine/src/object.rs
index 6b4bf4e..a650abc 100644
--- a/engine/src/object.rs
+++ b/engine/src/object.rs
@@ -1,5 +1,10 @@
+use crate::opengl::shader::{
+ Error as ShaderError,
+ Kind as ShaderKind,
+ Program as ShaderProgram,
+ Shader,
+};
use crate::renderer::Renderable;
-use crate::shader::{Kind as ShaderKind, Program as ShaderProgram, Shader};
use crate::vertex::Vertex;
#[derive(Debug)]
@@ -61,11 +66,11 @@ impl Object
pub enum Error
{
#[error("Failed to create vertex shader")]
- CreateVertexShaderFailed(#[source] crate::shader::Error),
+ CreateVertexShaderFailed(#[source] ShaderError),
#[error("Failed to create fragment shader")]
- CreateFragmentShaderFailed(#[source] crate::shader::Error),
+ CreateFragmentShaderFailed(#[source] ShaderError),
#[error("Failed to link shader program")]
- LinkShaderProgramFailed(#[source] crate::shader::Error),
+ LinkShaderProgramFailed(#[source] ShaderError),
}
diff --git a/engine/src/currently_bound.rs b/engine/src/opengl/currently_bound.rs
index eefa239..eefa239 100644
--- a/engine/src/currently_bound.rs
+++ b/engine/src/opengl/currently_bound.rs
diff --git a/engine/src/opengl/mod.rs b/engine/src/opengl/mod.rs
new file mode 100644
index 0000000..a58e72e
--- /dev/null
+++ b/engine/src/opengl/mod.rs
@@ -0,0 +1,38 @@
+use bitflags::bitflags;
+use glfw::WindowSize;
+
+use crate::vector::Vec2;
+
+pub mod currently_bound;
+pub mod shader;
+pub mod vertex_array;
+pub mod vertex_buffer;
+
+pub fn set_viewport(position: &Vec2<u32>, size: &WindowSize)
+{
+ unsafe {
+ #[allow(clippy::cast_possible_wrap)]
+ gl::Viewport(
+ position.x as i32,
+ position.y as i32,
+ size.width as i32,
+ size.height as i32,
+ );
+ }
+}
+
+pub fn clear_buffers(mask: BufferClearMask)
+{
+ unsafe {
+ gl::Clear(mask.bits());
+ }
+}
+
+bitflags! {
+ #[derive(Debug, Clone, Copy)]
+ pub struct BufferClearMask: u32 {
+ const COLOR = gl::COLOR_BUFFER_BIT;
+ const DEPTH = gl::DEPTH_BUFFER_BIT;
+ const STENCIL = gl::STENCIL_BUFFER_BIT;
+ }
+}
diff --git a/engine/src/shader.rs b/engine/src/opengl/shader.rs
index 0bbca77..0bbca77 100644
--- a/engine/src/shader.rs
+++ b/engine/src/opengl/shader.rs
diff --git a/engine/src/renderer/vertex_array.rs b/engine/src/opengl/vertex_array.rs
index 2bd6a5b..f255b8a 100644
--- a/engine/src/renderer/vertex_array.rs
+++ b/engine/src/opengl/vertex_array.rs
@@ -1,7 +1,7 @@
use std::mem::size_of;
-use crate::currently_bound::CurrentlyBound;
-use crate::renderer::vertex_buffer::VertexBuffer;
+use crate::opengl::currently_bound::CurrentlyBound;
+use crate::opengl::vertex_buffer::VertexBuffer;
use crate::vertex::{Attribute, AttributeComponentType, Vertex};
const VERTEX_STRIDE: usize = size_of::<Vertex>();
diff --git a/engine/src/renderer/vertex_buffer.rs b/engine/src/opengl/vertex_buffer.rs
index 57debc3..0050a9f 100644
--- a/engine/src/renderer/vertex_buffer.rs
+++ b/engine/src/opengl/vertex_buffer.rs
@@ -1,6 +1,6 @@
use std::mem::size_of_val;
-use crate::currently_bound::CurrentlyBound;
+use crate::opengl::currently_bound::CurrentlyBound;
use crate::vertex::Vertex;
#[derive(Debug)]
diff --git a/engine/src/renderer/mod.rs b/engine/src/renderer/mod.rs
index a23d5a0..4ed3c0a 100644
--- a/engine/src/renderer/mod.rs
+++ b/engine/src/renderer/mod.rs
@@ -3,14 +3,13 @@ use std::process::abort;
use glfw::WindowSize;
-use crate::renderer::vertex_array::{PrimitiveKind, VertexArray};
-use crate::renderer::vertex_buffer::{BufferUsage, VertexBuffer};
+use crate::opengl::shader::Program as ShaderProgram;
+use crate::opengl::vertex_array::{PrimitiveKind, VertexArray};
+use crate::opengl::vertex_buffer::{BufferUsage, VertexBuffer};
+use crate::opengl::{clear_buffers, BufferClearMask};
use crate::vector::Vec2;
use crate::vertex::Vertex;
-mod vertex_array;
-mod vertex_buffer;
-
pub fn initialize(window: &glfw::Window) -> Result<(), Error>
{
gl::load_with(|symbol| {
@@ -38,9 +37,7 @@ pub fn initialize(window: &glfw::Window) -> Result<(), Error>
pub fn render<'renderable>(renderables: impl IntoIterator<Item = &'renderable Renderable>)
{
- unsafe {
- gl::Clear(gl::COLOR_BUFFER_BIT);
- }
+ clear_buffers(BufferClearMask::COLOR);
for renderable in renderables {
renderable.shader_program.activate();
@@ -51,10 +48,15 @@ pub fn render<'renderable>(renderables: impl IntoIterator<Item = &'renderable Re
}
}
+pub fn set_viewport(position: &Vec2<u32>, size: &WindowSize)
+{
+ crate::opengl::set_viewport(position, size);
+}
+
#[derive(Debug)]
pub struct Renderable
{
- shader_program: crate::shader::Program,
+ shader_program: ShaderProgram,
vertex_arr: VertexArray,
/// Vertex buffer has to live as long as the vertex array
@@ -63,7 +65,7 @@ pub struct Renderable
impl Renderable
{
- pub fn new(shader_program: crate::shader::Program, vertices: &[Vertex]) -> Self
+ pub fn new(shader_program: ShaderProgram, vertices: &[Vertex]) -> Self
{
let vertex_arr = VertexArray::new();
let vertex_buffer = VertexBuffer::new();
@@ -84,19 +86,6 @@ impl Renderable
}
}
-pub fn set_viewport(position: &Vec2<u32>, size: &WindowSize)
-{
- unsafe {
- #[allow(clippy::cast_possible_wrap)]
- gl::Viewport(
- position.x as i32,
- position.y as i32,
- size.width as i32,
- size.height as i32,
- );
- }
-}
-
/// Renderer error.
#[derive(Debug, thiserror::Error)]
pub enum Error