add line drawing
This commit is contained in:
		| @@ -85,8 +85,7 @@ endif() | ||||
| if(TWN_RENDERING_API MATCHES OPENGL_15) | ||||
|         set(SYSTEM_SOURCE_FILES ${SYSTEM_SOURCE_FILES} | ||||
|                 src/rendering/twn_gl_any_rendering.c | ||||
|                 src/rendering/twn_gl_15_rendering.c | ||||
|                 src/rendering/twn_gl_15_gpu_texture.c) | ||||
|                 src/rendering/twn_gl_15_rendering.c) | ||||
| endif() | ||||
|  | ||||
| set(TWN_THIRD_PARTY_SOURCE_FILES | ||||
| @@ -142,7 +141,7 @@ set_target_properties(${TWN_TARGET} PROPERTIES | ||||
|                                     C_STANDARD_REQUIRED ON | ||||
|                                     C_EXTENSIONS ON) # extensions are required by stb_ds.h | ||||
|  | ||||
| add_compile_definitions(${TWN_TARGET} $<$<BOOL:${TWN_FEATURE_PUSH_AUDIO}>:TWN_FEATURE_PUSH_AUDIO>) | ||||
| target_compile_definitions(${TWN_TARGET} PRIVATE $<$<BOOL:${TWN_FEATURE_PUSH_AUDIO}>:TWN_FEATURE_PUSH_AUDIO>) | ||||
|  | ||||
| # precompile commonly used not-so-small headers | ||||
| target_precompile_headers(${TWN_TARGET} PRIVATE | ||||
|   | ||||
| @@ -42,6 +42,11 @@ TWN_API void draw_nine_slice(char const *texture, | ||||
|                              float       border_thickness,  /* optional, default: 0 */ | ||||
|                              Color       color);            /* optional, default: all 255 */ | ||||
|  | ||||
| TWN_API void draw_line(Vec2 start, | ||||
|                        Vec2 finish, | ||||
|                        float thickness, /* optional, default: 1 */ | ||||
|                        Color color);    /* optional, default: all 255 */ | ||||
|  | ||||
| /* pushes a textured 3d triangle onto the render queue */ | ||||
| /* texture coordinates are in pixels */ | ||||
| TWN_API void draw_triangle(char const *texture, | ||||
|   | ||||
| @@ -3,6 +3,7 @@ | ||||
| #include "twn_engine_context_c.h" | ||||
| #include "twn_camera_c.h" | ||||
| #include "twn_types.h" | ||||
| #include "twn_util.h" | ||||
| #include "twn_vec.h" | ||||
| #include "twn_deferred_commands.h" | ||||
|  | ||||
| @@ -284,6 +285,20 @@ static void render_2d(void) { | ||||
|                 break; | ||||
|             } | ||||
|  | ||||
|             /* TODO: batching */ | ||||
|             case PRIMITIVE_2D_LINE: { | ||||
|                 struct Render2DInvocation const invocation = { | ||||
|                     .primitive = current, | ||||
|                     .layer = layer, | ||||
|                 }; | ||||
|  | ||||
|                 if (current->line.color.a != 255) | ||||
|                     arrput(ghostly_invocations, invocation); | ||||
|                 else | ||||
|                     arrput(opaque_invocations, invocation); | ||||
|                 break; | ||||
|             } | ||||
|  | ||||
|             case PRIMITIVE_2D_TEXT: { | ||||
|                 struct Render2DInvocation const invocation = { | ||||
|                     .primitive = current, | ||||
| @@ -320,6 +335,9 @@ static void render_2d(void) { | ||||
|             case PRIMITIVE_2D_CIRCLE: | ||||
|                 render_circle(&invocation.primitive->circle); | ||||
|                 break; | ||||
|             case PRIMITIVE_2D_LINE: | ||||
|                 render_line(&invocation.primitive->line); | ||||
|                 break; | ||||
|             case PRIMITIVE_2D_TEXT: | ||||
|             default: | ||||
|                 SDL_assert(false); | ||||
| @@ -349,6 +367,9 @@ static void render_2d(void) { | ||||
|             case PRIMITIVE_2D_TEXT: | ||||
|                 render_text(&invocation.primitive->text); | ||||
|                 break; | ||||
|             case PRIMITIVE_2D_LINE: | ||||
|                 render_line(&invocation.primitive->line); | ||||
|                 break; | ||||
|             default: | ||||
|                 SDL_assert(false); | ||||
|         } | ||||
| @@ -480,3 +501,28 @@ void issue_deferred_draw_commands(void) { | ||||
|         } | ||||
|     } | ||||
| } | ||||
|  | ||||
|  | ||||
| /* TODO: Support thickness */ | ||||
| void draw_line(Vec2 start, | ||||
|                Vec2 finish, | ||||
|                float thickness, | ||||
|                Color color) | ||||
| { | ||||
|     if (fabsf(1.0f - thickness) >= 0.00001f) | ||||
|         log_warn("Thickness isn't yet implemented for line drawing (got %f)", (double)thickness); | ||||
|  | ||||
|     LinePrimitive line = { | ||||
|         .start = start, | ||||
|         .finish = finish, | ||||
|         .thickness = thickness, | ||||
|         .color = color, | ||||
|     }; | ||||
|  | ||||
|     Primitive2D primitive = { | ||||
|         .type = PRIMITIVE_2D_LINE, | ||||
|         .line = line, | ||||
|     }; | ||||
|  | ||||
|     arrput(ctx.render_queue_2d, primitive); | ||||
| } | ||||
|   | ||||
| @@ -56,6 +56,13 @@ typedef struct SpritePrimitive { | ||||
|     bool repeat; | ||||
| } SpritePrimitive; | ||||
|  | ||||
| typedef struct LinePrimitive { | ||||
|     Vec2 start; | ||||
|     Vec2 finish; | ||||
|     float thickness; | ||||
|     Color color; | ||||
| } LinePrimitive; | ||||
|  | ||||
| typedef struct RectPrimitive { | ||||
|     Rect rect; | ||||
|     Color color; | ||||
| @@ -77,6 +84,7 @@ typedef struct TextPrimitive { | ||||
|  | ||||
| typedef enum Primitive2DType { | ||||
|     PRIMITIVE_2D_SPRITE, | ||||
|     PRIMITIVE_2D_LINE, | ||||
|     PRIMITIVE_2D_RECT, | ||||
|     PRIMITIVE_2D_CIRCLE, | ||||
|     PRIMITIVE_2D_TEXT, | ||||
| @@ -87,6 +95,7 @@ typedef struct Primitive2D { | ||||
|  | ||||
|     union { | ||||
|         SpritePrimitive sprite; | ||||
|         LinePrimitive line; | ||||
|         RectPrimitive rect; | ||||
|         CirclePrimitive circle; | ||||
|         TextPrimitive text; | ||||
| @@ -288,6 +297,8 @@ VertexBuffer get_circle_element_buffer(void); | ||||
|  | ||||
| void render_circle(const CirclePrimitive *circle); | ||||
|  | ||||
| void render_line(const LinePrimitive *line); | ||||
|  | ||||
| void render_rectangle(const RectPrimitive *rectangle); | ||||
|  | ||||
| void finally_render_quads(Primitive2D const primitives[], | ||||
|   | ||||
| @@ -548,3 +548,13 @@ void finally_draw_command(DeferredCommandDraw command) { | ||||
|     glBindBuffer(GL_ARRAY_BUFFER, 0); | ||||
|     glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0); | ||||
| } | ||||
|  | ||||
| void render_line(const LinePrimitive *line) { | ||||
|     finally_use_2d_pipeline(); | ||||
|     glBegin(GL_LINES); | ||||
|         glColor4b(line->color.r, line->color.g, line->color.b, line->color.a); | ||||
|         glVertex2f(line->start.x, line->start.y); | ||||
|         glColor4b(line->color.r, line->color.g, line->color.b, line->color.a); | ||||
|         glVertex2f(line->finish.x, line->finish.y); | ||||
|     glEnd(); | ||||
| } | ||||
|   | ||||
| @@ -170,6 +170,4 @@ void push_quad_payload_to_vertex_buffer_builder(struct QuadBatch batch, | ||||
|  | ||||
|         ((ElementIndexedQuadWithoutColorWithoutTexture *)builder->base)[index] = payload; | ||||
|     } | ||||
|  | ||||
|     SDL_assert(false); | ||||
| } | ||||
|   | ||||
		Reference in New Issue
	
	Block a user