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