Crude OpenGL triangle example
This commit is contained in:
parent
194b40d1ee
commit
0ac7d6e7aa
@ -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}
|
||||
)
|
||||
|
||||
|
18
README.md
Normal file
18
README.md
Normal file
@ -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
|
||||
```
|
@ -4,12 +4,14 @@
|
||||
*/
|
||||
|
||||
#include <game/game.h>
|
||||
#include <scene/scene.h>
|
||||
#include <scene/example/example.h>
|
||||
#include <graphics/opengl/opengl.h>
|
||||
|
||||
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();
|
||||
}
|
||||
}
|
@ -11,6 +11,7 @@
|
||||
#include <stack>
|
||||
|
||||
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;
|
||||
|
19
src/graphics/graphics.h
Normal file
19
src/graphics/graphics.h
Normal file
@ -0,0 +1,19 @@
|
||||
/**
|
||||
* Copyright (c) 2023, Furkan Mudanyali <fmudanyali@icloud.com>
|
||||
* 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
|
82
src/graphics/opengl/opengl.cpp
Normal file
82
src/graphics/opengl/opengl.cpp
Normal file
@ -0,0 +1,82 @@
|
||||
/**
|
||||
* Copyright (c) 2023, Furkan Mudanyali <fmudanyali@icloud.com>
|
||||
* All rights reserved
|
||||
*/
|
||||
|
||||
#include <GLES3/gl3.h>
|
||||
#define GLEW_STATIC
|
||||
#include <SDL2/SDL_video.h>
|
||||
#include <GL/glew.h>
|
||||
|
||||
#include <graphics/opengl/opengl.h>
|
||||
#include <game/game.h>
|
||||
#include <window/window.h>
|
||||
#include <string>
|
||||
#include <iostream>
|
||||
|
||||
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);
|
||||
}
|
25
src/graphics/opengl/opengl.h
Normal file
25
src/graphics/opengl/opengl.h
Normal file
@ -0,0 +1,25 @@
|
||||
/**
|
||||
* Copyright (c) 2023, Furkan Mudanyali <fmudanyali@icloud.com>
|
||||
* All rights reserved
|
||||
*/
|
||||
|
||||
#ifndef _HOMD_GRAPHICS_GL
|
||||
#define _HOMD_GRAPHICS_GL
|
||||
|
||||
#include <SDL2/SDL_video.h>
|
||||
#include <graphics/graphics.h>
|
||||
|
||||
class Window;
|
||||
|
||||
class OpenGL : public Graphics {
|
||||
SDL_GLContext context = nullptr;
|
||||
|
||||
public:
|
||||
OpenGL(Game*);
|
||||
~OpenGL() override = default;
|
||||
|
||||
void setGLContext();
|
||||
void drawSomething() override;
|
||||
};
|
||||
|
||||
#endif
|
16
src/scene/example/example.cpp
Normal file
16
src/scene/example/example.cpp
Normal file
@ -0,0 +1,16 @@
|
||||
/**
|
||||
* Copyright (c) 2023, Furkan Mudanyali <fmudanyali@icloud.com>
|
||||
* All rights reserved
|
||||
*/
|
||||
|
||||
#include <game/game.h>
|
||||
#include <graphics/graphics.h>
|
||||
#include <scene/example/example.h>
|
||||
|
||||
Example::Example(Game* pGame) {
|
||||
this->game = pGame;
|
||||
}
|
||||
|
||||
void Example::draw() {
|
||||
this->game->pRenderer->drawSomething();
|
||||
}
|
12
src/scene/example/example.h
Normal file
12
src/scene/example/example.h
Normal file
@ -0,0 +1,12 @@
|
||||
/**
|
||||
* Copyright (c) 2023, Furkan Mudanyali <fmudanyali@icloud.com>
|
||||
* All rights reserved
|
||||
*/
|
||||
|
||||
#include <scene/scene.h>
|
||||
|
||||
class Example : public Scene {
|
||||
public:
|
||||
Example(Game*);
|
||||
void draw() override;
|
||||
};
|
@ -1,6 +0,0 @@
|
||||
/**
|
||||
* Copyright (c) 2023, Furkan Mudanyali <fmudanyali@icloud.com>
|
||||
* All rights reserved
|
||||
*/
|
||||
|
||||
#include <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
|
Reference in New Issue
Block a user