Browse Source

saved 162 bytes

feature/2015
Christian Kroll 14 years ago
parent
commit
8963b27a65
  1. 3
      animations/bitmapscroller.c
  2. 29
      games/tetris/bucket.c
  3. 16
      games/tetris/bucket.h
  4. 12
      games/tetris/piece.c
  5. 16
      games/tetris/piece.h
  6. 24
      games/tetris/variant_std.c
  7. 23
      random/prng.c
  8. 27
      random/prng.h
  9. 19
      scrolltext/scrolltext3.c

3
animations/bitmapscroller.c

@ -87,7 +87,8 @@ static unsigned char bitmap_getAlignedChunk(bitmap_t const *const pBitmap,
/**
* This function actually draws the bitmap onto the screen. The viewport is
* This function actually draws the bitmap onto the screen. The viewport is the
* part of the bitmap which can be seen on the display.
* @param pBitmap The bitmap which shall be shown.
* @param nX The x-coordinate of the bitmap which shall be displayed at the top
* left of the viewport.

29
games/tetris/bucket.c

@ -92,15 +92,11 @@ static void tetris_bucket_hoverStatus(tetris_bucket_t *pBucket)
{
assert(pBucket != NULL);
// if the piece touches the dump we ensure that the status is "gliding"
if (tetris_bucket_collision(pBucket, pBucket->nColumn, pBucket->nRow + 1))
{
pBucket->status = TETRIS_BUS_GLIDING;
}
else
{
pBucket->status = TETRIS_BUS_HOVERING;
}
// status depends on whether the piece touches the dump or not
// NOTE: 0 == TETRIS_BUS_HOVERING, 1 == TETRIS_BUS_GLIDING,
// tetris_bucket_collision(...) either returns 0 or 1
pBucket->status = tetris_bucket_collision(pBucket, pBucket->nColumn,
pBucket->nRow + 1);
}
@ -177,7 +173,7 @@ tetris_piece_t *tetris_bucket_insertPiece(tetris_bucket_t *pBucket,
pBucket->pPiece = pPiece;
// did we already collide with something?
if (tetris_bucket_collision(pBucket, pBucket->nColumn, pBucket->nRow) == 1)
if (tetris_bucket_collision(pBucket, pBucket->nColumn, pBucket->nRow))
{
// game over man, game over!!
pBucket->status = TETRIS_BUS_GAMEOVER;
@ -243,19 +239,20 @@ void tetris_bucket_advancePiece(tetris_bucket_t *pBucket)
uint8_t tetris_bucket_movePiece(tetris_bucket_t *pBucket,
tetris_bucket_direction_t direction)
tetris_bucket_direction_t nDirection)
{
assert(pBucket != NULL);
// a piece can only be moved if it is still hovering or gliding
assert((pBucket->status == TETRIS_BUS_HOVERING) ||
(pBucket->status == TETRIS_BUS_GLIDING));
// only the values of the direction enumeration are allowed
assert((nDirection == TETRIS_BUD_LEFT) || (nDirection = TETRIS_BUD_RIGHT));
int8_t nOffset = (direction == TETRIS_BUD_LEFT) ? -1 : 1;
if (tetris_bucket_collision(pBucket, pBucket->nColumn + nOffset,
if (tetris_bucket_collision(pBucket, pBucket->nColumn + nDirection,
pBucket->nRow) == 0)
{
pBucket->nColumn += nOffset;
pBucket->nColumn += nDirection;
// are we gliding?
tetris_bucket_hoverStatus(pBucket);
@ -277,8 +274,8 @@ uint8_t tetris_bucket_rotatePiece(tetris_bucket_t *pBucket,
tetris_piece_rotate(pBucket->pPiece, rotation);
// does the rotated piece cause a collision?
if (tetris_bucket_collision(pBucket, pBucket->nColumn, pBucket->nRow) != 0)
// does the rotated piece collide with something?
if (tetris_bucket_collision(pBucket, pBucket->nColumn, pBucket->nRow))
{
// in that case we revert the rotation
tetris_piece_rotate(pBucket->pPiece, rotation == TETRIS_PC_ROT_CW ?

16
games/tetris/bucket.h

@ -20,11 +20,11 @@
// directions to which a piece can be moved
enum tetris_bucket_direction
{
TETRIS_BUD_LEFT,
TETRIS_BUD_RIGHT
TETRIS_BUD_LEFT = -1,
TETRIS_BUD_RIGHT = 1
};
#ifdef NDEBUG
typedef uint8_t tetris_bucket_direction_t;
typedef int8_t tetris_bucket_direction_t;
#else
typedef enum tetris_bucket_direction tetris_bucket_direction_t;
#endif
@ -33,11 +33,11 @@ enum tetris_bucket_direction
// status of the bucket
enum tetris_bucket_status
{
TETRIS_BUS_READY, /** ready to get next piece */
TETRIS_BUS_HOVERING, /** piece is still hovering */
TETRIS_BUS_GLIDING, /** piece is gliding on the dump */
TETRIS_BUS_DOCKED, /** piece has been docked */
TETRIS_BUS_GAMEOVER /** bucket is filled up */
TETRIS_BUS_HOVERING = 0, /** piece is hovering */
TETRIS_BUS_GLIDING = 1, /** piece is gliding on the dump */
TETRIS_BUS_DOCKED = 2, /** piece has been docked */
TETRIS_BUS_READY = 3, /** ready to get next piece */
TETRIS_BUS_GAMEOVER = 4 /** bucket is filled up */
};
#ifdef NDEBUG
typedef uint8_t tetris_bucket_status_t;

12
games/tetris/piece.c

@ -47,18 +47,6 @@ uint16_t tetris_piece_getBitmap(tetris_piece_t *pPc)
}
void tetris_piece_rotate(tetris_piece_t *pPc,
tetris_piece_rotation_t nRotation)
{
assert(pPc != NULL);
assert(nRotation < 2);
// we just rotate through the available angles in the given direction and
// wrap around (via modulo) where appropriate
pPc->angle = (pPc->angle + ((nRotation == TETRIS_PC_ROT_CW) ? 1 : 3)) % 4;
}
uint8_t tetris_piece_getAngleCount(tetris_piece_t *pPc)
{
assert(pPc != NULL);

16
games/tetris/piece.h

@ -48,8 +48,8 @@ enum tetris_piece_angle
/** rotation attributes */
enum tetris_piece_rotation
{
TETRIS_PC_ROT_CW, /**< clockwise rotation */
TETRIS_PC_ROT_CCW /**< counter clockwise rotation */
TETRIS_PC_ROT_CW = 1, /**< clockwise rotation */
TETRIS_PC_ROT_CCW = 3 /**< counter clockwise rotation */
};
#ifdef NDEBUG
typedef uint8_t tetris_piece_rotation_t;
@ -119,8 +119,16 @@ uint16_t tetris_piece_getBitmap(tetris_piece_t *pPc);
* @param pPc piece to rotate
* @param nRotation type of rotation (see tetris_piece_rotation_t)
*/
void tetris_piece_rotate(tetris_piece_t *pPc,
tetris_piece_rotation_t nRotation);
inline static void tetris_piece_rotate(tetris_piece_t *pPc,
tetris_piece_rotation_t nRotation)
{
assert(pPc != NULL);
assert(nRotation == TETRIS_PC_ROT_CW || nRotation == TETRIS_PC_ROT_CCW);
// we just rotate through the available angles in the given direction and
// wrap around (via modulo) where appropriate
pPc->angle = (pPc->angle + nRotation) % 4;
}
/**

24
games/tetris/variant_std.c

@ -74,6 +74,8 @@ void *tetris_std_construct(tetris_bucket_t *pBucket)
malloc(sizeof(tetris_standard_variant_t));
assert(pStdVariant != NULL);
memset(pStdVariant, 0, sizeof(tetris_standard_variant_t));
pStdVariant->pPreviewPiece =
tetris_piece_construct(random8() % 7, TETRIS_PC_ANGLE_0);
return pStdVariant;
}
@ -84,10 +86,7 @@ void tetris_std_destruct(void *pVariantData)
assert(pVariantData != 0);
tetris_standard_variant_t *pStdVariant =
(tetris_standard_variant_t *)pVariantData;
if (pStdVariant->pPreviewPiece != NULL)
{
tetris_piece_destruct(pStdVariant->pPreviewPiece);
}
tetris_piece_destruct(pStdVariant->pPreviewPiece);
free(pStdVariant);
}
@ -102,19 +101,10 @@ tetris_piece_t* tetris_std_choosePiece(void *pVariantData)
assert(pVariantData != 0);
tetris_standard_variant_t *pStdVariant =
(tetris_standard_variant_t *)pVariantData;
if (pStdVariant->pPreviewPiece == NULL)
{
pStdVariant->pPreviewPiece =
tetris_piece_construct(random8() % 7, TETRIS_PC_ANGLE_0);
return tetris_piece_construct(random8() % 7, TETRIS_PC_ANGLE_0);
}
else
{
tetris_piece_t *pPiece = pStdVariant->pPreviewPiece;
pStdVariant->pPreviewPiece =
tetris_piece_construct(random8() % 7, TETRIS_PC_ANGLE_0);
return pPiece;
}
tetris_piece_t *pPiece = pStdVariant->pPreviewPiece;
pStdVariant->pPreviewPiece =
tetris_piece_construct(random8() % 7, TETRIS_PC_ANGLE_0);
return pPiece;
}

23
random/prng.c

@ -6,15 +6,13 @@
*
*/
#include "noekeon.h"
#include "memxor.h"
#include <stdint.h>
#include <string.h>
#include "noekeon.h"
uint8_t random_state[16];
uint8_t random_key[16];
uint8_t random8(void){
static uint8_t sr[16];
static uint8_t i=0;
@ -28,22 +26,3 @@ uint8_t random8(void){
--i;
return sr[i];
}
void random_block(void* dest){
noekeon_enc(random_state, random_key);
memcpy(dest, random_state, 16);
}
void srandom32(uint32_t seed){
memcpy(random_key, &seed, 4);
}
void random_seed(const void* buffer){
memcpy(random_key, buffer, 16);
}
void random_add(const void* buffer){
memxor(random_key, buffer, 16);
}

27
random/prng.h

@ -10,12 +10,31 @@
#define PRNG_H_
#include <stdint.h>
#include "string.h"
#include "noekeon.h"
#include "memxor.h"
extern uint8_t random_state[16];
extern uint8_t random_key[16];
uint8_t random8(void);
void random_block(void* dest);
void srandom32(uint32_t seed);
void random_seed(const void* buffer);
void random_add(const void* buffer);
inline static void random_block(void* dest){
noekeon_enc(random_state, random_key);
memcpy(dest, random_state, 16);
}
inline static void srandom32(uint32_t seed){
memcpy(random_key, &seed, 4);
}
inline static void random_seed(const void* buffer){
memcpy(random_key, buffer, 16);
}
inline static void random_add(const void* buffer){
memxor(random_key, buffer, 16);
}
#endif /* PRNG_H_*/

19
scrolltext/scrolltext3.c

@ -53,14 +53,8 @@ static void text_setpixel(pixel p, unsigned char value ){
}
}
static void clear_text_pixmap(unsigned char value){
unsigned char y, z;
for(y=NUM_ROWS;y--;){
for(z=LINEBYTES;z--;){
(*text_pixmap)[y][z] = 0;
}
}
static void clear_text_pixmap(){
memset(text_pixmap, 0, NUM_ROWS * LINEBYTES);
}
void update_pixmap(){
@ -521,7 +515,8 @@ void scrolltext(char *str) {
fonts[0] = SCROLLTEXT_FONT;
text_pixmap = malloc(NUM_ROWS * LINEBYTES);
unsigned char auto_pixmap[NUM_ROWS][LINEBYTES];
text_pixmap = &auto_pixmap;
if(scrolltext_text[0] == 0){
strcpy_P(scrolltext_text, default_text);
@ -531,15 +526,12 @@ void scrolltext(char *str) {
blob_t *startblob=0, *aktblob, *nextblob=0;
memcpy (tmp_jmpbuf, newmode_jmpbuf, sizeof(jmp_buf));
if((ljmp_retval = setjmp(newmode_jmpbuf))){
while(startblob){
aktblob = startblob;
startblob = aktblob->next;
free(aktblob);
}
free(text_pixmap);
memcpy (newmode_jmpbuf, tmp_jmpbuf, sizeof(jmp_buf));
longjmp(newmode_jmpbuf, ljmp_retval);
}
@ -585,7 +577,7 @@ void scrolltext(char *str) {
}
aktblob = startblob;
clear_text_pixmap(0);
clear_text_pixmap();
while(aktblob){
drawBlob(aktblob);
aktblob = aktblob->next;
@ -598,6 +590,5 @@ void scrolltext(char *str) {
}while(startblob);
exit:
free(text_pixmap);
memcpy (newmode_jmpbuf, tmp_jmpbuf, sizeof(jmp_buf));
}

Loading…
Cancel
Save