summaryrefslogtreecommitdiff
path: root/src/render
diff options
context:
space:
mode:
authornavewindre <boneyaard@gmail.com>2025-11-28 14:41:22 +0100
committerGitHub <noreply@github.com>2025-11-28 14:41:22 +0100
commit9c05c795d7b59c5ab94fb769f315c712b37df0cd (patch)
tree16cccf8cbb88703de798066a06f94013f89a8a5a /src/render
parentf8b92ce3aa08b1445c9f956d8166830946562d12 (diff)
parent3e094f20d4dda90e0356aba3f0abc4b7c7015844 (diff)
Merge pull request #1 from navewindre/windows-compat
Windows compat
Diffstat (limited to 'src/render')
-rw-r--r--src/render/gl.cpp62
-rw-r--r--src/render/gl.h5
-rw-r--r--src/render/gl_2d.cpp60
-rw-r--r--src/render/gl_2d_font.cpp13
-rw-r--r--src/render/gl_3d.cpp10
-rw-r--r--src/render/gl_3d.h4
6 files changed, 92 insertions, 62 deletions
diff --git a/src/render/gl.cpp b/src/render/gl.cpp
index 915722e..e44a165 100644
--- a/src/render/gl.cpp
+++ b/src/render/gl.cpp
@@ -29,7 +29,7 @@ GL_DATA* gl_create( I32* _canvas ) {
return 0;
}
- SDL_GL_SetAttribute( SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_ES );
+ SDL_GL_SetAttribute( SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_CORE );
SDL_GL_SetAttribute( SDL_GL_CONTEXT_MAJOR_VERSION, 3 );
SDL_GL_SetAttribute( SDL_GL_CONTEXT_MINOR_VERSION, 2 );
SDL_GL_SetAttribute( SDL_GL_DOUBLEBUFFER, 1 );
@@ -43,8 +43,8 @@ GL_DATA* gl_create( I32* _canvas ) {
GL_DATA* gl = new GL_DATA;
gl->window = SDL_CreateWindow(
"game",
- 0,
- 0,
+ SDL_WINDOWPOS_CENTERED,
+ SDL_WINDOWPOS_CENTERED,
_canvas[0],
_canvas[1],
SDL_WINDOW_OPENGL | SDL_WINDOW_SHOWN
@@ -52,22 +52,32 @@ GL_DATA* gl_create( I32* _canvas ) {
if( !gl->window ) {
dlog( "gl_init() could not create window: %s\n", SDL_GetError() );
- return 0;
- }
-
- U32 renderer_flags = SDL_RENDERER_ACCELERATED | SDL_RENDERER_TARGETTEXTURE;
- gl->renderer = SDL_CreateRenderer( gl->window, -1, renderer_flags );
- if( !gl->renderer ) {
- dlog( "gl_init() could not create renderer: %s\n", SDL_GetError() );
+ delete gl;
return 0;
}
gl->ctx = SDL_GL_CreateContext( gl->window );
if( !gl->ctx ) {
dlog( "gl_init() could not create context: %s\n", SDL_GetError() );
+ SDL_DestroyWindow( gl->window );
+ delete gl;
+ return 0;
+ }
+
+ SDL_GL_SetSwapInterval(0);
+ SDL_GL_MakeCurrent( gl->window, gl->ctx );
+
+ GLenum glewError = glewInit();
+ if( glewError != GLEW_OK ) {
+ dlog( "gl_create() : Failed to initialize GLEW: %s\n", glewGetErrorString(glewError) );
+ SDL_GL_DeleteContext( gl->ctx );
+ SDL_DestroyWindow( gl->window );
+ delete gl;
return 0;
}
+ while( glGetError() != GL_NO_ERROR ); // clear errors
+ //
glGetIntegerv( GL_MAX_TEXTURE_IMAGE_UNITS, &gl->shader_texture_limit );
if( gl->shader_texture_limit > 255 )
gl->shader_texture_limit = 255;
@@ -78,17 +88,19 @@ GL_DATA* gl_create( I32* _canvas ) {
memcpy( gl->canvas_size, _canvas, sizeof(I32) * 2 );
gl->clip_start = { 0, 0 };
gl->clip_dim = { (F32)gl->canvas_size[0], (F32)gl->canvas_size[1] };
+ glViewport( 0, 0, gl->canvas_size[0], gl->canvas_size[1] );
gl_inst = gl;
return gl;
}
void gl_gen_buffers( GL_DATA* gl ) {
- glGenBuffers( 1, &gl->vbuffer );
+ glGenVertexArrays( 1, &gl->vao );
+ glBindVertexArray( gl->vao );
+ glGenBuffers( 1, &gl->vbuffer );
glBindBuffer( GL_ARRAY_BUFFER, gl->vbuffer );
glBufferData( GL_ARRAY_BUFFER, 8192, 0, GL_STATIC_DRAW );
-
glBindBuffer( GL_ARRAY_BUFFER, 0 );
}
@@ -131,7 +143,7 @@ void gl_update_window( GL_DATA* gl, I32* size ) {
glUseProgram( 0 );
}
-STAT gl_shader_compile( GL_DATA* gl, GL_SHADER* shader ) {
+STAT gl_shader_compile( GL_DATA* gl, GL_SHADER_DEF* shader ) {
static char* log_buf = 0;
if( !log_buf )
log_buf = (char*)malloc( 8192 );
@@ -154,7 +166,7 @@ STAT gl_shader_compile( GL_DATA* gl, GL_SHADER* shader ) {
return STAT_OK;
}
-void gl_shader_destroy( GL_DATA* gl, GL_SHADER* shader ) {
+void gl_shader_destroy( GL_DATA* gl, GL_SHADER_DEF* shader ) {
if( shader->code )
free( (void*)shader->code );
}
@@ -264,7 +276,7 @@ GL_TEX2D* gl_texture_from_file( GL_DATA* gl, const char* name ) {
return tex;
}
-GL_TEX2D* gl_texture_from_bitmap( GL_DATA* gl, const char* name, U8* bitmap, U32 width, U32 height ) {
+GL_TEX2D* gl_texture_from_bitmap( GL_DATA* gl, const char* name, U8* bitmap, U32 w, U32 h ) {
GL_TEX2D* tex = gl_texture_create( gl, name );
glBindTexture( GL_TEXTURE_2D, tex->id );
glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT );
@@ -272,22 +284,16 @@ GL_TEX2D* gl_texture_from_bitmap( GL_DATA* gl, const char* name, U8* bitmap, U32
glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR );
glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR );
- tex->width = width;
- tex->height = height;
-
- glTexImage2D(
- GL_TEXTURE_2D,
- 0, GL_RGBA,
- (I32)width, (I32)height, 0,
- GL_RGBA, GL_UNSIGNED_BYTE, bitmap
- );
-
+ glTexImage2D( GL_TEXTURE_2D, 0, GL_RGBA, w, h, 0, GL_RGBA, GL_UNSIGNED_BYTE, bitmap );
+ glGenerateMipmap( GL_TEXTURE_2D );
glBindTexture( GL_TEXTURE_2D, 0 );
+ tex->width = w;
+ tex->height = h;
+
return tex;
}
-
GL_TEX2D* gl_texture_create( GL_DATA* gl, const char* name ) {
GL_TEX2D* tex = (GL_TEX2D*)malloc( sizeof(GL_TEX2D) );
strcpy( tex->name, name );
@@ -307,10 +313,6 @@ void gl_texture_destroy( GL_DATA* gl, GL_TEX2D* tex ) {
}
STAT gl_beginframe( GL_DATA* gl ) {
- SDL_SetRenderTarget( gl->renderer, 0 );
- SDL_SetRenderDrawColor( gl->renderer, 0, 0, 0, 255 );
- SDL_RenderClear( gl->renderer );
-
glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
gl->last_tick = u_tick();
return STAT_OK;
diff --git a/src/render/gl.h b/src/render/gl.h
index cf2b5dd..e4e2d5a 100644
--- a/src/render/gl.h
+++ b/src/render/gl.h
@@ -1,7 +1,8 @@
#pragma once
#include <SDL.h>
-#include <GLES2/gl2.h>
+// #include <GLES2/gl2.h>
+#include <GL/glew.h>
#include "../util.h"
#include "../util/matrix.h"
@@ -68,6 +69,8 @@ typedef struct GL_DATA {
VEC2 viewport_dim;
MAT4* proj_matrix;
+
+ GLuint vao;
} *PGL_DATA;
GL_DATA* gl_instance();
diff --git a/src/render/gl_2d.cpp b/src/render/gl_2d.cpp
index 44e7e00..78af1da 100644
--- a/src/render/gl_2d.cpp
+++ b/src/render/gl_2d.cpp
@@ -1,8 +1,8 @@
#include "gl_2d.h"
#include "../util.h"
-GL_PROGRAM* gl_2d_init( GL_DATA* gl, VEC2 screensize, const char* shadername ) {
- GL_PROGRAM* program = gl_program_create( gl, shadername );
+GL_SHADER_PROGRAM* gl_2d_init( GL_DATA* gl, VEC2 screensize, const char* shadername ) {
+ GL_SHADER_PROGRAM* program = gl_program_create( gl, shadername );
if( !OK( gl_program_compile( gl, program ) ) )
dlog( "gl_2d_init() : error compiling shader %s\n", shadername );
@@ -26,7 +26,7 @@ GL_PROGRAM* gl_2d_init( GL_DATA* gl, VEC2 screensize, const char* shadername ) {
return program;
}
-void gl_2d_line( GL_PROGRAM* gl2d, VEC2 start, VEC2 end, CLR col ) {
+void gl_2d_line( GL_SHADER_PROGRAM* gl2d, VEC2 start, VEC2 end, CLR col ) {
static const U16 order[] = { 0, 1 };
glUseProgram( gl2d->id );
@@ -49,7 +49,7 @@ void gl_2d_line( GL_PROGRAM* gl2d, VEC2 start, VEC2 end, CLR col ) {
glDrawElements( GL_LINES, 2, GL_UNSIGNED_SHORT, order );
}
-void gl_2d_rect( GL_PROGRAM* gl2d, VEC2 origin, VEC2 dim, CLR col ) {
+void gl_2d_rect( GL_SHADER_PROGRAM* gl2d, VEC2 origin, VEC2 dim, CLR col ) {
static const U16 order[] = { 0, 1, 2, 3, 4 };
glUseProgram( gl2d->id );
@@ -75,10 +75,11 @@ void gl_2d_rect( GL_PROGRAM* gl2d, VEC2 origin, VEC2 dim, CLR col ) {
glDrawElements( GL_LINE_STRIP, 5, GL_UNSIGNED_SHORT, order );
}
-void gl_2d_textured_frect( GL_PROGRAM* gl2d, VEC2 origin, VEC2 dim, GL_TEX2D* texture, CLR col, VEC2* uv, F32 rotation ) {
+void gl_2d_textured_frect( GL_SHADER_PROGRAM* gl2d, VEC2 origin, VEC2 dim, GL_TEX2D* texture, CLR col, VEC2* uv, F32 rotation ) {
static const U16 order[] = { 0, 1, 2, 3 };
glUseProgram( gl2d->id );
+
VERTEX vertices[] = {
{ { origin.x, origin.y }, uv? uv[0] : VEC2{ 0.f, 0.f }, col, 0 },
{ { origin.x + dim.x, origin.y }, uv? uv[1] : VEC2{ 1.f, 0.f }, col, 0 },
@@ -103,34 +104,43 @@ void gl_2d_textured_frect( GL_PROGRAM* gl2d, VEC2 origin, VEC2 dim, GL_TEX2D* te
glBindBuffer( GL_ARRAY_BUFFER, gl2d->gl->vbuffer );
glBufferData( GL_ARRAY_BUFFER, sizeof(vertices), &vertices[0], GL_DYNAMIC_DRAW );
+
I32 position = glGetAttribLocation( gl2d->id, "in_pos" );
+ I32 color = glGetAttribLocation( gl2d->id, "in_col" );
+ I32 texcoord = glGetAttribLocation( gl2d->id, "in_texcoord" );
+ I32 sampler = glGetAttribLocation( gl2d->id, "in_sampler" );
+
glEnableVertexAttribArray( position );
glVertexAttribPointer( position, 2, GL_FLOAT, 0, sizeof(VERTEX), 0 );
- I32 color = glGetAttribLocation( gl2d->id, "in_col" );
+
glEnableVertexAttribArray( color );
glVertexAttribPointer( color, 4, GL_FLOAT, 0, sizeof(VERTEX), &( (VERTEX*)nullptr)->clr );
- I32 texcoord = glGetAttribLocation( gl2d->id, "in_texcoord" );
+
glEnableVertexAttribArray( texcoord );
glVertexAttribPointer( texcoord, 2, GL_FLOAT, 0, sizeof(VERTEX), &( (VERTEX*)nullptr)->uv );
- I32 sampler = glGetAttribLocation( gl2d->id, "in_sampler" );
+
glEnableVertexAttribArray( sampler );
glVertexAttribPointer( sampler, 1, GL_UNSIGNED_BYTE, 1, sizeof(VERTEX), &( (VERTEX*)nullptr)->sampler );
+ glActiveTexture( GL_TEXTURE0 );
+ glBindTexture( GL_TEXTURE_2D, texture->id );
+
glBindBuffer( GL_ARRAY_BUFFER, 0 );
glActiveTexture( GL_TEXTURE0 );
glBindTexture( GL_TEXTURE_2D, texture->id );
- glUniform1f( glGetUniformLocation( gl2d->id, "in_time" ), u_time() );
+ // glUniform1f( glGetUniformLocation( gl2d->id, "in_time" ), u_time() );
glDrawElements( GL_TRIANGLE_STRIP, 4, GL_UNSIGNED_SHORT, order );
glBindTexture( GL_TEXTURE_2D, 0 );
}
-void gl_2d_frect( GL_PROGRAM* gl2d, VEC2 origin, VEC2 dim, CLR col ) {
+void gl_2d_frect( GL_SHADER_PROGRAM* gl2d, VEC2 origin, VEC2 dim, CLR col ) {
static const U16 order[] = { 0, 1, 2, 3 };
glUseProgram( gl2d->id );
+
VERTEX vertices[] = {
{ .pos = { origin.x, origin.y }, .uv = { 0.f, 0.f }, .clr = col },
{ .pos = { origin.x + dim.x, origin.y }, .uv = { 1.f, 0.f }, .clr = col },
@@ -140,21 +150,31 @@ void gl_2d_frect( GL_PROGRAM* gl2d, VEC2 origin, VEC2 dim, CLR col ) {
glBindBuffer( GL_ARRAY_BUFFER, gl2d->gl->vbuffer );
glBufferData( GL_ARRAY_BUFFER, sizeof(vertices), &vertices[0], GL_DYNAMIC_DRAW );
+
I32 position = glGetAttribLocation( gl2d->id, "in_pos" );
- glEnableVertexAttribArray( position );
- glVertexAttribPointer( position, 2, GL_FLOAT, 0, sizeof(VERTEX), 0 );
I32 color = glGetAttribLocation( gl2d->id, "in_col" );
- glEnableVertexAttribArray( color );
- glVertexAttribPointer( color, 4, GL_FLOAT, 0, sizeof(VERTEX), &( (VERTEX*)nullptr)->clr );
+
+ if (position >= 0) {
+ glEnableVertexAttribArray( position );
+ glVertexAttribPointer( position, 2, GL_FLOAT, 0, sizeof(VERTEX), 0 );
+ }
+
+ if (color >= 0) {
+ glEnableVertexAttribArray( color );
+ glVertexAttribPointer( color, 4, GL_FLOAT, 0, sizeof(VERTEX), &( (VERTEX*)nullptr)->clr );
+ }
+
I32 texcoord = glGetAttribLocation( gl2d->id, "in_texcoord" );
- glEnableVertexAttribArray( texcoord );
- glVertexAttribPointer( texcoord, 2, GL_FLOAT, 0, sizeof(VERTEX), &( (VERTEX*)nullptr)->uv );
+ if (texcoord >= 0) {
+ glEnableVertexAttribArray( texcoord );
+ glVertexAttribPointer( texcoord, 2, GL_FLOAT, 0, sizeof(VERTEX), &( (VERTEX*)nullptr)->uv );
+ }
glBindBuffer( GL_ARRAY_BUFFER, 0 );
glDrawElements( GL_TRIANGLE_STRIP, 4, GL_UNSIGNED_SHORT, order );
}
-void gl_2d_circle( GL_PROGRAM* gl2d, VEC2 origin, F32 radius, CLR col, U32 res ) {
+void gl_2d_circle( GL_SHADER_PROGRAM* gl2d, VEC2 origin, F32 radius, CLR col, U32 res ) {
static U16* order = 0;
const F32 step = 360.f / (F32)res;
@@ -195,7 +215,7 @@ void gl_2d_circle( GL_PROGRAM* gl2d, VEC2 origin, F32 radius, CLR col, U32 res )
free( vertices );
}
-void gl_2d_fcircle( GL_PROGRAM* gl2d, VEC2 origin, F32 radius, CLR col, U32 res ) {
+void gl_2d_fcircle( GL_SHADER_PROGRAM* gl2d, VEC2 origin, F32 radius, CLR col, U32 res ) {
static U16* order = 0;
const F32 step = 360.f / (F32)res;
@@ -248,7 +268,7 @@ void gl_2d_fcircle( GL_PROGRAM* gl2d, VEC2 origin, F32 radius, CLR col, U32 res
free( vertices );
}
-void gl_polygon( GL_PROGRAM* gl2d, VERTEX* vertices, U32 vertices_count ) {
+void gl_polygon( GL_SHADER_PROGRAM* gl2d, VERTEX* vertices, U32 vertices_count ) {
glUseProgram( gl2d->id );
glBindBuffer( GL_ARRAY_BUFFER, gl2d->gl->vbuffer );
@@ -274,7 +294,7 @@ void gl_polygon( GL_PROGRAM* gl2d, VERTEX* vertices, U32 vertices_count ) {
}
void gl_textured_polygon(
- GL_PROGRAM *gl2d,
+ GL_SHADER_PROGRAM *gl2d,
VERTEX *vertices,
U32 vertices_count,
GL_TEX2D *tex
diff --git a/src/render/gl_2d_font.cpp b/src/render/gl_2d_font.cpp
index 1dc498b..10ae71a 100644
--- a/src/render/gl_2d_font.cpp
+++ b/src/render/gl_2d_font.cpp
@@ -210,22 +210,27 @@ void gl_font_draw( GL_FONT* font, GL_SHADER_PROGRAM* shader, VEC2 origin, const
glBindBuffer( GL_ARRAY_BUFFER, shader->gl->vbuffer );
glBufferData( GL_ARRAY_BUFFER, sizeof(VERTEX) * 6 * len, vertices, GL_STATIC_DRAW );
+
I32 position = glGetAttribLocation( shader->id, "in_pos" );
+ I32 color = glGetAttribLocation( shader->id, "in_clr" );
+ I32 texcoord = glGetAttribLocation( shader->id, "in_texcoord" );
+ I32 sampler = glGetAttribLocation( shader->id, "in_sampler" );
+
glEnableVertexAttribArray( position );
glVertexAttribPointer( position, 2, GL_FLOAT, 0, sizeof(VERTEX), &( (VERTEX*)nullptr)->pos );
- I32 color = glGetAttribLocation( shader->id, "in_clr" );
glEnableVertexAttribArray( color );
glVertexAttribPointer( color, 4, GL_FLOAT, 0, sizeof(VERTEX), &( (VERTEX*)nullptr)->clr );
- I32 texcoord = glGetAttribLocation( shader->id, "in_texcoord" );
glEnableVertexAttribArray( texcoord );
glVertexAttribPointer( texcoord, 2, GL_FLOAT, 0, sizeof(VERTEX), &( (VERTEX*)nullptr)->uv );
- I32 sampler = glGetAttribLocation( shader->id, "in_sampler" );
glEnableVertexAttribArray( sampler );
glVertexAttribPointer( sampler, 1, GL_UNSIGNED_BYTE, 1, sizeof(VERTEX), &( (VERTEX*)nullptr)->sampler );
+ glActiveTexture( GL_TEXTURE0 );
+ glBindTexture( GL_TEXTURE_2D, font->atlas->id );
+
glBindBuffer( GL_ARRAY_BUFFER, 0 );
glActiveTexture( GL_TEXTURE0 );
@@ -234,7 +239,7 @@ void gl_font_draw( GL_FONT* font, GL_SHADER_PROGRAM* shader, VEC2 origin, const
glDrawElements( GL_TRIANGLES, len * 6, GL_UNSIGNED_SHORT, indices );
glBindTexture( GL_TEXTURE_2D, 0 );
-
+
free( vertices );
free( indices );
free( coords );
diff --git a/src/render/gl_3d.cpp b/src/render/gl_3d.cpp
index 78e7f5e..40162e8 100644
--- a/src/render/gl_3d.cpp
+++ b/src/render/gl_3d.cpp
@@ -3,8 +3,8 @@
#include "gl.h"
#include "gl_batch.h"
-GL_3D* gl_3d_init( GL_DATA* gl, VEC2 screensize, const char* shadername ) {
- GL_3D *gl3d = (GL_3D*)gl_program_create( gl, shadername, sizeof(GL_3D) );
+GL3D* gl_3d_init( GL_DATA* gl, VEC2 screensize, const char* shadername ) {
+ GL3D *gl3d = (GL3D*)gl_program_create( gl, shadername, sizeof(GL3D) );
if( !OK( gl_program_compile( gl, gl3d ) ) )
dlog( "gl_2d_init() : error compiling shader %s\n", shadername );
@@ -17,8 +17,8 @@ GL_3D* gl_3d_init( GL_DATA* gl, VEC2 screensize, const char* shadername ) {
return gl3d;
}
-void gl_3d_projection_setup( GL_PROGRAM *_gl3d, VEC3 pos, F32 fov_deg, F32 yaw, F32 pitch, F32 near, F32 far, VEC2 winsize ) {
- GL_3D* gl3d = (GL_3D*)_gl3d;
+void gl_3d_projection_setup( GL_SHADER_PROGRAM *_gl3d, VEC3 pos, F32 fov_deg, F32 yaw, F32 pitch, F32 near, F32 far, VEC2 winsize ) {
+ GL3D* gl3d = (GL3D*)_gl3d;
MAT4 proj, yaw_rot, pitch_rot, view, transl, tmp;
F32 fov_rad = m_deg2rad( fov_deg );
@@ -63,7 +63,7 @@ void gl_3d_batch_setup( GL_BATCH3D *batch ) {
}
void gl_3d_polygon(
- GL_PROGRAM* gl3d,
+ GL_SHADER_PROGRAM* gl3d,
VERTEX3D* vertices,
U32 vertices_count,
GL_TEX2D* tex
diff --git a/src/render/gl_3d.h b/src/render/gl_3d.h
index 531c3a3..69f171a 100644
--- a/src/render/gl_3d.h
+++ b/src/render/gl_3d.h
@@ -13,7 +13,7 @@ struct VERTEX3D {
};
using GL_BATCH3D = GL_BATCH<VERTEX3D>;
-struct GL_3D : GL_SHADER_PROGRAM {
+struct GL3D : GL_SHADER_PROGRAM {
F32 aspect;
MAT4 projmat;
@@ -21,7 +21,7 @@ struct GL_3D : GL_SHADER_PROGRAM {
};
// shader init -----------------------------------------------------------
-extern GL_3D* gl_3d_init( GL_DATA* gl, VEC2 screensize, const char* shadername );
+extern GL3D* gl_3d_init( GL_DATA* gl, VEC2 screensize, const char* shadername );
// sets up projection matrix and sends it to the gpu ------------------------------------------------------------
extern void gl_3d_projection_setup( GL_SHADER_PROGRAM* gl3d, VEC3 pos, F32 fov_deg, F32 yaw, F32 pitch, F32 near, F32 far, VEC2 winsize );
extern void gl_3d_batch_setup( GL_BATCH3D* batch );