From: Laurent Mazet Date: Sun, 21 Jul 2024 20:01:15 +0000 (+0200) Subject: more code X-Git-Tag: v1.0~10 X-Git-Url: https://secure.softndesign.org/git/?a=commitdiff_plain;h=4b2f1730d1d9e19ee78f3a824f0170454388a1fd;p=tetris.git more code --- diff --git a/constant.c b/constant.c index eb0f8ee..0fcb035 100644 --- a/constant.c +++ b/constant.c @@ -5,4 +5,28 @@ #include "constant.h" +#define _nb_blocks_std 6 + +block_t _blocks_std[_nb_blocks_std] = { + {3, 2, 2, " .... "}, + {3, 2, 3, ".. .."}, + {2, 3, 4, ". . .."}, + {2, 3, 5, " . ..."}, + {2, 2, 6, "...."}, + {1, 4, 7, "...."} +}; + +block_t *getblocks (char *name, int *nb) +{ + block_t *pt = NULL; + + if (strcmp (name, "std") == 0) { + pt = _blocks_std; + *nb = _nb_blocks_std; + } + + return pt; +} + + /* vim: set ts=4 sw=4 et: */ diff --git a/constant.h b/constant.h index e270e12..b31eb6f 100644 --- a/constant.h +++ b/constant.h @@ -3,6 +3,8 @@ #include "type.h" +block_t *getblocks (char *name, int *nb); + #endif /* __CONSTANT_H__ */ /* vim: set ts=4 sw=4 et: */ diff --git a/function.c b/function.c index d12ca6f..154833d 100644 --- a/function.c +++ b/function.c @@ -29,14 +29,12 @@ board_t *initboard (int width, int height) board->tab = (char *) calloc (1, width * height + 1); CHECKALLOC (board->tab); memset (board->tab, ' ', width * height); - board->current = (char *) calloc (1, width * height + 1); - CHECKALLOC (board->current); - memset (board->current, ' ', width * height); board->scale = 0; board->xsize = board->width = width; board->ysize = board->height = height; board->xoffset = 0; board->yoffset = 0; + board->current = -1; board->next = -1; return board; } @@ -45,12 +43,9 @@ board_t *copyboard (board_t *board) { board_t *newboard = initboard (board->width, board->height); char *tab = newboard->tab; - char *current = newboard->current; memcpy (newboard, board, sizeof (board_t)); newboard->tab = tab; - newboard->current = current; memcpy (newboard->tab, board->tab, board->width * board->height + 1); - memcpy (newboard->current, board->current, board->width * board->height + 1); return newboard; } @@ -82,7 +77,6 @@ void freeboard (board_t *board) { if (board) { free (board->tab); - free (board->current); } free (board); } @@ -102,7 +96,7 @@ int _makecomments (char *buffer, board_t *board) char *saveboard (board_t *board) { - int size = 3 * (8 + 3) + 8 + board->width * board->height + 1; + int size = 4 * (8 + 3) + 8 + board->width * board->height + 1; VERBOSE (INFO, size += board->height * (8 + board->width)); char *buffer = (char *) calloc (size, 1); @@ -111,7 +105,7 @@ char *saveboard (board_t *board) int l = sprintf (buffer, "width: %d\n", board->width); l += sprintf (buffer + l, "height: %d\n", board->height); l += sprintf (buffer + l, "tab: \"%s\"\n", board->tab); - l += sprintf (buffer + l, "current: \"%s\"\n", board->current); + l += sprintf (buffer + l, "current: \"%d\"\n", board->current); l += sprintf (buffer + l, "next: %d\n", board->next); VERBOSE (INFO, _makecomments (buffer + l, board)); @@ -179,7 +173,7 @@ board_t *loadboard (char *str) int width = 0; int height = 0; char *tab = NULL; - char *current = NULL; + int current = -1; int next = -1; char *saveptr1, *saveptr2; @@ -201,7 +195,7 @@ board_t *loadboard (char *str) } else if (strcmp (keyword, "tab") == 0) { tab = atos (value); } else if (strcmp (keyword, "current") == 0) { - current = atos (value); + current = atoi (value); } else if (strcmp (keyword, "next") == 0) { next = atoi (value); } else if (strcmp (keyword, "rem") == 0) { @@ -214,25 +208,101 @@ board_t *loadboard (char *str) } board_t *board = NULL; - if ((tab) && (strlen (tab) == (size_t)(width * height)) && - (current) && (strlen (current) == (size_t)(width * height))) { + if ((tab) && (strlen (tab) == (size_t)(width * height))) { board = initboard (width, height); memcpy (board->tab, tab, width * height); - memcpy (board->current, current, width * height); + board->current = current; board->next = next; } return board; } -char *getcell (board_t *board, int x, int y) +block_t *initblock (int width, int height) { - return board->tab + x + board->width * y; + block_t *block = (block_t *) malloc (sizeof (block_t)); + CHECKALLOC (block); + block->tab = (char *) calloc (1, width * height + 1); + CHECKALLOC (block->tab); + memset (block->tab, ' ', width * height); + block->width = width; + block->height = height; + return block; } -char getvalue (board_t *board, int x, int y) +block_t *changeblock (block_t *dest, block_t *src) { - return (x >= 0) && (x < board->width) && (y >= 0) && (y < board->height) ? *getcell (board, x, y) : 0; + block_t *ret = NULL; + if (dest && src) { + free (dest->tab); + memcpy (dest, src, sizeof (block_t)); + dest->tab = strdup (src->tab); + CHECKALLOC (dest->tab); + ret = dest; + } + return ret; +} + +block_t *copyblock (block_t *block) +{ + block_t *newblock = initblock (block->width, block->height); + char *tab = newblock->tab; + memcpy (newblock, block, sizeof (block_t)); + newblock->tab = tab; + memcpy (newblock->tab, block->tab, block->width * block->height + 1); + return newblock; +} + +void freeblock (block_t *block) +{ + if (block) { + free (block->tab); + } + free (block); +} + +block_t *rotateelement (block_t *element, int rot) +{ + int i, j; + + rot = (rot > 0) ? rot % 4 : ((1 - rot / 4) * 4 + rot) % 4; + + block_t *newelement = NULL; + + switch (rot) { + case 0: + newelement = copyblock (element); + break; + case 1: + newelement = initblock (element->height, element->width); + for (i = 0; i < element->width; i++) { + for (j = 0; j < element->height; j++) { + *getcell (newelement, element->height - 1 - j, i) = *getcell (element, i, j); + } + } + break; + case 2: + newelement = initblock (element->width, element->height); + for (i = 0; i < element->width; i++) { + for (j = 0; j < element->height; j++) { + *getcell (newelement, element->width - 1 - i, element->height - 1 - j) = *getcell (element, i, j); + } + } + break; + case 3: + newelement = initblock (element->height, element->width); + for (i = 0; i < element->width; i++) { + for (j = 0; j < element->height; j++) { + *getcell (newelement, j, element->width - 1 - i) = *getcell (element, i, j); + } + } + break; + } + + changeblock (element, newelement); + freeblock (newelement); + + return element; } /* vim: set ts=4 sw=4 et: */ diff --git a/function.h b/function.h index b0d4842..883808e 100644 --- a/function.h +++ b/function.h @@ -37,9 +37,19 @@ char *readdata (char *filename); board_t *loadboard (char *str); -char *getcell (board_t *board, int x, int y); +#define getcell(b, x, y) ({ __typeof__ (b) _b = (b); int _x = (x), _y = (y);_b->tab + _x + _b->width * _y; }) -char getvalue (board_t *board, int x, int y); +#define getvalue(b, x, y) ({ __typeof__ (b) _b = (b); int _x = (x), _y = (y); (_x >= 0) && (_x < _b->width) && (_y >= 0) && (_y < _b->height) ? *getcell (_b, _x, _y) : 0; }) + +block_t *initblock (int xsize, int ysize); + +block_t *changeblock (block_t *dest, block_t *src); + +block_t *copyblock (block_t *block); + +void freeblock (block_t *block); + +block_t *rotateelement (block_t *element, int rot); #endif /* __FUNCTION_H__ */ diff --git a/tetris.c b/tetris.c index 22c6b24..da41f71 100644 --- a/tetris.c +++ b/tetris.c @@ -130,6 +130,12 @@ int main (int argc, char *argv[]) } setscale (board, scale); + /* get blocks */ + int nb_blocks = 0; + block_t *block_t = getblocks ("std", &nb_blocks); + int current = rand () % nb_blocks; + int next = rand () % nb_blocks; + /* init curses window */ initscr (); noecho (); @@ -160,6 +166,8 @@ int main (int argc, char *argv[]) /* event loop */ int mode = 0; + int speed = 255; + int stop = 0; while (!stop) { @@ -168,9 +176,11 @@ int main (int argc, char *argv[]) char msg[128] = {0}; switch (mode) { case 0: + halfdelay (0); sprintf (msg, "Get ready player One"); break; case 1: + halfdelay (speed); break; } msgwindow (msg, xmsg, ymsg, lmsg); diff --git a/type.h b/type.h index 3c66510..3b44784 100644 --- a/type.h +++ b/type.h @@ -5,15 +5,22 @@ typedef struct { int width; int height; char *tab; - char *current; int scale; int xsize; int ysize; int xoffset; int yoffset; + int current; int next; } board_t; +typedef struct { + int width; + int height; + int color; + char *tab; +} block_t; + #endif /* __TYPE_H__ */ /* vim: set ts=4 sw=4 et: */