]> 4ch.mooo.com Git - 16.git/blob - 16/modex16/scroll.c
modified: 16/MODEX16.ZIP
[16.git] / 16 / modex16 / scroll.c
1 #include "modex16.h"\r
2 #include <stdio.h>\r
3 #include <stdlib.h>\r
4 \r
5 word far *clock= (word far*) 0x046C; /* 18.2hz clock */\r
6 \r
7 typedef struct {\r
8     bitmap_t *data;\r
9     word tileHeight;\r
10     word tileWidth;\r
11     word rows;\r
12     word cols;\r
13 } tiles_t;\r
14 \r
15 \r
16 typedef struct {\r
17     byte    *data;\r
18     tiles_t *tiles;\r
19     word width;\r
20     word height;\r
21 } map_t;\r
22 \r
23 \r
24 typedef struct {\r
25     map_t *map;\r
26     page_t *page;\r
27     word tx;\r
28     word ty;\r
29 } map_view_t;\r
30 \r
31 \r
32 \r
33 \r
34 map_t allocMap(int w, int h);\r
35 void initMap(map_t *map);\r
36 void mapScrollRight(map_view_t *mv, byte offset);\r
37 void mapScrollLeft(map_view_t *mv, byte offest);\r
38 void mapScrollUp(map_view_t *mv, byte offset);\r
39 void mapScrollDown(map_view_t *mv, byte offset);\r
40 void mapGoTo(map_view_t *mv, byte tx, byte ty);\r
41 void mapDrawTile(tiles_t *t, word i, page_t *page, word x, word y);\r
42 \r
43 void main() {\r
44     int show1=1;\r
45     int tx, ty;\r
46     int x, y;\r
47     page_t screen;\r
48     map_t map;\r
49     map_view_t mv;\r
50     byte *ptr;\r
51     \r
52     /* create the map */\r
53     map = allocMap(80,60);\r
54     initMap(&map);\r
55     mv.map = &map;\r
56 \r
57     /* draw the tiles */\r
58     ptr = map.data;\r
59     modexEnter();\r
60     screen = modexDefaultPage();\r
61     screen.width = 352;\r
62     mv.page = &screen;\r
63     mapGoTo(&mv, 0, 0);\r
64 \r
65     /* scroll all the way to the right */\r
66     for(x=0; x<(80*16-SCREEN_WIDTH); x++) {\r
67         mapScrollRight(&mv, 1);\r
68         modexShowPage(mv.page);\r
69     }\r
70     \r
71 \r
72     /* spin for a time */\r
73     for(x=0; x<500; x++) {\r
74         modexWaitBorder();\r
75     }\r
76 \r
77     modexLeave();\r
78 }\r
79 \r
80 \r
81 map_t\r
82 allocMap(int w, int h) {\r
83     map_t result;\r
84     \r
85     result.width =w;\r
86     result.height=h;\r
87     result.data = malloc(sizeof(byte) * w * h);\r
88 \r
89     return result;\r
90 }\r
91 \r
92 \r
93 void\r
94 initMap(map_t *map) {\r
95     /* just a place holder to fill out an alternating pattern */\r
96     int x, y;\r
97     int i;\r
98     int tile = 1;\r
99     map->tiles = malloc(sizeof(tiles_t));\r
100 \r
101     /* create the tile set */\r
102     map->tiles->data = malloc(sizeof(bitmap_t));\r
103     map->tiles->data->width = 32;\r
104     map->tiles->data->height= 16;\r
105     map->tiles->data->data = malloc(32*16);\r
106     map->tiles->tileHeight = 16;\r
107     map->tiles->tileWidth = 16;\r
108     map->tiles->rows = 1;\r
109     map->tiles->cols = 2;\r
110 \r
111     i=0;\r
112     for(y=0; y<16; y++) {\r
113         for(x=0; x<32; x++) {\r
114             if(x<16)\r
115               map->tiles->data->data[i] = 0x00;\r
116             else\r
117               map->tiles->data->data[i] = 0x47;\r
118             i++;\r
119         }\r
120     }\r
121 \r
122     i=0;\r
123     for(y=0; y<map->height; y++) {\r
124         for(x=0; x<map->width; x++) {\r
125             map->data[i] = tile;\r
126             tile = tile ? 0 : 1;\r
127             i++;\r
128         }\r
129         tile = tile ? 0 : 1;\r
130     }\r
131 }\r
132 \r
133 \r
134 void\r
135 mapScrollRight(map_view_t *mv, byte offset) {\r
136     word x, y;  /* coordinate for drawing */\r
137     unsigned int i;\r
138 \r
139     /* increment the pixel position and update the page */\r
140     mv->page->dx += offset;\r
141 \r
142     /* check to see if this changes the tile */\r
143     if(mv->page->dx >= 16) {\r
144         /* go forward one tile */\r
145         mv->tx++;\r
146         /* Snap the origin forward */\r
147         mv->page->data += 4;\r
148         mv->page->dx =0;\r
149 \r
150 \r
151         /* draw the next column */\r
152         x= SCREEN_WIDTH;\r
153         i= mv->ty * mv->map->width + mv->tx + 20;\r
154         for(y=0; y<240; y+=16) {\r
155             mapDrawTile(mv->map->tiles, mv->map->data[i], mv->page, (int)mv->page->dx + x, (int)mv->page->dy+y);\r
156             i += mv->map->width;\r
157         }\r
158     }\r
159 }\r
160 \r
161 \r
162 void\r
163 mapScrollLeft(map_view_t *mv, byte offest) {\r
164 }\r
165 \r
166 \r
167 void\r
168 mapScrollUp(map_view_t *mv, byte offset) {\r
169 }\r
170 \r
171 \r
172 void\r
173 mapScrollDown(map_view_t *mv, byte offset) {\r
174 }\r
175 \r
176 \r
177 void\r
178 mapGoTo(map_view_t *mv, byte tx, byte ty) {\r
179     int px, py;\r
180     unsigned int i;\r
181 \r
182     /* set up the coordinates */\r
183     mv->tx = tx;\r
184     mv->ty = ty;\r
185     mv->page->dx = 0;\r
186     mv->page->dy = 0;\r
187 \r
188     /* draw the tiles */\r
189     modexClearRegion(mv->page, 0, 0, mv->page->width, mv->page->height, 0);\r
190     py=0;\r
191     i=mv->ty * mv->map->width + mv->tx;\r
192     for(ty=mv->ty; py < SCREEN_HEIGHT && ty < mv->map->height; ty++, py+=mv->map->tiles->tileHeight) {\r
193         px=0;\r
194         for(tx=mv->tx; px < SCREEN_WIDTH+16 && tx < mv->map->width+1; tx++, px+=mv->map->tiles->tileWidth) {\r
195             mapDrawTile(mv->map->tiles, mv->map->data[i], mv->page, px, py);\r
196             i++;\r
197         }\r
198         i+=mv->map->width - tx;\r
199     }\r
200 }\r
201 \r
202 \r
203 void\r
204 mapDrawTile(tiles_t *t, word i, page_t *page, word x, word y) {\r
205     word rx;\r
206     word ry;\r
207     rx = (i % t->cols) * t->tileWidth;\r
208     ry = (i / t->cols) * t->tileHeight;\r
209     modexDrawBmpRegion(page, x, y, rx, ry, t->tileWidth, t->tileHeight, t->data);\r
210 }\r