a-game

2D platformer written from scratch.
git clone git://git.amin.space/a-game.git
Log | Files | Refs | README | LICENSE

webgl.h (12013B)


      1 #ifndef WEBGL_H
      2 #define WEBGL_H
      3 
      4 typedef int    i32;
      5 typedef float  f32;
      6 typedef double f64;
      7 
      8 // NOTE(amin): Since these functions will be implemented in javascript, we can
      9 // only use i32, f32, and f64 params.
     10 void webglActiveTexture(i32 texture);
     11 void webglAttachShader(i32 program, i32 shader);
     12 void webglBindBuffer(i32 target, i32 buffer);
     13 void webglBindTexture(i32 target, i32 texture);
     14 void webglBindVertexArray(i32 vao);
     15 void webglBlendColor(f32 r, f32 g, f32 b, f32 a);
     16 void webglBlendFunc(i32 sfactor, i32 dfactor);
     17 void webglBufferData(i32 target, i32 size, i32 data, i32 usage);
     18 void webglClear(i32 mask);
     19 void webglClearColor(f32 r, f32 g, f32 b, f32 a);
     20 void webglCompileShader(i32 shader);
     21 i32  webglCreateBuffer(void);
     22 i32  webglCreateProgram(void);
     23 i32  webglCreateShader(i32 type);
     24 i32  webglCreateTexture(void);
     25 i32  webglCreateVertexArray(void);
     26 void webglDeleteBuffer(i32 bo);
     27 void webglDeleteShader(i32 shader);
     28 void webglDeleteVertexArray(i32 vao);
     29 void webglDepthMask(i32 flag);
     30 void webglDisable(i32 cap);
     31 void webglDrawElements(i32 mode, i32 count, i32 type, i32 offset);
     32 void webglEnable(i32 cap);
     33 void webglEnableVertexAttribArray(i32 index);
     34 void webglGenerateMipmap(i32 target);
     35 void webglGetProgramInfoLog(void);
     36 int  webglGetProgramParameter(i32 program, i32 param);
     37 void webglGetShaderInfoLog(i32 shader, char *out_buf);
     38 int  webglGetShaderParameter(i32 shader, i32 param);
     39 i32  webglGetUniformLocation(i32 program, const char name[static 1], i32 name_len);
     40 void webglLinkProgram(i32 program);
     41 void webglScissor(i32 x, i32 y, i32 width, i32 height);
     42 void webglShaderSource(i32 shader, const char source[static 1], i32 source_len);
     43 void webglTexImage2D(i32 target, i32 level, i32 internalformat, i32 width, i32 height, i32 border, i32 format, i32 type, i32 pixels);
     44 void webglTexImage3D(i32 target, i32 level, i32 internalformat, i32 width, i32 height, i32 depth, i32 border, i32 format, i32 type, i32 pixels);
     45 void webglTexParameteri(i32 target, i32 pname, i32 param);
     46 void webglTexStorage3D(i32 target, i32 levels, i32 internalformat, i32 width, i32 height, i32 depth);
     47 void webglTexSubImage3D (i32 target, i32 level, i32 xoffset, i32 yoffset, i32 zoffset, i32 width, i32 height, i32 depth, i32 format, i32 type, i32 pixels);
     48 void webglUniform1f(i32 location, f32 value);
     49 void webglUniform1i(i32 location, i32 value);
     50 void webglUniform3f(i32 location, f32 x, f32 y, f32 z);
     51 void webglUniformMatrix4fv(i32 location, i32 transpose, const f32 data[static 16]);
     52 void webglUseProgram(i32 program);
     53 void webglVertexAttribPointer(i32 index, i32 size, i32 type, i32 normalized, i32 stride, i32 offset);
     54 void webglViewport(i32 x, i32 y, i32 width, i32 height);
     55 
     56 #define GL_DEPTH_BUFFER_BIT 0x00000100
     57 #define GL_COLOR_BUFFER_BIT 0x00004000
     58 #define GL_FALSE 0
     59 #define GL_TRUE 1
     60 #define GL_LINES 0x0001
     61 #define GL_TRIANGLES 0x0004
     62 #define GL_SRC_ALPHA 0x0302
     63 #define GL_ONE_MINUS_SRC_ALPHA 0x0303
     64 #define GL_FRONT_AND_BACK 0x0408
     65 #define GL_DEPTH_TEST 0x0B71
     66 #define GL_BLEND 0x0BE2
     67 #define GL_SCISSOR_TEST 0x0C11
     68 #define GL_TEXTURE_2D 0x0DE1
     69 #define GL_UNSIGNED_BYTE 0x1401
     70 #define GL_UNSIGNED_INT 0x1405
     71 #define GL_FLOAT 0x1406
     72 #define GL_RGBA 0x1908
     73 #define GL_FILL 0x1B02
     74 #define GL_LINEAR 0x2601
     75 #define GL_LINEAR_MIPMAP_LINEAR 0x2703
     76 #define GL_TEXTURE_MAG_FILTER 0x2800
     77 #define GL_TEXTURE_MIN_FILTER 0x2801
     78 #define GL_TEXTURE_WRAP_S 0x2802
     79 #define GL_TEXTURE_WRAP_T 0x2803
     80 #define GL_CONSTANT_ALPHA 0x8003
     81 #define GL_RGBA8 0x8058
     82 #define GL_CLAMP_TO_EDGE 0x812F
     83 #define GL_TEXTURE0 0x84C0
     84 #define GL_ARRAY_BUFFER 0x8892
     85 #define GL_ELEMENT_ARRAY_BUFFER 0x8893
     86 #define GL_STATIC_DRAW 0x88E4
     87 #define GL_FRAGMENT_SHADER 0x8B30
     88 #define GL_VERTEX_SHADER 0x8B31
     89 #define GL_COMPILE_STATUS 0x8B81
     90 #define GL_LINK_STATUS 0x8B82
     91 #define GL_TEXTURE_2D_ARRAY 0x8C1A
     92 
     93 typedef unsigned int  GLenum;
     94 typedef unsigned char GLboolean;
     95 typedef unsigned int  GLbitfield;
     96 typedef void          GLvoid;
     97 typedef int           GLint;
     98 typedef unsigned int  GLuint;
     99 typedef int           GLsizei;
    100 typedef float         GLfloat;
    101 typedef char          GLchar;
    102 typedef long          GLsizeiptr;
    103 
    104 // TODO: add a version with safety checks
    105 // TODO: add a version that explicitely converts NULL to 0 (see todo in JS
    106 // loader)
    107 #define WEBGL_CAST_I32(x) (i32)(x)
    108 
    109 static i32 _webgl_strlen(const char *str)
    110 {
    111     i32 len = 0;
    112     while(str[len] != '\0')
    113     {
    114         len++;
    115     }
    116     return len;
    117 }
    118 
    119 static void glActiveTexture(GLenum texture)
    120 {
    121     webglActiveTexture(WEBGL_CAST_I32(texture));
    122 }
    123 
    124 static void glAttachShader(GLuint program, GLuint shader)
    125 {
    126     webglAttachShader(WEBGL_CAST_I32(program), WEBGL_CAST_I32(shader));
    127 }
    128 
    129 static void glBindBuffer(GLenum target, GLuint buffer)
    130 {
    131     webglBindBuffer(WEBGL_CAST_I32(target), WEBGL_CAST_I32(buffer));
    132 }
    133 
    134 static void glBindTexture(GLenum target, GLuint texture)
    135 {
    136     webglBindTexture(WEBGL_CAST_I32(target), WEBGL_CAST_I32(texture));
    137 }
    138 
    139 static void glBindVertexArray(GLuint array)
    140 {
    141     webglBindVertexArray(WEBGL_CAST_I32(array));
    142 }
    143 
    144 static void glBlendColor(GLfloat red, GLfloat green, GLfloat blue, GLfloat alpha)
    145 {
    146     webglBlendColor((f32)red, (f32)green, (f32)blue, (f32)alpha);
    147 }
    148 
    149 static void glBlendFunc(GLenum sfactor, GLenum dfactor)
    150 {
    151     webglBlendFunc(WEBGL_CAST_I32(sfactor), WEBGL_CAST_I32(dfactor));
    152 }
    153 
    154 static void glBufferData(GLenum target, GLsizeiptr size, const void *data, GLenum usage)
    155 {
    156     webglBufferData(WEBGL_CAST_I32(target), WEBGL_CAST_I32(size), WEBGL_CAST_I32(data), WEBGL_CAST_I32(usage));
    157 }
    158 
    159 static void glClear(GLbitfield mask)
    160 {
    161     webglClear(WEBGL_CAST_I32(mask));
    162 }
    163 
    164 static void glClearColor(GLfloat red, GLfloat green, GLfloat blue, GLfloat alpha)
    165 {
    166     webglClearColor((f32)red, (f32)green, (f32)blue, (f32)alpha);
    167 }
    168 
    169 static void glCompileShader(GLuint shader)
    170 {
    171     webglCompileShader(WEBGL_CAST_I32(shader));
    172 }
    173 
    174 static GLuint glCreateProgram(void)
    175 {
    176     i32 program_id = webglCreateProgram();
    177     return (GLuint)program_id;
    178 }
    179 
    180 static GLuint glCreateShader(GLenum type)
    181 {
    182     return webglCreateShader(WEBGL_CAST_I32(type));
    183 }
    184 
    185 static void glDeleteBuffers(GLsizei n, const GLuint *buffers)
    186 {
    187     i32 the_buffer = WEBGL_CAST_I32(buffers[0]);
    188     webglDeleteBuffer(the_buffer);
    189 }
    190 
    191 static void glDeleteShader(GLuint shader)
    192 {
    193     webglDeleteShader(WEBGL_CAST_I32(shader));
    194 }
    195 
    196 static void glDeleteVertexArrays(GLsizei n, const GLuint *arrays)
    197 {
    198     i32 the_array = WEBGL_CAST_I32(arrays[0]);
    199     webglDeleteVertexArray(the_array);
    200 }
    201 
    202 static void glDepthMask(GLboolean flag)
    203 {
    204     webglDepthMask(WEBGL_CAST_I32(flag));
    205 }
    206 
    207 static void glDisable(GLenum cap)
    208 {
    209     webglDisable(WEBGL_CAST_I32(cap));
    210 }
    211 
    212 static void glDrawElements(GLenum mode, GLsizei count, GLenum type, const void *indices)
    213 {
    214     webglDrawElements(WEBGL_CAST_I32(mode), WEBGL_CAST_I32(count), WEBGL_CAST_I32(type), WEBGL_CAST_I32(indices));
    215 }
    216 
    217 static void glEnable(GLenum cap)
    218 {
    219     webglEnable(WEBGL_CAST_I32(cap));
    220 }
    221 
    222 static void glEnableVertexAttribArray(GLuint index)
    223 {
    224     webglEnableVertexAttribArray(WEBGL_CAST_I32(index));
    225 }
    226 
    227 static void glGenBuffers(GLsizei n, GLuint *buffers)
    228 {
    229     i32 buffer_id = webglCreateBuffer();
    230     *buffers = (GLuint)buffer_id;
    231 }
    232 
    233 static void glGenerateMipmap(GLenum target)
    234 {
    235     webglGenerateMipmap(WEBGL_CAST_I32(target));
    236 }
    237 
    238 static void glGenTextures(GLsizei n, GLuint *textures)
    239 {
    240     i32 texture_id = webglCreateTexture();
    241     *textures = (GLuint)texture_id;
    242 }
    243 
    244 static void glGenVertexArrays(GLsizei n, GLuint *arrays)
    245 {
    246     i32 vao_id = webglCreateVertexArray();
    247     *arrays = (GLuint)vao_id;
    248 }
    249 
    250 static void glGetProgramInfoLog(GLuint program, GLsizei bufSize, GLsizei *length, GLchar *infoLog)
    251 {
    252     // TODO: implement
    253     //webglGetProgramInfoLog(WEBGL_CAST_I32(program), WEBGL_CAST_I32(bufsize), WEBGL_CAST_I32(*length), WEBGL_CAST_I32(*infoLog));
    254 }
    255 
    256 static void glGetProgramiv(GLuint program, GLenum pname, GLint *params)
    257 {
    258     *params = webglGetProgramParameter(WEBGL_CAST_I32(program), WEBGL_CAST_I32(pname));
    259 }
    260 
    261 static void glGetShaderInfoLog(GLuint shader, GLsizei bufSize, GLsizei *length, GLchar *infoLog)
    262 {
    263     webglGetShaderInfoLog(WEBGL_CAST_I32(shader), infoLog);
    264 }
    265 
    266 static void glGetShaderiv(GLuint shader, GLenum pname, GLint *params)
    267 {
    268     *params = webglGetShaderParameter(WEBGL_CAST_I32(shader), WEBGL_CAST_I32(pname));
    269 }
    270 
    271 static GLint glGetUniformLocation(GLuint program, const GLchar *name)
    272 {
    273     i32 name_len = _webgl_strlen(name);
    274     return webglGetUniformLocation(WEBGL_CAST_I32(program), name, name_len);
    275 }
    276 
    277 static void glLinkProgram(GLuint program)
    278 {
    279     webglLinkProgram(WEBGL_CAST_I32(program));
    280 }
    281 
    282 static void glPolygonMode(GLenum face, GLenum mode)
    283 {
    284     // No Op. This doesn't exist in webgl
    285 }
    286 
    287 static void glScissor(GLint x, GLint y, GLsizei width, GLsizei height)
    288 {
    289     webglScissor(WEBGL_CAST_I32(x), WEBGL_CAST_I32(y), WEBGL_CAST_I32(width), WEBGL_CAST_I32(height));
    290 }
    291 
    292 static void glShaderSource(GLuint shader, GLsizei count, const GLchar *const *string, const GLint *length)
    293 {
    294     const GLchar *s = (void *)*string;
    295     i32 l = _webgl_strlen(s);
    296     webglShaderSource(WEBGL_CAST_I32(shader), s, l);
    297 }
    298 
    299 static void glTexImage2D(GLenum target, GLint level, GLint internalformat, GLsizei width, GLsizei height, GLint border, GLenum format, GLenum type, const void *pixels)
    300 {
    301     webglTexImage2D(
    302             WEBGL_CAST_I32(target),
    303             WEBGL_CAST_I32(level),
    304             WEBGL_CAST_I32(internalformat),
    305             WEBGL_CAST_I32(width),
    306             WEBGL_CAST_I32(height),
    307             WEBGL_CAST_I32(border),
    308             WEBGL_CAST_I32(format),
    309             WEBGL_CAST_I32(type),
    310             WEBGL_CAST_I32(pixels));
    311 }
    312 
    313 static void glTexImage3D(GLenum target, GLint level, GLint internalformat, GLsizei width, GLsizei height, GLsizei depth, GLint border, GLenum format, GLenum type, const void *pixels)
    314 {
    315     webglTexImage3D(
    316             WEBGL_CAST_I32(target),
    317             WEBGL_CAST_I32(level),
    318             WEBGL_CAST_I32(internalformat),
    319             WEBGL_CAST_I32(width),
    320             WEBGL_CAST_I32(height),
    321             WEBGL_CAST_I32(depth),
    322             WEBGL_CAST_I32(border),
    323             WEBGL_CAST_I32(format),
    324             WEBGL_CAST_I32(type),
    325             WEBGL_CAST_I32(pixels));
    326 }
    327 
    328 static void glTexParameteri(GLenum target, GLenum pname, GLint param)
    329 {
    330     webglTexParameteri(WEBGL_CAST_I32(target), WEBGL_CAST_I32(pname), WEBGL_CAST_I32(param));
    331 }
    332 
    333 static void glTexStorage3D(GLenum target, GLsizei levels, GLenum internalformat, GLsizei width, GLsizei height, GLsizei depth)
    334 {
    335     webglTexStorage3D(target, levels, internalformat, width, height, depth);
    336 }
    337 
    338 static void glTexSubImage3D (GLenum target, GLint level, GLint xoffset, GLint yoffset, GLint zoffset, GLsizei width, GLsizei height, GLsizei depth, GLenum format, GLenum type, const void *pixels)
    339 {
    340     webglTexSubImage3D(target, level, xoffset, yoffset, zoffset, width, height, depth, format, type, WEBGL_CAST_I32(pixels));
    341 }
    342 
    343 static void glUniform1f(GLint location, GLfloat v0)
    344 {
    345     webglUniform1f(WEBGL_CAST_I32(location), (f32)v0);
    346 }
    347 
    348 static void glUniform1i(GLint location, GLint v0)
    349 {
    350     webglUniform1i(WEBGL_CAST_I32(location), WEBGL_CAST_I32(v0));
    351 }
    352 
    353 static void glUniform3f(GLint location, GLfloat v0, GLfloat v1, GLfloat v2)
    354 {
    355     webglUniform3f(WEBGL_CAST_I32(location), (f32)v0, (f32)v1, (f32)v2);
    356 }
    357 
    358 static void glUniformMatrix4fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *value)
    359 {
    360     webglUniformMatrix4fv(WEBGL_CAST_I32(location), WEBGL_CAST_I32(transpose), value);
    361 }
    362 
    363 static void glUseProgram(GLuint program)
    364 {
    365     webglUseProgram(WEBGL_CAST_I32(program));
    366 }
    367 
    368 static void glVertexAttribPointer(GLuint index, GLint size, GLenum type, GLboolean normalized, GLsizei stride, const void *pointer)
    369 {
    370     webglVertexAttribPointer(WEBGL_CAST_I32(index), WEBGL_CAST_I32(size), WEBGL_CAST_I32(type), WEBGL_CAST_I32(normalized), WEBGL_CAST_I32(stride), WEBGL_CAST_I32(pointer));
    371 }
    372 
    373 static void glViewport(GLint x, GLint y, GLsizei width, GLsizei height)
    374 {
    375     webglViewport(x, y, WEBGL_CAST_I32(width), WEBGL_CAST_I32(height));
    376 }
    377 
    378 #undef WEBGL_CAST_I32
    379 
    380 #endif //WEBGL_H