From 018a67ea3f9aba1b3892d714026a333cb2b80201 Mon Sep 17 00:00:00 2001 From: CRy386i Date: Mon, 23 Dec 2024 14:02:28 +0200 Subject: [PATCH] Refactor and add GL some stuff --- fragment.glsl | 8 +++ main.c | 171 +++++++++++++++++++++++++++++--------------------- vertex.glsl | 8 +++ 3 files changed, 116 insertions(+), 71 deletions(-) create mode 100644 fragment.glsl create mode 100644 vertex.glsl diff --git a/fragment.glsl b/fragment.glsl new file mode 100644 index 0000000..53bdfb9 --- /dev/null +++ b/fragment.glsl @@ -0,0 +1,8 @@ +#version 460 + +out vec4 outputColor; + +void main() +{ + outputColor = vec4(1.0f, 0.0f, 0.0f, 1.0f); +} diff --git a/main.c b/main.c index db1038f..b57d983 100644 --- a/main.c +++ b/main.c @@ -6,7 +6,6 @@ #include #include // only include this one in the source file with main()! - #define INITIAL_FPS 60 SDL_Window* window = NULL; @@ -20,7 +19,7 @@ static double FrameTime = 1.0 / INITIAL_FPS; static double PerformancePeriod; -static void panic ( const char *text ) __attribute__((noreturn)); +static void panicError ( void ) __attribute__((noreturn)); static double GetTime( void ) { @@ -30,42 +29,56 @@ static double GetTime( void ) static void SetTitle ( SDL_Window *window ) { char title[48] = {0}; - SDL_snprintf(title, sizeof(title), "Crude frame limiter @ %6.3f Hz", FPS); + SDL_snprintf(title, sizeof(title), "Hello Triangle! Framelimit: %6.3f Hz", FPS); SDL_SetWindowTitle(window, title); } -void panic ( const char *text ) +void panicError ( void ) { - //fprintf(stderr, "ERROR: %s. %s\n", text, SDL_GetError()); - SDL_LogCritical(SDL_LOG_CATEGORY_ASSERT, "%s. %s\n", text, SDL_GetError()); SDL_GL_DestroyContext(glcontext); SDL_DestroyWindow(window); SDL_Quit(); - exit(1); + exit(EXIT_FAILURE); } char *readFile ( const char *fileName ) { SDL_IOStream *ioStream = SDL_IOFromFile(fileName, "rb"); - if (!ioStream) panic("SDL_IOFromfile"); - if (!SDL_SeekIO(ioStream, 0, SDL_IO_SEEK_END)) panic("SDL_SeekIO_END"); + if (!ioStream) { + SDL_LogCritical(SDL_LOG_CATEGORY_ASSERT, "ioStream: %s.", SDL_GetError()); + panicError(); + } + if (SDL_SeekIO(ioStream, 0, SDL_IO_SEEK_END) == -1) { + SDL_LogCritical(SDL_LOG_CATEGORY_ASSERT, "SDL_SeekIO_END: %s.", SDL_GetError()); + panicError(); + } long len = SDL_TellIO(ioStream); - if (!len) panic("SDL_TellIO"); - if (!SDL_SeekIO(ioStream, 0, SDL_IO_SEEK_SET)) panic("SDL_SeekIO_SET"); + if (len == -1) { + SDL_LogCritical(SDL_LOG_CATEGORY_ASSERT, "SDL_TellIO: %s.", SDL_GetError()); + panicError(); + } + if (SDL_SeekIO(ioStream, 0, SDL_IO_SEEK_SET) == -1) { + SDL_LogCritical(SDL_LOG_CATEGORY_ASSERT, "SDL_IO_SEEK_SET: %s.", SDL_GetError()); + panicError(); + } char *res = SDL_malloc(len + 1); - if (!res) panic("SDL_malloc_RES"); - if (SDL_ReadIO(ioStream, res, len) != (size_t)len) panic("SDL_ReadIO"); - if (!SDL_CloseIO(ioStream)) panic("SDL_CloseIO"); - - - /* char *res = SDL_LoadFile_IO(ioStream, NULL, true); */ - /* if (!res) panic("SDL_LoadFile_IO"); */ - + if (!res) { + SDL_LogCritical(SDL_LOG_CATEGORY_ASSERT, "SDL_malloc: %s.", SDL_GetError()); + panicError(); + } + if (SDL_ReadIO(ioStream, res, len) != (size_t)len) { + SDL_LogCritical(SDL_LOG_CATEGORY_ASSERT, "SDL_ReadIO: %s.", SDL_GetError()); + panicError(); + } + if (!SDL_CloseIO(ioStream)) { + SDL_LogCritical(SDL_LOG_CATEGORY_ASSERT, "SDL_CloseIO: %s.", SDL_GetError()); + panicError(); + } res[len] = 0; return res; } -GLuint CreateShader ( const char *shaderFile, GLenum shaderType ) +GLuint createShader ( const char *shaderFile, GLenum shaderType ) { const char *strShaderType; if (shaderType == GL_VERTEX_SHADER) { @@ -75,13 +88,13 @@ GLuint CreateShader ( const char *shaderFile, GLenum shaderType ) } else if (shaderType == GL_FRAGMENT_SHADER) { strShaderType = "fragment"; } else { - SDL_LogCritical(SDL_LOG_CATEGORY_ASSERT, "Unrecognized shader type.\n"); - exit(1); + SDL_LogCritical(SDL_LOG_CATEGORY_ASSERT, "Unrecognized shader type."); + panicError(); } GLuint shader = glCreateShader(shaderType); if (!shader) { - SDL_LogCritical(SDL_LOG_CATEGORY_ASSERT, "Error create shader type %s\n", strShaderType); - exit(1); + SDL_LogCritical(SDL_LOG_CATEGORY_ASSERT, "Error create shader type %s.", strShaderType); + panicError(); } GLchar *content = readFile(shaderFile); glShaderSource(shader, 1, (const GLchar **)&content, NULL); @@ -94,18 +107,18 @@ GLuint CreateShader ( const char *shaderFile, GLenum shaderType ) glGetShaderiv(shader, GL_INFO_LOG_LENGTH, &infoLen); GLchar *info = SDL_malloc(sizeof(GLchar) * (infoLen + 1)); glGetShaderInfoLog(shader, infoLen, NULL, info); - SDL_LogCritical(SDL_LOG_CATEGORY_ASSERT, "Filed to compile %s shader:\n%s\n", strShaderType, info); - exit(1); + SDL_LogCritical(SDL_LOG_CATEGORY_ASSERT, "Filed to compile %s shader:\n%s.", strShaderType, info); + panicError(); } return shader; } -void CreateProgram ( GLuint *shaders, int len ) +void createProgram ( GLuint *shaders, int len ) { program = glCreateProgram(); if (!program) { - SDL_LogCritical(SDL_LOG_CATEGORY_ASSERT, "Can't to create shader program\n"); - exit(1); + SDL_LogCritical(SDL_LOG_CATEGORY_ASSERT, "Can't to create shader program."); + panicError(); } for (int i = 0; i < len; i++) { glAttachShader(program, shaders[i]); @@ -118,13 +131,12 @@ void CreateProgram ( GLuint *shaders, int len ) glGetProgramiv(program, GL_INFO_LOG_LENGTH, &infoLen); GLchar *info = SDL_malloc(sizeof(GLchar) * (infoLen + 1)); glGetProgramInfoLog(program, infoLen, NULL, info); - SDL_LogCritical(SDL_LOG_CATEGORY_ASSERT, "Link failed: %s\n", info); - exit(1); + SDL_LogCritical(SDL_LOG_CATEGORY_ASSERT, "Link failed: %s.", info); + panicError(); } } - -void CreateBuffer ( void ) +void createBuffer ( void ) { float vertices[] = { -1.0f, -1.0f, 0.0f, @@ -136,36 +148,53 @@ void CreateBuffer ( void ) glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW); } -void ConfProg ( void ) +void startShaderProg ( void ) { - CreateBuffer(); + createBuffer(); GLuint shaders[] = { - CreateShader("vertex.glsl", GL_VERTEX_SHADER), - CreateShader("fragmet.glsl", GL_FRAGMENT_SHADER) + createShader("vertex.glsl", GL_VERTEX_SHADER), + createShader("fragment.glsl", GL_FRAGMENT_SHADER) }; int len = sizeof(shaders) / sizeof(shaders[0]); - CreateProgram(shaders, len); + createProgram(shaders, len); for (int i = 0; i < len; i++) { glDeleteShader(shaders[i]); } } +void _GLBuffer( void ) +{ + glClearColor(0.0f, 0.0f, 1.0f, 0.0f); + glClear(GL_COLOR_BUFFER_BIT); + glUseProgram(program); + + glEnableVertexAttribArray(0); + glBindBuffer(GL_ARRAY_BUFFER, vbo); + glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, 0); + glDrawArrays(GL_TRIANGLES, 0, 3); + glDisableVertexAttribArray(0); + + glUseProgram(0); +} + int main( int argc, char* argv[] ) { const int WIDTH = 640; const int HEIGHT = 480; int delay_threshold = 2; - double sleep_time; + int r, g, b, GLJ, GLN = 0; + double sleep_time = 0; double time_counter = 0; bool loopShouldStop = false; SDL_SetHint(SDL_HINT_RENDER_VSYNC, 0); - if (!SDL_Init(SDL_INIT_VIDEO)) { - panic("SDL_Init failed"); + SDL_LogCritical(SDL_LOG_CATEGORY_ASSERT, "SDL_Init failed: %s.", SDL_GetError()); } + SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 2); + SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 0); SDL_GL_SetAttribute(SDL_GL_RED_SIZE, 5); SDL_GL_SetAttribute(SDL_GL_GREEN_SIZE, 5); SDL_GL_SetAttribute(SDL_GL_BLUE_SIZE, 5); @@ -177,29 +206,41 @@ int main( int argc, char* argv[] ) window = SDL_CreateWindow("Hello SDL", WIDTH, HEIGHT, SDL_WINDOW_OPENGL|SDL_WINDOW_RESIZABLE); if (!window) { - panic("SDL_CreateWindow"); + SDL_LogCritical(SDL_LOG_CATEGORY_ASSERT, "SDL_CreateWindow: %s.", SDL_GetError()); + panicError(); } glcontext = SDL_GL_CreateContext(window); if (!glcontext) { - panic("SDL_GL_CreateContext"); + SDL_LogCritical(SDL_LOG_CATEGORY_ASSERT, "SDL_GL_CreateContext: %s.", SDL_GetError()); + panicError(); } - ConfProg(); - SetTitle(window); - //if (!SDL_SetRenderVSync(renderer, 1)) - //{ - // panic("SDL_SetRenderVSync"); - //} - SDL_HideCursor(); - int red = 0; - int green = 0; + glewExperimental = GL_TRUE; + GLenum err = glewInit(); + if (err != GLEW_OK) { + SDL_LogCritical(SDL_LOG_CATEGORY_ASSERT, "glewInit: %s.", glewGetErrorString(err)); + panicError(); + } + + SDL_LogVerbose(SDL_LOG_CATEGORY_RENDER, "Status: Using GLEW %s\n", glewGetString(GLEW_VERSION)); + err = SDL_GL_GetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, &GLJ); + SDL_GL_GetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, &GLN); + SDL_GL_GetAttribute(SDL_GL_RED_SIZE, &r); + SDL_GL_GetAttribute(SDL_GL_GREEN_SIZE, &g); + SDL_GL_GetAttribute(SDL_GL_BLUE_SIZE, &b); + + SDL_Log("%s", SDL_GetError()); + + SDL_LogVerbose(SDL_LOG_CATEGORY_RENDER, "Red size: %d, Green size: %d, Blue size: %d, OpenGL: %d.%d.", r, g, b, GLJ, GLN); + + SDL_GL_SetSwapInterval(0); + startShaderProg(); - int r, g, b; while (!loopShouldStop) { SDL_Event e; SDL_zero(e); @@ -225,12 +266,11 @@ int main( int argc, char* argv[] ) } } - /* SDL_SetRenderDrawColor(renderer, red, green, 0, 255); */ - /* SDL_RenderClear(renderer); */ - - glClearColor(0, 0, 0, 1); - glClear(GL_COLOR_BUFFER_BIT); - SDL_GL_SwapWindow(window); + _GLBuffer(); + if (!SDL_GL_SwapWindow(window)) { + SDL_LogCritical(SDL_LOG_CATEGORY_ASSERT, "SDL_GL_SwapWwindow: %s.", SDL_GetError()); + panicError(); + } sleep_time = time_counter - GetTime(); if (sleep_time * 1000 > delay_threshold) { @@ -245,22 +285,11 @@ int main( int argc, char* argv[] ) while (time_counter > GetTime()) { /* Waiting for the right moment. */ } - - /* red = (red + 1) % 256; */ - /* green = (green + 1) % 64; */ - time_counter += FrameTime; } - SDL_GL_GetAttribute(SDL_GL_RED_SIZE, &r); - SDL_GL_GetAttribute(SDL_GL_GREEN_SIZE, &g); - SDL_GL_GetAttribute(SDL_GL_BLUE_SIZE, &b); - - printf("Red size: %d, Green size: %d, Blue size: %d\n", r, g, b); - - SDL_DestroyWindow(window); SDL_GL_DestroyContext(glcontext); SDL_Quit(); - exit(0); + exit(EXIT_SUCCESS); } diff --git a/vertex.glsl b/vertex.glsl new file mode 100644 index 0000000..383b13a --- /dev/null +++ b/vertex.glsl @@ -0,0 +1,8 @@ +#version 460 + +layout(location = 0) in vec3 pos; + +void main() +{ + gl_Position = vec4(pos.x, pos.y, pos.z, 1.0); +}