]> 4ch.mooo.com Git - 16.git/blob - 16/modex16/scroll.c
16_ca needs huge amounts of work and I should remember what needs to be done soon...
[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         unsigned int tilex,tiley; // tile position on the map\r
15 } tiles_t;\r
16 \r
17 \r
18 typedef struct {\r
19         byte    *data;\r
20         tiles_t *tiles;\r
21         int width;\r
22         int height;\r
23 } map_t;\r
24 \r
25 \r
26 typedef struct {\r
27         map_t *map;\r
28         page_t *page;\r
29         int tx; //???? appears to be the tile position on the viewable screen map\r
30         int ty; //???? appears to be the tile position on the viewable screen map\r
31         word dxThresh; //????\r
32         word dyThresh; //????\r
33 } map_view_t;\r
34 \r
35 struct {\r
36         int tx; //player position on the viewable map\r
37         int ty; //player position on the viewable map\r
38 } player;\r
39 \r
40 \r
41 map_t allocMap(int w, int h);\r
42 void initMap(map_t *map);\r
43 void mapScrollRight(map_view_t *mv, byte offset);\r
44 void mapScrollLeft(map_view_t *mv, byte offest);\r
45 void mapScrollUp(map_view_t *mv, byte offset);\r
46 void mapScrollDown(map_view_t *mv, byte offset);\r
47 void mapGoTo(map_view_t *mv, int tx, int ty);\r
48 void mapDrawTile(tiles_t *t, word i, page_t *page, word x, word y);\r
49 void mapDrawRow(map_view_t *mv, int tx, int ty, word y);\r
50 void mapDrawCol(map_view_t *mv, int tx, int ty, word x);\r
51 \r
52 #define TILEWH 16\r
53 #define QUADWH (TILEWH/4)\r
54 //#define SWAP(a, b) tmp=a; a=b; b=tmp;\r
55 void main() {\r
56 //      int show1=1;\r
57         int tx, ty;\r
58         int x, y;\r
59         //int ch=0x0;\r
60 //      byte ch;\r
61         int q=0;\r
62         page_t screen;//,screen2;\r
63         map_t map;\r
64         map_view_t mv;//, mv2;\r
65         map_view_t *draw;//, *show, *tmp;\r
66         byte *ptr;\r
67 \r
68         //default player position on the viewable map\r
69         player.tx = 10;\r
70         player.ty = 8;\r
71 \r
72         setkb(1);\r
73         /* create the map */\r
74         map = allocMap(160,120); //20x15 is the resolution of the screen you can make maps smaller than 20x15 but the null space needs to be drawn properly\r
75         initMap(&map);\r
76         mv.map = &map;\r
77 //      mv2.map = &map;\r
78 \r
79         /* draw the tiles */\r
80         ptr = map.data;\r
81         modexEnter();\r
82         screen = modexDefaultPage();\r
83         screen.width += (TILEWH*2);\r
84         mv.page = &screen;\r
85         mapGoTo(&mv, 16, 16);\r
86 //      screen2=modexNextPage(mv.page);\r
87 //      mv2.page = &screen2;\r
88 //      mapGoTo(&mv2, 16, 16);\r
89 //      modexShowPage(mv.page);\r
90 \r
91         /* set up paging */\r
92 //      show = &mv;\r
93 //      draw = &mv2;\r
94         draw = &mv;\r
95 \r
96         //TODO: set player position data here according to the viewable map screen thingy\r
97 \r
98         while(!keyp(1)) {\r
99         //TODO: top left corner & bottem right corner of map veiw be set as map edge trigger since maps are actually square\r
100         //to stop scrolling and have the player position data move to the edge of the screen with respect to the direction\r
101         //when player.tx or player.ty == 0 or player.tx == 20 or player.ty == 15 then stop because that is edge of map and you do not want to walk of the map\r
102         if(keyp(77)){\r
103 //              for(q=0; q<TILEWH; q++) {\r
104                 mapScrollRight(draw, 1);\r
105 //              modexShowPage(draw->page);\r
106 //              mapScrollRight(draw, 1);\r
107 //              SWAP(draw, show);\r
108 //              }\r
109         }\r
110 \r
111         if(keyp(75)){\r
112 //              for(q=0; q<TILEWH; q++) {\r
113                 mapScrollLeft(draw, 1);\r
114 //              modexShowPage(draw->page);\r
115 //              mapScrollLeft(show, 1);\r
116 //              SWAP(draw, show);\r
117 //              }\r
118         }\r
119 \r
120         if(keyp(80)){\r
121 //              for(q=0; q<TILEWH; q++) {\r
122                 mapScrollDown(draw, 1);\r
123 //              modexShowPage(draw->page);\r
124 //              mapScrollDown(show, 1);\r
125 //              SWAP(draw, show);\r
126 //              }\r
127         }\r
128 \r
129         if(keyp(72)){\r
130 //              for(q=0; q<TILEWH; q++) {\r
131                 mapScrollUp(draw, 1);\r
132 //              modexShowPage(draw->page);\r
133 //              mapScrollUp(show, 1);\r
134 //              SWAP(draw, show);\r
135 //              }\r
136         }\r
137 \r
138         //keyp(ch);\r
139         modexShowPage(draw->page);\r
140 \r
141         }\r
142 \r
143         modexLeave();\r
144         setkb(0);\r
145 }\r
146 \r
147 \r
148 map_t\r
149 allocMap(int w, int h) {\r
150         map_t result;\r
151 \r
152         result.width =w;\r
153         result.height=h;\r
154         result.data = malloc(sizeof(byte) * w * h);\r
155 \r
156         return result;\r
157 }\r
158 \r
159 \r
160 void\r
161 initMap(map_t *map) {\r
162         /* just a place holder to fill out an alternating pattern */\r
163         int x, y;\r
164         int i;\r
165         int tile = 1;\r
166         map->tiles = malloc(sizeof(tiles_t));\r
167 \r
168         /* create the tile set */\r
169         map->tiles->data = malloc(sizeof(bitmap_t));\r
170         map->tiles->data->width = (TILEWH*2);\r
171         map->tiles->data->height= TILEWH;\r
172         map->tiles->data->data = malloc((TILEWH*2)*TILEWH);\r
173         map->tiles->tileHeight = TILEWH;\r
174         map->tiles->tileWidth =TILEWH;\r
175         map->tiles->rows = 1;\r
176         map->tiles->cols = 2;\r
177 \r
178         i=0;\r
179         for(y=0; y<TILEWH; y++) {\r
180         for(x=0; x<(TILEWH*2); x++) {\r
181                 if(x<TILEWH)\r
182                   map->tiles->data->data[i] = 0x24;\r
183                 else\r
184                   map->tiles->data->data[i] = 0x34;\r
185                 i++;\r
186         }\r
187         }\r
188 \r
189         i=0;\r
190         for(y=0; y<map->height; y++) {\r
191                 for(x=0; x<map->width; x++) {\r
192                         map->data[i] = tile;\r
193                         tile = tile ? 0 : 1;\r
194                         i++;\r
195                 }\r
196                 tile = tile ? 0 : 1;\r
197         }\r
198 }\r
199 \r
200 \r
201 void\r
202 mapScrollRight(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->dx += offset;\r
207 \r
208         /* check to see if this changes the tile */\r
209         if(mv->page->dx >= mv->dxThresh ) {\r
210         /* go forward one tile */\r
211         mv->tx++;\r
212         /* Snap the origin forward */\r
213         mv->page->data += 4;\r
214         mv->page->dx = mv->map->tiles->tileWidth;\r
215 \r
216 \r
217         /* draw the next column */\r
218         x= SCREEN_WIDTH + mv->map->tiles->tileWidth;\r
219                 mapDrawCol(mv, mv->tx + 20 , mv->ty-1, x);\r
220         }\r
221 }\r
222 \r
223 \r
224 void\r
225 mapScrollLeft(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->dx -= offset;\r
230 \r
231         /* check to see if this changes the tile */\r
232         if(mv->page->dx == 0) {\r
233         /* go backward one tile */\r
234         mv->tx--;\r
235                 \r
236         /* Snap the origin backward */\r
237         mv->page->data -= 4;\r
238         mv->page->dx = mv->map->tiles->tileWidth;\r
239 \r
240         /* draw the next column */\r
241                 mapDrawCol(mv, mv->tx-1, mv->ty-1, 0);\r
242         }\r
243 }\r
244 \r
245 \r
246 void\r
247 mapScrollUp(map_view_t *mv, byte offset) {\r
248         word x, y;  /* coordinate for drawing */\r
249 \r
250         /* increment the pixel position and update the page */\r
251         mv->page->dy -= offset;\r
252 \r
253         /* check to see if this changes the tile */\r
254         if(mv->page->dy == 0 ) {\r
255         /* go down one tile */\r
256         mv->ty--;\r
257         /* Snap the origin downward */\r
258         mv->page->data -= mv->page->width*4;\r
259         mv->page->dy = mv->map->tiles->tileHeight;\r
260 \r
261 \r
262         /* draw the next row */\r
263         y= 0;\r
264                 mapDrawRow(mv, mv->tx-1 , mv->ty-1, y);\r
265         }\r
266 }\r
267 \r
268 \r
269 void\r
270 mapScrollDown(map_view_t *mv, byte offset) {\r
271         word x, y;  /* coordinate for drawing */\r
272 \r
273         /* increment the pixel position and update the page */\r
274         mv->page->dy += offset;\r
275 \r
276         /* check to see if this changes the tile */\r
277         if(mv->page->dy >= mv->dyThresh ) {\r
278         /* go down one tile */\r
279         mv->ty++;\r
280         /* Snap the origin downward */\r
281         mv->page->data += mv->page->width*4;\r
282         mv->page->dy = mv->map->tiles->tileHeight;\r
283 \r
284 \r
285         /* draw the next row */\r
286         y= SCREEN_HEIGHT + mv->map->tiles->tileHeight;\r
287                 mapDrawRow(mv, mv->tx-1 , mv->ty+15, y);\r
288         }\r
289 \r
290 }\r
291 \r
292 \r
293 void\r
294 mapGoTo(map_view_t *mv, int tx, int ty) {\r
295         int px, py;\r
296         unsigned int i;\r
297 \r
298         /* set up the coordinates */\r
299         mv->tx = tx;\r
300         mv->ty = ty;\r
301         mv->page->dx = mv->map->tiles->tileWidth;\r
302         mv->page->dy = mv->map->tiles->tileHeight;\r
303 \r
304         /* set up the thresholds */\r
305         mv->dxThresh = mv->map->tiles->tileWidth * 2;\r
306         mv->dyThresh = mv->map->tiles->tileHeight * 2;\r
307 \r
308         /* draw the tiles */\r
309         modexClearRegion(mv->page, 0, 0, mv->page->width, mv->page->height, 0);\r
310         py=0;\r
311         i=mv->ty * mv->map->width + mv->tx;\r
312         for(ty=mv->ty-1; py < SCREEN_HEIGHT+mv->dyThresh && ty < mv->map->height; ty++, py+=mv->map->tiles->tileHeight) {\r
313                 mapDrawRow(mv, tx-1, ty, py);\r
314         i+=mv->map->width - tx;\r
315         }\r
316 }\r
317 \r
318 \r
319 void\r
320 mapDrawTile(tiles_t *t, word i, page_t *page, word x, word y) {\r
321         word rx;\r
322         word ry;\r
323         rx = (i % t->cols) * t->tileWidth;\r
324         ry = (i / t->cols) * t->tileHeight;\r
325         modexDrawBmpRegion(page, x, y, rx, ry, t->tileWidth, t->tileHeight, t->data);\r
326 }\r
327 \r
328 \r
329 void \r
330 mapDrawRow(map_view_t *mv, int tx, int ty, word y) {\r
331         word x;\r
332         int i;\r
333 \r
334         /* the position within the map array */\r
335         i=ty * mv->map->width + tx;\r
336         for(x=0; x<SCREEN_WIDTH+mv->dxThresh && tx < mv->map->width; x+=mv->map->tiles->tileWidth, tx++) {\r
337         if(i>=0) {\r
338                 /* we are in the map, so copy! */\r
339                 mapDrawTile(mv->map->tiles, mv->map->data[i], mv->page, x, y);\r
340         }\r
341         i++; /* next! */\r
342         }\r
343 }\r
344 \r
345 \r
346 void \r
347 mapDrawCol(map_view_t *mv, int tx, int ty, word x) {\r
348         int y;\r
349         int i;\r
350 \r
351         /* location in the map array */\r
352         i=ty * mv->map->width + tx;\r
353 \r
354         /* We'll copy all of the columns in the screen, \r
355            i + 1 row above and one below */\r
356         for(y=0; y<SCREEN_HEIGHT+mv->dyThresh && ty < mv->map->height; y+=mv->map->tiles->tileHeight, ty++) {\r
357         if(i>=0) {\r
358                 /* we are in the map, so copy away! */\r
359                 mapDrawTile(mv->map->tiles, mv->map->data[i], mv->page, x, y);\r
360         }\r
361         i += mv->map->width;\r
362         }\r
363 }\r