OSDN Git Service

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