diff options
author | talha <talha@talhaamir.xyz> | 2024-02-11 13:35:07 +0500 |
---|---|---|
committer | talha <talha@talhaamir.xyz> | 2024-02-11 13:35:07 +0500 |
commit | 538cb8303b5c7ad14f9a6417ff0ae716fcd06571 (patch) | |
tree | bdafd7275588e00ddf9dce8ef1912a601f4ca185 /main.c |
setup git repo
Diffstat (limited to 'main.c')
-rw-r--r-- | main.c | 125 |
1 files changed, 125 insertions, 0 deletions
@@ -0,0 +1,125 @@ +#include <math.h> +#include <SDL2/SDL.h> +#include <glad/glad.h> + +int main(int argc, char *argv[]) +{ + int width = 1920; + int height = 1080; + + if (SDL_Init(SDL_INIT_VIDEO) != 0) + { + printf("Error initialising SDL2: %s\n", SDL_GetError()); + return 0; + }; + + // initialise window with opengl flag + SDL_Window *window = SDL_CreateWindow("SDL Test", + SDL_WINDOWPOS_UNDEFINED, + SDL_WINDOWPOS_UNDEFINED, + width, + height, + SDL_WINDOW_OPENGL); + + // create an opengl context + SDL_GLContext context = SDL_GL_CreateContext(window); + if (!context) + { + printf("OpenGL context creation failed: %s\n", SDL_GetError()); + return -1; + } + + // load glad + if (!gladLoadGLLoader((GLADloadproc)SDL_GL_GetProcAddress)) { + printf("Failed to initialize Glad\n"); + return 1; + } + + // opengl rendering and shader stuff here + // @todo: + // have to learn how to setup shaders and other things with modern opengl (4.5+) + const char* vertexSource = + "#version 330 core\n" + "layout(location = 0) in vec3 position;" + "void main() {" + " gl_Position = vec4(position, 1.0);" + "}"; + + const char* fragmentSource = + "#version 330 core\n" + "out vec4 fragColor;" + "void main() {" + " fragColor = vec4(1.0, 0.0, 0.0, 1.0);" + "}"; + + GLuint vertexShader = glCreateShader(GL_VERTEX_SHADER); + glShaderSource(vertexShader, 1, &vertexSource, NULL); + glCompileShader(vertexShader); + + GLuint fragmentShader = glCreateShader(GL_FRAGMENT_SHADER); + glShaderSource(fragmentShader, 1, &fragmentSource, NULL); + glCompileShader(fragmentShader); + + GLuint shaderProgram = glCreateProgram(); + glAttachShader(shaderProgram, vertexShader); + glAttachShader(shaderProgram, fragmentShader); + glLinkProgram(shaderProgram); + glUseProgram(shaderProgram); + + glDeleteShader(vertexShader); + glDeleteShader(fragmentShader); + + GLfloat vertices[] = { + -0.5f, -0.5f, 0.0f, + 0.5f, -0.5f, 0.0f, + 0.0f, 0.5f, 0.0f + }; + + GLuint VBO, VAO; + glGenVertexArrays(1, &VAO); + glGenBuffers(1, &VBO); + + glBindVertexArray(VAO); + + glBindBuffer(GL_ARRAY_BUFFER, VBO); + glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW); + + glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(GLfloat), (GLvoid*)0); + glEnableVertexAttribArray(0); + + glBindBuffer(GL_ARRAY_BUFFER, 0); + glBindVertexArray(0); + for(;;) + { + SDL_Event ev; + while(SDL_PollEvent(&ev)) + { + if (ev.type == SDL_QUIT) + { + return 0; + } + } + + // opengl rendering code here + // @note: this part is more or less the same + // as with my learn opengl experiments + glClearColor(0.2f, 0.3f, 0.3f, 1.0f); + glClear(GL_COLOR_BUFFER_BIT); + + glUseProgram(shaderProgram); + glBindVertexArray(VAO); + glDrawArrays(GL_TRIANGLES, 0, 3); + glBindVertexArray(0); + + SDL_GL_SwapWindow(window); + } + // opengl free calls + glDeleteVertexArrays(1, &VAO); + glDeleteBuffers(1, &VBO); + glDeleteProgram(shaderProgram); + // sdl free calls + SDL_GL_DeleteContext(context); + SDL_DestroyWindow(window); + SDL_Quit(); + return 0; +} |