1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205
|
/* $Id: gameobject.c,v 1.3 2002/03/02 21:10:55 sverrehu Exp $ */
/**************************************************************************
*
* FILE gameobject.c
* MODULE OF snake4 - game of snake eating fruit
*
* DESCRIPTION
*
* WRITTEN BY Sverre H. Huseby <shh@thathost.com>
*
**************************************************************************/
#include <stdlib.h>
#include <X11/Xlib.h>
#include <shhmsg.h>
#include "board.h"
#include "gameobject.h"
/**************************************************************************
* *
* P R I V A T E D A T A *
* *
**************************************************************************/
static GameObject *list = NULL;
/**************************************************************************
* *
* P U B L I C F U N C T I O N S *
* *
**************************************************************************/
GameObject *
goNew(int type)
{
GameObject *go;
if ((go = malloc(sizeof(GameObject))) == NULL)
msgFatal("out of memory\n");
go->type = type;
go->active = 0;
go->x = go->y = 0;
go->pixmaps = NULL;
go->numPixmaps = 0;
go->currPixmap = 0;
go->ticksPixmap = NULL;
go->tickCount = 0;
go->allPixmapsCallback = NULL;
go->deactivateCallback = NULL;
/* include in list of handeled objects */
go->next = list;
if (list)
list->prev = go;
go->prev = NULL;
list = go;
return go;
}
void
goFree(GameObject *go)
{
if (go->prev == NULL) {
/* first element in list */
list = go->next;
if (go->next)
go->next->prev = NULL;
} else {
go->prev->next = go->next;
if (go->next)
go->next->prev = go->prev;
}
free(go->pixmaps);
free(go->ticksPixmap);
free(go);
}
void
goAddPixmap(GameObject *go, Pixmap pix, int ticks)
{
if (!go->numPixmaps) {
go->pixmaps = malloc(sizeof(Pixmap));
go->ticksPixmap = malloc(sizeof(int));
} else {
go->pixmaps
= realloc(go->pixmaps, (go->numPixmaps + 1) * sizeof(Pixmap));
go->ticksPixmap
= realloc(go->ticksPixmap, (go->numPixmaps + 1) * sizeof(int));
}
if (go->pixmaps == NULL || go->ticksPixmap == NULL)
msgFatal("out of memory\n");
go->pixmaps[go->numPixmaps] = pix;
go->ticksPixmap[go->numPixmaps] = ticks;
++go->numPixmaps;
}
void
goHandleTick(void)
{
GameObject *go;
go = list;
while (go) {
if (go->active && go->numPixmaps > 1) {
if (++go->tickCount >= go->ticksPixmap[go->currPixmap]) {
go->tickCount = 0;
if (++go->currPixmap >= go->numPixmaps) {
go->currPixmap = 0;
if (go->allPixmapsCallback)
go->allPixmapsCallback(go);
}
if (go->active) { /* may have been changed by callback */
boardClearBlock(go->x, go->y);
boardDrawBlock(go->pixmaps[go->currPixmap], go->x, go->y);
}
}
}
go = go->next;
}
}
void
goMove(GameObject *go, int x, int y)
{
int oldx, oldy;
GameObject *go2;
oldx = go->x;
oldy = go->y;
go->x = x;
go->y = y;
if (go->active) {
if ((go2 = goGetObjectAt(oldx, oldy)) != NULL)
boardDrawBlock(go2->pixmaps[go2->currPixmap], go2->x, go2->y);
else
boardClearBlock(oldx, oldy);
boardDrawBlock(go->pixmaps[go->currPixmap], go->x, go->y);
}
}
void
goActivate(GameObject *go)
{
if (go->active)
return;
go->active = 1;
goDraw(go);
}
void
goDeactivate(GameObject *go)
{
if (!go->active)
return;
go->active = 0;
if (go->deactivateCallback)
go->deactivateCallback(go);
goDraw(go);
}
void
goDraw(GameObject *go)
{
GameObject *go2;
if (go->active)
boardDrawBlock(go->pixmaps[go->currPixmap], go->x, go->y);
else {
if ((go2 = goGetObjectAt(go->x, go->y)) != NULL)
boardDrawBlock(go2->pixmaps[go2->currPixmap], go2->x, go2->y);
else
boardClearBlock(go->x, go->y);
}
}
void
goDrawAll(void)
{
GameObject *go;
go = list;
while (go) {
if (go->active)
boardDrawBlock(go->pixmaps[go->currPixmap], go->x, go->y);
go = go->next;
}
}
GameObject *
goGetObjectAt(int x, int y)
{
GameObject *go;
go = list;
while (go) {
if (go->active && go->x == x && go->y == y)
return go;
go = go->next;
}
return NULL;
}
|