]> 4ch.mooo.com Git - 16.git/blob - src/scroll.c
I just realized it dose not support colors of different RGB values!!!
[16.git] / src / scroll.c
1 #include "src/lib/dos_kb.h"
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"
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();
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
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() {
77         size_t oldfreemem=GetFreeSize();\r
78         long emmhandle;\r
79         long emsavail;\r
80         char teststr[80];\r
81         int i;
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
123         /* create the map */
124 //0000  printf("Total used @ before map load:                   %zu\n", oldfreemem-GetFreeSize());
125         loadmap("data/test.map", &map);
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
127         //if(isEMS()) printf("%d tesuto\n", coretotalEMS());
128 //----  initMap(&map);
129         mv.map = &map;
130         mv2.map = &map;
131         mv3.map = &map;
132
133         /* draw the tiles */
134         ptr = map.data;
135         mappalptr = map.tiles->data->palette;
136         /* data */
137 //0000  printf("Total used @ before image loading:              %zu\n", oldfreemem-GetFreeSize());
138         ptmp = bitmapLoadPcx("data/ptmp.pcx"); // load sprite
139         //npctmp = bitmapLoadPcx("ptmp1.pcx"); // load sprite
140         
141         /* create the planar buffer */
142 //0000  printf("Total used @ before planar buffer creation:     %zu\n", oldfreemem-GetFreeSize());
143         p = planar_buf_from_bitmap(&ptmp);\r
144 //0000  printf("Total used @ after planar buffer creation:      %zu\n", oldfreemem-GetFreeSize());
145
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
177         /* save the palette */
178         dpal = modexNewPal();
179         modexPalSave(dpal);
180         modexFadeOff(4, dpal);
181
182         setkb(1);
183         modexEnter();
184         modexPalBlack();        //reset the palette~
185 //      printf("Total used @ before palette initiation:         %zu\n", oldfreemem-GetFreeSize());
186         ptmp.offset=(paloffset/3);
187         modexPalUpdate(&ptmp, &paloffset, 0, 0);
188         //printf("      %d\n", sizeof(ptmp.data));
189         //printf("1:    %d\n", paloffset);
190         map.tiles->data->offset=(paloffset/3);
191         modexPalUpdate(map.tiles->data, &paloffset, 0, 0);
192         printf("\n====\n");
193         printf("0       paloffset=      %d\n", paloffset/3);
194         printf("====\n\n");
195         gpal = modexNewPal();\r
196         modexPalSave(gpal);
197         modexSavePalFile("data/g.pal", gpal);
198         modexPalBlack();        //so player will not see loadings~
199
200         /* setup camera and screen~ */
201         screen = modexDefaultPage();
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);
249 //      printf("Total used @ before loop:                       %zu\n", oldfreemem-GetFreeSize());
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; }
548         if(keyp(25)){ pdump(bg); pdump(spri); }
549         if(keyp(24)){ modexPalUpdate3(gpal); paloffset=0; pdump(bg); pdump(spri); }
550         if(keyp(22)){ 
551         paloffset=0; modexPalBlack(); modexPalUpdate(&ptmp, &paloffset, 0, 0);
552         printf("1paloffset      =       %d\n", paloffset/3);
553          modexPalUpdate(map.tiles->data, &paloffset, 0, 0);
554         printf("2paloffset      =       %d\n", paloffset/3);
555          pdump(bg); pdump(spri); }
556
557         if(keyp(87))
558         {
559                 modexLeave();\r
560                 setkb(0);
561                 __asm
562                 {
563                         mov ah,31h
564                         int 21h
565                 }
566         }\r
567         }\r
568 \r
569         /* fade back to text mode */
570         /* but 1st lets save the game palette~ */
571         modexPalSave(gpal);
572         modexSavePalFile("data/g.pal", gpal);
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);
590         printf("palette offset: %d\n", paloffset/3);
591         printf("Total used: %zu\n", oldfreemem-GetFreeSize());
592         printf("Total free: %zu\n", GetFreeSize());
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("\n");\r
596         //xmsfree(&map);\r
597         //xmsfree(bg);\r
598         //xmsfree(spri);\r
599         //xmsfree(mask);\r
600         //xmsreport();\r
601         //emmclose(emmhandle);\r
602         switch(detectcpu())\r
603         {\r
604                 case 0: cpus = "8086/8088 or 186/88"; break;\r
605                 case 1: cpus = "286"; break;\r
606                 case 2: cpus = "386 or newer"; break;\r
607                 default: cpus = "internal error"; break;\r
608         }\r
609         printf("detected CPU type: %s\n", cpus);
610         modexFadeOn(4, dpal);\r
611 }\r
612 \r
613 \r
614 map_t\r
615 allocMap(int w, int h) {\r
616         map_t result;\r
617 \r
618         result.width =w;\r
619         result.height=h;\r
620         result.data = malloc(sizeof(byte) * w * h);\r
621         //result.data = (byte *)alloc_emem(((int)sizeof(byte) * w * h)/1024);\r
622         /*if(isEMS() || checkEMS())\r
623         {\r
624                 XMOVE mm;\r
625                 //emmhandle = mallocEMS(coretotalEMS());//alloc_emem((int)sizeof(map))\r
626                 mm.length=sizeof(result);\r
627                 mm.sourceH=0;\r
628                 mm.sourceOff=ptr2long(&result);\r
629                 mm.destH=emmhandle;\r
630                 mm.destOff=0;\r
631                 ist = move_emem(&mm);\r
632                 if(!ist){ dealloc_emem(emmhandle); exit(5); }\r
633                 printf("%d\n", coretotalEMS());\r
634         }*/\r
635 \r
636         return result;\r
637 }\r
638 \r
639 void\r
640 initMap(map_t *map) {\r
641         /* just a place holder to fill out an alternating pattern */\r
642         int x, y, xx, yy;\r
643         int i, q;\r
644 //      int tile = 1;\r
645         //if(!isEMS() || !checkEMS())\r
646 //              map->tiles = malloc(sizeof(tiles_t));\r
647         //else\r
648         //      map->tiles = (tiles_t *)alloc_emem(sizeof(tiles_t));\r
649 \r
650         /* create the tile set */\r
651         //if(!isEMS() || !checkEMS())\r
652 //              map->tiles->data = malloc(sizeof(bitmap_t));\r
653         //else\r
654         //      map->tiles->data = (bitmap_t *)alloc_emem(sizeof(bitmap_t));\r
655 //      map->tiles->data->width = (TILEWH/**2*/);\r
656 //      map->tiles->data->height= TILEWH;\r
657         //if(!isEMS() || !checkEMS())\r
658 //              map->tiles->data->data = malloc((TILEWH*2)*TILEWH);\r
659         //else\r
660         //      map->tiles->data->data = (byte *)alloc_emem((TILEWH*2)*TILEWH);\r
661 //      map->tiles->tileHeight = TILEWH;\r
662 //      map->tiles->tileWidth =TILEWH;\r
663 //      map->tiles->rows = 1;\r
664 //      map->tiles->cols = 1;//2;\r
665 \r
666         /*q=0;
667         //for(y=0; y<map->height; y++) {\r
668         //for(x=0; x<map->width; x++) {
669         i=0;\r
670         for(yy=0; yy<TILEWH; yy++) {\r
671         for(xx=0; xx<(TILEWH); xx++) {\r
672                 //if(x<TILEWH){\r
673                   map->tiles->data->data[i+1] = map->data[q];//28;//0x24;
674 //                printf("[%d]", map->tiles->data->data[i]);\r
675                 //}else{\r
676                   //map->tiles->data->data[i] = map->data[q];//0;//0x34;
677                   //printf("]%d[==[%d]", i, map->tiles->data->data[i]);
678                 //}\r
679                 i++;\r
680         }
681 //      printf("\n");
682         }
683 //      printf("[%d]", map->data[q]);
684         q++;
685 //      }
686         //printf("\n\n");\r
687 //      }*/\r
688 \r
689         /*i=0;\r
690         for(y=0; y<map->height; y++) {\r
691                 for(x=0; x<map->width; x++) {
692 //                      map->data[i]=255;\r
693                         printf("[%d]", map->data[i]);\r
694                         //tile = tile ? 0 : 1;\r
695                         i++;\r
696                 }\r
697                 //tile = tile ? 0 : 1;\r
698         }*/\r
699 }\r
700 \r
701 \r
702 void\r
703 mapScrollRight(map_view_t *mv, byte offset) {\r
704         word x, y;  /* coordinate for drawing */\r
705 \r
706         /* increment the pixel position and update the page */\r
707         mv->page->dx += offset;\r
708 \r
709         /* check to see if this changes the tile */\r
710         if(mv->page->dx >= mv->dxThresh ) {\r
711         /* go forward one tile */\r
712         mv->tx++;\r
713         /* Snap the origin forward */\r
714         mv->page->data += 4;\r
715         mv->page->dx = mv->map->tiles->tileWidth;\r
716         //}\r
717 \r
718         /* draw the next column */\r
719         x= SCREEN_WIDTH + mv->map->tiles->tileWidth;\r
720                 mapDrawCol(mv, mv->tx + 20 , mv->ty-1, x);\r
721         }\r
722 }\r
723 \r
724 \r
725 void\r
726 mapScrollLeft(map_view_t *mv, byte offset) {\r
727         word x, y;  /* coordinate for drawing */\r
728 \r
729         /* increment the pixel position and update the page */\r
730         mv->page->dx -= offset;\r
731 \r
732         /* check to see if this changes the tile */\r
733         if(mv->page->dx == 0) {\r
734         /* go backward one tile */\r
735         mv->tx--;\r
736 \r
737         /* Snap the origin backward */\r
738         mv->page->data -= 4;\r
739         mv->page->dx = mv->map->tiles->tileWidth;\r
740         //}\r
741         /* draw the next column */\r
742                 mapDrawCol(mv, mv->tx-1, mv->ty-1, 0);\r
743         }\r
744 }\r
745 \r
746 \r
747 void\r
748 mapScrollUp(map_view_t *mv, byte offset) {\r
749         word x, y;  /* coordinate for drawing */\r
750 \r
751         /* increment the pixel position and update the page */\r
752         mv->page->dy -= offset;\r
753 \r
754         /* check to see if this changes the tile */\r
755         if(mv->page->dy == 0 ) {\r
756         /* go down one tile */\r
757         mv->ty--;\r
758         /* Snap the origin downward */\r
759         mv->page->data -= mv->page->width*4;\r
760         mv->page->dy = mv->map->tiles->tileHeight;\r
761         //}\r
762 \r
763         /* draw the next row */\r
764         y= 0;\r
765                 mapDrawRow(mv, mv->tx-1 , mv->ty-1, y);\r
766         }\r
767 }\r
768 \r
769 \r
770 void\r
771 mapScrollDown(map_view_t *mv, byte offset) {\r
772         word x, y;  /* coordinate for drawing */\r
773 \r
774         /* increment the pixel position and update the page */\r
775         mv->page->dy += offset;\r
776 \r
777         /* check to see if this changes the tile */\r
778         if(mv->page->dy >= mv->dyThresh ) {\r
779         /* go down one tile */\r
780         mv->ty++;\r
781         /* Snap the origin downward */\r
782         mv->page->data += mv->page->width*4;\r
783         mv->page->dy = mv->map->tiles->tileHeight;\r
784         //}\r
785 \r
786         /* draw the next row */\r
787         y= SCREEN_HEIGHT + mv->map->tiles->tileHeight;\r
788                 mapDrawRow(mv, mv->tx-1 , mv->ty+15, y);\r
789         }\r
790 \r
791 }\r
792 \r
793 \r
794 void\r
795 mapGoTo(map_view_t *mv, int tx, int ty) {\r
796         int px, py;\r
797         unsigned int i;\r
798 \r
799         /* set up the coordinates */\r
800         mv->tx = tx;\r
801         mv->ty = ty;\r
802         mv->page->dx = mv->map->tiles->tileWidth;\r
803         mv->page->dy = mv->map->tiles->tileHeight;\r
804 \r
805         /* set up the thresholds */\r
806         mv->dxThresh = mv->map->tiles->tileWidth * 2;\r
807         mv->dyThresh = mv->map->tiles->tileHeight * 2;\r
808 \r
809         /* draw the tiles */\r
810         modexClearRegion(mv->page, 0, 0, mv->page->width, mv->page->height, 0);\r
811         py=0;\r
812         i=mv->ty * mv->map->width + mv->tx;\r
813         for(ty=mv->ty-1; py < SCREEN_HEIGHT+mv->dyThresh && ty < mv->map->height; ty++, py+=mv->map->tiles->tileHeight) {\r
814                 mapDrawRow(mv, tx-1, ty, py);\r
815         i+=mv->map->width - tx;\r
816         }\r
817 }\r
818 \r
819 \r
820 void\r
821 mapDrawTile(tiles_t *t, word i, page_t *page, word x, word y) {\r
822         word rx;\r
823         word ry;
824         //if(i==0) i=2;
825         if(i==0)
826         {
827                 //wwww
828         }
829         else
830         {
831         rx = (((i-1) % ((t->data->width)/t->tileWidth)) * t->tileWidth);\r
832         ry = (((i-1) / ((t->data->height)/t->tileHeight)) * t->tileHeight);
833         //mxPutTile(t->data, x, y, t->tileWidth, t->tileHeight);\r
834         modexDrawBmpRegion(page, x, y, rx, ry, t->tileWidth, t->tileHeight, (t->data));
835         }\r
836 }\r
837 \r
838 \r
839 void\r
840 mapDrawRow(map_view_t *mv, int tx, int ty, word y) {\r
841         word x;\r
842         int i;\r
843 \r
844         /* the position within the map array */\r
845         i=ty * mv->map->width + tx;\r
846         for(x=0; x<SCREEN_WIDTH+mv->dxThresh && tx < mv->map->width; x+=mv->map->tiles->tileWidth, tx++) {\r
847         if(i>=0) {\r
848                 /* we are in the map, so copy! */
849                 mapDrawTile(mv->map->tiles, mv->map->data[i], mv->page, x, y);\r
850         }\r
851         i++; /* next! */\r
852         }\r
853 }\r
854 \r
855 void\r
856 mapDrawCol(map_view_t *mv, int tx, int ty, word x) {\r
857         int y;\r
858         int i;\r
859 \r
860         /* location in the map array */\r
861         i=ty * mv->map->width + tx;\r
862 \r
863         /* We'll copy all of the columns in the screen, \r
864            i + 1 row above and one below */\r
865         for(y=0; y<SCREEN_HEIGHT+mv->dyThresh && ty < mv->map->height; y+=mv->map->tiles->tileHeight, ty++) {\r
866         if(i>=0) {\r
867                 /* we are in the map, so copy away! */
868                 mapDrawTile(mv->map->tiles, mv->map->data[i], mv->page, x, y);\r
869         }\r
870         i += mv->map->width;\r
871         }\r
872 }\r
873 \r
874 void qclean()\r
875 {\r
876         modexLeave();\r
877         setkb(0);\r
878 }
879
880 void pdump(map_view_t *pee)
881 {\r
882         int mult=(QUADWH);\r
883         int palq=(mult)*TILEWH;\r
884         int palcol=0;
885         int palx, paly;
886         for(paly=0; paly<palq; paly+=mult){\r
887                 for(palx=0; palx<palq; palx+=mult){
888                                 modexClearRegion(pee->page, palx+TILEWH, paly+TILEWH, mult, mult, palcol);\r
889                         palcol++;\r
890                 }\r
891         }\r
892 }
893 \r
894 void\r
895 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
896 {\r
897         sword dire=32*d; //direction\r
898         sword qq; //scroll offset\r
899 \r
900         if(scrolloffsetswitch==0) qq = 0;\r
901         else qq = ((lp)*SPEED);\r
902         switch (d)\r
903         {\r
904                 case 0:\r
905                         //up\r
906                         x=x-4;\r
907                         y=y-qq-TILEWH;\r
908                 break;\r
909                 case 1:\r
910                         // right\r
911                         x=x+qq-4;\r
912                         y=y-TILEWH;\r
913                 break;\r
914                 case 2:\r
915                         //down\r
916                         x=x-4;\r
917                         y=y+qq-TILEWH;\r
918                 break;\r
919                 case 3:\r
920                         //left\r
921                         x=x-qq-4;\r
922                         y=y-TILEWH;\r
923                 break;\r
924         }\r
925         modexCopyPageRegion(dest->page, src->page, x-4, y-4, x-4, y-4, 28, 40);\r
926         if(2>ls && ls>=1) { modexDrawSpriteRegion(dest->page, x, y, 48, dire, 24, 32, bmp); }else\r
927         if(3>ls && ls>=2) { modexDrawSpriteRegion(dest->page, x, y, 24, dire, 24, 32, bmp); }else\r
928         if(4>ls && ls>=3) { modexDrawSpriteRegion(dest->page, x, y, 0, dire, 24, 32, bmp); }else\r
929         if(5>ls && ls>=4) { modexDrawSpriteRegion(dest->page, x, y, 24, dire, 24, 32, bmp); }\r
930         //TODO: mask copy //modexCopyPageRegion(dest->page, src->page, x-4, y-4, x-4, y-4, 28, 40);\r
931         //modexClearRegion(top->page, 66, 66, 2, 40, 0);\r
932         //modexCopyPageRegion(dest->page, top->page, 66, 66, 66, 66, 2, 40);\r
933         //turn this off if XT\r
934         if(detectcpu() > 0) modexWaitBorder();\r
935 }\r