CHDK_DE Vorschauversion  Trunk Rev. 5272
 Alle Datenstrukturen Dateien Funktionen Variablen Typdefinitionen Aufzählungen Aufzählungswerte Makrodefinitionen
gui_tetris.c-Dateireferenz
#include "camera_info.h"
#include "stdlib.h"
#include "keyboard.h"
#include "clock.h"
#include "backlight.h"
#include "lang.h"
#include "gui.h"
#include "gui_draw.h"
#include "gui_lang.h"
#include "gui_batt.h"
#include "gui_mbox.h"
#include "modes.h"
#include "module_def.h"
#include "simple_game.c"
+ Include-Abhängigkeitsdiagramm für gui_tetris.c:

gehe zum Quellcode dieser Datei

Datenstrukturen

struct  StcTetramino
 
struct  StcGame
 

Makrodefinitionen

#define BOARD_WIDTH   (10)
 
#define BOARD_HEIGHT   (22)
 
#define INI_DELAY_FALL   (540)
 
#define SCORE_1_FILLED_ROW   (40)
 
#define SCORE_2_FILLED_ROW   (100)
 
#define SCORE_3_FILLED_ROW   (300)
 
#define SCORE_4_FILLED_ROW   (1200)
 
#define FILLED_ROWS_FOR_LEVEL_UP   (10)
 
#define DELAY_FACTOR_FOR_LEVEL_UP   (0.9)
 
#define GAME_ERROR_NONE   (0) /* Everything is OK, oh wonders! */
 
#define GAME_ERROR_USER_QUITS   (1) /* The user quits (bored?), our fail */
 
#define GAME_ERROR_NO_MEMORY   (-1) /* Not enough memory */
 
#define GAME_ERROR_NO_VIDEO   (-2) /* Video system was not initialized */
 
#define GAME_ERROR_NO_IMAGES   (-3) /* Problem loading the image files */
 
#define GAME_ERROR_ASSERT   (-100) /* Something went very very wrong... */
 
#define EVENT_NONE   (0)
 
#define EVENT_MOVE_DOWN   (1 << 1)
 
#define EVENT_MOVE_LEFT   (1 << 2)
 
#define EVENT_MOVE_RIGHT   (1 << 3)
 
#define EVENT_ROTATE_CW   (1 << 4) /* rotate clockwise */
 
#define EVENT_ROTATE_CCW   (1 << 5) /* rotate counter-clockwise */
 
#define EVENT_DROP   (1 << 6)
 
#define EVENT_PAUSE   (1 << 7)
 
#define EVENT_RESTART   (1 << 8)
 
#define EVENT_SHOW_NEXT   (1 << 9) /* toggle show next tetromino */
 
#define TETROMINO_I   (0)
 
#define TETROMINO_O   (1)
 
#define TETROMINO_T   (2)
 
#define TETROMINO_S   (3)
 
#define TETROMINO_Z   (4)
 
#define TETROMINO_J   (5)
 
#define TETROMINO_L   (6)
 
#define TETRIS_COLOR_CYAN   COLOR_CYAN
 
#define TETRIS_COLOR_RED   COLOR_RED
 
#define TETRIS_COLOR_BLUE   COLOR_BLUE
 
#define TETRIS_COLOR_ORANGE   COLOR_WHITE
 
#define TETRIS_COLOR_GREEN   COLOR_GREEN
 
#define TETRIS_COLOR_YELLOW   COLOR_YELLOW
 
#define TETRIS_COLOR_PURPLE   COLOR_MAGENTA
 
#define EMPTY_CELL   (-1)
 
#define TETRIS_COLOR_BG   COLOR_GREY_DK
 
#define TETRIS_COLOR_BOARD   COLOR_GREY
 
#define TETRIS_COLOR_TEXT   COLOR_WHITE
 
#define PREVIEW_X   (tile_size*15)
 
#define PREVIEW_Y   (tile_size)
 
#define BOARD_X   (tile_size)
 
#define BOARD_Y   (tile_size)
 
#define TILE_SIZE   (tile_size)
 

Typdefinitionen

typedef struct StcTetramino StcTetramino
 
typedef struct StcPlatform StcPlatform
 
typedef struct StcGame StcGame
 

Funktionen

void gui_game_menu_kbd_process ()
 
int gui_tetris_kbd_process ()
 
void gui_tetris_draw ()
 
StcGamecreateGame ()
 
void deleteGame (StcGame *pGame)
 
int gameInit (StcGame *gameInstance)
 
void gameEnd (StcGame *gameInstance)
 
void gameUpdate (StcGame *gameInstance)
 
long mkdir_if_not_exist (const char *dirname)
 
int platformInit (StcGame *gameInstance)
 
void platformEnd (StcGame *gameInstance)
 
void platformReadInput (StcGame *gameInstance)
 
void platformRenderGame (StcGame *gameInstance)
 
long platformGetSystemTime ()
 
static void setMatrixCells (int *matrix, int width, int height, int value)
 
static void setTetramino (int indexTetramino, StcTetramino *tetramino)
 
static void startGame (StcGame *game)
 
void rotateTetramino (StcGame *game, int clockwise)
 
static int checkCollision (StcGame *game, int dx, int dy)
 
static void onFilledRows (StcGame *game, int filledRows)
 
static void moveTetramino (StcGame *game, int x, int y)
 
static void dropTetramino (StcGame *game)
 
void gui_tetris_init ()
 
int basic_module_init ()
 

Variablen

gui_handler GUI_MODE_TETRIS
 
StcGamegame
 
int tile_size
 
int tmp [BOARD_WIDTH][BOARD_HEIGHT]
 
int tmp2 [BOARD_WIDTH][BOARD_HEIGHT]
 
int prevNextBlockType = -1
 
ModuleInfo _module_info
 

Makro-Dokumentation

#define BOARD_HEIGHT   (22)

Definiert in Zeile 24 der Datei gui_tetris.c.

#define BOARD_WIDTH   (10)

Definiert in Zeile 23 der Datei gui_tetris.c.

#define BOARD_X   (tile_size)

Definiert in Zeile 137 der Datei gui_tetris.c.

#define BOARD_Y   (tile_size)

Definiert in Zeile 138 der Datei gui_tetris.c.

#define DELAY_FACTOR_FOR_LEVEL_UP   (0.9)

Definiert in Zeile 35 der Datei gui_tetris.c.

#define EMPTY_CELL   (-1)

Definiert in Zeile 70 der Datei gui_tetris.c.

#define EVENT_DROP   (1 << 6)

Definiert in Zeile 50 der Datei gui_tetris.c.

#define EVENT_MOVE_DOWN   (1 << 1)

Definiert in Zeile 45 der Datei gui_tetris.c.

#define EVENT_MOVE_LEFT   (1 << 2)

Definiert in Zeile 46 der Datei gui_tetris.c.

#define EVENT_MOVE_RIGHT   (1 << 3)

Definiert in Zeile 47 der Datei gui_tetris.c.

#define EVENT_NONE   (0)

Definiert in Zeile 44 der Datei gui_tetris.c.

#define EVENT_PAUSE   (1 << 7)

Definiert in Zeile 51 der Datei gui_tetris.c.

#define EVENT_RESTART   (1 << 8)

Definiert in Zeile 52 der Datei gui_tetris.c.

#define EVENT_ROTATE_CCW   (1 << 5) /* rotate counter-clockwise */

Definiert in Zeile 49 der Datei gui_tetris.c.

#define EVENT_ROTATE_CW   (1 << 4) /* rotate clockwise */

Definiert in Zeile 48 der Datei gui_tetris.c.

#define EVENT_SHOW_NEXT   (1 << 9) /* toggle show next tetromino */

Definiert in Zeile 53 der Datei gui_tetris.c.

#define FILLED_ROWS_FOR_LEVEL_UP   (10)

Definiert in Zeile 33 der Datei gui_tetris.c.

#define GAME_ERROR_ASSERT   (-100) /* Something went very very wrong... */

Definiert in Zeile 42 der Datei gui_tetris.c.

#define GAME_ERROR_NO_IMAGES   (-3) /* Problem loading the image files */

Definiert in Zeile 41 der Datei gui_tetris.c.

#define GAME_ERROR_NO_MEMORY   (-1) /* Not enough memory */

Definiert in Zeile 39 der Datei gui_tetris.c.

#define GAME_ERROR_NO_VIDEO   (-2) /* Video system was not initialized */

Definiert in Zeile 40 der Datei gui_tetris.c.

#define GAME_ERROR_NONE   (0) /* Everything is OK, oh wonders! */

Definiert in Zeile 37 der Datei gui_tetris.c.

#define GAME_ERROR_USER_QUITS   (1) /* The user quits (bored?), our fail */

Definiert in Zeile 38 der Datei gui_tetris.c.

#define INI_DELAY_FALL   (540)

Definiert in Zeile 26 der Datei gui_tetris.c.

#define PREVIEW_X   (tile_size*15)

Definiert in Zeile 135 der Datei gui_tetris.c.

#define PREVIEW_Y   (tile_size)

Definiert in Zeile 136 der Datei gui_tetris.c.

#define SCORE_1_FILLED_ROW   (40)

Definiert in Zeile 28 der Datei gui_tetris.c.

#define SCORE_2_FILLED_ROW   (100)

Definiert in Zeile 29 der Datei gui_tetris.c.

#define SCORE_3_FILLED_ROW   (300)

Definiert in Zeile 30 der Datei gui_tetris.c.

#define SCORE_4_FILLED_ROW   (1200)

Definiert in Zeile 31 der Datei gui_tetris.c.

#define TETRIS_COLOR_BG   COLOR_GREY_DK

Definiert in Zeile 72 der Datei gui_tetris.c.

#define TETRIS_COLOR_BLUE   COLOR_BLUE

Definiert in Zeile 65 der Datei gui_tetris.c.

#define TETRIS_COLOR_BOARD   COLOR_GREY

Definiert in Zeile 73 der Datei gui_tetris.c.

#define TETRIS_COLOR_CYAN   COLOR_CYAN

Definiert in Zeile 63 der Datei gui_tetris.c.

#define TETRIS_COLOR_GREEN   COLOR_GREEN

Definiert in Zeile 67 der Datei gui_tetris.c.

#define TETRIS_COLOR_ORANGE   COLOR_WHITE

Definiert in Zeile 66 der Datei gui_tetris.c.

#define TETRIS_COLOR_PURPLE   COLOR_MAGENTA

Definiert in Zeile 69 der Datei gui_tetris.c.

#define TETRIS_COLOR_RED   COLOR_RED

Definiert in Zeile 64 der Datei gui_tetris.c.

#define TETRIS_COLOR_TEXT   COLOR_WHITE

Definiert in Zeile 74 der Datei gui_tetris.c.

#define TETRIS_COLOR_YELLOW   COLOR_YELLOW

Definiert in Zeile 68 der Datei gui_tetris.c.

#define TETROMINO_I   (0)

Definiert in Zeile 55 der Datei gui_tetris.c.

#define TETROMINO_J   (5)

Definiert in Zeile 60 der Datei gui_tetris.c.

#define TETROMINO_L   (6)

Definiert in Zeile 61 der Datei gui_tetris.c.

#define TETROMINO_O   (1)

Definiert in Zeile 56 der Datei gui_tetris.c.

#define TETROMINO_S   (3)

Definiert in Zeile 58 der Datei gui_tetris.c.

#define TETROMINO_T   (2)

Definiert in Zeile 57 der Datei gui_tetris.c.

#define TETROMINO_Z   (4)

Definiert in Zeile 59 der Datei gui_tetris.c.

#define TILE_SIZE   (tile_size)

Definiert in Zeile 139 der Datei gui_tetris.c.

Dokumentation der benutzerdefinierten Typen

typedef struct StcGame StcGame
typedef struct StcPlatform StcPlatform

Definiert in Zeile 84 der Datei gui_tetris.c.

typedef struct StcTetramino StcTetramino

Dokumentation der Funktionen

int basic_module_init ( )

Definiert in Zeile 786 der Datei gui_tetris.c.

787 {
789  gui_tetris_init();
790  return 1;
791 }
static int checkCollision ( StcGame game,
int  dx,
int  dy 
)
static

Definiert in Zeile 484 der Datei gui_tetris.c.

485 {
486  int newx, newy, i, j;
487 
488  newx = game->fallingBlock.x + dx;
489  newy = game->fallingBlock.y + dy;
490 
491  for (i = 0; i < game->fallingBlock.size; ++i)
492  {
493  for (j = 0; j < game->fallingBlock.size; ++j)
494  {
495  if (game->fallingBlock.cells[i][j] != EMPTY_CELL)
496  {
497  /* Check the tetramino would be inside the left, right and bottom borders */
498  if ((newx + i < 0) || (newx + i >= BOARD_WIDTH) || (newy + j >= BOARD_HEIGHT))
499  {
500  return 1;
501  }
502  /* Check the tetromino won't collide with existing cells in the map */
503  if (game->map[newx + i][newy + j] != EMPTY_CELL)
504  {
505  return 1;
506  }
507  }
508  }
509  }
510  return 0;
511 }
StcGame * createGame ( )

Definiert in Zeile 383 der Datei gui_tetris.c.

384 {
385  /* Allocate space for our game object */
386  StcGame *game = (StcGame *) malloc(sizeof(StcGame));
387  return game;
388 }
void deleteGame ( StcGame pGame)

Definiert in Zeile 412 der Datei gui_tetris.c.

413 {
414  free(game);
415 }
static void dropTetramino ( StcGame game)
static

Definiert in Zeile 649 der Datei gui_tetris.c.

650 {
651  int y;
652  y = 1;
653  /* Calculate number of cells to drop */
654  while (!checkCollision(game, 0, y))
655  {
656  y++;
657  }
658  moveTetramino(game, 0, y - 1);
659 }
void gameEnd ( StcGame gameInstance)

Definiert in Zeile 406 der Datei gui_tetris.c.

407 {
408  /* Free platform resources */
409  platformEnd(game);
410 }
int gameInit ( StcGame gameInstance)

Definiert in Zeile 393 der Datei gui_tetris.c.

394 {
395  int errorCode;
396 
397  errorCode = platformInit(game);
398  if (errorCode == GAME_ERROR_NONE)
399  {
400  startGame(game);
401  return GAME_ERROR_NONE;
402  }
403  return errorCode;
404 };
void gameUpdate ( StcGame gameInstance)

Definiert in Zeile 664 der Datei gui_tetris.c.

665 {
666  long sysTime;
667  /* Read user input */
669 
670  /* Update game state */
671  if (game->isOver)
672  {
673 
674  if (game->stats.score > game->stats.high)
675  {
677  FILE * f;
678  long buf;
679  buf = game->stats.score;
680 
681  mkdir_if_not_exist("A/CHDK/GAMES");
682  f = fopen("A/CHDK/GAMES/TETRIS.SCO", "wb");
683  fwrite(&buf, 1, sizeof(buf), f);
684  fclose(f);
685  }
686 
687  //if (game->events & EVENT_RESTART) {
688  if (game->events & EVENT_PAUSE)
689  {
690 
691  //TurnOnBackLight();
692 
693  game->isOver = 0;
694  startGame(game);
695 
696  }
697  }
698  else
699  {
700  sysTime = platformGetSystemTime();
701 
702  /* Always handle pause event */
703  if (game->events & EVENT_PAUSE)
704  {
705  game->isPaused = !game->isPaused;
707  }
708 
709  /* Check if the game is paused */
710  if (game->isPaused)
711  {
712  /* We achieve the effect of pausing the game
713  * adding the last frame duration to lastFallTime */
714  game->lastFallTime += (sysTime - game->systemTime);
715  }
716  else
717  {
718  if (game->events != EVENT_NONE)
719  {
720  if (game->events & EVENT_SHOW_NEXT)
721  {
723  }
724  if (game->events & EVENT_DROP)
725  {
727  }
728  if (game->events & EVENT_ROTATE_CW)
729  {
730  rotateTetramino(game, 1);
731  }
733  {
734  moveTetramino(game, 1, 0);
735  }
736  else if (game->events & EVENT_MOVE_LEFT)
737  {
738  moveTetramino(game, -1, 0);
739  }
740  if (game->events & EVENT_MOVE_DOWN)
741  {
742  moveTetramino(game, 0, 1);
743  }
745  }
746  /* Check if it's time to move downwards the falling tetromino */
747  if (sysTime - game->lastFallTime >= game->delay)
748  {
749  moveTetramino(game, 0, 1);
750  game->lastFallTime = sysTime;
751  }
752  }
753  game->systemTime = sysTime;
754  }
755  /* Draw game state */
757 }
void gui_game_menu_kbd_process ( )

Definiert in Zeile 7 der Datei simple_game.c.

8 {
9  running = 0;
11 }
void gui_tetris_draw ( )

Definiert in Zeile 793 der Datei gui_tetris.c.

794 {
795  gameUpdate(game);
796 }
void gui_tetris_init ( )

Definiert in Zeile 759 der Datei gui_tetris.c.

760 {
762 
765  game = createGame();
766  gameInit(game);
767 
768  long buf;
769  FILE *f;
770 
771  f = fopen("A/CHDK/GAMES/TETRIS.SCO", "rb");
772  if (!f)
773  {
774  game->stats.high = 0;
775  }
776  else
777  {
778  fread(&buf, 1, sizeof(buf), f);
779  game->stats.high = buf;
780  }
781 
782  fclose(f);
783  startGame(game);
784 }
int gui_tetris_kbd_process ( )

Definiert in Zeile 798 der Datei gui_tetris.c.

799 {
800  switch (kbd_get_autoclicked_key())
801  {
802  case KEY_UP:
803  if ((game->isPaused) || (game->isOver))
804  {
805  TurnOnBackLight();
806  }
807  else
808  {
810  }
811  game->events |= EVENT_PAUSE;
812  break;
813  case KEY_LEFT:
815  break;
816  case KEY_RIGHT:
818  break;
819  case KEY_DOWN:
821  break;
822  //case KEY_SET:
823  //game->events |= EVENT_RESTART;
824  //break;
825  case KEY_DISPLAY:
826  case KEY_ERASE:
827  case KEY_SET:
829  break;
830  default:
831  break;
832  }
833  return 0;
834 }
long mkdir_if_not_exist ( const char *  dirname)

Definiert in Zeile 121 der Datei gui_tetris.c.

122 {
123  // Check if directory exists and create it if it does not.
124  if (stat(dirname,0) != 0) return mkdir(dirname);
125  return 0; // Success
126 }
static void moveTetramino ( StcGame game,
int  x,
int  y 
)
static

Definiert in Zeile 552 der Datei gui_tetris.c.

553 {
554  int i, j, hasFullRow, numFilledRows;
555 
556  /* Check if the move would create a collision */
557  if (checkCollision(game, x, y))
558  {
559  /* In case of collision check if move was downwards (y == 1) */
560  if (y == 1)
561  {
562  /* Check if collision occur when the falling
563  * tetromino is in the 1st or 2nd row */
564  if (game->fallingBlock.y <= 1)
565  {
566  game->isOver = 1; /* if this happens the game is over */
567  }
568  else
569  {
570  /* The falling tetromino has reached the bottom,
571  * so we copy their cells to the board map */
572  for (i = 0; i < game->fallingBlock.size; ++i)
573  {
574  for (j = 0; j < game->fallingBlock.size; ++j)
575  {
576  if (game->fallingBlock.cells[i][j] != EMPTY_CELL)
577  {
578  game->map[game->fallingBlock.x + i][game->fallingBlock.y + j] = game->fallingBlock.cells[i][j];
579  }
580  }
581  }
582 
583  /* Check if the landing tetromino has created full rows */
584  numFilledRows = 0;
585  for (j = 1; j < BOARD_HEIGHT; ++j)
586  {
587  hasFullRow = 1;
588  for (i = 0; i < BOARD_WIDTH; ++i)
589  {
590  if (game->map[i][j] == EMPTY_CELL)
591  {
592  hasFullRow = 0;
593  break;
594  }
595  }
596  /* If we found a full row we need to remove that row from the map
597  * we do that by just moving all the above rows one row below */
598  if (hasFullRow)
599  {
600  for (x = 0; x < BOARD_WIDTH; ++x)
601  {
602  for (y = j; y > 0; --y)
603  {
604  game->map[x][y] = game->map[x][y - 1];
605  }
606  }
607  numFilledRows++; /* increase filled row counter */
608  }
609  }
610 
611  /* Update game statistics */
612  if (numFilledRows)
613  {
614  onFilledRows(game, numFilledRows);
615  }
616  game->stats.totalPieces++;
617  game->stats.pieces[game->fallingBlock.type]++;
618 
619  /* Use preview tetromino as falling tetromino.
620  * Copy preview tetramino for falling tetramino */
621  for (i = 0; i < 4; ++i)
622  {
623  for (j = 0; j < 4; ++j)
624  {
625  game->fallingBlock.cells[i][j] = game->nextBlock.cells[i][j];
626  }
627  }
628  game->fallingBlock.size = game->nextBlock.size;
629  game->fallingBlock.type = game->nextBlock.type;
630 
631  /* Reset position */
632  game->fallingBlock.y = 0;
633  game->fallingBlock.x = (BOARD_WIDTH - game->fallingBlock.size) / 2;
634 
635  /* Create next preview tetromino */
636  setTetramino(rand() % 7, &game->nextBlock);
637  }
638  }
639  }
640  else
641  {
642  /* There are no collisions, just move the tetramino */
643  game->fallingBlock.x += x;
644  game->fallingBlock.y += y;
645  }
646 }
static void onFilledRows ( StcGame game,
int  filledRows 
)
static

Definiert in Zeile 514 der Datei gui_tetris.c.

515 {
516  /* Update total number of filled rows */
517  game->stats.lines += filledRows;
518 
519  /* Increase score accordingly to the number of filled rows */
520  switch (filledRows)
521  {
522  case 1:
523  game->stats.score += (SCORE_1_FILLED_ROW * (game->stats.level));
524  break;
525  case 2:
526  game->stats.score += (SCORE_2_FILLED_ROW * (game->stats.level));
527  break;
528  case 3:
529  game->stats.score += (SCORE_3_FILLED_ROW * (game->stats.level));
530  break;
531  case 4:
532  game->stats.score += (SCORE_4_FILLED_ROW * (game->stats.level));
533  break;
534  default:
535  game->errorCode = GAME_ERROR_ASSERT; /* This can't happen */
536  }
537  /* Check if we need to update level */
538  if (game->stats.lines >= FILLED_ROWS_FOR_LEVEL_UP * (game->stats.level))
539  {
540  game->stats.level++;
541 
542  /* Increase speed for falling tetrominoes */
544  }
545 }
void platformEnd ( StcGame gameInstance)

Definiert in Zeile 129 der Datei gui_tetris.c.

129 {}
long platformGetSystemTime ( )

Definiert in Zeile 267 der Datei gui_tetris.c.

267 { return get_tick_count(); }
int platformInit ( StcGame gameInstance)

Definiert in Zeile 128 der Datei gui_tetris.c.

128 { return GAME_ERROR_NONE; }
void platformReadInput ( StcGame gameInstance)

Definiert in Zeile 132 der Datei gui_tetris.c.

132 {}
void platformRenderGame ( StcGame gameInstance)

Definiert in Zeile 145 der Datei gui_tetris.c.

146 {
147  int i, j;
148 
149  for (i = 0; i < BOARD_WIDTH; ++i)
150  {
151  for (j = 0; j < BOARD_HEIGHT; ++j)
152  {
153  tmp[i][j] = EMPTY_CELL;
154  tmp2[i][j] = EMPTY_CELL;
155  }
156  }
157 
158  /* Draw preview block */
160  {
162  for (i = 0; i < 4; ++i)
163  {
164  for (j = 0; j < 4; ++j)
165  {
166  if (game->nextBlock.cells[i][j] != EMPTY_CELL)
167  {
169  PREVIEW_Y + (TILE_SIZE * j),
171  PREVIEW_Y + (TILE_SIZE * j) + TILE_SIZE - 1,
173  }
174  else
175  {
177  PREVIEW_Y + (TILE_SIZE * j),
179  PREVIEW_Y + (TILE_SIZE * j) + TILE_SIZE - 1,
181  }
182  }
183  }
184  }
185 
186  /* Draw the cells in the board */
187  for (i = 0; i < BOARD_WIDTH; ++i)
188  {
189  for (j = 0; j < BOARD_HEIGHT; ++j)
190  {
191  if (game->map[i][j] != EMPTY_CELL)
192  {
193  tmp2[i][j] = game->map[i][j];
194  }
195  }
196  }
197  /* Draw falling tetromino */
198  for (i = 0; i < 4; ++i)
199  {
200  for (j = 0; j < 4; ++j)
201  {
202  if (game->fallingBlock.cells[i][j] != EMPTY_CELL)
203  {
205  }
206  }
207  }
208 
209  for (i = 0; i < BOARD_WIDTH; ++i)
210  {
211  for (j = 0; j < BOARD_HEIGHT; ++j)
212  {
213  if (tmp[i][j] != EMPTY_CELL)
214  {
216  BOARD_Y + (TILE_SIZE * j),
218  BOARD_Y + (TILE_SIZE * j) + TILE_SIZE - 1,
219  MAKE_COLOR(tmp[i][j], tmp[i][j]), RECT_BORDER0 | DRAW_FILLED);
220  }
221  else if (tmp2[i][j] != EMPTY_CELL)
222  {
224  BOARD_Y + (TILE_SIZE * j),
226  BOARD_Y + (TILE_SIZE * j) + TILE_SIZE - 1,
227  MAKE_COLOR(tmp2[i][j], tmp2[i][j]), RECT_BORDER0 | DRAW_FILLED);
228  }
229  else
230  {
232  BOARD_Y + (TILE_SIZE * j),
234  BOARD_Y + (TILE_SIZE * j) + TILE_SIZE - 1,
236  }
237  }
238  }
239 
240  /* output game info */
241  int tx = camera_screen.disp_right - 22 * FONT_WIDTH;
243  int yo = FONT_HEIGHT;
244  char str_buf[100];
245  static struct tm *ttm;
246  sprintf(str_buf, "High: %5d", game->stats.high);
247  draw_string(tx, camera_screen.height - yo * 11, str_buf, cl);
248  sprintf(str_buf, "Points: %5d", game->stats.score);
249  draw_string(tx, camera_screen.height - yo * 10, str_buf, cl);
250  sprintf(str_buf, "Lines: %5d", game->stats.lines);
251  draw_string(tx, camera_screen.height - yo * 9, str_buf, cl);
252  sprintf(str_buf, "Level: %5d", game->stats.level);
253  draw_string(tx, camera_screen.height - yo * 8, str_buf, cl);
254  sprintf(str_buf, "UP -> Pause");
255  draw_string(tx, camera_screen.height - yo * 6, str_buf, cl);
256  sprintf(str_buf, "SET -> Rotate");
257  draw_string(tx, camera_screen.height - yo * 5, str_buf, cl);
258 
259  ttm = get_localtime();
260  sprintf(str_buf, "Time: %2u:%02u", ttm->tm_hour, ttm->tm_min);
261  draw_string(tx, camera_screen.height - yo * 3, str_buf, cl);
262  sprintf(str_buf, "Batt: %3d%%", get_batt_perc());
263  draw_string(tx, camera_screen.height - yo * 2, str_buf, cl);
264 }
void rotateTetramino ( StcGame game,
int  clockwise 
)

Definiert in Zeile 421 der Datei gui_tetris.c.

422 {
423  int i, j;
424  int rotated[4][4]; /* temporary array to hold rotated cells */
425 
426  /* If TETRAMINO_O is falling return immediately */
427  if (game->fallingBlock.type == TETROMINO_O)
428  {
429  return; /* rotation doesn't require any changes */
430  }
431 
432  /* Initialize rotated cells to blank */
433  setMatrixCells(&rotated[0][0], 4, 4, EMPTY_CELL);
434 
435  /* Copy rotated cells to the temporary array */
436  for (i = 0; i < game->fallingBlock.size; ++i)
437  {
438  for (j = 0; j < game->fallingBlock.size; ++j)
439  {
440  if (clockwise)
441  {
442  rotated[game->fallingBlock.size - j - 1][i] = game->fallingBlock.cells[i][j];
443  }
444  else
445  {
446  rotated[j][game->fallingBlock.size - i - 1] = game->fallingBlock.cells[i][j];
447  }
448  }
449  }
450  /* Check collision of the temporary array */
451  for (i = 0; i < game->fallingBlock.size; ++i)
452  {
453  for (j = 0; j < game->fallingBlock.size; ++j)
454  {
455  if (rotated[i][j] != EMPTY_CELL)
456  {
457  /* Check collision with left, right or bottom borders of the map */
458  if ((game->fallingBlock.x + i < 0) || (game->fallingBlock.x + i >= BOARD_WIDTH) || (game->fallingBlock.y + j >= BOARD_HEIGHT))
459  {
460  return; /* there was collision therefore return */
461  }
462  /* Check collision with existing cells in the map */
463  if (game->map[i + game->fallingBlock.x][j + game->fallingBlock.y] != EMPTY_CELL)
464  {
465  return; /* there was collision therefore return */
466  }
467  }
468  }
469  }
470  /* There are no collisions, replace tetramino cells with rotated cells */
471  for (i = 0; i < 4; ++i)
472  {
473  for (j = 0; j < 4; ++j)
474  {
475  game->fallingBlock.cells[i][j] = rotated[i][j];
476  }
477  }
478 }
static void setMatrixCells ( int *  matrix,
int  width,
int  height,
int  value 
)
static

Definiert in Zeile 270 der Datei gui_tetris.c.

271 {
272  int i, j;
273  for (i = 0; i < width; ++i)
274  {
275  for (j = 0; j < height; ++j)
276  {
277  *(matrix + i + (j * width)) = value;
278  }
279  }
280 }
static void setTetramino ( int  indexTetramino,
StcTetramino tetramino 
)
static

Definiert in Zeile 283 der Datei gui_tetris.c.

284 {
285 
286  /* Initialize tetromino cells to empty cells */
287  setMatrixCells(&tetramino->cells[0][0], 4, 4, EMPTY_CELL);
288 
289  /* Almost all the blocks have size 3 */
290  tetramino->size = 3;
291 
292  /* Initial configuration from: http://www.tetrisconcept.com/wiki/index.php/SRS */
293  switch (indexTetramino)
294  {
295  case TETROMINO_I:
296  tetramino->cells[0][1] = TETRIS_COLOR_CYAN;
297  tetramino->cells[1][1] = TETRIS_COLOR_CYAN;
298  tetramino->cells[2][1] = TETRIS_COLOR_CYAN;
299  tetramino->cells[3][1] = TETRIS_COLOR_CYAN;
300  tetramino->size = 4;
301  break;
302  case TETROMINO_O:
303  tetramino->cells[0][0] = TETRIS_COLOR_YELLOW;
304  tetramino->cells[0][1] = TETRIS_COLOR_YELLOW;
305  tetramino->cells[1][0] = TETRIS_COLOR_YELLOW;
306  tetramino->cells[1][1] = TETRIS_COLOR_YELLOW;
307  tetramino->size = 2;
308  break;
309  case TETROMINO_T:
310  tetramino->cells[0][1] = TETRIS_COLOR_PURPLE;
311  tetramino->cells[1][0] = TETRIS_COLOR_PURPLE;
312  tetramino->cells[1][1] = TETRIS_COLOR_PURPLE;
313  tetramino->cells[2][1] = TETRIS_COLOR_PURPLE;
314  break;
315  case TETROMINO_S:
316  tetramino->cells[0][1] = TETRIS_COLOR_GREEN;
317  tetramino->cells[1][0] = TETRIS_COLOR_GREEN;
318  tetramino->cells[1][1] = TETRIS_COLOR_GREEN;
319  tetramino->cells[2][0] = TETRIS_COLOR_GREEN;
320  break;
321  case TETROMINO_Z:
322  tetramino->cells[0][0] = TETRIS_COLOR_RED;
323  tetramino->cells[1][0] = TETRIS_COLOR_RED;
324  tetramino->cells[1][1] = TETRIS_COLOR_RED;
325  tetramino->cells[2][1] = TETRIS_COLOR_RED;
326  break;
327  case TETROMINO_J:
328  tetramino->cells[0][0] = TETRIS_COLOR_BLUE;
329  tetramino->cells[0][1] = TETRIS_COLOR_BLUE;
330  tetramino->cells[1][1] = TETRIS_COLOR_BLUE;
331  tetramino->cells[2][1] = TETRIS_COLOR_BLUE;
332  break;
333  case TETROMINO_L:
334  tetramino->cells[0][1] = TETRIS_COLOR_ORANGE;
335  tetramino->cells[1][1] = TETRIS_COLOR_ORANGE;
336  tetramino->cells[2][0] = TETRIS_COLOR_ORANGE;
337  tetramino->cells[2][1] = TETRIS_COLOR_ORANGE;
338  break;
339  }
340  tetramino->type = indexTetramino;
341 }
static void startGame ( StcGame game)
static

Definiert in Zeile 344 der Datei gui_tetris.c.

345 {
346  int i;
347 
348  /* Initialize game data */
349  game->errorCode = GAME_ERROR_NONE;
351  game->lastFallTime = game->systemTime;
352  game->isOver = 0;
353  game->isPaused = 0;
354  game->showPreview = 1;
355  game->events = EVENT_NONE;
356  game->delay = INI_DELAY_FALL;
357  /* Initialize game statistics */
358  game->stats.score = 0;
359  game->stats.lines = 0;
360  game->stats.totalPieces = 0;
361  game->stats.level = 0;
362  for (i = 0; i < 7; ++i)
363  {
364  game->stats.pieces[i] = 0;
365  }
366 
367  /* Initialize rand generator */
368  srand(game->systemTime);
369 
370  /* Initialize game tile map */
372 
373  /* Initialize falling tetromino */
374  setTetramino(rand() % 7, &game->fallingBlock);
375  game->fallingBlock.x = (BOARD_WIDTH - game->fallingBlock.size) / 2;
376  game->fallingBlock.y = 0;
377 
378  /* Initialize preview tetromino */
379  setTetramino(rand() % 7, &game->nextBlock);
380 }

Variablen-Dokumentation

StcGame* game

Definiert in Zeile 118 der Datei gui_tetris.c.

int prevNextBlockType = -1

Definiert in Zeile 144 der Datei gui_tetris.c.

int tile_size

Definiert in Zeile 134 der Datei gui_tetris.c.

Definiert in Zeile 141 der Datei gui_tetris.c.

Definiert in Zeile 142 der Datei gui_tetris.c.