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