finally compiling and running, text still needs rework

This commit is contained in:
2024-09-16 16:17:00 +03:00
parent 551d60ef85
commit 16c96010dc
43 changed files with 299 additions and 309 deletions

View File

@ -1,5 +1,5 @@
#include "townengine/util.h"
#include "townengine/context.h"
#include "twn_util.h"
#include "twn_engine_context_c.h"
#include "twn_rendering_c.h"
#include <SDL2/SDL.h>

View File

@ -1,28 +1,68 @@
static gpu_texture new_gl_texture(void) {
#include "twn_gpu_texture_c.h"
#include "twn_util.h"
gpu_texture create_gpu_texture(enum texture_filter filter, bool generate_mipmaps) {
GLuint texture;
glGenTextures(1, &texture);
glBindTexture(GL_TEXTURE_2D, texture);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_GENERATE_MIPMAP, GL_TRUE);
glTexParameteri(GL_TEXTURE_2D, GL_GENERATE_MIPMAP, (GLboolean)generate_mipmaps);
if (filter == TEXTURE_FILTER_NEAREAST) {
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
} else if (filter == TEXTURE_FILTER_LINEAR) {
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
}
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
#if !defined(EMSCRIPTEN)
glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
#endif
glBindTexture(GL_TEXTURE_2D, 0);
return create_gpu_texture(TEXTURE_FILTER_NEAREST, true);
return texture;
}
void delete_gpu_texture(gpu_texture texture) {
glDeleteTextures(1, &texture);
}
void upload_gpu_texture(gpu_texture texture, void *pixels, int channels, int width, int height) {
glBindTexture(GL_TEXTURE_2D, texture);
int format_internal, format;
if (channels == 4) {
format_internal = GL_RGBA8;
format = GL_RGBA;
} else if (channels == 1) {
format_internal = GL_ALPHA;
format = GL_ALPHA;
} else {
CRY("upload_gpu_texture", "Unsupported channel count");
return;
}
glTexImage2D(GL_TEXTURE_2D,
0,
GL_RGBA8,
surface->w,
surface->h,
format_internal,
width,
height,
0,
GL_RGBA,
format,
GL_UNSIGNED_BYTE,
surface->pixels);
pixels);
glBindTexture(GL_TEXTURE_2D, 0);
}
void bind_gpu_texture(gpu_texture texture) {
glBindTexture(GL_TEXTURE_2D, texture);
}

View File

@ -1,8 +1,7 @@
#include "twn_gl_15_rendering_c.h"
#include "twn_rendering_c.h"
#include "townengine/util.h"
#include "townengine/config.h"
#include "townengine/context.h"
#include "twn_util.h"
#include "twn_config.h"
#include "twn_engine_context_c.h"
#include "twn_text_c.h"
#include <glad/glad.h>
@ -206,6 +205,7 @@ void use_texture_mode(enum texture_mode mode) {
vertex_buffer_builder build_vertex_buffer(vertex_buffer buffer, size_t bytes) {
glBindBuffer(GL_ARRAY_BUFFER, buffer);
glBufferData(GL_ARRAY_BUFFER, bytes, NULL, GL_STREAM_DRAW);
void *mapping = glMapBuffer(GL_ARRAY_BUFFER, GL_WRITE_ONLY);
if (!mapping)
CRY("build_vertex_buffer", "Error mapping a vertex array buffer");
@ -231,17 +231,19 @@ bool push_to_vertex_buffer_builder(vertex_buffer_builder *builder,
return false;
}
builder->mapping = (void *)((uintptr_t)builder->mapping + size);
return true;
}
void finally_render_sprites(const struct primitive_2d primitives[],
const struct sprite_batch batch,
const vertex_buffer vertex_buffer)
const vertex_buffer buffer)
{
/* TODO: maybe do, dunno */
// glBindBuffer(GL_VERTEX_ARRAY, vertex_buffer);
(void)vertex_buffer;
(void)buffer;
GLsizei off;
GLsizei voff;
@ -360,13 +362,13 @@ bool push_sprite_payload_to_vertex_buffer_builder(struct sprite_batch batch,
void finally_draw_uncolored_space_traingle_batch(const struct mesh_batch *batch,
const t_texture_key texture_key,
const vertex_buffer vertex_buffer)
const vertex_buffer buffer)
{
const size_t primitives_len = arrlenu(batch->primitives);
textures_bind(&ctx.texture_cache, texture_key);
glBindBuffer(GL_ARRAY_BUFFER, vertex_buffer);
glBindBuffer(GL_ARRAY_BUFFER, buffer);
/* vertex specification*/
glEnableClientState(GL_VERTEX_ARRAY);
@ -399,7 +401,9 @@ bool push_text_payload_to_vertex_buffer_builder(struct font_data const *font_dat
stbtt_aligned_quad quad)
{
(void)font_data;
(void)builder;
/* TODO: use vertex arrays */
glTexCoord2f(quad.s0, quad.t0);
glVertex2f(quad.x0, quad.y0);
glTexCoord2f(quad.s1, quad.t0);
@ -408,6 +412,8 @@ bool push_text_payload_to_vertex_buffer_builder(struct font_data const *font_dat
glVertex2f(quad.x1, quad.y1);
glTexCoord2f(quad.s0, quad.t1);
glVertex2f(quad.x0, quad.y1);
return false;
}
@ -416,6 +422,9 @@ void finally_draw_text(struct font_data const *font_data,
t_color color,
vertex_buffer buffer)
{
(void)len;
(void)buffer;
use_texture_mode(TEXTURE_MODE_GHOSTLY);
glBindTexture(GL_TEXTURE_2D, font_data->texture);

View File

@ -1,65 +0,0 @@
#ifndef TWN_GL_15_RENDERING_H
#define TWN_GL_15_RENDERING_H
/*
* OpenGL 1.5 and any 2.0+ compatibility version render implementation.
*/
#include "twn_rendering_c.h"
#include "twn_gl_any_rendering_c.h"
#ifdef EMSCRIPTEN
#include <GLES2/gl2.h>
#else
#include <glad/glad.h>
#endif
#include <stb_truetype.h>
void render_circle(const struct circle_primitive *circle);
void render_rectangle(const struct rect_primitive *rectangle);
void use_space_pipeline(void);
void use_2d_pipeline(void);
void use_texture_mode(enum texture_mode mode);
/* uses present in 1.5 buffer mapping feature */
vertex_buffer_builder build_vertex_buffer(vertex_buffer buffer, size_t bytes);
/* collects bytes for sending to the gpu until all is pushed, which is when false is returned */
bool push_to_vertex_buffer_builder(vertex_buffer_builder *builder,
void *bytes,
size_t size);
void finally_render_sprites(struct primitive_2d const primitives[],
struct sprite_batch batch,
vertex_buffer buffer);
size_t get_sprite_payload_size(struct sprite_batch batch);
bool push_sprite_payload_to_vertex_buffer_builder(struct sprite_batch batch,
vertex_buffer_builder *builder,
t_fvec2 v0, t_fvec2 v1, t_fvec2 v2, t_fvec2 v3,
t_fvec2 uv0, t_fvec2 uv1, t_fvec2 uv2, t_fvec2 uv3,
t_color color);
void finally_draw_uncolored_space_traingle_batch(struct mesh_batch const *batch,
t_texture_key texture_key,
vertex_buffer buffer);
size_t get_text_payload_size(void);
bool push_text_payload_to_vertex_buffer_builder(struct font_data const *font_data,
vertex_buffer_builder *builder,
stbtt_aligned_quad quad);
void finally_draw_text(struct font_data const *font_data,
size_t len,
t_color color,
vertex_buffer buffer);
#endif

View File

@ -1,6 +1,6 @@
#include "twn_gl_any_rendering_c.h"
#include "townengine/context.h"
#include "townengine/util.h"
#include "twn_rendering_c.h"
#include "twn_engine_context_c.h"
#include "twn_util.h"
#ifdef EMSCRIPTEN
#include <GLES2/gl2.h>
@ -13,7 +13,6 @@ void setup_viewport(int x, int y, int width, int height) {
glViewport(x, y, width, height);
}
//////// VERTEX BUFFER ////////
vertex_buffer create_vertex_buffer(void) {
GLuint result;
@ -32,8 +31,6 @@ void specify_vertex_buffer(vertex_buffer buffer, void *data, size_t bytes) {
glBufferData(GL_ARRAY_BUFFER, bytes, data, GL_STREAM_DRAW);
}
//////// END OF VERTEX BUFFER ////////
void bind_quad_element_buffer(void) {
static GLuint buffer = 0;

View File

@ -1,43 +0,0 @@
#ifndef TWN_GL_ANY_RENDERING_H
#define TWN_GL_ANY_RENDERING_H
/*
* Any OpenGL version base render methods.
*/
#ifdef EMSCRIPTEN
#include <GLES2/gl2.h>
#else
#include <glad/glad.h>
#endif
#include <stdbool.h>
#define QUAD_ELEMENT_BUFFER_LENGTH (65536 / 6)
typedef GLuint vertex_buffer;
typedef struct vertex_buffer_builder {
size_t bytes_left;
void *mapping;
} vertex_buffer_builder;
vertex_buffer create_vertex_buffer(void);
void delete_vertex_buffer(vertex_buffer buffer);
void specify_vertex_buffer(vertex_buffer buffer, void *data, size_t bytes);
void setup_viewport(int x, int y, int width, int height);
void bind_quad_element_buffer(void);
void clear_draw_buffer(void);
void swap_buffers(void);
void set_depth_range(double low, double high);
#endif

View File

@ -1,15 +1,8 @@
#ifndef TWN_GPU_TEXTURE_H
#define TWN_GPU_TEXTURE_H
#ifdef EMSCRIPTEN
#include <GLES2/gl2.h>
#else
#include <glad/glad.h>
#endif
#ifndef TWN_GPU_TEXTURE_C_H
#define TWN_GPU_TEXTURE_C_H
#include <stdbool.h>
typedef GLuint gpu_texture;
enum texture_filter {
@ -17,12 +10,11 @@ enum texture_filter {
TEXTURE_FILTER_LINEAR,
};
gpu_texture create_gpu_texture(enum texture_filter filter, bool generate_mipmaps);
void delete_gpu_texture(gpu_texture texture);
void specify_gpu_texture(gpu_texture texture, void *pixels, int channels, int width, int height);
void upload_gpu_texture(gpu_texture texture, void *pixels, int channels, int width, int height);
void bind_gpu_texture(gpu_texture texture);

View File

@ -1,10 +1,7 @@
#include "twn_rendering_c.h"
#include "townengine/twn_rendering.h"
#include "townengine/textures/internal_api.h"
#include "townengine/context.h"
#include "townengine/camera.h"
#include "twn_rendering_platform.h"
#include "twn_rendering.h"
#include "twn_engine_context_c.h"
#include "twn_camera.h"
#include <SDL2/SDL.h>
#include <stb_ds.h>
@ -114,23 +111,23 @@ void render(void) {
textures_update_atlas(&ctx.texture_cache);
/* fit rendering context onto the resizable screen */
if (ctx.window_size_has_changed) {
if ((float)ctx.window_w / (float)ctx.window_h > RENDER_BASE_RATIO) {
float ratio = (float)ctx.window_h / (float)RENDER_BASE_HEIGHT;
if (ctx.game.window_size_has_changed) {
if ((float)ctx.game.window_w / (float)ctx.game.window_h > RENDER_BASE_RATIO) {
float ratio = (float)ctx.game.window_h / (float)RENDER_BASE_HEIGHT;
int w = (int)((float)RENDER_BASE_WIDTH * ratio);
setup_viewport(
ctx.window_w / 2 - w / 2,
ctx.game.window_w / 2 - w / 2,
0,
w,
ctx.window_h
ctx.game.window_h
);
} else {
float ratio = (float)ctx.window_w / (float)RENDER_BASE_WIDTH;
float ratio = (float)ctx.game.window_w / (float)RENDER_BASE_WIDTH;
int h = (int)((float)RENDER_BASE_HEIGHT * ratio);
setup_viewport(
0,
ctx.window_h / 2 - h / 2,
ctx.window_w,
ctx.game.window_h / 2 - h / 2,
ctx.game.window_w,
h
);
}

View File

@ -1,17 +1,35 @@
#ifndef RENDERING_INTERNAL_API_H
#define RENDERING_INTERNAL_API_H
#ifndef TWN_RENDERING_C_H
#define TWN_RENDERING_C_H
#include "townengine/textures/internal_api.h"
#include "townengine/util.h"
#include "townengine/macros/option.h"
#include "twn_textures_c.h"
#include "twn_util.h"
#include "twn_option.h"
#include <SDL2/SDL.h>
#include <stb_truetype.h>
#ifdef EMSCRIPTEN
#include <GLES2/gl2.h>
#else
#include <glad/glad.h>
#endif
#include <stdbool.h>
extern t_matrix4 camera_projection_matrix;
extern t_matrix4 camera_look_at_matrix;
#define QUAD_ELEMENT_BUFFER_LENGTH (65536 / 6)
typedef GLuint vertex_buffer;
typedef struct vertex_buffer_builder {
size_t bytes_left;
void *mapping;
} vertex_buffer_builder;
struct sprite_primitive {
t_frect rect;
t_color color;
@ -100,22 +118,13 @@ struct text_cache {
struct font_data **data;
};
/* renders the background, then the primitives in all render queues */
void render(void);
/* clears all render queues */
void render_queue_clear(void);
void push_circle(t_fvec2 position, float radius, t_color color);
void unfurl_triangle(const char *path,
t_fvec3 v0,
t_fvec3 v1,
t_fvec3 v2,
t_shvec2 uv0,
t_shvec2 uv1,
t_shvec2 uv2);
void create_circle_geometry(t_fvec2 position,
t_color color,
float radius,
@ -136,10 +145,77 @@ void render_sprites(const struct primitive_2d primitives[],
void draw_uncolored_space_traingle_batch(struct mesh_batch *batch,
t_texture_key texture_key);
/* text */
void render_text(const struct text_primitive *text);
void text_cache_init(struct text_cache *cache);
void text_cache_deinit(struct text_cache *cache);
/* vertex buffer */
vertex_buffer create_vertex_buffer(void);
void delete_vertex_buffer(vertex_buffer buffer);
void specify_vertex_buffer(vertex_buffer buffer, void *data, size_t bytes);
/* uses present in 1.5 buffer mapping feature */
vertex_buffer_builder build_vertex_buffer(vertex_buffer buffer, size_t bytes);
/* collects bytes for sending to the gpu until all is pushed, which is when false is returned */
bool push_to_vertex_buffer_builder(vertex_buffer_builder *builder,
void *bytes,
size_t size);
/* state */
void setup_viewport(int x, int y, int width, int height);
void clear_draw_buffer(void);
void swap_buffers(void);
void set_depth_range(double low, double high);
void bind_quad_element_buffer(void);
void render_circle(const struct circle_primitive *circle);
void render_rectangle(const struct rect_primitive *rectangle);
void use_space_pipeline(void);
void use_2d_pipeline(void);
void use_texture_mode(enum texture_mode mode);
void finally_render_sprites(struct primitive_2d const primitives[],
struct sprite_batch batch,
vertex_buffer buffer);
size_t get_sprite_payload_size(struct sprite_batch batch);
bool push_sprite_payload_to_vertex_buffer_builder(struct sprite_batch batch,
vertex_buffer_builder *builder,
t_fvec2 v0, t_fvec2 v1, t_fvec2 v2, t_fvec2 v3,
t_fvec2 uv0, t_fvec2 uv1, t_fvec2 uv2, t_fvec2 uv3,
t_color color);
void finally_draw_uncolored_space_traingle_batch(struct mesh_batch const *batch,
t_texture_key texture_key,
vertex_buffer buffer);
size_t get_text_payload_size(void);
bool push_text_payload_to_vertex_buffer_builder(struct font_data const *font_data,
vertex_buffer_builder *builder,
stbtt_aligned_quad quad);
void finally_draw_text(struct font_data const *font_data,
size_t len,
t_color color,
vertex_buffer buffer);
#endif

View File

@ -1,10 +0,0 @@
#ifndef TWN_RENDERING_PLATFORM_H
#define TWN_RENDERING_PLATFORM_H
#ifdef EMSCRIPTEN
#include "twn_gl_es2_rendering_c.h"
#else
#include "twn_gl_15_rendering_c.h"
#endif
#endif

View File

@ -1,9 +1,8 @@
#include "townengine/twn_rendering.h"
#include "twn_rendering.h"
#include "twn_rendering_c.h"
#include "townengine/context.h"
#include "townengine/util.h"
#include "townengine/textures/internal_api.h"
#include "twn_rendering_platform.h"
#include "twn_engine_context_c.h"
#include "twn_util.h"
#include "twn_textures_c.h"
#include <stb_ds.h>
@ -103,7 +102,7 @@ void render_sprites(const struct primitive_2d primitives[],
if (vertex_array == 0)
vertex_array = create_vertex_buffer();
use_sprite_blendmode(batch.mode);
use_texture_mode(batch.mode);
const t_frect dims =
textures_get_dims(&ctx.texture_cache, primitives->sprite.texture_key);

View File

@ -1,10 +1,8 @@
#include "twn_rendering_c.h"
#include "townengine/util.h"
#include "townengine/config.h"
#include "townengine/context.h"
#include "townengine/twn_rendering.h"
#include "twn_rendering_platform.h"
#include "twn_rendering.h"
#include "twn_util.h"
#include "twn_config.h"
#include "twn_engine_context_c.h"
#include <stb_truetype.h>
@ -66,7 +64,7 @@ static struct font_data *text_load_font_data(const char *path, int height_px) {
}
font_data->texture = create_gpu_texture(TEXT_FONT_FILTERING, true);
specify_gpu_texture(
upload_gpu_texture(
font_data->texture,
bitmap,
1,

View File

@ -1,5 +1,3 @@
#include "twn_rendering_platform.h"
#include <stb_truetype.h>

View File

@ -1,7 +1,6 @@
#include "twn_rendering_c.h"
#include "twn_context.h"
#include "twn_engine_context_c.h"
#include "twn_textures_c.h"
#include "twn_rendering_platform.h"
#include <stb_ds.h>