Advertisement
microwerx

Goldfish #1

Apr 29th, 2023 (edited)
915
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.71 KB | Source Code | 0 0
  1. /// H File Follows
  2.  
  3. /**
  4.  * Copyright (c) 2023 Jonathan Metzgar
  5.  *
  6.  * This software is released under the MIT License.
  7.  * https://opensource.org/licenses/MIT
  8.  */
  9.  
  10. #include <SDL2/SDL_opengl.h>
  11. #include <SDL2/SDL_opengl_glext.h>
  12. #include "object.h"
  13.  
  14. struct GfFunction_t
  15. {
  16.     Object_t self;
  17. } GfFunction_t, *GfFunction;
  18.  
  19. typedef struct GfPipelineState_t
  20. {
  21.     Object_t self;
  22.  
  23.     GLuint program;
  24. } GfPipelineState_t, *GfPipelineState;
  25.  
  26. struct GfBuffer_t
  27. {
  28.  
  29. } GfBuffer_t, *GfBuffer;
  30.  
  31. GfPipelineState GfNewPipeline(const char *vertSource, const char *fragSource);
  32. void GfUsePipeline(GfPipelineState pso);
  33.  
  34.  
  35. /// C File follows
  36.  
  37.  
  38. /**
  39.  * Copyright (c) 2023 Jonathan Metzgar
  40.  *
  41.  * This software is released under the MIT License.
  42.  * https://opensource.org/licenses/MIT
  43.  */
  44. #include <stdio.h>
  45.  
  46. #define GL_SILENCE_DEPRECATION 1
  47. #include <OpenGL/gl3.h>
  48. #include "goldfish.h"
  49.  
  50. void Log(const char *label, const char *str)
  51. {
  52.     fprintf(stderr, "%s: %s\n", label, str);
  53. }
  54.  
  55. GLuint CompileShader(const char *source, GLenum type)
  56. {
  57.     if (source == NULL)
  58.         return 0;
  59.     GLuint shader = glCreateShader(type);
  60.     glShaderSource(shader, 1, &source, NULL);
  61.     glCompileShader(shader);
  62.     GLint status;
  63.     glGetShaderiv(shader, GL_COMPILE_STATUS, &status);
  64.     if (status != GL_TRUE)
  65.     {
  66.         GLchar infoLog[1024];
  67.         GLsizei length;
  68.         glGetShaderInfoLog(shader, 1024, &length, infoLog);
  69.         Log("error", infoLog);
  70.         glDeleteShader(shader);
  71.         return 0;
  72.     }
  73.     return shader;
  74. }
  75.  
  76. GLuint CompileProgram(const char *vertSource, const char *fragSource)
  77. {
  78.     GLuint vs = CompileShader(vertSource, GL_VERTEX_SHADER);
  79.     GLuint fs = CompileShader(fragSource, GL_FRAGMENT_SHADER);
  80.     GLuint program = glCreateProgram();
  81.     if (vs != 0)
  82.     {
  83.         glAttachShader(program, vs);
  84.     }
  85.     if (fs != 0)
  86.     {
  87.         glAttachShader(program, fs);
  88.     }
  89.     glLinkProgram(program);
  90.     GLint status;
  91.     glGetProgramiv(program, GL_LINK_STATUS, &status);
  92.     if (status != GL_TRUE)
  93.     {
  94.         GLchar infoLog[1024];
  95.         GLsizei length;
  96.         glGetProgramInfoLog(program, 1024, &length, infoLog);
  97.         Log("error", infoLog);
  98.         glDeleteProgram(program);
  99.         program = 0;
  100.     }
  101.     glDeleteShader(vs);
  102.     glDeleteShader(fs);
  103.     return program;
  104. }
  105.  
  106. GfPipelineState GfNewPipeline(const char *vertSource, const char *fragSource)
  107. {
  108.     GfPipelineState pso = (GfPipelineState)allocObject(sizeof(GfPipelineState_t));
  109.    
  110.     pso->program = CompileProgram(vertSource, fragSource);
  111.  
  112.     return pso;
  113. }
  114.  
  115. void GfUsePipeline(GfPipelineState pso)
  116. {
  117.     if (pso == NULL)
  118.     {
  119.         return;
  120.     }
  121.     glUseProgram(pso->program);
  122. }
  123.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement