2012-11-17 17:24:01 +00:00
|
|
|
#include <assert.h>
|
2008-12-03 05:40:16 +00:00
|
|
|
#include "invaders2.h"
|
2012-11-17 17:24:01 +00:00
|
|
|
|
|
|
|
/*--------------------double buffered graphics-------------------*/
|
|
|
|
|
|
|
|
static void setOffScreenPixel(offScreen_t offScreen, unsigned char x,
|
|
|
|
unsigned char y, unsigned char color)
|
|
|
|
{
|
|
|
|
assert(offScreen != 0);
|
|
|
|
assert(x < NUM_COLS);
|
|
|
|
assert(y < NUM_ROWS);
|
|
|
|
assert(color <= NUMPLANE);
|
|
|
|
|
|
|
|
offScreen[color][y][x / 8u] |= shl_table[x % 8u];
|
|
|
|
}
|
|
|
|
|
|
|
|
static void flushOffScreenBuffer(offScreen_t offScreen)
|
|
|
|
{
|
|
|
|
assert(offScreen != 0);
|
|
|
|
|
|
|
|
/* off-screen blitting borrowed from fixed point math patterns animation,
|
|
|
|
see fpmath_patterns.c (fixDrawPattern(...) for details */
|
|
|
|
unsigned char *pPixmap =
|
|
|
|
(&pixmap[NUMPLANE - 1][NUM_ROWS - 1][LINEBYTES - 1]) + 1;
|
|
|
|
unsigned char *pOffScreenDistHigh =
|
|
|
|
(&offScreen[NUMPLANE][NUM_ROWS - 1][LINEBYTES - 1]) + 1;
|
|
|
|
unsigned char *pOffScreenDistLow =
|
|
|
|
(&offScreen[NUMPLANE - 1][NUM_ROWS - 1][LINEBYTES - 1]) + 1;
|
|
|
|
while (pPixmap != &pixmap[0][0][0]) // stop at the beginning
|
|
|
|
{
|
|
|
|
*(--pPixmap) = *(--pOffScreenDistHigh);
|
|
|
|
*(--pOffScreenDistLow) |= *pOffScreenDistHigh;
|
|
|
|
*pOffScreenDistHigh = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-12-03 05:40:16 +00:00
|
|
|
/*----------------------getter/setter----------------------------*/
|
|
|
|
|
|
|
|
unsigned char getInvaderPixel(Invaders * iv, unsigned char x, unsigned char y)
|
|
|
|
{
|
2012-11-17 17:24:01 +00:00
|
|
|
if (((x - iv->pos.x) < MAX_INVADER_WIDTH) && ((x - iv->pos.x) >= 0) &&
|
|
|
|
((y - iv->pos.y) < MAX_INVADER_HEIGHT) && ((y - iv->pos.y) >= 0))
|
2008-12-03 05:40:16 +00:00
|
|
|
{
|
|
|
|
return iv->map[x - iv->pos.x][y - iv->pos.y];
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
void setInvaderPixel(Invaders * iv, unsigned char x, unsigned char y,
|
|
|
|
unsigned char val)
|
|
|
|
{
|
2012-11-17 17:24:01 +00:00
|
|
|
if (((x - iv->pos.x) < MAX_INVADER_WIDTH) && ((x - iv->pos.x) >= 0) &&
|
|
|
|
((y - iv->pos.y) < MAX_INVADER_HEIGHT) && ((y - iv->pos.y) >= 0))
|
2008-12-03 05:40:16 +00:00
|
|
|
{
|
|
|
|
iv->map[x - iv->pos.x][y - iv->pos.y] = val;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-11-17 17:24:01 +00:00
|
|
|
void setGuardPixel(unsigned char *guards, unsigned char x, unsigned char y,
|
|
|
|
unsigned char val)
|
2008-12-03 05:40:16 +00:00
|
|
|
{
|
2012-11-17 17:24:01 +00:00
|
|
|
assert(val <= NUMPLANE);
|
|
|
|
|
|
|
|
if (x < NUM_COLS && y == GUARD_LINE)
|
|
|
|
{
|
2008-12-03 05:40:16 +00:00
|
|
|
guards[x] = val;
|
2012-11-17 17:24:01 +00:00
|
|
|
}
|
2008-12-03 05:40:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*----------------------drawing Method---------------------------*/
|
|
|
|
|
2012-11-17 17:24:01 +00:00
|
|
|
void draw(offScreen_t offScreen, Invaders * iv, Spaceship * sc, Player * pl,
|
|
|
|
Cannon * cn, unsigned char *guards, pixel *st, pixel * shot)
|
2008-12-03 05:40:16 +00:00
|
|
|
{
|
2011-01-30 22:47:44 +00:00
|
|
|
unsigned char x, y;
|
2008-12-03 05:40:16 +00:00
|
|
|
|
|
|
|
/*---SPACESHIP---*/
|
2012-11-17 17:24:01 +00:00
|
|
|
if (sc->pos < NUM_COLS)
|
2008-12-03 05:40:16 +00:00
|
|
|
{
|
2012-11-17 17:24:01 +00:00
|
|
|
setOffScreenPixel(offScreen, sc->pos, SPACESHIP_LINE, sc->lives);
|
2008-12-03 05:40:16 +00:00
|
|
|
}
|
2012-11-17 17:24:01 +00:00
|
|
|
if ((sc->pos > 0) && ((sc->pos - 1) < NUM_COLS))
|
2008-12-03 05:40:16 +00:00
|
|
|
{
|
2012-11-17 17:24:01 +00:00
|
|
|
setOffScreenPixel(offScreen, sc->pos - 1, SPACESHIP_LINE, sc->lives);
|
2008-12-03 05:40:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*---INVADERS--*/
|
2011-01-30 22:47:44 +00:00
|
|
|
for (y = MAX_INVADER_HEIGHT; y--;)
|
2008-12-03 05:40:16 +00:00
|
|
|
{
|
2011-01-30 22:47:44 +00:00
|
|
|
for (x = MAX_INVADER_WIDTH; x--;)
|
2008-12-03 05:40:16 +00:00
|
|
|
{
|
2012-11-17 17:24:01 +00:00
|
|
|
if ((x + iv->pos.x >= 0) && (x + iv->pos.x < NUM_COLS) &&
|
|
|
|
(y + iv->pos.y < NUM_ROWS))
|
|
|
|
{
|
|
|
|
setOffScreenPixel(offScreen, x + iv->pos.x, y + iv->pos.y,
|
|
|
|
iv->map[x][y]);
|
|
|
|
}
|
2008-12-03 05:40:16 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*---GUARDS---*/
|
2012-11-17 17:24:01 +00:00
|
|
|
for (x = NUM_COLS; x--;)
|
2008-12-03 05:40:16 +00:00
|
|
|
{
|
2012-11-17 17:24:01 +00:00
|
|
|
setOffScreenPixel(offScreen, x, GUARD_LINE, guards[x]);
|
2008-12-03 05:40:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*---SHOTS--*/
|
2011-01-30 22:47:44 +00:00
|
|
|
unsigned char i;
|
|
|
|
for (i = MAX_SHOTS; i--;)
|
2008-12-03 05:40:16 +00:00
|
|
|
{
|
2012-11-17 17:24:01 +00:00
|
|
|
if (st[i].x < NUM_COLS && st[i].y < NUM_ROWS)
|
2008-12-03 05:40:16 +00:00
|
|
|
{
|
2012-11-17 17:24:01 +00:00
|
|
|
setOffScreenPixel(offScreen, st[i].x, st[i].y, 3);
|
2008-12-03 05:40:16 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-11-17 17:24:01 +00:00
|
|
|
/*draw player shot*/
|
2008-12-03 05:40:16 +00:00
|
|
|
if (!(cn->ready))
|
|
|
|
{
|
2012-11-17 17:24:01 +00:00
|
|
|
setOffScreenPixel(offScreen, shot->x, shot->y, 3);
|
2008-12-03 05:40:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*-- CANNON --*/
|
2012-11-17 17:24:01 +00:00
|
|
|
if (cn->pos > 0) /* right border */
|
2008-12-03 05:40:16 +00:00
|
|
|
{
|
2012-11-17 17:24:01 +00:00
|
|
|
setOffScreenPixel(offScreen, cn->pos - 1, NUM_ROWS - 1, pl->lives);
|
2008-12-03 05:40:16 +00:00
|
|
|
}
|
2012-11-17 17:24:01 +00:00
|
|
|
if (cn->pos < NUM_COLS)
|
2008-12-03 05:40:16 +00:00
|
|
|
{
|
2012-11-17 17:24:01 +00:00
|
|
|
setOffScreenPixel(offScreen, cn->pos, NUM_ROWS - 2, pl->lives);
|
|
|
|
setOffScreenPixel(offScreen, cn->pos, NUM_ROWS - 1, pl->lives);
|
2008-12-03 05:40:16 +00:00
|
|
|
}
|
2012-11-17 17:24:01 +00:00
|
|
|
if (cn->pos < (NUM_COLS - 1)) /* left border */
|
2008-12-03 05:40:16 +00:00
|
|
|
{
|
2012-11-17 17:24:01 +00:00
|
|
|
setOffScreenPixel(offScreen, cn->pos + 1, NUM_ROWS - 1, pl->lives);
|
2008-12-03 05:40:16 +00:00
|
|
|
}
|
|
|
|
|
2012-11-17 17:24:01 +00:00
|
|
|
flushOffScreenBuffer(offScreen);
|
2008-12-03 05:40:16 +00:00
|
|
|
}
|