diff --git a/Sandbox/assets/shaders/FlatColor.glsl b/Sandbox/assets/shaders/FlatColor.glsl new file mode 100644 index 00000000..2d39fc5b --- /dev/null +++ b/Sandbox/assets/shaders/FlatColor.glsl @@ -0,0 +1,26 @@ +// Flat Color Shader + +#type vertex +#version 330 core + +layout(location = 0) in vec3 a_Position; + +uniform mat4 u_ViewProjection; +uniform mat4 u_Transform; + +void main() +{ + gl_Position = u_ViewProjection * u_Transform * vec4(a_Position, 1.0); +} + +#type fragment +#version 330 core + +layout(location = 0) out vec4 color; + +uniform vec4 u_Color; + +void main() +{ + color = u_Color; +} \ No newline at end of file diff --git a/Sandbox/assets/shaders/Texture.glsl b/Sandbox/assets/shaders/Texture.glsl index 70f82152..7ae91bce 100644 --- a/Sandbox/assets/shaders/Texture.glsl +++ b/Sandbox/assets/shaders/Texture.glsl @@ -24,9 +24,10 @@ in vec2 v_TexCoord; + uniform vec4 u_Color; uniform sampler2D u_Texture; void main() { - color = texture(u_Texture, v_TexCoord); + color = texture(u_Texture, v_TexCoord * 10.0) * u_Color; } \ No newline at end of file diff --git a/Sandbox/src/Sandbox2D.cpp b/Sandbox/src/Sandbox2D.cpp new file mode 100644 index 00000000..f81dacca --- /dev/null +++ b/Sandbox/src/Sandbox2D.cpp @@ -0,0 +1,72 @@ +#include "Sandbox2D.h" + +#include "imgui/imgui.h" + +#include +#include + +Sandbox2D::Sandbox2D() + :Layer("Sandbox2D"), m_CameraController(1280.0f / 720.0f, true) +{ + +} + +void Sandbox2D::OnAttach() +{ + m_CheckerboardTexture = StarStudio::Texture2D::Create("assets/textures/Checkerboard.png"); +} + +void Sandbox2D::OnDetach() +{ + +} + +void Sandbox2D::OnUpdate(StarStudio::Timestep ts) +{ + // Update + m_CameraController.OnUpdate(ts); + // Render + StarStudio::RenderCommand::SetClearColor({ 0.1f, 0.1f, 0.1f, 1 }); + StarStudio::RenderCommand::Clear(); + + StarStudio::Renderer2D::BeginScene(m_CameraController.GetCamera()); + StarStudio::Renderer2D::DrawQuad({ -1.0f, 0.0f }, { 0.8f, 0.8f }, { 0.8f, 0.2f, 0.3f, 1.0f }); + StarStudio::Renderer2D::DrawQuad({ 0.5f, -0.5f }, { 0.5f, 0.75f }, { 0.2f, 0.3f, 0.8f, 1.0f }); + StarStudio::Renderer2D::DrawQuad({ 0.0f, 0.0f, -0.1f }, { 10.0f, 10.0f }, m_CheckerboardTexture); + StarStudio::Renderer2D::EndScene(); +} + +void Sandbox2D::OnImGuiRender() +{ + //Camera Info + ImGui::Begin("Camera Info"); + + ImGui::Text("Camera Position"); + ImGui::Text("X: %.2f", m_CameraController.GetCamera().GetPosition().x); + ImGui::Text("Y: %.2f", m_CameraController.GetCamera().GetPosition().y); + ImGui::Text("Z: %.2f", m_CameraController.GetCamera().GetPosition().z); + + ImGui::Text("Camera Rotation: %.2f", m_CameraController.GetCamera().GetRotation()); + + if (ImGui::Button("Reset")) { + m_CameraController.GetCamera().SetPosition(glm::vec3(0.0f)); + m_CameraController.GetCamera().SetRotation(0.0f); + } + + //Color Picker + ImGui::Text("Color Picker"); + + ImGui::ColorEdit3("Square Color", glm::value_ptr(m_SquareColor)); + + ImGui::Text("Frame Counter"); + + ImGui::Text("FPS: %.u", ImGui::GetIO().Framerate); + + ImGui::End(); + +} + +void Sandbox2D::OnEvent(StarStudio::Event& e) +{ + m_CameraController.OnEvent(e); +} diff --git a/Sandbox/src/Sandbox2D.h b/Sandbox/src/Sandbox2D.h new file mode 100644 index 00000000..6aef8da9 --- /dev/null +++ b/Sandbox/src/Sandbox2D.h @@ -0,0 +1,27 @@ +#pragma once +#include "StarStudio.h" +#include "StarStudio/Renderer/OrthographicCameraController.h" + +class Sandbox2D : public StarStudio::Layer +{ +public: + Sandbox2D(); + virtual ~Sandbox2D() = default; + + virtual void OnAttach() override; + virtual void OnDetach() override; + + void OnUpdate(StarStudio::Timestep ts) override; + virtual void OnImGuiRender() override; + void OnEvent(StarStudio::Event& e) override; +private: + StarStudio::OrthographicCameraController m_CameraController; + + // Temp + StarStudio::Ref m_SquareVA; + StarStudio::Ref m_FlatColorShader; + + StarStudio::Ref m_CheckerboardTexture; + + glm::vec4 m_SquareColor = { 0.2f, 0.3f, 0.8f, 1.0f }; +}; diff --git a/Sandbox/src/SandboxApp.cpp b/Sandbox/src/SandboxApp.cpp index e2c9e246..39dc10aa 100644 --- a/Sandbox/src/SandboxApp.cpp +++ b/Sandbox/src/SandboxApp.cpp @@ -1,19 +1,24 @@ #include +#include #include "Platform/OpenGL/OpenGLShader.h" #include "imgui/imgui.h" #include + +#include "Sandbox2D.h" + +#include "StarStudio/Renderer/OrthographicCameraController.h" #include "glm/gtc/type_ptr.hpp" class ExampleLayer : public StarStudio::Layer { public: ExampleLayer() - : Layer("Example"), m_Camera(-1.6f, 1.6f, -0.9f, 0.9f), m_CameraPosition(0.0f) + : Layer("Example"), m_CameraController(1280.0f / 720.0f, true) { - m_VertexArray.reset(StarStudio::VertexArray::Create()); + m_VertexArray = StarStudio::VertexArray::Create(); float vertices[3 * 7] = { -0.5f, -0.5f, 0.0f, 0.8f, 0.2f, 0.8f, 1.0f, @@ -35,7 +40,7 @@ class ExampleLayer : public StarStudio::Layer indexBuffer.reset(StarStudio::IndexBuffer::Create(indices, sizeof(indices) / sizeof(uint32_t))); m_VertexArray->SetIndexBuffer(indexBuffer); - m_SquareVA.reset(StarStudio::VertexArray::Create()); + m_SquareVA = StarStudio::VertexArray::Create(); float squareVertices[5 * 4] = { -0.5f, -0.5f, 0.0f, 0.0f, 0.0f, @@ -139,28 +144,14 @@ class ExampleLayer : public StarStudio::Layer void OnUpdate(StarStudio::Timestep ts) override { - if (StarStudio::Input::IsKeyPressed(SS_KEY_LEFT)) - m_CameraPosition.x -= m_CameraMoveSpeed * ts; - else if (StarStudio::Input::IsKeyPressed(SS_KEY_RIGHT)) - m_CameraPosition.x += m_CameraMoveSpeed * ts; - - if (StarStudio::Input::IsKeyPressed(SS_KEY_UP)) - m_CameraPosition.y += m_CameraMoveSpeed * ts; - else if (StarStudio::Input::IsKeyPressed(SS_KEY_DOWN)) - m_CameraPosition.y -= m_CameraMoveSpeed * ts; - - if (StarStudio::Input::IsKeyPressed(SS_KEY_A)) - m_CameraRotation += m_CameraRotationSpeed * ts; - if (StarStudio::Input::IsKeyPressed(SS_KEY_D)) - m_CameraRotation -= m_CameraRotationSpeed * ts; + // Update + m_CameraController.OnUpdate(ts); + // Render StarStudio::RenderCommand::SetClearColor({ 0.1f, 0.1f, 0.1f, 1 }); StarStudio::RenderCommand::Clear(); - m_Camera.SetPosition(m_CameraPosition); - m_Camera.SetRotation(m_CameraRotation); - - StarStudio::Renderer::BeginScene(m_Camera); + StarStudio::Renderer::BeginScene(m_CameraController.GetCamera()); glm::mat4 scale = glm::scale(glm::mat4(1.0f), glm::vec3(0.1f)); @@ -199,34 +190,31 @@ class ExampleLayer : public StarStudio::Layer ImGui::Begin("Camera Info"); ImGui::Text("Camera Position"); - ImGui::Text("X: %.2f", m_CameraPosition.x); - ImGui::Text("Y: %.2f", m_CameraPosition.y); - ImGui::Text("Z: %.2f", m_CameraPosition.z); + ImGui::Text("X: %.2f", m_CameraController.GetCamera().GetPosition().x); + ImGui::Text("Y: %.2f", m_CameraController.GetCamera().GetPosition().y); + ImGui::Text("Z: %.2f", m_CameraController.GetCamera().GetPosition().z); - ImGui::Text("Camera Rotation: %.2f", m_CameraRotation); + ImGui::Text("Camera Rotation: %.2f", m_CameraController.GetCamera().GetRotation()); if (ImGui::Button("Reset")) { - m_CameraPosition = glm::vec3(0.0f); - m_CameraRotation = 0.0f; + m_CameraController.GetCamera().SetPosition(glm::vec3(0.0f)); + m_CameraController.GetCamera().SetRotation(0.0f); } ImGui::End(); - /* + //Color Picker ImGui::Begin("Color Picker"); ImGui::ColorEdit3("Square Color", glm::value_ptr(m_SquareColor)); - if (ImGui::Button("Reset")) - { - m_SquareColor = { 0.2f, 0.2f, 0.2f}; - } ImGui::End(); - */ - } - void OnEvent(StarStudio::Event& event) override - { + + } + void OnEvent(StarStudio::Event& e) override + { + m_CameraController.OnEvent(e); } private: @@ -239,12 +227,7 @@ class ExampleLayer : public StarStudio::Layer StarStudio::Ref m_Texture, m_starLogTexture; - StarStudio::OrthographicCamera m_Camera; - glm::vec3 m_CameraPosition; - float m_CameraMoveSpeed = 5.0f; - - float m_CameraRotation = 0.0f; - float m_CameraRotationSpeed = 180.0f; + StarStudio::OrthographicCameraController m_CameraController; glm::vec3 m_SquareColor = { 0.2f, 0.2f, 0.2f}; }; @@ -254,7 +237,8 @@ class Sandbox : public StarStudio::Application public: Sandbox() { - PushLayer(new ExampleLayer()); + // PushLayer(new ExampleLayer()); + PushLayer(new Sandbox2D()); } ~Sandbox() diff --git a/StarStudio/src/Platform/OpenGL/OpenGLContext.cpp b/StarStudio/src/Platform/OpenGL/OpenGLContext.cpp index 29ae087a..6f1466ab 100644 --- a/StarStudio/src/Platform/OpenGL/OpenGLContext.cpp +++ b/StarStudio/src/Platform/OpenGL/OpenGLContext.cpp @@ -23,6 +23,14 @@ namespace StarStudio { SS_CORE_INFO(" Vendor: {0}", (const char*)glGetString(GL_VENDOR)); SS_CORE_INFO(" Renderer: {0}", (const char*)glGetString(GL_RENDERER)); SS_CORE_INFO(" Version: {0}", (const char*)glGetString(GL_VERSION)); + + #ifdef SS_ENABLE_ASSERTS + int versionMajor; + int versionMinor; + glGetIntegerv(GL_MAJOR_VERSION, &versionMajor); + glGetIntegerv(GL_MINOR_VERSION, &versionMinor); + SS_CORE_ASSERT(versionMajor > 4 || (versionMajor == 4 && versionMinor >= 5), "StarStudio requires at least OpenGL version 4.5!"); + #endif } void OpenGLContext::SwapBuffers() diff --git a/StarStudio/src/Platform/OpenGL/OpenGLRendererAPI.cpp b/StarStudio/src/Platform/OpenGL/OpenGLRendererAPI.cpp index 994ca42c..0126dcc7 100644 --- a/StarStudio/src/Platform/OpenGL/OpenGLRendererAPI.cpp +++ b/StarStudio/src/Platform/OpenGL/OpenGLRendererAPI.cpp @@ -9,6 +9,12 @@ namespace StarStudio { glEnable(GL_BLEND); glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); + glEnable(GL_DEPTH_TEST); + } + + void OpenGLRendererAPI::SetViewport(uint32_t x, uint32_t y, uint32_t width, uint32_t height) + { + glViewport(x, y, width, height); } void OpenGLRendererAPI::SetClearColor(const glm::vec4& color) @@ -22,5 +28,6 @@ namespace StarStudio void OpenGLRendererAPI::DrawIndexed(const Ref& vertexArray) { glDrawElements(GL_TRIANGLES, vertexArray->GetIndexBuffer()->GetCount(), GL_UNSIGNED_INT, nullptr); + glBindTexture(GL_TEXTURE_2D, 0); } } \ No newline at end of file diff --git a/StarStudio/src/Platform/OpenGL/OpenGLRendererAPI.h b/StarStudio/src/Platform/OpenGL/OpenGLRendererAPI.h index 9e1adc78..7226160f 100644 --- a/StarStudio/src/Platform/OpenGL/OpenGLRendererAPI.h +++ b/StarStudio/src/Platform/OpenGL/OpenGLRendererAPI.h @@ -9,6 +9,7 @@ namespace StarStudio { public: virtual void Init() override; + virtual void SetViewport(uint32_t x, uint32_t y, uint32_t width, uint32_t height) override; virtual void SetClearColor(const glm::vec4& color) override; virtual void Clear() override; diff --git a/StarStudio/src/Platform/OpenGL/OpenGLShader.cpp b/StarStudio/src/Platform/OpenGL/OpenGLShader.cpp index 62d3f061..7173a380 100644 --- a/StarStudio/src/Platform/OpenGL/OpenGLShader.cpp +++ b/StarStudio/src/Platform/OpenGL/OpenGLShader.cpp @@ -73,18 +73,20 @@ namespace StarStudio { const char* typeToken = "#type"; size_t typeTokenLength = strlen(typeToken); - size_t pos = source.find(typeToken, 0); + size_t pos = source.find(typeToken, 0); //Start of shader type declaration line while (pos != std::string::npos) { - size_t eol = source.find_first_of("\r\n", pos); + size_t eol = source.find_first_of("\r\n", pos); //End of shader type declaration line SS_CORE_ASSERT(eol != std::string::npos, "Syntax error"); - size_t begin = pos + typeTokenLength + 1; + size_t begin = pos + typeTokenLength + 1; //Start of shader type name (after "#type " keyword) std::string type = source.substr(begin, eol - begin); SS_CORE_ASSERT(ShaderTypeFromString(type), "Invalid shader type specified"); - size_t nextLinePos = source.find_first_not_of("\r\n", eol); - pos = source.find(typeToken, nextLinePos); - shaderSources[ShaderTypeFromString(type)] = source.substr(nextLinePos, pos - (nextLinePos == std::string::npos ? source.size() - 1 : nextLinePos)); + size_t nextLinePos = source.find_first_not_of("\r\n", eol); //Start of shader code after shader type declaration line + SS_CORE_ASSERT(nextLinePos != std::string::npos, "Syntax error"); + pos = source.find(typeToken, nextLinePos); //Start of next shader type declaration line + + shaderSources[ShaderTypeFromString(type)] = (pos == std::string::npos) ? source.substr(nextLinePos) : source.substr(nextLinePos, pos - nextLinePos); } return shaderSources; @@ -154,7 +156,10 @@ namespace StarStudio { } for (auto id : glShaderIDs) + { glDetachShader(program, id); + glDeleteShader(id); + } } @@ -168,6 +173,26 @@ namespace StarStudio { glUseProgram(0); } + void OpenGLShader::SetInt(const std::string& name, int value) + { + UploadUniformInt(name, value); + } + + void OpenGLShader::SetFloat3(const std::string& name, const glm::vec3& value) + { + UploadUniformFloat3(name, value); + } + + void OpenGLShader::SetFloat4(const std::string& name, const glm::vec4& value) + { + UploadUniformFloat4(name, value); + } + + void OpenGLShader::SetMat4(const std::string& name, const glm::mat4& value) + { + UploadUniformMat4(name, value); + } + void OpenGLShader::UploadUniformInt(const std::string& name, int value) { GLint location = glGetUniformLocation(m_RendererID, name.c_str()); diff --git a/StarStudio/src/Platform/OpenGL/OpenGLShader.h b/StarStudio/src/Platform/OpenGL/OpenGLShader.h index fca88ae2..bc6425a3 100644 --- a/StarStudio/src/Platform/OpenGL/OpenGLShader.h +++ b/StarStudio/src/Platform/OpenGL/OpenGLShader.h @@ -21,6 +21,11 @@ namespace StarStudio { virtual void Bind() const override; virtual void Unbind() const override; + virtual void SetInt(const std::string& name, int value) override; + virtual void SetFloat3(const std::string& name, const glm::vec3& value) override; + virtual void SetFloat4(const std::string& name, const glm::vec4& value) override; + virtual void SetMat4(const std::string& name, const glm::mat4& value) override; + virtual const std::string& GetName() const override { return m_Name; } void UploadUniformInt(const std::string& name, int value); diff --git a/StarStudio/src/Platform/OpenGL/OpenGLTexture.cpp b/StarStudio/src/Platform/OpenGL/OpenGLTexture.cpp index bdafec3a..725f34fc 100644 --- a/StarStudio/src/Platform/OpenGL/OpenGLTexture.cpp +++ b/StarStudio/src/Platform/OpenGL/OpenGLTexture.cpp @@ -3,10 +3,21 @@ #include "stb_image.h" -#include - namespace StarStudio { + OpenGLTexture2D::OpenGLTexture2D(uint32_t width, uint32_t height) + : m_Width(width), m_Height(height) + { + m_InternalFormat = GL_RGBA8; + m_DataFormat = GL_RGBA; + glCreateTextures(GL_TEXTURE_2D, 1, &m_RendererID); + glTextureStorage2D(m_RendererID, 1, m_InternalFormat, m_Width, m_Height); + glTextureParameteri(m_RendererID, GL_TEXTURE_MIN_FILTER, GL_LINEAR); + glTextureParameteri(m_RendererID, GL_TEXTURE_MAG_FILTER, GL_NEAREST); + glTextureParameteri(m_RendererID, GL_TEXTURE_WRAP_S, GL_REPEAT); + glTextureParameteri(m_RendererID, GL_TEXTURE_WRAP_T, GL_REPEAT); + } + OpenGLTexture2D::OpenGLTexture2D(const std::string& path) : m_Path(path) { @@ -31,6 +42,9 @@ namespace StarStudio { dataFormat = GL_RGB; } + m_InternalFormat = internalFormat; + m_DataFormat = dataFormat; + SS_CORE_ASSERT(internalFormat & dataFormat, "Format not supported!"); glCreateTextures(GL_TEXTURE_2D, 1, &m_RendererID); @@ -40,11 +54,21 @@ namespace StarStudio { glTextureParameteri(m_RendererID, GL_TEXTURE_MIN_FILTER, GL_LINEAR); glTextureParameteri(m_RendererID, GL_TEXTURE_MAG_FILTER, GL_NEAREST); + glTextureParameteri(m_RendererID, GL_TEXTURE_WRAP_S, GL_REPEAT); + glTextureParameteri(m_RendererID, GL_TEXTURE_WRAP_T, GL_REPEAT); + glTextureSubImage2D(m_RendererID, 0, 0, 0, m_Width, m_Height, dataFormat, GL_UNSIGNED_BYTE, data); stbi_image_free(data); } + void OpenGLTexture2D::SetData(void* data, uint32_t size) + { + uint32_t bpp = m_DataFormat == GL_RGBA ? 4 : 3; + SS_CORE_ASSERT(size == m_Width * m_Height * bpp, "Data must be entire texture!"); + glTextureSubImage2D(m_RendererID, 0, 0, 0, m_Width, m_Height, m_DataFormat, GL_UNSIGNED_BYTE, data); + } + OpenGLTexture2D::~OpenGLTexture2D() { glDeleteTextures(1, &m_RendererID); diff --git a/StarStudio/src/Platform/OpenGL/OpenGLTexture.h b/StarStudio/src/Platform/OpenGL/OpenGLTexture.h index 934a8727..52f987cc 100644 --- a/StarStudio/src/Platform/OpenGL/OpenGLTexture.h +++ b/StarStudio/src/Platform/OpenGL/OpenGLTexture.h @@ -2,12 +2,15 @@ #include "StarStudio/Renderer/Texture.h" +#include + namespace StarStudio { class OpenGLTexture2D : public Texture2D { public: + OpenGLTexture2D(uint32_t width, uint32_t height); OpenGLTexture2D(const std::string& path); virtual ~OpenGLTexture2D(); @@ -15,11 +18,15 @@ namespace StarStudio { virtual uint32_t GetWidth() const override { return m_Width; } virtual uint32_t GetHeight() const override { return m_Height; } + virtual void SetData(void* data, uint32_t size) override; + virtual void Bind(uint32_t slot = 0) const override; private: std::string m_Path; uint32_t m_Width, m_Height; uint32_t m_RendererID; + + GLenum m_InternalFormat, m_DataFormat; }; } \ No newline at end of file diff --git a/StarStudio/src/Platform/OpenGL/OpenGLVertexArray.cpp b/StarStudio/src/Platform/OpenGL/OpenGLVertexArray.cpp index cafa03ce..8c15b86f 100644 --- a/StarStudio/src/Platform/OpenGL/OpenGLVertexArray.cpp +++ b/StarStudio/src/Platform/OpenGL/OpenGLVertexArray.cpp @@ -58,7 +58,7 @@ namespace StarStudio { ShaderDataTypeToOpenGLBaseType(element.Type), element.Normalized ? GL_TRUE : GL_FALSE, layout.GetStride(), - (const void*)(intptr_t)element.Offset); + (const void*)element.Offset); m_VertexBufferIndex++; } diff --git a/StarStudio/src/Platform/Windows/WindowsInput.cpp b/StarStudio/src/Platform/Windows/WindowsInput.cpp index a029501c..e7b340e8 100644 --- a/StarStudio/src/Platform/Windows/WindowsInput.cpp +++ b/StarStudio/src/Platform/Windows/WindowsInput.cpp @@ -6,7 +6,7 @@ namespace StarStudio { - Input* Input::s_Instance = new WindowsInput(); + Scope Input::s_Instance = CreateScope(); bool WindowsInput::IsKeyPressedImpl(int keycode) { diff --git a/StarStudio/src/Platform/Windows/WindowsWindow.cpp b/StarStudio/src/Platform/Windows/WindowsWindow.cpp index 7ab2bd60..6428467c 100644 --- a/StarStudio/src/Platform/Windows/WindowsWindow.cpp +++ b/StarStudio/src/Platform/Windows/WindowsWindow.cpp @@ -9,7 +9,7 @@ namespace StarStudio { - static bool s_GLFWInitialized = false; + static uint8_t s_GLFWWindowCount = 0; static void GLFWErrorCallback(int error, const char* description) { @@ -39,18 +39,18 @@ namespace StarStudio { SS_CORE_INFO("Creating window {0} ({1}, {2})", props.Title, props.Width, props.Height); - if (!s_GLFWInitialized) + if (s_GLFWWindowCount == 0) { - // TODO: glfwTerminate on system shutdown + SS_CORE_INFO("Initializing GLFW"); int success = glfwInit(); SS_CORE_ASSERT(success, "Could not intialize GLFW!"); glfwSetErrorCallback(GLFWErrorCallback); - s_GLFWInitialized = true; } m_Window = glfwCreateWindow((int)props.Width, (int)props.Height, m_Data.Title.c_str(), nullptr, nullptr); + ++s_GLFWWindowCount; - m_Context = new OpenGLContext(m_Window); + m_Context = CreateScope(m_Window); m_Context->Init(); glfwSetWindowUserPointer(m_Window, &m_Data); @@ -146,6 +146,12 @@ namespace StarStudio { void WindowsWindow::Shutdown() { glfwDestroyWindow(m_Window); + + if (--s_GLFWWindowCount == 0) + { + SS_CORE_INFO("Terminating GLFW"); + glfwTerminate(); + } } void WindowsWindow::OnUpdate() diff --git a/StarStudio/src/Platform/Windows/WindowsWindow.h b/StarStudio/src/Platform/Windows/WindowsWindow.h index 8c087a96..b8ae4bed 100644 --- a/StarStudio/src/Platform/Windows/WindowsWindow.h +++ b/StarStudio/src/Platform/Windows/WindowsWindow.h @@ -29,7 +29,7 @@ namespace StarStudio { virtual void Shutdown(); private: GLFWwindow* m_Window; - GraphicsContext* m_Context; + Scope m_Context; struct WindowData { diff --git a/StarStudio/src/StarStudio.h b/StarStudio/src/StarStudio.h index 171be63e..95f6f24e 100644 --- a/StarStudio/src/StarStudio.h +++ b/StarStudio/src/StarStudio.h @@ -7,16 +7,17 @@ #include "StarStudio/Core/Timestep.h" +#include "StarStudio/ImGui/ImGuiLayer.h" + // ---Input------------------------- #include "StarStudio/Core/Input.h" #include "StarStudio/Core/KeyCodes.h" #include "StarStudio/Core/MouseButtonCodes.h" // --------------------------------- -#include "StarStudio/ImGui/ImGuiLayer.h" - // ---Renderer------------------------ #include "StarStudio/Renderer/Renderer.h" +#include "StarStudio/Renderer/Renderer2D.h" #include "StarStudio/Renderer/RenderCommand.h" #include "StarStudio/Renderer/Buffer.h" @@ -25,8 +26,4 @@ #include "StarStudio/Renderer/VertexArray.h" #include "StarStudio/Renderer/OrthographicCamera.h" -// ----------------------------------- - -// ---Entry Point--------------------- -#include "StarStudio/Core/EntryPoint.h" // ----------------------------------- \ No newline at end of file diff --git a/StarStudio/src/StarStudio/Core/Application.cpp b/StarStudio/src/StarStudio/Core/Application.cpp index ebcc9cea..386349a0 100644 --- a/StarStudio/src/StarStudio/Core/Application.cpp +++ b/StarStudio/src/StarStudio/Core/Application.cpp @@ -19,23 +19,15 @@ namespace StarStudio SS_CORE_ASSERT(!s_Instance, "Application already exists!"); s_Instance = this; - m_Window = Scope(Window::Create()); + m_Window = std::unique_ptr(Window::Create()); m_Window->SetEventCallback(BIND_EVENT_FN(OnEvent)); Renderer::Init(); - SS_CORE_INFO("In order to use StarStudio, you need the assets folder from Sandbox, in the root folder of the .exe"); - SS_CORE_INFO("If you don't have the assets folder, you can download it from the StarStudio repository"); - m_ImGuiLayer = new ImGuiLayer(); PushOverlay(m_ImGuiLayer); } - Application::~Application() - { - - } - void Application::PushLayer(Layer* layer) { m_LayerStack.PushLayer(layer); @@ -50,6 +42,7 @@ namespace StarStudio { EventDispatcher dispatcher(e); dispatcher.Dispatch(BIND_EVENT_FN(OnWindowClose)); + dispatcher.Dispatch(BIND_EVENT_FN(OnWindowResize)); for (auto it = m_LayerStack.end(); it != m_LayerStack.begin(); ) { @@ -67,15 +60,17 @@ namespace StarStudio Timestep timestep = time - m_LastFrameTime; m_LastFrameTime = time; - for (Layer* layer : m_LayerStack) - layer->OnUpdate(timestep); + if (!m_Minimized) + { + for (Layer* layer : m_LayerStack) + layer->OnUpdate(timestep); + } m_ImGuiLayer->Begin(); for (Layer* layer : m_LayerStack) layer->OnImGuiRender(); m_ImGuiLayer->End(); - m_Window->OnUpdate(); } } @@ -86,4 +81,16 @@ namespace StarStudio return true; } + bool Application::OnWindowResize(WindowResizeEvent& e) + { + if (e.GetWidth() == 0 || e.GetHeight() == 0) + { + m_Minimized = true; + return false; + } + m_Minimized = false; + Renderer::OnWindowResize(e.GetWidth(), e.GetHeight()); + return false; + } + } diff --git a/StarStudio/src/StarStudio/Core/Application.h b/StarStudio/src/StarStudio/Core/Application.h index a420cff4..35c62642 100644 --- a/StarStudio/src/StarStudio/Core/Application.h +++ b/StarStudio/src/StarStudio/Core/Application.h @@ -17,7 +17,7 @@ namespace StarStudio { public: Application(); - virtual ~Application(); + virtual ~Application() = default; void Run(); @@ -29,16 +29,16 @@ namespace StarStudio inline static Application& Get() { return *s_Instance; } private: bool OnWindowClose(WindowCloseEvent& e); - - Scope m_Window; + bool OnWindowResize(WindowResizeEvent& e); + private: + std::unique_ptr m_Window; ImGuiLayer* m_ImGuiLayer; - bool m_Running = true; + bool m_Minimized = false; LayerStack m_LayerStack; - float m_LastFrameTime = 0.0f; - private: - static Application* s_Instance; + private: + static Application* s_Instance; }; // To be defined in CLIENT diff --git a/StarStudio/src/StarStudio/Core/Core.h b/StarStudio/src/StarStudio/Core/Core.h index 707fb158..d4d100ac 100644 --- a/StarStudio/src/StarStudio/Core/Core.h +++ b/StarStudio/src/StarStudio/Core/Core.h @@ -2,42 +2,95 @@ #include +// Platform detection using predefined macros +#ifdef _WIN32 + /* Windows x64/x86 */ +#ifdef _WIN64 + /* Windows x64 */ +#define SS_PLATFORM_WINDOWS +#else + /* Windows x86 */ +#error "x86 Builds are not supported!" +#endif +#elif defined(__APPLE__) || defined(__MACH__) +#include +/* TARGET_OS_MAC exists on all the platforms + * so we must check all of them (in this order) + * to ensure that we're running on MAC + * and not some other Apple platform */ +#if TARGET_IPHONE_SIMULATOR == 1 +#error "IOS simulator is not supported!" +#elif TARGET_OS_IPHONE == 1 +#define SS_PLATFORM_IOS +#error "IOS is not supported!" +#elif TARGET_OS_MAC == 1 +#define SS_PLATFORM_MACOS +#error "MacOS is not supported!" +#else +#error "Unknown Apple platform!" +#endif + /* We also have to check __ANDROID__ before __linux__ + * since android is based on the linux kernel + * it has __linux__ defined */ +#elif defined(__ANDROID__) +#define SS_PLATFORM_ANDROID +#error "Android is not supported!" +#elif defined(__linux__) +#define SS_PLATFORM_LINUX +#error "Linux is not supported!" +#else + /* Unknown compiler/platform */ +#error "Unknown platform!" +#endif // End of platform detection + + +// DLL support #ifdef SS_PLATFORM_WINDOWS #if SS_DYNAMIC_LINK - #ifdef SS_BUILD_DLL - #define STARSTUDIO_API __declspec(dllexport) - #else - #define STARSTUDIO_API __declspec(dllimport) - #endif +#ifdef SS_BUILD_DLL +#define STARSTUDIO_API __declspec(dllexport) #else - #define STARSTUDIO_API +#define STARSTUDIO_API __declspec(dllimport) #endif #else - #error StarStudio only supports Windows! +#define STARSTUDIO_API #endif +#else +#error StarStudio only supports Windows! +#endif // End of DLL support #ifdef SS_DEBUG - #define SS_ENABLE_ASSERTS +#define SS_ENABLE_ASSERTS #endif #ifdef SS_ENABLE_ASSERTS - #define SS_ASSERT(x, ...) { if(!(x)) { SS_ERROR("Assertion Failed: {0}", __VA_ARGS__); __debugbreak(); } } - #define SS_CORE_ASSERT(x, ...) { if(!(x)) { SS_CORE_ERROR("Assertion Failed: {0}", __VA_ARGS__); __debugbreak(); } } +#define SS_ASSERT(x, ...) { if(!(x)) { SS_ERROR("Assertion Failed: {0}", __VA_ARGS__); __debugbreak(); } } +#define SS_CORE_ASSERT(x, ...) { if(!(x)) { SS_CORE_ERROR("Assertion Failed: {0}", __VA_ARGS__); __debugbreak(); } } #else - #define SS_ASSERT(x, ...) - #define SS_CORE_ASSERT(x, ...) +#define SS_ASSERT(x, ...) +#define SS_CORE_ASSERT(x, ...) #endif - #define BIT(x) (1 << x) + #define SS_BIND_EVENT_FN(fn) std::bind(&fn, this, std::placeholders::_1) -namespace StarStudio -{ +namespace StarStudio { + template using Scope = std::unique_ptr; + template + constexpr Scope CreateScope(Args&& ... args) + { + return std::make_unique(std::forward(args)...); + } template using Ref = std::shared_ptr; + template + constexpr Ref CreateRef(Args&& ... args) + { + return std::make_shared(std::forward(args)...); + } } \ No newline at end of file diff --git a/StarStudio/src/StarStudio/Core/Input.h b/StarStudio/src/StarStudio/Core/Input.h index 83ce3bc8..21bbdc5f 100644 --- a/StarStudio/src/StarStudio/Core/Input.h +++ b/StarStudio/src/StarStudio/Core/Input.h @@ -26,7 +26,7 @@ namespace StarStudio { virtual float GetMouseXImpl() = 0; virtual float GetMouseYImpl() = 0; private: - static Input* s_Instance; + static Scope s_Instance; }; } \ No newline at end of file diff --git a/StarStudio/src/StarStudio/Core/Layer.cpp b/StarStudio/src/StarStudio/Core/Layer.cpp index 2ab667ef..38ac5cb5 100644 --- a/StarStudio/src/StarStudio/Core/Layer.cpp +++ b/StarStudio/src/StarStudio/Core/Layer.cpp @@ -7,9 +7,5 @@ namespace StarStudio { : m_DebugName(debugName) { - } - Layer::~Layer() - { - } } diff --git a/StarStudio/src/StarStudio/Core/Layer.h b/StarStudio/src/StarStudio/Core/Layer.h index d78b24e0..c1054ab9 100644 --- a/StarStudio/src/StarStudio/Core/Layer.h +++ b/StarStudio/src/StarStudio/Core/Layer.h @@ -10,7 +10,7 @@ namespace StarStudio { public: Layer(const std::string& name = "Layer"); - virtual ~Layer(); + virtual ~Layer() = default; virtual void OnAttach() {} virtual void OnDetach() {} diff --git a/StarStudio/src/StarStudio/Events/Event.h b/StarStudio/src/StarStudio/Events/Event.h index fdc21280..4fcf30b4 100644 --- a/StarStudio/src/StarStudio/Events/Event.h +++ b/StarStudio/src/StarStudio/Events/Event.h @@ -29,7 +29,7 @@ namespace StarStudio { EventCategoryMouseButton = BIT(4) }; -#define EVENT_CLASS_TYPE(type) static EventType GetStaticType() { return EventType::##type; }\ +#define EVENT_CLASS_TYPE(type) static EventType GetStaticType() { return EventType::type; }\ virtual EventType GetEventType() const override { return GetStaticType(); }\ virtual const char* GetName() const override { return #type; } diff --git a/StarStudio/src/StarStudio/ImGui/ImGuiLayer.cpp b/StarStudio/src/StarStudio/ImGui/ImGuiLayer.cpp index c436bbec..e145e454 100644 --- a/StarStudio/src/StarStudio/ImGui/ImGuiLayer.cpp +++ b/StarStudio/src/StarStudio/ImGui/ImGuiLayer.cpp @@ -16,10 +16,7 @@ namespace StarStudio { ImGuiLayer::ImGuiLayer() : Layer("ImGuiLayer") { - } - ImGuiLayer::~ImGuiLayer() - { } void ImGuiLayer::OnAttach() diff --git a/StarStudio/src/StarStudio/ImGui/ImGuiLayer.h b/StarStudio/src/StarStudio/ImGui/ImGuiLayer.h index 019dff33..e3adb6e2 100644 --- a/StarStudio/src/StarStudio/ImGui/ImGuiLayer.h +++ b/StarStudio/src/StarStudio/ImGui/ImGuiLayer.h @@ -12,7 +12,7 @@ namespace StarStudio { { public: ImGuiLayer(); - ~ImGuiLayer(); + ~ImGuiLayer() = default; virtual void OnAttach() override; virtual void OnDetach() override; diff --git a/StarStudio/src/StarStudio/Renderer/Buffer.h b/StarStudio/src/StarStudio/Renderer/Buffer.h index 34e58079..0d9d413c 100644 --- a/StarStudio/src/StarStudio/Renderer/Buffer.h +++ b/StarStudio/src/StarStudio/Renderer/Buffer.h @@ -34,10 +34,10 @@ namespace StarStudio { std::string Name; ShaderDataType Type; uint32_t Size; - uint32_t Offset; + size_t Offset; bool Normalized; - BufferElement() {} + BufferElement() = default; BufferElement(ShaderDataType type, const std::string& name, bool normalized = false) : Name(name), Type(type), Size(ShaderDataTypeSize(type)), Offset(0), Normalized(normalized) { @@ -89,7 +89,7 @@ namespace StarStudio { private: void CalculateOffsetsAndStride() { - uint32_t offset = 0; + size_t offset = 0; m_Stride = 0; for (auto& element : m_Elements) @@ -107,7 +107,7 @@ namespace StarStudio { class VertexBuffer { public: - virtual ~VertexBuffer() {} + virtual ~VertexBuffer() = default; virtual void Bind() const = 0; virtual void Unbind() const = 0; @@ -121,7 +121,7 @@ namespace StarStudio { class IndexBuffer { public: - virtual ~IndexBuffer() {} + virtual ~IndexBuffer() = default; virtual void Bind() const = 0; virtual void Unbind() const = 0; diff --git a/StarStudio/src/StarStudio/Renderer/OrthographicCamera.cpp b/StarStudio/src/StarStudio/Renderer/OrthographicCamera.cpp index 32bd8d8b..1baabca6 100644 --- a/StarStudio/src/StarStudio/Renderer/OrthographicCamera.cpp +++ b/StarStudio/src/StarStudio/Renderer/OrthographicCamera.cpp @@ -11,6 +11,12 @@ namespace StarStudio { m_ViewProjectionMatrix = m_ProjectionMatrix * m_ViewMatrix; } + void OrthographicCamera::SetProjection(float left, float right, float bottom, float top) + { + m_ProjectionMatrix = glm::ortho(left, right, bottom, top, -1.0f, 1.0f); + m_ViewProjectionMatrix = m_ProjectionMatrix * m_ViewMatrix; + } + void OrthographicCamera::RecalculateViewMatrix() { glm::mat4 transform = glm::translate(glm::mat4(1.0f), m_Position) * diff --git a/StarStudio/src/StarStudio/Renderer/OrthographicCamera.h b/StarStudio/src/StarStudio/Renderer/OrthographicCamera.h index f78f63a7..d53703d3 100644 --- a/StarStudio/src/StarStudio/Renderer/OrthographicCamera.h +++ b/StarStudio/src/StarStudio/Renderer/OrthographicCamera.h @@ -2,6 +2,8 @@ #include +#include "glm/ext/matrix_clip_space.hpp" + namespace StarStudio { class OrthographicCamera @@ -15,6 +17,8 @@ namespace StarStudio { float GetRotation() const { return m_Rotation; } void SetRotation(float rotation) { m_Rotation = rotation; RecalculateViewMatrix(); } + void SetProjection(float left, float right, float bottom, float top); + const glm::mat4& GetProjectionMatrix() const { return m_ProjectionMatrix; } const glm::mat4& GetViewMatrix() const { return m_ViewMatrix; } const glm::mat4& GetViewProjectionMatrix() const { return m_ViewProjectionMatrix; } @@ -29,4 +33,4 @@ namespace StarStudio { float m_Rotation = 0.0f; }; -} \ No newline at end of file +} diff --git a/StarStudio/src/StarStudio/Renderer/OrthographicCameraController.cpp b/StarStudio/src/StarStudio/Renderer/OrthographicCameraController.cpp new file mode 100644 index 00000000..599d8bfa --- /dev/null +++ b/StarStudio/src/StarStudio/Renderer/OrthographicCameraController.cpp @@ -0,0 +1,79 @@ +#include "sspch.h" +#include "OrthographicCameraController.h" + +#include "StarStudio/Core/Input.h" +#include "StarStudio/Core/KeyCodes.h" + +namespace StarStudio { + + OrthographicCameraController::OrthographicCameraController(float aspectRatio, bool rotation) + : m_AspectRatio(aspectRatio), m_Camera(-m_AspectRatio * m_ZoomLevel, m_AspectRatio* m_ZoomLevel, -m_ZoomLevel, m_ZoomLevel), m_Rotation(rotation), m_CameraPosition(), m_CameraRotation() + { + + } + void OrthographicCameraController::OnUpdate(Timestep ts) + { + if (Input::IsKeyPressed(SS_KEY_A)) + { + m_CameraPosition.x -= cos(glm::radians(m_CameraRotation)) * m_CameraTranslationSpeed * ts; + m_CameraPosition.y -= sin(glm::radians(m_CameraRotation)) * m_CameraTranslationSpeed * ts; + } + else if (Input::IsKeyPressed(SS_KEY_D)) + { + m_CameraPosition.x += cos(glm::radians(m_CameraRotation)) * m_CameraTranslationSpeed * ts; + m_CameraPosition.y += sin(glm::radians(m_CameraRotation)) * m_CameraTranslationSpeed * ts; + } + + if (Input::IsKeyPressed(SS_KEY_W)) + { + m_CameraPosition.x += -sin(glm::radians(m_CameraRotation)) * m_CameraTranslationSpeed * ts; + m_CameraPosition.y += cos(glm::radians(m_CameraRotation)) * m_CameraTranslationSpeed * ts; + } + else if (Input::IsKeyPressed(SS_KEY_S)) + { + m_CameraPosition.x -= -sin(glm::radians(m_CameraRotation)) * m_CameraTranslationSpeed * ts; + m_CameraPosition.y -= cos(glm::radians(m_CameraRotation)) * m_CameraTranslationSpeed * ts; + } + + if (m_Rotation) + { + if (Input::IsKeyPressed(SS_KEY_Q)) + m_CameraRotation += m_CameraRotationSpeed * ts; + + if (Input::IsKeyPressed(SS_KEY_E)) + m_CameraRotation -= m_CameraRotationSpeed * ts; + + m_Camera.SetRotation(m_CameraRotation); + + if (m_CameraRotation > 180.0f) + m_CameraRotation -= 360.0f; + else if (m_CameraRotation <= -180.0f) + m_CameraRotation += 360.0f; + } + + m_Camera.SetPosition(m_CameraPosition); + m_CameraTranslationSpeed = m_ZoomLevel; + } + + void OrthographicCameraController::OnEvent(Event& e) + { + EventDispatcher dispatcher(e); + dispatcher.Dispatch(SS_BIND_EVENT_FN(OrthographicCameraController::OnMouseScrolled)); + dispatcher.Dispatch(SS_BIND_EVENT_FN(OrthographicCameraController::OnWindowResized)); + } + + bool OrthographicCameraController::OnMouseScrolled(MouseScrolledEvent& e) + { + m_ZoomLevel -= e.GetYOffset() * 0.25f; + m_ZoomLevel = std::max(m_ZoomLevel, 0.25f); + m_Camera.SetProjection(-m_AspectRatio * m_ZoomLevel, m_AspectRatio * m_ZoomLevel, -m_ZoomLevel, m_ZoomLevel); + return false; + } + + bool OrthographicCameraController::OnWindowResized(WindowResizeEvent& e) + { + m_AspectRatio = (float)e.GetWidth() / (float)e.GetHeight(); + m_Camera.SetProjection(-m_AspectRatio * m_ZoomLevel, m_AspectRatio * m_ZoomLevel, -m_ZoomLevel, m_ZoomLevel); + return false; + } +} \ No newline at end of file diff --git a/StarStudio/src/StarStudio/Renderer/OrthographicCameraController.h b/StarStudio/src/StarStudio/Renderer/OrthographicCameraController.h new file mode 100644 index 00000000..cd40e793 --- /dev/null +++ b/StarStudio/src/StarStudio/Renderer/OrthographicCameraController.h @@ -0,0 +1,41 @@ +#pragma once +#include "StarStudio/Renderer/OrthographicCamera.h" +#include "StarStudio/Core/Timestep.h" + +#include "StarStudio/Events/ApplicationEvent.h" +#include "StarStudio/Events/MouseEvent.h" + +namespace StarStudio { + + class OrthographicCameraController + { + + public: + OrthographicCameraController(float aspectRatio, bool rotation = false); + + void OnUpdate(Timestep ts); + void OnEvent(Event& e); + + OrthographicCamera& GetCamera() { return m_Camera; } + const OrthographicCamera& GetCamera() const { return m_Camera; } + + float GetZoomLevel() const { return m_ZoomLevel; } + void SetZoomLevel(float level) { m_ZoomLevel = level; } + private: + bool OnMouseScrolled(MouseScrolledEvent& e); + bool OnWindowResized(WindowResizeEvent& e); + private: + float m_AspectRatio; + float m_ZoomLevel = 1.0f; + + OrthographicCamera m_Camera; + + bool m_Rotation; + + glm::vec3 m_CameraPosition = { 0.0f, 0.0f, 0.0f }; + + float m_CameraRotation = 0.0f; + + float m_CameraTranslationSpeed = 5.0f, m_CameraRotationSpeed = 90.0f; + }; +} \ No newline at end of file diff --git a/StarStudio/src/StarStudio/Renderer/RenderCommand.cpp b/StarStudio/src/StarStudio/Renderer/RenderCommand.cpp index 5dc99ee6..ce3a29df 100644 --- a/StarStudio/src/StarStudio/Renderer/RenderCommand.cpp +++ b/StarStudio/src/StarStudio/Renderer/RenderCommand.cpp @@ -5,5 +5,5 @@ namespace StarStudio { - RendererAPI* RenderCommand::s_RendererAPI = new OpenGLRendererAPI(); + Scope RenderCommand::s_RendererAPI = CreateScope(); } \ No newline at end of file diff --git a/StarStudio/src/StarStudio/Renderer/RenderCommand.h b/StarStudio/src/StarStudio/Renderer/RenderCommand.h index 6357ee5b..f6307fae 100644 --- a/StarStudio/src/StarStudio/Renderer/RenderCommand.h +++ b/StarStudio/src/StarStudio/Renderer/RenderCommand.h @@ -12,6 +12,11 @@ namespace StarStudio s_RendererAPI->Init(); } + inline static void SetViewport(uint32_t x, uint32_t y, uint32_t width, uint32_t height) + { + s_RendererAPI->SetViewport(x, y, width, height); + } + inline static void SetClearColor(const glm::vec4& color) { s_RendererAPI->SetClearColor(color); @@ -26,6 +31,6 @@ namespace StarStudio s_RendererAPI->DrawIndexed(vertexArray); } private: - static RendererAPI* s_RendererAPI; + static Scope s_RendererAPI; }; } diff --git a/StarStudio/src/StarStudio/Renderer/Renderer.cpp b/StarStudio/src/StarStudio/Renderer/Renderer.cpp index bc768f0d..a6b54188 100644 --- a/StarStudio/src/StarStudio/Renderer/Renderer.cpp +++ b/StarStudio/src/StarStudio/Renderer/Renderer.cpp @@ -1,15 +1,24 @@ #include "sspch.h" + #include "Renderer.h" +#include "Renderer2D.h" +#include "Platform/OpenGL/OpenGLShader.h" #include "Platform/OpenGL/OpenGLShader.h" namespace StarStudio { - Renderer::SceneData* Renderer::s_SceneData = new Renderer::SceneData; + Scope Renderer::s_SceneData = CreateScope(); void Renderer::Init() { RenderCommand::Init(); + Renderer2D::Init(); + } + + void Renderer::OnWindowResize(uint32_t width, uint32_t height) + { + RenderCommand::SetViewport(0, 0, width, height); } void Renderer::BeginScene(OrthographicCamera& camera) diff --git a/StarStudio/src/StarStudio/Renderer/Renderer.h b/StarStudio/src/StarStudio/Renderer/Renderer.h index bab044de..bf927ab4 100644 --- a/StarStudio/src/StarStudio/Renderer/Renderer.h +++ b/StarStudio/src/StarStudio/Renderer/Renderer.h @@ -12,6 +12,7 @@ namespace StarStudio { { public: static void Init(); + static void OnWindowResize(uint32_t width, uint32_t height); static void BeginScene(OrthographicCamera& camera); static void EndScene(); @@ -24,6 +25,6 @@ namespace StarStudio { { glm::mat4 ViewProjectionMatrix; }; - static SceneData* s_SceneData; + static Scope s_SceneData; }; } diff --git a/StarStudio/src/StarStudio/Renderer/Renderer2D.cpp b/StarStudio/src/StarStudio/Renderer/Renderer2D.cpp new file mode 100644 index 00000000..42439918 --- /dev/null +++ b/StarStudio/src/StarStudio/Renderer/Renderer2D.cpp @@ -0,0 +1,105 @@ +#include "sspch.h" +#include "Renderer2D.h" + +#include "StarStudio/Renderer/VertexArray.h" +#include "StarStudio/Renderer/Shader.h" +#include "StarStudio/Renderer/RenderCommand.h" + +#include + +namespace StarStudio { + + struct Renderer2DStorage + { + Ref QuadVertexArray; + Ref TextureShader; + Ref WhiteTexture; + }; + + static Renderer2DStorage* s_Data; + + void Renderer2D::Init() + { + s_Data = new Renderer2DStorage(); + s_Data->QuadVertexArray = VertexArray::Create(); + + float squareVertices[5 * 4] = { + -0.5f, -0.5f, 0.0f, 0.0f, 0.0f, + 0.5f, -0.5f, 0.0f, 1.0f, 0.0f, + 0.5f, 0.5f, 0.0f, 1.0f, 1.0f, + -0.5f, 0.5f, 0.0f, 0.0f, 1.0f + }; + + Ref squareVB; + squareVB.reset(VertexBuffer::Create(squareVertices, sizeof(squareVertices))); + squareVB->SetLayout({ + { ShaderDataType::Float3, "a_Position" }, + { ShaderDataType::Float2, "a_TexCoord" } + }); + s_Data->QuadVertexArray->AddVertexBuffer(squareVB); + + uint32_t squareIndices[6] = { 0, 1, 2, 2, 3, 0 }; + Ref squareIB; + squareIB.reset(IndexBuffer::Create(squareIndices, sizeof(squareIndices) / sizeof(uint32_t))); + s_Data->QuadVertexArray->SetIndexBuffer(squareIB); + + s_Data->WhiteTexture = Texture2D::Create(1, 1); + uint32_t whiteTextureData = 0xffffffff; + s_Data->WhiteTexture->SetData(&whiteTextureData, sizeof(uint32_t)); + + s_Data->TextureShader = Shader::Create("assets/shaders/Texture.glsl"); + s_Data->TextureShader->Bind(); + s_Data->TextureShader->SetInt("u_Texture", 0); + } + + void Renderer2D::Shutdown() + { + delete s_Data; + } + + void Renderer2D::BeginScene(const OrthographicCamera& camera) + { + s_Data->TextureShader->Bind(); + s_Data->TextureShader->SetMat4("u_ViewProjection", camera.GetViewProjectionMatrix()); + } + + void Renderer2D::EndScene() + { + + } + + void Renderer2D::DrawQuad(const glm::vec2& position, const glm::vec2& size, const glm::vec4& color) + { + DrawQuad({ position.x, position.y, 0.0f }, size, color); + } + + void Renderer2D::DrawQuad(const glm::vec3& position, const glm::vec2& size, const glm::vec4& color) + { + s_Data->TextureShader->SetFloat4("u_Color", color); + s_Data->WhiteTexture->Bind(); + + glm::mat4 transform = glm::translate(glm::mat4(1.0f), position) * glm::scale(glm::mat4(1.0f), { size.x, size.y, 1.0f }); + s_Data->TextureShader->SetMat4("u_Transform", transform); + + s_Data->QuadVertexArray->Bind(); + RenderCommand::DrawIndexed(s_Data->QuadVertexArray); + } + + void Renderer2D::DrawQuad(const glm::vec2& position, const glm::vec2& size, const Ref& texture) + { + DrawQuad({ position.x, position.y, 0.0f }, size, texture); + } + + void Renderer2D::DrawQuad(const glm::vec3& position, const glm::vec2& size, const Ref& texture) + { + s_Data->TextureShader->SetFloat4("u_Color", glm::vec4(1.0f)); + texture->Bind(); + + glm::mat4 transform = glm::translate(glm::mat4(1.0f), position) * glm::scale(glm::mat4(1.0f), { size.x, size.y, 1.0f }); + s_Data->TextureShader->SetMat4("u_Transform", transform); + + s_Data->QuadVertexArray->Bind(); + + RenderCommand::DrawIndexed(s_Data->QuadVertexArray); + } +} diff --git a/StarStudio/src/StarStudio/Renderer/Renderer2D.h b/StarStudio/src/StarStudio/Renderer/Renderer2D.h new file mode 100644 index 00000000..a65d8272 --- /dev/null +++ b/StarStudio/src/StarStudio/Renderer/Renderer2D.h @@ -0,0 +1,24 @@ +#pragma once + +#include "OrthographicCamera.h" + +#include "Texture.h" + +namespace StarStudio { + class Renderer2D + { + public: + + static void Init(); + static void Shutdown(); + + static void BeginScene(const OrthographicCamera& camera); + static void EndScene(); + + // Primitives + static void DrawQuad(const glm::vec2& position, const glm::vec2& size, const glm::vec4& color); + static void DrawQuad(const glm::vec3& position, const glm::vec2& size, const glm::vec4& color); + static void DrawQuad(const glm::vec2& position, const glm::vec2& size, const Ref& texture); + static void DrawQuad(const glm::vec3& position, const glm::vec2& size, const Ref& texture); + }; +} diff --git a/StarStudio/src/StarStudio/Renderer/RendererAPI.h b/StarStudio/src/StarStudio/Renderer/RendererAPI.h index a2146902..1d8ce5c8 100644 --- a/StarStudio/src/StarStudio/Renderer/RendererAPI.h +++ b/StarStudio/src/StarStudio/Renderer/RendererAPI.h @@ -14,6 +14,7 @@ namespace StarStudio }; public: virtual void Init() = 0; + virtual void SetViewport(uint32_t x, uint32_t y, uint32_t width, uint32_t height) = 0; virtual void SetClearColor(const glm::vec4& color) = 0; virtual void Clear() = 0; diff --git a/StarStudio/src/StarStudio/Renderer/Shader.cpp b/StarStudio/src/StarStudio/Renderer/Shader.cpp index 55c1c558..8ed8b8bb 100644 --- a/StarStudio/src/StarStudio/Renderer/Shader.cpp +++ b/StarStudio/src/StarStudio/Renderer/Shader.cpp @@ -43,21 +43,21 @@ namespace StarStudio { Add(name, shader); } - Ref ShaderLibrary::Load(const std::string& filepath) + Ref ShaderLibrary::Load(const std::string& filepath) { auto shader = Shader::Create(filepath); Add(shader); return shader; } - Ref ShaderLibrary::Load(const std::string& name, const std::string& filepath) + Ref ShaderLibrary::Load(const std::string& name, const std::string& filepath) { auto shader = Shader::Create(filepath); Add(name, shader); return shader; } - Ref ShaderLibrary::Get(const std::string& name) + Ref ShaderLibrary::Get(const std::string& name) { SS_CORE_ASSERT(Exists(name), "Shader not found!"); return m_Shaders[name]; diff --git a/StarStudio/src/StarStudio/Renderer/Shader.h b/StarStudio/src/StarStudio/Renderer/Shader.h index 46738ae2..0bf28fe7 100644 --- a/StarStudio/src/StarStudio/Renderer/Shader.h +++ b/StarStudio/src/StarStudio/Renderer/Shader.h @@ -3,6 +3,8 @@ #include #include +#include + namespace StarStudio { class Shader @@ -13,6 +15,11 @@ namespace StarStudio { virtual void Bind() const = 0; virtual void Unbind() const = 0; + virtual void SetInt(const std::string& name, int value) = 0; + virtual void SetFloat3(const std::string& name, const glm::vec3& value) = 0; + virtual void SetFloat4(const std::string& name, const glm::vec4& value) = 0; + virtual void SetMat4(const std::string& name, const glm::mat4& value) = 0; + virtual const std::string& GetName() const = 0; static Ref Create(const std::string& filepath); static Ref Create(const std::string& name, const std::string& vertexSrc, const std::string& fragmentSrc); diff --git a/StarStudio/src/StarStudio/Renderer/Texture.cpp b/StarStudio/src/StarStudio/Renderer/Texture.cpp index 0c0c111c..e99f9bdb 100644 --- a/StarStudio/src/StarStudio/Renderer/Texture.cpp +++ b/StarStudio/src/StarStudio/Renderer/Texture.cpp @@ -6,12 +6,23 @@ namespace StarStudio { + Ref Texture2D::Create(uint32_t width, uint32_t height) + { + switch (Renderer::GetAPI()) + { + case RendererAPI::API::None: SS_CORE_ASSERT(false, "RendererAPI::None is currently not supported!"); return nullptr; + case RendererAPI::API::OpenGL: return CreateRef(width, height); + } + SS_CORE_ASSERT(false, "Unknown RendererAPI!"); + return nullptr; + } + Ref Texture2D::Create(const std::string& path) { switch (Renderer::GetAPI()) { - case RendererAPI::API::None: SS_CORE_ASSERT(false, "RendererAPI::None is currently not supported") return nullptr; - case RendererAPI::API::OpenGL: return std::make_shared(path); + case RendererAPI::API::None: SS_CORE_ASSERT(false, "RendererAPI::None is currently not supported") return nullptr; + case RendererAPI::API::OpenGL: return CreateRef(path); } SS_CORE_ASSERT(false, "Unknown RendererAPI!"); diff --git a/StarStudio/src/StarStudio/Renderer/Texture.h b/StarStudio/src/StarStudio/Renderer/Texture.h index ae92c3c3..0719f95f 100644 --- a/StarStudio/src/StarStudio/Renderer/Texture.h +++ b/StarStudio/src/StarStudio/Renderer/Texture.h @@ -10,8 +10,12 @@ namespace StarStudio{ { public: virtual ~Texture() = default; + virtual uint32_t GetWidth() const = 0; virtual uint32_t GetHeight() const = 0; + + virtual void SetData(void* data, uint32_t size) = 0; + virtual void Bind(uint32_t slot = 0) const = 0; }; diff --git a/StarStudio/src/StarStudio/Renderer/VertexArray.cpp b/StarStudio/src/StarStudio/Renderer/VertexArray.cpp index 40d13d80..0bf3fb74 100644 --- a/StarStudio/src/StarStudio/Renderer/VertexArray.cpp +++ b/StarStudio/src/StarStudio/Renderer/VertexArray.cpp @@ -6,12 +6,12 @@ namespace StarStudio { - VertexArray* VertexArray::Create() { + Ref VertexArray::Create() { switch (Renderer::GetAPI()) { case RendererAPI::API::None: SS_CORE_ASSERT(false, "RendererAPI::None is currently not supported") return nullptr; - case RendererAPI::API::OpenGL: return new OpenGLVertexArray(); + case RendererAPI::API::OpenGL: return std::make_shared(); } SS_CORE_ASSERT(false, "Unknown RendererAPI!"); diff --git a/StarStudio/src/StarStudio/Renderer/VertexArray.h b/StarStudio/src/StarStudio/Renderer/VertexArray.h index e9356e9e..2991dc26 100644 --- a/StarStudio/src/StarStudio/Renderer/VertexArray.h +++ b/StarStudio/src/StarStudio/Renderer/VertexArray.h @@ -19,6 +19,6 @@ namespace StarStudio { virtual const std::vector>& GetVertexBuffers() const = 0; virtual const Ref& GetIndexBuffer() const = 0; - static VertexArray* Create(); + static Ref Create(); }; } diff --git a/premake5.lua b/premake5.lua index 84969be6..b337b7b1 100644 --- a/premake5.lua +++ b/premake5.lua @@ -85,7 +85,6 @@ project "StarStudio" defines { - "SS_PLATFORM_WINDOWS", "SS_BUILD_DLL", "GLFW_INCLUDE_NONE" } @@ -137,13 +136,6 @@ project "Sandbox" "StarStudio" } - filter "system:windows" - systemversion "latest" - defines - { - "SS_PLATFORM_WINDOWS" - } - filter "configurations:Debug" defines "SS_DEBUG" runtime "Debug"