From 0ac7d6e7aa805f12f4fda8f4fa2cf1cf0a44c459 Mon Sep 17 00:00:00 2001 From: Furkan Mudanyali Date: Sun, 28 May 2023 00:07:50 +0300 Subject: [PATCH] Crude OpenGL triangle example --- CMakeLists.txt | 6 ++- README.md | 18 ++++++++ src/game/game.cpp | 8 +++- src/game/game.h | 2 + src/graphics/graphics.h | 19 ++++++++ src/graphics/opengl/opengl.cpp | 82 ++++++++++++++++++++++++++++++++++ src/graphics/opengl/opengl.h | 25 +++++++++++ src/scene/example/example.cpp | 16 +++++++ src/scene/example/example.h | 12 +++++ src/scene/scene.cpp | 6 --- src/scene/scene.h | 7 ++- 11 files changed, 191 insertions(+), 10 deletions(-) create mode 100644 README.md create mode 100644 src/graphics/graphics.h create mode 100644 src/graphics/opengl/opengl.cpp create mode 100644 src/graphics/opengl/opengl.h create mode 100644 src/scene/example/example.cpp create mode 100644 src/scene/example/example.h delete mode 100644 src/scene/scene.cpp diff --git a/CMakeLists.txt b/CMakeLists.txt index fbd1344..176e2c4 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -67,9 +67,11 @@ SET(CMAKE_CXX_FLAGS SET(SOURCE_FILES src/game/game.cpp + src/graphics/opengl/opengl.cpp + src/input/input.cpp - src/scene/scene.cpp + src/scene/example/example.cpp src/window/window.cpp @@ -112,6 +114,8 @@ ENDIF() TARGET_LINK_LIBRARIES(HomdEngine sdl2 + glew32 + opengl32 ${win32_link} ) diff --git a/README.md b/README.md new file mode 100644 index 0000000..5d28e98 --- /dev/null +++ b/README.md @@ -0,0 +1,18 @@ +# Setting Up Environment for Windows + +- Install MSYS2 from [here](https://www.msys2.org/) to `C:\msys64` +- Once installed, open MSYS2 CLANG64 and run the following command: + +``` +pacman -S mingw-w64-clang-x86_64-toolchain mingw-w64-clang-x86_64-cmake mingw-w64-clang-x86_64-SDL2 mingw-w64-clang-x86_64-glew +``` + +- Add `C:\msys64\clang64\bin` to your `Path` from Environment Variables. +- Run the following steps: + +``` +mkdir build +cd build +cmake .. +ninja +``` diff --git a/src/game/game.cpp b/src/game/game.cpp index b5907c6..2a0563e 100644 --- a/src/game/game.cpp +++ b/src/game/game.cpp @@ -4,12 +4,14 @@ */ #include -#include +#include +#include Game::Game() { this->pWindow = new Window(this); + this->pRenderer = new OpenGL(this); this->pInput = new Input(this); - this->scenes.push(new Scene); + this->scenes.push(new Example(this)); } void Game::loop() { @@ -22,5 +24,7 @@ void Game::loop() { this->scenes.pop(); continue; } + + this->scenes.top()->draw(); } } \ No newline at end of file diff --git a/src/game/game.h b/src/game/game.h index df8055a..136bd59 100644 --- a/src/game/game.h +++ b/src/game/game.h @@ -11,6 +11,7 @@ #include class Scene; +class Graphics; class Game { bool done = false; @@ -19,6 +20,7 @@ class Game { public: Window* pWindow; Input* pInput; + Graphics* pRenderer; Game(); ~Game() = default; diff --git a/src/graphics/graphics.h b/src/graphics/graphics.h new file mode 100644 index 0000000..3bf35cd --- /dev/null +++ b/src/graphics/graphics.h @@ -0,0 +1,19 @@ +/** + * Copyright (c) 2023, Furkan Mudanyali + * All rights reserved + */ + +#ifndef _HOMD_GRAPHICS +#define _HOMD_GRAPHICS + +class Game; + +class Graphics { + public: + Game* game; + Graphics() = default; + virtual ~Graphics() = default; + virtual void drawSomething() = 0; +}; + +#endif \ No newline at end of file diff --git a/src/graphics/opengl/opengl.cpp b/src/graphics/opengl/opengl.cpp new file mode 100644 index 0000000..7d51911 --- /dev/null +++ b/src/graphics/opengl/opengl.cpp @@ -0,0 +1,82 @@ +/** + * Copyright (c) 2023, Furkan Mudanyali + * All rights reserved + */ + +#include +#define GLEW_STATIC +#include +#include + +#include +#include +#include +#include +#include + +static const GLchar* vertexShaderSrc = R"( + attribute vec4 position; + void main() { + gl_Position = vec4(position.xyz, 1.0); + } +)"; + +static const GLchar* fragmentShaderSrc = R"( + precision mediump float; + void main() { + gl_FragColor = vec4(1.0, 1.0, 1.0, 1.0); + } +)"; + +static GLfloat vertices[] = {0.0, 0.8, -0.8, -0.8, 0.8, -0.8}; + +OpenGL::OpenGL(Game* pGame) { + glewExperimental = GL_TRUE; + this->game = pGame; + + SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 4); + SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 3); + SDL_GL_SetSwapInterval(0); + SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1); + SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 24); + + this->context = SDL_GL_CreateContext(this->game->pWindow->window); + + GLenum err = glewInit(); + std::cout << glewGetErrorString(err); + + GLuint vertexArrObj; + glGenVertexArrays(1, &vertexArrObj); + glBindVertexArray(vertexArrObj); + + GLuint vertexBufObj; + glGenBuffers(1, &vertexBufObj); + + glBindBuffer(GL_ARRAY_BUFFER, vertexBufObj); + glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW); + + GLuint vertexShader = glCreateShader(GL_VERTEX_SHADER); + glShaderSource(vertexShader, 1, &vertexShaderSrc, nullptr); + glCompileShader(vertexShader); + + GLuint fragmentShader = glCreateShader(GL_FRAGMENT_SHADER); + glShaderSource(fragmentShader, 1, &fragmentShaderSrc, nullptr); + glCompileShader(fragmentShader); + + GLuint shaderProgram = glCreateProgram(); + glAttachShader(shaderProgram, vertexShader); + glAttachShader(shaderProgram, fragmentShader); + glLinkProgram(shaderProgram); + glUseProgram(shaderProgram); + + GLint posAttribute = glGetAttribLocation(shaderProgram, "position"); + glEnableVertexAttribArray(posAttribute); + glVertexAttribPointer(posAttribute, 2, GL_FLOAT, GL_FALSE, 0, nullptr); +} + +void OpenGL::drawSomething() { + glClearColor(0.0F, 0.0F, 0.0F, 1.0F); + glClear(GL_COLOR_BUFFER_BIT); + glDrawArrays(GL_TRIANGLES, 0, 3); + SDL_GL_SwapWindow(this->game->pWindow->window); +} \ No newline at end of file diff --git a/src/graphics/opengl/opengl.h b/src/graphics/opengl/opengl.h new file mode 100644 index 0000000..9ed2fc3 --- /dev/null +++ b/src/graphics/opengl/opengl.h @@ -0,0 +1,25 @@ +/** + * Copyright (c) 2023, Furkan Mudanyali + * All rights reserved + */ + +#ifndef _HOMD_GRAPHICS_GL +#define _HOMD_GRAPHICS_GL + +#include +#include + +class Window; + +class OpenGL : public Graphics { + SDL_GLContext context = nullptr; + + public: + OpenGL(Game*); + ~OpenGL() override = default; + + void setGLContext(); + void drawSomething() override; +}; + +#endif \ No newline at end of file diff --git a/src/scene/example/example.cpp b/src/scene/example/example.cpp new file mode 100644 index 0000000..e356631 --- /dev/null +++ b/src/scene/example/example.cpp @@ -0,0 +1,16 @@ +/** + * Copyright (c) 2023, Furkan Mudanyali + * All rights reserved + */ + +#include +#include +#include + +Example::Example(Game* pGame) { + this->game = pGame; +} + +void Example::draw() { + this->game->pRenderer->drawSomething(); +} \ No newline at end of file diff --git a/src/scene/example/example.h b/src/scene/example/example.h new file mode 100644 index 0000000..331ce9c --- /dev/null +++ b/src/scene/example/example.h @@ -0,0 +1,12 @@ +/** + * Copyright (c) 2023, Furkan Mudanyali + * All rights reserved + */ + +#include + +class Example : public Scene { + public: + Example(Game*); + void draw() override; +}; \ No newline at end of file diff --git a/src/scene/scene.cpp b/src/scene/scene.cpp deleted file mode 100644 index 6eef621..0000000 --- a/src/scene/scene.cpp +++ /dev/null @@ -1,6 +0,0 @@ -/** - * Copyright (c) 2023, Furkan Mudanyali - * All rights reserved - */ - -#include \ No newline at end of file diff --git a/src/scene/scene.h b/src/scene/scene.h index e00a7bf..f2b8f5d 100644 --- a/src/scene/scene.h +++ b/src/scene/scene.h @@ -6,11 +6,16 @@ #ifndef _HOMD_SCENE #define _HOMD_SCENE +class Game; + class Scene { public: + Game* game; + bool destroy = false; Scene() = default; - ~Scene() = default; + virtual ~Scene() = default; + virtual void draw() = 0; }; #endif \ No newline at end of file