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