diff options
author | Oxore <oxore@protonmail.com> | 2017-11-29 02:14:29 +0300 |
---|---|---|
committer | Oxore <oxore@protonmail.com> | 2017-11-29 02:14:29 +0300 |
commit | 66783797c33d457e9d4d7ca669a6ad1fa3cf554c (patch) | |
tree | 8f63ff37fb65d6053a35fc271245a2628d609a34 | |
parent | cbe7e7f3803ecb7d4c8b3a0ebf06e086fdf0bd9f (diff) |
Change coding style
-rw-r--r-- | _vimrc_local.vim | 6 | ||||
-rwxr-xr-x | codestyle.pl | 2 | ||||
-rw-r--r-- | src/functions.c | 930 | ||||
-rw-r--r-- | src/include/common.h | 14 | ||||
-rw-r--r-- | src/include/tet_conf.h | 4 | ||||
-rw-r--r-- | src/main.c | 194 | ||||
-rw-r--r-- | src/shape_maps.c | 56 |
7 files changed, 603 insertions, 603 deletions
diff --git a/_vimrc_local.vim b/_vimrc_local.vim index fcc650a..c5055bf 100644 --- a/_vimrc_local.vim +++ b/_vimrc_local.vim @@ -1,5 +1,5 @@ if (&ft=='c' || &ft=='h' || &ft=='cpp') - setlocal shiftwidth=8 - setlocal tabstop=8 - setlocal noexpandtab + setlocal shiftwidth=4 + setlocal tabstop=4 + setlocal expandtab endif diff --git a/codestyle.pl b/codestyle.pl index 20b074d..77ce512 100755 --- a/codestyle.pl +++ b/codestyle.pl @@ -10,7 +10,7 @@ sub ln80 { or die("$file: no such file or direcory\n"); while (<FILE>) { chomp; - 1 while $_ =~ s/\t+/' ' x (length($&) * 8 - length($`) % 8)/e; + 1 while $_ =~ s/\t+/' ' x (length($&) * 4 - length($`) % 4)/e; if (length($_) > 80) { print "WRN: in $file line $. has ".length($_)."/80 chars\n"; } diff --git a/src/functions.c b/src/functions.c index c5f3297..7698540 100644 --- a/src/functions.c +++ b/src/functions.c @@ -17,7 +17,7 @@ extern sfText *textScore; extern sfFont *fontScore; extern int gameIsStarted; -extern uint8_t arrKeys; // Arrow keys states byte container +extern uint8_t arrKeys; // Arrow keys states byte container /* arrKeys = ...n|7|6|5|4|3|2|1|0| (just a bit of so called "bit fucking") * 0 - Right arrow pushed and held * 1 - Down arrow pushed and held @@ -31,9 +31,9 @@ extern uint8_t arrKeys; // Arrow keys states byte container extern sfClock *gameTick; extern sfClock *mTick; -extern sfClock *repPushDown; // Clock for repeat latency when Down arrow long push -extern sfClock *repKeyLeft; // Clock for repeat latency when Left arrow long push -extern sfClock *repKeyRight; // Clock for repeat latency when Left arrow long push +extern sfClock *repPushDown; // Clock for repeat latency when Down arrow long push +extern sfClock *repKeyLeft; // Clock for repeat latency when Left arrow long push +extern sfClock *repKeyRight; // Clock for repeat latency when Left arrow long push extern int lvlLatency; extern int scoreCurrent; @@ -51,38 +51,38 @@ extern uint8_t arrShapeT[4][4]; /* Field init routine */ void initFld() { - /* Create field */ - for (int j = 0; j < fldSize.y; j++) { - for(int i = 0; i < fldSize.x; i++) { - fldCAtt[j][i].a = 0; // Inactive = empty - fldCPos[j][i].x = fldPos.x - + (i * (fldCSize.x + 2 * fldCOutThick)); - fldCPos[j][i].y = fldPos.y - - (j * (fldCSize.y + 2 * fldCOutThick)); - fld[j][i] = sfRectangleShape_create(); - sfRectangleShape_setFillColor(fld[j][i], uiColor1); - sfRectangleShape_setSize(fld[j][i], fldCSize); - sfRectangleShape_setPosition(fld[j][i], fldCPos[j][i]); - sfRectangleShape_setOutlineColor(fld[j][i], uiColor3); - sfRectangleShape_setOutlineThickness(fld[j][i], - fldCOutThick); - } - } - /* Create next shape field */ - sfVector2f nsPos; - for (int j = 0; j < 4; j++) { - for(int i = 0; i < 4; i++) { - nsPos.x = nxtShape.x+i*(nsCSize.x+2*fldCOutThick); - nsPos.y = nxtShape.y-j*(nsCSize.y+2*fldCOutThick); - ns[j][i] = sfRectangleShape_create(); - sfRectangleShape_setSize(ns[j][i], nsCSize); - sfRectangleShape_setPosition(ns[j][i], nsPos); - sfRectangleShape_setOutlineThickness(ns[j][i], - fldCOutThick); - } - } - genNextShape(); - resetActiveShape(); + /* Create field */ + for (int j = 0; j < fldSize.y; j++) { + for(int i = 0; i < fldSize.x; i++) { + fldCAtt[j][i].a = 0; // Inactive = empty + fldCPos[j][i].x = fldPos.x + + (i * (fldCSize.x + 2 * fldCOutThick)); + fldCPos[j][i].y = fldPos.y - + (j * (fldCSize.y + 2 * fldCOutThick)); + fld[j][i] = sfRectangleShape_create(); + sfRectangleShape_setFillColor(fld[j][i], uiColor1); + sfRectangleShape_setSize(fld[j][i], fldCSize); + sfRectangleShape_setPosition(fld[j][i], fldCPos[j][i]); + sfRectangleShape_setOutlineColor(fld[j][i], uiColor3); + sfRectangleShape_setOutlineThickness(fld[j][i], + fldCOutThick); + } + } + /* Create next shape field */ + sfVector2f nsPos; + for (int j = 0; j < 4; j++) { + for(int i = 0; i < 4; i++) { + nsPos.x = nxtShape.x+i*(nsCSize.x+2*fldCOutThick); + nsPos.y = nxtShape.y-j*(nsCSize.y+2*fldCOutThick); + ns[j][i] = sfRectangleShape_create(); + sfRectangleShape_setSize(ns[j][i], nsCSize); + sfRectangleShape_setPosition(ns[j][i], nsPos); + sfRectangleShape_setOutlineThickness(ns[j][i], + fldCOutThick); + } + } + genNextShape(); + resetActiveShape(); } /* @@ -91,15 +91,15 @@ void initFld() */ void scoreDisplay(int score, sfText *textScore) { - char a[64]; - char b[8]; - sprintf(b, "Score: "); - sprintf(a, "%d", score); - for (int i = 63; i >= 7; i--) - a[i] = a[i-7]; - for (int i = 0; i < 7; i++) - a[i] = b[i]; - sfText_setString(textScore, (char *)&a); + char a[64]; + char b[8]; + sprintf(b, "Score: "); + sprintf(a, "%d", score); + for (int i = 63; i >= 7; i--) + a[i] = a[i-7]; + for (int i = 0; i < 7; i++) + a[i] = b[i]; + sfText_setString(textScore, (char *)&a); } /* @@ -108,35 +108,35 @@ void scoreDisplay(int score, sfText *textScore) */ int linesRmScore() { - int k = 0; // "Filled line" indicator - int s = 0; - for (int j = 0; j < 20; j++) { - for (int i = 0; i < 10; i++) { - if (fldCAtt[j][i].a != 0) - k++; - } - if (k >= 10) { // If line is full - s++; // Give scores for line - for (int n = j; n < 20; n++) { // Drop all lines down - if (n == 19) { - for (int m = 0; m < 10; m++) { - fldCAtt[n][m].a = 0; - fldCAtt[n][m].fColor = uiColor1; - } - break; - } - for (int m = 0; m < 10; m++) { - fldCAtt[n][m].a = fldCAtt[n+1][m].a; - fldCAtt[n][m].fColor - = fldCAtt[n+1][m].fColor; - } - } - j--; // Do not let loop to go to next line because - // next line go down by itself =) - } - k = 0; // Clear line fill indicator - } - return s; // Return number of deleted lines + int k = 0; // "Filled line" indicator + int s = 0; + for (int j = 0; j < 20; j++) { + for (int i = 0; i < 10; i++) { + if (fldCAtt[j][i].a != 0) + k++; + } + if (k >= 10) { // If line is full + s++; // Give scores for line + for (int n = j; n < 20; n++) { // Drop all lines down + if (n == 19) { + for (int m = 0; m < 10; m++) { + fldCAtt[n][m].a = 0; + fldCAtt[n][m].fColor = uiColor1; + } + break; + } + for (int m = 0; m < 10; m++) { + fldCAtt[n][m].a = fldCAtt[n+1][m].a; + fldCAtt[n][m].fColor + = fldCAtt[n+1][m].fColor; + } + } + j--; // Do not let loop to go to next line because + // next line go down by itself =) + } + k = 0; // Clear line fill indicator + } + return s; // Return number of deleted lines } /* @@ -146,44 +146,44 @@ int linesRmScore() */ void putShape() { - for (int j = 0; j < 4; j++) { - for (int i = 0; i < 4; i++) { - if (actiSh.c[j][i]) { - fldCAtt[j+actiSh.y][i+actiSh.x].a - = actiSh.c[j][i]; - if ((j+actiSh.y >= 0) - && (i+actiSh.x >= 0)) { - fldCAtt[j+actiSh.y][i+actiSh.x].fColor - = actiSh.fColor; - } - } - } - } - scoreCurrent += linesRmScore()*100; // Remove filled lines and - // get score; - for (int i = 0; i < 10; i++) - if (fldCAtt[20][i].a) { - gameover(); - return; - } - resetActiveShape(); + for (int j = 0; j < 4; j++) { + for (int i = 0; i < 4; i++) { + if (actiSh.c[j][i]) { + fldCAtt[j+actiSh.y][i+actiSh.x].a + = actiSh.c[j][i]; + if ((j+actiSh.y >= 0) + && (i+actiSh.x >= 0)) { + fldCAtt[j+actiSh.y][i+actiSh.x].fColor + = actiSh.fColor; + } + } + } + } + scoreCurrent += linesRmScore()*100; // Remove filled lines and + // get score; + for (int i = 0; i < 10; i++) + if (fldCAtt[20][i].a) { + gameover(); + return; + } + resetActiveShape(); } void resetActiveShape() { - genNextShape(); - copyShape(&actiSh); - actiSh.x = 3; - if (actiSh.t == 6) - actiSh.y = 17; - else - actiSh.y = 16; - for (;;) { - if (cellCollisionCheck(0b0010)) - actiSh.y++; - else - break; - } + genNextShape(); + copyShape(&actiSh); + actiSh.x = 3; + if (actiSh.t == 6) + actiSh.y = 17; + else + actiSh.y = 16; + for (;;) { + if (cellCollisionCheck(0b0010)) + actiSh.y++; + else + break; + } } /* @@ -191,16 +191,16 @@ void resetActiveShape() * */ void tTick() -{ // If tick exceeds current level tick latency - if (sfClock_getElapsedTime(gameTick).microseconds >= lvlLatency) { - sfClock_restart(gameTick); // Restart gameTick - /* if bottom not reached */ - if ((wallCollisionCheck(0b0010) == 0) - && (cellCollisionCheck(0b0010) == 0)) - actiSh.y--; // Move - else - putShape(); // Just put the shape - } +{ // If tick exceeds current level tick latency + if (sfClock_getElapsedTime(gameTick).microseconds >= lvlLatency) { + sfClock_restart(gameTick); // Restart gameTick + /* if bottom not reached */ + if ((wallCollisionCheck(0b0010) == 0) + && (cellCollisionCheck(0b0010) == 0)) + actiSh.y--; // Move + else + putShape(); // Just put the shape + } } @@ -210,19 +210,19 @@ void tTick() */ void rotateLeft() { - uint8_t arr[4][4]; - memcpy(&arr[0][0], &actiSh.c[0][0], sizeof(uint8_t)*4*4); - if (actiSh.t == 5) - return; - if (actiSh.t == 6) { - for (int j = 3; j >= 0; j--) - for (int i = 0; i < 4; i++) - actiSh.c[j][i] = arr[3-i][3-j]; - return; - } - for (int j = 3; j > 0; j--) - for (int i = 0; i < 3; i++) - actiSh.c[j][i] = arr[3-i][j-1]; + uint8_t arr[4][4]; + memcpy(&arr[0][0], &actiSh.c[0][0], sizeof(uint8_t)*4*4); + if (actiSh.t == 5) + return; + if (actiSh.t == 6) { + for (int j = 3; j >= 0; j--) + for (int i = 0; i < 4; i++) + actiSh.c[j][i] = arr[3-i][3-j]; + return; + } + for (int j = 3; j > 0; j--) + for (int i = 0; i < 3; i++) + actiSh.c[j][i] = arr[3-i][j-1]; } @@ -232,19 +232,19 @@ void rotateLeft() */ void rotateRight() { - uint8_t arr[4][4]; - memcpy(&arr[0][0], &actiSh.c[0][0], sizeof(uint8_t)*4*4); - if (actiSh.t == 5) - return; - if (actiSh.t == 6) { - for (int j = 3; j >= 0; j--) - for (int i = 0; i < 4; i++) - actiSh.c[j][i] = arr[3-i][3-j]; - return; - } - for (int j = 3; j > 0; j--) - for (int i = 0; i < 3; i++) - actiSh.c[j][i] = arr[i+1][3-j]; + uint8_t arr[4][4]; + memcpy(&arr[0][0], &actiSh.c[0][0], sizeof(uint8_t)*4*4); + if (actiSh.t == 5) + return; + if (actiSh.t == 6) { + for (int j = 3; j >= 0; j--) + for (int i = 0; i < 4; i++) + actiSh.c[j][i] = arr[3-i][3-j]; + return; + } + for (int j = 3; j > 0; j--) + for (int i = 0; i < 3; i++) + actiSh.c[j][i] = arr[i+1][3-j]; } @@ -254,86 +254,86 @@ void rotateRight() */ void rotateShape() { - rotateRight(); // Make rotate - if ((wallRotCollisionCheck() == 1) - || (cellRotCollisionCheck() == 1)) - rotateLeft(); // Just rotate back =) + rotateRight(); // Make rotate + if ((wallRotCollisionCheck() == 1) + || (cellRotCollisionCheck() == 1)) + rotateLeft(); // Just rotate back =) } int cellRotCollisionCheck() { - for (int j = 0; j < 4; j++) - for (int i = 0; i < 4; i++) - if (actiSh.c[j][i] - && fldCAtt[j+actiSh.y][i+actiSh.x].a) - return 1; // Collision happens - return 0; // Else it do not + for (int j = 0; j < 4; j++) + for (int i = 0; i < 4; i++) + if (actiSh.c[j][i] + && fldCAtt[j+actiSh.y][i+actiSh.x].a) + return 1; // Collision happens + return 0; // Else it do not } int wallRotCollisionCheck() { - if(actiSh.y < 0) //If shape has crossed Bottom border - for(int i = 0; i < 4; i++) - if (actiSh.c[-1-actiSh.y][i]) - return 1; // Collision happens - if(actiSh.x < 0) //If shape has crossed Left border - for(int i = 0; i < 4; i++) - if (actiSh.c[i][-1-actiSh.x]) - return 1; // Collision happens - if(actiSh.x > 6) //If shape has crossed Right border - for(int i = 0; i < 4; i++) - if (actiSh.c[i][3-(actiSh.x-7)]) - return 1; // Collision happens - return 0; // If no conditions are met collision is absent + if(actiSh.y < 0) //If shape has crossed Bottom border + for(int i = 0; i < 4; i++) + if (actiSh.c[-1-actiSh.y][i]) + return 1; // Collision happens + if(actiSh.x < 0) //If shape has crossed Left border + for(int i = 0; i < 4; i++) + if (actiSh.c[i][-1-actiSh.x]) + return 1; // Collision happens + if(actiSh.x > 6) //If shape has crossed Right border + for(int i = 0; i < 4; i++) + if (actiSh.c[i][3-(actiSh.x-7)]) + return 1; // Collision happens + return 0; // If no conditions are met collision is absent } int cellCollisionCheck(int dir) { - for (int j = 0; j < 4; j++) { - for (int i = 0; i < 4; i++) { - if ((dir & 0b0001) // Check Right - && (j+actiSh.y >= 0) // Avoiding nonexisting fld cells - && (i+actiSh.x+1 >= 0) // --- - && actiSh.c[j][i] // Check activity - && fldCAtt[j+actiSh.y][i+actiSh.x+1].a) - return 1; // Collision happens - if ((dir & 0b1000) // Check Left - && (j+actiSh.y >= 0) // Avoiding nonexisting fld cells - && (i+actiSh.x-1 >= 0) // --- - && actiSh.c[j][i] // Check activity - && fldCAtt[j+actiSh.y][i+actiSh.x-1].a) - return 1; // Collision happens - if ((dir & 0b0010) // Check Bottom - && (j+actiSh.y-1 >= 0) // Avoiding nonexisting fld cells - && (i+actiSh.x >= 0) // --- - && actiSh.c[j][i] // Check activity - && fldCAtt[j+actiSh.y-1][i+actiSh.x].a) - return 1; // Collision happens - } - } - return 0; // Else it do not + for (int j = 0; j < 4; j++) { + for (int i = 0; i < 4; i++) { + if ((dir & 0b0001) // Check Right + && (j+actiSh.y >= 0) // Avoiding nonexisting fld cells + && (i+actiSh.x+1 >= 0) // --- + && actiSh.c[j][i] // Check activity + && fldCAtt[j+actiSh.y][i+actiSh.x+1].a) + return 1; // Collision happens + if ((dir & 0b1000) // Check Left + && (j+actiSh.y >= 0) // Avoiding nonexisting fld cells + && (i+actiSh.x-1 >= 0) // --- + && actiSh.c[j][i] // Check activity + && fldCAtt[j+actiSh.y][i+actiSh.x-1].a) + return 1; // Collision happens + if ((dir & 0b0010) // Check Bottom + && (j+actiSh.y-1 >= 0) // Avoiding nonexisting fld cells + && (i+actiSh.x >= 0) // --- + && actiSh.c[j][i] // Check activity + && fldCAtt[j+actiSh.y-1][i+actiSh.x].a) + return 1; // Collision happens + } + } + return 0; // Else it do not } int wallCollisionCheck(int dir) { - if (dir & 0b0001) { // Right collision request - if (actiSh.x >= 6) // If shape has reached Right boreder - for (int i = 0 ; i < 4; i++) - if (actiSh.c[i][3-(actiSh.x-6)]) - return 1; // Collision happens - } else if (dir & 0b0010) { // Bottom collision request - if (actiSh.y <= 0) //If shape has reached Bottom border - for (int i = 0; i < 4; i++) - if (actiSh.c[-actiSh.y][i]) - return 1; // Collision happens - } else if (dir & 0b1000) // Left collision request - if (actiSh.x <= 0) // If shape has reached Left border - for (int i = 0; i < 4; i++) - if (actiSh.c[i][-actiSh.x]) - return 1; // Collision happens - return 0; + if (dir & 0b0001) { // Right collision request + if (actiSh.x >= 6) // If shape has reached Right boreder + for (int i = 0 ; i < 4; i++) + if (actiSh.c[i][3-(actiSh.x-6)]) + return 1; // Collision happens + } else if (dir & 0b0010) { // Bottom collision request + if (actiSh.y <= 0) //If shape has reached Bottom border + for (int i = 0; i < 4; i++) + if (actiSh.c[-actiSh.y][i]) + return 1; // Collision happens + } else if (dir & 0b1000) // Left collision request + if (actiSh.x <= 0) // If shape has reached Left border + for (int i = 0; i < 4; i++) + if (actiSh.c[i][-actiSh.x]) + return 1; // Collision happens + return 0; } @@ -343,103 +343,103 @@ int wallCollisionCheck(int dir) */ void tKeyCtrl() { - /* Up arrow key 'hold' handler */ - if (sfKeyboard_isKeyPressed(sfKeyUp)) { - if (!(arrKeys & 0b0100)) { - arrKeys = arrKeys | 0b0100; - rotateShape(); - } - } else { - if ((arrKeys & 0b0100)) { - arrKeys = arrKeys & ~0b0100; - } - } - - /* Down Arrow Key 'hold' handler */ - if (sfKeyboard_isKeyPressed(sfKeyDown)) { - if (!(arrKeys & 0b0010)) { - arrKeys = arrKeys | 0b0010; - if (!wallCollisionCheck(0b0010) - && !cellCollisionCheck(0b0010)) { - actiSh.y--; - // Avoid excess move down by gameTick - sfClock_restart(gameTick); - scoreCurrent++; - } - repPushDown = sfClock_create(); - } else { - if (sfClock_getElapsedTime(repPushDown).microseconds - >= moveRepeatLatency2) - arrKeys = arrKeys & ~0b0010; - } - } else { - if ((arrKeys & 0b0010)) { - arrKeys = arrKeys & ~0b0010; - arrKeys = arrKeys & ~0b100000; - } - } - - /* Left Arrow Key 'hold' handler */ - if (sfKeyboard_isKeyPressed(sfKeyLeft) - && !sfKeyboard_isKeyPressed(sfKeyRight)) { - if (!(arrKeys & 0b1000)) { - arrKeys = arrKeys | 0b1000; - if (!wallCollisionCheck(0b1000) - && !cellCollisionCheck(0b1000)) - actiSh.x--; - repKeyLeft = sfClock_create(); - } else { - if (!(arrKeys & 0b10000000)) { - if (sfClock_getElapsedTime(repKeyLeft) - .microseconds - >= moveRepeatLatency1) { - arrKeys = arrKeys | 0b10000000; - arrKeys = arrKeys & ~0b1000; - } - } else { - if (sfClock_getElapsedTime(repKeyLeft) - .microseconds - >= moveRepeatLatency2) - arrKeys = arrKeys & ~0b1000; - } - } - } else if (!sfKeyboard_isKeyPressed(sfKeyLeft)) { - if ((arrKeys & 0b1000)){ - arrKeys = arrKeys & ~0b1000; - arrKeys = arrKeys & ~0b10000000; - } - } - - /* Right Arrow Key 'hold' handler */ - if (sfKeyboard_isKeyPressed(sfKeyRight) - && !sfKeyboard_isKeyPressed(sfKeyLeft)) { - if (!(arrKeys & 0b0001)){ - arrKeys = arrKeys | 0b0001; - if (!wallCollisionCheck(0b0001) - && !cellCollisionCheck(0b0001)) - actiSh.x++; - repKeyRight = sfClock_create(); - } else { - if (!(arrKeys & 0b10000)) { - if (sfClock_getElapsedTime(repKeyRight) - .microseconds - >= moveRepeatLatency1) { - arrKeys = arrKeys | 0b10000; - arrKeys = arrKeys & ~0b0001; - } - } else if (!sfKeyboard_isKeyPressed(sfKeyLeft)) { - if (sfClock_getElapsedTime(repKeyRight) - .microseconds - >= moveRepeatLatency2) // Wait short time - arrKeys = arrKeys & ~0b0001; - } - } - } else { - if ((arrKeys & 0b0001)) { - arrKeys = arrKeys & ~0b0001; - arrKeys = arrKeys & ~0b10000; - } - } + /* Up arrow key 'hold' handler */ + if (sfKeyboard_isKeyPressed(sfKeyUp)) { + if (!(arrKeys & 0b0100)) { + arrKeys = arrKeys | 0b0100; + rotateShape(); + } + } else { + if ((arrKeys & 0b0100)) { + arrKeys = arrKeys & ~0b0100; + } + } + + /* Down Arrow Key 'hold' handler */ + if (sfKeyboard_isKeyPressed(sfKeyDown)) { + if (!(arrKeys & 0b0010)) { + arrKeys = arrKeys | 0b0010; + if (!wallCollisionCheck(0b0010) + && !cellCollisionCheck(0b0010)) { + actiSh.y--; + // Avoid excess move down by gameTick + sfClock_restart(gameTick); + scoreCurrent++; + } + repPushDown = sfClock_create(); + } else { + if (sfClock_getElapsedTime(repPushDown).microseconds + >= moveRepeatLatency2) + arrKeys = arrKeys & ~0b0010; + } + } else { + if ((arrKeys & 0b0010)) { + arrKeys = arrKeys & ~0b0010; + arrKeys = arrKeys & ~0b100000; + } + } + + /* Left Arrow Key 'hold' handler */ + if (sfKeyboard_isKeyPressed(sfKeyLeft) + && !sfKeyboard_isKeyPressed(sfKeyRight)) { + if (!(arrKeys & 0b1000)) { + arrKeys = arrKeys | 0b1000; + if (!wallCollisionCheck(0b1000) + && !cellCollisionCheck(0b1000)) + actiSh.x--; + repKeyLeft = sfClock_create(); + } else { + if (!(arrKeys & 0b10000000)) { + if (sfClock_getElapsedTime(repKeyLeft) + .microseconds + >= moveRepeatLatency1) { + arrKeys = arrKeys | 0b10000000; + arrKeys = arrKeys & ~0b1000; + } + } else { + if (sfClock_getElapsedTime(repKeyLeft) + .microseconds + >= moveRepeatLatency2) + arrKeys = arrKeys & ~0b1000; + } + } + } else if (!sfKeyboard_isKeyPressed(sfKeyLeft)) { + if ((arrKeys & 0b1000)){ + arrKeys = arrKeys & ~0b1000; + arrKeys = arrKeys & ~0b10000000; + } + } + + /* Right Arrow Key 'hold' handler */ + if (sfKeyboard_isKeyPressed(sfKeyRight) + && !sfKeyboard_isKeyPressed(sfKeyLeft)) { + if (!(arrKeys & 0b0001)){ + arrKeys = arrKeys | 0b0001; + if (!wallCollisionCheck(0b0001) + && !cellCollisionCheck(0b0001)) + actiSh.x++; + repKeyRight = sfClock_create(); + } else { + if (!(arrKeys & 0b10000)) { + if (sfClock_getElapsedTime(repKeyRight) + .microseconds + >= moveRepeatLatency1) { + arrKeys = arrKeys | 0b10000; + arrKeys = arrKeys & ~0b0001; + } + } else if (!sfKeyboard_isKeyPressed(sfKeyLeft)) { + if (sfClock_getElapsedTime(repKeyRight) + .microseconds + >= moveRepeatLatency2) // Wait short time + arrKeys = arrKeys & ~0b0001; + } + } + } else { + if ((arrKeys & 0b0001)) { + arrKeys = arrKeys & ~0b0001; + arrKeys = arrKeys & ~0b10000; + } + } } @@ -449,25 +449,25 @@ void tKeyCtrl() */ void colorizeFld() { - for(int j = 0; j < fldSize.y-5; j++) { - for(int i = 0; i < fldSize.x; i++) { - if (fldCAtt[j][i].a) { - sfRectangleShape_setFillColor( - fld[j][i], - fldCAtt[j][i].fColor); - sfRectangleShape_setOutlineColor( - fld[j][i], - uiColor3); - } else { - sfRectangleShape_setFillColor( - fld[j][i], - uiColor1); - sfRectangleShape_setOutlineColor( - fld[j][i], - uiColor2); - } - } - } + for(int j = 0; j < fldSize.y-5; j++) { + for(int i = 0; i < fldSize.x; i++) { + if (fldCAtt[j][i].a) { + sfRectangleShape_setFillColor( + fld[j][i], + fldCAtt[j][i].fColor); + sfRectangleShape_setOutlineColor( + fld[j][i], + uiColor3); + } else { + sfRectangleShape_setFillColor( + fld[j][i], + uiColor1); + sfRectangleShape_setOutlineColor( + fld[j][i], + uiColor2); + } + } + } } @@ -478,18 +478,18 @@ void colorizeFld() */ void colorizeActiSh() { - for(int j = 0; j < 4; j++) { - for(int i = 0; i < 4; i++) { - if (actiSh.c[j][i] && j+actiSh.y < 20) { - sfRectangleShape_setFillColor( - fld[j+actiSh.y][i+actiSh.x], - actiSh.fColor); - sfRectangleShape_setOutlineColor( - fld[j+actiSh.y][i+actiSh.x], - uiColor3); - } - } - } + for(int j = 0; j < 4; j++) { + for(int i = 0; i < 4; i++) { + if (actiSh.c[j][i] && j+actiSh.y < 20) { + sfRectangleShape_setFillColor( + fld[j+actiSh.y][i+actiSh.x], + actiSh.fColor); + sfRectangleShape_setOutlineColor( + fld[j+actiSh.y][i+actiSh.x], + uiColor3); + } + } + } } @@ -499,170 +499,170 @@ void colorizeActiSh() */ void drawFld(sfRenderWindow *window) { - for (int j = 0; j < fldSize.y; j++){ - for(int i = 0; i < fldSize.x; i++){ - sfRenderWindow_drawRectangleShape(window, - fld[j][i], - NULL); - } - } + for (int j = 0; j < fldSize.y; j++){ + for(int i = 0; i < fldSize.x; i++){ + sfRenderWindow_drawRectangleShape(window, + fld[j][i], + NULL); + } + } } void menuTick() { - if(sfClock_getElapsedTime(mTick).microseconds >= lvlLatency) { - sfClock_restart(mTick); - colorizeRandom(); - } + if(sfClock_getElapsedTime(mTick).microseconds >= lvlLatency) { + sfClock_restart(mTick); + colorizeRandom(); + } } void colorizeRandom() { - int a; - for (int j = 0; j < fldSize.y-5; j++) { - for (int i = 0; i < fldSize.x; i++) { - a = rand()%7+1; - switch (a) { - case 1 : - sfRectangleShape_setFillColor(fld[j][i], - tOrange); - break; - case 2 : - sfRectangleShape_setFillColor(fld[j][i], - tBlue); - break; - case 3 : - sfRectangleShape_setFillColor(fld[j][i], - tRed); - break; - case 4 : - sfRectangleShape_setFillColor(fld[j][i], - tGreen); - break; - case 5 : - sfRectangleShape_setFillColor(fld[j][i], - tYellow); - break; - case 6 : - sfRectangleShape_setFillColor(fld[j][i], - tCyan); - break; - case 7 : - sfRectangleShape_setFillColor(fld[j][i], - tMagneta); - break; - } - sfRectangleShape_setOutlineColor(fld[j][i], uiColor3); - } - } + int a; + for (int j = 0; j < fldSize.y-5; j++) { + for (int i = 0; i < fldSize.x; i++) { + a = rand()%7+1; + switch (a) { + case 1 : + sfRectangleShape_setFillColor(fld[j][i], + tOrange); + break; + case 2 : + sfRectangleShape_setFillColor(fld[j][i], + tBlue); + break; + case 3 : + sfRectangleShape_setFillColor(fld[j][i], + tRed); + break; + case 4 : + sfRectangleShape_setFillColor(fld[j][i], + tGreen); + break; + case 5 : + sfRectangleShape_setFillColor(fld[j][i], + tYellow); + break; + case 6 : + sfRectangleShape_setFillColor(fld[j][i], + tCyan); + break; + case 7 : + sfRectangleShape_setFillColor(fld[j][i], + tMagneta); + break; + } + sfRectangleShape_setOutlineColor(fld[j][i], uiColor3); + } + } } void gameover() { - gameIsStarted = 0; - scoreCurrent = 0; + gameIsStarted = 0; + scoreCurrent = 0; } void genNextShape() { - actiSh.t = nxtShape.t; - nxtShape.t = (rand()%7)+1; // Insert new random shape of 7 variants - copyShape(&nxtShape); - if (nxtShape.t == 5) - for (int j = 0; j < 3; j++) - for (int i = 0; i < 4; i++) - nxtShape.c[i][j] = nxtShape.c[i][j+1]; + actiSh.t = nxtShape.t; + nxtShape.t = (rand()%7)+1; // Insert new random shape of 7 variants + copyShape(&nxtShape); + if (nxtShape.t == 5) + for (int j = 0; j < 3; j++) + for (int i = 0; i < 4; i++) + nxtShape.c[i][j] = nxtShape.c[i][j+1]; } void copyShape(struct shapeSt *localSh) { - switch (localSh->t) { // Copy cell active/inactive state - case 1 : - memcpy(&localSh->c[0][0], - &arrShapeL[0][0], - sizeof(uint8_t)*4*4); - localSh->fColor = tOrange; - break; - case 2 : - memcpy(&localSh->c[0][0], - &arrShapeRL[0][0], - sizeof(uint8_t)*4*4); - localSh->fColor = tBlue; - break; - case 3 : - memcpy(&localSh->c[0][0], - &arrShapeZ[0][0], - sizeof(uint8_t)*4*4); - localSh->fColor = tRed; - break; - case 4 : - memcpy(&localSh->c[0][0], - &arrShapeS[0][0], - sizeof(uint8_t)*4*4); - localSh->fColor = tGreen; - break; - case 5 : - memcpy(&localSh->c[0][0], - &arrShapeB[0][0], - sizeof(uint8_t)*4*4); - localSh->fColor = tYellow; - break; - case 6 : - memcpy(&localSh->c[0][0], - &arrShapeI[0][0], - sizeof(uint8_t)*4*4); - localSh->fColor = tCyan; - break; - case 7 : - memcpy(&localSh->c[0][0], - &arrShapeT[0][0], - sizeof(uint8_t)*4*4); - localSh->fColor = tMagneta; - break; - } + switch (localSh->t) { // Copy cell active/inactive state + case 1 : + memcpy(&localSh->c[0][0], + &arrShapeL[0][0], + sizeof(uint8_t)*4*4); + localSh->fColor = tOrange; + break; + case 2 : + memcpy(&localSh->c[0][0], + &arrShapeRL[0][0], + sizeof(uint8_t)*4*4); + localSh->fColor = tBlue; + break; + case 3 : + memcpy(&localSh->c[0][0], + &arrShapeZ[0][0], + sizeof(uint8_t)*4*4); + localSh->fColor = tRed; + break; + case 4 : + memcpy(&localSh->c[0][0], + &arrShapeS[0][0], + sizeof(uint8_t)*4*4); + localSh->fColor = tGreen; + break; + case 5 : + memcpy(&localSh->c[0][0], + &arrShapeB[0][0], + sizeof(uint8_t)*4*4); + localSh->fColor = tYellow; + break; + case 6 : + memcpy(&localSh->c[0][0], + &arrShapeI[0][0], + sizeof(uint8_t)*4*4); + localSh->fColor = tCyan; + break; + case 7 : + memcpy(&localSh->c[0][0], + &arrShapeT[0][0], + sizeof(uint8_t)*4*4); + localSh->fColor = tMagneta; + break; + } } void drawNextShape(sfRenderWindow *window) { - static sfText *textNextShape; - sfVector2f textNextShapePos; - textNextShapePos.x = 250+10+10; - textNextShapePos.y = 80; - if (!textNextShape) - textNextShape = sfText_create(); - sfText_setFont(textNextShape, fontScore); - sfText_setCharacterSize(textNextShape, 20); - sfText_setPosition(textNextShape, textNextShapePos); - char a[13]; - sprintf(a, "Next Shape: "); - sfText_setString(textNextShape, (char *)&a); - sfRenderWindow_drawText(window, textNextShape, NULL); - for(int j = 0; j < 4; j++) - for(int i = 0; i < 4; i++) - if (nxtShape.c[j][i]) { - sfRectangleShape_setFillColor(ns[j][i], - nxtShape.fColor); - sfRectangleShape_setOutlineColor(ns[j][i], - uiColor3); - sfRenderWindow_drawRectangleShape(window, - ns[j][i], - NULL); - } + static sfText *textNextShape; + sfVector2f textNextShapePos; + textNextShapePos.x = 250+10+10; + textNextShapePos.y = 80; + if (!textNextShape) + textNextShape = sfText_create(); + sfText_setFont(textNextShape, fontScore); + sfText_setCharacterSize(textNextShape, 20); + sfText_setPosition(textNextShape, textNextShapePos); + char a[13]; + sprintf(a, "Next Shape: "); + sfText_setString(textNextShape, (char *)&a); + sfRenderWindow_drawText(window, textNextShape, NULL); + for(int j = 0; j < 4; j++) + for(int i = 0; i < 4; i++) + if (nxtShape.c[j][i]) { + sfRectangleShape_setFillColor(ns[j][i], + nxtShape.fColor); + sfRectangleShape_setOutlineColor(ns[j][i], + uiColor3); + sfRenderWindow_drawRectangleShape(window, + ns[j][i], + NULL); + } } /* Cleanup resources */ void cleanup() { - for (int j = 0; j < fldSize.y; j++) - for(int i = 0; i < fldSize.x; i++) - sfRectangleShape_destroy(fld[j][i]); - for (int j = 0; j < 4; j++) - for(int i = 0; i < 4; i++) - sfRectangleShape_destroy(ns[j][i]); + for (int j = 0; j < fldSize.y; j++) + for(int i = 0; i < fldSize.x; i++) + sfRectangleShape_destroy(fld[j][i]); + for (int j = 0; j < 4; j++) + for(int i = 0; i < 4; i++) + sfRectangleShape_destroy(ns[j][i]); } diff --git a/src/include/common.h b/src/include/common.h index b080898..bc3911a 100644 --- a/src/include/common.h +++ b/src/include/common.h @@ -13,8 +13,8 @@ #include "tet_conf.h" struct tCell { - uint8_t a; // active/empty state of cell - sfColor fColor; // fill color + uint8_t a; // active/empty state of cell + sfColor fColor; // fill color }; struct tCell fldCAtt[25][10];// fld cells attributes @@ -31,11 +31,11 @@ struct tCell fldCAtt[25][10];// fld cells attributes */ struct shapeSt { - int x; // x coord of shape's left side - int y; // y coord of shape's bottom - int t; // shape type - sfColor fColor; // shape color - uint8_t c[4][4]; // array of shape cells + int x; // x coord of shape's left side + int y; // y coord of shape's bottom + int t; // shape type + sfColor fColor; // shape color + uint8_t c[4][4]; // array of shape cells }; struct shapeSt actiSh; diff --git a/src/include/tet_conf.h b/src/include/tet_conf.h index eba92e5..4896a56 100644 --- a/src/include/tet_conf.h +++ b/src/include/tet_conf.h @@ -10,9 +10,9 @@ #define tCyan sfColor_fromRGB(0, 255, 255) #define tMagneta sfColor_fromRGB(192, 0, 255) #define moveRepeatLatency1 150000 // microseconds, only for left-right arrows, - // first repeat move when long push + // first repeat move when long push #define moveRepeatLatency2 30000 // microseconds, for Left, Right and Down - // arrows, the rest repeat move when long push + // arrows, the rest repeat move when long push /* @@ -16,14 +16,14 @@ sfVector2f textScore_pos; char *scoreDisp; int scoreCurrent = 0; -sfRectangleShape* ns[4][4]; // Array of next shape image rectangles -sfRectangleShape* fld[25][10]; // Array of fld rectangles -sfVector2f fldCPos[25][10]; // Array of absolute coordinates of fld - // rectangles +sfRectangleShape* ns[4][4]; // Array of next shape image rectangles +sfRectangleShape* fld[25][10]; // Array of fld rectangles +sfVector2f fldCPos[25][10]; // Array of absolute coordinates of fld + // rectangles sfRectangleShape* ns[4][4]; // Array of next shape image rectangles sfVector2f nsCSize; // Next shape rectangles size variable x/y -int fldCOutThick = 1; // Field rectangles outline thickness -sfVector2f fldCSize; // Field rectangles size variable x/y +int fldCOutThick = 1; // Field rectangles outline thickness +sfVector2f fldCSize; // Field rectangles size variable x/y sfVector2i fldSize; sfVector2f fldPos; @@ -34,118 +34,118 @@ int lvlLatency = 500000; sfClock *gameTick; sfClock *mTick; -sfClock *repPushDown; // Clock for repeat latency when Down arrow long push -sfClock *repKeyLeft; // Clock for repeat latency when Left arrow long push -sfClock *repKeyRight; // Clock for repeat latency when Left arrow long push +sfClock *repPushDown; // Clock for repeat latency when Down arrow long push +sfClock *repKeyLeft; // Clock for repeat latency when Left arrow long push +sfClock *repKeyRight; // Clock for repeat latency when Left arrow long push void prepare() { - srand( time(NULL) ); - gameTick = sfClock_create(); - mTick = sfClock_create(); - fontScore = sfFont_createFromFile("dat/arial.ttf"); - if (!fontScore) { - printf("dat/arial.ttf font load failed"); - exit(-1); - } - - - textScore_pos = (sfVector2f){.x = 250+10+10, .y = 10}; - textScore = sfText_create(); - sfText_setFont(textScore, fontScore); - sfText_setCharacterSize(textScore, 20); - sfText_setPosition(textScore, textScore_pos); - - /* - * Dimensions of every fld's cell - * 23px - fill color 1px - for outline, 25 - at all - */ - fldCSize = (sfVector2f){.x = 23, .y = 23}; //Fld's cell size in pixels - fldPos = (sfVector2f){.x = 10, .y = 10+500-20}; // Fld bot left corner - fldSize = (sfVector2i){.x = 10, .y = 25}; // Field's size in blocks - - nsCSize = (sfVector2f){.x = 23, .y = 23}; - nxtShape = (struct shapeSt){.x = 250+10+20, .y = 200}; - - initFld(); - - /* - * Menu texts - * - */ - textMenu1_pos.x = 10+250+30; - textMenu1_pos.y = 100; - textMenu1 = sfText_create(); - sfText_setFont(textMenu1, fontScore); - sfText_setCharacterSize(textMenu1, 36); - sfText_setPosition(textMenu1, textMenu1_pos); - char b[7]; - sprintf(b, "TETRIS"); - sfText_setString(textMenu1, (char *)&b); + srand( time(NULL) ); + gameTick = sfClock_create(); + mTick = sfClock_create(); + fontScore = sfFont_createFromFile("dat/arial.ttf"); + if (!fontScore) { + printf("dat/arial.ttf font load failed"); + exit(-1); + } + + + textScore_pos = (sfVector2f){.x = 250+10+10, .y = 10}; + textScore = sfText_create(); + sfText_setFont(textScore, fontScore); + sfText_setCharacterSize(textScore, 20); + sfText_setPosition(textScore, textScore_pos); + + /* + * Dimensions of every fld's cell + * 23px - fill color 1px - for outline, 25 - at all + */ + fldCSize = (sfVector2f){.x = 23, .y = 23}; //Fld's cell size in pixels + fldPos = (sfVector2f){.x = 10, .y = 10+500-20}; // Fld bot left corner + fldSize = (sfVector2i){.x = 10, .y = 25}; // Field's size in blocks + + nsCSize = (sfVector2f){.x = 23, .y = 23}; + nxtShape = (struct shapeSt){.x = 250+10+20, .y = 200}; + + initFld(); + + /* + * Menu texts + * + */ + textMenu1_pos.x = 10+250+30; + textMenu1_pos.y = 100; + textMenu1 = sfText_create(); + sfText_setFont(textMenu1, fontScore); + sfText_setCharacterSize(textMenu1, 36); + sfText_setPosition(textMenu1, textMenu1_pos); + char b[7]; + sprintf(b, "TETRIS"); + sfText_setString(textMenu1, (char *)&b); } void handleWindowEvents() { - while (sfRenderWindow_pollEvent(window, &event)) - if (event.type == sfEvtClosed) - sfRenderWindow_close(window); + while (sfRenderWindow_pollEvent(window, &event)) + if (event.type == sfEvtClosed) + sfRenderWindow_close(window); } void gameLoop() { - tTick(); - tKeyCtrl(); - scoreDisplay(scoreCurrent, textScore); - colorizeFld(); - colorizeActiSh(); - drawFld(window); - drawNextShape(window); - sfRenderWindow_drawText(window, textScore, NULL); + tTick(); + tKeyCtrl(); + scoreDisplay(scoreCurrent, textScore); + colorizeFld(); + colorizeActiSh(); + drawFld(window); + drawNextShape(window); + sfRenderWindow_drawText(window, textScore, NULL); } void menuLoop() { - menuTick(); - drawFld(window); - sfRenderWindow_drawText(window, textMenu1, NULL); - if (sfKeyboard_isKeyPressed(sfKeyS) == 1) { - gameIsStarted = 1; - cleanup(); - initFld(); - } + menuTick(); + drawFld(window); + sfRenderWindow_drawText(window, textMenu1, NULL); + if (sfKeyboard_isKeyPressed(sfKeyS) == 1) { + gameIsStarted = 1; + cleanup(); + initFld(); + } } void mainLoop() { - while (sfRenderWindow_isOpen(window)) { - handleWindowEvents(); - sfRenderWindow_clear(window, sfBlack); - if (gameIsStarted) { - gameLoop(); - } else { - menuLoop(); - } - sfRenderWindow_display(window); - } + while (sfRenderWindow_isOpen(window)) { + handleWindowEvents(); + sfRenderWindow_clear(window, sfBlack); + if (gameIsStarted) { + gameLoop(); + } else { + menuLoop(); + } + sfRenderWindow_display(window); + } } int main() { - prepare(); - window = sfRenderWindow_create(mode, - windowName_conf, - sfResize | sfClose, - NULL); - if (!window) - return EXIT_FAILURE; + prepare(); + window = sfRenderWindow_create(mode, + windowName_conf, + sfResize | sfClose, + NULL); + if (!window) + return EXIT_FAILURE; - /* colorize field once at start */ - colorizeRandom(); + /* colorize field once at start */ + colorizeRandom(); - mainLoop(); + mainLoop(); - /* Just senseless printf */ - printf("%d\n", scoreCurrent); + /* Just senseless printf */ + printf("%d\n", scoreCurrent); - cleanup(); - sfRenderWindow_destroy(window); - sfText_destroy(textScore); - sfText_destroy(textMenu1); + cleanup(); + sfRenderWindow_destroy(window); + sfText_destroy(textScore); + sfText_destroy(textMenu1); - return EXIT_SUCCESS; + return EXIT_SUCCESS; } diff --git a/src/shape_maps.c b/src/shape_maps.c index 6fc99ab..0ee8d49 100644 --- a/src/shape_maps.c +++ b/src/shape_maps.c @@ -14,10 +14,10 @@ * .... .... .... .... */ uint8_t arrShapeL[4][4] = { - {0, 0, 0, 0}, - {0, 0, 0, 0}, - {1, 1, 1, 0}, - {0, 0, 1, 0}}; + {0, 0, 0, 0}, + {0, 0, 0, 0}, + {1, 1, 1, 0}, + {0, 0, 1, 0}}; /* * RL #2 tBlue @@ -28,10 +28,10 @@ uint8_t arrShapeL[4][4] = { */ uint8_t arrShapeRL[4][4] = { - {0, 0, 0, 0}, - {0, 0, 0, 0}, - {1, 1, 1, 0}, - {1, 0, 0, 0}}; + {0, 0, 0, 0}, + {0, 0, 0, 0}, + {1, 1, 1, 0}, + {1, 0, 0, 0}}; /* * Z #3 @@ -41,10 +41,10 @@ uint8_t arrShapeRL[4][4] = { * .... .... .... .... */ uint8_t arrShapeZ[4][4] = { - {0, 0, 0, 0}, - {0, 0, 0, 0}, - {0, 1, 1, 0}, - {1, 1, 0, 0}}; + {0, 0, 0, 0}, + {0, 0, 0, 0}, + {0, 1, 1, 0}, + {1, 1, 0, 0}}; /* * S #4 @@ -54,10 +54,10 @@ uint8_t arrShapeZ[4][4] = { * .... .... .... .... */ uint8_t arrShapeS[4][4] = { - {0, 0, 0, 0}, - {0, 0, 0, 0}, - {1, 1, 0, 0}, - {0, 1, 1, 0}}; + {0, 0, 0, 0}, + {0, 0, 0, 0}, + {1, 1, 0, 0}, + {0, 1, 1, 0}}; /* @@ -68,10 +68,10 @@ uint8_t arrShapeS[4][4] = { * .... .... .... .... */ uint8_t arrShapeB[4][4] = { - {0, 0, 0, 0}, - {0, 0, 0, 0}, - {0, 1, 1, 0}, - {0, 1, 1, 0}}; + {0, 0, 0, 0}, + {0, 0, 0, 0}, + {0, 1, 1, 0}, + {0, 1, 1, 0}}; /* @@ -82,10 +82,10 @@ uint8_t arrShapeB[4][4] = { * .#.. .... .#.. .... */ uint8_t arrShapeI[4][4] = { - {0, 0, 0, 0}, - {0, 0, 0, 0}, - {1, 1, 1, 1}, - {0, 0, 0, 0}}; + {0, 0, 0, 0}, + {0, 0, 0, 0}, + {1, 1, 1, 1}, + {0, 0, 0, 0}}; /* @@ -96,8 +96,8 @@ uint8_t arrShapeI[4][4] = { * .... .... .... .... */ uint8_t arrShapeT[4][4] = { - {0, 0, 0, 0}, - {0, 0, 0, 0}, - {1, 1, 1, 0}, - {0, 1, 0, 0}}; + {0, 0, 0, 0}, + {0, 0, 0, 0}, + {1, 1, 1, 0}, + {0, 1, 0, 0}}; |