From f8dca42deecede995151366b129f6582d1602466 Mon Sep 17 00:00:00 2001 From: Ivan Mahonin Date: Aug 02 2020 17:49:01 +0000 Subject: questionBox --- diff --git a/demo/src/common.c b/demo/src/common.c index 8a05859..9740f03 100644 --- a/demo/src/common.c +++ b/demo/src/common.c @@ -8,6 +8,7 @@ #include "common.h" +static int answer = 0; static char buffer[4096] = {}; static Sound beep; @@ -77,8 +78,8 @@ void commonDraw() { textSize(16); text(buffer, 16, 48); - char fpsbuf[256] = {}; - sprintf(fpsbuf, "%6.2f", 1/worldGetFrameTime()); + char fpsbuf[1024] = {}; + sprintf(fpsbuf, "answer: %d, fps: %6.2f", answer, 1/worldGetFrameTime()); text(fpsbuf, 16, 16); if (mouseWentDown("left")) @@ -87,8 +88,16 @@ void commonDraw() { if (mouseWentDown("middle")) askTextEx("Test\ntext input", buffer, sizeof(buffer), shift, ctrl); - if (mouseWentDown("right")) - messageBox("Test message box\nwith test message."); + if (mouseWentDown("right")) { + if (ctrl) { + answer = questionBox3("Test question box3\nwith test message.", "cancel", "no", "yes"); + } else + if (shift) { + answer = questionBox("Test question box\nwith test message.", "no", "yes"); + } else { + messageBox("Test message box\nwith test message."); + } + } restoreState(); } diff --git a/demo/src/drawing.c b/demo/src/drawing.c index 2a01ac4..4ec4417 100644 --- a/demo/src/drawing.c +++ b/demo/src/drawing.c @@ -11,7 +11,7 @@ static Animation texture; void drawingInit() { background(colorByName("yellow")); - texture = createAnimationEx("data/sprite/bricks-tile.png", TRUE, TRUE); + texture = createAnimationEx("data/sprite/bricks-tile.png", TRUE, TRUE, TRUE); } diff --git a/src/animation.c b/src/animation.c index f1a170d..65b337d 100644 --- a/src/animation.c +++ b/src/animation.c @@ -104,7 +104,7 @@ int imageLoad(const char *path, int *outWidth, int *outHeight, unsigned char **o } -unsigned int imageToGLTexture(int width, int height, const unsigned char *pixels, int horWrap, int vertWrap) { +unsigned int imageToGLTexture(int width, int height, const unsigned char *pixels, int smooth, int horWrap, int vertWrap) { // convert to float and premult alpha size_t count = (size_t)width*height*4; float *buffer = (float*)malloc(count*sizeof(float)); @@ -176,8 +176,8 @@ unsigned int imageToGLTexture(int width, int height, const unsigned char *pixels unsigned int texid = 0; glGenTextures(1, &texid); glBindTexture(GL_TEXTURE_2D, texid); - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, smooth ? GL_LINEAR : GL_NEAREST); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, smooth ? GL_LINEAR_MIPMAP_LINEAR : GL_NEAREST_MIPMAP_NEAREST); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, horWrap ? GL_REPEAT : GL_CLAMP_TO_EDGE); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, vertWrap ? GL_REPEAT : GL_CLAMP_TO_EDGE); glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_LOD_BIAS, -0.7f); @@ -227,11 +227,15 @@ static void unloadTexture(HeliTexture *texture) { static HeliTexture* loadTexture(const char *key) { HeliTexture *texture = calloc(1, sizeof(*texture)); texture->key = heliStringCopy(key); - int w = 0, h = 0; - unsigned char *pixels = NULL; - if (imageLoad(key+2, &w, &h, &pixels)) { - texture->id = imageToGLTexture(w, h, pixels, key[0] == 'W', key[1] == 'W'); - free(pixels); + if (key[0] == 'T') { + texture->id = (unsigned int)atoll(key + 3); + } else { + int w = 0, h = 0; + unsigned char *pixels = NULL; + if (imageLoad(key+3, &w, &h, &pixels)) { + texture->id = imageToGLTexture(w, h, pixels, key[0] == 'L', key[1] == 'W', key[2] == 'W'); + free(pixels); + } } return texture; } @@ -275,13 +279,14 @@ Animation createAnimationEmpty() { return animation; } -Animation createAnimationEx(const char* path, int horWrap, int vertWrap) { +Animation createAnimationEx(const char* path, int smooth, int horWrap, int vertWrap) { Animation animation = createAnimationEmpty(); - char *key = heliStringConcat("CC", path); - if (horWrap) key[0] = 'W'; - if (vertWrap) key[1] = 'W'; + char *key = heliStringConcat("NCC", path); + if (smooth) key[0] = 'L'; + if (horWrap) key[1] = 'W'; + if (vertWrap) key[2] = 'W'; - Directory d = openDirectory(key + 2); + Directory d = openDirectory(key + 3); if (d) { int count = directoryGetCount(d); for(int i = 0; i < count; ++i) { @@ -300,8 +305,17 @@ Animation createAnimationEx(const char* path, int horWrap, int vertWrap) { return animation; } +Animation createAnimationFromGLTexId(unsigned int texid) { + Animation animation = createAnimationEmpty(); + char key[256] = "TTT"; + sprintf(key+3, "%d", texid); + heliArrayInsert(&animation->frames, -1, getTexture(key), (HeliFreeCallback)&unrefTexture); + return animation; +} + + Animation createAnimation(const char *path) - { return createAnimationEx(path, FALSE, FALSE); } + { return createAnimationEx(path, TRUE, FALSE, FALSE); } void animationDestroy(Animation animation) { *(animation->prev ? &animation->prev->next : &first) = animation->next; diff --git a/src/animation.h b/src/animation.h index 883beeb..c9a6aed 100644 --- a/src/animation.h +++ b/src/animation.h @@ -8,11 +8,12 @@ typedef struct _Animation *Animation; int imageLoad(const char *path, int *outWidth, int *outHeight, unsigned char **outPixels); -unsigned int imageToGLTexture(int width, int height, const unsigned char *pixels, int horWrap, int vertWrap); +unsigned int imageToGLTexture(int width, int height, const unsigned char *pixels, int smooth, int horWrap, int vertWrap); Animation createAnimation(const char *path); -Animation createAnimationEx(const char *path, int horWrap, int vertWrap); +Animation createAnimationEx(const char *path, int smooth, int horWrap, int vertWrap); +Animation createAnimationFromGLTexId(unsigned int texid); Animation createAnimationEmpty(); void animationDestroy(Animation animation); diff --git a/src/drawing.c b/src/drawing.c index 95e2aba..6776b6a 100644 --- a/src/drawing.c +++ b/src/drawing.c @@ -148,6 +148,13 @@ void rect(double x, double y, double width, double height) { endPath(TRUE, TRUE, FALSE, FALSE); } +void rectTextured(Animation animation, double x, double y, double width, double height) { + saveState(); + fillTexture(animation, x, y, width, height, FALSE); + rect(x, y, width, height); + restoreState(); +} + void line(double x1, double y1, double x2, double y2) { resetPath(); moveTo(x1, y1); @@ -296,29 +303,31 @@ void heliFillRectSimple(double x0, double y0, double x1, double y1, double aaBor double hw = (x1 - x0)*0.5; double hh = (y1 - y0)*0.5; - double w0 = hw - 0.5*aaBorder; - double w1 = hw + 0.5*aaBorder; + double k0 = 0; + double k1 = 0.5; + double w0 = hw - k0*aaBorder; + double w1 = hw + k1*aaBorder; if (w0 < HELI_PRECISION) w0 = 0; - double h0 = hh - 0.5*aaBorder; - double h1 = hh + 0.5*aaBorder; + double h0 = hh - k0*aaBorder; + double h1 = hh + k1*aaBorder; if (h0 < HELI_PRECISION) h0 = 0; - const double k = (w1 < aaBorder ? w1/aaBorder : 1) - * (h1 < aaBorder ? h1/aaBorder : 1); + const double k = (w0 ? 1 : w1/((k0+k1)*aaBorder)) + * (h0 ? 1 : h1/((k0+k1)*aaBorder)); color[3] *= k; const int vcnt = 14; const double vertices[][2] = { - { -w1, -h1 }, { -w0, -h0 }, - { w1, -h1 }, { w0, -h0 }, - { w1, h1 }, { w0, h0 }, - { -w1, h1 }, { -w0, h0 }, - { -w1, -h1 }, { -w0, -h0 }, - { -w0, -h0 }, - { w0, -h0 }, - { -w0, h0 }, - { w0, h0 } }; + { x-w1, y-h1 }, { x-w0, y-h0 }, + { x+w1, y-h1 }, { x+w0, y-h0 }, + { x+w1, y+h1 }, { x+w0, y+h0 }, + { x-w1, y+h1 }, { x-w0, y+h0 }, + { x-w1, y-h1 }, { x-w0, y-h0 }, + { x-w0, y-h0 }, + { x+w0, y-h0 }, + { x-w0, y+h0 }, + { x+w0, y+h0 } }; const double *colors[] = { sideColor, color, @@ -331,15 +340,12 @@ void heliFillRectSimple(double x0, double y0, double x1, double y1, double aaBor color, color }; - glPushMatrix(); - glTranslated(x, y, 0); glBegin(GL_TRIANGLE_STRIP); for(int i = 0; i < vcnt; ++i) { glColor4dv(colors[i]); glVertex2dv(vertices[i]); } glEnd(); - glPopMatrix(); } heliDrawingResetTexture(); } diff --git a/src/drawing.h b/src/drawing.h index 366370d..df5e59a 100644 --- a/src/drawing.h +++ b/src/drawing.h @@ -65,6 +65,7 @@ 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 rectTextured(Animation animation, double x, double y, double width, double height); void ellipse(double x, double y, double width, double height); void arc(double x, double y, double w, double h, double start, double stop); void arcPath(double x, double y, double w, double h, double start, double stop); @@ -72,6 +73,8 @@ void line(double x1, double y1, double x2, double y2); void point(double x, double y); void regularPolygon(double x, double y, int sides, double size); +void drawAnimation(Animation animation, double x, double y, double width, double height); + void moveTo(double x, double y); void lineTo(double x, double y); void resetPath(); diff --git a/src/font.c b/src/font.c index dcb89c8..369e755 100644 --- a/src/font.c +++ b/src/font.c @@ -326,7 +326,7 @@ static void expand(double *min, double *max, double x) { TextLayout createTextLayout(const char *text) { TextLayout layout = calloc(1, sizeof(*layout)); - if (!text || !text[0]) return layout; + if (!text) return layout; HeliDrawingState *drawingState = heliDrawingGetState(); diff --git a/src/sprite.c b/src/sprite.c index c67848a..61b20ea 100644 --- a/src/sprite.c +++ b/src/sprite.c @@ -298,6 +298,9 @@ void spriteSetColliderEx( } } +Animation spriteGetAnimation(Sprite sprite) + { return sprite->animation; } + void spriteSetAnimation(Sprite sprite, Animation animation) { sprite->animation = animation; } @@ -397,8 +400,7 @@ static void drawSprite(Sprite s, double aaBorder) { rotate(s->rotation); noStroke(); 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); + rectTextured(s->animation, -w, -h, 2*w, 2*h); restoreState(); } diff --git a/src/sprite.h b/src/sprite.h index bc0dc26..f003461 100644 --- a/src/sprite.h +++ b/src/sprite.h @@ -88,6 +88,7 @@ void spriteSetColliderEx( double xOffset, double yOffset, double rotationOffset, double width, double height, double radius); +Animation spriteGetAnimation(Sprite sprite); void spriteSetAnimation(Sprite sprite, Animation animation); void spriteSetNoAnimation(Sprite sprite); diff --git a/src/world.c b/src/world.c index f6a0a4c..e75dd14 100644 --- a/src/world.c +++ b/src/world.c @@ -206,6 +206,61 @@ void messageBox(const char *message) { prevFrameTimeMs = SDL_GetTicks(); } +int questionBox(const char *question, const char *answer0, const char *answer1) { + SDL_MessageBoxButtonData buttons[2] = {}; + + buttons[0].buttonid = 1; + buttons[0].flags = SDL_MESSAGEBOX_BUTTON_RETURNKEY_DEFAULT; + buttons[0].text = answer1; + + buttons[1].buttonid = 0; + buttons[1].flags = SDL_MESSAGEBOX_BUTTON_ESCAPEKEY_DEFAULT; + buttons[1].text = answer0; + + SDL_MessageBoxData data = {}; + data.flags = SDL_MESSAGEBOX_INFORMATION; + data.window = window; + data.title = title; + data.message = question; + data.buttons = buttons; + data.numbuttons = 2; + + int buttonid = 0;; + SDL_ShowMessageBox(&data, &buttonid); + prevFrameTimeMs = SDL_GetTicks(); + + return buttonid; +} + +int questionBox3(const char* question, const char* answer0, const char* answer1, const char* answer2) { + SDL_MessageBoxButtonData buttons[3] = {}; + + buttons[0].buttonid = 2; + buttons[0].flags = SDL_MESSAGEBOX_BUTTON_RETURNKEY_DEFAULT; + buttons[0].text = answer2; + + buttons[1].buttonid = 1; + buttons[1].text = answer1; + + buttons[2].buttonid = 0; + buttons[2].flags = SDL_MESSAGEBOX_BUTTON_ESCAPEKEY_DEFAULT; + buttons[2].text = answer0; + + SDL_MessageBoxData data = {}; + data.flags = SDL_MESSAGEBOX_INFORMATION; + data.window = window; + data.title = title; + data.message = question; + data.buttons = buttons; + data.numbuttons = 3; + + int buttonid = 0;; + SDL_ShowMessageBox(&data, &buttonid); + prevFrameTimeMs = SDL_GetTicks(); + + return buttonid; +} + int askText(const char *question, char *answer, int maxAnswerSize) { return askTextEx(question, answer, maxAnswerSize, FALSE, FALSE); } diff --git a/src/world.h b/src/world.h index 1b0c93f..178d534 100644 --- a/src/world.h +++ b/src/world.h @@ -46,6 +46,8 @@ int mouseIsOver(Sprite sprite); int mousePressedOver(Sprite sprite); void messageBox(const char *message); +int questionBox(const char *question, const char *answer0, const char *answer1); +int questionBox3(const char *question, const char *answer0, const char *answer1, const char *answer2); int askText(const char *question, char *answer, int maxAnswerSize); int askTextEx(const char *question, char *answer, int maxAnswerSize, int multiline, int password); diff --git a/src/worldui.c b/src/worldui.c index 7b5693a..5577bf9 100644 --- a/src/worldui.c +++ b/src/worldui.c @@ -205,7 +205,7 @@ static void draw(HeliDialog *dialog) { selPos = utf8pos(dialog->answer, selPos)*((int)sizeof(passwordPattern) - 1); } - cliprect(l-1, t-1, r-l+2, b-t+2); + cliprect(l-2, t-2, r-l+2, b-t+2); double tx = l; double ty = t; double cx = textLayoutCursorGetX(layout, pos) + tx;