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