]> O.S.I.I.S - jp/vkvg.git/commitdiff
update tests
authorJean-Philippe Bruyère <jp_bruyere@hotmail.com>
Wed, 5 Jan 2022 17:53:05 +0000 (18:53 +0100)
committerJean-Philippe Bruyère <jp_bruyere@hotmail.com>
Wed, 5 Jan 2022 17:53:05 +0000 (18:53 +0100)
17 files changed:
tests/CMakeLists.txt
tests/clip.c
tests/common/test.c
tests/compositing.c
tests/curve.c
tests/fill_and_stroke.c
tests/fill_non_zero.c
tests/gradient.c
tests/img_surf.c
tests/inverse_colinear.c
tests/paint_surf.c
tests/path_extents.c [new file with mode: 0644]
tests/pattern_transforms.c [new file with mode: 0644]
tests/save_restore.c
tests/simple_paint.c
tests/test1.c
tests/transform.c [new file with mode: 0644]

index 40da768339cf23711640b005d2e275e332536ce4..a0a05691e94a00f7b32bbf0492c53fc6c3449f67 100644 (file)
@@ -11,34 +11,28 @@ ENDIF ()
 FUNCTION (buildtest TEST_FILE)
        GET_FILENAME_COMPONENT(TEST_NAME ${TEST_FILE} NAME_WE)
        ADD_EXECUTABLE(${TEST_NAME} ${TEST_FILE})
-       TARGET_INCLUDE_DIRECTORIES(${TEST_NAME} PRIVATE
-               ${Vulkan_INCLUDE_DIRS}
-               ${GLFW3_INCLUDE_DIR}
-               ${CMAKE_CURRENT_SOURCE_DIR}/../include
-               ${CMAKE_CURRENT_SOURCE_DIR}/../src
-               ${CMAKE_CURRENT_SOURCE_DIR}/common
-               ${CMAKE_CURRENT_SOURCE_DIR}/../vkh/include
-               ${CMAKE_CURRENT_SOURCE_DIR}/../vkh/src
-       )
        TARGET_LINK_LIBRARIES(${TEST_NAME}
                tests_common
        )
 ENDFUNCTION (buildtest)
 
 ADD_LIBRARY("tests_common" STATIC common/vkengine.c common/test.c common/rnd.c)
-TARGET_INCLUDE_DIRECTORIES(tests_common PRIVATE
-       ${Vulkan_INCLUDE_DIRS}
-       ${GLFW3_INCLUDE_DIR}
-       "${CMAKE_CURRENT_SOURCE_DIR}/../include"
-       "${CMAKE_CURRENT_SOURCE_DIR}/../src"
-       "${CMAKE_CURRENT_SOURCE_DIR}/../common"
-       "${CMAKE_CURRENT_SOURCE_DIR}/../vkh/include"
-       "${CMAKE_CURRENT_SOURCE_DIR}/../vkh/src"
-       )
+
+TARGET_INCLUDE_DIRECTORIES(tests_common
+       PUBLIC
+               ${Vulkan_INCLUDE_DIRS}
+               ${GLFW3_INCLUDE_DIR}
+               "${CMAKE_CURRENT_SOURCE_DIR}/../include"
+               "${CMAKE_CURRENT_SOURCE_DIR}/../src"
+               "${CMAKE_CURRENT_SOURCE_DIR}/common"
+               "${CMAKE_CURRENT_SOURCE_DIR}/../vkh/include"
+               "${CMAKE_CURRENT_SOURCE_DIR}/../vkh/src"
+)
 TARGET_LINK_LIBRARIES(tests_common
-       ${Vulkan_LIBRARIES}
-       ${GLFW3_LIBRARY}
-       vkvg_static
+       PUBLIC
+               ${Vulkan_LIBRARIES}
+               ${GLFW3_LIBRARY}
+               vkvg
 )
 
 file(GLOB_RECURSE DATAS RELATIVE "${CMAKE_CURRENT_SOURCE_DIR}" "data/*")
index 032229500018b966d100a86ab42b3b27da5f7ced..e5c03a0a4682236d10e19a42f56d86f795339c23 100644 (file)
@@ -68,6 +68,22 @@ void clipped_transformed_ec () {
        vkvg_paint(ctx);
        vkvg_destroy(ctx);
 }
+void clip_transformed_ec () {
+       VkvgContext ctx = vkvg_create(surf);
+       vkvg_set_fill_rule(ctx, VKVG_FILL_RULE_NON_ZERO);
+       vkvg_set_source_rgb(ctx,1,0,0);
+       vkvg_paint(ctx);
+       vkvg_rotate(ctx,0.1f);
+       vkvg_set_source_rgb(ctx,0,1,0);
+       vkvg_rectangle(ctx, 100,100,300,200);
+       vkvg_clip(ctx);
+       vkvg_paint(ctx);
+       vkvg_translate (ctx,200,100);
+       vkvg_set_source_rgb(ctx,0,0,1);
+       vkvg_paint(ctx);
+
+       vkvg_destroy(ctx);
+}
 void clipped_transformed_eo () {
        VkvgContext ctx = vkvg_create(surf);
        vkvg_set_fill_rule(ctx, VKVG_FILL_RULE_EVEN_ODD);
@@ -122,11 +138,9 @@ void test_clip2(){
        vkvg_clip(ctx);
        vkvg_save(ctx);
 
-
        vkvg_set_source_rgb(ctx,1,1,0);
        vkvg_paint(ctx);
 
-
        vkvg_rectangle(ctx, 200,200,200,200);
        vkvg_clip(ctx);
 
@@ -135,7 +149,6 @@ void test_clip2(){
 
        vkvg_restore(ctx);
 
-
        vkvg_rectangle(ctx, 350,350,420,420);
        vkvg_set_source_rgb(ctx,0,0,1);
        vkvg_fill(ctx);
@@ -160,6 +173,7 @@ void test_clip2(){
 }
 int main(int argc, char *argv[]) {
        no_test_size = true;
+       PERFORM_TEST (clip_transformed_ec, argc, argv);
        PERFORM_TEST (clipped_paint_ec, argc, argv);
        PERFORM_TEST (clipped_paint_eo, argc, argv);
        PERFORM_TEST (clipped_transformed_ec, argc, argv);
index 1572d1691ff56da2df7316d1ea3c134ad606b559..20c6a38c3053f868f783b4c60a3dae45de760ded 100644 (file)
@@ -190,6 +190,9 @@ void _print_usage_and_exit () {
        printf("\t\t\t - b: Butt (default)\n");
        printf("\t\t\t - r: Rount\n");
        printf("\t\t\t - s: Square\n");
+       printf("\t-f fill_rule:\tset current fill rule:\n");
+       printf("\t\t\t - 0: Even Odd\n");
+       printf("\t\t\t - 1: Non Zero\n");
        printf("\t-d:\t\tenable dashes.\n");
        printf("\t-n index:\tRun only a single test, zero based index.\n");
        printf("\t-q:\t\tQuiet, don't print measures table head row, usefull for batch tests.\n");
@@ -246,6 +249,10 @@ void _parse_args (int argc, char* argv[]) {
                        line_width = atoi (argv[i]);
                }else if (strcmp (argv[i], "-d\0") == 0) {
                        dashes_count = 2;
+               }else if (strcmp (argv[i], "-f\0") == 0) {
+                       if (argc -1 < ++i)
+                               _print_usage_and_exit();
+                       fill_rule = atoi (argv[i]);
                } else if (strcmp (argv[i], "-o\0") == 0) {
                        offscreen = true;
                }else if (strcmp (argv[i], "-j\0") == 0) {
index e1449733ee04e77704feafb3bd6a299f3c7dd2db..54711b4bbb7a5d7156b4a78737b438221926228a 100644 (file)
@@ -14,9 +14,27 @@ void compositing(){
 
        vkvg_destroy(ctx);
 }
+void opacity(){
+       VkvgContext ctx = vkvg_create(surf);
+       vkvg_clear(ctx);
+
+       vkvg_set_source_rgba(ctx, 1,0,0,1.0f);
+       vkvg_rectangle(ctx,100,100,200,200);
+       vkvg_fill(ctx);
+
+       vkvg_set_opacity(ctx,0.5f);
+
+       vkvg_set_source_rgba(ctx, 0,0,1,1.0f);
+       vkvg_rectangle(ctx,200,200,200,200);
+       vkvg_fill(ctx);
+
+       vkvg_destroy(ctx);
+}
+
 
 int main(int argc, char *argv[]) {
        no_test_size = true;
        PERFORM_TEST (compositing, argc, argv);
+       PERFORM_TEST (opacity, argc, argv);
        return 0;
 }
index 6ad3d269a917ddd1afb1156d8ffe91fd433d8c00..28e7b1ef505a03296d0f9d933727e695b932223b 100644 (file)
@@ -3,7 +3,6 @@
 void test(){
        VkvgContext ctx = vkvg_create(surf);
 
-       vkvg_set_fill_rule(ctx, VKVG_FILL_RULE_NON_ZERO);
        vkvg_set_line_width(ctx, 20);
 
        vkvg_scale(ctx,2,2);
@@ -134,7 +133,6 @@ void _long_curv () {
        float h = (float)test_height;
 
        VkvgContext ctx = _initCtx();
-       vkvg_set_fill_rule(ctx, fill_rule);
 
        randomize_color(ctx);
        float x1 = rndf() * w;
index 92352d56639003bd19540e6e1904e0a7646365dd..6e3e21e7d1779fc1d51bf1900b48b69c0e7caa76 100644 (file)
@@ -6,7 +6,7 @@ void test(){
        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_rel_line_to (ctx, 50, -80);
index b70c7eebe042b21edef6b79618be13d4bf600efc..4f5f51bffffa14aae8955de921e89b8f4f2a4b40 100644 (file)
@@ -25,6 +25,7 @@ void test(){
 }
 
 int main(int argc, char *argv[]) {
+       //vkvg_log_level = VKVG_LOG_FULL;
        no_test_size = true;
        PERFORM_TEST (test, argc, argv);
        return 0;
index 3fef7dbd21d766b947589741daa40283fd3d7751..01f809f53ab0fcb40f00e42d692a831e114aec3c 100644 (file)
@@ -21,25 +21,27 @@ void paint(){
        float x = 100;
        pat = create_grad(ctx,x);
        //vkvg_pattern_set_extend(pat,VKVG_EXTEND_NONE);
-       vkvg_rectangle(ctx, x,200,20,20);
+       vkvg_rectangle(ctx, x,200,50,50);
        vkvg_set_source (ctx, pat);
        vkvg_pattern_destroy (pat);
        vkvg_fill(ctx);
 
        x+=100;
 
-       pat = create_grad(ctx,x);
+       pat = vkvg_pattern_create_linear(10,0,300,0);
+       vkvg_pattern_add_color_stop(pat, 0, 0, 0, 1, 0);
+       vkvg_pattern_add_color_stop(pat, 1, 1, 0, 0, 1);
        //vkvg_pattern_set_extend(pat,VKVG_EXTEND_NONE);
-       vkvg_rectangle(ctx, x,200,20,20);
+       vkvg_rectangle(ctx, 10,10,300,50);
        vkvg_set_source (ctx, pat);
        vkvg_pattern_destroy (pat);
        vkvg_fill(ctx);
 
-       x+=100;
+       x+=200;
 
        pat = create_grad(ctx,x);
        //vkvg_pattern_set_extend(pat,VKVG_EXTEND_NONE);
-       vkvg_rectangle(ctx, x,200,20,20);
+       vkvg_rectangle(ctx, x,200,50,50);
        vkvg_set_source (ctx, pat);
        vkvg_pattern_destroy (pat);
        vkvg_fill(ctx);
@@ -48,7 +50,7 @@ void paint(){
 
        pat = create_grad(ctx,x);
        //vkvg_pattern_set_extend(pat,VKVG_EXTEND_NONE);
-       vkvg_rectangle(ctx, x,200,20,20);
+       vkvg_rectangle(ctx, x,200,50,50);
        vkvg_set_source (ctx, pat);
        vkvg_pattern_destroy (pat);
        vkvg_fill(ctx);
@@ -134,7 +136,7 @@ void gradient_transform() {
 int main(int argc, char *argv[]) {
        no_test_size = true;
        PERFORM_TEST(paint, argc, argv);
-       /*PERFORM_TEST(paint_repeat, argc, argv);
-       PERFORM_TEST(gradient_transform, argc, argv);*/
+       PERFORM_TEST(paint_repeat, argc, argv);
+       PERFORM_TEST(gradient_transform, argc, argv);
        return 0;
 }
index af6f9a6685d63d32aba263f664d43e09b24f6ba3..bffc65cc05981da43290c171602d956d0ca33acd 100644 (file)
@@ -32,6 +32,58 @@ void paint_with_scale(){
        vkvg_surface_destroy(imgSurf);
        vkvg_destroy(ctx);
 }
+void translate(){
+       VkvgContext ctx = vkvg_create(surf);
+       vkvg_translate (ctx, 150,50);
+       VkvgSurface imgSurf = vkvg_surface_create_from_image(device, imgPath);
+       vkvg_set_source_surface(ctx, imgSurf, 0, 0);
+
+       vkvg_paint(ctx);
+
+       vkvg_surface_destroy(imgSurf);
+       vkvg_destroy(ctx);
+}
+void offset_and_scale(){
+       VkvgContext ctx = vkvg_create(surf);
+       vkvg_scale (ctx, 0.2f,0.2f);
+       VkvgSurface imgSurf = vkvg_surface_create_from_image(device, imgPath);
+       vkvg_set_source_surface(ctx, imgSurf, 100, 100);
+
+       vkvg_paint(ctx);
+
+       vkvg_surface_destroy(imgSurf);
+       vkvg_destroy(ctx);
+}
+
+static float angle = 0;
+void paint_with_rot(){
+       angle += 0.005;
+       VkvgContext ctx = vkvg_create(surf);
+       vkvg_clear(ctx);
+
+       vkvg_rotate (ctx, angle);
+       VkvgSurface imgSurf = vkvg_surface_create_from_image(device, imgPath);
+       vkvg_set_source_surface(ctx, imgSurf, 0, 0);
+
+       vkvg_paint(ctx);
+
+       vkvg_surface_destroy(imgSurf);
+       vkvg_destroy(ctx);
+}
+void offset_and_rot(){
+       angle += 0.005;
+       VkvgContext ctx = vkvg_create(surf);
+       vkvg_clear(ctx);
+
+       vkvg_rotate (ctx, angle);
+       VkvgSurface imgSurf = vkvg_surface_create_from_image(device, imgPath);
+       vkvg_set_source_surface(ctx, imgSurf, 100, 100);
+
+       vkvg_paint(ctx);
+
+       vkvg_surface_destroy(imgSurf);
+       vkvg_destroy(ctx);
+}
 
 void paint_pattern () {
        VkvgContext ctx = vkvg_create(surf);
@@ -108,6 +160,10 @@ int main(int argc, char *argv[]) {
        PERFORM_TEST (paint, argc, argv);
        PERFORM_TEST (paint_offset, argc, argv);
        PERFORM_TEST (paint_with_scale, argc, argv);
+       PERFORM_TEST (offset_and_scale, argc, argv);
+       PERFORM_TEST (translate, argc, argv);
+       PERFORM_TEST (paint_with_rot, argc, argv);
+       PERFORM_TEST (offset_and_rot, argc, argv);
        PERFORM_TEST (paint_pattern, argc, argv);
        PERFORM_TEST (paint_patt_repeat, argc, argv);
        PERFORM_TEST (paint_patt_repeat_scalled, argc, argv);
index 2df57e389d9c6cf2ebaf09b8abdb14db7e6ed3f8..47146fb5894940882d2995accc21d1ea14f78782 100644 (file)
@@ -44,13 +44,13 @@ void draw (){
        vkvg_set_line_cap       (ctx, lineCap);
 
        if (startWithArc)
-               vkvg_arc(ctx,pts[0].x,pts[0].y,200, 0.3f, M_PIF);
+               vkvg_arc_negative(ctx,pts[0].x,pts[0].y,200, M_PIF*1.5f, M_PIF);
        else
                vkvg_move_to(ctx,pts[0].x,pts[0].y);
        for (int i=1; i<ptsCount-1; i++)
                vkvg_line_to(ctx,pts[i].x,pts[i].y);
        if (endWithArc)
-               vkvg_arc(ctx,pts[ptsCount-1].x,pts[ptsCount-1].y,200, 0.3f, M_PIF);
+               vkvg_arc_negative(ctx,pts[ptsCount-1].x,pts[ptsCount-1].y,200, M_PIF*1.5f, M_PIF);
        else
                vkvg_line_to(ctx,pts[ptsCount-1].x,pts[ptsCount-1].y);
 
@@ -64,7 +64,7 @@ void draw (){
                vkvg_set_source_rgba(ctx,0,0,0,1);
                vkvg_stroke(ctx);
                vkvg_set_source_rgba(ctx,0.5f,0.5f,1.0f,0.7f);
-               vkvg_arc(ctx, pts[hoverPt].x, pts[hoverPt].y, pointSize, 0, M_PIF*2);
+               vkvg_arc (ctx, pts[hoverPt].x, pts[hoverPt].y, pointSize, 0, M_PIF*2);
                vkvg_fill_preserve(ctx);
                vkvg_stroke(ctx);
        } else
index 0347df9f1042a44264d1b2420c5c9ae55a19598c..ae93bdfbc9c2542b3ea85506e9d00d34c6d1e265 100644 (file)
@@ -1,12 +1,10 @@
 #include "test.h"
 
-vkvg_fill_rule_t fillrule = VKVG_FILL_RULE_NON_ZERO;
 float lineWidth = 10.f;
 
 VkvgSurface createSurf (uint32_t width, uint32_t height) {
        VkvgSurface s = vkvg_surface_create(device, width, height);
-       VkvgContext ctx = vkvg_create(s);
-       vkvg_set_fill_rule(ctx,fillrule);
+       VkvgContext ctx = _initCtx(surf);
        vkvg_set_line_width(ctx,lineWidth);
        float hlw = lineWidth/2.f;
        /*
@@ -23,8 +21,7 @@ VkvgSurface createSurf (uint32_t width, uint32_t height) {
 
 void paint(){
        VkvgSurface src = createSurf(256,256);
-       VkvgContext ctx = vkvg_create(surf);
-       vkvg_set_fill_rule(ctx,fillrule);
+       VkvgContext ctx = _initCtx(surf);
        vkvg_set_source_surface(ctx, src, 0, 0);
        vkvg_paint(ctx);
        vkvg_destroy(ctx);
@@ -32,10 +29,9 @@ void paint(){
 }
 void paint_with_offset(){
        VkvgSurface src = createSurf(256,256);
-       VkvgContext ctx = vkvg_create(surf);
+       VkvgContext ctx = _initCtx(surf);
        vkvg_set_source_rgba(ctx,0,1,0,0.5);
        vkvg_paint(ctx);
-       vkvg_set_fill_rule(ctx,fillrule);
        vkvg_set_source_surface(ctx, src, 100, 100);
        vkvg_paint(ctx);
        vkvg_destroy(ctx);
@@ -43,8 +39,7 @@ void paint_with_offset(){
 }
 void paint_multiple(){
        VkvgSurface src = createSurf(256,256);
-       VkvgContext ctx = vkvg_create(surf);
-       vkvg_set_fill_rule(ctx,fillrule);
+       VkvgContext ctx = _initCtx(surf);
        for(int i=0;i<10;i++) {
                vkvg_set_source_surface(ctx, src, i * 20, i * 20);
                vkvg_paint(ctx);
@@ -54,8 +49,7 @@ void paint_multiple(){
 }
 void paint_with_rotation(){
        VkvgSurface src = createSurf(256,256);
-       VkvgContext ctx = vkvg_create(surf);
-       vkvg_set_fill_rule(ctx,fillrule);
+       VkvgContext ctx = _initCtx(surf);
        vkvg_set_source_surface(ctx, src, 0, 0);
        vkvg_rotate(ctx, 45);
        vkvg_paint(ctx);
@@ -64,8 +58,7 @@ void paint_with_rotation(){
 }
 void paint_with_scale(){
        VkvgSurface src = createSurf(256,256);
-       VkvgContext ctx = vkvg_create(surf);
-       vkvg_set_fill_rule(ctx,fillrule);
+       VkvgContext ctx = _initCtx(surf);
        vkvg_set_source_surface(ctx, src, 0, 0);
        vkvg_scale (ctx, 0.2f,0.2f);
        vkvg_paint(ctx);
@@ -74,8 +67,7 @@ void paint_with_scale(){
 }
 void paint_rect(){
        VkvgSurface src = createSurf(256,256);
-       VkvgContext ctx = vkvg_create(surf);
-       vkvg_set_fill_rule(ctx,fillrule);
+       VkvgContext ctx = _initCtx(surf);
        vkvg_set_source_surface(ctx, src, 0, 0);
        vkvg_rectangle(ctx,100,100,300,200);
        vkvg_paint(ctx);
@@ -84,8 +76,7 @@ void paint_rect(){
 }
 //TODO:test failed: full screen paint instead of rotated rect
 void paint_rect_with_rotation(){
-       VkvgContext ctx = vkvg_create(surf);
-       vkvg_set_fill_rule(ctx,fillrule);
+       VkvgContext ctx = _initCtx(surf);
        vkvg_rotate(ctx, 45);
        vkvg_set_source_rgba(ctx,1,0,0,1);
        vkvg_rectangle(ctx,100,100,300,200);
@@ -93,8 +84,7 @@ void paint_rect_with_rotation(){
        vkvg_destroy(ctx);
 }
 void paint_rect_with_scale(){
-       VkvgContext ctx = vkvg_create(surf);
-       vkvg_set_fill_rule(ctx,fillrule);
+       VkvgContext ctx = _initCtx(surf);
        vkvg_scale (ctx, 0.2f,0.2f);
        vkvg_set_source_rgba(ctx,1,0,0,1);
        vkvg_rectangle(ctx,100,100,300,200);
@@ -109,7 +99,7 @@ int main(int argc, char *argv[]) {
        PERFORM_TEST (paint_with_rotation, argc, argv);
        PERFORM_TEST (paint_with_scale, argc, argv);
        PERFORM_TEST (paint_rect, argc, argv);
-       /*PERFORM_TEST (paint_rect_with_rotation, argc, argv);
-       PERFORM_TEST (paint_rect_with_scale, argc, argv);*/
+       PERFORM_TEST (paint_rect_with_rotation, argc, argv);
+       PERFORM_TEST (paint_rect_with_scale, argc, argv);
        return 0;
 }
diff --git a/tests/path_extents.c b/tests/path_extents.c
new file mode 100644 (file)
index 0000000..3011f1e
--- /dev/null
@@ -0,0 +1,185 @@
+#include "test.h"
+#include "vectors.h"
+
+vkvg_fill_rule_t fillrule = VKVG_FILL_RULE_NON_ZERO;
+static VkSampleCountFlags samples = VK_SAMPLE_COUNT_8_BIT;
+float lineWidth = 4.0f;
+vkvg_line_join_t lineJoin = VKVG_LINE_JOIN_MITER;
+vkvg_line_cap_t lineCap = VKVG_LINE_CAP_BUTT;
+bool isClosed = false, refresh = true;
+bool startWithArc = false, endWithArc = false;
+
+int ptsCount = 0;
+vec2 pts[1024];
+int hoverPt = -1;
+double pointSize = 7;
+float _dashes[] = {5.0f,3.0f};
+int _ndashes = 2;
+
+void emit_path (VkvgContext ctx) {
+       vkvg_move_to(ctx,pts[0].x,pts[0].y);
+       for (int i=1; i<ptsCount; i++)
+               vkvg_line_to(ctx,pts[i].x,pts[i].y);
+       if (isClosed)
+               vkvg_close_path(ctx);
+}
+
+void draw (){
+       refresh = false;
+
+       VkvgContext ctx = vkvg_create(surf);
+       vkvg_clear(ctx);
+       vkvg_set_source_rgba(ctx,1,0,0,1);
+       vkvg_set_line_width     (ctx,lineWidth);
+       vkvg_set_line_join      (ctx, lineJoin);
+       vkvg_set_line_cap       (ctx, lineCap);
+
+       emit_path(ctx);
+       float x1, y1, x2, y2;
+       vkvg_path_extents(ctx, &x1, &y1, &x2, &y2);
+       vkvg_set_source_rgba(ctx,0.2,0.2,1,0.5);
+       if (hoverPt>=0) {
+               if (isClosed)
+                       vkvg_fill_preserve(ctx);
+               vkvg_set_source_rgba(ctx,1,0.2,0.2,0.5);
+               vkvg_stroke_preserve(ctx);
+               vkvg_set_dash(ctx, NULL, 0, 0);
+               vkvg_set_line_width(ctx,1);
+               vkvg_set_source_rgba(ctx,0,0,0,1);
+               vkvg_stroke(ctx);
+               vkvg_set_source_rgba(ctx,0.5f,0.5f,1.0f,0.7f);
+               vkvg_arc (ctx, pts[hoverPt].x, pts[hoverPt].y, pointSize, 0, M_PIF*2);
+               vkvg_fill_preserve(ctx);
+               vkvg_stroke(ctx);
+       } else {
+               if (isClosed)
+                       vkvg_fill_preserve(ctx);
+               vkvg_set_source_rgba(ctx,1,0.2,0.2,0.5);
+               vkvg_stroke(ctx);
+       }
+
+       vkvg_rectangle(ctx, x1, y1, x2 - x1, y2 - y1);
+       vkvg_set_dash(ctx, _dashes, _ndashes, 0);
+       vkvg_set_line_width(ctx,1);
+       vkvg_set_source_rgba(ctx,0,1,0,1);
+       vkvg_stroke(ctx);
+
+       vkvg_destroy(ctx);
+}
+static void key_callback(GLFWwindow* window, int key, int scancode, int action, int mods) {
+       if (action != GLFW_PRESS)
+               return;
+       switch (key) {
+       case GLFW_KEY_ESCAPE :
+               glfwSetWindowShouldClose(window, GLFW_TRUE);
+               break;
+       case GLFW_KEY_W :
+               isClosed ^= true;
+               break;
+       case GLFW_KEY_DELETE :
+               ptsCount = 0;
+               break;
+       case GLFW_KEY_J :
+               lineJoin++;
+               if (lineJoin > 2)
+                       lineJoin = 0;
+               break;
+       case GLFW_KEY_C :
+               lineCap++;
+               if (lineCap > 2)
+                       lineCap = 0;
+               break;
+       case GLFW_KEY_KP_SUBTRACT :
+               if (ptsCount > 1)
+                       ptsCount--;
+               break;
+       }
+       refresh = true;
+}
+static vec2 mousePos;
+static void mouse_move_callback(GLFWwindow* window, double x, double y){
+       mousePos = (vec2) {x, y};
+       if (mouseDown) {
+               if (hoverPt < 0)
+                       return;
+               pts[hoverPt] = mousePos;
+               refresh = true;
+       } else {
+               for (int i=0; i<ptsCount; i++) {
+                       if (x > pts[i].x - pointSize &&
+                               x < pts[i].x + pointSize &&
+                               y > pts[i].y - pointSize &&
+                               y < pts[i].y + pointSize) {
+                               hoverPt = i;
+                               refresh = true;
+                               return;
+                       }
+               }
+               refresh = true;
+               hoverPt = -1;
+       }
+}
+static void scroll_callback(GLFWwindow* window, double x, double y){
+       if (y<0.f)
+               zoom *= 0.5f;
+       else
+               zoom *= 2.0f;
+}
+static void mouse_button_callback(GLFWwindow* window, int but, int state, int modif){
+       if (but != GLFW_MOUSE_BUTTON_1)
+               return;
+       if (state == GLFW_TRUE)
+               mouseDown = true;
+       else
+               mouseDown = false;
+       if (state == GLFW_TRUE && hoverPt < 0)
+               pts[ptsCount++] = mousePos;
+       refresh = true;
+}
+
+
+
+int main(int argc, char* argv[]) {
+
+       _parse_args (argc, argv);
+       VkEngine e;
+       e = vkengine_create (VK_PHYSICAL_DEVICE_TYPE_DISCRETE_GPU, VK_PRESENT_MODE_FIFO_KHR, test_width, test_height);
+
+       VkhPresenter r = e->renderer;
+       vkengine_set_key_callback (e, key_callback);
+       vkengine_set_mouse_but_callback(e, mouse_button_callback);
+       vkengine_set_cursor_pos_callback(e, mouse_move_callback);
+       vkengine_set_scroll_callback(e, scroll_callback);
+
+       bool deferredResolve = false;
+
+       device = vkvg_device_create_from_vk_multisample(vkh_app_get_inst(e->app), r->dev->phy, r->dev->dev, r->qFam, 0, samples, deferredResolve);
+       surf = vkvg_surface_create(device, test_width, test_height);
+
+       vkh_presenter_build_blit_cmd (r, vkvg_surface_get_vk_image(surf), test_width, test_height);
+
+       while (!vkengine_should_close (e)) {
+               glfwPollEvents();
+
+               if (refresh)
+                       draw ();
+
+               if (!vkh_presenter_draw (r)){
+                       vkh_presenter_get_size (r, &test_width, &test_height);
+                       vkvg_surface_destroy (surf);
+                       surf = vkvg_surface_create(device, test_width, test_height);
+                       vkh_presenter_build_blit_cmd (r, vkvg_surface_get_vk_image(surf), test_width, test_height);
+                       vkDeviceWaitIdle(r->dev->dev);
+                       continue;
+               }
+       }
+       vkDeviceWaitIdle(e->dev->dev);
+
+       vkvg_surface_destroy    (surf);
+
+       vkvg_device_destroy     (device);
+
+       vkengine_destroy (e);
+
+       return 0;
+}
diff --git a/tests/pattern_transforms.c b/tests/pattern_transforms.c
new file mode 100644 (file)
index 0000000..18e528a
--- /dev/null
@@ -0,0 +1,93 @@
+#include "test.h"
+
+float lineWidth = 10.f;
+const char* imgPath = "data/miroir.jpg";
+
+VkvgPattern create_grad (VkvgContext ctx) {
+       VkvgPattern pat = vkvg_pattern_create_linear(0,0,200,0);
+       vkvg_pattern_add_color_stop(pat,   0, 1, 0, 0, 1);
+       vkvg_pattern_add_color_stop(pat, 0.5, 0, 1, 0, 1);
+       vkvg_pattern_add_color_stop(pat, 1.0, 0, 0, 1, 1);
+       return pat;
+}
+
+VkvgSurface createSurf (uint32_t width, uint32_t height) {
+       VkvgSurface s = vkvg_surface_create(device, width, height);
+       VkvgContext ctx = _initCtx(surf);
+       vkvg_set_line_width(ctx,lineWidth);
+       float hlw = lineWidth/2.f;
+       /*
+       vkvg_set_source_rgba(ctx,0,1,0,0.5);
+       vkvg_fill_preserve(ctx);*/
+       vkvg_set_source_rgba(ctx,1,0,0,0.5);
+       vkvg_paint(ctx);
+       vkvg_set_source_rgba(ctx,0,0,1,0.5);
+       vkvg_rectangle(ctx,hlw,hlw,(float)width-lineWidth,(float)height-lineWidth);
+       vkvg_stroke(ctx);
+       vkvg_destroy(ctx);
+       return s;
+}
+
+void img_scale() {
+       VkvgSurface imgSurf = vkvg_surface_create_from_image(device, imgPath);
+       VkvgPattern pat = vkvg_pattern_create_for_surface(imgSurf);
+       VkvgContext ctx = _initCtx(surf);
+
+       vkvg_translate(ctx, 100,100);
+
+       vkvg_matrix_t mat;
+       vkvg_matrix_init_scale(&mat, 2.0, 2.0);
+       vkvg_pattern_set_matrix (pat, &mat);
+
+       vkvg_set_source (ctx, pat);
+       vkvg_rectangle(ctx, 0 , 0, 200, 140);
+       vkvg_fill(ctx);
+       vkvg_destroy(ctx);
+       vkvg_pattern_destroy(pat);
+       vkvg_surface_destroy(imgSurf);
+}
+
+void pat_scale(){
+
+       VkvgContext ctx = _initCtx(surf);
+       VkvgPattern pat = create_grad(ctx);
+
+       vkvg_translate(ctx, 100,100);
+
+       vkvg_matrix_t mat;
+       vkvg_matrix_init_scale(&mat, 0.5, 0.5);
+       vkvg_pattern_set_matrix (pat, &mat);
+
+       vkvg_set_source (ctx, pat);
+       vkvg_rectangle(ctx, 0 , 0, 200, 140);
+       vkvg_fill(ctx);
+       vkvg_destroy(ctx);
+       vkvg_pattern_destroy(pat);
+}
+static float angle = 0;
+void pat_rotate(){
+       angle += 0.001f;
+
+       VkvgContext ctx = _initCtx(surf);
+       VkvgPattern pat = create_grad(ctx);
+
+       //vkvg_translate(ctx, 100,100);
+
+       vkvg_matrix_t mat;
+       vkvg_matrix_init_translate(&mat, 100, 70);
+       vkvg_matrix_rotate(&mat, angle);
+       vkvg_pattern_set_matrix (pat, &mat);
+
+       vkvg_set_source (ctx, pat);
+       vkvg_rectangle(ctx, 0 , 0, 200, 140);
+       vkvg_fill(ctx);
+       vkvg_destroy(ctx);
+       vkvg_pattern_destroy(pat);
+}
+int main(int argc, char *argv[]) {
+       no_test_size = true;
+       PERFORM_TEST (pat_scale, argc, argv);
+       PERFORM_TEST (pat_rotate, argc, argv);
+       PERFORM_TEST (img_scale, argc, argv);
+       return 0;
+}
index dc7f2a6cbd7fe2d8911eab5c73766ad8fe4a280c..b50861b926060f7a3d2fe7930628f4a21b7ebec1 100644 (file)
@@ -6,10 +6,10 @@ void recurse_draw(VkvgContext ctx, int depth) {
 
        vkvg_translate (ctx, 5,5);
        vkvg_rectangle(ctx, (float)depth,(float)depth,200,200);
-       vkvg_clip_preserve(ctx);
+       /*vkvg_clip_preserve(ctx);
        vkvg_set_source_rgb(ctx, 1.f/depth, 1.f / depth, 1.f / depth);
-       vkvg_fill_preserve(ctx);
-       vkvg_set_source_rgb(ctx, 0,0,0);
+       vkvg_fill_preserve(ctx);*/
+       vkvg_set_source_rgb(ctx, 0,1,0);
        vkvg_stroke(ctx);
 
        if (depth < 20)
index f451c6b0ec85924c84fd8ea2ee530cf9683e643a..7dc47d19b860392c642d1f755c18e46794de0d3f 100644 (file)
@@ -1,33 +1,27 @@
 #include "test.h"
 
-vkvg_fill_rule_t fillrule = VKVG_FILL_RULE_NON_ZERO;
-
 void paint(){
-       VkvgContext ctx = vkvg_create(surf);
-       vkvg_set_fill_rule(ctx,fillrule);
+       VkvgContext ctx = _initCtx(surf);
        vkvg_set_source_rgba(ctx,1,0,0,1);
        vkvg_paint(ctx);
        vkvg_destroy(ctx);
 }
 void paint_with_rotation(){
-       VkvgContext ctx = vkvg_create(surf);
-       vkvg_set_fill_rule(ctx,fillrule);
+       VkvgContext ctx = _initCtx(surf);
        vkvg_rotate(ctx, 45);
        vkvg_set_source_rgba(ctx,1,0,0,1);
        vkvg_paint(ctx);
        vkvg_destroy(ctx);
 }
 void paint_with_scale(){
-       VkvgContext ctx = vkvg_create(surf);
-       vkvg_set_fill_rule(ctx,fillrule);
+       VkvgContext ctx = _initCtx(surf);
        vkvg_scale (ctx, 0.2f,0.2f);
        vkvg_set_source_rgba(ctx,1,0,0,1);
        vkvg_paint(ctx);
        vkvg_destroy(ctx);
 }
 void paint_rect(){
-       VkvgContext ctx = vkvg_create(surf);
-       vkvg_set_fill_rule(ctx,fillrule);
+       VkvgContext ctx = _initCtx(surf);
        vkvg_set_source_rgba(ctx,1,0,0,1);
        vkvg_rectangle(ctx,100,100,300,200);
        vkvg_paint(ctx);
@@ -35,8 +29,7 @@ void paint_rect(){
 }
 //TODO:test failed: full screen paint instead of rotated rect
 void paint_rect_with_rotation(){
-       VkvgContext ctx = vkvg_create(surf);
-       vkvg_set_fill_rule(ctx,fillrule);
+       VkvgContext ctx = _initCtx(surf);
        vkvg_rotate(ctx, 45);
        vkvg_set_source_rgba(ctx,1,0,0,1);
        vkvg_rectangle(ctx,100,100,300,200);
@@ -44,8 +37,7 @@ void paint_rect_with_rotation(){
        vkvg_destroy(ctx);
 }
 void paint_rect_with_scale(){
-       VkvgContext ctx = vkvg_create(surf);
-       vkvg_set_fill_rule(ctx,fillrule);
+       VkvgContext ctx = _initCtx(surf);
        vkvg_scale (ctx, 0.2f,0.2f);
        vkvg_set_source_rgba(ctx,1,0,0,1);
        vkvg_rectangle(ctx,100,100,300,200);
index df454fcc25f3808c832ab6891f32ae3e62e1a100..302f8a76740a59293f8e33f3872365fe01085511 100644 (file)
@@ -345,10 +345,11 @@ void cairo_tests () {
        rotation+=0.002f;
 
        vkvg_matrix_t mat;
-       vkvg_matrix_init_translate (&mat, 512,400);
-       vkvg_matrix_rotate(&mat,rotation);
+       vkvg_matrix_init_translate (&mat, panX,panY);
        vkvg_matrix_scale(&mat,zoom,zoom);
-       vkvg_matrix_translate(&mat,-512.f + panX,-400.f +panY);
+       vkvg_matrix_translate (&mat, 400,400);
+       vkvg_matrix_rotate(&mat,rotation);
+       vkvg_matrix_translate(&mat,-400.f,-400.f);
 
 
        VkvgContext ctx = vkvg_create(surf);
@@ -357,6 +358,11 @@ void cairo_tests () {
        vkvg_paint(ctx);
 
        //vkvg_set_matrix(ctx,&mat);
+       vkvg_translate (ctx, panX,panY);
+       vkvg_scale(ctx,zoom,zoom);
+       vkvg_translate (ctx, 400,400);
+       vkvg_rotate(ctx,rotation);
+       vkvg_translate(ctx,-400.f,-400.f);
 
        cairo_print_arc(ctx);
 
diff --git a/tests/transform.c b/tests/transform.c
new file mode 100644 (file)
index 0000000..b89db50
--- /dev/null
@@ -0,0 +1,116 @@
+/*
+ * Copyright (c) 2018-2022 Jean-Philippe Bruyère <jp_bruyere@hotmail.com>
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy of
+ * this software and associated documentation files (the "Software"), to deal in
+ * the Software without restriction, including without limitation the rights to use,
+ * copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the
+ * Software, and to permit persons to whom the Software is furnished to do so, subject
+ * to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in all
+ * copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ * THE SOFTWARE.
+ */
+
+#include "test.h"
+
+
+void _test_rounded_rect (VkvgContext cr) {
+       /* a custom shape that could be wrapped in a function */
+       float x0      = -100,   /* parameters like vkvg_rectangle */
+                  y0     = -100,
+                  rect_width  = 200,
+                  rect_height = 200,
+                  radius = 102.4f;   /* and an approximate curvature radius */
+
+       float x1,y1;
+
+       x1=x0+rect_width;
+       y1=y0+rect_height;
+       if (!rect_width || !rect_height)
+               return;
+       if (rect_width/2<radius) {
+               if (rect_height/2<radius) {
+                       vkvg_move_to  (cr, x0, (y0 + y1)/2);
+                       vkvg_curve_to (cr, x0 ,y0, x0, y0, (x0 + x1)/2, y0);
+                       vkvg_curve_to (cr, x1, y0, x1, y0, x1, (y0 + y1)/2);
+                       vkvg_curve_to (cr, x1, y1, x1, y1, (x1 + x0)/2, y1);
+                       vkvg_curve_to (cr, x0, y1, x0, y1, x0, (y0 + y1)/2);
+               } else {
+                       vkvg_move_to  (cr, x0, y0 + radius);
+                       vkvg_curve_to (cr, x0 ,y0, x0, y0, (x0 + x1)/2, y0);
+                       vkvg_curve_to (cr, x1, y0, x1, y0, x1, y0 + radius);
+                       vkvg_line_to (cr, x1 , y1 - radius);
+                       vkvg_curve_to (cr, x1, y1, x1, y1, (x1 + x0)/2, y1);
+                       vkvg_curve_to (cr, x0, y1, x0, y1, x0, y1- radius);
+               }
+       } else {
+               if (rect_height/2<radius) {
+                       vkvg_move_to  (cr, x0, (y0 + y1)/2);
+                       vkvg_curve_to (cr, x0 , y0, x0 , y0, x0 + radius, y0);
+                       vkvg_line_to (cr, x1 - radius, y0);
+                       vkvg_curve_to (cr, x1, y0, x1, y0, x1, (y0 + y1)/2);
+                       vkvg_curve_to (cr, x1, y1, x1, y1, x1 - radius, y1);
+                       vkvg_line_to (cr, x0 + radius, y1);
+                       vkvg_curve_to (cr, x0, y1, x0, y1, x0, (y0 + y1)/2);
+               } else {
+                       vkvg_move_to  (cr, x0, y0 + radius);
+                       vkvg_curve_to (cr, x0 , y0, x0 , y0, x0 + radius, y0);
+                       vkvg_line_to (cr, x1 - radius, y0);
+                       vkvg_curve_to (cr, x1, y0, x1, y0, x1, y0 + radius);
+                       vkvg_line_to (cr, x1 , y1 - radius);
+                       vkvg_curve_to (cr, x1, y1, x1, y1, x1 - radius, y1);
+                       vkvg_line_to (cr, x0 + radius, y1);
+                       vkvg_curve_to (cr, x0, y1, x0, y1, x0, y1- radius);
+               }
+       }
+       vkvg_close_path (cr);
+
+       vkvg_set_source_rgb (cr, 0.5f, 0.5f, 1);
+       vkvg_fill_preserve (cr);
+       vkvg_set_source_rgba (cr, 0.5f, 0, 0, 0.5f);
+       vkvg_set_line_width (cr, 10.0f);
+       vkvg_stroke (cr);
+}
+
+
+static float rotation = 0.f;
+
+void cairo_tests () {
+       rotation+=0.002f;
+
+       VkvgContext ctx = vkvg_create(surf);
+       vkvg_set_fill_rule(ctx, VKVG_FILL_RULE_EVEN_ODD);
+       vkvg_set_source_rgba(ctx,1.0f,1.0f,1.0f,1);
+       vkvg_paint(ctx);
+
+       vkvg_set_line_width(ctx,10);
+
+       vkvg_set_source_rgb(ctx,0,0,1);
+
+       vkvg_translate (ctx, test_width/2,test_height/2);
+       vkvg_rotate(ctx, rotation);
+       //vkvg_scale(ctx, 200,200);
+
+       vkvg_arc (ctx, 0, 0, 150, 0, M_PI*1.5);
+       vkvg_stroke(ctx);
+
+       _test_rounded_rect(ctx);
+
+       vkvg_destroy(ctx);
+}
+
+
+int main(int argc, char *argv[]) {
+       no_test_size = true;
+       PERFORM_TEST (cairo_tests, argc, argv);
+       return 0;
+}