aboutsummaryrefslogtreecommitdiff
path: root/src/main.cpp
blob: 03222f1ca1058a434f712f7daf82c1b080cc262a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
#include <cstdio>
#include <GL/glew.h>
#include <GLFW/glfw3.h>
#include <glm/glm.hpp>
#include "../include/RenderBackend/OpenGL/VertexShader.hpp"
#include "../include/RenderBackend/OpenGL/PixelShader.hpp"
#include "../include/RenderBackend/OpenGL/ShaderProgram.hpp"
#include "../include/RenderBackend/OpenGL/DeviceMemory.hpp"

using namespace amalgine;
using namespace std;

int main()
{
    if(!glfwInit())
    {
        fprintf(stderr, "Failed to initialize GLFW\n");
        return -1;
    }

    glfwWindowHint(GLFW_SAMPLES, 4);
    glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 4);
    glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 5);
    glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE);
    glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
    glfwWindowHint(GLFW_RESIZABLE, GL_FALSE);

    GLFWwindow *window = glfwCreateWindow(1920, 1080, "Amalgine", nullptr, nullptr);
    if(!window)
    {
        fprintf(stderr, "Failed to open GLFW window\n");
        glfwTerminate();
        return -1;
    }

    glfwMakeContextCurrent(window);
    glewExperimental = true;
    if(glewInit() != GLEW_OK)
    {
        fprintf(stderr, "Failed to initialize GLEW\n");
        glfwTerminate();
        return -1;
    }

    glfwSetInputMode(window, GLFW_STICKY_KEYS, GL_TRUE);
    
    
    f32 verticesRaw[] = 
    {
        0.0f, 0.5f,
        0.5f, -0.5f,
        -0.5f, -0.5f
    };
    DataView<f32> vertices(verticesRaw, 6);
    DeviceMemory triangle;
    triangle.copyStatic(vertices);
    
    ShaderProgram shaderProgram;
    
    VertexShader vertexShader;
    ShaderInputVec2 inputPosition = vertexShader.defineInputVec2("position");
    
    vertexShader.defineMain([&inputPosition]()
    {
        return ShaderVec4(inputPosition, 0.0f, 1.0f);
    });
    
    PixelShader pixelShader;
    ShaderOutputVec4 outColor = shaderProgram.defineOutputVec4("outColor");
    
    pixelShader.defineMain([&outColor]()
    {
        outColor = ShaderVec4(1.0f, 1.0f, 1.0f, 1.0f);
    });
    
    string vertexShaderSource = vertexShader.build();
    printf("Vertex shader source:\n%s", vertexShaderSource.c_str());
    string pixelShaderSource = pixelShader.build();
    printf("Pixel shader source:\n%s", pixelShaderSource.c_str());

    while(!glfwWindowShouldClose(window))
    {
        if(glfwGetKey(window, GLFW_KEY_ESCAPE) != GLFW_PRESS)
            glfwSetWindowShouldClose(window, GL_TRUE);

        glfwSwapBuffers(window);
        glfwPollEvents();
    }

    return 0;
}