[56] | 1 | /* |
---|
| 2 | * Abuse - dark 2D side-scrolling platform game |
---|
| 3 | * Copyright (c) 1995 Crack dot Com |
---|
[494] | 4 | * Copyright (c) 2005-2011 Sam Hocevar <sam@hocevar.net> |
---|
[56] | 5 | * |
---|
| 6 | * This software was released into the Public Domain. As with most public |
---|
[555] | 7 | * domain software, no warranty is made or implied by Crack dot Com, by |
---|
| 8 | * Jonathan Clark, or by Sam Hocevar. |
---|
[56] | 9 | */ |
---|
| 10 | |
---|
[555] | 11 | #if defined HAVE_CONFIG_H |
---|
| 12 | # include "config.h" |
---|
| 13 | #endif |
---|
[56] | 14 | |
---|
[512] | 15 | #include "common.h" |
---|
| 16 | |
---|
[481] | 17 | #include "particle.h" |
---|
| 18 | #include "view.h" |
---|
| 19 | #include "lisp.h" |
---|
| 20 | #include "cache.h" |
---|
| 21 | #include "jrand.h" |
---|
[2] | 22 | |
---|
| 23 | |
---|
| 24 | static int total_pseqs=0; |
---|
| 25 | static part_sequence **pseqs=NULL; |
---|
| 26 | static part_animation *first_anim=NULL,*last_anim=NULL; |
---|
| 27 | |
---|
| 28 | void free_pframes() |
---|
| 29 | { |
---|
[494] | 30 | for (int i=0; i<total_pseqs; i++) |
---|
[2] | 31 | delete pseqs[i]; |
---|
| 32 | if (total_pseqs) |
---|
[129] | 33 | free(pseqs); |
---|
[2] | 34 | } |
---|
| 35 | |
---|
| 36 | part_frame::~part_frame() |
---|
| 37 | { |
---|
[129] | 38 | free(data); |
---|
[2] | 39 | } |
---|
| 40 | |
---|
| 41 | void add_panim(int id, long x, long y, int dir) |
---|
| 42 | { |
---|
| 43 | CONDITION(id>=0 && id<total_pseqs,"bad id for particle animation"); |
---|
| 44 | part_animation *pan=new part_animation(pseqs[id],x,y,dir,NULL); |
---|
| 45 | if (!first_anim) |
---|
| 46 | first_anim=last_anim=pan; |
---|
| 47 | else |
---|
| 48 | { |
---|
| 49 | last_anim->next=pan; |
---|
| 50 | last_anim=pan; |
---|
[115] | 51 | } |
---|
[2] | 52 | } |
---|
| 53 | |
---|
| 54 | void delete_panims() |
---|
| 55 | { |
---|
| 56 | while (first_anim) |
---|
| 57 | { |
---|
| 58 | last_anim=first_anim; |
---|
| 59 | first_anim=first_anim->next; |
---|
| 60 | delete last_anim; |
---|
| 61 | } |
---|
| 62 | last_anim=NULL; |
---|
| 63 | } |
---|
| 64 | |
---|
| 65 | int defun_pseq(void *args) |
---|
| 66 | { |
---|
[492] | 67 | LSymbol *sym=(LSymbol *)lcar(args); |
---|
[2] | 68 | if (item_type(sym)!=L_SYMBOL) |
---|
| 69 | { |
---|
[493] | 70 | ((LObject *)args)->Print(); |
---|
[2] | 71 | printf("expecting first arg to def-particle to be a symbol!\n"); |
---|
| 72 | exit(0); |
---|
| 73 | } |
---|
[635] | 74 | LSpace *sp = LSpace::Current; |
---|
| 75 | LSpace::Current = &LSpace::Perm; |
---|
[484] | 76 | sym->SetNumber(total_pseqs); // set the symbol value to the object number |
---|
[635] | 77 | LSpace::Current = sp; |
---|
[129] | 78 | pseqs=(part_sequence **)realloc(pseqs,sizeof(part_sequence *)*(total_pseqs+1)); |
---|
[2] | 79 | |
---|
| 80 | args=lcdr(args); |
---|
| 81 | pseqs[total_pseqs]=new part_sequence(args); |
---|
[115] | 82 | total_pseqs++; |
---|
[2] | 83 | return total_pseqs; |
---|
| 84 | } |
---|
| 85 | |
---|
| 86 | extern int total_files_open; |
---|
| 87 | |
---|
| 88 | part_sequence::part_sequence(void *args) |
---|
| 89 | { |
---|
[115] | 90 | char *fn=lstring_value(lcar(args)); |
---|
[2] | 91 | bFILE *fp=open_file(fn,"rb"); |
---|
| 92 | if (fp->open_failure()) |
---|
| 93 | { |
---|
| 94 | delete fp; |
---|
[493] | 95 | ((LObject *)args)->Print(); |
---|
[2] | 96 | fprintf(stderr,"\nparticle sequence : Unable to open %s for reading\n",fn); |
---|
| 97 | fprintf(stderr,"total files open=%d\n",total_files_open); |
---|
| 98 | |
---|
| 99 | FILE *fp=fopen(fn,"rb"); |
---|
| 100 | printf("convet = %d\n",fp!=NULL); |
---|
| 101 | exit(1); |
---|
| 102 | } |
---|
| 103 | |
---|
| 104 | // count how many frames are in the file |
---|
| 105 | spec_directory sd(fp); |
---|
| 106 | delete fp; |
---|
| 107 | tframes=0; |
---|
| 108 | int i=0; |
---|
[494] | 109 | for (; i<sd.total; i++) |
---|
[2] | 110 | if (sd.entries[i]->type==SPEC_PARTICLE) tframes++; |
---|
[129] | 111 | frames=(int *)malloc(sizeof(int)*tframes); |
---|
[115] | 112 | |
---|
[2] | 113 | int on=0; |
---|
[494] | 114 | for (i=0; i<sd.total; i++) |
---|
[115] | 115 | if (sd.entries[i]->type==SPEC_PARTICLE) |
---|
[123] | 116 | frames[on++]=cache.reg(fn,sd.entries[i]->name,SPEC_PARTICLE,1); |
---|
[2] | 117 | |
---|
| 118 | } |
---|
| 119 | |
---|
| 120 | part_frame::part_frame(bFILE *fp) |
---|
| 121 | { |
---|
[17] | 122 | t=fp->read_uint32(); |
---|
[129] | 123 | data=(part *)malloc(sizeof(part)*t); |
---|
[2] | 124 | x1=y1=100000; x2=y2=-100000; |
---|
[494] | 125 | for (int i=0; i<t; i++) |
---|
[2] | 126 | { |
---|
[17] | 127 | int16_t x=fp->read_uint16(); |
---|
| 128 | int16_t y=fp->read_uint16(); |
---|
[2] | 129 | if (x<x1) x1=x; |
---|
| 130 | if (y<y1) y1=y; |
---|
| 131 | if (x>x2) x2=x; |
---|
| 132 | if (y>y2) y2=x; |
---|
| 133 | data[i].x=x; |
---|
[115] | 134 | data[i].y=y; |
---|
| 135 | data[i].color=fp->read_uint8(); |
---|
[2] | 136 | } |
---|
| 137 | } |
---|
| 138 | |
---|
| 139 | void tick_panims() |
---|
| 140 | { |
---|
| 141 | part_animation *last=NULL; |
---|
[494] | 142 | for (part_animation *p=first_anim; p; ) |
---|
[2] | 143 | { |
---|
| 144 | p->frame++; |
---|
| 145 | if (p->frame>=p->seq->tframes) |
---|
| 146 | { |
---|
| 147 | if (last) |
---|
| 148 | last->next=p->next; |
---|
| 149 | else first_anim=first_anim->next; |
---|
| 150 | if (last_anim==p) last_anim=last; |
---|
| 151 | part_animation *d=p; |
---|
| 152 | p=p->next; |
---|
| 153 | delete d; |
---|
[115] | 154 | } else |
---|
[2] | 155 | { |
---|
| 156 | last=p; |
---|
| 157 | p=p->next; |
---|
| 158 | } |
---|
| 159 | |
---|
| 160 | } |
---|
| 161 | } |
---|
| 162 | |
---|
| 163 | void draw_panims(view *v) |
---|
| 164 | { |
---|
[494] | 165 | for (part_animation *p=first_anim; p; p=p->next) |
---|
[2] | 166 | { |
---|
[674] | 167 | cache.part(p->seq->frames[p->frame])->draw(main_screen,p->x-v->xoff()+v->m_aa.x,p->y-v->yoff()+v->m_aa.y,p->dir); |
---|
[2] | 168 | } |
---|
| 169 | } |
---|
| 170 | |
---|
| 171 | void part_frame::draw(image *screen, int x, int y, int dir) |
---|
| 172 | { |
---|
[682] | 173 | ivec2 caa, cbb; |
---|
[665] | 174 | screen->GetClip(caa, cbb); |
---|
[2] | 175 | |
---|
[665] | 176 | if (x + x1 >= cbb.x || x + x2 < caa.x || y + y1 >= cbb.y || y + y2 < caa.y) |
---|
| 177 | return; |
---|
| 178 | |
---|
[2] | 179 | part *pon=data; |
---|
[665] | 180 | caa.y -= y; |
---|
| 181 | cbb.y -= y; |
---|
[2] | 182 | |
---|
| 183 | int i=t; |
---|
[665] | 184 | while (i && pon->y<caa.y) { pon++; i--; } |
---|
[2] | 185 | if (!i) return ; |
---|
[515] | 186 | screen->Lock(); |
---|
[2] | 187 | if (dir>0) |
---|
| 188 | { |
---|
[665] | 189 | while (i && pon->y < cbb.y) |
---|
[2] | 190 | { |
---|
| 191 | long dx=x-pon->x; |
---|
[665] | 192 | if (dx >= caa.x && dx < cbb.x) |
---|
[2] | 193 | *(screen->scan_line(pon->y+y)+dx)=pon->color; |
---|
| 194 | i--; |
---|
| 195 | pon++; |
---|
[115] | 196 | } |
---|
[2] | 197 | } else |
---|
| 198 | { |
---|
[665] | 199 | while (i && pon->y < cbb.y) |
---|
[2] | 200 | { |
---|
| 201 | long dx=pon->x+x; |
---|
[665] | 202 | if (dx >= caa.x && dx < cbb.x) |
---|
[2] | 203 | *(screen->scan_line(pon->y+y)+dx)=pon->color; |
---|
| 204 | i--; |
---|
| 205 | pon++; |
---|
[115] | 206 | } |
---|
[2] | 207 | } |
---|
[515] | 208 | screen->Unlock(); |
---|
[2] | 209 | } |
---|
| 210 | |
---|
[682] | 211 | void ScatterLine(ivec2 p1, ivec2 p2, int c, int s) |
---|
[2] | 212 | { |
---|
[682] | 213 | ivec2 caa, cbb; |
---|
[665] | 214 | main_screen->GetClip(caa, cbb); |
---|
[2] | 215 | |
---|
[672] | 216 | int t = 1 + Max(abs(p2.x - p1.x), abs(p2.y - p1.y)); |
---|
| 217 | int xo = p1.x << 16, |
---|
| 218 | yo = p1.y << 16, |
---|
| 219 | dx = ((p2.x - p1.x) << 16) / t, |
---|
| 220 | dy = ((p2.y - p1.y) << 16) / t; |
---|
[2] | 221 | |
---|
[672] | 222 | int xm = (1 << s); |
---|
| 223 | int ym = (1 << s); |
---|
| 224 | s = (15 - s); |
---|
[2] | 225 | |
---|
[643] | 226 | main_screen->Lock(); |
---|
[672] | 227 | while(t--) |
---|
[115] | 228 | { |
---|
[672] | 229 | int x = (xo >> 16) + (jrand() >> s) - xm; |
---|
| 230 | int y = (yo >> 16) + (jrand() >> s) - ym; |
---|
| 231 | if(!(x < caa.x || y < caa.y || x >= cbb.x || y >= cbb.y)) |
---|
[115] | 232 | { |
---|
[672] | 233 | *(main_screen->scan_line(y) + x) = c; |
---|
[115] | 234 | } |
---|
| 235 | xo += dx; |
---|
| 236 | yo += dy; |
---|
| 237 | } |
---|
[643] | 238 | main_screen->Unlock(); |
---|
[2] | 239 | } |
---|
| 240 | |
---|
[682] | 241 | void AScatterLine(ivec2 p1, ivec2 p2, int c1, int c2, int s) |
---|
[2] | 242 | { |
---|
[682] | 243 | ivec2 caa, cbb; |
---|
[665] | 244 | main_screen->GetClip(caa, cbb); |
---|
[2] | 245 | |
---|
[672] | 246 | int t = 1 + Max(abs(p2.x - p1.x), abs(p2.y - p1.y)); |
---|
| 247 | int xo = p1.x << 16, |
---|
| 248 | yo = p1.y << 16, |
---|
| 249 | dx = ((p2.x - p1.x) << 16) / t, |
---|
| 250 | dy = ((p2.y - p1.y) << 16) / t; |
---|
[2] | 251 | |
---|
[672] | 252 | int xm = (1 << s); |
---|
| 253 | int ym = (1 << s); |
---|
| 254 | s = (15 - s); |
---|
[2] | 255 | |
---|
[643] | 256 | main_screen->Lock(); |
---|
[2] | 257 | |
---|
[643] | 258 | int w = main_screen->Size().x; |
---|
[115] | 259 | uint8_t *addr; |
---|
| 260 | |
---|
[672] | 261 | while(t--) |
---|
[115] | 262 | { |
---|
[672] | 263 | int x = (xo >> 16) + (jrand() >> s) - xm; |
---|
| 264 | int y = (yo >> 16) + (jrand() >> s) - ym; |
---|
[518] | 265 | // FIXME: these clip values seemed wrong to me before the GetClip |
---|
| 266 | // refactoring. |
---|
[672] | 267 | if(!(x <= caa.x || y <= caa.y || x >= cbb.x - 1 || y >= cbb.y - 1)) |
---|
[115] | 268 | { |
---|
[672] | 269 | addr = main_screen->scan_line(y) + x; |
---|
[115] | 270 | *addr = c1; |
---|
| 271 | *(addr + w) = c2; |
---|
| 272 | *(addr - w) = c2; |
---|
| 273 | *(addr - 1) = c2; |
---|
| 274 | *(addr + 1) = c2; |
---|
| 275 | } |
---|
| 276 | xo += dx; |
---|
| 277 | yo += dy; |
---|
| 278 | } |
---|
| 279 | |
---|
[643] | 280 | main_screen->Unlock(); |
---|
[2] | 281 | } |
---|
[115] | 282 | |
---|