From 930d67a15bd03920a68276be0a7889dbc4841fe9 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Jean-Philippe=20Bruy=C3=A8re?= Date: Sat, 21 Apr 2018 05:40:12 +0200 Subject: [PATCH] some code clean and comments --- src/vkvg_context.c | 43 +++++++++++++++---------------------- src/vkvg_context_internal.c | 2 +- src/vkvg_context_internal.h | 20 ++++++++++------- tests/test1.c | 14 ++++++------ 4 files changed, 37 insertions(+), 42 deletions(-) diff --git a/src/vkvg_context.c b/src/vkvg_context.c index a6d6abe..cd59d27 100644 --- a/src/vkvg_context.c +++ b/src/vkvg_context.c @@ -155,7 +155,6 @@ void vkvg_destroy (VkvgContext ctx) free(ctx); } - void vkvg_close_path (VkvgContext ctx){ if (ctx->pathPtr % 2 == 0)//current path is empty return; @@ -335,46 +334,39 @@ void vkvg_clip (VkvgContext ctx){ void vkvg_fill_preserve (VkvgContext ctx){ if (ctx->pathPtr == 0) //nothing to fill return; - if (ctx->pathPtr % 2 != 0) //current path is no close + if (ctx->pathPtr % 2 != 0) //current path is not finished, close it vkvg_close_path(ctx); - if (ctx->pointCount * 4 > ctx->sizeIndices - ctx->indCount) + if (ctx->pointCount * 4 > ctx->sizeIndices - ctx->indCount)//flush if vk buff is full vkvg_flush(ctx); - uint32_t lastPathPointIdx, i = 0, ptrPath = 0;; + uint32_t ptrPath = 0;; Vertex v = {}; v.uv.z = -1; while (ptrPath < ctx->pathPtr){ - if (!_path_is_closed(ctx,ptrPath)){ - ptrPath+=2; - continue; - } + if (!_path_is_closed(ctx, ptrPath)) + //close path + ctx->pathes[ptrPath+1] = ctx->pathes[ptrPath]; + uint32_t firstPtIdx = ctx->pathes[ptrPath]; - lastPathPointIdx = _get_last_point_of_closed_path (ctx, ptrPath); - uint32_t pathPointCount = lastPathPointIdx - ctx->pathes[ptrPath] + 1; + uint32_t lastPtIdx = _get_last_point_of_closed_path (ctx, ptrPath); + uint32_t pathPointCount = lastPtIdx - ctx->pathes[ptrPath] + 1; uint32_t firstVertIdx = ctx->vertCount; ear_clip_point ecps[pathPointCount]; uint32_t ecps_count = pathPointCount; - i = 0; + uint32_t i = 0; + //init points link list while (i < pathPointCount-1){ v.pos = ctx->points[i+firstPtIdx]; - ear_clip_point ecp = { - v.pos, - i+firstVertIdx, - &ecps[i+1] - }; + ear_clip_point ecp = {v.pos, i+firstVertIdx, &ecps[i+1]}; ecps[i] = ecp; _add_vertex(ctx, v); i++; } v.pos = ctx->points[i+firstPtIdx]; - ear_clip_point ecp = { - v.pos, - i+firstVertIdx, - ecps - }; + ear_clip_point ecp = {v.pos, i+firstVertIdx, ecps}; ecps[i] = ecp; _add_vertex(ctx, v); @@ -390,22 +382,21 @@ void vkvg_fill_preserve (VkvgContext ctx){ ear_clip_point* vP = v2->next; bool isEar = true; while (vP!=v1){ - if (ptInTriangle(vP->pos,v0->pos,v2->pos,v1->pos)){ + if (ptInTriangle (vP->pos, v0->pos, v2->pos, v1->pos)){ isEar = false; break; } vP = vP->next; } if (isEar){ - _add_triangle_indices(ctx, v0->idx,v1->idx,v2->idx); + _add_triangle_indices (ctx, v0->idx, v1->idx, v2->idx); v1->next = v2; ecps_count --; }else ecp_current = ecp_current->next; } - if (ecps_count == 3){ - _add_triangle_indices(ctx, ecp_current->next->idx,ecp_current->idx,ecp_current->next->next->idx); - } + if (ecps_count == 3) + _add_triangle_indices(ctx, ecp_current->next->idx, ecp_current->idx, ecp_current->next->next->idx); ptrPath+=2; } diff --git a/src/vkvg_context_internal.c b/src/vkvg_context_internal.c index e8131ae..3af9c4e 100644 --- a/src/vkvg_context_internal.c +++ b/src/vkvg_context_internal.c @@ -250,7 +250,7 @@ void _clear_path (VkvgContext ctx){ ctx->pointCount = 0; ctx->curPosExists = false; } -bool _path_is_closed (VkvgContext ctx, uint32_t ptrPath){ +inline bool _path_is_closed (VkvgContext ctx, uint32_t ptrPath){ return (ctx->pathes[ptrPath] == ctx->pathes[ptrPath+1]); } uint32_t _get_last_point_of_closed_path(VkvgContext ctx, uint32_t ptrPath){ diff --git a/src/vkvg_context_internal.h b/src/vkvg_context_internal.h index ec38078..f3abb74 100644 --- a/src/vkvg_context_internal.h +++ b/src/vkvg_context_internal.h @@ -108,15 +108,15 @@ typedef struct _vkvg_context_t { vkvg_buff uboGrad;//uniform buff obj holdings gradient infos //vk buffers, holds data until flush - vkvg_buff indices; - size_t sizeIndices; - uint32_t indCount; + vkvg_buff indices; //index buffer with persistent map memory + size_t sizeIndices; //reserved size + uint32_t indCount; //current indice count uint32_t curIndStart; - vkvg_buff vertices; - size_t sizeVertices; - uint32_t vertCount; + vkvg_buff vertices; //vertex buffer with persistent mapped memory + size_t sizeVertices; //reserved size + uint32_t vertCount; //effective vertices count //pathes, exists until stroke of fill vec2* points; //points array @@ -124,12 +124,16 @@ typedef struct _vkvg_context_t { uint32_t pointCount; //effective points count uint32_t pathPtr; + //pathes array is a list of couple (start,end) point idx refering to point array + //it split points list in subpathes and tell if path is closed. + //if path is closed, end index is the same as start. + //(I should use a boolean instead to keep last point in array) uint32_t* pathes; size_t sizePathes; - vec2 curPos; + vec2 curPos; //current position handling bool curPosExists; - vec4 curRGBA; + vec4 curRGBA; //is store in pushConsts => may be removed. float lineWidth; vkvg_line_cap_t lineCap; diff --git a/tests/test1.c b/tests/test1.c index 023bf77..91c917a 100644 --- a/tests/test1.c +++ b/tests/test1.c @@ -453,19 +453,19 @@ void vkvg_test_fill_and_stroke (VkvgContext ctx){ vkvg_move_to (ctx, 100, 100); vkvg_rel_line_to (ctx, 50, -80); vkvg_rel_line_to (ctx, 50, 80); - vkvg_close_path (ctx); + //vkvg_close_path (ctx); - /*vkvg_move_to (ctx, 300, 100); + vkvg_move_to (ctx, 300, 100); vkvg_rel_line_to (ctx, 50, -80); vkvg_rel_line_to (ctx, 50, 80); - vkvg_close_path (ctx);*/ + vkvg_close_path (ctx); vkvg_set_line_width (ctx, 10.0); vkvg_set_source_rgb (ctx, 0, 0, 1); - //vkvg_fill_preserve (ctx); - vkvg_fill(ctx); - //vkvg_set_source_rgb (ctx, 0, 0, 0); - //vkvg_stroke (ctx); + vkvg_fill_preserve (ctx); + //vkvg_fill(ctx); + vkvg_set_source_rgb (ctx, 0, 0, 0); + vkvg_stroke (ctx); } void vkvg_test_curves2 (VkvgContext ctx) { vkvg_set_source_rgba (ctx, 0.5,0.0,1.0,0.5); -- 2.47.3