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