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