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