summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAndrew Opalach <andrew@akon.city> 2019-10-26 22:25:53 -0400
committerAndrew Opalach <andrew@akon.city> 2019-10-26 22:25:53 -0400
commit06701c4619678f8f34ce9c66c20c73645c85912e (patch)
tree3032c4725470dbe65b421460716320184be7226c
parente6be4377a3a078addb73983e4cde179f2acda061 (diff)
downloadcetris-06701c4619678f8f34ce9c66c20c73645c85912e.tar.gz
cetris-06701c4619678f8f34ce9c66c20c73645c85912e.tar.bz2
cetris-06701c4619678f8f34ce9c66c20c73645c85912e.zip
add piece queue and hold fix memory
-rw-r--r--frontends/sdl/sdl_ui.c83
-rw-r--r--lib/cetris.h31
2 files changed, 82 insertions, 32 deletions
diff --git a/frontends/sdl/sdl_ui.c b/frontends/sdl/sdl_ui.c
index 0fe4081..069db4e 100644
--- a/frontends/sdl/sdl_ui.c
+++ b/frontends/sdl/sdl_ui.c
@@ -19,7 +19,7 @@
#include <cetris.h>
#include <ini.h>
-#define W 480
+#define W 570
#define H 640
#define BLOCK_SIZE 25
@@ -34,19 +34,6 @@ SDL_Surface *screen;
cetris_game g;
-/*
-uint8_t colors[8][3] = {
- {0, 0, 0}, // Black
- {127,219,255}, // Aqua
- {61,153,112}, // Olive
- {177,13,201}, // Purple
- {240,18,190}, // Fuchsia
- {255,133,27}, // Orange
- {0,31,63}, // Navy
- {255,220,0} // Yellow
-};
-*/
-
uint8_t colors[7][3] = {
{253,253,150}, // Yellow
{174,198,207}, // Aqua
@@ -120,9 +107,15 @@ void draw() {
SDL_RenderClear(render);
SDL_Rect board = {X_OFFSET, Y_OFFSET, 250, 500};
+ SDL_Rect queue = {W - (W / 4), Y_OFFSET, (BLOCK_SIZE * 4) + 20, BLOCK_SIZE * 5 * 3};
+ SDL_Rect hold = {30, Y_OFFSET, BLOCK_SIZE * 4, BLOCK_SIZE * 4};
SDL_SetRenderDrawColor(render, 235, 235, 235, 255);
SDL_RenderFillRect(render, &board);
+ SDL_RenderFillRect(render, &queue);
+ SDL_RenderFillRect(render, &hold);
SDL_RenderDrawRect(render, &board);
+ SDL_RenderDrawRect(render, &queue);
+ SDL_RenderDrawRect(render, &hold);
SDL_SetRenderDrawColor(render, 255, 255, 255, 255);
for (int x = 0; x < CETRIS_BOARD_X + 1; x++) {
@@ -164,9 +157,63 @@ void draw() {
SDL_RenderDrawRect(render, &b);
}
- //if ((default_matrices[game.piece_queue[g.current_index]][y]>>(3 - x))&1) {
- // mvaddstr(6 + y, (x * 2) + 36, BLOCK);
- //}
+
+ if (g.piece_held) {
+ if ((g.held.m[y]>>(3 - x))&1) {
+ b.x = 40 + ((x) * BLOCK_SIZE);
+ b.y = Y_OFFSET + ((y) * BLOCK_SIZE);
+ if (g.held.t == MINO_I) {
+ b.x -= 10;
+ b.y += 10;
+ }
+ if (g.held.t == MINO_O) b.x -= 10;
+
+ uint8_t (*color)[3] = &(colors[g.held.t]);
+ SDL_SetRenderDrawColor(render, (*color)[0], (*color)[1], (*color)[2], 255);
+
+ SDL_RenderFillRect(render, &b);
+ SDL_RenderDrawRect(render, &b);
+
+ SDL_SetRenderDrawColor(render, 240, 240, 240, 255);
+ w.y = b.y - 1;
+ w.x = b.x - 1;
+ SDL_RenderDrawRect(render, &w);
+ }
+ }
+
+ for (int i = 0; i < 5; i++) {
+ int index = (g.current_index + i);
+
+ uint8_t mino;
+ if (index <= 6) {
+ mino = g.piece_queue[index];
+ } else {
+ index = index % 7;
+ mino = g.next_queue[index];
+ }
+
+ uint8_t (*color)[3] = &(colors[mino]);
+ uint8_t res = (default_matrices[mino][y]>>(3 - x))&1;
+
+ if (res) {
+ b.x = 20 + (W - (W / 4)) + (x * BLOCK_SIZE);
+ b.y = (Y_OFFSET - 10) + (BLOCK_SIZE * i * 3) + (y * BLOCK_SIZE);
+ if (mino == MINO_I) {
+ b.x -= 10;
+ b.y += 10;
+ }
+ if (mino == MINO_O) b.x -= 10;
+ SDL_SetRenderDrawColor(render, (*color)[0], (*color)[1], (*color)[2], 255);
+
+ SDL_RenderFillRect(render, &b);
+ SDL_RenderDrawRect(render, &b);
+
+ SDL_SetRenderDrawColor(render, 240, 240, 240, 255);
+ w.y = b.y - 1;
+ w.x = b.x - 1;
+ SDL_RenderDrawRect(render, &w);
+ }
+ }
}
}
@@ -203,9 +250,11 @@ void draw() {
free(buf);
SDL_SetRenderTarget(render, NULL);
+
SDL_Point center = {W / 2, H / 2};
SDL_RenderCopyEx(render, m, NULL, NULL, 0, &center, SDL_FLIP_NONE);
+ SDL_DestroyTexture(m);
SDL_RenderPresent(render);
}
diff --git a/lib/cetris.h b/lib/cetris.h
index 152f78c..e5a7a7e 100644
--- a/lib/cetris.h
+++ b/lib/cetris.h
@@ -108,12 +108,13 @@ typedef struct {
// constant queue of all 7 possible tetrimino
uint8_t piece_queue[7];
+ uint8_t next_queue[7];
+ uint8_t current_index;
// current tetrimino
tetrimino current;
tetrimino held;
bool piece_held;
- uint8_t current_index;
// internal game tick
ctick tick;
@@ -212,18 +213,12 @@ static void set_piece(uint8_t type, tetrimino* mino) {
mino->pos.y = (type == MINO_I) ? 17 : 16;
}
-static void init_queue(cetris_game *g) {
- for (int i = 0; i < 7; i++) {
- g->piece_queue[i] = i;
- }
-}
-
static void shuffle_queue(cetris_game *g) {
for (int i = 0; i < 7; i++) {
- int rand_index = rand() % 7;
- uint8_t tmp = g->piece_queue[i];
- g->piece_queue[i] = g->piece_queue[rand_index];
- g->piece_queue[rand_index] = tmp;
+ uint8_t rand_index = rand() % 7;
+ uint8_t tmp = g->next_queue[i];
+ g->next_queue[i] = g->next_queue[rand_index];
+ g->next_queue[rand_index] = tmp;
}
}
@@ -346,13 +341,14 @@ static void next_piece(cetris_game *g) {
if (check_matrix(g, &g->current.m) <= 0) {
g->game_over = true;
}
- g->current_index++;
if (!g->game_over) {
move_current(g, DOWN);
}
-
- if (g->current_index >= 7) {
+
+ g->current_index++;
+ if (g->current_index == 7) {
+ memcpy(&g->piece_queue, &g->next_queue, sizeof(g->piece_queue));
g->current_index = 0;
shuffle_queue(g);
}
@@ -626,7 +622,12 @@ CETRIS_EXPORT void init_game(cetris_game *g) {
g->level = CETRIS_STARTING_LEVEL;
g->highest_line = CETRIS_BOARD_Y;
- init_queue(g);
+ for (int i = 0; i < 7; i++) {
+ g->next_queue[i] = i;
+ }
+
+ shuffle_queue(g);
+ memcpy(&g->piece_queue, &g->next_queue, sizeof(g->piece_queue));
shuffle_queue(g);
next_piece(g);