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