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