templates/SDLOpenGL/code/renderer.h

171 lines
8.0 KiB
C

#ifndef RENDERER_H
#define RENDERER_H
#pragma pack(push, 1)
typedef struct { float x; float y; } v2;
typedef struct { float x; float y; float z; float w; } v4;
typedef struct { v2 pos; v2 tex_coord; } render_target;
#pragma pack(pop)
typedef struct { v2 pos; float width; float height; float y_offset; float x_advance;} font_info;
typedef struct {int width; int height;} init_info;
typedef struct {GLuint vao; GLuint vbo; GLuint ebo; GLuint shader_program;} renderer_state;
static const char *vertex_shader_src =
"#version 330 core\n"
"layout (location = 0) in vec2 position;\n"
"layout (location = 1) in vec2 texture;\n"
"out vec2 texture_coords;\n"
"\n"
"void main()\n"
"{\n"
"\n"
" gl_Position = vec4(position.x, position.y, 1, 1);\n"
" texture_coords = texture;\n"
"\n"
"}\n"
"\n";
static const char *fragment_shader_src =
"#version 330 core\n"
"out vec4 frag_color;\n"
"in vec2 texture_coords;\n"
"\n"
"uniform sampler2D texture1;\n"
"uniform vec4 color;\n"
"\n"
"void main()\n"
"{\n"
"\n"
" frag_color = texture(texture1, texture_coords) * color;\n"
"\n"
"}\n"
"\n";
/*****************************************************************************
* text: the text to draw *
* x, y: the position, x and y are floats between -1 and 1 *
* color: the color of the text, 0xRRGGBBAA *
* scaler: the size of the text will be scaled by this value, 1 is default *
*****************************************************************************/
void r_draw_text(char *text, float x, float y, uint32_t color, float scaler);
/******************************************************************
* Takes a color represented as a 32 bit number *
* Outputs a 4 component vector with values between 0 and 1 *
*******************************************************************/
v4 extract_color_v4_from_u32(uint32_t in);
static font_info base_font_info[95] = {
{ 0.12890625f, 0.0f, 0.05859375f, 0.125f, 0.0f, 0.05859375f },
{ 0.19140625f, 0.0f, 0.05859375f, 0.125f, 0.0f, 0.05859375f },
{ 0.25390625f, 0.0f, 0.05859375f, 0.125f, 0.0f, 0.05859375f },
{ 0.31640625f, 0.0f, 0.05859375f, 0.125f, 0.0f, 0.05859375f },
{ 0.37890625f, 0.0f, 0.05859375f, 0.125f, 0.0f, 0.05859375f },
{ 0.44140625f, 0.0f, 0.05859375f, 0.125f, 0.0f, 0.05859375f },
{ 0.50390625f, 0.0f, 0.05859375f, 0.125f, 0.0f, 0.05859375f },
{ 0.56640625f, 0.0f, 0.05859375f, 0.125f, 0.0f, 0.05859375f },
{ 0.62890625f, 0.0f, 0.05859375f, 0.125f, 0.0f, 0.05859375f },
{ 0.69140625f, 0.0f, 0.05859375f, 0.125f, 0.0f, 0.05859375f },
{ 0.75390625f, 0.0f, 0.05859375f, 0.125f, 0.0f, 0.05859375f },
{ 0.81640625f, 0.0f, 0.05859375f, 0.125f, 0.0f, 0.05859375f },
{ 0.87890625f, 0.0f, 0.05859375f, 0.125f, 0.0f, 0.05859375f },
{ 0.0f, 0.12890625f, 0.05859375f, 0.125f, 0.0f, 0.05859375f },
{ 0.0625f, 0.12890625f, 0.05859375f, 0.125f, 0.0f, 0.05859375f },
{ 0.125f, 0.12890625f, 0.05859375f, 0.125f, 0.0f, 0.05859375f },
{ 0.1875f, 0.12890625f, 0.05859375f, 0.125f, 0.0f, 0.05859375f },
{ 0.25f, 0.12890625f, 0.05859375f, 0.125f, 0.0f, 0.05859375f },
{ 0.3125f, 0.12890625f, 0.05859375f, 0.125f, 0.0f, 0.05859375f },
{ 0.375f, 0.12890625f, 0.05859375f, 0.125f, 0.0f, 0.05859375f },
{ 0.4375f, 0.12890625f, 0.05859375f, 0.125f, 0.0f, 0.05859375f },
{ 0.5f, 0.12890625f, 0.05859375f, 0.125f, 0.0f, 0.05859375f },
{ 0.5625f, 0.12890625f, 0.05859375f, 0.125f, 0.0f, 0.05859375f },
{ 0.625f, 0.12890625f, 0.05859375f, 0.125f, 0.0f, 0.05859375f },
{ 0.6875f, 0.12890625f, 0.05859375f, 0.125f, 0.0f, 0.05859375f },
{ 0.75f, 0.12890625f, 0.05859375f, 0.125f, 0.0f, 0.05859375f },
{ 0.8125f, 0.12890625f, 0.05859375f, 0.125f, 0.0f, 0.05859375f },
{ 0.875f, 0.12890625f, 0.05859375f, 0.125f, 0.0f, 0.05859375f },
{ 0.9375f, 0.12890625f, 0.05859375f, 0.125f, 0.0f, 0.05859375f },
{ 0.0f, 0.2578125f, 0.05859375f, 0.125f, 0.0f, 0.05859375f },
{ 0.0625f, 0.2578125f, 0.05859375f, 0.125f, 0.0f, 0.05859375f },
{ 0.125f, 0.2578125f, 0.05859375f, 0.125f, 0.0f, 0.05859375f },
{ 0.1875f, 0.2578125f, 0.05859375f, 0.125f, 0.0f, 0.05859375f },
{ 0.25f, 0.2578125f, 0.05859375f, 0.125f, 0.0f, 0.05859375f },
{ 0.3125f, 0.2578125f, 0.05859375f, 0.125f, 0.0f, 0.05859375f },
{ 0.375f, 0.2578125f, 0.05859375f, 0.125f, 0.0f, 0.05859375f },
{ 0.4375f, 0.2578125f, 0.05859375f, 0.125f, 0.0f, 0.05859375f },
{ 0.5f, 0.2578125f, 0.05859375f, 0.125f, 0.0f, 0.05859375f },
{ 0.5625f, 0.2578125f, 0.05859375f, 0.125f, 0.0f, 0.05859375f },
{ 0.625f, 0.2578125f, 0.05859375f, 0.125f, 0.0f, 0.05859375f },
{ 0.6875f, 0.2578125f, 0.05859375f, 0.125f, 0.0f, 0.05859375f },
{ 0.75f, 0.2578125f, 0.05859375f, 0.125f, 0.0f, 0.05859375f },
{ 0.8125f, 0.2578125f, 0.05859375f, 0.125f, 0.0f, 0.05859375f },
{ 0.875f, 0.2578125f, 0.05859375f, 0.125f, 0.0f, 0.05859375f },
{ 0.9375f, 0.2578125f, 0.05859375f, 0.125f, 0.0f, 0.05859375f },
{ 0.0f, 0.38671875f, 0.05859375f, 0.125f, 0.0f, 0.05859375f },
{ 0.0625f, 0.38671875f, 0.05859375f, 0.125f, 0.0f, 0.05859375f },
{ 0.125f, 0.38671875f, 0.05859375f, 0.125f, 0.0f, 0.05859375f },
{ 0.1875f, 0.38671875f, 0.05859375f, 0.125f, 0.0f, 0.05859375f },
{ 0.0f, 0.0f, 0.0625f, 0.125f, 0.0f, 0.0625f },
{ 0.25f, 0.38671875f, 0.05859375f, 0.125f, 0.0f, 0.05859375f },
{ 0.3125f, 0.38671875f, 0.05859375f, 0.125f, 0.0f, 0.05859375f },
{ 0.375f, 0.38671875f, 0.05859375f, 0.125f, 0.0f, 0.05859375f },
{ 0.4375f, 0.38671875f, 0.05859375f, 0.125f, 0.0f, 0.05859375f },
{ 0.5f, 0.38671875f, 0.05859375f, 0.125f, 0.0f, 0.05859375f },
{ 0.5625f, 0.38671875f, 0.05859375f, 0.125f, 0.0f, 0.05859375f },
{ 0.625f, 0.38671875f, 0.05859375f, 0.125f, 0.0f, 0.05859375f },
{ 0.6875f, 0.38671875f, 0.05859375f, 0.125f, 0.0f, 0.05859375f },
{ 0.75f, 0.38671875f, 0.05859375f, 0.125f, 0.0f, 0.05859375f },
{ 0.8125f, 0.38671875f, 0.05859375f, 0.125f, 0.0f, 0.05859375f },
{ 0.875f, 0.38671875f, 0.05859375f, 0.125f, 0.0f, 0.05859375f },
{ 0.9375f, 0.38671875f, 0.05859375f, 0.125f, 0.0f, 0.05859375f },
{ 0.0f, 0.515625f, 0.05859375f, 0.125f, 0.0f, 0.05859375f },
{ 0.0625f, 0.515625f, 0.05859375f, 0.125f, 0.0f, 0.05859375f },
{ 0.125f, 0.515625f, 0.05859375f, 0.125f, 0.0f, 0.05859375f },
{ 0.1875f, 0.515625f, 0.05859375f, 0.125f, 0.0f, 0.05859375f },
{ 0.25f, 0.515625f, 0.05859375f, 0.125f, 0.0f, 0.05859375f },
{ 0.3125f, 0.515625f, 0.05859375f, 0.125f, 0.0f, 0.05859375f },
{ 0.375f, 0.515625f, 0.05859375f, 0.125f, 0.0f, 0.05859375f },
{ 0.4375f, 0.515625f, 0.05859375f, 0.125f, 0.0f, 0.05859375f },
{ 0.5f, 0.515625f, 0.05859375f, 0.125f, 0.0f, 0.05859375f },
{ 0.5625f, 0.515625f, 0.05859375f, 0.125f, 0.0f, 0.05859375f },
{ 0.625f, 0.515625f, 0.05859375f, 0.125f, 0.0f, 0.05859375f },
{ 0.6875f, 0.515625f, 0.05859375f, 0.125f, 0.0f, 0.05859375f },
{ 0.75f, 0.515625f, 0.05859375f, 0.125f, 0.0f, 0.05859375f },
{ 0.8125f, 0.515625f, 0.05859375f, 0.125f, 0.0f, 0.05859375f },
{ 0.875f, 0.515625f, 0.05859375f, 0.125f, 0.0f, 0.05859375f },
{ 0.9375f, 0.515625f, 0.05859375f, 0.125f, 0.0f, 0.05859375f },
{ 0.0f, 0.64453125f, 0.05859375f, 0.125f, 0.0f, 0.05859375f },
{ 0.0625f, 0.64453125f, 0.05859375f, 0.125f, 0.0f, 0.05859375f },
{ 0.125f, 0.64453125f, 0.05859375f, 0.125f, 0.0f, 0.05859375f },
{ 0.1875f, 0.64453125f, 0.05859375f, 0.125f, 0.0f, 0.05859375f },
{ 0.25f, 0.64453125f, 0.05859375f, 0.125f, 0.0f, 0.05859375f },
{ 0.3125f, 0.64453125f, 0.05859375f, 0.125f, 0.0f, 0.05859375f },
{ 0.375f, 0.64453125f, 0.05859375f, 0.125f, 0.0f, 0.05859375f },
{ 0.4375f, 0.64453125f, 0.05859375f, 0.125f, 0.0f, 0.05859375f },
{ 0.5f, 0.64453125f, 0.05859375f, 0.125f, 0.0f, 0.05859375f },
{ 0.5625f, 0.64453125f, 0.05859375f, 0.125f, 0.0f, 0.05859375f },
{ 0.625f, 0.64453125f, 0.05859375f, 0.125f, 0.0f, 0.05859375f },
{ 0.6875f, 0.64453125f, 0.05859375f, 0.125f, 0.0f, 0.05859375f },
{ 0.75f, 0.64453125f, 0.05859375f, 0.125f, 0.0f, 0.05859375f },
{ 0.8125f, 0.64453125f, 0.05859375f, 0.125f, 0.0f, 0.05859375f },
{ 0.875f, 0.64453125f, 0.05859375f, 0.125f, 0.0f, 0.05859375f },
{ 0.9375f, 0.64453125f, 0.05859375f, 0.125f, 0.0f, 0.05859375f },
{ 0.0f, 0.7734375f, 0.05859375f, 0.125f, 0.0f, 0.05859375f }
};
#endif //RENDERER_H