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