OSDN Git Service

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