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