From c9b119217829f976a7830a0555650401cf9df2cb Mon Sep 17 00:00:00 2001 From: axtloss Date: Tue, 27 Feb 2024 01:44:26 +0100 Subject: add functionality to read from bvg file --- fbwarn/src/BVGTypes.h | 5 +++ fbwarn/src/parseBVG.c | 68 +++++++++++++++++++++++++++--- fbwarn/src/parseBVG.h | 1 + fbwarn/src/warn.c | 113 ++++++++++++++++++++++++++------------------------ 4 files changed, 128 insertions(+), 59 deletions(-) (limited to 'fbwarn/src') diff --git a/fbwarn/src/BVGTypes.h b/fbwarn/src/BVGTypes.h index be7669f..95e1dd8 100644 --- a/fbwarn/src/BVGTypes.h +++ b/fbwarn/src/BVGTypes.h @@ -1,6 +1,11 @@ #include #include +typedef struct BVGIMG { + int width; + int height; +} BVGIMG; + typedef struct BVGRectangle { Rectangle rayrectangle; bool fill; diff --git a/fbwarn/src/parseBVG.c b/fbwarn/src/parseBVG.c index b71b55f..4209383 100644 --- a/fbwarn/src/parseBVG.c +++ b/fbwarn/src/parseBVG.c @@ -29,8 +29,6 @@ void BVGDrawCircle(BVGCircle *circle) { if (circle->drawSector) { DrawCircleSector(center, circle->radius, circle->startAngle, circle->endAngle, circle->segments, circle->color); } else { - printf("center: %f, %f\n", center.x, center.y); - printf("radius: %f, color: %d\n", circle->radius, circle->color.a); DrawCircle(circle->centerX, circle->centerY, circle->radius, circle->color); } } @@ -40,6 +38,14 @@ void BVGDrawRing(BVGRing *ring) { DrawRing(center, ring->inRadius, ring->outRadius, ring->startAngle, ring->endAngle, ring->segmets, ring->color); } +void BVGDrawTriangle(BVGTriangle *triangle) { + if (triangle->fill) { + DrawTriangle(triangle->corner1, triangle->corner2, triangle->corner3, triangle->color); + } else { + DrawTriangleLines(triangle->corner1, triangle->corner2, triangle->corner3, triangle->color); + } +} + void BVGDrawText(BVGText *text) { DrawText(text->text, text->x, text->y, text->fontSize, text->color); } @@ -61,9 +67,11 @@ FILE *readFile(char *path) { char *multiToSingle(char *s) { // allocating the size of lines is safe since characters arent added char *midresult = malloc (strlen(s)+1); + char *notab = malloc(strlen(s)+1); char *line; midresult = replaceStr(s, "\n", ""); - return replaceStr(midresult, ", ", ","); + notab = replaceStr(midresult, "\t", ""); + return replaceStr(notab, ", ", ","); } /* @@ -85,7 +93,11 @@ void collectArgs(char *res[], char *call, int n) { the res array contains only the values of each argument */ void orderArgs(char *res[], char *argv[], int n, char *knownArgs[]) { + if (argv == NULL || knownArgs == NULL) + exit(1); for(int i=0; iwidth=width; result->height=height; + return result; +} + /* Creates a BVGRectangle based on a BVG function call */ @@ -137,7 +161,6 @@ BVGRectangle *BVGParseRectangle(char *argv[7]) { size_t argN = 7; char *args[argN]; char *knownArgs[7] = {"x", "y", "width", "height", "color", "fill", "thickness"}; - //printf("118 Parsing...%s \n", args[0]); orderArgs(args, argv, argN, knownArgs); printf("119 Parsing...%s \n", args[0]); int x, y, width, height, r, g, b, a; @@ -275,6 +298,34 @@ BVGRing *BVGParseRing(char *argv[8]) { return result; } +BVGTriangle *BVGParseTriangle(char *argv[8]) { + BVGTriangle *result = malloc(sizeof(BVGTriangle)); + size_t argN = 8; + char *args[argN]; + char *knownArgs[8] = {"x1", "y1", "x2", "y2", "x3", "y3", "fill", "color"}; + orderArgs(args, argv, argN, knownArgs); + int x1, x2, x3; + int y1, y2, y3; + bool fill; + Color *clr; + sscanf(args[0], "%d", &x1); + sscanf(args[1], "%d", &y1); + sscanf(args[2], "%d", &x2); + sscanf(args[3], "%d", &y2); + sscanf(args[4], "%d", &x3); + sscanf(args[5], "%d", &y3); + fill = parseBoolValue(args[6]); + args[7] = args[7]+2; + args[7][strlen(args[7])-1] = '\0'; + clr = parseColorFromHex(args[7]); + result->corner1 = (Vector2){x1, y1}; + result->corner2 = (Vector2){x2, y2}; + result->corner3 = (Vector2){x3, y3}; + result->fill = fill; + result->color = *clr; + return result; +} + BVGText *BVGParseText(char *argv[5]) { BVGText *result = malloc(sizeof(BVGText)); size_t argN = 5; @@ -347,6 +398,13 @@ void matchFunctionCall(char *call) { BVGRing *ring = malloc(sizeof(BVGRing)); ring = BVGParseRing(argv); BVGDrawRing(ring); + } else if (strcmp(function, "triangle") == 0) { + char *argv[8]; + call = call+strlen("triangle ("); + collectArgs(argv, call, 8); + BVGTriangle *triangle = malloc(sizeof(BVGTriangle)); + triangle = BVGParseTriangle(argv); + BVGDrawTriangle(triangle); } else if (strcmp(function, "text") == 0) { char *argv[5]; call = call+strlen("text ("); diff --git a/fbwarn/src/parseBVG.h b/fbwarn/src/parseBVG.h index ea465ca..bf833cf 100644 --- a/fbwarn/src/parseBVG.h +++ b/fbwarn/src/parseBVG.h @@ -13,6 +13,7 @@ Color *parseColorFromHex(char *hex); bool parseBoolValue(char *hex); // Shape functions +BVGIMG *BVGParseIMG(char *argv[2]); BVGRectangle *BVGParseRectangle(char *argv[7]); void BVGDrawRectangle(BVGRectangle *rectangle); BVGRoundedRectangle *BVGParseRoundedRectangle(char *argv[9]); diff --git a/fbwarn/src/warn.c b/fbwarn/src/warn.c index db2750d..a495f6b 100644 --- a/fbwarn/src/warn.c +++ b/fbwarn/src/warn.c @@ -1,76 +1,81 @@ #include "raylib.h" #include "parseBVG.h" +#include "extString.h" +#include #include #include #include int main(void) { - char *rectA, *rectB, *rectC, *rectD, *rectAFree, *rectBFree, *rectCFree, *rectDFree, *singleA, *singleB, *singleC, *singleD; - char *rectE, *rectF, *rectEFree, *rectFFree, *singleE, *singleF; - rectAFree = rectA = strdup("rectangle (x=10,y=20,\nwidth=100,\nheight=100,\ncolor='#9787CFFF',\nfill=true,\nthickness=1.0)\n"); - if (rectA == NULL) - return 1; - singleA = multiToSingle(rectA); - free(rectAFree); - - rectBFree = rectB = strdup("rectangle (x=130,y=160,\nwidth=100,\nheight=60,\ncolor='#88C2B1FF',\nfill=false,\nthickness=5.0)\n"); - if (rectB == NULL) - return 1; - singleB = multiToSingle(rectB); - free(rectBFree); + FILE *bvgfile = readFile("./test.bvg"); + char *line = NULL; + size_t len = 0; + ssize_t nread = 0; + ssize_t totallinesize = 0; + ssize_t funcCount = 0; + int inComment = 0; + char *funcline = strdup(""); + char **funcs = malloc(1*sizeof(char)); + + while ((nread = getline(&line, &len, bvgfile)) != -1) { + if (strstr(line, "/*")) { + inComment += 1; + continue; + } + if (strstr(line, "*/")) { + inComment -= 1; + continue; + } + if (inComment != 0 || strstr(line, "//") || line[0] == '\n') + continue; + void *newfuncline = realloc(funcline, sizeof(char)*(strlen(funcline)+strlen(line)+1)); + if (newfuncline) + funcline = newfuncline; + else + exit(2); + totallinesize=totallinesize+nread; + sprintf(funcline, "%s%s", funcline, line); + if (line[nread-2] == ')') { + funcCount += 1; + void *newfuncs = realloc(funcs, sizeof(char)*(sizeof(funcs)+1+strlen(funcline)*2)); + if (newfuncs) + funcs = newfuncs; + else + exit(2); + funcs[funcCount-1]=strdup(funcline); + totallinesize = 0; + free(funcline); + funcline = strdup(""); + } + } + free(line); - rectCFree = rectC = strdup("circlesegment (x=300, y=200,radius=100,color='#BE79A7FF',startangle=0.0,endangle=90.0,segments=10)"); - if (rectC == NULL) - return 1; - singleC = multiToSingle(rectC); - printf("SingleC %s", rectC); - free(rectCFree); + for (int i = 0; iwidth, imgsize->height, ":3"); while (!WindowShouldClose ()) { BeginDrawing (); ClearBackground (RAYWHITE); - char *parseA = strdup(singleA); - matchFunctionCall(parseA); - - char *parseB = strdup(singleB); - matchFunctionCall(parseB); - - char *parseC = strdup(singleC); - matchFunctionCall(parseC); - char *parseD = strdup(singleD); - matchFunctionCall(parseD); - - char *parseE = strdup(singleE); - matchFunctionCall(parseE); + for (int i = 0; i