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