OSDN Git Service

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