aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorAlex Gaynor <alex.gaynor@gmail.com>2018-09-11 11:48:47 -0400
committerPaul Kehrer <paul.l.kehrer@gmail.com>2018-09-11 11:48:47 -0400
commit245fbdfc76b33547ea8eada515c520286ed656e4 (patch)
tree205e35737263af1c841c7dffe1fc774194ee0cfe
parent09403100de2f6f1cdd0d484dcb8e620f1c335c8f (diff)
downloadcryptography-245fbdfc76b33547ea8eada515c520286ed656e4.tar.gz
cryptography-245fbdfc76b33547ea8eada515c520286ed656e4.tar.bz2
cryptography-245fbdfc76b33547ea8eada515c520286ed656e4.zip
1.1.1 is out! (#4466)
-rw-r--r--.travis.yml4
1 files changed, 2 insertions, 2 deletions
diff --git a/.travis.yml b/.travis.yml
index a34c50e9..de78953f 100644
--- a/.travis.yml
+++ b/.travis.yml
@@ -42,9 +42,9 @@ matrix:
- python: 3.5
env: TOXENV=py35 OPENSSL=1.1.0i
- python: 2.7
- env: TOXENV=py27 OPENSSL=1.1.1-pre9
+ env: TOXENV=py27 OPENSSL=1.1.1
- python: 3.7
- env: TOXENV=py37 OPENSSL=1.1.1-pre9
+ env: TOXENV=py37 OPENSSL=1.1.1
dist: xenial
sudo: true
- python: 3.7
='n139' href='#n139'>139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501
#include "gfx.h"
#include "src/gwin/gwin_class.h"
#include "stdlib.h"
#include "string.h"
#include "jg10.h"

GEventMouse     ev;
GHandle         mainWin, Jg10SelectWidget;

typedef struct jg10WidgetObject_t {
    GWidgetObject w;  // Base Class
} jg10WidgetObject;

GHandle jg10SelectionWidgetGCreate(GDisplay* g, jg10WidgetObject* wo, GWidgetInit* pInit);
#define jg10SelectionWidgetCreate(wo, pInit) jg10SelectionWidgetGCreate(GDISP, wo, pInit)


typedef struct {          // Node properties
  uint8_t num;            // Node number
  gBool check;           // Node needs to be checked or not
  gBool sel;             // Node selected or not
} nodeProps;

nodeProps             jg10Field[JG10_FIELD_WIDTH][JG10_FIELD_HEIGHT];   // jg10 field array
gBool                jg10GameOver                           = gFalse;
const char            *jg10Graph[] = {"background.bmp", "1.bmp","2.bmp","3.bmp","4.bmp","5.bmp","6.bmp","7.bmp","8.bmp", "9.bmp", "10.bmp", "11.bmp", "12.bmp", "13.bmp", "14.bmp", "15.bmp", "16.bmp", "17.bmp", "18.bmp", "19.bmp", "20.bmp"}; // 21 elements (0-20)
gdispImage            jg10Image[JG10_MAX_COUNT];
#define JG10_ANIM_IMAGES 5
#define JG10_ANIM_DELAY 60
const char            *jg10GraphAnim[] = {"a1.bmp","a2.bmp","a3.bmp","a4.bmp","background.bmp"}; // 5 elements (0-4)
gdispImage            jg10ImageAnim[JG10_ANIM_IMAGES];
uint8_t               jg10MaxVal=4;                                     // Max value in field...
font_t font;
#if JG10_SHOW_SPLASH
GTimer                jg10SplashBlink;
gBool                jg10SplashTxtVisible = gFalse;
gdispImage            jg10SplashImage;
#endif


static void initRng(void) {
    srand(gfxSystemTicks());
}

static uint32_t randomInt(uint32_t max) {
    return rand() % max;
}

static int uitoa(unsigned int value, char * buf, int max) {
  int n = 0;
  int i = 0;
  int tmp = 0;

  if (!buf) return -3;
  if (2 > max) return -4;
  i=1;
  tmp = value;
  if (0 > tmp) {
    tmp *= -1;
    i++;
  }
  for (;;) {
    tmp /= 10;
    if (0 >= tmp) break;
    i++;
  }
  if (i >= max) {
    buf[0] = '?';
    buf[1] = 0x0;
    return 2;
  }
  n = i;
  tmp = value;
  if (0 > tmp) {
    tmp *= -1;
  }
  buf[i--] = 0x0;
  for (;;) {
    buf[i--] = (tmp % 10) + '0';
    tmp /= 10;
    if (0 >= tmp) break;
  }
  if (-1 != i) {
    buf[i--] = '-';
  }
  return n;
}

static gBool inRange(int16_t x, int16_t y) {
    if ((x >= 0) && (x < JG10_FIELD_WIDTH) && (y >= 0) && (y < JG10_FIELD_HEIGHT)) return gTrue; else return gFalse;
}

static void clean_SelCheck(void) {
    uint16_t i ,j;
    for (i = 0; i < JG10_FIELD_WIDTH; i++) {
        for (j = 0; j < JG10_FIELD_HEIGHT; j++) {
            jg10Field[i][j].check = gFalse;
            jg10Field[i][j].sel = gFalse;
        }
    }
}

static void remove_Selected(void) {
    uint16_t i ,j, step;
    systemticks_t delay_start = 0;
    systemticks_t delay=0;
    for (step = 0; step < JG10_ANIM_IMAGES; step++) {
        delay_start = gfxSystemTicks();
        for (i = 0; i < JG10_FIELD_WIDTH; i++) {
            for (j = 0; j < JG10_FIELD_HEIGHT; j++) {
                if (jg10Field[i][j].sel) {
                    gdispImageDraw(&jg10ImageAnim[step], (i*JG10_CELL_HEIGHT)+1, (j*JG10_CELL_WIDTH)+1, JG10_CELL_WIDTH, JG10_CELL_HEIGHT, 0, 0);
                }
            }
        }
        delay = gfxSystemTicks()-delay_start;
        if (delay <= JG10_ANIM_DELAY) {
            gfxSleepMilliseconds(JG10_ANIM_DELAY-delay);
        }
    }
    for (i = 0; i < JG10_FIELD_WIDTH; i++) {
        for (j = 0; j < JG10_FIELD_HEIGHT; j++) {
            if (jg10Field[i][j].sel) {
                jg10Field[i][j].sel = gFalse;
                jg10Field[i][j].num = 0;
            }
        }
    }
//    gwinRedraw(mainWin);
}

static uint8_t jg10_randomer(uint8_t max, uint8_t th) {
    uint32_t r = randomInt((1<<max)-1);
	
    if (r != 0) {
        for (int8_t i = max; i >= 0; i--) {
            if (r >= (uint32_t)(1<<i)) {
                if ((max-i) >= th) {
                    return randomInt(max-i)+1;
                } else {
                    return randomInt(th)+1;
                }
            }
        }
    }
    return randomInt(max-1)+1;
}

static void movePiecesDown(void) {
    uint8_t tmp = 0;
    gBool needToCheck = gTrue;
    while (needToCheck) {
        needToCheck = gFalse;
        for (int8_t y = (JG10_FIELD_HEIGHT-1); y >= 0; y--) {
            for (uint8_t x = 0; x < JG10_FIELD_WIDTH; x++) {
                if (jg10Field[x][y].num == 0) {
                    // check if there is at least single none empty piece
                    tmp = 0;
                    for (int8_t tmpy = y; tmpy >= 0; tmpy--) {
                        if (jg10Field[x][tmpy].num != 0) tmp++;
                    }
                    if (tmp != 0) {
                        for (int8_t tmpy = y; tmpy > 0; tmpy--) {
                            jg10Field[x][tmpy].num = jg10Field[x][tmpy-1].num;
                        }
                        jg10Field[x][0].num = 0;
                        needToCheck = gTrue;
                    }
                }
            }
        }
    }
    gwinRedraw(mainWin);
    // Add new pieces
    needToCheck = gTrue;
    while (needToCheck) {
        needToCheck = gFalse;
        for (int8_t y = (JG10_FIELD_HEIGHT-1); y >= 0; y--) {
            for (uint8_t x = 0; x < JG10_FIELD_WIDTH; x++) {
                if (jg10Field[x][y].num == 0) {
                    for (int8_t tmpy = y; tmpy > 0; tmpy--) {
                        jg10Field[x][tmpy].num = jg10Field[x][tmpy-1].num;
                    }
                    jg10Field[x][0].num = jg10_randomer(jg10MaxVal, 3);
                    needToCheck = gTrue;
                }
            }
        }
        gwinRedraw(mainWin);
        gfxSleepMilliseconds(50);
    }
}

static gBool checkForPossibleMove(void) {
    gBool canMove = gFalse;
    uint16_t i ,j;
    for (i = 0; i < JG10_FIELD_WIDTH; i++) {
        for (j = 0; j < JG10_FIELD_HEIGHT; j++) {
            if ((inRange(i,j-1) && jg10Field[i][j-1].num == jg10Field[i][j].num) ||
                (inRange(i-1,j) && jg10Field[i-1][j].num == jg10Field[i][j].num) ||
                (inRange(i,j+1) && jg10Field[i][j+1].num == jg10Field[i][j].num) ||
                (inRange(i+1,j) && jg10Field[i+1][j].num == jg10Field[i][j].num)) {
                canMove = gTrue;
                return canMove;
            }
        }
    }
    return canMove;
}

static void printGameOver(void) {
    gdispFillArea(JG10_TOTAL_FIELD_WIDTH, (gdispGetHeight()/2)-10, gdispGetWidth()-JG10_TOTAL_FIELD_WIDTH, 80, GFX_BLACK);
    if (jg10GameOver) {
        gdispDrawString(JG10_TOTAL_FIELD_WIDTH+((gdispGetWidth()-JG10_TOTAL_FIELD_WIDTH)/2)-(gdispGetStringWidth("Game Over", font)/2), gdispGetHeight()/2, "Game Over", font, GFX_WHITE);
    }
}

static void printCongrats(void) {
    char pps_str[3];
    gdispFillArea(JG10_TOTAL_FIELD_WIDTH, (gdispGetHeight()/2)-10, gdispGetWidth()-JG10_TOTAL_FIELD_WIDTH, 80, GFX_BLACK);
    gdispDrawString(JG10_TOTAL_FIELD_WIDTH+((gdispGetWidth()-JG10_TOTAL_FIELD_WIDTH)/2)-(gdispGetStringWidth("Congrats, now try to get", font)/2), gdispGetHeight()/2, "Congrats, now try to get", font, GFX_WHITE);
    uitoa(jg10MaxVal+1, pps_str, sizeof(pps_str));
    gdispDrawString(JG10_TOTAL_FIELD_WIDTH+((gdispGetWidth()-JG10_TOTAL_FIELD_WIDTH)/2)-(gdispGetStringWidth(pps_str, font)/2), (gdispGetHeight()/2)+20, pps_str, font, GFX_RED);
}

static DECLARE_THREAD_FUNCTION(thdJg10, msg) {
    (void)msg;
    uint16_t x,y;
    while (!jg10GameOver) {
        srand(gfxSystemTicks());
        ginputGetMouseStatus(0, &ev);
        if (ev.buttons & GINPUT_MOUSE_BTN_LEFT) {
            x = ev.x/JG10_CELL_WIDTH;
            y = ev.y/JG10_CELL_HEIGHT;
            if (x < JG10_FIELD_WIDTH && y < JG10_FIELD_HEIGHT) {
                while (ev.buttons & GINPUT_MOUSE_BTN_LEFT) { // Wait until release
                    ginputGetMouseStatus(0, &ev);
                }
                if (!jg10Field[x][y].sel) {
                    // Check if we have at least two
                    if ((inRange(x,y-1) && jg10Field[x][y-1].num == jg10Field[x][y].num) ||
                        (inRange(x-1,y) && jg10Field[x-1][y].num == jg10Field[x][y].num) ||
                        (inRange(x,y+1) && jg10Field[x][y+1].num == jg10Field[x][y].num) ||
                        (inRange(x+1,y) && jg10Field[x+1][y].num == jg10Field[x][y].num)) {
                        gwinSetVisible(Jg10SelectWidget, gFalse);
                        clean_SelCheck();
                        jg10Field[x][y].check = gTrue;
                        gwinSetVisible(Jg10SelectWidget, gTrue);
                    }
                } else {
                    // already selected section clicked...
                    jg10Field[x][y].num++;
                    if (jg10Field[x][y].num > jg10MaxVal) {
                        jg10MaxVal = jg10Field[x][y].num;
                        if (jg10MaxVal >= 10) printCongrats();
                        if (jg10MaxVal == 20) { // Just in case someone got so far :D I cannot imaginge though 
                            jg10GameOver = gTrue;
                            printGameOver();
                        }
                    }
                    jg10Field[x][y].sel = gFalse;
                    gwinSetVisible(Jg10SelectWidget, gFalse);
                    remove_Selected();
                    movePiecesDown();
                    if (checkForPossibleMove()) {
                        clean_SelCheck();
                        //gwinRedraw(mainWin);
                    } else {
                        jg10GameOver = gTrue;
                        printGameOver();
                    }
                }
            }
        }
    }
    THREAD_RETURN(0);
}

static void initField(void) {
    jg10MaxVal = 4;
    for (uint8_t x = 0; x < JG10_FIELD_WIDTH; x++) {
        for (uint8_t y = 0; y < JG10_FIELD_HEIGHT; y++) {
          jg10Field[x][y].num = randomInt(jg10MaxVal)+1;
            //jg10Field[x][y].num = 1;      // good for animation testing
            //jg10Field[x][y].num = x+x+5;    // good to get high score fast
            //jg10Field[x][y].num = x+y+5;  // good demo to check out pieces :D
            jg10Field[x][y].check = gFalse;
            jg10Field[x][y].sel = gFalse;
        }
    }
    jg10GameOver = gFalse;
    printGameOver();
}

static void mainWinDraw(GWidgetObject* gw, void* param) {
	(void)param;
	
    for (uint8_t x = 0; x < JG10_FIELD_WIDTH; x++) {
        for (uint8_t y = 0; y < JG10_FIELD_HEIGHT; y++) {
            gdispGImageDraw(gw->g.display, &jg10Image[jg10Field[x][y].num], (x*JG10_CELL_HEIGHT)+1, (y*JG10_CELL_WIDTH)+1, JG10_CELL_WIDTH, JG10_CELL_HEIGHT, 0, 0);
        }
    }
}

static void jg10SelectionWidget_Draw(GWidgetObject* gw, void* param) {
    int16_t x, y;
    gBool needToCheck = gTrue;

	(void)param;

    while (needToCheck) {
        needToCheck = gFalse;
        for (x = 0; x < JG10_FIELD_WIDTH; x++) {
            for (y = 0; y < JG10_FIELD_HEIGHT; y++) {
                if (jg10Field[x][y].check && !jg10Field[x][y].sel) {
                    jg10Field[x][y].sel = gTrue;
                    jg10Field[x][y].check = gFalse;
                    // Up
                    if (inRange(x, y-1) && !jg10Field[x][y-1].sel && (jg10Field[x][y-1].num == jg10Field[x][y].num)) {
                        jg10Field[x][y-1].check = gTrue;
                        needToCheck = gTrue;
                    } else  if (!inRange(x, y-1) || (inRange(x, y-1) && !jg10Field[x][y-1].sel)) {
                        // We need longer line if this is wide corner inside shape
                        if (inRange(x+1, y) && inRange(x+1, y-1) && (jg10Field[x][y].num == jg10Field[x+1][y].num) && (jg10Field[x][y].num == jg10Field[x+1][y-1].num)) {
                            gdispGFillArea(gw->g.display, (x*JG10_CELL_WIDTH)+1, (y*JG10_CELL_HEIGHT)+1, JG10_CELL_WIDTH+2, 2, JG10_SEL_COLOR);
                        } else {
                            gdispGFillArea(gw->g.display, (x*JG10_CELL_WIDTH)+1, (y*JG10_CELL_HEIGHT)+1, JG10_CELL_WIDTH, 2, JG10_SEL_COLOR);
                        }
                    }
                    // Down
                    if (inRange(x, y+1) && !jg10Field[x][y+1].sel && (jg10Field[x][y+1].num == jg10Field[x][y].num)) {
                        jg10Field[x][y+1].check = gTrue;
                        needToCheck = gTrue;
                    } else if (!inRange(x, y+1) || (inRange(x, y+1) && !jg10Field[x][y+1].sel)) {
                        // We need longer line if this is wide corner inside shape
                        if (inRange(x-1, y) && inRange(x-1, y+1) && (jg10Field[x][y].num == jg10Field[x-1][y].num) && (jg10Field[x][y].num == jg10Field[x-1][y+1].num)) {
                            gdispGFillArea(gw->g.display, (x*JG10_CELL_WIDTH)-1, ((y+1)*JG10_CELL_HEIGHT)-1, JG10_CELL_WIDTH+2, 2, JG10_SEL_COLOR);
                        } else {
                            gdispGFillArea(gw->g.display, (x*JG10_CELL_WIDTH)+1, ((y+1)*JG10_CELL_HEIGHT)-1, JG10_CELL_WIDTH, 2, JG10_SEL_COLOR);
                        }
                    }
                    // Left
                    if (inRange(x-1, y) && !jg10Field[x-1][y].sel && (jg10Field[x-1][y].num == jg10Field[x][y].num)) {
                        jg10Field[x-1][y].check = gTrue;
                        needToCheck = gTrue;
                    } else if (!inRange(x-1, y) || (inRange(x-1, y) && !jg10Field[x-1][y].sel)) {
                        // We need longer line if this is wide corner inside shape
                        if (inRange(x, y-1) && inRange(x-1, y-1) && (jg10Field[x][y].num == jg10Field[x][y-1].num) && (jg10Field[x][y].num == jg10Field[x-1][y-1].num)) {
                            gdispGFillArea(gw->g.display, (x*JG10_CELL_WIDTH)+1, (y*JG10_CELL_HEIGHT)-1, 2, JG10_CELL_HEIGHT+2, JG10_SEL_COLOR);
                        } else {
                            gdispGFillArea(gw->g.display, (x*JG10_CELL_WIDTH)+1, (y*JG10_CELL_HEIGHT)+1, 2, JG10_CELL_HEIGHT, JG10_SEL_COLOR);
                        }
                    }
                    // Right
                    if (inRange(x+1, y) && !jg10Field[x+1][y].sel && (jg10Field[x+1][y].num == jg10Field[x][y].num)) {
                        jg10Field[x+1][y].check = gTrue;
                        needToCheck = gTrue;
                    } else if (!inRange(x+1, y) || (inRange(x+1, y) && !jg10Field[x+1][y].sel)) {