1 /* Project 16 Source Code~
2 * Copyright (C) 2012-2015 sparky4 & pngwen & andrius4669
4 * This file is part of Project 16.
6 * Project 16 is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 3 of the License, or
9 * (at your option) any later version.
11 * Project 16 is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>, or
18 * write to the Free Software Foundation, Inc., 51 Franklin Street,
19 * Fifth Floor, Boston, MA 02110-1301 USA.
25 #include "src/lib/scroll16.h"
27 void walk(map_view_t *pip, player_t *player, word pn)
29 #define INC_PER_FRAME if(player[pn].q&1) player[pn].persist_aniframe++; if(player[pn].persist_aniframe>4) player[pn].persist_aniframe = 1;
31 switch(player[pn].info.dir)
34 if(pip[0].tx >= 0 && pip[0].tx+20 < pip[0].map->width && player[pn].tx == pip[0].tx + 10 &&
35 !(pip[0].map->data[(player[pn].tx)+(pip[0].map->width*(player[pn].ty-1))] == 0))//!(player[pn].tx+1 == TRIGGX && player[pn].ty == TRIGGY)) //collision detection!
37 if(player[pn].q<=(TILEWH/(player[pn].speed)))
40 animatePlayer(pip, player, pn, 1);
41 mapScrollRight(pip, player, 0, pn);
42 mapScrollRight(pip, player, 1, pn);
43 modexShowPage(pip[1].page);
45 } else { player[pn].q = 1; player[pn].info.dir = 0; player[pn].tx++; }
47 else if(player[pn].tx < pip[0].map->width && !(pip[0].map->data[(player[pn].tx)+(pip[0].map->width*(player[pn].ty-1))] == 0))//!(player[pn].tx+1 == TRIGGX && player[pn].ty == TRIGGY))
49 if(player[pn].q<=(TILEWH/(player[pn].speed)))
52 player[pn].x+=(player[pn].speed);
53 animatePlayer(pip, player, pn, 0);
54 modexShowPage(pip[1].page);
56 } else { player[pn].q = 1; player[pn].info.dir = 0; player[pn].tx++; }
60 modexCopyPageRegion(pip[1].page, pip[0].page, player[pn].x-4, player[pn].y-TILEWH, player[pn].x-4, player[pn].y-TILEWH, 24, 32);
61 modexDrawSpriteRegion(pip[1].page, player[pn].x-4, player[pn].y-TILEWH, 24, 32, 24, 32, &player[pn].data);
62 modexShowPage(pip[1].page);
63 player[pn].info.dir = 0;
65 player[pn].triggerx = player[pn].tx+1;
66 player[pn].triggery = player[pn].ty;
71 if(pip[0].tx > 0 && pip[0].tx+20 <= pip[0].map->width && player[pn].tx == pip[0].tx + 10 &&
72 !(pip[0].map->data[(player[pn].tx-2)+(pip[0].map->width*(player[pn].ty-1))] == 0))//!(player[pn].tx-1 == TRIGGX && player[pn].ty == TRIGGY)) //collision detection!
74 if(player[pn].q<=(TILEWH/(player[pn].speed)))
77 animatePlayer(pip, player, pn, 1);
78 mapScrollLeft(pip, player, 0, pn);
79 mapScrollLeft(pip, player, 1, pn);
80 modexShowPage(pip[1].page);
82 } else { player[pn].q = 1; player[pn].info.dir = 0; player[pn].tx--; }
84 else if(player[pn].tx > 1 && !(pip[0].map->data[(player[pn].tx-2)+(pip[0].map->width*(player[pn].ty-1))] == 0))//!(player[pn].tx-1 == TRIGGX && player[pn].ty == TRIGGY))
86 if(player[pn].q<=(TILEWH/(player[pn].speed)))
89 player[pn].x-=(player[pn].speed);
90 animatePlayer(pip, player, pn, 0);
91 modexShowPage(pip[1].page);
93 } else { player[pn].q = 1; player[pn].info.dir = 0; player[pn].tx--; }
97 modexCopyPageRegion(pip[1].page, pip[0].page, player[pn].x-4, player[pn].y-TILEWH, player[pn].x-4, player[pn].y-TILEWH, 24, 32);
98 modexDrawSpriteRegion(pip[1].page, player[pn].x-4, player[pn].y-TILEWH, 24, 96, 24, 32, &player[pn].data);
99 modexShowPage(pip[1].page);
100 player[pn].info.dir = 0;
102 player[pn].triggerx = player[pn].tx-1;
103 player[pn].triggery = player[pn].ty;
108 if(pip[0].ty >= 0 && pip[0].ty+15 < pip[0].map->height && player[pn].ty == pip[0].ty + 8 &&
109 !(pip[0].map->data[(player[pn].tx-1)+(pip[0].map->width*(player[pn].ty))] == 0))//!(player[pn].tx == TRIGGX && player[pn].ty+1 == TRIGGY)) //collision detection!
111 if(player[pn].q<=(TILEWH/(player[pn].speed)))
114 animatePlayer(pip, player, pn, 1);
115 mapScrollDown(pip, player, 0, pn);
116 mapScrollDown(pip, player, 1, pn);
117 modexShowPage(pip[1].page);
119 } else { player[pn].q = 1; player[pn].info.dir = 0; player[pn].ty++; }
121 else if(player[pn].ty < pip[0].map->height && !(pip[0].map->data[(player[pn].tx-1)+(pip[0].map->width*(player[pn].ty))] == 0))//!(player[pn].tx == TRIGGX && player[pn].ty+1 == TRIGGY))
123 if(player[pn].q<=(TILEWH/(player[pn].speed)))
126 player[pn].y+=(player[pn].speed);
127 animatePlayer(pip, player, pn, 0);
128 modexShowPage(pip[1].page);
130 } else { player[pn].q = 1; player[pn].info.dir = 0; player[pn].ty++; }
134 modexCopyPageRegion(pip[1].page, pip[0].page, player[pn].x-4, player[pn].y-TILEWH, player[pn].x-4, player[pn].y-TILEWH, 24, 32);
135 modexDrawSpriteRegion(pip[1].page, player[pn].x-4, player[pn].y-TILEWH, 24, 64, 24, 32, &player[pn].data);
136 modexShowPage(pip[1].page);
137 player[pn].info.dir = 0;
139 player[pn].triggerx = player[pn].tx;
140 player[pn].triggery = player[pn].ty+1;
145 if(pip[0].ty > 0 && pip[0].ty+15 <= pip[0].map->height && player[pn].ty == pip[0].ty + 8 &&
146 !(pip[0].map->data[(player[pn].tx-1)+(pip[0].map->width*(player[pn].ty-2))] == 0))//!(player[pn].tx == TRIGGX && player[pn].ty-1 == TRIGGY)) //collision detection!
148 if(player[pn].q<=(TILEWH/(player[pn].speed)))
151 animatePlayer(pip, player, pn, 1);
152 mapScrollUp(pip, player, 0, pn);
153 mapScrollUp(pip, player, 1, pn);
154 modexShowPage(pip[1].page);
156 } else { player[pn].q = 1; player[pn].info.dir = 0; player[pn].ty--; }
158 else if(player[pn].ty > 1 && !(pip[0].map->data[(player[pn].tx-1)+(pip[0].map->width*(player[pn].ty-2))] == 0))//!(player[pn].tx == TRIGGX && player[pn].ty-1 == TRIGGY))
160 if(player[pn].q<=(TILEWH/(player[pn].speed)))
163 player[pn].y-=(player[pn].speed);
164 animatePlayer(pip, player, 0, pn);
165 modexShowPage(pip[1].page);
167 } else { player[pn].q = 1; player[pn].info.dir = 0; player[pn].ty--; }
171 modexCopyPageRegion(pip[1].page, pip[0].page, player[pn].x-4, player[pn].y-TILEWH, player[pn].x-4, player[pn].y-TILEWH, 24, 32);
172 modexDrawSpriteRegion(pip[1].page, player[pn].x-4, player[pn].y-TILEWH, 24, 0, 24, 32, &player[pn].data);
173 modexShowPage(pip[1].page);
174 player[pn].info.dir = 0;
176 player[pn].triggerx = player[pn].tx;
177 player[pn].triggery = player[pn].ty-1;
183 allocMap(int w, int h) {
188 result.data = malloc(sizeof(byte) * w * h);
189 //result.data = (byte *)alloc_emem(((int)sizeof(byte) * w * h)/1024);
190 if(isEMS() || checkEMS())
193 //emmhandle = mallocEMS(coretotalEMS());//alloc_emem((int)sizeof(map))
194 mm.length=sizeof(result);
196 mm.sourceOff=ptr2long(&result);
199 ist = move_emem(&mm);
200 if(!ist){ dealloc_emem(emmhandle); exit(5); }
201 printf("%d\n", coretotalEMS());
208 initMap(map_t *map) {
209 // just a place holder to fill out an alternating pattern
213 //if(!isEMS() || !checkEMS())
214 // map->tiles = malloc(sizeof(tiles_t));
216 // map->tiles = (tiles_t *)alloc_emem(sizeof(tiles_t));
218 //create the tile set
219 //if(!isEMS() || !checkEMS())
220 // map->tiles->data = malloc(sizeof(bitmap_t));
222 // map->tiles->data = (bitmap_t *)alloc_emem(sizeof(bitmap_t));
223 // map->tiles->data->width = (TILEWH);
224 // map->tiles->data->height= TILEWH;
225 //if(!isEMS() || !checkEMS())
226 // map->tiles->data->data = malloc((TILEWH*2)*TILEWH);
228 // map->tiles->data->data = (byte *)alloc_emem((TILEWH*2)*TILEWH);
229 // map->tiles->tileHeight = TILEWH;
230 // map->tiles->tileWidth =TILEWH;
231 // map->tiles->rows = 1;
232 // map->tiles->cols = 1;//2;
235 //for(y=0; y<map->height; y++) {
236 //for(x=0; x<map->width; x++) {
238 for(yy=0; yy<TILEWH; yy++) {
239 for(xx=0; xx<(TILEWH); xx++) {
241 map->tiles->data->data[i+1] = map->data[q];//28;//0x24;
242 // printf("[%d]", map->tiles->data->data[i]);
244 //map->tiles->data->data[i] = map->data[q];//0;//0x34;
245 //printf("]%d[==[%d]", i, map->tiles->data->data[i]);
251 // printf("[%d]", map->data[q]);
258 for(y=0; y<map->height; y++) {
259 for(x=0; x<map->width; x++) {
261 printf("[%d]", map->data[i]);
262 //tile = tile ? 0 : 1;
265 //tile = tile ? 0 : 1;
269 void mapScrollRight(map_view_t *mv, player_t *player, word id, word plid)
271 word x, y; /* coordinate for drawing */
273 /* increment the pixel position and update the page */
274 mv[id].page->dx += player[plid].speed;
276 /* check to see if this changes the tile */
277 if(mv[id].page->dx >= mv[id].dxThresh )
279 /* go forward one tile */
281 /* Snap the origin forward */
282 mv[id].page->data += 4;
283 mv[id].page->dx = mv[id].map->tiles->tileWidth;
286 /* draw the next column */
287 x= SCREEN_WIDTH + mv[id].map->tiles->tileWidth;
290 mapDrawCol(&mv[0], mv[0].tx + 20 , mv[0].ty-1, x, player, mv->page->dx);
292 modexCopyPageRegion(mv[id].page, mv[0].page, x, 0, x, 0, mv[id].map->tiles->tileWidth, mv[id].map->tiles->tileHeight*17);
297 void mapScrollLeft(map_view_t *mv, player_t *player, word id, word plid)
299 word x, y; /* coordinate for drawing */
301 /* increment the pixel position and update the page */
302 mv[id].page->dx -= player[plid].speed;
304 /* check to see if this changes the tile */
305 if(mv[id].page->dx == 0)
307 /* go backward one tile */
310 /* Snap the origin backward */
311 mv[id].page->data -= 4;
312 mv[id].page->dx = mv[id].map->tiles->tileWidth;
315 /* draw the next column */
319 mapDrawCol(&mv[0], mv[0].tx - 1, mv[0].ty-1, x, player, mv->page->dx);
321 modexCopyPageRegion(mv[id].page, mv[0].page, x, 0, x, 0, mv[id].map->tiles->tileWidth, mv[id].map->tiles->tileHeight*17);
326 void mapScrollUp(map_view_t *mv, player_t *player, word id, word plid)
328 word x, y; /* coordinate for drawing */
330 /* increment the pixel position and update the page */
331 mv[id].page->dy -= player[plid].speed;
333 /* check to see if this changes the tile */
334 if(mv[id].page->dy == 0 )
336 /* go down one tile */
338 /* Snap the origin downward */
339 mv[id].page->data -= mv[id].page->width*4;
340 mv[id].page->dy = mv[id].map->tiles->tileHeight;
343 /* draw the next row */
347 mapDrawRow(&mv[0], mv[0].tx - 1, mv[0].ty-1, y, player, mv->page->dy);
349 modexCopyPageRegion(mv[id].page, mv[0].page, 0, y, 0, y, mv[id].map->tiles->tileWidth*22, mv[id].map->tiles->tileHeight);
353 void mapScrollDown(map_view_t *mv, player_t *player, word id, word plid)
355 word x, y; /* coordinate for drawing */
357 /* increment the pixel position and update the page */
358 mv[id].page->dy += player[plid].speed;
360 /* check to see if this changes the tile */
361 if(mv[id].page->dy >= mv[id].dyThresh )
363 /* go down one tile */
365 /* Snap the origin downward */
366 mv[id].page->data += mv[id].page->width*4;
367 mv[id].page->dy = mv[id].map->tiles->tileHeight;
370 /* draw the next row */
371 y= SCREEN_HEIGHT + mv[id].map->tiles->tileHeight;
374 mapDrawRow(&mv[0], mv[0].tx - 1, mv[0].ty+15, y, player, mv->page->dy);
376 modexCopyPageRegion(mv[id].page, mv[0].page, 0, y, 0, y, mv[id].map->tiles->tileWidth*22, mv[id].map->tiles->tileHeight);
381 void mapGoTo(map_view_t *mv, int tx, int ty)
386 /* set up the coordinates */
389 mv->page->dx = mv->map->tiles->tileWidth;
390 mv->page->dy = mv->map->tiles->tileHeight;
392 /* set up the thresholds */
393 mv->dxThresh = mv->map->tiles->tileWidth * 2;
394 mv->dyThresh = mv->map->tiles->tileHeight * 2;
397 modexClearRegion(mv->page, 0, 0, mv->page->width, mv->page->height, 0);
399 i=mv->ty * mv->map->width + mv->tx;
400 for(ty=mv->ty-1; py < SCREEN_HEIGHT+mv->dyThresh && ty < mv->map->height; ty++, py+=mv->map->tiles->tileHeight) {
401 mapDrawWRow(mv, tx-1, ty, py);
402 i+=mv->map->width - tx;
408 mapDrawTile(tiles_t *t, word i, page_t *page, word x, word y)
416 modexClearRegion(page, x, y, t->tileWidth, t->tileHeight, 0); //currently the over scan color!
420 rx = (((i-1) % ((t->data->width)/t->tileWidth)) * t->tileWidth);
421 ry = (((i-1) / ((t->data->height)/t->tileHeight)) * t->tileHeight);
422 ////0000 printf("i=%d\n", i);
423 //mxPutTile(t->data, x, y, t->tileWidth, t->tileHeight);
424 modexDrawBmpRegion(page, x, y, rx, ry, t->tileWidth, t->tileHeight, (t->data));
428 void mapDrawRow(map_view_t *mv, int tx, int ty, word y, player_t *p, word poopoffset)
432 poopoffset%=p[0].speed;
433 //printf("y: %d\n", poopoffset);
434 /* the position within the map array */
435 i=ty * mv->map->width + tx;
436 for(x=poopoffset; x<(SCREEN_WIDTH+mv->dxThresh)/(poopoffset+1) && tx < mv->map->width; x+=mv->map->tiles->tileWidth, tx++) {
438 /* we are in the map, so copy! */
439 mapDrawTile(mv->map->tiles, mv->map->data[i], mv->page, x, y);
445 void mapDrawCol(map_view_t *mv, int tx, int ty, word x, player_t *p, word poopoffset)
449 poopoffset%=p[0].speed;
450 //printf("x: %d\n", poopoffset);
451 /* location in the map array */
452 i=ty * mv->map->width + tx;
454 /* We'll copy all of the columns in the screen,
455 i + 1 row above and one below */
456 for(y=poopoffset; y<(SCREEN_HEIGHT+mv->dyThresh)/(poopoffset+1) && ty < mv->map->height; y+=mv->map->tiles->tileHeight, ty++) {
458 /* we are in the map, so copy away! */
459 mapDrawTile(mv->map->tiles, mv->map->data[i], mv->page, x, y);
465 void mapDrawWRow(map_view_t *mv, int tx, int ty, word y)
470 /* the position within the map array */
471 i=ty * mv->map->width + tx;
472 for(x=0; x<SCREEN_WIDTH+mv->dxThresh && tx < mv->map->width; x+=mv->map->tiles->tileWidth, tx++) {
474 /* we are in the map, so copy! */
475 mapDrawTile(mv->map->tiles, mv->map->data[i], mv->page, x, y);
481 void mapDrawWCol(map_view_t *mv, int tx, int ty, word x)
486 /* location in the map array */
487 i=ty * mv->map->width + tx;
489 /* We'll copy all of the columns in the screen,
490 i + 1 row above and one below */
491 for(y=0; y<SCREEN_HEIGHT+mv->dyThresh && ty < mv->map->height; y+=mv->map->tiles->tileHeight, ty++) {
493 /* we are in the map, so copy away! */
494 mapDrawTile(mv->map->tiles, mv->map->data[i], mv->page, x, y);
506 void pdump(page_t *pee)
509 int palq=(mult)*TILEWH;
512 for(paly=0; paly<palq; paly+=mult){
513 for(palx=0; palx<palq; palx+=mult){
514 modexClearRegion(pee, palx+TILEWH, paly+TILEWH, mult, mult, palcol);
520 void animatePlayer(map_view_t *pip, player_t *player, word playnum, sword scrollswitch)
522 sword dire=32; //direction
523 sword qq; //scroll offset
524 word ls = player[playnum].persist_aniframe;
526 if(scrollswitch==0) qq = 0;
527 else qq = ((player[playnum].persist_aniframe)*player[playnum].speed);
528 switch (player[playnum].info.dir)
532 dire*=player[playnum].info.dir;
533 player[playnum].x=player[playnum].x-4;
534 player[playnum].y=player[playnum].y-qq-TILEWH;
538 dire*=(player[playnum].info.dir-2);
539 player[playnum].x=player[playnum].x+qq-4;
540 player[playnum].y=player[playnum].y-TILEWH;
546 dire*=(player[playnum].info.dir-2);
547 player[playnum].x=player[playnum].x-4;
548 player[playnum].y=player[playnum].y+qq-TILEWH;
552 dire*=(player[playnum].info.dir+2);
553 player[playnum].x=player[playnum].x-qq-4;
554 player[playnum].y=player[playnum].y-TILEWH;
557 modexCopyPageRegion(pip[1].page, pip[0].page, player[playnum].x-4, player[playnum].y-4, player[playnum].x-4, player[playnum].y-4, 28, 40);
558 if(2>ls && ls>=1) { modexDrawSpriteRegion(pip[1].page, player[playnum].x, player[playnum].y, 48, dire, 24, 32, &player[playnum].data); }else
559 if(3>ls && ls>=2) { modexDrawSpriteRegion(pip[1].page, player[playnum].x, player[playnum].y, 24, dire, 24, 32, &player[playnum].data); }else
560 if(4>ls && ls>=3) { modexDrawSpriteRegion(pip[1].page, player[playnum].x, player[playnum].y, 0, dire, 24, 32, &player[playnum].data); }else
561 if(5>ls && ls>=4) { modexDrawSpriteRegion(pip[1].page, player[playnum].x, player[playnum].y, 24, dire, 24, 32, &player[playnum].data); }
562 //TODO: mask copy //modexCopyPageRegion(dest->page, src->page, x-4, y-4, x-4, y-4, 28, 40);
563 //modexClearRegion(top->page, 66, 66, 2, 40, 0);
564 //modexCopyPageRegion(dest->page, top->page, 66, 66, 66, 66, 2, 40);
565 //turn this off if XT
566 //XTif(detectcpu() > 0)