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