From dab01f15415ebdfa4823766817ed2cdf30981440 Mon Sep 17 00:00:00 2001 From: Nigel Date: Wed, 20 Apr 2022 21:40:35 +0200 Subject: [PATCH] Initial setup. Correctly linking glfw and spdlog... Linking could be improved by not needing the sandbox application to also be linked with glfw --- .gitignore | 6 +- .gitmodules | 3 - MyGraphicsEngine/glWindow.c | 135 ------------------ .../include/MyGraphicsEngine/Graphics.h | 9 ++ .../include/MyGraphicsEngine/window.h | 22 +++ .../{Shader => shaders}/fragment.shader | 0 .../{Shader => shaders}/vertex.shader | 0 MyGraphicsEngine/window.cpp | 47 ++++++ SandboxApplication/Sandbox.cpp | 11 ++ SandboxApplication/hello.cpp | 69 --------- SandboxApplication/hello.lua | 14 -- activate.sh | 4 - premake5.lua | 66 +++++---- 13 files changed, 132 insertions(+), 254 deletions(-) delete mode 100644 .gitmodules delete mode 100644 MyGraphicsEngine/glWindow.c create mode 100644 MyGraphicsEngine/include/MyGraphicsEngine/Graphics.h create mode 100644 MyGraphicsEngine/include/MyGraphicsEngine/window.h rename MyGraphicsEngine/{Shader => shaders}/fragment.shader (100%) rename MyGraphicsEngine/{Shader => shaders}/vertex.shader (100%) create mode 100644 MyGraphicsEngine/window.cpp create mode 100644 SandboxApplication/Sandbox.cpp delete mode 100644 SandboxApplication/hello.cpp delete mode 100644 SandboxApplication/hello.lua delete mode 100755 activate.sh diff --git a/.gitignore b/.gitignore index e64a995..b4bebf5 100644 --- a/.gitignore +++ b/.gitignore @@ -1,5 +1,7 @@ -artifacts/ +build/ +intermediates/ +tools/ *.make -premake5 Makefile .vscode/ +libs/ \ No newline at end of file diff --git a/.gitmodules b/.gitmodules deleted file mode 100644 index cf6778e..0000000 --- a/.gitmodules +++ /dev/null @@ -1,3 +0,0 @@ -[submodule "ogre"] - path = libs/ogre - url = https://github.com/OGRECave/ogre.git diff --git a/MyGraphicsEngine/glWindow.c b/MyGraphicsEngine/glWindow.c deleted file mode 100644 index 9d464af..0000000 --- a/MyGraphicsEngine/glWindow.c +++ /dev/null @@ -1,135 +0,0 @@ -#include -#include "glfw3.h" -//#include "spdlog.h" -GLFWwindow * window; - -int main (void) { - -// spdlog::info("Welcome to spdlog!"); - - if ( !glfwInit()){ - puts("Failed to initialize GLFW!"); - return -1; - } - - - window = glfwCreateWindow( 640 , 480, "BarinkEngine", NULL, NULL); - - if( !window){ - puts("Failed to create the window"); - glfwTerminate(); - return -1; - } - - - - glfwMakeContextCurrent(window); - - if(glfwVulkanSupported()) - { - puts("Vulkan is supported!"); - } - - - int width,height; - glfwGetFramebufferSize(window, &width, &height); - glViewport(0,0, width, height); - -// SOURCE: https://learnopengl.com/Getting-started/Hello-Triangle - - - float vertices[] = { - -0.5, -0.5f, 0.0f, - 0.5, -0.5f, 0.0f, - 0.5, 0.5f, 0.0f, - - }; - - unsigned int VBO; - glGenBuffers(1,&VBO); - - glBindBuffer(GL_ARRAY_BUFFER, VBO); - - glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW); - - // Load the vertex shader program - const char *vertexShaderSource = "#version 330 core\n" - "layout (location = 0) in vec3 aPos;\n" - "void main()\n" - "{\n" - " gl_Position = vec4(aPos.x, aPos.y, aPos.z, 1.0);\n" - "}\0"; - - unsigned int vertexShader; - vertexShader = glCreateShader(GL_VERTEX_SHADER); - glShaderSource(vertexShader, 1, &vertexShaderSource, NULL); - glCompileShader(vertexShader); - - const char *fragmentShaderSource = "#version 330 core\n" - "out vec4 FragColor;\n" - "void main ()\n {\n" - "FragColor = vec4(1.0f, 0.5f, 0.2f, 1.0f); \n" - "}\n"; - - unsigned int fragmentShader ; - fragmentShader = glCreateShader(GL_FRAGMENT_SHADER); - glShaderSource(fragmentShader, 1, &fragmentShaderSource, NULL); - glCompileShader(fragmentShader); - - unsigned int shaderProgram; - shaderProgram = glCreateProgram(); - - glAttachShader (shaderProgram, vertexShader); - glAttachShader(shaderProgram, fragmentShader); - glLinkProgram(shaderProgram); - - glUseProgram(shaderProgram); - - glVertexAttribPointer(0,3,GL_FLOAT,GL_FALSE,3*sizeof(float), (void*)0); - glEnableVertexAttribArray(0); - - // Copy our vertices array in a buffer for OpenGL to use - glBindBuffer(GL_ARRAY_BUFFER, VBO); - glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW); - - // then set the vertex attributes pointers - glVertexAttribPointer(0,3,GL_FLOAT,GL_FALSE,3*sizeof(float), (void*)0); - glEnableVertexAttribArray(0); - - - unsigned int VAO; - glGenVertexArrays(1,&VAO); - - glBindVertexArray(VAO); - - // use our hader program when we want to render an object - glUseProgram(shaderProgram); - glBindVertexArray(VAO); - - - while(!glfwWindowShouldClose(window)){ - /* Start rendering here */ - glClearColor(0.2f, 0.3f, 0.3f, 1.0f); - glClear(GL_COLOR_BUFFER_BIT); - - - glDrawArrays(GL_TRIANGLES, 0, 3); - - - /* Swap front and back buffers */ - glfwSwapBuffers(window); - - /* Poll for and process events */ - glfwPollEvents(); - } - - - glDeleteShader(vertexShader); - glDeleteShader(fragmentShader); - - - glfwTerminate(); - - - return 0; -} diff --git a/MyGraphicsEngine/include/MyGraphicsEngine/Graphics.h b/MyGraphicsEngine/include/MyGraphicsEngine/Graphics.h new file mode 100644 index 0000000..075860b --- /dev/null +++ b/MyGraphicsEngine/include/MyGraphicsEngine/Graphics.h @@ -0,0 +1,9 @@ +#pragma once +#include +#include "MyGraphicsEngine/window.h" + + +inline void test (){ + spdlog::info("Linked correctly!"); + +} \ No newline at end of file diff --git a/MyGraphicsEngine/include/MyGraphicsEngine/window.h b/MyGraphicsEngine/include/MyGraphicsEngine/window.h new file mode 100644 index 0000000..60d3f06 --- /dev/null +++ b/MyGraphicsEngine/include/MyGraphicsEngine/window.h @@ -0,0 +1,22 @@ +#pragma once +#include + + +class BarinkWindow{ + private: + GLFWwindow* window; + bool FullScreen; + bool VulkanSupported; + int Width, Height; + + bool InitGLFW(); + + + public: + BarinkWindow(const int width, const int height); + ~BarinkWindow(); + + void EnterLoop(); + + +}; \ No newline at end of file diff --git a/MyGraphicsEngine/Shader/fragment.shader b/MyGraphicsEngine/shaders/fragment.shader similarity index 100% rename from MyGraphicsEngine/Shader/fragment.shader rename to MyGraphicsEngine/shaders/fragment.shader diff --git a/MyGraphicsEngine/Shader/vertex.shader b/MyGraphicsEngine/shaders/vertex.shader similarity index 100% rename from MyGraphicsEngine/Shader/vertex.shader rename to MyGraphicsEngine/shaders/vertex.shader diff --git a/MyGraphicsEngine/window.cpp b/MyGraphicsEngine/window.cpp new file mode 100644 index 0000000..7e462ac --- /dev/null +++ b/MyGraphicsEngine/window.cpp @@ -0,0 +1,47 @@ +#include "MyGraphicsEngine/window.h" + + +bool BarinkWindow::InitGLFW(){ + if(!glfwInit()){ + return false; + } + return true; +} + +BarinkWindow::BarinkWindow(const int width, const int height) : +Width(width), Height(height), FullScreen(false){ + InitGLFW(); + + window = glfwCreateWindow(Width, Height, "BarinkEngine", NULL, NULL); + + if( !window){ + glfwTerminate(); + return; + } + + glfwMakeContextCurrent(window); + + VulkanSupported = glfwVulkanSupported(); + + glfwGetFramebufferSize(window, &Width, &Height); + glViewport(0,0, Width, Height); + + +} + + +BarinkWindow::~BarinkWindow(){ + + glfwTerminate(); +} + +void BarinkWindow::EnterLoop(){ + while(!glfwWindowShouldClose(window)) + { + glClear(GL_COLOR_BUFFER_BIT); + + glfwSwapBuffers(window); + glfwPollEvents(); + + } +} \ No newline at end of file diff --git a/SandboxApplication/Sandbox.cpp b/SandboxApplication/Sandbox.cpp new file mode 100644 index 0000000..2f293dd --- /dev/null +++ b/SandboxApplication/Sandbox.cpp @@ -0,0 +1,11 @@ +#include + +int main (int argc, char *argv[] ){ + + test(); + + BarinkWindow GameWindow(800, 600); + + GameWindow.EnterLoop(); +} + diff --git a/SandboxApplication/hello.cpp b/SandboxApplication/hello.cpp deleted file mode 100644 index da4865c..0000000 --- a/SandboxApplication/hello.cpp +++ /dev/null @@ -1,69 +0,0 @@ -#include -#include - -extern "c" { - static int l_cppfunction(lua_State *L) { - double arg = luaL_checknumber(L,1); - lua_pushnumber(L, arg * 0.5); - return 1; - } -} - - -int main (int argc, char *argv[] ){ - printf("Test lua embedding"); - printf("init lua"); - lua_State *L; - L = luaL_newState(); - printf("Load the (optional) standard libraries, to have to print function"); - luaL_openLibs(L); - printf("Load chenk. without executing it."); - if( luaL_loadfile(L, "hello.lua")){ - printf("Something went wrong loading the check (syntax error?)"); - printf(lua_tostring(L, -1)); - lua_pop(L,1); - } - - printf("Make a insert a global var into lua from C++"); - lua_pushnumber(L, 1.1); - lua_setglobal(L, "cppvar"); - - printf("Execute the Lua chunk"); - if(lua_pcall(L, 0, LUA_MULTRET, 0)){ - printf("Something went wrong during execution"); - printf(lua_tostring(L, -1)); - lua_pop(L,1); - } - - printf("read a global var from lua into C++"); - lua_getglobal(L, "luavar"); - double luavar = lua_tonumber(L, -1); - lua_pop(L,1); - printf("c++ can read the value set from lua luavar = %s", luavar ); - - printf("execute a lua function from cpp"); - lua_getglobal(L, "myluafunction"); - lua_pushnumber(L, 5); - lua_pcall(L,1,1,0); - printf("the return value of the function was %s", lua_tostring(L, -1)); - lua_pop(L,1); - - printf("execute a cpp function from lua"); - printf("first register the function in lua"); - lua_pushcfunction(L , l_cppfunction); - lua_setglobal(L, "cppfunction"); - - printf("Call a lua function that uses the cpp function"); - lua_setglobal(L, "myFunction"); - lua_pushnumber(L, 5); - lua_pcall(L,1,1,0); - printf("the return value of the function was %s", lua_tonumber(L,-1)); - lua_pop(L,1); - - printf("Release the lua environment"); - lua_close(L); - - - -} - diff --git a/SandboxApplication/hello.lua b/SandboxApplication/hello.lua deleted file mode 100644 index 4a7be95..0000000 --- a/SandboxApplication/hello.lua +++ /dev/null @@ -1,14 +0,0 @@ --- Pack this into object file with ld: ld -r -b binary -o hello.o hello.lua - -print ("Hello from lua") -print ("Lua code is capable of reading value set from C++", cppvar) -luavar = cppvar * 3 - -function myluafunction (times) - return string.rep("(-)", times) -end - -function myfunction(arg) - return cppfunction(arg) -end - diff --git a/activate.sh b/activate.sh deleted file mode 100755 index 1b18b59..0000000 --- a/activate.sh +++ /dev/null @@ -1,4 +0,0 @@ -#!/bin/bash -echo "BarinkEngine Terminal" -echo "Loading environment..." -source ./scripts/load.env \ No newline at end of file diff --git a/premake5.lua b/premake5.lua index 627e335..2b8f040 100644 --- a/premake5.lua +++ b/premake5.lua @@ -1,51 +1,63 @@ workspace "BarinkEngine" configurations { "Debug", "Test", "Release" } + language "C++" + cppdialect "C++17" + targetdir "./build/%{prj.name}/%{cfg.buildcfg}" + objdir "./intermediates/%{prj.name}/%{cfg.buildcfg}" + project "SandboxApplication" kind "ConsoleApp" - language "C++" - cppdialect "C++17" - targetdir "./artifacts/build/%{cfg.buildcfg}" - objdir "./artifacts/obj/%{cfg.buildcfg}" + + buildmessage "Building BarinkEngineSandbox ..." includedirs { - "./libs/lua-5.4.3/", - + "./MyGraphicsEngine/include" } - libdirs { - os.findlib("lua") + + + libdirs{ + "./libs/spdlog-1.9.1/build" + } + + links{ + "spdlog", + "glfw3", + "X11", + "GL", + "GLU", + "pthread", + "dl", + "m", + "MyGraphicsEngine" + } + + files { + "SandboxApplication/*.cpp" } - files { - "SandboxApplication/*.c" - } - - links{ - "lua" - } project "MyGraphicsEngine" - kind "ConsoleApp" - language "C++" - cppdialect "c++17" - targetdir "./artifacts/build/%{cfg.buildcfg}" - objdir "./artifacts/obj/%{cfg.buildcfg}" - buildmessage "Building MyGraphicsEngine" + kind "StaticLib" + + buildmessage "Building MyGraphicsEngine ..." includedirs { - "./libs/glfw-3.3.4/include/GLFW/", - "./libs/spdlog-1.9.1/include/spdlog/" + "./libs/glfw-3.3.4/include", + "./libs/spdlog-1.9.1/include", + "./MyGraphicsEngine/include" } + libdirs{ - "./libs/spdlog-1.9.1/build/libspdlog.a", - os.findlib("glfw3"), + "./libs/spdlog-1.9.1/build" } + links { - -- "libspdlog", + "libspdlog", "glfw3", "X11", "GL", @@ -55,7 +67,7 @@ workspace "BarinkEngine" "m" } - files {"MyGraphicsEngine/*.c"} + files {"MyGraphicsEngine/*.cpp"} filter "configurations:Debug" defines {"DEBUG"}