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