first person tetris added

This commit is contained in:
kju 2010-01-21 20:15:05 +00:00
parent 254d0d2f3d
commit ab08ab3fa2
8 changed files with 230 additions and 39 deletions

View File

@ -3,6 +3,7 @@ comment "Games"
dep_bool "tetris" GAME_TETRIS $JOYSTICK_SUPPORT $RANDOM_SUPPORT dep_bool "tetris" GAME_TETRIS $JOYSTICK_SUPPORT $RANDOM_SUPPORT
dep_bool "bastet" GAME_BASTET $GAME_TETRIS dep_bool "bastet" GAME_BASTET $GAME_TETRIS
dep_bool "first person tetris" GAME_TETRIS_FP $GAME_TETRIS
dep_bool "space invaders" GAME_SPACE_INVADERS $JOYSTICK_SUPPORT $RANDOM_SUPPORT dep_bool "space invaders" GAME_SPACE_INVADERS $JOYSTICK_SUPPORT $RANDOM_SUPPORT
dep_bool "snake" GAME_SNAKE $JOYSTICK_SUPPORT $RANDOM_SUPPORT dep_bool "snake" GAME_SNAKE $JOYSTICK_SUPPORT $RANDOM_SUPPORT
dep_bool "breakout" GAME_BREAKOUT $JOYSTICK_SUPPORT $RANDOM_SUPPORT dep_bool "breakout" GAME_BREAKOUT $JOYSTICK_SUPPORT $RANDOM_SUPPORT

View File

@ -7,6 +7,10 @@
#include "../../util.h" #include "../../util.h"
#include "input.h" #include "input.h"
#ifdef GAME_TETRIS_FP
#include "tetrisfp.h"
#endif
#include "../../compat/pgmspace.h" #include "../../compat/pgmspace.h"
#define WAIT(ms) wait(ms) #define WAIT(ms) wait(ms)
#define PM(value) pgm_read_word(&value) #define PM(value) pgm_read_word(&value)
@ -117,7 +121,7 @@ void tetris_input_chatterProtect (tetris_input_t *pIn,
* Argument pIn: pointer to an input object * Argument pIn: pointer to an input object
* Return value: see definition of tetris_input_command_t * Return value: see definition of tetris_input_command_t
*/ */
tetris_input_command_t tetris_input_queryJoystick() tetris_input_command_t tetris_input_queryJoystick(uint8_t nFirstPerson)
{ {
tetris_input_command_t cmdReturn; tetris_input_command_t cmdReturn;
@ -127,11 +131,30 @@ tetris_input_command_t tetris_input_queryJoystick()
} }
else if (JOYISLEFT) else if (JOYISLEFT)
{ {
#ifdef GAME_TETRIS_FP
switch (tetris_screendir) {
case 0: cmdReturn = TETRIS_INCMD_LEFT; break;
case 1: cmdReturn = TETRIS_INCMD_DOWN; break;
case 2: cmdReturn = TETRIS_INCMD_RIGHT; break;
case 3: cmdReturn = TETRIS_INCMD_ROT_CW; break;
}
#else
cmdReturn = TETRIS_INCMD_LEFT; cmdReturn = TETRIS_INCMD_LEFT;
#endif
} }
else if (JOYISRIGHT) else if (JOYISRIGHT)
{ {
#ifdef GAME_TETRIS_FP
switch (tetris_screendir) {
case 0: cmdReturn = TETRIS_INCMD_RIGHT; break;
case 1: cmdReturn = TETRIS_INCMD_ROT_CW; break;
case 2: cmdReturn = TETRIS_INCMD_LEFT; break;
case 3: cmdReturn = TETRIS_INCMD_DOWN; break;
}
#else
cmdReturn = TETRIS_INCMD_RIGHT; cmdReturn = TETRIS_INCMD_RIGHT;
#endif
} }
else if (JOYISUP && JOYISDOWN) else if (JOYISUP && JOYISDOWN)
{ {
@ -140,11 +163,29 @@ tetris_input_command_t tetris_input_queryJoystick()
} }
else if (JOYISDOWN) else if (JOYISDOWN)
{ {
#ifdef GAME_TETRIS_FP
switch (tetris_screendir) {
case 0: cmdReturn = TETRIS_INCMD_DOWN; break;
case 1: cmdReturn = TETRIS_INCMD_RIGHT; break;
case 2: cmdReturn = TETRIS_INCMD_ROT_CW; break;
case 3: cmdReturn = TETRIS_INCMD_LEFT; break;
}
#else
cmdReturn = TETRIS_INCMD_DOWN; cmdReturn = TETRIS_INCMD_DOWN;
#endif
} }
else if (JOYISUP) else if (JOYISUP)
{ {
#ifdef GAME_TETRIS_FP
switch (tetris_screendir) {
case 0: cmdReturn = TETRIS_INCMD_ROT_CW; break;
case 1: cmdReturn = TETRIS_INCMD_LEFT; break;
case 2: cmdReturn = TETRIS_INCMD_DOWN; break;
case 3: cmdReturn = TETRIS_INCMD_RIGHT; break;
}
#else
cmdReturn = TETRIS_INCMD_ROT_CW; cmdReturn = TETRIS_INCMD_ROT_CW;
#endif
} }
else else
{ {
@ -203,7 +244,8 @@ void tetris_input_destruct(tetris_input_t *pIn)
* Return value: see definition of tetris_input_command_t * Return value: see definition of tetris_input_command_t
*/ */
tetris_input_command_t tetris_input_getCommand(tetris_input_t *pIn, tetris_input_command_t tetris_input_getCommand(tetris_input_t *pIn,
tetris_input_pace_t nPace) tetris_input_pace_t nPace,
uint8_t nFirstPerson)
{ {
assert (pIn != NULL); assert (pIn != NULL);
@ -230,7 +272,7 @@ tetris_input_command_t tetris_input_getCommand(tetris_input_t *pIn,
while (pIn->nLoopCycles < nMaxCycles) while (pIn->nLoopCycles < nMaxCycles)
{ {
cmdJoystick = tetris_input_queryJoystick(); cmdJoystick = tetris_input_queryJoystick(nFirstPerson);
// only obey current command if it is not considered as chattering // only obey current command if it is not considered as chattering
if (((cmdJoystick < TETRIS_INCMD_NONE) ? if (((cmdJoystick < TETRIS_INCMD_NONE) ?

View File

@ -112,7 +112,8 @@ void tetris_input_destruct(tetris_input_t *pIn);
* Return value: see definition of tetris_input_command_t * Return value: see definition of tetris_input_command_t
*/ */
tetris_input_command_t tetris_input_getCommand(tetris_input_t *pIn, tetris_input_command_t tetris_input_getCommand(tetris_input_t *pIn,
tetris_input_pace_t nPace); tetris_input_pace_t nPace,
uint8_t nFirstPerson);
/* Function: tetris_input_setLevel /* Function: tetris_input_setLevel

View File

@ -13,6 +13,7 @@
#include "../../compat/pgmspace.h" #include "../../compat/pgmspace.h"
#include "../../menu/menu.h" #include "../../menu/menu.h"
#include "../../random/prng.h" #include "../../random/prng.h"
#include "../../pixel.h"
#include "logic.h" #include "logic.h"
#include "piece.h" #include "piece.h"
@ -21,13 +22,15 @@
#include "input.h" #include "input.h"
#include "highscore.h" #include "highscore.h"
#define NUMHIGHSCORES 3
#ifdef GAME_BASTET #ifdef GAME_BASTET
#include "bast.h" #include "bast.h"
#define NUMHIGHSCORES 2
#else
#define NUMHIGHSCORES 1
#endif #endif
#ifdef GAME_TETRIS_FP
#include "tetrisfp.h"
#endif
#ifdef EEMEM #ifdef EEMEM
/*********************** /***********************
@ -51,6 +54,19 @@ game_descriptor_t tetris_game_descriptor
tetris_icon, tetris_icon,
}; };
#ifdef GAME_TETRIS_FP
// Bastet icon, MSB is leftmost pixel
static uint8_t tetrisfp_icon[8] PROGMEM =
{ 0xee, 0x89, 0xee, 0x88, 0x88, 0x20, 0x2c, 0x6c };
game_descriptor_t tetrisfp_game_descriptor
__attribute__((section(".game_descriptors"))) =
{
&tetris_fp,
tetrisfp_icon,
};
#endif
#ifdef GAME_BASTET #ifdef GAME_BASTET
// Bastet icon, MSB is leftmost pixel // Bastet icon, MSB is leftmost pixel
static uint8_t bastet_icon[8] PROGMEM = static uint8_t bastet_icon[8] PROGMEM =
@ -62,6 +78,8 @@ game_descriptor_t bastet_game_descriptor
bastet_icon, bastet_icon,
}; };
#endif #endif
#endif /*MENU_SUPPORT*/ #endif /*MENU_SUPPORT*/
/*************************** /***************************
@ -227,12 +245,23 @@ void tetris_bastet()
} }
/* Function: tetris_main /* Function: tetris_fp
* Description: runs the tetris game * Description: runs the tetris first person game
* Argument nBastet: 0 for normal Tetris, 1 for Bastet
* Return value: void * Return value: void
*/ */
void tetris_main(int8_t nBastet) void tetris_fp()
{
tetris_main(2);
}
/* Function: tetris_main
* Description: runs the tetris game
* Argument nMode: 0 for normal Tetris, 1 for Bastet
* 2 for first person tetris
* Return value: void
*/
void tetris_main(int8_t nMode)
{ {
// get view dependent dimensions of the playfield // get view dependent dimensions of the playfield
int8_t nWidth; int8_t nWidth;
@ -242,11 +271,15 @@ void tetris_main(int8_t nBastet)
// holds the current user command which should be processed // holds the current user command which should be processed
tetris_input_command_t inCmd; tetris_input_command_t inCmd;
#ifdef GAME_TETRIS_FP
tetris_screendir = 0;
#endif
// prepare data structures that drive the game... // prepare data structures that drive the game...
tetris_logic_t *pLogic = tetris_logic_construct(nBastet); tetris_logic_t *pLogic = tetris_logic_construct((nMode==1));
tetris_playfield_t *pPl = tetris_playfield_construct(nWidth, nHeight); tetris_playfield_t *pPl = tetris_playfield_construct(nWidth, nHeight);
tetris_input_t *pIn = tetris_input_construct(); tetris_input_t *pIn = tetris_input_construct();
tetris_view_t *pView = tetris_view_construct(pLogic, pPl); tetris_view_t *pView = tetris_view_construct(pLogic, pPl, (nMode==2));
#ifdef GAME_BASTET #ifdef GAME_BASTET
tetris_bastet_t *pBastet; tetris_bastet_t *pBastet;
#endif #endif
@ -261,8 +294,8 @@ void tetris_main(int8_t nBastet)
if (nHighscore == 0) if (nHighscore == 0)
{ {
#endif #endif
nHighscore = tetris_logic_retrieveHighscore(nBastet); nHighscore = tetris_logic_retrieveHighscore(nMode);
nHighscoreName = tetris_logic_retrieveHighscoreName(nBastet); nHighscoreName = tetris_logic_retrieveHighscoreName(nMode);
#ifndef GAME_BASTET #ifndef GAME_BASTET
} }
#endif #endif
@ -271,7 +304,7 @@ void tetris_main(int8_t nBastet)
tetris_piece_t *pPiece; tetris_piece_t *pPiece;
tetris_piece_t *pNextPiece; tetris_piece_t *pNextPiece;
#ifdef GAME_BASTET #ifdef GAME_BASTET
if (nBastet) if (nMode == 1)
{ {
pBastet = tetris_bastet_construct(pPl); pBastet = tetris_bastet_construct(pPl);
pNextPiece = pPiece = NULL; pNextPiece = pPiece = NULL;
@ -304,7 +337,7 @@ void tetris_main(int8_t nBastet)
// the playfield awaits a new piece // the playfield awaits a new piece
case TETRIS_PFS_READY: case TETRIS_PFS_READY:
#ifdef GAME_BASTET #ifdef GAME_BASTET
if (nBastet) if (nMode == 1)
{ {
if (pPiece != NULL) if (pPiece != NULL)
{ {
@ -361,7 +394,7 @@ void tetris_main(int8_t nBastet)
} }
// ensure correct view mode if the game isn't paused // ensure correct view mode if the game isn't paused
if ((inCmd = tetris_input_getCommand(pIn, inPace)) if ((inCmd = tetris_input_getCommand(pIn, inPace, (nMode==2)))
!= TETRIS_INCMD_PAUSE) != TETRIS_INCMD_PAUSE)
{ {
tetris_view_setViewMode(pView, TETRIS_VIMO_RUNNING); tetris_view_setViewMode(pView, TETRIS_VIMO_RUNNING);
@ -403,7 +436,17 @@ void tetris_main(int8_t nBastet)
// player rotated the piece clockwise // player rotated the piece clockwise
case TETRIS_INCMD_ROT_CW: case TETRIS_INCMD_ROT_CW:
#ifdef GAME_TETRIS_FP
if (nMode == 2) {
tetris_view_rotate();
tetris_playfield_rotatePiece(pPl, TETRIS_PC_ROT_CCW);
} else
{
#endif
tetris_playfield_rotatePiece(pPl, TETRIS_PC_ROT_CW); tetris_playfield_rotatePiece(pPl, TETRIS_PC_ROT_CW);
#ifdef GAME_TETRIS_FP
}
#endif
break; break;
// player rotated the piece counter clockwise // player rotated the piece counter clockwise
@ -466,13 +509,13 @@ void tetris_main(int8_t nBastet)
{ {
nHighscore = nScore; nHighscore = nScore;
nHighscoreName = tetris_highscore_inputName(); nHighscoreName = tetris_highscore_inputName();
tetris_logic_saveHighscore(nBastet, nHighscore); tetris_logic_saveHighscore(nMode, nHighscore);
tetris_logic_saveHighscoreName(nBastet, nHighscoreName); tetris_logic_saveHighscoreName(nMode, nHighscoreName);
} }
// clean up // clean up
#ifdef GAME_BASTET #ifdef GAME_BASTET
if (nBastet) if (nMode == 1)
{ {
tetris_bastet_destruct(pBastet); tetris_bastet_destruct(pBastet);
} }

View File

@ -55,13 +55,20 @@ void tetris();
*/ */
void tetris_bastet(); void tetris_bastet();
/* Function: tetris_fp
* Description: runs the tetris first person game
* Return value: void
*/
void tetris_fp();
/* Function: tetris_main /* Function: tetris_main
* Description: runs the tetris game * Description: runs the tetris game
* Argument nBastet: 0 for normal Tetris, 1 for Bastet * Argument nMode: 0 for normal Tetris, 1 for Bastet,
* 2 for first person tetris
* Return value: void * Return value: void
*/ */
void tetris_main(int8_t nBastet); void tetris_main(int8_t nMode);
/* Function: tetris_logic_singleDrop /* Function: tetris_logic_singleDrop
* Description: add points which result from single step dropping * Description: add points which result from single step dropping
@ -172,5 +179,12 @@ void tetris_logic_setPreviewPiece(tetris_logic_t *pLogic,
*/ */
tetris_piece_t* tetris_logic_getPreviewPiece(tetris_logic_t *pLogic); tetris_piece_t* tetris_logic_getPreviewPiece(tetris_logic_t *pLogic);
#ifdef GAME_TETRIS_FP
/* Function: tetris_view_rotate
* Description: rotate view for first person mode
* Return value: void
*/
void tetris_view_rotate(void);
#endif
#endif /*TETRIS_LOGIC_H_*/ #endif /*TETRIS_LOGIC_H_*/

6
games/tetris/tetrisfp.h Normal file
View File

@ -0,0 +1,6 @@
#ifndef TETRISFP_H_
#define TETRISFP_H_
extern uint8_t tetris_screendir;
#endif /* BAST_H_ */

View File

@ -14,6 +14,12 @@
#define WAIT(ms) wait(ms) #define WAIT(ms) wait(ms)
#ifdef GAME_TETRIS_FP
uint8_t tetris_screendir;
#endif
void (*setpixel_wrapper)(pixel p, unsigned char value);
/*********** /***********
* defines * * defines *
@ -38,6 +44,9 @@
#define TETRIS_VIEW_COLORCOUNTER 2 #define TETRIS_VIEW_COLORCOUNTER 2
/*************************** /***************************
* non-interface functions * * non-interface functions *
***************************/ ***************************/
@ -136,7 +145,7 @@ void tetris_view_drawDump(tetris_view_t *pV)
{ {
nColor = TETRIS_VIEW_COLORSPACE; nColor = TETRIS_VIEW_COLORSPACE;
} }
setpixel((pixel){14-x,nRow}, nColor); setpixel_wrapper((pixel){14-x,nRow}, nColor);
nElementMask <<= 1; nElementMask <<= 1;
} }
} }
@ -177,7 +186,7 @@ void tetris_view_drawPreviewPiece(tetris_view_t *pV, tetris_piece_t *pPc)
{ {
nColor = TETRIS_VIEW_COLORSPACE; nColor = TETRIS_VIEW_COLORSPACE;
} }
setpixel((pixel) {3 - x, y + 6}, nColor); setpixel_wrapper((pixel) {3 - x, y + 6}, nColor);
nElementMask <<= 1; nElementMask <<= 1;
} }
} }
@ -188,7 +197,7 @@ void tetris_view_drawPreviewPiece(tetris_view_t *pV, tetris_piece_t *pPc)
{ {
for (uint8_t x = 0; x < 4; ++x) for (uint8_t x = 0; x < 4; ++x)
{ {
setpixel((pixel) {3 - x, y + 6}, TETRIS_VIEW_COLORSPACE); setpixel_wrapper((pixel) {3 - x, y + 6}, TETRIS_VIEW_COLORSPACE);
} }
} }
} }
@ -206,8 +215,8 @@ void tetris_view_drawBorders(uint8_t nColor)
uint8_t x, y; uint8_t x, y;
for (y = 0; y < 16; ++y) for (y = 0; y < 16; ++y)
{ {
setpixel((pixel){4, y}, nColor); setpixel_wrapper((pixel){4, y}, nColor);
setpixel((pixel){15, y}, nColor); setpixel_wrapper((pixel){15, y}, nColor);
} }
for (y = 0; y < 5; ++y) for (y = 0; y < 5; ++y)
@ -216,8 +225,8 @@ void tetris_view_drawBorders(uint8_t nColor)
{ {
if ((y < 1 || y > 3) || (x < 1 || y > 3)) if ((y < 1 || y > 3) || (x < 1 || y > 3))
{ {
setpixel((pixel){x, y}, nColor); setpixel_wrapper((pixel){x, y}, nColor);
setpixel((pixel){x, y + 11}, nColor); setpixel_wrapper((pixel){x, y + 11}, nColor);
} }
} }
} }
@ -276,7 +285,7 @@ void tetris_view_blinkLines(tetris_playfield_t *pPl)
uint8_t nColor = (nColIdx == 0 ? TETRIS_VIEW_COLORFADE uint8_t nColor = (nColIdx == 0 ? TETRIS_VIEW_COLORFADE
: TETRIS_VIEW_COLORPIECE); : TETRIS_VIEW_COLORPIECE);
setpixel((pixel){14 - x, y}, nColor); setpixel_wrapper((pixel){14 - x, y}, nColor);
} }
} }
} }
@ -316,9 +325,9 @@ void tetris_view_showLineNumbers(tetris_view_t *pV)
x = 1; x = 1;
} }
// ones // ones
setpixel((pixel){x, y}, nOnesPen); setpixel_wrapper((pixel){x, y}, nOnesPen);
// tens (increment x, add vertical offset for lower part of the border) // tens (increment x, add vertical offset for lower part of the border)
setpixel((pixel){x++, y + 11}, nTensPen); setpixel_wrapper((pixel){x++, y + 11}, nTensPen);
} }
} }
@ -334,7 +343,8 @@ void tetris_view_showLineNumbers(tetris_view_t *pV)
* Return value: pointer to a newly created view * Return value: pointer to a newly created view
*/ */
tetris_view_t *tetris_view_construct(tetris_logic_t *pLogic, tetris_view_t *tetris_view_construct(tetris_logic_t *pLogic,
tetris_playfield_t *pPl) tetris_playfield_t *pPl,
uint8_t nFirstPerson)
{ {
// memory allocation // memory allocation
assert((pLogic != NULL) && (pPl != NULL)); assert((pLogic != NULL) && (pPl != NULL));
@ -349,6 +359,16 @@ tetris_view_t *tetris_view_construct(tetris_logic_t *pLogic,
pView->modeCurrent = TETRIS_VIMO_RUNNING; pView->modeCurrent = TETRIS_VIMO_RUNNING;
pView->modeOld = TETRIS_VIMO_RUNNING; pView->modeOld = TETRIS_VIMO_RUNNING;
#ifdef GAME_TETRIS_FP
// set setpixel wrapper
if (nFirstPerson)
setpixel_wrapper = tetris_view_setpixel_fp;
else
setpixel_wrapper = setpixel;
#else
setpixel_wrapper = setpixel;
#endif
// drawing some first stuff // drawing some first stuff
clear_screen(0); clear_screen(0);
tetris_view_drawBorders(TETRIS_VIEW_COLORBORDER); tetris_view_drawBorders(TETRIS_VIEW_COLORBORDER);
@ -487,3 +507,59 @@ void tetris_view_showResults(tetris_view_t *pV)
#endif #endif
} }
#ifdef GAME_TETRIS_FP
/* Function: tetris_view_setpixel_fp
* Description: own setpixel wrapper for first person mode
* Return value: void
*/
void tetris_view_setpixel_fp(pixel p, unsigned char value) {
switch (tetris_screendir) {
case 0: setpixel(p,value); break;
case 1: setpixel((pixel){p.y,15-p.x}, value); break;
case 2: setpixel((pixel){15-p.x,15-p.y}, value); break;
case 3: setpixel((pixel){15-p.y,p.x}, value); break;
}
}
/* Function: tetris_view_rotate
* Description: rotate view for first person mode
* Return value: void
*/
void tetris_view_rotate(void) {
unsigned char plane, row, byte, shift, off, sbyte, hrow;
unsigned char new_pixmap[NUMPLANE][NUM_ROWS][LINEBYTES];
tetris_screendir = (tetris_screendir+1)%4;
// if ( NUM_ROWS != 16 || LINEBYTES != 2 ) return;
memset(&new_pixmap, 0, sizeof(new_pixmap));
for(plane=0; plane<NUMPLANE; plane++){
for(row=0;row<NUM_ROWS; row++){
for(byte=0;byte<LINEBYTES;byte++){
hrow = row%8;
shift = 7-hrow;
off = ((byte==0)?15:7);
sbyte = (row<8) ? 1 : 0;
new_pixmap[plane][row][1-byte] =
(
( ((pixmap[plane][off ][sbyte] >> shift)&1) << 7 ) |
( ((pixmap[plane][off-1][sbyte] >> shift)&1) << 6 ) |
( ((pixmap[plane][off-2][sbyte] >> shift)&1) << 5 ) |
( ((pixmap[plane][off-3][sbyte] >> shift)&1) << 4 ) |
( ((pixmap[plane][off-4][sbyte] >> shift)&1) << 3 ) |
( ((pixmap[plane][off-5][sbyte] >> shift)&1) << 2 ) |
( ((pixmap[plane][off-6][sbyte] >> shift)&1) << 1 ) |
( ((pixmap[plane][off-7][sbyte] >> shift)&1) << 0 )
);
}
}
}
memcpy(&pixmap, &new_pixmap, sizeof(pixmap));
}
#endif

View File

@ -42,7 +42,8 @@ tetris_view_t;
* Return value: pointer to a newly created view * Return value: pointer to a newly created view
*/ */
tetris_view_t *tetris_view_construct(tetris_logic_t *pLogic, tetris_view_t *tetris_view_construct(tetris_logic_t *pLogic,
tetris_playfield_t *pPl); tetris_playfield_t *pPl,
uint8_t nFirstPerson);
/* Function: tetris_view_destruct /* Function: tetris_view_destruct
@ -91,6 +92,13 @@ void tetris_view_update(tetris_view_t *pV);
*/ */
void tetris_view_showResults(tetris_view_t *pV); void tetris_view_showResults(tetris_view_t *pV);
#ifdef GAME_TETRIS_FP
/* Function: tetris_view_setpixel_fp
* Description: own setpixel wrapper for first person mode
* Return value: void
*/
void tetris_view_setpixel_fp(pixel p, unsigned char value);
#endif
#endif /*TETRIS_VIEW_H_*/ #endif /*TETRIS_VIEW_H_*/