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