CHDK_DE Vorschauversion  Trunk Rev. 5163
 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   150
 
#define PREVIEW_Y   10
 
#define BOARD_X   10
 
#define BOARD_Y   10
 
#define TILE_SIZE   10
 

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 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   10

Definiert in Zeile 137 der Datei gui_tetris.c.

#define BOARD_Y   10

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   150

Definiert in Zeile 135 der Datei gui_tetris.c.

#define PREVIEW_Y   10

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   10

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 690 der Datei gui_tetris.c.

690  {
692  gui_tetris_init();
693  return 1;
694 }
static int checkCollision ( StcGame game,
int  dx,
int  dy 
)
static

Definiert in Zeile 432 der Datei gui_tetris.c.

432  {
433  int newx, newy, i, j;
434 
435  newx = game->fallingBlock.x + dx;
436  newy = game->fallingBlock.y + dy;
437 
438  for (i = 0; i < game->fallingBlock.size; ++i) {
439  for (j = 0; j < game->fallingBlock.size; ++j) {
440  if (game->fallingBlock.cells[i][j] != EMPTY_CELL) {
441  /* Check the tetramino would be inside the left, right and bottom borders */
442  if ((newx + i < 0) || (newx + i >= BOARD_WIDTH)
443  || (newy + j >= BOARD_HEIGHT)) {
444  return 1;
445  }
446  /* Check the tetromino won't collide with existing cells in the map */
447  if (game->map[newx + i][newy + j] != EMPTY_CELL) {
448  return 1;
449  }
450  }
451  }
452  }
453  return 0;
454 }
StcGame * createGame ( )

Definiert in Zeile 349 der Datei gui_tetris.c.

349  {
350  /* Allocate space for our game object */
351  StcGame *game = (StcGame *) malloc(sizeof(StcGame));
352  return game;
353 }
void deleteGame ( StcGame pGame)

Definiert in Zeile 374 der Datei gui_tetris.c.

374  {
375  free(game);
376 }
static void dropTetramino ( StcGame game)
static

Definiert in Zeile 572 der Datei gui_tetris.c.

572  {
573  int y;
574  y = 1;
575  /* Calculate number of cells to drop */
576  while (!checkCollision(game, 0, y)) {
577  y++;
578  }
579  moveTetramino(game, 0, y - 1);
580 }
void gameEnd ( StcGame gameInstance)

Definiert in Zeile 369 der Datei gui_tetris.c.

369  {
370  /* Free platform resources */
371  platformEnd(game);
372 }
int gameInit ( StcGame gameInstance)

Definiert in Zeile 358 der Datei gui_tetris.c.

358  {
359  int errorCode;
360 
361  errorCode = platformInit(game);
362  if (errorCode == GAME_ERROR_NONE) {
363  startGame(game);
364  return GAME_ERROR_NONE;
365  }
366  return errorCode;
367 };
void gameUpdate ( StcGame gameInstance)

Definiert in Zeile 585 der Datei gui_tetris.c.

585  {
586  long sysTime;
587  /* Read user input */
589 
590  /* Update game state */
591  if (game->isOver) {
592 
593  if (game->stats.score > game->stats.high) {
595  FILE * f;
596  long buf;
597  buf = game->stats.score;
598 
599  mkdir_if_not_exist("A/CHDK/GAMES");
600  f = fopen ( "A/CHDK/GAMES/TETRIS.SCO" , "wb" );
601  fwrite (&buf , 1 , sizeof(buf) , f );
602  fclose (f);
603  }
604 
605 
606  //if (game->events & EVENT_RESTART) {
607  if (game->events & EVENT_PAUSE) {
608 
609  //TurnOnBackLight();
610 
611  game->isOver = 0;
612  startGame(game);
613 
614  }
615  }
616  else {
617  sysTime = platformGetSystemTime();
618 
619  /* Always handle pause event */
620  if (game->events & EVENT_PAUSE) {
621  game->isPaused = !game->isPaused;
623  }
624 
625  /* Check if the game is paused */
626  if (game->isPaused) {
627  /* We achieve the effect of pausing the game
628  * adding the last frame duration to lastFallTime */
629  game->lastFallTime += (sysTime - game->systemTime);
630  }
631  else {
632  if (game->events != EVENT_NONE) {
633  if (game->events & EVENT_SHOW_NEXT) {
635  }
636  if (game->events & EVENT_DROP) {
638  }
639  if (game->events & EVENT_ROTATE_CW) {
640  rotateTetramino(game, 1);
641  }
642  if (game->events & EVENT_MOVE_RIGHT) {
643  moveTetramino(game, 1, 0);
644  }
645  else if (game->events & EVENT_MOVE_LEFT) {
646  moveTetramino(game, -1, 0);
647  }
648  if (game->events & EVENT_MOVE_DOWN) {
649  moveTetramino(game, 0, 1);
650  }
652  }
653  /* Check if it's time to move downwards the falling tetromino */
654  if (sysTime - game->lastFallTime >= game->delay) {
655  moveTetramino(game, 0, 1);
656  game->lastFallTime = sysTime;
657  }
658  }
659  game->systemTime = sysTime;
660  }
661  /* Draw game state */
663 }
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 696 der Datei gui_tetris.c.

696  {
697  gameUpdate(game);
698 }
void gui_tetris_init ( )

Definiert in Zeile 665 der Datei gui_tetris.c.

665  {
670  game = createGame();
671  gameInit(game);
672 
673  long buf;
674  FILE *f;
675 
676  f=fopen("A/CHDK/GAMES/TETRIS.SCO","rb");
677  if(!f) {
678  game->stats.high = 0;
679  } else {
680 
681  fread( &buf, 1, sizeof( buf ), f );
682  game->stats.high = buf;
683 
684  }
685 
686  fclose (f);
687  startGame(game);
688 }
int gui_tetris_kbd_process ( )

Definiert in Zeile 700 der Datei gui_tetris.c.

700  {
701  switch ( kbd_get_autoclicked_key() )
702  {
703  case KEY_UP:
704  if ((game->isPaused) || (game->isOver)) {
705  TurnOnBackLight();
706  } else { TurnOffBackLight(); }
707  game->events |= EVENT_PAUSE;
708  break;
709  case KEY_LEFT:
711  break;
712  case KEY_RIGHT:
714  break;
715  case KEY_DOWN:
717  break;
718  //case KEY_SET:
719  //game->events |= EVENT_RESTART;
720  //break;
721  case KEY_DISPLAY:
722  case KEY_ERASE:
723  case KEY_SET:
725  break;
726  default:
727  break;
728  }
729  return 0;
730 }
long mkdir_if_not_exist ( const char *  dirname)

Definiert in Zeile 122 der Datei gui_tetris.c.

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

Definiert in Zeile 492 der Datei gui_tetris.c.

492  {
493  int i, j, hasFullRow, numFilledRows;
494 
495  /* Check if the move would create a collision */
496  if (checkCollision(game, x, y)) {
497  /* In case of collision check if move was downwards (y == 1) */
498  if (y == 1) {
499  /* Check if collision occur when the falling
500  * tetromino is in the 1st or 2nd row */
501  if (game->fallingBlock.y <= 1) {
502  game->isOver = 1; /* if this happens the game is over */
503  }
504  else {
505  /* The falling tetromino has reached the bottom,
506  * so we copy their cells to the board map */
507  for (i = 0; i < game->fallingBlock.size; ++i) {
508  for (j = 0; j < game->fallingBlock.size; ++j) {
509  if (game->fallingBlock.cells[i][j] != EMPTY_CELL) {
510  game->map[game->fallingBlock.x + i][game->fallingBlock.y + j]
511  = game->fallingBlock.cells[i][j];
512  }
513  }
514  }
515 
516  /* Check if the landing tetromino has created full rows */
517  numFilledRows = 0;
518  for (j = 1; j < BOARD_HEIGHT; ++j) {
519  hasFullRow = 1;
520  for (i = 0; i < BOARD_WIDTH; ++i) {
521  if (game->map[i][j] == EMPTY_CELL) {
522  hasFullRow = 0;
523  break;
524  }
525  }
526  /* If we found a full row we need to remove that row from the map
527  * we do that by just moving all the above rows one row below */
528  if (hasFullRow) {
529  for (x = 0; x < BOARD_WIDTH; ++x) {
530  for (y = j; y > 0; --y) {
531  game->map[x][y] = game->map[x][y - 1];
532  }
533  }
534  numFilledRows++; /* increase filled row counter */
535  }
536  }
537 
538  /* Update game statistics */
539  if (numFilledRows) {
540  onFilledRows(game, numFilledRows);
541  }
542  game->stats.totalPieces++;
543  game->stats.pieces[game->fallingBlock.type]++;
544 
545  /* Use preview tetromino as falling tetromino.
546  * Copy preview tetramino for falling tetramino */
547  for (i = 0; i < 4; ++i) {
548  for (j = 0; j < 4; ++j) {
549  game->fallingBlock.cells[i][j] = game->nextBlock.cells[i][j];
550  }
551  }
552  game->fallingBlock.size = game->nextBlock.size;
553  game->fallingBlock.type = game->nextBlock.type;
554 
555  /* Reset position */
556  game->fallingBlock.y = 0;
557  game->fallingBlock.x = (BOARD_WIDTH - game->fallingBlock.size) / 2;
558 
559  /* Create next preview tetromino */
560  setTetramino(rand() % 7, &game->nextBlock);
561  }
562  }
563  }
564  else {
565  /* There are no collisions, just move the tetramino */
566  game->fallingBlock.x += x;
567  game->fallingBlock.y += y;
568  }
569 }
static void onFilledRows ( StcGame game,
int  filledRows 
)
static

Definiert in Zeile 457 der Datei gui_tetris.c.

457  {
458  /* Update total number of filled rows */
459  game->stats.lines += filledRows;
460 
461  /* Increase score accordingly to the number of filled rows */
462  switch (filledRows) {
463  case 1:
464  game->stats.score += (SCORE_1_FILLED_ROW * (game->stats.level));
465  break;
466  case 2:
467  game->stats.score += (SCORE_2_FILLED_ROW * (game->stats.level));
468  break;
469  case 3:
470  game->stats.score += (SCORE_3_FILLED_ROW * (game->stats.level));
471  break;
472  case 4:
473  game->stats.score += (SCORE_4_FILLED_ROW * (game->stats.level));
474  break;
475  default:
476  game->errorCode = GAME_ERROR_ASSERT; /* This can't happen */
477  }
478  /* Check if we need to update level */
479  if (game->stats.lines >= FILLED_ROWS_FOR_LEVEL_UP * (game->stats.level)) {
480  game->stats.level++;
481 
482  /* Increase speed for falling tetrominoes */
484  }
485 }
void platformEnd ( StcGame gameInstance)

Definiert in Zeile 131 der Datei gui_tetris.c.

131 {}
long platformGetSystemTime ( )

Definiert in Zeile 240 der Datei gui_tetris.c.

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

Definiert in Zeile 130 der Datei gui_tetris.c.

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

Definiert in Zeile 134 der Datei gui_tetris.c.

134 {}
void platformRenderGame ( StcGame gameInstance)

Definiert in Zeile 145 der Datei gui_tetris.c.

145  {
146  int i, j;
147 
148  for(i = 0; i < BOARD_WIDTH; ++i) {
149  for (j = 0; j < BOARD_HEIGHT; ++j){
150  tmp[i][j] = EMPTY_CELL;
151  tmp2[i][j] = EMPTY_CELL;
152  }
153  }
154 
155  /* Draw preview block */
158  for (i = 0; i < 4; ++i) {
159  for (j = 0; j < 4; ++j) {
160  if (game->nextBlock.cells[i][j] != EMPTY_CELL) {
162  PREVIEW_Y + (TILE_SIZE * j),
164  PREVIEW_Y + (TILE_SIZE * j)+TILE_SIZE-1,
166  }else{
168  PREVIEW_Y + (TILE_SIZE * j),
170  PREVIEW_Y + (TILE_SIZE * j)+TILE_SIZE-1,
172  }
173  }
174  }
175  }
176 
177  /* Draw the cells in the board */
178  for (i = 0; i < BOARD_WIDTH; ++i) {
179  for (j = 0; j < BOARD_HEIGHT; ++j){
180  if (game->map[i][j] != EMPTY_CELL) {
181  tmp2[i][j] = game->map[i][j];
182  }
183  }
184  }
185  /* Draw falling tetromino */
186  for (i = 0; i<4; ++i) {
187  for (j = 0; j < 4; ++j) {
188  if (game->fallingBlock.cells[i][j] != EMPTY_CELL) {
190  }
191  }
192  }
193 
194  for (i = 0; i < BOARD_WIDTH; ++i) {
195  for (j = 0; j < BOARD_HEIGHT; ++j){
196  if(tmp[i][j] != EMPTY_CELL){
198  BOARD_Y + (TILE_SIZE * j),
200  BOARD_Y + (TILE_SIZE * j)+TILE_SIZE-1,
201  MAKE_COLOR(tmp[i][j], tmp[i][j]), RECT_BORDER0|DRAW_FILLED);
202  }else if(tmp2[i][j] != EMPTY_CELL){
204  BOARD_Y + (TILE_SIZE * j),
206  BOARD_Y + (TILE_SIZE * j)+TILE_SIZE-1,
207  MAKE_COLOR(tmp2[i][j], tmp2[i][j]), RECT_BORDER0|DRAW_FILLED);
208  }else{
210  BOARD_Y + (TILE_SIZE * j),
212  BOARD_Y + (TILE_SIZE * j)+TILE_SIZE-1,
214  }
215  }
216  }
217  /* output game info */
218  char str_buf[100];
219  static struct tm *ttm;
220  sprintf(str_buf,"High: %5d",game->stats.high);
222  sprintf(str_buf,"Points: %5d",game->stats.score);
224  sprintf(str_buf,"Lines: %5d",game->stats.lines);
226  sprintf(str_buf,"Level: %5d",game->stats.level);
228  sprintf(str_buf,"UP -> Pause");
230  sprintf(str_buf,"SET -> Rotate");
232  ttm = get_localtime();
233  sprintf(str_buf,"Time: %2u:%02u", ttm->tm_hour, ttm->tm_min);
235  sprintf(str_buf,"Batt: %3d%%", get_batt_perc());
237 }
void rotateTetramino ( StcGame game,
int  clockwise 
)

Definiert in Zeile 382 der Datei gui_tetris.c.

382  {
383  int i, j;
384  int rotated[4][4]; /* temporary array to hold rotated cells */
385 
386  /* If TETRAMINO_O is falling return immediately */
387  if (game->fallingBlock.type == TETROMINO_O) {
388  return; /* rotation doesn't require any changes */
389  }
390 
391  /* Initialize rotated cells to blank */
392  setMatrixCells(&rotated[0][0], 4, 4, EMPTY_CELL);
393 
394  /* Copy rotated cells to the temporary array */
395  for (i = 0; i < game->fallingBlock.size; ++i) {
396  for (j = 0; j < game->fallingBlock.size; ++j) {
397  if (clockwise) {
398  rotated[game->fallingBlock.size - j - 1][i] = game->fallingBlock.cells[i][j];
399  } else {
400  rotated[j][game->fallingBlock.size - i - 1] = game->fallingBlock.cells[i][j];
401  }
402  }
403  }
404  /* Check collision of the temporary array */
405  for (i = 0; i < game->fallingBlock.size; ++i) {
406  for (j = 0; j < game->fallingBlock.size; ++j) {
407  if (rotated[i][j] != EMPTY_CELL) {
408  /* Check collision with left, right or bottom borders of the map */
409  if ((game->fallingBlock.x + i < 0) || (game->fallingBlock.x + i >= BOARD_WIDTH)
410  || (game->fallingBlock.y + j >= BOARD_HEIGHT)) {
411  return; /* there was collision therefore return */
412  }
413  /* Check collision with existing cells in the map */
414  if (game->map[i + game->fallingBlock.x][j + game->fallingBlock.y] != EMPTY_CELL) {
415  return; /* there was collision therefore return */
416  }
417  }
418  }
419  }
420  /* There are no collisions, replace tetramino cells with rotated cells */
421  for (i = 0; i < 4; ++i) {
422  for (j = 0; j < 4; ++j) {
423  game->fallingBlock.cells[i][j] = rotated[i][j];
424  }
425  }
426 }
static void setMatrixCells ( int *  matrix,
int  width,
int  height,
int  value 
)
static

Definiert in Zeile 243 der Datei gui_tetris.c.

243  {
244  int i, j;
245  for (i = 0; i < width; ++i) {
246  for (j = 0; j < height; ++j) {
247  *(matrix + i + (j * width)) = value;
248  }
249  }
250 }
static void setTetramino ( int  indexTetramino,
StcTetramino tetramino 
)
static

Definiert in Zeile 253 der Datei gui_tetris.c.

253  {
254 
255  /* Initialize tetromino cells to empty cells */
256  setMatrixCells(&tetramino->cells[0][0], 4, 4, EMPTY_CELL);
257 
258  /* Almost all the blocks have size 3 */
259  tetramino->size = 3;
260 
261  /* Initial configuration from: http://www.tetrisconcept.com/wiki/index.php/SRS */
262  switch (indexTetramino) {
263  case TETROMINO_I:
264  tetramino->cells[0][1] = TETRIS_COLOR_CYAN;
265  tetramino->cells[1][1] = TETRIS_COLOR_CYAN;
266  tetramino->cells[2][1] = TETRIS_COLOR_CYAN;
267  tetramino->cells[3][1] = TETRIS_COLOR_CYAN;
268  tetramino->size = 4;
269  break;
270  case TETROMINO_O:
271  tetramino->cells[0][0] = TETRIS_COLOR_YELLOW;
272  tetramino->cells[0][1] = TETRIS_COLOR_YELLOW;
273  tetramino->cells[1][0] = TETRIS_COLOR_YELLOW;
274  tetramino->cells[1][1] = TETRIS_COLOR_YELLOW;
275  tetramino->size = 2;
276  break;
277  case TETROMINO_T:
278  tetramino->cells[0][1] = TETRIS_COLOR_PURPLE;
279  tetramino->cells[1][0] = TETRIS_COLOR_PURPLE;
280  tetramino->cells[1][1] = TETRIS_COLOR_PURPLE;
281  tetramino->cells[2][1] = TETRIS_COLOR_PURPLE;
282  break;
283  case TETROMINO_S:
284  tetramino->cells[0][1] = TETRIS_COLOR_GREEN;
285  tetramino->cells[1][0] = TETRIS_COLOR_GREEN;
286  tetramino->cells[1][1] = TETRIS_COLOR_GREEN;
287  tetramino->cells[2][0] = TETRIS_COLOR_GREEN;
288  break;
289  case TETROMINO_Z:
290  tetramino->cells[0][0] = TETRIS_COLOR_RED;
291  tetramino->cells[1][0] = TETRIS_COLOR_RED;
292  tetramino->cells[1][1] = TETRIS_COLOR_RED;
293  tetramino->cells[2][1] = TETRIS_COLOR_RED;
294  break;
295  case TETROMINO_J:
296  tetramino->cells[0][0] = TETRIS_COLOR_BLUE;
297  tetramino->cells[0][1] = TETRIS_COLOR_BLUE;
298  tetramino->cells[1][1] = TETRIS_COLOR_BLUE;
299  tetramino->cells[2][1] = TETRIS_COLOR_BLUE;
300  break;
301  case TETROMINO_L:
302  tetramino->cells[0][1] = TETRIS_COLOR_ORANGE;
303  tetramino->cells[1][1] = TETRIS_COLOR_ORANGE;
304  tetramino->cells[2][0] = TETRIS_COLOR_ORANGE;
305  tetramino->cells[2][1] = TETRIS_COLOR_ORANGE;
306  break;
307  }
308  tetramino->type = indexTetramino;
309 }
static void startGame ( StcGame game)
static

Definiert in Zeile 312 der Datei gui_tetris.c.

312  {
313  int i;
314 
315  /* Initialize game data */
316  game->errorCode = GAME_ERROR_NONE;
318  game->lastFallTime = game->systemTime;
319  game->isOver = 0;
320  game->isPaused = 0;
321  game->showPreview = 1;
322  game->events = EVENT_NONE;
323  game->delay = INI_DELAY_FALL;
324  /* Initialize game statistics */
325  game->stats.score = 0;
326  game->stats.lines = 0;
327  game->stats.totalPieces = 0;
328  game->stats.level = 0;
329  for (i = 0; i < 7; ++i) {
330  game->stats.pieces[i] = 0;
331  }
332 
333  /* Initialize rand generator */
334  srand(game->systemTime);
335 
336  /* Initialize game tile map */
338 
339  /* Initialize falling tetromino */
340  setTetramino(rand() % 7, &game->fallingBlock);
341  game->fallingBlock.x = (BOARD_WIDTH - game->fallingBlock.size) / 2;
342  game->fallingBlock.y = 0;
343 
344  /* Initialize preview tetromino */
345  setTetramino(rand() % 7, &game->nextBlock);
346 }

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.

Definiert in Zeile 141 der Datei gui_tetris.c.

Definiert in Zeile 142 der Datei gui_tetris.c.