Refactor and add GL some stuff

This commit is contained in:
CRy386i 2024-12-23 14:02:28 +02:00
parent 3cf0b585dc
commit 018a67ea3f
3 changed files with 116 additions and 71 deletions

8
fragment.glsl Normal file
View file

@ -0,0 +1,8 @@
#version 460
out vec4 outputColor;
void main()
{
outputColor = vec4(1.0f, 0.0f, 0.0f, 1.0f);
}

171
main.c
View file

@ -6,7 +6,6 @@
#include <SDL3/SDL_opengl.h> #include <SDL3/SDL_opengl.h>
#include <SDL3/SDL_main.h> // only include this one in the source file with main()! #include <SDL3/SDL_main.h> // only include this one in the source file with main()!
#define INITIAL_FPS 60 #define INITIAL_FPS 60
SDL_Window* window = NULL; SDL_Window* window = NULL;
@ -20,7 +19,7 @@ static double FrameTime = 1.0 / INITIAL_FPS;
static double PerformancePeriod; static double PerformancePeriod;
static void panic ( const char *text ) __attribute__((noreturn)); static void panicError ( void ) __attribute__((noreturn));
static double GetTime( void ) static double GetTime( void )
{ {
@ -30,42 +29,56 @@ static double GetTime( void )
static void SetTitle ( SDL_Window *window ) static void SetTitle ( SDL_Window *window )
{ {
char title[48] = {0}; 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); 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_GL_DestroyContext(glcontext);
SDL_DestroyWindow(window); SDL_DestroyWindow(window);
SDL_Quit(); SDL_Quit();
exit(1); exit(EXIT_FAILURE);
} }
char *readFile ( const char *fileName ) char *readFile ( const char *fileName )
{ {
SDL_IOStream *ioStream = SDL_IOFromFile(fileName, "rb"); SDL_IOStream *ioStream = SDL_IOFromFile(fileName, "rb");
if (!ioStream) panic("SDL_IOFromfile"); if (!ioStream) {
if (!SDL_SeekIO(ioStream, 0, SDL_IO_SEEK_END)) panic("SDL_SeekIO_END"); 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); long len = SDL_TellIO(ioStream);
if (!len) panic("SDL_TellIO"); if (len == -1) {
if (!SDL_SeekIO(ioStream, 0, SDL_IO_SEEK_SET)) panic("SDL_SeekIO_SET"); 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); char *res = SDL_malloc(len + 1);
if (!res) panic("SDL_malloc_RES"); if (!res) {
if (SDL_ReadIO(ioStream, res, len) != (size_t)len) panic("SDL_ReadIO"); SDL_LogCritical(SDL_LOG_CATEGORY_ASSERT, "SDL_malloc: %s.", SDL_GetError());
if (!SDL_CloseIO(ioStream)) panic("SDL_CloseIO"); panicError();
}
if (SDL_ReadIO(ioStream, res, len) != (size_t)len) {
/* char *res = SDL_LoadFile_IO(ioStream, NULL, true); */ SDL_LogCritical(SDL_LOG_CATEGORY_ASSERT, "SDL_ReadIO: %s.", SDL_GetError());
/* if (!res) panic("SDL_LoadFile_IO"); */ panicError();
}
if (!SDL_CloseIO(ioStream)) {
SDL_LogCritical(SDL_LOG_CATEGORY_ASSERT, "SDL_CloseIO: %s.", SDL_GetError());
panicError();
}
res[len] = 0; res[len] = 0;
return res; return res;
} }
GLuint CreateShader ( const char *shaderFile, GLenum shaderType ) GLuint createShader ( const char *shaderFile, GLenum shaderType )
{ {
const char *strShaderType; const char *strShaderType;
if (shaderType == GL_VERTEX_SHADER) { if (shaderType == GL_VERTEX_SHADER) {
@ -75,13 +88,13 @@ GLuint CreateShader ( const char *shaderFile, GLenum shaderType )
} else if (shaderType == GL_FRAGMENT_SHADER) { } else if (shaderType == GL_FRAGMENT_SHADER) {
strShaderType = "fragment"; strShaderType = "fragment";
} else { } else {
SDL_LogCritical(SDL_LOG_CATEGORY_ASSERT, "Unrecognized shader type.\n"); SDL_LogCritical(SDL_LOG_CATEGORY_ASSERT, "Unrecognized shader type.");
exit(1); panicError();
} }
GLuint shader = glCreateShader(shaderType); GLuint shader = glCreateShader(shaderType);
if (!shader) { if (!shader) {
SDL_LogCritical(SDL_LOG_CATEGORY_ASSERT, "Error create shader type %s\n", strShaderType); SDL_LogCritical(SDL_LOG_CATEGORY_ASSERT, "Error create shader type %s.", strShaderType);
exit(1); panicError();
} }
GLchar *content = readFile(shaderFile); GLchar *content = readFile(shaderFile);
glShaderSource(shader, 1, (const GLchar **)&content, NULL); 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); glGetShaderiv(shader, GL_INFO_LOG_LENGTH, &infoLen);
GLchar *info = SDL_malloc(sizeof(GLchar) * (infoLen + 1)); GLchar *info = SDL_malloc(sizeof(GLchar) * (infoLen + 1));
glGetShaderInfoLog(shader, infoLen, NULL, info); glGetShaderInfoLog(shader, infoLen, NULL, info);
SDL_LogCritical(SDL_LOG_CATEGORY_ASSERT, "Filed to compile %s shader:\n%s\n", strShaderType, info); SDL_LogCritical(SDL_LOG_CATEGORY_ASSERT, "Filed to compile %s shader:\n%s.", strShaderType, info);
exit(1); panicError();
} }
return shader; return shader;
} }
void CreateProgram ( GLuint *shaders, int len ) void createProgram ( GLuint *shaders, int len )
{ {
program = glCreateProgram(); program = glCreateProgram();
if (!program) { if (!program) {
SDL_LogCritical(SDL_LOG_CATEGORY_ASSERT, "Can't to create shader program\n"); SDL_LogCritical(SDL_LOG_CATEGORY_ASSERT, "Can't to create shader program.");
exit(1); panicError();
} }
for (int i = 0; i < len; i++) { for (int i = 0; i < len; i++) {
glAttachShader(program, shaders[i]); glAttachShader(program, shaders[i]);
@ -118,13 +131,12 @@ void CreateProgram ( GLuint *shaders, int len )
glGetProgramiv(program, GL_INFO_LOG_LENGTH, &infoLen); glGetProgramiv(program, GL_INFO_LOG_LENGTH, &infoLen);
GLchar *info = SDL_malloc(sizeof(GLchar) * (infoLen + 1)); GLchar *info = SDL_malloc(sizeof(GLchar) * (infoLen + 1));
glGetProgramInfoLog(program, infoLen, NULL, info); glGetProgramInfoLog(program, infoLen, NULL, info);
SDL_LogCritical(SDL_LOG_CATEGORY_ASSERT, "Link failed: %s\n", info); SDL_LogCritical(SDL_LOG_CATEGORY_ASSERT, "Link failed: %s.", info);
exit(1); panicError();
} }
} }
void createBuffer ( void )
void CreateBuffer ( void )
{ {
float vertices[] = { float vertices[] = {
-1.0f, -1.0f, 0.0f, -1.0f, -1.0f, 0.0f,
@ -136,36 +148,53 @@ void CreateBuffer ( void )
glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW); glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);
} }
void ConfProg ( void ) void startShaderProg ( void )
{ {
CreateBuffer(); createBuffer();
GLuint shaders[] = { GLuint shaders[] = {
CreateShader("vertex.glsl", GL_VERTEX_SHADER), createShader("vertex.glsl", GL_VERTEX_SHADER),
CreateShader("fragmet.glsl", GL_FRAGMENT_SHADER) createShader("fragment.glsl", GL_FRAGMENT_SHADER)
}; };
int len = sizeof(shaders) / sizeof(shaders[0]); int len = sizeof(shaders) / sizeof(shaders[0]);
CreateProgram(shaders, len); createProgram(shaders, len);
for (int i = 0; i < len; i++) { for (int i = 0; i < len; i++) {
glDeleteShader(shaders[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[] ) int main( int argc, char* argv[] )
{ {
const int WIDTH = 640; const int WIDTH = 640;
const int HEIGHT = 480; const int HEIGHT = 480;
int delay_threshold = 2; int delay_threshold = 2;
double sleep_time; int r, g, b, GLJ, GLN = 0;
double sleep_time = 0;
double time_counter = 0; double time_counter = 0;
bool loopShouldStop = false; bool loopShouldStop = false;
SDL_SetHint(SDL_HINT_RENDER_VSYNC, 0); SDL_SetHint(SDL_HINT_RENDER_VSYNC, 0);
if (!SDL_Init(SDL_INIT_VIDEO)) { 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_RED_SIZE, 5);
SDL_GL_SetAttribute(SDL_GL_GREEN_SIZE, 5); SDL_GL_SetAttribute(SDL_GL_GREEN_SIZE, 5);
SDL_GL_SetAttribute(SDL_GL_BLUE_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); window = SDL_CreateWindow("Hello SDL", WIDTH, HEIGHT, SDL_WINDOW_OPENGL|SDL_WINDOW_RESIZABLE);
if (!window) { if (!window) {
panic("SDL_CreateWindow"); SDL_LogCritical(SDL_LOG_CATEGORY_ASSERT, "SDL_CreateWindow: %s.", SDL_GetError());
panicError();
} }
glcontext = SDL_GL_CreateContext(window); glcontext = SDL_GL_CreateContext(window);
if (!glcontext) { if (!glcontext) {
panic("SDL_GL_CreateContext"); SDL_LogCritical(SDL_LOG_CATEGORY_ASSERT, "SDL_GL_CreateContext: %s.", SDL_GetError());
panicError();
} }
ConfProg();
SetTitle(window); SetTitle(window);
//if (!SDL_SetRenderVSync(renderer, 1))
//{
// panic("SDL_SetRenderVSync");
//}
SDL_HideCursor(); SDL_HideCursor();
int red = 0; glewExperimental = GL_TRUE;
int green = 0; 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) { while (!loopShouldStop) {
SDL_Event e; SDL_Event e;
SDL_zero(e); SDL_zero(e);
@ -225,12 +266,11 @@ int main( int argc, char* argv[] )
} }
} }
/* SDL_SetRenderDrawColor(renderer, red, green, 0, 255); */ _GLBuffer();
/* SDL_RenderClear(renderer); */ if (!SDL_GL_SwapWindow(window)) {
SDL_LogCritical(SDL_LOG_CATEGORY_ASSERT, "SDL_GL_SwapWwindow: %s.", SDL_GetError());
glClearColor(0, 0, 0, 1); panicError();
glClear(GL_COLOR_BUFFER_BIT); }
SDL_GL_SwapWindow(window);
sleep_time = time_counter - GetTime(); sleep_time = time_counter - GetTime();
if (sleep_time * 1000 > delay_threshold) { if (sleep_time * 1000 > delay_threshold) {
@ -245,22 +285,11 @@ int main( int argc, char* argv[] )
while (time_counter > GetTime()) { while (time_counter > GetTime()) {
/* Waiting for the right moment. */ /* Waiting for the right moment. */
} }
/* red = (red + 1) % 256; */
/* green = (green + 1) % 64; */
time_counter += FrameTime; 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_DestroyWindow(window);
SDL_GL_DestroyContext(glcontext); SDL_GL_DestroyContext(glcontext);
SDL_Quit(); SDL_Quit();
exit(0); exit(EXIT_SUCCESS);
} }

8
vertex.glsl Normal file
View file

@ -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);
}