D_SharedState D_shared_state = ZI; //////////////////////////////// //~ Startup void D_StartupCore(void) { __prof; D_SharedState *g = &D_shared_state; u32 pixel_white = 0xFFFFFFFF; g->solid_white_texture = GPU_AllocTexture(GP_TEXTURE_FORMAT_R8G8B8A8_UNORM, 0, VEC2I32(1, 1), &pixel_white); } //////////////////////////////// //~ Material void D_DrawMaterial(GPU_RenderSig *sig, D_MaterialParams params) { GPU_RenderCmdDesc cmd = ZI; cmd.kind = GP_RENDER_CMD_KIND_DRAW_MATERIAL; cmd.material.xf = params.xf; cmd.material.texture = params.texture; cmd.material.clip = params.clip; cmd.material.tint = params.tint; cmd.material.is_light = params.is_light; cmd.material.light_emittance = params.light_emittance; GPU_PushRenderCmd(sig, &cmd); } //////////////////////////////// //~ Solid shapes void D_DrawPolyEx(GPU_RenderSig *sig, Vec2Array vertices, GPU_Indices indices, u32 color) { GPU_RenderCmdDesc cmd = ZI; cmd.kind = GP_RENDER_CMD_KIND_DRAW_UI_SHAPE; cmd.ui_shape.vertices = vertices; cmd.ui_shape.indices = indices; cmd.ui_shape.color = color; GPU_PushRenderCmd(sig, &cmd); } /* Draws a filled polygon using triangles in a fan pattern */ void D_DrawPoly(GPU_RenderSig *sig, Vec2Array vertices, u32 color) { if (vertices.count >= 3) { TempArena scratch = BeginScratchNoConflict(); u32 num_tris = vertices.count - 2; u32 num_indices = num_tris * 3; /* Generate indices in a fan pattern */ GPU_Indices indices = ZI; indices.count = num_indices; indices.indices = PushStructsNoZero(scratch.arena, u32, num_indices); for (u32 i = 0; i < num_tris; ++i) { u32 tri_offset = i * 3; indices.indices[tri_offset + 0] = 0; indices.indices[tri_offset + 1] = (i + 1); indices.indices[tri_offset + 2] = (i + 2); } D_DrawPolyEx(sig, vertices, indices, color); EndScratch(scratch); } } void D_DrawCircle(GPU_RenderSig *sig, Vec2 pos, f32 radius, u32 color, u32 detail) { TempArena scratch = BeginScratchNoConflict(); Vec2 *points = PushStructsNoZero(scratch.arena, Vec2, detail); for (u32 i = 0; i < detail; ++i) { f32 angle = ((f32)i / (f32)detail) * Tau; Vec2 p = VEC2( radius * CosF32(angle), radius * SinF32(angle) ); points[i] = AddVec2(pos, p); } Vec2Array vertices = { .points = points, .count = detail }; D_DrawPoly(sig, vertices, color); EndScratch(scratch); } void D_DrawQuad(GPU_RenderSig *sig, Quad quad, u32 color) { LocalPersist u32 indices_array[6] = { 0, 1, 2, 0, 2, 3 }; Vec2Array vertices = { .count = 4, .points = quad.e }; GPU_Indices indices = { .count = 6, .indices = indices_array }; D_DrawPolyEx(sig, vertices, indices, color); } //////////////////////////////// //~ Line shapes void D_DrawLineGradient(GPU_RenderSig *sig, Vec2 start, Vec2 end, f32 thickness, u32 start_color, u32 end_color) { #if 0 D_SharedState *g = &D_shared_state; Quad quad = QuadFromLine(start, end, thickness); D_DrawMaterial(sig, D_MATERIALPARAMS(.texture = g->solid_white_texture, .tint0 = start_color, .tint1 = end_color, .quad = quad)); #else /* Placeholder */ LAX end_color; Quad quad = QuadFromLine(start, end, thickness); D_DrawQuad(sig, quad, start_color); #endif } void D_DrawLine(GPU_RenderSig *sig, Vec2 start, Vec2 end, f32 thickness, u32 color) { Quad quad = QuadFromLine(start, end, thickness); D_DrawQuad(sig, quad, color); } void D_DrawRay(GPU_RenderSig *sig, Vec2 pos, Vec2 rel, f32 thickness, u32 color) { Quad quad = QuadFromRay(pos, rel, thickness); D_DrawQuad(sig, quad, color); } void D_DrawPolyLine(GPU_RenderSig *sig, Vec2Array points, b32 loop, f32 thickness, u32 color) { if (points.count >= 2) { for (u64 i = 1; i < points.count; ++i) { Vec2 p1 = points.points[i - 1]; Vec2 p2 = points.points[i]; Quad q = QuadFromLine(p1, p2, thickness); D_DrawQuad(sig, q, color); } if (loop && points.count > 2) { Vec2 p1 = points.points[points.count - 1]; Vec2 p2 = points.points[0]; Quad q = QuadFromLine(p1, p2, thickness); D_DrawQuad(sig, q, color); } } } void D_DrawCircleLine(GPU_RenderSig *sig, Vec2 pos, f32 radius, f32 thickness, u32 color, u32 detail) { TempArena scratch = BeginScratchNoConflict(); Vec2 *points = PushStructsNoZero(scratch.arena, Vec2, detail); for (u32 i = 0; i < detail; ++i) { f32 angle = ((f32)i / (f32)detail) * Tau; Vec2 p = VEC2( radius * CosF32(angle), radius * SinF32(angle) ); points[i] = AddVec2(pos, p); } Vec2Array a = { .points = points, .count = detail }; D_DrawPolyLine(sig, a, 1, thickness, color); EndScratch(scratch); } void D_DrawQuadLine(GPU_RenderSig *sig, Quad quad, f32 thickness, u32 color) { Vec2 points[] = { quad.p0, quad.p1, quad.p2, quad.p3 }; Vec2Array a = { .points = points, .count = countof(points) }; D_DrawPolyLine(sig, a, 1, thickness, color); } void D_DrawArrowLine(GPU_RenderSig *sig, Vec2 start, Vec2 end, f32 thickness, f32 arrowhead_height, u32 color) { const f32 head_width_ratio = 0.5f; /* Width of arrowhead relative to its length */ const f32 max_height_to_line_ratio = 0.9f; /* Maximum length of arrowhead relative to total line length */ arrowhead_height = MinF32(arrowhead_height, Vec2Len(SubVec2(end, start)) * max_height_to_line_ratio); Vec2 head_start_dir = SubVec2(start, end); head_start_dir = NormVec2(head_start_dir); head_start_dir = MulVec2(head_start_dir, arrowhead_height); Vec2 head_start = AddVec2(end, head_start_dir); Vec2 head_p1_dir = MulPerpVec2(head_start_dir, head_width_ratio); Vec2 head_p2_dir = NegVec2(head_p1_dir); Vec2 head_p1 = AddVec2(head_start, head_p1_dir); Vec2 head_p2 = AddVec2(head_start, head_p2_dir); Vec2 head_points[] = { end, head_p1, head_p2 }; Vec2Array head_points_v2_array = { .points = head_points, .count = countof(head_points) }; D_DrawPoly(sig, head_points_v2_array, color); Quad line_quad = QuadFromLine(start, head_start, thickness); D_DrawQuad(sig, line_quad, color); } void D_DrawArrowRay(GPU_RenderSig *sig, Vec2 pos, Vec2 rel, f32 thickness, f32 arrowhead_height, u32 color) { Vec2 end = AddVec2(pos, rel); D_DrawArrowLine(sig, pos, end, thickness, arrowhead_height, color); } void D_DrawColliderLine(GPU_RenderSig *sig, CLD_Shape shape, Xform shape_xf, f32 thickness, u32 color, u32 detail) { TempArena scratch = BeginScratchNoConflict(); Vec2Array poly = ZI; if (shape.radius == 0) { poly.count = shape.count; poly.points = PushStructsNoZero(scratch.arena, Vec2, shape.count); for (u32 i = 0; i < shape.count; ++i) { Vec2 p = MulXformV2(shape_xf, shape.points[i]); poly.points[i] = p; } } else { poly.count = detail; poly.points = PushStructsNoZero(scratch.arena, Vec2, detail); for (u32 i = 0; i < detail; ++i) { f32 angle = ((f32)i / (f32)detail) * Tau; Vec2 dir = VEC2(CosF32(angle), SinF32(angle)); Vec2 p = CLD_SupportPointFromDir(&shape, shape_xf, dir).p; poly.points[i] = p; } } D_DrawPolyLine(sig, poly, 1, thickness, color); EndScratch(scratch); } //////////////////////////////// //~ Grid void D_DrawGrid(GPU_RenderSig *sig, Xform xf, u32 bg0_color, u32 bg1_color, u32 line_color, u32 x_color, u32 y_color, f32 thickness, f32 spacing, Vec2 offset) { i32 grid_id = 0; { GPU_RenderCmdDesc cmd = ZI; cmd.kind = GP_RENDER_CMD_KIND_PUSH_GRID; cmd.grid.bg0_color = bg0_color; cmd.grid.bg1_color = bg1_color; cmd.grid.line_color = line_color; cmd.grid.x_color = x_color; cmd.grid.y_color = y_color; cmd.grid.line_thickness = thickness; cmd.grid.line_spacing = spacing; cmd.grid.offset = offset; grid_id = GPU_PushRenderCmd(sig, &cmd); } GPU_RenderCmdDesc cmd = ZI; cmd.kind = GP_RENDER_CMD_KIND_DRAW_MATERIAL; cmd.material.xf = xf; cmd.material.tint = ColorWhite; cmd.material.grid_cmd_id = grid_id; GPU_PushRenderCmd(sig, &cmd); } //////////////////////////////// //~ Ui void D_DrawUiRect(GPU_RenderSig *sig, D_UiRectParams params) { GPU_RenderCmdDesc cmd = ZI; cmd.kind = GP_RENDER_CMD_KIND_DRAW_UI_RECT; cmd.ui_rect.xf = params.xf; cmd.ui_rect.texture = params.texture; cmd.ui_rect.clip = params.clip; cmd.ui_rect.tint = params.tint; GPU_PushRenderCmd(sig, &cmd); } //////////////////////////////// //~ Text /* Returns the rect of the text area */ Rect draw_text(GPU_RenderSig *sig, D_TextParams params) { TempArena scratch = BeginScratchNoConflict(); f32 inv_font_image_width = 1.0 / (f32)params.font->image_width; f32 inv_font_image_height = 1.0 / (f32)params.font->image_height; f32 line_spacing = params.font->point_size * 1.5f * params.scale; //- Build line glyphs u64 num_lines = 0; f32 widest_line = 0; D_TextLine *first_line = 0; D_TextLine *last_line = 0; f32 first_line_top_offset = 0; f32 last_line_bottom_offset = 0; if (params.str.len > 0) { b32 string_done = 0; CodepointIter iter = BeginCodepointIter(params.str); while (!string_done) { f32 line_width = 0; f32 top_offset = 0; f32 bottom_offset = 0; u64 num_line_glyphs = 0; D_TextGlyph *line_glyphs = PushDry(scratch.arena, D_TextGlyph); b32 line_done = 0; while (!line_done) { string_done = !AdvanceCodepointIter(&iter); if (string_done) { line_done = 1; } else { u32 codepoint = iter.codepoint; if (codepoint == '\n') { line_done = 1; } else { D_TextGlyph *tg = PushStruct(scratch.arena, D_TextGlyph); ++num_line_glyphs; F_Glyph *glyph = F_GetGlyph(params.font, codepoint); tg->off_x = glyph->off_x * params.scale; tg->off_y = glyph->off_y * params.scale; tg->width = glyph->width * params.scale; tg->height = glyph->height * params.scale; tg->advance = glyph->advance * params.scale; Rect glyph_atlas_rect = glyph->atlas_rect; tg->clip = (ClipRect) { { glyph_atlas_rect.x * inv_font_image_width, glyph_atlas_rect.y * inv_font_image_height }, { (glyph_atlas_rect.x + glyph_atlas_rect.width) * inv_font_image_width, (glyph_atlas_rect.y + glyph_atlas_rect.height) * inv_font_image_height } }; line_width += tg->advance; top_offset = MinF32(top_offset, tg->off_y); bottom_offset = MaxF32(bottom_offset, tg->off_y + tg->height); } } } /* Line ended */ /* TODO: Only create nodes for non-empty lines. Embed line number in the node. */ D_TextLine *node = PushStruct(scratch.arena, D_TextLine); node->line_width = line_width; node->num_glyphs = num_line_glyphs; node->glyphs = line_glyphs; if (last_line) { last_line->next = node; } else { first_line = node; first_line_top_offset = top_offset; } last_line = node; last_line_bottom_offset = bottom_offset; widest_line = MaxF32(widest_line, line_width); ++num_lines; } EndCodepointIter(&iter); } //- Determine text bounds Rect bounds = ZI; bounds.x = params.pos.x; bounds.y = params.pos.y; bounds.width = widest_line; bounds.height = num_lines * line_spacing + first_line_top_offset + last_line_bottom_offset; /* Offset bounds X */ switch (params.offset_x) { case DRAW_TEXT_OFFSET_X_LEFT: break; case DRAW_TEXT_OFFSET_X_CENTER: { bounds.x -= bounds.width / 2.f; } break; case DRAW_TEXT_OFFSET_X_RIGHT: { bounds.x -= bounds.width; } break; } /* Offset bounds Y */ switch (params.offset_y) { case DRAW_TEXT_OFFSET_Y_TOP: break; case DRAW_TEXT_OFFSET_Y_CENTER: { bounds.y -= bounds.height / 2.f; } break; case DRAW_TEXT_OFFSET_Y_BOTTOM: { if (last_line) { bounds.y -= bounds.height + last_line_bottom_offset; } } break; } //- Draw lines u64 line_number = 0; for (D_TextLine *line = first_line; line; line = line->next) { Vec2 draw_pos = bounds.pos; draw_pos.y += line_number * line_spacing - first_line_top_offset; /* Alignment */ switch (params.alignment) { case DRAW_TEXT_ALIGNMENT_LEFT: break; case DRAW_TEXT_ALIGNMENT_CENTER: { draw_pos.x += (bounds.width - line->line_width) / 2.f; } break; case DRAW_TEXT_ALIGNMENT_RIGHT: { draw_pos.x += bounds.width - line->line_width; } break; } /* Draw glyphs in line */ for (u64 i = 0; i < line->num_glyphs; ++i) { D_TextGlyph *tg = &line->glyphs[i]; Vec2 pos = VEC2(draw_pos.x + tg->off_x, draw_pos.y + tg->off_y); Vec2 size = VEC2(tg->width, tg->height); Xform xf = XformFromRect(RectFromVec2(pos, size)); D_DrawUiRect(sig, D_UIRECTPARAMS(.xf = xf, .texture = params.font->texture, .tint = params.color, .clip = tg->clip)); draw_pos.x += tg->advance; } ++line_number; } EndScratch(scratch); return bounds; }