From d4e89fa0e93f1d9e143d6e28d4748950461ca98d Mon Sep 17 00:00:00 2001 From: Ivan Mahonin Date: Aug 01 2020 18:56:41 +0000 Subject: color code --- diff --git a/demo/src/drawing.c b/demo/src/drawing.c index 0cea3d0..2a01ac4 100644 --- a/demo/src/drawing.c +++ b/demo/src/drawing.c @@ -10,7 +10,7 @@ static Animation texture; void drawingInit() { - background("yellow"); + background(colorByName("yellow")); texture = createAnimationEx("data/sprite/bricks-tile.png", TRUE, TRUE); } @@ -23,27 +23,30 @@ void drawingDraw() { strokeWidth(20); - fill("red"); stroke("blue"); + fill(colorByName("red")); + stroke(colorByName("blue")); rect(x, y, 48*2, 48); x += 48*2 + 16; - fill("#C71585AA"); stroke("0 0 1 0.5"); + fill(colorByName("#C71585AA")); + stroke(colorByName("0 0 1 0.5")); ellipse(x, y, 48*2, 48); x += 48*2 + 16; - fill("green"); stroke("magenta"); + fill(colorByName("green")); + stroke(colorByName("magenta")); regularPolygon(x + 24, y + 24, 6, 48); x += 64; - stroke("brown"); + stroke(colorByName("brown")); point(x, y); - stroke("cyan"); + stroke(colorByName("cyan")); line(x, y + 48, x + 48, y); - stroke("brown"); + stroke(colorByName("brown")); arc(x, y, 48, 48, 0, 90); x += 64; - stroke("gray"); + stroke(colorByName("gray")); moveTo(x, y + 24); arcPath(x, y, 48, 48, 180, 360); lineTo(x + 48, y + 48); @@ -51,9 +54,9 @@ void drawingDraw() { strokePath(); x += 64; - fill("red"); + fill(colorByName("red")); fillTexture(texture, 0, 0, 32, 32, FALSE); - stroke("transparent"); + stroke(colorByName("transparent")); moveTo(x, y + 24); arcPath(x, y, 48, 48, 180, 360); lineTo(x + 48, y + 48); diff --git a/demo/src/font.c b/demo/src/font.c index c4b8673..dc669a8 100644 --- a/demo/src/font.c +++ b/demo/src/font.c @@ -21,7 +21,7 @@ void fontDraw() { double x = 512; double y = 256; - fill("1 0 0 0.5"); + fill(colorByName("1 0 0 0.5")); textSize(64*(1+sin(worldGetSeconds()/4))); text("Here is the\nleft aligned\ntext. VAW.", x, y); diff --git a/demo/src/main.c b/demo/src/main.c index 46d8df4..c23f938 100644 --- a/demo/src/main.c +++ b/demo/src/main.c @@ -25,7 +25,7 @@ void draw() { drawSprites(); saveState(); - stroke("red"); + stroke(colorByName("red")); strokeWidth(20); point(mouseX(), mouseY()); restoreState(); diff --git a/demo/src/phisics.c b/demo/src/phisics.c index b3824b7..f677e3c 100644 --- a/demo/src/phisics.c +++ b/demo/src/phisics.c @@ -20,7 +20,7 @@ void phisicsInit() { brick1 = createSpriteEx(200-32, 200+64, 64, 64); spriteSetAnimation(brick1, createAnimation("data/sprite/bricks.png")); - spriteSetTintColor(brick1, rgb(0, 0, 1)); + spriteSetTintColor(brick1, colorByRGB(0, 0, 1)); spriteSetColliderRectangle(brick1, 0, 0, 0, -1, -1, 20); spriteSetBounciness(brick1, 0.5); @@ -30,7 +30,7 @@ void phisicsInit() { brick3 = createSpriteEx(200+32+64, 200+64, 64, 64); spriteSetAnimation(brick3, createAnimation("data/sprite/bricks.png")); - spriteSetTintColor(brick3, rgba(1, 0, 1, 0.5)); + spriteSetTintColor(brick3, colorByRGBA(1, 0, 1, 0.5)); spriteSetColliderRectangle(brick3, 0, 0, 0, -1, -1, 20); spriteSetBounciness(brick3, 2); diff --git a/demo/src/sprites.c b/demo/src/sprites.c index 8e64421..1a65d9f 100644 --- a/demo/src/sprites.c +++ b/demo/src/sprites.c @@ -36,21 +36,21 @@ void spritesInit() { // without texture s = createSpriteEx(x, y, 48, 48); - spriteSetShapeColor(s, "blue"); + spriteSetShapeColor(s, colorByName("blue")); groupAdd(pulse, s); x += 64; // with tint color s = createSpriteEx(x, y, 48, 48); spriteSetAnimation(s, createAnimation("data/sprite/bricks.png")); - spriteSetTintColor(s, "red"); + spriteSetTintColor(s, colorByName("red")); groupAdd(pulse, s); x += 64; // semi-transparent s = createSpriteEx(x, y, 48, 48); spriteSetAnimation(s, createAnimation("data/sprite/bricks.png")); - spriteSetTintColor(s, rgba(1, 1, 1, 0.5)); + spriteSetTintColor(s, colorByRGBA(1, 1, 1, 0.5)); groupAdd(pulse, s); x += 64; @@ -75,12 +75,12 @@ void spritesInit() { // blend two sprites s = createSpriteEx(x, y, 48, 48); spriteSetAnimation(s, createAnimation("data/sprite/bricks-tile.png")); - spriteSetTintColor(s, rgba(1, 1, 1, 0.5)); + spriteSetTintColor(s, colorByRGBA(1, 1, 1, 0.5)); groupAdd(pulse, s); x -= 16; s = createSpriteEx(x, y + 16, 48, 48); spriteSetAnimation(s, createAnimation("data/sprite/bricks-tile.png")); - spriteSetTintColor(s, rgba(1, 1, 1, 0.5)); + spriteSetTintColor(s, colorByRGBA(1, 1, 1, 0.5)); groupAdd(pulse, s); x -= 64; diff --git a/dev-build.sh b/dev-build.sh index 41dad88..de546c8 100755 --- a/dev-build.sh +++ b/dev-build.sh @@ -2,7 +2,14 @@ set -e -scons PREFIX=$1 DEBUG=1 install -scons PREFIX=$1-release BUILD_DIR=build-release install -cd demo -scons +if [ "$2" == "clean" ]; then + scons PREFIX=$1 DEBUG=1 -c + scons PREFIX=$1-release BUILD_DIR=build-release -c + cd demo + scons -c +else + scons PREFIX=$1 DEBUG=1 install + scons PREFIX=$1-release BUILD_DIR=build-release install + cd demo + scons +fi diff --git a/src/animation.c b/src/animation.c index a98fb7f..f1a170d 100644 --- a/src/animation.c +++ b/src/animation.c @@ -85,8 +85,15 @@ int imageLoad(const char *path, int *outWidth, int *outHeight, unsigned char **o assert(w > 0 && h > 0); size_t rsize = (size_t)w*4; unsigned char *pixels = malloc(h*rsize); - for(int i = 0; i < h; ++i) - memcpy(pixels + i*rsize, surface->pixels + surface->pitch*i, rsize); + for(int i = 0; i < h; ++i) { + const unsigned char *pp = surface->pixels + surface->pitch*i; + for(unsigned char *p = pixels + i*rsize, *end = p + rsize; p < end; p += 4, pp += 4) { + p[0] = pp[3]; + p[1] = pp[2]; + p[2] = pp[1]; + p[3] = pp[0]; + } + } SDL_UnlockSurface(surface); SDL_FreeSurface(surface); @@ -103,11 +110,11 @@ unsigned int imageToGLTexture(int width, int height, const unsigned char *pixels float *buffer = (float*)malloc(count*sizeof(float)); const unsigned char *pp = pixels; for(float *p = buffer, *end = p + count; p < end; p += 4, pp += 4) { - if (pp[0]) { - float a = pp[0]/255.f; - p[0] = pp[3]/255.f*a; - p[1] = pp[2]/255.f*a; - p[2] = pp[1]/255.f*a; + if (pp[3]) { + float a = pp[3]/255.f; + p[0] = pp[0]/255.f*a; + p[1] = pp[1]/255.f*a; + p[2] = pp[2]/255.f*a; p[3] = a; } else { p[0] = p[1] = p[2] = p[3] = 0; diff --git a/src/common.c b/src/common.c index 331af3f..53ba18c 100644 --- a/src/common.c +++ b/src/common.c @@ -129,10 +129,34 @@ void heliLowercase(char *x) { while(*x) { *x = tolower(*x); ++x; } } -void heliParseColor(const char *x, double *color) { - color[0] = color[1] = color[2] = 0; - color[3] = 1; +static unsigned int colorToInt(double c) { + if (!(c > 0)) c = 0; + if (!(c < 1)) c = 1; + return (unsigned int)floor(c*255.99); +} + + +void heliColorToDouble(unsigned int code, double *rgba) { + rgba[0] = ( code >> 24 )/255.0; + rgba[1] = ((code >> 16) & 0xFFu)/255.0; + rgba[2] = ((code >> 8) & 0xFFu)/255.0; + rgba[3] = ( code & 0xFFu)/255.0; +} + +unsigned int colorByRGBA(double r, double g, double b, double a) { + return (colorToInt(r) << 24) + | (colorToInt(g) << 16) + | (colorToInt(b) << 8) + | colorToInt(a); +} + +unsigned int colorByRGB(double r, double g, double b) + { return colorByRGBA(r, g, b, 1); } + +unsigned int colorByName(const char *colorName) { + unsigned int code = 0; + const char *x = colorName; if (*x == '#') { ++x; int hex[8] = { 0, 0, 0, 0, 0, 0, 15, 15 }; @@ -141,8 +165,8 @@ void heliParseColor(const char *x, double *color) { if (c >= '0' && c <= '9') hex[i] = c - '0'; if (c >= 'a' && c <= 'f') hex[i] = c - 'a' + 10; } - for(int i = 0; i < 4; ++i) - color[i] = (hex[i*2]*16 + hex[i*2 + 1])/255.0; + for(int i = 0; i < 8; ++i) + code |= ( hex[i] << ((7-i)*4) ); } else if (isalpha(*x)) { int count = (int)(sizeof(colors)/sizeof(*colors)); @@ -151,12 +175,15 @@ void heliParseColor(const char *x, double *color) { do { if (tolower(*a) != tolower(*b)) break; ++a, ++b; - if (!*a && !*b) { heliParseColor(colors[i][1], color); return; } + if (!*a && !*b) return colorByName(colors[i][1]); } while (*a && *b); } } else { - sscanf(x, "%lf %lf %lf %lf", &color[0], &color[1], &color[2], &color[3]); + double r, g, b, a; + sscanf(x, "%lf %lf %lf %lf", &r, &g, &b, &a); + code = colorByRGBA(r, g, b, a); } + return code; } diff --git a/src/drawing.c b/src/drawing.c index e9d94ab..95e2aba 100644 --- a/src/drawing.c +++ b/src/drawing.c @@ -72,12 +72,12 @@ void heliDrawingResetTexture() { } -void background(const char *color) - { heliParseColor(color, colorBack); } -void fill(const char *color) - { heliParseColor(color, heliDrawingGetState()->fillColor); } +void background(unsigned int colorCode) + { heliColorToDouble(colorCode, colorBack); } +void fill(unsigned int colorCode) + { heliColorToDouble(colorCode, heliDrawingGetState()->fillColor); } void noFill() - { fill("transparent"); } + { fill(0); } void fillTexture(Animation animation, double x, double y, double width, double height, int fixed) { HeliTextureState *s = &heliDrawingGetState()->fillTexture; s->animation = animation; @@ -88,10 +88,10 @@ void fillTexture(Animation animation, double x, double y, double width, double h s->fixed = fixed != 0; } -void stroke(const char *color) - { heliParseColor(color, heliDrawingGetState()->strokeColor); } +void stroke(unsigned int colorCode) + { heliColorToDouble(colorCode, heliDrawingGetState()->strokeColor); } void noStroke() - { stroke("transparent"); } + { stroke(0); } void strokeTexture(Animation animation, double x, double y, double width, double height, int fixed) { HeliTextureState *s = &heliDrawingGetState()->strokeTexture; s->animation = animation; diff --git a/src/drawing.h b/src/drawing.h index 70be85b..366370d 100644 --- a/src/drawing.h +++ b/src/drawing.h @@ -39,12 +39,12 @@ typedef enum _StateFlags { } StateFlags; -void background(const char *color); +void background(unsigned int colorCode); -void fill(const char *color); +void fill(unsigned int colorCode); void fillTexture(Animation animation, double x, double y, double width, double height, int fixed); void noFill(); -void stroke(const char *color); +void stroke(unsigned int colorCode); void strokeTexture(Animation animation, double x, double y, double width, double height, int fixed); void noStroke(); void strokeWidth(double width); @@ -60,8 +60,9 @@ void saveState(); void saveStateEx(unsigned int flags); void restoreState(); -const char* rgb(double r, double g, double b); -const char* rgba(double r, double g, double b, double a); +unsigned int colorByName(const char *colorName); +unsigned int colorByRGB(double r, double g, double b); +unsigned int colorByRGBA(double r, double g, double b, double a); void rect(double x, double y, double width, double height); void ellipse(double x, double y, double width, double height); diff --git a/src/group.c b/src/group.c index a2acd38..c4d0d22 100644 --- a/src/group.c +++ b/src/group.c @@ -162,9 +162,9 @@ double groupGetMaxDepth(Group group) { static void foreachInt(Group group, int value, HeliSpriteEashInt func) { for(int i = 0; i < groupGetCount(group); ++i) func(groupGet(group, i), value); } -static void foreachDouble(Group group, double value, HeliSpriteEashDouble func) +static void foreachUInt(Group group, unsigned int value, HeliSpriteEashUInt func) { for(int i = 0; i < groupGetCount(group); ++i) func(groupGet(group, i), value); } -static void foreachString(Group group, const char *value, HeliSpriteEashString func) +static void foreachDouble(Group group, double value, HeliSpriteEashDouble func) { for(int i = 0; i < groupGetCount(group); ++i) func(groupGet(group, i), value); } @@ -197,10 +197,10 @@ void groupSetMirrorYEach(Group group, int mirrorY) { foreachInt(group, mirrorY, &spriteSetMirrorY); } void groupSetTagEach(Group group, int tag) { foreachInt(group, tag, &spriteSetTag); } -void groupSetShapeColorEach(Group group, const char *color) - { foreachString(group, color, &spriteSetShapeColor); } -void groupSetTintColorEach(Group group, const char *color) - { foreachString(group, color, &spriteSetTintColor); } +void groupSetShapeColorEach(Group group, unsigned int colorCode) + { foreachUInt(group, colorCode, &spriteSetShapeColor); } +void groupSetTintColorEach(Group group, unsigned int colorCode) + { foreachUInt(group, colorCode, &spriteSetTintColor); } void groupSetAnimationEach(Group group, Animation animation) { for(int i = groupGetCount(group) - 1; i >= 0 ; --i) diff --git a/src/group.h b/src/group.h index b618c7c..1fb7540 100644 --- a/src/group.h +++ b/src/group.h @@ -64,8 +64,8 @@ void groupPointToEach(Group group, double x, double y); void groupSetSpeedAndDirectionEach(Group group, double speed, double angle); void groupSetAnimationEach(Group group, Animation animation); void groupSetNoAnimationEach(Group group); -void groupSetShapeColorEach(Group group, const char *color); -void groupSetTintColorEach(Group group, const char *color); +void groupSetShapeColorEach(Group group, unsigned int colorCode); +void groupSetTintColorEach(Group group, unsigned int colorCode); void groupSetColliderEach(Group group, Collider type, double xOffset, double yOffset, double rotationOffset); void groupSetColliderCircleEach(Group group, double xOffset, double yOffset, double radius); diff --git a/src/private.h b/src/private.h index 64b3842..f98d5ee 100644 --- a/src/private.h +++ b/src/private.h @@ -39,7 +39,11 @@ char* heliStringConcat(const char *a, const char *b); char* heliStringConcat3(const char *a, const char *b, const char *c); int heliStringEndsWithLowcase(const char *s, const char *tail); void heliLowercase(char *x); -void heliParseColor(const char *x, double *color); + + +// common + +void heliColorToDouble(unsigned int code, double *rgba); // pointer array @@ -199,8 +203,8 @@ int heliPointCollision(HeliCollider *c, double x, double y); // sprite typedef void (*HeliSpriteEashInt)(Sprite, int); +typedef void (*HeliSpriteEashUInt)(Sprite, unsigned int); typedef void (*HeliSpriteEashDouble)(Sprite, double); -typedef void (*HeliSpriteEashString)(Sprite, const char*); HeliArray* heliSpriteGetGroups(Sprite sprite); void heliSpriteUpdate(double dt); void heliSpriteFinish(); diff --git a/src/sprite.c b/src/sprite.c index bd14b2f..c67848a 100644 --- a/src/sprite.c +++ b/src/sprite.c @@ -304,10 +304,10 @@ void spriteSetAnimation(Sprite sprite, Animation animation) void spriteSetNoAnimation(Sprite sprite) { spriteSetAnimation(sprite, NULL); } -void spriteSetShapeColor(Sprite sprite, const char *color) - { heliParseColor(color, sprite->shapeColor); } -void spriteSetTintColor(Sprite sprite, const char *color) - { heliParseColor(color, sprite->tintColor); } +void spriteSetShapeColor(Sprite sprite, unsigned int colorCode) + { heliColorToDouble(colorCode, sprite->shapeColor); } +void spriteSetTintColor(Sprite sprite, unsigned int colorCode) + { heliColorToDouble(colorCode, sprite->tintColor); } void spriteSetVelocityXY(Sprite sprite, double x, double y) { sprite->vx = x; @@ -354,8 +354,8 @@ static void drawSpriteDebug(Sprite s) { double hw = 0.5 * s->scale * s->width; double hh = 0.5 * s->scale * s->height; - fill("transparent"); - stroke(rgba(0, 0, 0, 0.75)); + noFill(); + stroke(colorByRGBA(0, 0, 0, 0.75)); // frame rect(-hw, -hh, 2*hw, 2*hh); @@ -365,7 +365,6 @@ static void drawSpriteDebug(Sprite s) { line(0, -hh/4, 0, hh/4); // depth - stroke(rgba(0, 0, 0, 0.75)); char buf[1024]; snprintf(buf, sizeof(buf)-1, "%lf", s->depth); double s1 = hw*0.25; @@ -397,7 +396,7 @@ static void drawSprite(Sprite s, double aaBorder) { translate(s->x, s->y); rotate(s->rotation); noStroke(); - fill(rgba(color[0], color[1], color[2], color[3])); + fill(colorByRGBA(color[0], color[1], color[2], color[3])); fillTexture(s->animation, -w, -h, 2*w, 2*h, FALSE); rect(-w, -h, 2*w, 2*h); restoreState(); diff --git a/src/sprite.h b/src/sprite.h index b5497f0..bc0dc26 100644 --- a/src/sprite.h +++ b/src/sprite.h @@ -91,8 +91,8 @@ void spriteSetColliderEx( void spriteSetAnimation(Sprite sprite, Animation animation); void spriteSetNoAnimation(Sprite sprite); -void spriteSetShapeColor(Sprite sprite, const char *color); -void spriteSetTintColor(Sprite sprite, const char *color); +void spriteSetShapeColor(Sprite sprite, unsigned int colorCode); +void spriteSetTintColor(Sprite sprite, unsigned int colorCode); void spriteSetVelocityXY(Sprite sprite, double x, double y); void spriteSetSpeedAndDirection(Sprite sprite, double speed, double angle); diff --git a/src/worldui.c b/src/worldui.c index ea7a443..7b5693a 100644 --- a/src/worldui.c +++ b/src/worldui.c @@ -170,10 +170,10 @@ static void draw(HeliDialog *dialog) { const double br1 = w - border + 2; const double bl1 = br1 - bw; - const char *strokeColor = "white"; - const char *fillColor = "0.3 0.3 0.3"; - const char *selTextColor = "black"; - const char *selFillColor = "white"; + unsigned int strokeColor = colorByName("white"); + unsigned int fillColor = colorByName("0.3 0.3 0.3"); + unsigned int selTextColor = colorByName("black"); + unsigned int selFillColor = colorByName("white"); strokeWidth(1); textFontDefault(); textSize(16); @@ -243,7 +243,7 @@ static void draw(HeliDialog *dialog) { textLayoutDrawFrom(layout, tx, ty, p1); } - stroke(rgba(1, 1, 1, 0.5 + 0.5*sin(time/0.5*2*PI))); + stroke(colorByRGBA(1, 1, 1, 0.5 + 0.5*sin(time/0.5*2*PI))); line(cx, cy, cx, cy - ch); textLayoutDestroy(layout);