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