]> 4ch.mooo.com Git - 16.git/blob - scroll.c
modified: scroll.c
[16.git] / scroll.c
1 #include "modex16.h"
2 #include <stdio.h>
3 #include <stdlib.h>
4 #include "dos_kb.h"
5
6 //word far *clock= (word far*) 0x046C; /* 18.2hz clock */
7
8 typedef struct {
9         bitmap_t *data;
10         word tileHeight;
11         word tileWidth;
12         unsigned int rows;
13         unsigned int cols;
14         //unsigned int tilex,tiley; // tile position on the map
15 } tiles_t;
16
17
18 typedef struct {
19         byte    *data;
20         tiles_t *tiles;
21         int width;
22         int height;
23 } map_t;
24
25
26 typedef struct {
27         map_t *map;
28         page_t *page;
29         int tx; //appears to be the top left tile position on the viewable screen map
30         int ty; //appears to be the top left tile position on the viewable screen map
31         word dxThresh; //????
32         word dyThresh; //????
33 } map_view_t;
34
35 struct {
36         int x; //player exact position on the viewable map
37         int y; //player exact position on the viewable map
38         int tx; //player tile position on the viewable map
39         int ty; //player tile position on the viewable map
40         int hp; //hitpoints of the player
41 } player;
42
43
44 map_t allocMap(int w, int h);
45 void initMap(map_t *map);
46 void mapScrollRight(map_view_t *mv, byte offset);
47 void mapScrollLeft(map_view_t *mv, byte offest);
48 void mapScrollUp(map_view_t *mv, byte offset);
49 void mapScrollDown(map_view_t *mv, byte offset);
50 void mapGoTo(map_view_t *mv, int tx, int ty);
51 void mapDrawTile(tiles_t *t, word i, page_t *page, word x, word y);
52 void mapDrawRow(map_view_t *mv, int tx, int ty, word y);
53 void mapDrawCol(map_view_t *mv, int tx, int ty, word x);
54 void animatePlayer(map_view_t *src, map_view_t *dest, short d1, short d2, int x, int y, int ls, bitmap_t *bmp);
55
56 #define TILEWH 16
57 #define QUADWH (TILEWH/4)
58 #define SPEED 2
59
60 //place holder definitions
61 #define MAPX 40
62 #define MAPY 30
63 #define SWAP(a, b) tmp=a; a=b; b=tmp;
64 void main() {
65         bitmap_t ptmp; // player sprite
66         int q=0;
67         page_t screen, screen2;
68         map_t map;
69         map_view_t mv, mv2;
70         map_view_t *bg, *spri, *tmp;
71         byte *ptr;
72
73         setkb(1);
74         /* create the map */
75         map = allocMap(MAPX,MAPY); //20x15 is the resolution of the screen you can make maps smaller than 20x15 but the null space needs to be bgn properly
76         initMap(&map);
77         mv.map = &map;
78         mv2.map = &map;
79
80         /* bg the tiles */
81         ptr = map.data;
82         ptmp = bitmapLoadPcx("ptmp.pcx"); // load sprite
83         modexEnter();
84         modexPalUpdate(ptmp.palette);
85         screen = modexDefaultPage();
86         screen.width += (TILEWH*2);
87         mv.page = &screen;
88         screen2 = modexNextPage(mv.page);
89         screen2.width += (TILEWH*2);
90         mv2.page = &screen2;
91         //modexShowPage(mv.page);
92
93         /* set up paging */
94         bg = &mv;
95         spri = &mv2;
96
97 //TODO: LOAD map data and position the map in the middle of the screen if smaller then screen
98         mapGoTo(bg, 0, 0);
99         mapGoTo(spri, 0, 0);
100
101         //TODO: put player in starting position of spot
102         //default player position on the viewable map
103         player.tx = bg->tx + 10;
104         player.ty = bg->ty + 8;
105         player.x = player.tx*TILEWH;
106         player.y = player.ty*TILEWH;
107         modexDrawSpriteRegion(spri->page, player.x-4, player.y-TILEWH, 24, 64, 24, 32, &ptmp);
108         modexCopyPageRegion(bg->page, spri->page, player.x-4, player.y-TILEWH-2, player.x-4, player.y-TILEWH-2, 24, 36);
109         modexShowPage(bg->page);
110         while(!keyp(1))
111         {
112         //top left corner & bottem right corner of map veiw be set as map edge trigger since maps are actually square
113         //to stop scrolling and have the player position data move to the edge of the screen with respect to the direction
114         //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
115         
116         //TODO: render the player properly with animation and sprite sheet
117         //TODO: fexible speeds
118         if(keyp(77) && !keyp(75))
119         {
120                 if(bg->tx >= 0 && bg->tx+20 < MAPX && player.tx == bg->tx + 10)
121                 {
122                         for(q=0; q<(TILEWH/SPEED); q++)
123                         {
124                                 animatePlayer(bg, spri, 1, 1, player.x, player.y, q, &ptmp);
125                                 mapScrollRight(bg, SPEED);
126                                 mapScrollRight(spri, SPEED);
127                                 modexShowPage(spri->page);
128                         }
129                         player.tx++;
130                 }
131                 else if(player.tx < MAPX)
132                 {
133                         for(q=0; q<(TILEWH/SPEED); q++)
134                         {
135                                 player.x+=SPEED;
136                                 animatePlayer(bg, spri, 1, 0, player.x, player.y, q, &ptmp);
137                                 modexShowPage(spri->page);
138                         }
139                         player.tx++;
140                 }
141                 else
142                 {
143                         modexCopyPageRegion(spri->page, bg->page, player.x-4, player.y-TILEWH-2, player.x-4, player.y-TILEWH-2, 24, 36);
144                         modexDrawSpriteRegion(spri->page, player.x-4, player.y-TILEWH, 24, 32, 24, 32, &ptmp);
145                         modexShowPage(spri->page);
146                 }
147         }
148
149         if(keyp(75) && !keyp(77))
150         {
151                 if(bg->tx > 0 && bg->tx+20 <= MAPX && player.tx == bg->tx + 10)
152                 {
153                         for(q=0; q<(TILEWH/SPEED); q++)
154                         {
155                                 
156                                 animatePlayer(bg, spri, 3, 1, player.x, player.y, q, &ptmp);
157                                 mapScrollLeft(bg, SPEED);
158                                 mapScrollLeft(spri, SPEED);
159                                 modexShowPage(spri->page);
160                         }
161                         player.tx--;
162                 }
163                 else if(player.tx > 1)
164                 {
165                         for(q=0; q<(TILEWH/SPEED); q++)
166                         {
167                                 player.x-=SPEED;
168                                 animatePlayer(bg, spri, 3, 0, player.x, player.y, q, &ptmp);
169                                 modexShowPage(spri->page);
170                         }
171                         player.tx--;
172                 }
173                 else
174                 {
175                         modexCopyPageRegion(spri->page, bg->page, player.x-4, player.y-TILEWH-2, player.x-4, player.y-TILEWH-2, 24, 36);
176                         modexDrawSpriteRegion(spri->page, player.x-4, player.y-TILEWH, 24, 96, 24, 32, &ptmp);
177                         modexShowPage(spri->page);
178                 }
179         }
180
181         if(keyp(80) && !keyp(72))
182         {
183                 if(bg->ty >= 0 && bg->ty+15 < MAPY && player.ty == bg->ty + 8)
184                 {
185                         for(q=0; q<(TILEWH/SPEED); q++)
186                         {
187                                 animatePlayer(bg, spri, 2, 1, player.x, player.y, q, &ptmp);
188                                 mapScrollDown(bg, SPEED);
189                                 mapScrollDown(spri, SPEED);
190                                 modexShowPage(spri->page);
191                         }
192                         player.ty++;
193                 }
194                 else if(player.ty < MAPY)
195                 {
196                         for(q=0; q<(TILEWH/SPEED); q++)
197                         {
198                                 player.y+=SPEED;
199                                 animatePlayer(bg, spri, 2, 0, player.x, player.y, q, &ptmp);
200                                 modexShowPage(spri->page);
201                         }
202                         player.ty++;
203                 }
204                 else
205                 {
206                         modexCopyPageRegion(spri->page, bg->page, player.x-4, player.y-TILEWH-2, player.x-4, player.y-TILEWH-2, 24, 36);
207                         modexDrawSpriteRegion(spri->page, player.x-4, player.y-TILEWH, 24, 64, 24, 32, &ptmp);
208                         modexShowPage(spri->page);
209                 }
210         }
211
212         if(keyp(72) && !keyp(80))
213         {
214                 if(bg->ty > 0 && bg->ty+15 <= MAPY && player.ty == bg->ty + 8)
215                 {
216                         for(q=0; q<(TILEWH/SPEED); q++)
217                         {
218                                 animatePlayer(bg, spri, 0, 1, player.x, player.y, q, &ptmp);
219                                 mapScrollUp(bg, SPEED);
220                                 mapScrollUp(spri, SPEED);
221                                 modexShowPage(spri->page);
222                         }
223                         player.ty--;
224                 }
225                 else if(player.ty > 1)
226                 {
227                         for(q=0; q<(TILEWH/SPEED); q++)
228                         {
229                                 player.y-=SPEED;
230                                 animatePlayer(bg, spri, 0, 0, player.x, player.y, q, &ptmp);
231                                 modexShowPage(spri->page);
232                         }
233                         player.ty--;
234                 }
235                 else
236                 {
237                         modexCopyPageRegion(spri->page, bg->page, player.x-4, player.y-TILEWH-2, player.x-4, player.y-TILEWH-2, 24, 36);
238                         modexDrawSpriteRegion(spri->page, player.x-4, player.y-TILEWH, 24, 0, 24, 32, &ptmp);
239                         modexShowPage(spri->page);
240                 }
241         }
242         
243         }
244
245         modexLeave();
246         setkb(0);
247         printf("Project 16 scroll.exe\n");
248         printf("tx: %d\n", bg->tx);
249         printf("ty: %d\n", bg->ty);
250         printf("player.x: %d\n", player.x);
251         printf("player.y: %d\n", player.y);
252         printf("player.tx: %d\n", player.tx);
253         printf("player.ty: %d\n", player.ty);
254 }
255
256
257 map_t
258 allocMap(int w, int h) {
259         map_t result;
260
261         result.width =w;
262         result.height=h;
263         result.data = malloc(sizeof(byte) * w * h);
264
265         return result;
266 }
267
268
269 void
270 initMap(map_t *map) {
271         /* just a place holder to fill out an alternating pattern */
272         int x, y;
273         int i;
274         int tile = 1;
275         map->tiles = malloc(sizeof(tiles_t));
276
277         /* create the tile set */
278         map->tiles->data = malloc(sizeof(bitmap_t));
279         map->tiles->data->width = (TILEWH*2);
280         map->tiles->data->height= TILEWH;
281         map->tiles->data->data = malloc((TILEWH*2)*TILEWH);
282         map->tiles->tileHeight = TILEWH;
283         map->tiles->tileWidth =TILEWH;
284         map->tiles->rows = 1;
285         map->tiles->cols = 2;
286
287         i=0;
288         for(y=0; y<TILEWH; y++) {
289         for(x=0; x<(TILEWH*2); x++) {
290                 if(x<TILEWH)
291                   map->tiles->data->data[i] = 0x24;
292                 else
293                   map->tiles->data->data[i] = 0x34;
294                 i++;
295         }
296         }
297
298         i=0;
299         for(y=0; y<map->height; y++) {
300                 for(x=0; x<map->width; x++) {
301                         map->data[i] = tile;
302                         tile = tile ? 0 : 1;
303                         i++;
304                 }
305                 tile = tile ? 0 : 1;
306         }
307 }
308
309
310 void
311 mapScrollRight(map_view_t *mv, byte offset) {
312         word x, y;  /* coordinate for bging */
313
314         /* increment the pixel position and update the page */
315         mv->page->dx += offset;
316
317         /* check to see if this changes the tile */
318         if(mv->page->dx >= mv->dxThresh ) {
319         /* go forward one tile */
320         mv->tx++;
321         /* Snap the origin forward */
322         mv->page->data += 4;
323         mv->page->dx = mv->map->tiles->tileWidth;
324
325
326         /* bg the next column */
327         x= SCREEN_WIDTH + mv->map->tiles->tileWidth;
328                 mapDrawCol(mv, mv->tx + 20 , mv->ty-1, x);
329         }
330 }
331
332
333 void
334 mapScrollLeft(map_view_t *mv, byte offset) {
335         word x, y;  /* coordinate for bging */
336
337         /* increment the pixel position and update the page */
338         mv->page->dx -= offset;
339
340         /* check to see if this changes the tile */
341         if(mv->page->dx == 0) {
342         /* go backward one tile */
343         mv->tx--;
344                 
345         /* Snap the origin backward */
346         mv->page->data -= 4;
347         mv->page->dx = mv->map->tiles->tileWidth;
348
349         /* bg the next column */
350                 mapDrawCol(mv, mv->tx-1, mv->ty-1, 0);
351         }
352 }
353
354
355 void
356 mapScrollUp(map_view_t *mv, byte offset) {
357         word x, y;  /* coordinate for bging */
358
359         /* increment the pixel position and update the page */
360         mv->page->dy -= offset;
361
362         /* check to see if this changes the tile */
363         if(mv->page->dy == 0 ) {
364         /* go down one tile */
365         mv->ty--;
366         /* Snap the origin downward */
367         mv->page->data -= mv->page->width*4;
368         mv->page->dy = mv->map->tiles->tileHeight;
369
370
371         /* bg the next row */
372         y= 0;
373                 mapDrawRow(mv, mv->tx-1 , mv->ty-1, y);
374         }
375 }
376
377
378 void
379 mapScrollDown(map_view_t *mv, byte offset) {
380         word x, y;  /* coordinate for bging */
381
382         /* increment the pixel position and update the page */
383         mv->page->dy += offset;
384
385         /* check to see if this changes the tile */
386         if(mv->page->dy >= mv->dyThresh ) {
387         /* go down one tile */
388         mv->ty++;
389         /* Snap the origin downward */
390         mv->page->data += mv->page->width*4;
391         mv->page->dy = mv->map->tiles->tileHeight;
392
393
394         /* bg the next row */
395         y= SCREEN_HEIGHT + mv->map->tiles->tileHeight;
396                 mapDrawRow(mv, mv->tx-1 , mv->ty+15, y);
397         }
398
399 }
400
401
402 void
403 mapGoTo(map_view_t *mv, int tx, int ty) {
404         int px, py;
405         unsigned int i;
406
407         /* set up the coordinates */
408         mv->tx = tx;
409         mv->ty = ty;
410         mv->page->dx = mv->map->tiles->tileWidth;
411         mv->page->dy = mv->map->tiles->tileHeight;
412
413         /* set up the thresholds */
414         mv->dxThresh = mv->map->tiles->tileWidth * 2;
415         mv->dyThresh = mv->map->tiles->tileHeight * 2;
416
417         /* bg the tiles */
418         modexClearRegion(mv->page, 0, 0, mv->page->width, mv->page->height, 0);
419         py=0;
420         i=mv->ty * mv->map->width + mv->tx;
421         for(ty=mv->ty-1; py < SCREEN_HEIGHT+mv->dyThresh && ty < mv->map->height; ty++, py+=mv->map->tiles->tileHeight) {
422                 mapDrawRow(mv, tx-1, ty, py);
423         i+=mv->map->width - tx;
424         }
425 }
426
427
428 void
429 mapDrawTile(tiles_t *t, word i, page_t *page, word x, word y) {
430         word rx;
431         word ry;
432         rx = (i % t->cols) * t->tileWidth;
433         ry = (i / t->cols) * t->tileHeight;
434         modexDrawBmpRegion(page, x, y, rx, ry, t->tileWidth, t->tileHeight, t->data);
435 }
436
437
438 void 
439 mapDrawRow(map_view_t *mv, int tx, int ty, word y) {
440         word x;
441         int i;
442
443         /* the position within the map array */
444         i=ty * mv->map->width + tx;
445         for(x=0; x<SCREEN_WIDTH+mv->dxThresh && tx < mv->map->width; x+=mv->map->tiles->tileWidth, tx++) {
446         if(i>=0) {
447                 /* we are in the map, so copy! */
448                 mapDrawTile(mv->map->tiles, mv->map->data[i], mv->page, x, y);
449         }
450         i++; /* next! */
451         }
452 }
453
454
455 void 
456 mapDrawCol(map_view_t *mv, int tx, int ty, word x) {
457         int y;
458         int i;
459
460         /* location in the map array */
461         i=ty * mv->map->width + tx;
462
463         /* We'll copy all of the columns in the screen, 
464            i + 1 row above and one below */
465         for(y=0; y<SCREEN_HEIGHT+mv->dyThresh && ty < mv->map->height; y+=mv->map->tiles->tileHeight, ty++) {
466         if(i>=0) {
467                 /* we are in the map, so copy away! */
468                 mapDrawTile(mv->map->tiles, mv->map->data[i], mv->page, x, y);
469         }
470         i += mv->map->width;
471         }
472 }
473
474 void
475 animatePlayer(map_view_t *src, map_view_t *dest, short d1, short d2, int x, int y, int ls, bitmap_t *bmp)
476 {
477         short dire=32*d1;
478         short qq;
479         short lo = ((TILEWH / SPEED) / 3);
480         short loo = (ls + lo);
481
482         if(d2==0) qq = 0;
483         else qq = ((ls+1)*SPEED);
484         switch (d1)
485         {
486                 case 0:
487                         //up
488                         x=x-4;
489                         y=y-qq-TILEWH;
490                 break;
491                 case 1:
492                         // right
493                         x=x+qq-4;
494                         y=y-TILEWH;
495                 break;
496                 case 2:
497                         //down
498                         x=x-4;
499                         y=y+qq-TILEWH;
500                 break;
501                 case 3:
502                         //left
503                         x=x-qq-4;
504                         y=y-TILEWH;
505                 break;
506         }               //TODO: make flexible animation thingy
507                         if(ls<1) { modexCopyPageRegion(dest->page, src->page, x, y-2, x, y-2, 24, 36);// modexWaitBorder();
508                         modexDrawSpriteRegion(dest->page, x, y, 24, dire, 24, 32, bmp); }else
509                         if(4>ls && ls>=1) { modexCopyPageRegion(dest->page, src->page, x, y-2, x, y-2, 24, 36);// modexWaitBorder();
510                         modexDrawSpriteRegion(dest->page, x, y, 48, dire, 24, 32, bmp); }else
511                         if(7>ls && ls>=4) { modexCopyPageRegion(dest->page, src->page, x, y-2, x, y-2, 24, 36);// modexWaitBorder();
512                         modexDrawSpriteRegion(dest->page, x, y, 0, dire, 24, 32, bmp); }else
513                         if(8>=ls && ls>=7) { modexCopyPageRegion(dest->page, src->page, x, y-2, x, y-2, 24, 36);// modexWaitBorder();
514                         modexDrawSpriteRegion(dest->page, x, y, 24, dire, 24, 32, bmp); }else ls-=ls;
515 }