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