]> 4ch.mooo.com Git - 16.git/blob - src/scroll.c
pomf~
[16.git] / src / scroll.c
1 #include "src/lib/dos_kb.h"\r
2 #include "src/lib/mapread.c"\r
3 //#include "16\lib\x\modex.h"\r
4 #include "src/lib/wtest/wtest.c"\r
5 #include "src/lib/planar.c"\r
6 //====#include "src\lib\ems.c"\r
7 \r
8 //word far *clock= (word far*) 0x046C; /* 18.2hz clock */\r
9 \r
10 /*typedef struct {\r
11         bitmap_t *data;\r
12         word tileHeight;\r
13         word tileWidth;\r
14         unsigned int rows;\r
15         unsigned int cols;\r
16 } tiles_t;\r
17 \r
18 \r
19 typedef struct {\r
20         byte    *data;\r
21         tiles_t *tiles;\r
22         int width;\r
23         int height;\r
24 } map_t;*/\r
25 \r
26 \r
27 typedef struct {\r
28         map_t *map;\r
29         page_t *page;\r
30         int tx; //appears to be the top left tile position on the viewable screen map\r
31         int ty; //appears to be the top left tile position on the viewable screen map\r
32         word dxThresh; //????\r
33         word dyThresh; //????\r
34 } map_view_t;\r
35 \r
36 typedef struct {\r
37         int x; //player exact position on the viewable map\r
38         int y; //player exact position on the viewable map\r
39         int tx; //player tile position on the viewable map\r
40         int ty; //player tile position on the viewable map\r
41         int triggerx; //player's trigger box tile position on the viewable map\r
42         int triggery; //player's trigger box tile position on the viewable map\r
43         int setx; //NOT USED YET! player sprite sheet set on the image x\r
44         int sety; //NOT USED YET! player sprite sheet set on the image y\r
45         word q; //loop variable\r
46         word d; //direction\r
47         bitmap_t data; //supposively the sprite sheet data\r
48         int hp; //hitpoints of the player\r
49 } actor_t;\r
50 \r
51 map_t allocMap(int w, int h);\r
52 void initMap(map_t *map);\r
53 void mapScrollRight(map_view_t *mv, byte offset);\r
54 void mapScrollLeft(map_view_t *mv, byte offest);\r
55 void mapScrollUp(map_view_t *mv, byte offset);\r
56 void mapScrollDown(map_view_t *mv, byte offset);\r
57 void mapGoTo(map_view_t *mv, int tx, int ty);\r
58 void mapDrawTile(tiles_t *t, word i, page_t *page, word x, word y);\r
59 void mapDrawRow(map_view_t *mv, int tx, int ty, word y);\r
60 void mapDrawCol(map_view_t *mv, int tx, int ty, word x);\r
61 void qclean();\r
62 void pdump(map_view_t *pee);\r
63 void animatePlayer(map_view_t *src, map_view_t *dest, /*map_view_t *top, */sword d, short scrolloffsetswitch, int x, int y, int ls, int lp, bitmap_t *bmp);\r
64 \r
65 #define TILEWH 16\r
66 #define QUADWH                  TILEWH/2\r
67 #define SPEED 16\r
68 //#define LOOPMAX (TILEWH/SPEED)\r
69 \r
70 //place holder definitions\r
71 //#define MAPX 200\r
72 //#define MAPY 150\r
73 #define TRIGGX 10\r
74 #define TRIGGY 9\r
75 \r
76 void main() {\r
77         size_t oldfreemem=GetFreeSize();\r
78         long emmhandle;\r
79         long emsavail;\r
80         char teststr[80];\r
81         int i;\r
82         static word paloffset=0;\r
83         bitmap_t ptmp;//, npctmp; // player sprite\r
84         planar_buf_t *p;\r
85         const char *cpus;\r
86         static int persist_aniframe = 0;    /* gonna be increased to 1 before being used, so 0 is ok for default */\r
87         page_t screen, screen2, screen3;\r
88         map_t map;\r
89         map_view_t mv, mv2, mv3;\r
90         map_view_t *bg, *spri, *mask;//, *tmp;\r
91         byte *dpal, *gpal;\r
92         byte *ptr, *mappalptr;\r
93         actor_t player;\r
94         //actor_t npc0;\r
95 \r
96 //      atexit(qclean());\r
97         /*if(!emmtest())\r
98         {\r
99                 printf("Expanded memory is not present\n");\r
100                 exit(0);\r
101         }\r
102      \r
103         if(!emmok())\r
104         {\r
105                 printf("Expanded memory manager is not present\n");\r
106                 exit(0);\r
107         }\r
108 \r
109         emsavail = emmavail();\r
110         if(emsavail == -1)\r
111         {\r
112                 printf("Expanded memory manager error\n");\r
113                 exit(0);\r
114         }\r
115         printf("There are %ld pages available\n",emsavail);\r
116 \r
117         if((emmhandle = emmalloc(emsavail)) < 0)\r
118         {\r
119                 printf("Insufficient pages available\n");\r
120                 exit(0);\r
121         }*/\r
122 \r
123         /* create the map */\r
124 //0000  printf("Total used @ before map load:                   %zu\n", oldfreemem-GetFreeSize());\r
125         loadmap("data/test.map", &map);\r
126 //----  map = allocMap(map.width,map.height); //20x15 is the resolution of the screen you can make maps smaller than 20x15 but the null space needs to be drawn properly\r
127         //if(isEMS()) printf("%d tesuto\n", coretotalEMS());\r
128 //----  initMap(&map);\r
129         mv.map = &map;\r
130         mv2.map = &map;\r
131         mv3.map = &map;\r
132 \r
133         /* draw the tiles */\r
134         ptr = map.data;\r
135         mappalptr = map.tiles->data->palette;\r
136         /* data */\r
137 //0000  printf("Total used @ before image loading:              %zu\n", oldfreemem-GetFreeSize());\r
138         ptmp = bitmapLoadPcx("data/ptmp.pcx"); // load sprite\r
139         //npctmp = bitmapLoadPcx("ptmp1.pcx"); // load sprite\r
140         \r
141         /* create the planar buffer */\r
142 //0000  printf("Total used @ before planar buffer creation:     %zu\n", oldfreemem-GetFreeSize());\r
143         p = planar_buf_from_bitmap(&ptmp);\r
144 //0000  printf("Total used @ after planar buffer creation:      %zu\n", oldfreemem-GetFreeSize());\r
145 \r
146         /*if(isEMS())\r
147         {\r
148                 XMOVE mm;\r
149                 mm.length=sizeof(map);\r
150                 mm.sourceH=0;\r
151                 mm.sourceOff=(long)&map;\r
152                 mm.destH=emmhandle;\r
153                 mm.destOff=1;\r
154                 //halp!\r
155                 ist = move_emem(&mm);\r
156                 printf("%d\n", coretotalEMS());\r
157                 if(!ist){ dealloc_emem(emmhandle); exit(5); }\r
158                 //printf("%d\n", emmhandle);\r
159         }\r
160 \r
161         if(isEMS())\r
162         {\r
163                 XMOVE mm;\r
164                 mm.length=emmhandle;\r
165                 mm.sourceH=0;\r
166                 mm.sourceOff=(long)&ptmp;\r
167                 mm.destH=emmhandle;\r
168                 mm.destOff=0;\r
169                 //halp!\r
170                 ist = move_emem(&mm);\r
171                 printf("%d\n", coretotalEMS());\r
172                 if(!ist){ dealloc_emem(emmhandle); exit(5); }\r
173                 //printf("%d\n", emmhandle);\r
174         }\r
175 */\r
176 \r
177         /* save the palette */\r
178         dpal = modexNewPal();\r
179         modexPalSave(dpal);\r
180         modexFadeOff(4, dpal);\r
181 \r
182         setkb(1);\r
183         modexEnter();\r
184         modexPalBlack();        //reset the palette~\r
185 //      printf("Total used @ before palette initiation:         %zu\n", oldfreemem-GetFreeSize());\r
186         ptmp.offset=(paloffset/3);\r
187         modexPalUpdate(&ptmp, &paloffset, 0, 0);\r
188         //printf("      %d\n", sizeof(ptmp.data));\r
189         //printf("1:    %d\n", paloffset);\r
190         map.tiles->data->offset=(paloffset/3);\r
191         modexPalUpdate(map.tiles->data, &paloffset, 0, 0);\r
192         printf("\n====\n");\r
193         printf("0       paloffset=      %d\n", paloffset/3);\r
194         printf("====\n\n");\r
195         gpal = modexNewPal();\r
196         modexPalSave(gpal);\r
197         modexSavePalFile("data/g.pal", gpal);\r
198         modexPalBlack();        //so player will not see loadings~\r
199 \r
200         /* setup camera and screen~ */\r
201         screen = modexDefaultPage();\r
202         screen.width += (TILEWH*2);\r
203         screen.height += (TILEWH*2)+QUADWH;\r
204         mv.page = &screen;\r
205         screen2 = modexNextPage(mv.page);\r
206         mv2.page = &screen2;\r
207         screen3 = screen2;\r
208         mv3.page = &screen3;\r
209 \r
210         /* set up paging */\r
211         bg = &mv;\r
212         spri = &mv2;\r
213         mask = &mv3;\r
214 \r
215 //TODO: LOAD map data and position the map in the middle of the screen if smaller then screen\r
216         mapGoTo(bg, 0, 0);\r
217         mapGoTo(spri, 0, 0);\r
218         //mapGoTo(mask, 0, 0);\r
219 \r
220         //TODO: put player in starting position of spot\r
221         //default player position on the viewable map\r
222         player.tx = bg->tx + 10;\r
223         player.ty = bg->ty + 8;\r
224         player.x = player.tx*TILEWH;\r
225         player.y = player.ty*TILEWH;\r
226         player.triggerx = player.tx;\r
227         player.triggery = player.ty+1;\r
228         player.q=1;\r
229         player.d=0;\r
230         player.hp=4;\r
231         //npc\r
232         /*npc0.tx = bg->tx + 1;\r
233         npc0.ty = bg->ty + 1;\r
234         npc0.x = npc0.tx*TILEWH;\r
235         npc0.y = npc0.ty*TILEWH;\r
236         npc0.triggerx = npc0.tx;\r
237         npc0.triggery = npc0.ty+1;\r
238         npc0.q=1;\r
239         npc0.d=0;\r
240         modexDrawSpriteRegion(spri->page, npc0.x-4, npc0.y-TILEWH, 24, 64, 24, 32, &npctmp);*/\r
241         modexDrawSpriteRegion(spri->page, player.x-4, player.y-TILEWH, 24, 64, 24, 32, &ptmp);\r
242 \r
243         //----modexClearRegion(spri->page, player.triggerx*16, player.triggery*16, 16, 16, 1);\r
244         //----modexClearRegion(bg->page, player.triggerx*16, player.triggery*16, 16, 16, 1);\r
245 \r
246         //----modexClearRegion(spri->page, 5*16, 5*16, 16, 16, 255);\r
247         //----modexClearRegion(bg->page, 5*16, 5*16, 16, 16, 255);\r
248         modexShowPage(spri->page);\r
249 //      printf("Total used @ before loop:                       %zu\n", oldfreemem-GetFreeSize());\r
250         modexFadeOn(4, gpal);\r
251         while(!keyp(1) && player.hp>0)\r
252         {\r
253         //top left corner & bottem right corner of map veiw be set as map edge trigger since maps are actually square\r
254         //to stop scrolling and have the player position data move to the edge of the screen with respect to the direction\r
255         //when player.tx or player.ty == 0 or player.tx == 20 or player.ty == 15 then stop because that is edge of map and you do not want to walk of the map\r
256         #define INC_PER_FRAME if(player.q&1) persist_aniframe++; if(persist_aniframe>4) persist_aniframe = 1;\r
257         /*#define INC_PER_FRAME_NPC if(npc0.q&1) persist_aniframe++; if(persist_aniframe>4) persist_aniframe = 1;\r
258 \r
259         if(npc0.d == 0 && npc0.q == 1) npc0.d =rand()%8;\r
260         if(npc0.d>4)\r
261                 npc0.d=0;\r
262 \r
263         //right movement\r
264         if(npc0.d == 2)\r
265         {\r
266                 if(npc0.tx < map.width && !(npc0.tx+1 == TRIGGX && npc0.ty == TRIGGY) && !(npc0.tx+1 == player.tx && npc0.ty == player.ty))\r
267                 {\r
268                         if(npc0.q<=(TILEWH/SPEED))\r
269                         {\r
270                                 INC_PER_FRAME_NPC;\r
271                                 npc0.x+=SPEED;\r
272                                 //animatePlayer(bg, spri, mask, 1, 0, npc0.x, npc0.y, persist_aniframe, q, &npctmp);\r
273                                 animatePlayer(bg, spri, npc0.d-1, 0, npc0.x, npc0.y, persist_aniframe, npc0.q, &npctmp);\r
274                                 modexShowPage(spri->page);\r
275                                 npc0.q++;\r
276                         } else { npc0.q = 1; npc0.d = 0; npc0.tx++; }\r
277                 }\r
278                 else\r
279                 {\r
280                         modexCopyPageRegion(spri->page, bg->page, npc0.x-4, npc0.y-TILEWH, npc0.x-4, npc0.y-TILEWH, 24, 32);\r
281                         modexDrawSpriteRegion(spri->page, npc0.x-4, npc0.y-TILEWH, 24, 32, 24, 32, &npctmp);\r
282                         modexShowPage(spri->page);\r
283                         npc0.d = 0;\r
284                 }\r
285                 npc0.triggerx = npc0.tx+1;\r
286                 npc0.triggery = npc0.ty;\r
287         }\r
288 \r
289         //left movement\r
290         if(npc0.d == 4)\r
291         {\r
292                 if(npc0.tx > 1 && !(npc0.tx-1 == TRIGGX && npc0.ty == TRIGGY) && !(npc0.tx-1 == player.tx && npc0.ty == player.ty))\r
293                 {\r
294                         if(npc0.q<=(TILEWH/SPEED))\r
295                         {\r
296                                 INC_PER_FRAME_NPC;\r
297                                 npc0.x-=SPEED;\r
298                                 //animatePlayer(bg, spri, mask, 3, 0, npc0.x, npc0.y, persist_aniframe, q, &npctmp);\r
299                                 animatePlayer(bg, spri, npc0.d-1, 0, npc0.x, npc0.y, persist_aniframe, npc0.q, &npctmp);\r
300                                 modexShowPage(spri->page);\r
301                                 npc0.q++;\r
302                         } else { npc0.q = 1; npc0.d = 0; npc0.tx--; }\r
303                 }\r
304                 else\r
305                 {\r
306                         modexCopyPageRegion(spri->page, bg->page, npc0.x-4, npc0.y-TILEWH, npc0.x-4, npc0.y-TILEWH, 24, 32);\r
307                         modexDrawSpriteRegion(spri->page, npc0.x-4, npc0.y-TILEWH, 24, 96, 24, 32, &npctmp);\r
308                         modexShowPage(spri->page);\r
309                         npc0.d = 0;\r
310                 }\r
311                 npc0.triggerx = npc0.tx-1;\r
312                 npc0.triggery = npc0.ty;\r
313         }\r
314 \r
315         //down movement\r
316         if(npc0.d == 3)\r
317         {\r
318                 if(npc0.ty < map.height && !(npc0.tx == TRIGGX && npc0.ty+1 == TRIGGY) && !(npc0.tx == player.tx && npc0.ty == player.ty+1))\r
319                 {\r
320                         if(npc0.q<=(TILEWH/SPEED))\r
321                         {\r
322                                 INC_PER_FRAME_NPC;\r
323                                 npc0.y+=SPEED;\r
324                                 //animatePlayer(bg, spri, mask, 2, 0, npc0.x, npc0.y, persist_aniframe, q, &npctmp);\r
325                                 animatePlayer(bg, spri, npc0.d-1, 0, npc0.x, npc0.y, persist_aniframe, npc0.q, &npctmp);\r
326                                 modexShowPage(spri->page);\r
327                                 npc0.q++;\r
328                         } else { npc0.q = 1; npc0.d = 0; npc0.ty++; }\r
329                 }\r
330                 else\r
331                 {\r
332                         modexCopyPageRegion(spri->page, bg->page, npc0.x-4, npc0.y-TILEWH, npc0.x-4, npc0.y-TILEWH, 24, 32);\r
333                         modexDrawSpriteRegion(spri->page, npc0.x-4, npc0.y-TILEWH, 24, 64, 24, 32, &npctmp);\r
334                         modexShowPage(spri->page);\r
335                         npc0.d = 0;\r
336                 }\r
337                 npc0.triggerx = npc0.tx;\r
338                 npc0.triggery = npc0.ty+1;\r
339         }\r
340 \r
341         //up movement\r
342         if(npc0.d == 1)\r
343         {\r
344                 if(npc0.ty > 1 && !(npc0.tx == TRIGGX &&  npc0.ty-1 == TRIGGY) && !(npc0.tx+1 == player.tx && npc0.ty == player.ty-1))\r
345                 {\r
346                         if(npc0.q<=(TILEWH/SPEED))\r
347                         {\r
348                                 INC_PER_FRAME_NPC;\r
349                                 npc0.y-=SPEED;\r
350                                 //animatePlayer(bg, spri, mask, 0, 0, npc0.x, npc0.y, persist_aniframe, q, &npctmp);\r
351                                 modexShowPage(spri->page);\r
352                                 animatePlayer(bg, spri, npc0.d-1, 0, npc0.x, npc0.y, persist_aniframe, npc0.q, &npctmp);\r
353                                 npc0.q++;\r
354                         } else { npc0.q = 1; npc0.d = 0; npc0.ty--; }\r
355                 }\r
356                 else\r
357                 {\r
358                         modexCopyPageRegion(spri->page, bg->page, npc0.x-4, npc0.y-TILEWH, npc0.x-4, npc0.y-TILEWH, 24, 32);\r
359                         modexDrawSpriteRegion(spri->page, npc0.x-4, npc0.y-TILEWH, 24, 0, 24, 32, &npctmp);\r
360                         modexShowPage(spri->page);\r
361                         npc0.d = 0;\r
362                 }\r
363                 npc0.triggerx = npc0.tx;\r
364                 npc0.triggery = npc0.ty-1;\r
365         }\r
366 \r
367         if((npc0.triggery == player.ty && npc0.triggerx == player.tx) || (npc0.ty == player.ty && npc0.tx == player.tx)){ player.hp--; }\r
368 */\r
369 \r
370         //player movement\r
371         //TODO: make movement into a function!\r
372         //right movement\r
373         if((keyp(77) && !keyp(75) && player.d == 0) || player.d == 2)\r
374         {\r
375                 if(player.d == 0){ player.d = 2; }\r
376                 if(bg->tx >= 0 && bg->tx+20 < map.width && player.tx == bg->tx + 10 && !(player.tx+1 == TRIGGX && player.ty == TRIGGY))\r
377                 {\r
378                         if(player.q<=(TILEWH/SPEED))\r
379                         {\r
380                                 INC_PER_FRAME;\r
381                                 //animatePlayer(bg, spri, mask, 1, 1, player.x, player.y, persist_aniframe, q, &ptmp);\r
382                                 animatePlayer(bg, spri, player.d-1, 1, player.x, player.y, persist_aniframe, player.q, &ptmp);\r
383                                 mapScrollRight(bg, SPEED);\r
384                                 mapScrollRight(spri, SPEED);\r
385                                 //mapScrollRight(mask, SPEED);\r
386                                 modexShowPage(spri->page);\r
387                                 player.q++;\r
388                         } else { player.q = 1; player.d = 0; player.tx++; }\r
389                 }\r
390                 else if(player.tx < map.width && !(player.tx+1 == TRIGGX && player.ty == TRIGGY))\r
391                 {\r
392                         if(player.q<=(TILEWH/SPEED))\r
393                         {\r
394                                 INC_PER_FRAME;\r
395                                 player.x+=SPEED;\r
396                                 //animatePlayer(bg, spri, mask, 1, 0, player.x, player.y, persist_aniframe, q, &ptmp);\r
397                                 animatePlayer(bg, spri, player.d-1, 0, player.x, player.y, persist_aniframe, player.q, &ptmp);\r
398                                 modexShowPage(spri->page);\r
399                                 player.q++;\r
400                         } else { player.q = 1; player.d = 0; player.tx++; }\r
401                 }\r
402                 else\r
403                 {\r
404                         modexCopyPageRegion(spri->page, bg->page, player.x-4, player.y-TILEWH, player.x-4, player.y-TILEWH, 24, 32);\r
405                         modexDrawSpriteRegion(spri->page, player.x-4, player.y-TILEWH, 24, 32, 24, 32, &ptmp);\r
406                         modexShowPage(spri->page);\r
407                         player.d = 0;\r
408                 }\r
409                 player.triggerx = player.tx+1;\r
410                 player.triggery = player.ty;\r
411         }\r
412 \r
413         //left movement\r
414         if((keyp(75) && !keyp(77) && player.d == 0) || player.d == 4)\r
415         {\r
416                 if(player.d == 0){ player.d = 4; }\r
417                 if(bg->tx > 0 && bg->tx+20 <= map.width && player.tx == bg->tx + 10 && !(player.tx-1 == TRIGGX && player.ty == TRIGGY))\r
418                 {\r
419                         if(player.q<=(TILEWH/SPEED))\r
420                         {\r
421                                 INC_PER_FRAME;\r
422                                 //animatePlayer(bg, spri, mask, 3, 1, player.x, player.y, persist_aniframe, q, &ptmp);\r
423                                 animatePlayer(bg, spri, player.d-1, 1, player.x, player.y, persist_aniframe, player.q, &ptmp);\r
424                                 mapScrollLeft(bg, SPEED);\r
425                                 mapScrollLeft(spri, SPEED);\r
426                                 //mapScrollLeft(mask, SPEED);\r
427                                 modexShowPage(spri->page);\r
428                                 player.q++;\r
429                         } else { player.q = 1; player.d = 0; player.tx--; }\r
430                 }\r
431                 else if(player.tx > 1 && !(player.tx-1 == TRIGGX && player.ty == TRIGGY))\r
432                 {\r
433                         if(player.q<=(TILEWH/SPEED))\r
434                         {\r
435                                 INC_PER_FRAME;\r
436                                 player.x-=SPEED;\r
437                                 //animatePlayer(bg, spri, mask, 3, 0, player.x, player.y, persist_aniframe, q, &ptmp);\r
438                                 animatePlayer(bg, spri, player.d-1, 0, player.x, player.y, persist_aniframe, player.q, &ptmp);\r
439                                 modexShowPage(spri->page);\r
440                                 player.q++;\r
441                         } else { player.q = 1; player.d = 0; player.tx--; }\r
442                 }\r
443                 else\r
444                 {\r
445                         modexCopyPageRegion(spri->page, bg->page, player.x-4, player.y-TILEWH, player.x-4, player.y-TILEWH, 24, 32);\r
446                         modexDrawSpriteRegion(spri->page, player.x-4, player.y-TILEWH, 24, 96, 24, 32, &ptmp);\r
447                         modexShowPage(spri->page);\r
448                         player.d = 0;\r
449                 }\r
450                 player.triggerx = player.tx-1;\r
451                 player.triggery = player.ty;\r
452         }\r
453 \r
454         //down movement\r
455         if((keyp(80) && !keyp(72) && player.d == 0) || player.d == 3)\r
456         {\r
457                 if(player.d == 0){ player.d = 3; }\r
458                 if(bg->ty >= 0 && bg->ty+15 < map.height && player.ty == bg->ty + 8 && !(player.tx == TRIGGX && player.ty+1 == TRIGGY))\r
459                 {\r
460                         if(player.q<=(TILEWH/SPEED))\r
461                         {\r
462                                 INC_PER_FRAME;\r
463                                 //animatePlayer(bg, spri, mask, 2, 1, player.x, player.y, persist_aniframe, q, &ptmp);\r
464                                 animatePlayer(bg, spri, player.d-1, 1, player.x, player.y, persist_aniframe, player.q, &ptmp);\r
465                                 mapScrollDown(bg, SPEED);\r
466                                 mapScrollDown(spri, SPEED);\r
467                                 //mapScrollDown(mask, SPEED);\r
468                                 modexShowPage(spri->page);\r
469                                 player.q++;\r
470                         } else { player.q = 1; player.d = 0; player.ty++; }\r
471                 }\r
472                 else if(player.ty < map.height && !(player.tx == TRIGGX && player.ty+1 == TRIGGY))\r
473                 {\r
474                         if(player.q<=(TILEWH/SPEED))\r
475                         {\r
476                                 INC_PER_FRAME;\r
477                                 player.y+=SPEED;\r
478                                 //animatePlayer(bg, spri, mask, 2, 0, player.x, player.y, persist_aniframe, q, &ptmp);\r
479                                 animatePlayer(bg, spri, player.d-1, 0, player.x, player.y, persist_aniframe, player.q, &ptmp);\r
480                                 modexShowPage(spri->page);\r
481                                 player.q++;\r
482                         } else { player.q = 1; player.d = 0; player.ty++; }\r
483                 }\r
484                 else\r
485                 {\r
486                         modexCopyPageRegion(spri->page, bg->page, player.x-4, player.y-TILEWH, player.x-4, player.y-TILEWH, 24, 32);\r
487                         modexDrawSpriteRegion(spri->page, player.x-4, player.y-TILEWH, 24, 64, 24, 32, &ptmp);\r
488                         modexShowPage(spri->page);\r
489                         player.d = 0;\r
490                 }\r
491                 player.triggerx = player.tx;\r
492                 player.triggery = player.ty+1;\r
493         }\r
494 \r
495         //up movement\r
496         if((keyp(72) && !keyp(80) && player.d == 0) || player.d == 1)\r
497         {\r
498                 if(player.d == 0){ player.d = 1; }\r
499                 if(bg->ty > 0 && bg->ty+15 <= map.height && player.ty == bg->ty + 8 && !(player.tx == TRIGGX && player.ty-1 == TRIGGY))\r
500                 {\r
501                         if(player.q<=(TILEWH/SPEED))\r
502                         {\r
503                                 INC_PER_FRAME;\r
504                                 //animatePlayer(bg, spri, mask, 0, 1, player.x, player.y, persist_aniframe, q, &ptmp);\r
505                                 animatePlayer(bg, spri, player.d-1, 1, player.x, player.y, persist_aniframe, player.q, &ptmp);\r
506                                 mapScrollUp(bg, SPEED);\r
507                                 mapScrollUp(spri, SPEED);\r
508                                 //mapScrollUp(mask, SPEED);\r
509                                 modexShowPage(spri->page);\r
510                                 player.q++;\r
511                         } else { player.q = 1; player.d = 0; player.ty--; }\r
512                 }\r
513                 else if(player.ty > 1 && !(player.tx == TRIGGX &&  player.ty-1 == TRIGGY))\r
514                 {\r
515                         if(player.q<=(TILEWH/SPEED))\r
516                         {\r
517                                 INC_PER_FRAME;\r
518                                 player.y-=SPEED;\r
519                                 //animatePlayer(bg, spri, mask, 0, 0, player.x, player.y, persist_aniframe, q, &ptmp);\r
520                                 modexShowPage(spri->page);\r
521                                 animatePlayer(bg, spri, player.d-1, 0, player.x, player.y, persist_aniframe, player.q, &ptmp);\r
522                                 player.q++;\r
523                         } else { player.q = 1; player.d = 0; player.ty--; }\r
524                 }\r
525                 else\r
526                 {\r
527                         modexCopyPageRegion(spri->page, bg->page, player.x-4, player.y-TILEWH, player.x-4, player.y-TILEWH, 24, 32);\r
528                         modexDrawSpriteRegion(spri->page, player.x-4, player.y-TILEWH, 24, 0, 24, 32, &ptmp);\r
529                         modexShowPage(spri->page);\r
530                         player.d = 0;\r
531                 }\r
532                 player.triggerx = player.tx;\r
533                 player.triggery = player.ty-1;\r
534         }\r
535         //modexClearRegion(mask->page, 66, 66, 2, 40, 0);\r
536 \r
537         if(((player.triggerx == TRIGGX && player.triggery == TRIGGY) && keyp(0x1C))||(player.tx == 5 && player.ty == 5))\r
538         {\r
539                 short i;\r
540                 for(i=800; i>=400; i--)\r
541                 {\r
542                         sound(i);\r
543                 }\r
544                 nosound();\r
545         }\r
546         if(player.q == (TILEWH/SPEED)+1 && player.d > 0 && (player.triggerx == 5 && player.triggery == 5)){ player.hp--; }\r
547         //if(keyp(0x0E)) while(1){ if(xmsmalloc(24)) break; }\r
548         if(keyp(25)){ pdump(bg); pdump(spri); }\r
549         if(keyp(24)){ modexPalUpdate3(gpal); paloffset=0; pdump(bg); pdump(spri); }\r
550         if(keyp(22)){ \r
551         paloffset=0; modexPalBlack(); modexPalUpdate(&ptmp, &paloffset, 0, 0);\r
552         printf("1paloffset      =       %d\n", paloffset/3);\r
553          modexPalUpdate(map.tiles->data, &paloffset, 0, 0);\r
554         printf("2paloffset      =       %d\n", paloffset/3);\r
555          pdump(bg); pdump(spri); }\r
556 \r
557         if(keyp(87))\r
558         {\r
559                 modexLeave();\r
560                 setkb(0);\r
561                 __asm\r
562                 {\r
563                         mov ah,31h\r
564                         int 21h\r
565                 }\r
566         }\r
567         }\r
568 \r
569         /* fade back to text mode */\r
570         /* but 1st lets save the game palette~ */\r
571         modexPalSave(gpal);\r
572         modexSavePalFile("data/g.pal", gpal);\r
573         modexFadeOff(4, gpal);\r
574         modexLeave();\r
575         setkb(0);\r
576         //system("mem /E /P");\r
577         printf("Project 16 scroll.exe\n");\r
578         printf("tx: %d\n", bg->tx);\r
579         printf("ty: %d\n", bg->ty);\r
580         printf("player.x: %d", player.x);\r
581         if(player.hp==0) printf("%d wwww\n", player.y+8);\r
582         else printf("\nplayer.y: %d\n", player.y);\r
583         printf("player.tx: %d\n", player.tx);\r
584         printf("player.ty: %d\n", player.ty);\r
585         printf("player.triggx: %d\n", player.triggerx);\r
586         printf("player.triggy: %d\n", player.triggery);\r
587         printf("player.hp: %d\n", player.hp);\r
588         printf("player.q: %d\n", player.q);\r
589         printf("player.d: %d\n", player.d);\r
590         printf("palette offset: %d\n", paloffset/3);\r
591         printf("Total used: %zu\n", oldfreemem-GetFreeSize());\r
592         printf("Total free: %zu\n", GetFreeSize());\r
593         printf("temporary player sprite 0: http://www.pixiv.net/member_illust.php?mode=medium&illust_id=45556867\n");\r
594         printf("temporary player sprite 1: http://www.pixiv.net/member_illust.php?mode=medium&illust_id=44606385\n");\r
595         printf("Screen: %dx", screen.width);    printf("%d\n", screen.height);\r
596         printf("Screen2: %dx", screen2.width);  printf("%d\n", screen2.height);\r
597         //xmsfree(&map);\r
598         //xmsfree(bg);\r
599         //xmsfree(spri);\r
600         //xmsfree(mask);\r
601         //xmsreport();\r
602         //emmclose(emmhandle);\r
603         switch(detectcpu())\r
604         {\r
605                 case 0: cpus = "8086/8088 or 186/88"; break;\r
606                 case 1: cpus = "286"; break;\r
607                 case 2: cpus = "386 or newer"; break;\r
608                 default: cpus = "internal error"; break;\r
609         }\r
610         printf("detected CPU type: %s\n", cpus);\r
611         modexFadeOn(4, dpal);\r
612 }\r
613 \r
614 \r
615 map_t\r
616 allocMap(int w, int h) {\r
617         map_t result;\r
618 \r
619         result.width =w;\r
620         result.height=h;\r
621         result.data = malloc(sizeof(byte) * w * h);\r
622         //result.data = (byte *)alloc_emem(((int)sizeof(byte) * w * h)/1024);\r
623         /*if(isEMS() || checkEMS())\r
624         {\r
625                 XMOVE mm;\r
626                 //emmhandle = mallocEMS(coretotalEMS());//alloc_emem((int)sizeof(map))\r
627                 mm.length=sizeof(result);\r
628                 mm.sourceH=0;\r
629                 mm.sourceOff=ptr2long(&result);\r
630                 mm.destH=emmhandle;\r
631                 mm.destOff=0;\r
632                 ist = move_emem(&mm);\r
633                 if(!ist){ dealloc_emem(emmhandle); exit(5); }\r
634                 printf("%d\n", coretotalEMS());\r
635         }*/\r
636 \r
637         return result;\r
638 }\r
639 \r
640 void\r
641 initMap(map_t *map) {\r
642         /* just a place holder to fill out an alternating pattern */\r
643         int x, y, xx, yy;\r
644         int i, q;\r
645 //      int tile = 1;\r
646         //if(!isEMS() || !checkEMS())\r
647 //              map->tiles = malloc(sizeof(tiles_t));\r
648         //else\r
649         //      map->tiles = (tiles_t *)alloc_emem(sizeof(tiles_t));\r
650 \r
651         /* create the tile set */\r
652         //if(!isEMS() || !checkEMS())\r
653 //              map->tiles->data = malloc(sizeof(bitmap_t));\r
654         //else\r
655         //      map->tiles->data = (bitmap_t *)alloc_emem(sizeof(bitmap_t));\r
656 //      map->tiles->data->width = (TILEWH/**2*/);\r
657 //      map->tiles->data->height= TILEWH;\r
658         //if(!isEMS() || !checkEMS())\r
659 //              map->tiles->data->data = malloc((TILEWH*2)*TILEWH);\r
660         //else\r
661         //      map->tiles->data->data = (byte *)alloc_emem((TILEWH*2)*TILEWH);\r
662 //      map->tiles->tileHeight = TILEWH;\r
663 //      map->tiles->tileWidth =TILEWH;\r
664 //      map->tiles->rows = 1;\r
665 //      map->tiles->cols = 1;//2;\r
666 \r
667         /*q=0;\r
668         //for(y=0; y<map->height; y++) {\r
669         //for(x=0; x<map->width; x++) {\r
670         i=0;\r
671         for(yy=0; yy<TILEWH; yy++) {\r
672         for(xx=0; xx<(TILEWH); xx++) {\r
673                 //if(x<TILEWH){\r
674                   map->tiles->data->data[i+1] = map->data[q];//28;//0x24;\r
675 //                printf("[%d]", map->tiles->data->data[i]);\r
676                 //}else{\r
677                   //map->tiles->data->data[i] = map->data[q];//0;//0x34;\r
678                   //printf("]%d[==[%d]", i, map->tiles->data->data[i]);\r
679                 //}\r
680                 i++;\r
681         }\r
682 //      printf("\n");\r
683         }\r
684 //      printf("[%d]", map->data[q]);\r
685         q++;\r
686 //      }\r
687         //printf("\n\n");\r
688 //      }*/\r
689 \r
690         /*i=0;\r
691         for(y=0; y<map->height; y++) {\r
692                 for(x=0; x<map->width; x++) {\r
693 //                      map->data[i]=255;\r
694                         printf("[%d]", map->data[i]);\r
695                         //tile = tile ? 0 : 1;\r
696                         i++;\r
697                 }\r
698                 //tile = tile ? 0 : 1;\r
699         }*/\r
700 }\r
701 \r
702 \r
703 void\r
704 mapScrollRight(map_view_t *mv, byte offset) {\r
705         word x, y;  /* coordinate for drawing */\r
706 \r
707         /* increment the pixel position and update the page */\r
708         mv->page->dx += offset;\r
709 \r
710         /* check to see if this changes the tile */\r
711         if(mv->page->dx >= mv->dxThresh ) {\r
712         /* go forward one tile */\r
713         mv->tx++;\r
714         /* Snap the origin forward */\r
715         mv->page->data += 4;\r
716         mv->page->dx = mv->map->tiles->tileWidth;\r
717         //}\r
718 \r
719         /* draw the next column */\r
720         x= SCREEN_WIDTH + mv->map->tiles->tileWidth;\r
721                 mapDrawCol(mv, mv->tx + 20 , mv->ty-1, x);\r
722         }\r
723 }\r
724 \r
725 \r
726 void\r
727 mapScrollLeft(map_view_t *mv, byte offset) {\r
728         word x, y;  /* coordinate for drawing */\r
729 \r
730         /* increment the pixel position and update the page */\r
731         mv->page->dx -= offset;\r
732 \r
733         /* check to see if this changes the tile */\r
734         if(mv->page->dx == 0) {\r
735         /* go backward one tile */\r
736         mv->tx--;\r
737 \r
738         /* Snap the origin backward */\r
739         mv->page->data -= 4;\r
740         mv->page->dx = mv->map->tiles->tileWidth;\r
741         //}\r
742         /* draw the next column */\r
743                 mapDrawCol(mv, mv->tx-1, mv->ty-1, 0);\r
744         }\r
745 }\r
746 \r
747 \r
748 void\r
749 mapScrollUp(map_view_t *mv, byte offset) {\r
750         word x, y;  /* coordinate for drawing */\r
751 \r
752         /* increment the pixel position and update the page */\r
753         mv->page->dy -= offset;\r
754 \r
755         /* check to see if this changes the tile */\r
756         if(mv->page->dy == 0 ) {\r
757         /* go down one tile */\r
758         mv->ty--;\r
759         /* Snap the origin downward */\r
760         mv->page->data -= mv->page->width*4;\r
761         mv->page->dy = mv->map->tiles->tileHeight;\r
762         //}\r
763 \r
764         /* draw the next row */\r
765         y= 0;\r
766                 mapDrawRow(mv, mv->tx-1 , mv->ty-1, y);\r
767         }\r
768 }\r
769 \r
770 \r
771 void\r
772 mapScrollDown(map_view_t *mv, byte offset) {\r
773         word x, y;  /* coordinate for drawing */\r
774 \r
775         /* increment the pixel position and update the page */\r
776         mv->page->dy += offset;\r
777 \r
778         /* check to see if this changes the tile */\r
779         if(mv->page->dy >= mv->dyThresh ) {\r
780         /* go down one tile */\r
781         mv->ty++;\r
782         /* Snap the origin downward */\r
783         mv->page->data += mv->page->width*4;\r
784         mv->page->dy = mv->map->tiles->tileHeight;\r
785         //}\r
786 \r
787         /* draw the next row */\r
788         y= SCREEN_HEIGHT + mv->map->tiles->tileHeight;\r
789                 mapDrawRow(mv, mv->tx-1 , mv->ty+15, y);\r
790         }\r
791 \r
792 }\r
793 \r
794 \r
795 void\r
796 mapGoTo(map_view_t *mv, int tx, int ty) {\r
797         int px, py;\r
798         unsigned int i;\r
799 \r
800         /* set up the coordinates */\r
801         mv->tx = tx;\r
802         mv->ty = ty;\r
803         mv->page->dx = mv->map->tiles->tileWidth;\r
804         mv->page->dy = mv->map->tiles->tileHeight;\r
805 \r
806         /* set up the thresholds */\r
807         mv->dxThresh = mv->map->tiles->tileWidth * 2;\r
808         mv->dyThresh = mv->map->tiles->tileHeight * 2;\r
809 \r
810         /* draw the tiles */\r
811         modexClearRegion(mv->page, 0, 0, mv->page->width, mv->page->height, 0);\r
812         py=0;\r
813         i=mv->ty * mv->map->width + mv->tx;\r
814         for(ty=mv->ty-1; py < SCREEN_HEIGHT+mv->dyThresh && ty < mv->map->height; ty++, py+=mv->map->tiles->tileHeight) {\r
815                 mapDrawRow(mv, tx-1, ty, py);\r
816         i+=mv->map->width - tx;\r
817         }\r
818 }\r
819 \r
820 \r
821 void\r
822 mapDrawTile(tiles_t *t, word i, page_t *page, word x, word y) {\r
823         word rx;\r
824         word ry;\r
825         //if(i==0) i=2;\r
826         if(i==0)\r
827         {\r
828                 //wwww\r
829         }\r
830         else\r
831         {\r
832         rx = (((i-1) % ((t->data->width)/t->tileWidth)) * t->tileWidth);\r
833         ry = (((i-1) / ((t->data->height)/t->tileHeight)) * t->tileHeight);\r
834         //mxPutTile(t->data, x, y, t->tileWidth, t->tileHeight);\r
835         modexDrawBmpRegion(page, x, y, rx, ry, t->tileWidth, t->tileHeight, (t->data));\r
836         }\r
837 }\r
838 \r
839 \r
840 void\r
841 mapDrawRow(map_view_t *mv, int tx, int ty, word y) {\r
842         word x;\r
843         int i;\r
844 \r
845         /* the position within the map array */\r
846         i=ty * mv->map->width + tx;\r
847         for(x=0; x<SCREEN_WIDTH+mv->dxThresh && tx < mv->map->width; x+=mv->map->tiles->tileWidth, tx++) {\r
848         if(i>=0) {\r
849                 /* we are in the map, so copy! */\r
850                 mapDrawTile(mv->map->tiles, mv->map->data[i], mv->page, x, y);\r
851         }\r
852         i++; /* next! */\r
853         }\r
854 }\r
855 \r
856 void\r
857 mapDrawCol(map_view_t *mv, int tx, int ty, word x) {\r
858         int y;\r
859         int i;\r
860 \r
861         /* location in the map array */\r
862         i=ty * mv->map->width + tx;\r
863 \r
864         /* We'll copy all of the columns in the screen, \r
865            i + 1 row above and one below */\r
866         for(y=0; y<SCREEN_HEIGHT+mv->dyThresh && ty < mv->map->height; y+=mv->map->tiles->tileHeight, ty++) {\r
867         if(i>=0) {\r
868                 /* we are in the map, so copy away! */\r
869                 mapDrawTile(mv->map->tiles, mv->map->data[i], mv->page, x, y);\r
870         }\r
871         i += mv->map->width;\r
872         }\r
873 }\r
874 \r
875 void qclean()\r
876 {\r
877         modexLeave();\r
878         setkb(0);\r
879 }\r
880 \r
881 void pdump(map_view_t *pee)\r
882 {\r
883         int mult=(QUADWH);\r
884         int palq=(mult)*TILEWH;\r
885         int palcol=0;\r
886         int palx, paly;\r
887         for(paly=0; paly<palq; paly+=mult){\r
888                 for(palx=0; palx<palq; palx+=mult){\r
889                                 modexClearRegion(pee->page, palx+TILEWH, paly+TILEWH, mult, mult, palcol);\r
890                         palcol++;\r
891                 }\r
892         }\r
893 }\r
894 \r
895 void\r
896 animatePlayer(map_view_t *src, map_view_t *dest, /*map_view_t *top, */sword d, short scrolloffsetswitch, int x, int y, int ls, int lp, bitmap_t *bmp)\r
897 {\r
898         sword dire=32*d; //direction\r
899         sword qq; //scroll offset\r
900 \r
901         if(scrolloffsetswitch==0) qq = 0;\r
902         else qq = ((lp)*SPEED);\r
903         switch (d)\r
904         {\r
905                 case 0:\r
906                         //up\r
907                         x=x-4;\r
908                         y=y-qq-TILEWH;\r
909                 break;\r
910                 case 1:\r
911                         // right\r
912                         x=x+qq-4;\r
913                         y=y-TILEWH;\r
914                 break;\r
915                 case 2:\r
916                         //down\r
917                         x=x-4;\r
918                         y=y+qq-TILEWH;\r
919                 break;\r
920                 case 3:\r
921                         //left\r
922                         x=x-qq-4;\r
923                         y=y-TILEWH;\r
924                 break;\r
925         }\r
926         modexCopyPageRegion(dest->page, src->page, x-4, y-4, x-4, y-4, 28, 40);\r
927         if(2>ls && ls>=1) { modexDrawSpriteRegion(dest->page, x, y, 48, dire, 24, 32, bmp); }else\r
928         if(3>ls && ls>=2) { modexDrawSpriteRegion(dest->page, x, y, 24, dire, 24, 32, bmp); }else\r
929         if(4>ls && ls>=3) { modexDrawSpriteRegion(dest->page, x, y, 0, dire, 24, 32, bmp); }else\r
930         if(5>ls && ls>=4) { modexDrawSpriteRegion(dest->page, x, y, 24, dire, 24, 32, bmp); }\r
931         //TODO: mask copy //modexCopyPageRegion(dest->page, src->page, x-4, y-4, x-4, y-4, 28, 40);\r
932         //modexClearRegion(top->page, 66, 66, 2, 40, 0);\r
933         //modexCopyPageRegion(dest->page, top->page, 66, 66, 66, 66, 2, 40);\r
934         //turn this off if XT\r
935         //if(detectcpu() > 0) modexWaitBorder();\r
936 }\r