OSDN Git Service

meh i should organize gvar and mv soon and use the memory manager and i cleaned the...
[proj16/16.git] / src / scroll.c
1 /* Project 16 Source Code~
2  * Copyright (C) 2012-2016 sparky4 & pngwen & andrius4669 & joncampbell123
3  *
4  * This file is part of Project 16.
5  *
6  * Project 16 is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 3 of the License, or
9  * (at your option) any later version.
10  *
11  * Project 16 is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program.  If not, see <http://www.gnu.org/licenses/>, or
18  * write to the Free Software Foundation, Inc., 51 Franklin Street,
19  * Fifth Floor, Boston, MA 02110-1301 USA.
20  *
21  */
22
23 #include "src/lib/scroll16.h"
24 #include "src/lib/16_timer.h"
25 #include "src/lib/wcpu/wcpu.h"
26
27 //#define FADE
28 #define MODEX   //this is for mode x initiating
29
30 //word far *clock= (word far*) 0x046C; /* 18.2hz clock */
31 //bitmap_t *p;
32 global_game_variables_t gvar;
33 static map_t map;
34 player_t player[MaxPlayers];
35 map_view_t mv[4];
36 //word pn=0; //i forgot ww
37 //static planar_buf_t huge *pp;
38 float t;
39 sword bakapee;
40 pan_t pan;
41 //debugswitches
42 boolean panswitch=0;
43 boolean pageflipflop=1;
44         unsigned int i;
45         const char *cpus;
46         //static int persist_aniframe = 0;    /* gonna be increased to 1 before being used, so 0 is ok for default */
47
48         //map_view_db_t pgid[4];
49         word pg;
50 //#ifdef FADE
51         static word paloffset=0;
52         byte *dpal, *default_pal;
53 //#endif
54         byte *gpal;
55         byte *ptr;
56         byte *mappalptr;
57
58 void main(int argc, char *argv[])
59 {
60         byte *mesg=malloc(sizeof(dword));
61
62         if(argv[1]) bakapee = atoi(argv[1]);
63         else bakapee = 1;
64
65         // DOSLIB: check our environment
66         probe_dos();
67
68         // DOSLIB: what CPU are we using?
69         // NTS: I can see from the makefile Sparky4 intends this to run on 8088 by the -0 switch in CFLAGS.
70         //      So this code by itself shouldn't care too much what CPU it's running on. Except that other
71         //      parts of this project (DOSLIB itself) rely on CPU detection to know what is appropriate for
72         //      the CPU to carry out tasks. --J.C.
73         cpu_probe();
74
75         // DOSLIB: check for VGA
76         if (!probe_vga()) {
77                 printf("VGA probe failed\n");
78                 return;
79         }
80         // hardware must be VGA or higher!
81         if (!(vga_state.vga_flags & VGA_IS_VGA)) {
82                 printf("This program requires VGA or higher graphics hardware\n");
83                 return;
84         }
85
86         if (_DEBUG_INIT() == 0) {
87 #ifdef DEBUGSERIAL
88                 printf("WARNING: Failed to initialize DEBUG output\n");
89 #endif
90         }
91         _DEBUG("Serial debug output started\n"); // NTS: All serial output must end messages with newline, or DOSBox-X will not emit text to log
92         _DEBUGF("Serial debug output printf test %u %u %u\n",1U,2U,3U);
93
94         pan.pn=1;
95         //player[0].data = &pp;
96
97         //printf("starting timer        ");
98         start_timer(&gvar);
99         //printf("ok\n");
100
101 //      atexit(qclean());
102
103         /* create the map */
104         fprintf(stderr, "testing map load~      ");
105         loadmap("data/test.map", &map);
106         chkmap(&map, 0);
107         printf("chkmap ok       ");
108         fprintf(stderr, "yay map loaded~~\n");
109
110         /* draw the tiles */
111 #ifdef MODEX
112         ptr = map.data;
113         //mappalptr = map.tiles->btdata->palette;
114
115         /* data */
116         player[0].data = malloc(48*128); //TODO use exmm
117         *player[0].data = bitmapLoadPcx("data/chikyuu.pcx"); // load sprite
118         //npctmp = bitmapLoadPcx("ptmp1.pcx"); // load sprite
119
120         /* create the planar buffer */
121 ////++++        (player[0].data) = *planar_buf_from_bitmap(&p);
122         /*++++printf("load pee!!        ");
123         pp = planar_buf_from_bitmap(&p);
124         printf("done!\n");*/
125
126 #endif
127         /*      input!  */
128         IN_Startup();
129         IN_Default(0,&player,ctrl_Joystick);
130         //IN_Default(1,&player,ctrl_Joystick);
131
132         /* save the palette */
133 #ifdef MODEX
134 #ifdef FADE
135         dpal = modexNewPal();
136         default_pal = modexNewPal();
137         *default_pal = *dpal;
138         modexPalSave(dpal);
139         modexFadeOff(4, dpal);
140 #endif
141
142         textInit();
143         VGAmodeX(bakapee, 1, &gvar);
144 //      printf("%dx%d\n", gvar.video.page[0].sw, gvar.video.page[0].sh);
145 #ifdef FADE
146         modexPalBlack();        //reset the palette~
147 #endif
148 #endif
149 //      printf("Total used @ before palette initiation:         %zu\n", oldfreemem-GetFreeSize());
150 //++++  player[0].data.offset=(paloffset/3);
151 //++++  modexPalUpdate1(&player[0].data, &paloffset, 0, 0);
152                 modexPalUpdate1(player[0].data->palette);
153 //++++0000              modexPalUpdate1(map.tiles->btdata->palette);
154         //printf("      %d\n", sizeof(ptmp->data));
155         //printf("1:    %d\n", paloffset);
156 //++++  map.tiles->data->offset=(paloffset/3);
157         //XTmodexPalUpdate(map.tiles->data, &paloffset, 0, 0);
158 //      printf("\n====\n");
159 //      printf("0       paloffset=      %d\n", paloffset/3);
160 //      printf("====\n\n");
161 #ifdef MODEX
162 #ifdef FADE
163         gpal = modexNewPal();
164         modexPalSave(gpal);
165         modexSavePalFile("data/g.pal", gpal);
166         modexPalBlack();        //so player will not see loadings~
167 #endif
168 #endif
169
170         /* setup camera and screen~ */
171         modexHiganbanaPageSetup(&gvar.video);
172         for(i=0;i<gvar.video.num_of_pages;i++)
173         {
174                 mv[i].page = &gvar.video.page[i];
175                 mv[i].map = &map;
176                 mv[i].video = &gvar.video;
177                 mv[i].pan       = &pan;
178         }
179
180         /* set up paging */
181         //TODO: LOAD map data and position the map in the middle of the screen if smaller then screen
182         mapGoTo(mv, 0, 0);
183         //_fmemcpy(mv[1].page->data, mv[0].page->data, mv[0].page->pagesize);
184
185         //TODO: put player in starting position of spot
186         //default player position on the viewable map
187         player[0].tx = mv[0].tx + mv[0].page->tilemidposscreenx;
188         player[0].ty = mv[0].ty + mv[0].page->tilemidposscreeny;
189         IN_initplayer(&player, 0);
190         //IN_initplayer(&player, 1);
191
192 #ifndef SPRITE
193         modexClearRegion(mv[0].page, player[0].x, player[0].y-TILEWH, 16, 32, 15);
194         //modexClearRegion(mv[1].page, player[0].x, player[0].y-TILEWH, 16, 32, 15);
195 #else
196         //PBUFSFUN(mv[1].page, player[0].x, player[0].y-TILEWH, 16, 64, 24, 32, PLAYERBMPDATA);
197         PBUFSFUN(mv[0].page, player[0].x, player[0].y-TILEWH, 16, 64, 16, 32,   player[0].data);
198 #endif
199
200         if(!pageflipflop)       modexShowPage(mv[1].page);
201         else                    modexShowPage(mv[0].page);//!(gvar.video.p)
202                 shinku_fps_indicator_page = 0; // we're on page 1 now, shinku(). follow along please or it will not be visible.
203
204         /* buffer pages */
205 //      modexClearRegion(mv[2].page, 0, 0, mv[2].page->width, mv[2].page->height, 47);
206 //      modexClearRegion(mv[3].page, 0, 0, mv[3].page->width, mv[3].page->height, 45);
207 //      {
208 //              unsigned int k,j,o;
209 //              /* fill screen with a distinctive pattern */
210 //              for (k=0;k < vga_state.vga_width;k++) {
211 //                      o = k >> 2;
212 //                      vga_write_sequencer(0x02/*map mask*/,1 << (k&3));
213 //                              for (j=0;j < vga_state.vga_height;j++,o += vga_state.vga_stride)
214 //                                      vga_state.vga_graphics_ram[o] = (k^j)&15; // VRL samples put all colors in first 15!
215 //              }
216 //      }
217         modexClearRegion(mv[3].page, 0, 128, 24, 36, 15);
218
219 #ifdef MODEX
220 #ifdef FADE
221         modexFadeOn(4, gpal);
222 #endif
223 #endif
224         while(!IN_KeyDown(sc_Escape) && player[0].hp>0)
225         {
226                 shinku(&gvar);
227         //top left corner & bottem right corner of map veiw be set as map edge trigger since maps are actually square
228         //to stop scrolling and have the player position data move to the edge of the screen with respect to the direction
229         //when player[0].tx or player[0].ty == 0 or player[0].tx == 20 or player[0].ty == 15 then stop because that is edge of map and you do not want to walk of the map
230
231         //player movement
232                 IN_ReadControl(0,&player);
233         if(!panswitch){
234                 walk(mv, player, 0);
235         }else{
236                 panpagemanual(mv, player, 0);
237                 //printf("      player[0].q: %d", player[0].q); printf("        player[0].d: %d\n", player[0].d);
238         }
239
240         //the scripting stuff....
241         //if(((player[0].triggerx == TRIGGX && player[0].triggery == TRIGGY) && IN_KeyDown(0x1C))||(player[0].tx == 5 && player[0].ty == 5))
242         if(((mv[0].map->data[(player[0].triggerx-1)+(map.width*(player[0].triggery-1))] == 0) && IN_KeyDown(0x1C))||(player[0].tx == 5 && player[0].ty == 5))
243         {
244                 short i;
245                 for(i=800; i>=400; i--)
246                 {
247                         sound(i);
248                 }
249                 nosound();
250         }
251         if(player[0].q == (TILEWH/(player[0].speed))+1 && player[0].info.dir != 2 && (player[0].triggerx == 5 && player[0].triggery == 5)){ player[0].hp--; }
252         //debugging binds!
253         if(IN_KeyDown(2)){ modexShowPage(mv[0].page); pan.pn=0; }
254         if(IN_KeyDown(3)){ modexShowPage(mv[1].page); pan.pn=1; }
255         if(IN_KeyDown(4)){ modexShowPage(mv[2].page); pan.pn=2; }
256         if(IN_KeyDown(4+1)){ modexShowPage(mv[3].page); pan.pn=3; }
257         if(IN_KeyDown(25)){ modexpdump(mv[0].page); modexpdump(mv[1].page);
258                  IN_UserInput(1,1);
259         }       //p
260 #ifdef MODEX
261 #ifdef FADE
262         if(IN_KeyDown(24)){ modexPalUpdate0(gpal); paloffset=0; modexpdump(mv[0].page); modexpdump(mv[1].page);  IN_UserInput(1,1); }
263         if(IN_KeyDown(22)){
264         paloffset=0; modexPalBlack(); modexPalUpdate(player[0].data, &paloffset, 0, 0);
265         printf("1paloffset      =       %d\n", paloffset/3);
266          modexPalUpdate(map.tiles->data, &paloffset, 0, 0);
267         printf("2paloffset      =       %d\n", paloffset/3);
268          modexpdump(mv[0].page); modexpdump(mv[1].page);
269                 IN_UserInput(1,1);
270         }
271 #endif
272 #endif
273         //pan switch
274         if(IN_KeyDown(88)){panswitch=!panswitch; IN_UserInput(1,1);}    //f12
275         if(IN_KeyDown(87))      //f11
276         {
277                 pageflipflop=!pageflipflop;
278                 IN_UserInput(1,1);
279 //              VGAmodeX(0, 0, &gvar);
280 //              IN_Shutdown();
281 //              __asm
282 //              {
283 //                      mov ah,31h
284 //                      int 21h
285 //              }
286         }
287         if(IN_KeyDown(68))      //f10
288         {
289                 gvar.kurokku.fpscap=!gvar.kurokku.fpscap;
290                 IN_UserInput(1,1);
291         }
292         if(IN_KeyDown(67))      //f9
293         {
294                 modexClearRegion(mv[1].page, 0, 0, mv[1].page->width, mv[1].page->height, 32);
295                 modexClearRegion(mv[2].page, 0, 0, mv[2].page->width, mv[2].page->height, 47);
296                 modexClearRegion(mv[3].page, 0, 0, mv[3].page->width, mv[3].page->height, 45);
297                 modexClearRegion(mv[3].page, 0, 0, 20, 36, 15);
298                 //IN_UserInput(1,1);
299         }
300         if(IN_KeyDown(66))      //f8
301         {
302 //              modexDrawSprite(mv[0].page, 16, 16, p);
303                 modexDrawSprite(mv[0].page, 32+48, 16, (player[0].data));
304         }
305         //TODO fmemtest into page
306         /*if(IN_KeyDown(4+1))   //4
307         {
308                 pg=1;
309                 SELECT_ALL_PLANES();
310                 _fmemset(((mv[pg].page->data+4)+(16*(mv[pg].page->width/4))), 15, 4);
311         }*/
312
313         //9
314         if(IN_KeyDown(10)){ modexPalOverscan(rand()%56); modexPalUpdate1(default_pal); IN_UserInput(1,1); }
315         //if(IN_KeyDown(11)){ modexPalOverscan(15); }
316         if((player[0].q==1) && !(player[0].x%TILEWH==0 && player[0].y%TILEWH==0)) break;        //incase things go out of sync!
317         }
318
319         /* fade back to text mode */
320         /* but 1st lets save the game palette~ */
321 #ifdef MODEX
322 #ifdef FADE
323         modexPalSave(gpal);
324         modexSavePalFile("data/g.pal", gpal);
325         modexFadeOff(4, gpal);
326 #endif
327         VGAmodeX(0, 1, &gvar);
328 #endif
329         IN_Shutdown();
330         printf("\nProject 16 scroll.exe. This is just a test file!\n");
331         printf("version %s\n", VERSION);
332         printf("tx: %d  ", mv[0].tx);
333         printf("ty: %d\n", mv[0].ty);
334         printf("\n");
335         printf("player vars:\n");
336         printf("        x: %d", player[0].x); printf("  y: %d\n", player[0].y);
337         //if(player[0].hp==0) printf("%d wwww\n", player[0].y+8);
338         //else printf("\nplayer[0].y: %d\n", player[0].y);
339         printf("        tx: %d", player[0].tx); printf("        ty: %d\n", player[0].ty);
340         printf("        triggx: %d", player[0].triggerx); printf("      triggy: %d\n", player[0].triggery);
341         printf("        hp: %d", (player[0].hp));       printf("        q: %d", player[0].q);   printf("        player.info.dir: %d", player[0].info.dir);      printf("        player.d: %d ", player[0].d);
342                 printf("        pdir=%d\n", player[0].pdir);
343         printf("        tile data value at player trigger position: %d\n\n", mv[0].map->data[(player[0].triggerx-1)+(map.width*(player[0].triggery-1))]);
344 //      printf("palette offset: %d\n", paloffset/3);
345 //++++  printf("Total used: %zu\n", oldfreemem-GetFreeSize());
346 //++++  printf("Total free: %zu\n", GetFreeSize());
347 //not used now  printf("temporary player sprite 0: http://www.pixiv.net/member_illust.php?mode=medium&illust_id=45556867\n");
348 //not used now  printf("temporary player sprite 1: http://www.pixiv.net/member_illust.php?mode=medium&illust_id=44606385\n");
349         printf("Virtual Screen: %dx", gvar.video.page[0].width);        printf("%d      ", gvar.video.page[0].height);
350         printf("Screen: %dx", gvar.video.page[0].sw);   printf("%d\n", gvar.video.page[0].sh);
351         printf("virtual tile resolution: %dx", gvar.video.page[0].tilesw);      printf("%d      ", gvar.video.page[0].tilesh);
352         printf("tile resolution: %dx", gvar.video.page[0].tw);  printf("%d\n", gvar.video.page[0].th);
353         printf("middle tile position: %dx", gvar.video.page[0].tilemidposscreenx);      printf("%d\n", gvar.video.page[0].tilemidposscreeny);
354         modexprintmeminfo(&gvar.video);
355         //printf("mv[%u].tx: %d", pan.pn, mv[pan.pn].tx); printf("      mv[%u].ty: %d   ", pan.pn, mv[pan.pn].ty);
356         printf("gvar.video.p=%u ", gvar.video.p); printf("gvar.video.r=%u ", gvar.video.r);
357         printf("pageflipflop=%u\n", pageflipflop);
358         //0000printf("\ngvar.video.tickclk=%f\n", gvar.video.tickclk);
359         //0000printf("gvar.video.clk=%f", gvar.video.clk);
360         printf("\n");
361         //printf("map.width=%d  map.height=%d   map.data[0]=%d\n", mv[0].map->width, mv[0].map->height, mv[0].map->data[0]);
362
363         printf("\n");
364         switch(detectcpu())
365         {
366                 case 0: cpus = "8086/8088 or 186/88"; break;
367                 case 1: cpus = "286"; break;
368                 case 2: cpus = "386 or newer"; break;
369                 default: cpus = "internal error"; break;
370         }
371         printf("detected CPU type: %s\n", cpus);
372 #ifdef MODEX
373 #ifdef FADE
374         modexFadeOn(4, dpal);
375 #endif
376 #endif
377 }