borgware-2d/games/breakout/playfield.c

128 lines
2.4 KiB
C
Raw Normal View History

2010-01-21 14:45:16 +00:00
/*
* This program is free software; you can redistribute it and/or modify it
* under the terms and conditions of the GNU General Public License,
* version 2, as published by the Free Software Foundation.
*
* This program is distributed in the hope it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
* more details.
*
* You should have received a copy of the GNU General Public License along with
* this program; if not, write to the Free Software Foundation, Inc., 59 Temple
* Place - Suite 330, Boston, MA 02111-1307 USA.
*
* Author & Copyright (C) 2010: Soeren Heisrath (forename@surename.org)
*
*/
#include "common.h"
2010-01-15 16:02:31 +00:00
static enum game_field_t playfield[NUM_COLS][NUM_ROWS];
2010-01-15 16:02:31 +00:00
void playfield_set (uint8_t in_x, uint8_t in_y, enum game_field_t in_field)
{
2010-01-21 13:40:40 +00:00
if (in_x >= NUM_ROWS || in_y >= NUM_COLS)
{
return;
}
2010-01-15 16:02:31 +00:00
playfield[in_x][in_y] = in_field;
}
void brick_damage (uint8_t in_x, uint8_t in_y)
{
enum game_field_t newtype;
2010-01-21 13:40:40 +00:00
if (playfield[in_x][in_y] >= bs || playfield[in_x][in_y] == 0)
2010-01-15 12:31:58 +00:00
return;
2010-01-21 13:40:40 +00:00
2010-01-19 09:55:28 +00:00
playfield[in_x][in_y] -= 1;
score_add (1);
}
2010-01-19 09:55:28 +00:00
uint8_t check_bounce (int8_t in_x, int8_t in_y)
{
2010-01-19 09:55:28 +00:00
uint8_t ov = 0;
/* overflow check */
2010-01-19 09:55:28 +00:00
if (in_x >= NUM_ROWS || in_x < 0)
ov |= BOUNCE_X;
2010-01-19 09:55:28 +00:00
if (in_y >= NUM_COLS || in_y < 0)
ov |= BOUNCE_Y;
2010-01-21 13:40:40 +00:00
if (ov)
{
return ov;
}
/* collisions with real objects */
2010-01-19 09:55:28 +00:00
switch (playfield[abs(in_x)][abs(in_y)])
{
2010-01-15 12:31:58 +00:00
case b2:
case b3:
case b1:
brick_damage (in_x, in_y);
/* intentional fallthrough */
2010-01-15 12:31:58 +00:00
case bs:
2010-01-21 13:40:40 +00:00
ov |= BOUNCE_BRICK;
break;
2010-01-15 12:31:58 +00:00
/* bouncing on the rebound needs special care */
case rb:
2010-01-21 13:40:40 +00:00
ov |= BOUNCE_Y | BOUNCE_REBOUND;
break;
2010-01-19 09:55:28 +00:00
case sp:
case bl:
default:
2010-01-21 13:40:40 +00:00
break;
2010-01-19 09:55:28 +00:00
}
2010-01-21 13:40:40 +00:00
return ov;
}
/* this is the actual draw function for a single field
*/
2010-01-15 16:02:31 +00:00
static inline void draw_single_field (uint8_t in_x, uint8_t in_y, enum game_field_t in_f)
{
2010-01-15 16:02:31 +00:00
pixel tmp;
uint8_t b;
switch (in_f)
{
case b1:
2010-01-15 16:02:31 +00:00
b = 1;
break;
case rb:
case b2:
2010-01-15 16:02:31 +00:00
b = 2;
break;
case b3:
case bl:
case bs:
2010-01-15 16:02:31 +00:00
b = 3;
break;
default: /* this includes freespace */
2010-01-15 16:02:31 +00:00
b = 0;
break;
}
2010-01-15 16:02:31 +00:00
tmp.x = in_x;
tmp.y = in_y;
setpixel (tmp, b);
}
void playfield_draw ()
{
uint8_t x,y;
for (x=0;x<NUM_ROWS;x++)
{
for (y=0;y<NUM_COLS;y++)
{
2010-01-15 14:42:46 +00:00
draw_single_field (x,y, playfield[x][y]);
}
}
}