home *** CD-ROM | disk | FTP | other *** search
/ High Voltage Shareware / high1.zip / high1 / DIR3 / FGSCROLL.ZIP / SCROLL.CPP < prev    next >
C/C++ Source or Header  |  1993-12-13  |  9KB  |  383 lines

  1. // (c) 1993 John M. Warren
  2. //  10/01/1993
  3. //  This code is for educational purposes only.
  4. //  This is my first C program. I just skipped HELLO WORLD
  5. //  and went for the guts and glory of C++ <g>
  6. //
  7. //  you must run Fastgraph Lite 3.03 graphics engine before
  8. //  running this program.
  9. //  written with Turbo C++ 3.0
  10. //
  11. //  Some fine games coming to you soon!!
  12. //  Code 3 The Programmers Connection 904-535-1705 14.4k
  13. //      (programming, game programming code!!)
  14. // --------------------------------------------------------------
  15. #include <string.h>
  16. #include "boolean.h"
  17. enum DIRECTION{north=0,south=1,west=2,east=3};    // north up
  18.  
  19. DIRECTION HerosDirection=north; // facing up
  20.  
  21. Boolean GODMODEACTIVE = true;
  22. Boolean DEAD = false;
  23.  
  24. unsigned char WORKPAGE= 1;
  25. unsigned char DISPLAYPAGE= 0;
  26. unsigned char SCRAPPAGE = 2;
  27. unsigned char TILEPAGE= 3;
  28.  
  29. const MAPSIZEX = 64;
  30. const MAPSIZEY = 64;
  31. const DRAWINGWIDTH =  13 * 16;    // width of drawing area for tiles
  32. const DRAWINGHEIGTH = 11 * 16;    // heigth of drawing area for tiles
  33.  
  34.  
  35.  
  36. // the three cordinate sytstems
  37. // mapptrx,mapptry  current x,y postion in pixels of the entire game mape
  38. // blockptrx,blockptry current x,y position in 16 pixel blocks of the entire game mape
  39. // HERO_X,HERO_Y x,y offset of the hero within the drawing area
  40.  
  41.  
  42.  
  43. const blockptrmaxx = MAPSIZEX - 13;
  44. const blockptrmaxy = MAPSIZEY - 12;
  45. short int MapPositionX,MapPositionY;     // current x,y position in pixels
  46.                    // relative to the game map
  47. char blockptrx=0,blockptry=0;  // current x,y position in 16 pixel blocks
  48.                 // i.e. postionX / 16  postionY /16
  49.                 // current block number
  50. unsigned char HERO_X, HERO_Y;
  51.  
  52.  
  53.  
  54. unsigned char map[MAPSIZEY][MAPSIZEX]; // holds background tile data
  55.  
  56. unsigned char spritemap[DRAWINGHEIGTH][DRAWINGWIDTH]; // holds sprite data use as global collision table
  57.  
  58. //             11*16  13*16
  59.  
  60.  
  61. struct tilestruct{
  62.   int x,y;
  63.   int spritewidth,spriteheigth;
  64. };
  65. tilestruct TILES[256];
  66.  
  67.  
  68.  
  69. #include <stdlib.h>
  70. #include <dos.h>
  71. #include <fastgraf.h>  // fastgraph library
  72. #include <alloc.h>
  73. #include "keyint.h"
  74. #include "scancode.h"
  75. #include "fileio.c"
  76. #include "sprites.h"
  77. //----------------------- keyboard stuff
  78.  
  79.  
  80.  
  81. struct keyboard {
  82.      unsigned shiftRight : 1;
  83.      unsigned shiftLeft  : 1;
  84.      unsigned ctrl       : 1;
  85.      unsigned alt        : 1;
  86.      unsigned scrollLock : 1;
  87.      unsigned numLock    : 1;
  88.      unsigned capsLock   : 1;
  89.      unsigned insert     : 1;
  90.      unsigned            : 8;
  91.  };
  92.  
  93. keyboard far *keys = (keyboard far *) MK_FP(0x0040, 0x0017);
  94.  
  95.  
  96. void playdemo();
  97. void flippage();
  98. void rebuildbackground();
  99.  
  100.  
  101.  
  102.  
  103. main(){
  104. randomize();
  105. fg_setmode(20);
  106.  
  107.  
  108.  
  109. fg_tcmask(1);
  110. keyint(ON);
  111. playdemo();
  112. freelist();
  113. keyint(OFF);
  114. fg_reset();
  115. fg_setmode(3);
  116. return 0;
  117. }
  118.  
  119. char offsetx=0,offsety=0;
  120.  
  121. void playdemo(){
  122. MapPositionX = 0; MapPositionY = 0; // fine tune the scroll off by 8 pixels used by drawbackground()
  123. blockptrx = 0; blockptry = 0; // fine tune the scroll off by 8 pixels used by drawbackground()
  124. HERO_X = 40; HERO_Y = 40; // initial position of the hero inside the DRAWING AREA!!
  125.  
  126.  
  127. // create the sprite list
  128. alist = (struct atomstruct *) malloc(sizeof(struct atomstruct));
  129. alist->asprite = geticon(HERO_X,HERO_Y,AHERO);
  130. alist->avalue = 0;
  131. alist->next = NULL;
  132. alist->prev = NULL;
  133. ptrlast = alist;
  134.  
  135.  
  136.  
  137.  
  138. int x,y;
  139. for(x=0;x<MAPSIZEX;x++){
  140.  for(y=0;y<MAPSIZEY;y++){
  141.   map[y][x] = 0;
  142.  }}
  143.  
  144.  //for(x=20;x<60;x++){ y = 1+random(50); map[y][x] = 3;}
  145.  TILES[AHERO+north].x =0;
  146.  TILES[AHERO+north].y =16;
  147.  TILES[AHERO+north].spritewidth  = 15;
  148.  TILES[AHERO+north].spriteheigth = 15;
  149.  
  150.  TILES[AHERO+south].x =16;
  151.  TILES[AHERO+south].y =16;
  152.  TILES[AHERO+south].spritewidth  = 15;
  153.  TILES[AHERO+south].spriteheigth = 15;
  154.  
  155.  TILES[AHERO+west].x =32;
  156.  TILES[AHERO+west].y =16;
  157.  TILES[AHERO+west].spritewidth  = 15;
  158.  TILES[AHERO+west].spriteheigth = 15;
  159.  
  160.  TILES[AHERO+east].x =48;
  161.  TILES[AHERO+east].y =16;
  162.  TILES[AHERO+east].spritewidth  = 15;
  163.  TILES[AHERO+east].spriteheigth = 15;
  164.  
  165.  
  166.  TILES[0].x = 156;
  167.  TILES[0].y = 87;
  168.  TILES[3].x = 0;
  169.  TILES[3].y = 0;
  170.  TILES[1].x = 32;
  171.  TILES[1].y = 0;
  172.  TILES[2].x = 16;
  173.  TILES[2].y = 0;
  174.  TILES[5].x = 48;
  175.  TILES[5].y = 0;
  176.  TILES[4].x = 64;
  177.  TILES[4].y = 0;
  178. /*
  179.  
  180.  for(x=0;x<MAPSIZEX;x++) {
  181.       map[1][x] = 2; // top
  182.       map[2][x] = 2;
  183.       map[62][x] = 2; // bottom
  184.       map[63][x] = 2;
  185.       map[x][0] = 2; // left
  186.       map[x][1] = 2;
  187.       map[x][62] = 2; // right
  188.       map[x][63] = 2;
  189.  }
  190.   TILES[0].x = 0;
  191.   TILES[0].y = 40;
  192.  */
  193.  
  194.  
  195. //  map[1][MAPSIZEX] = 1;
  196. //  map[63][0] = 2;
  197. //  map[63][63] = 1;
  198.  
  199. LoadFile("1.map", map[0], MAPSIZEX * MAPSIZEY);    // load map
  200.  
  201.  
  202.  
  203.  
  204.  
  205.  
  206. fg_setpage(SCRAPPAGE);
  207. fg_showpcx("DROP.PCX\0",0);
  208.  
  209. fg_setpage(TILEPAGE);
  210. fg_setvpage(DISPLAYPAGE);
  211. fg_showpcx("TILE.PCX\0",0);
  212.  
  213.  
  214.  
  215. //unsigned char manptrx=7,manptry=9;
  216.  
  217.  
  218. //rebuildbackground();
  219. //flippage();
  220.  
  221.  
  222. char string[25];
  223.  
  224. Boolean STOPPED = false;
  225. while(!STOPPED){
  226.  rebuildbackground();
  227.  
  228. fg_setcolor(4);
  229. fg_setpage(DISPLAYPAGE);
  230.  
  231.  
  232. /*
  233. fg_locate(0,0);
  234. fg_text("MapPositionX",12);
  235. itoa(MapPositionX, string, 10);
  236. fg_text(string,strlen(string));
  237.  
  238.  
  239. fg_locate(10,0);
  240. fg_text("MapPositionY",12);
  241. itoa(MapPositionY, string, 10);
  242. fg_text(string,strlen(string));
  243.  
  244.  
  245. fg_locate(20,0);
  246. fg_text("HERO_X",6);
  247. itoa(HERO_X, string, 10);
  248. fg_text(string,strlen(string));
  249.  
  250. fg_locate(30,0);
  251. fg_text("HERO_Y",6);
  252. itoa(HERO_Y, string, 10);
  253. fg_text(string,strlen(string));
  254. */
  255.  
  256.  
  257.  alist->asprite->MoveTo(HERO_X,HERO_Y);
  258.  alist->asprite->Move();
  259.  alist->asprite->Draw();
  260.  flippage();
  261. // fg_copypage(DISPLAYPAGE,WORKPAGE); // make backup copy
  262.  
  263.   switch(KEYPRESSED){
  264.    case ESC :
  265.        STOPPED = true;
  266.        break;
  267.  
  268.    case LEFT:
  269.       HerosDirection = west;
  270.       HERO_X=HERO_X-8;
  271.       if(HERO_X < 24){
  272.       HERO_X = 24;
  273.        offsetx = !offsetx;
  274.        if(!offsetx){
  275.         MapPositionX = MapPositionX - 8;
  276.         if(MapPositionX < 0) {
  277.           MapPositionX = 0; // 16 * 64 - 16 * 13
  278.           offsetx = !offsetx;
  279.           }
  280.         }
  281.        }
  282.  
  283.        break;
  284.    case RIGHT:
  285.       HerosDirection = east;
  286.       HERO_X=HERO_X+8;
  287.       if(HERO_X > 184){
  288.       HERO_X = 184;
  289.        offsetx = !offsetx;
  290.        if(offsetx){
  291.         MapPositionX = MapPositionX + 8;
  292.         if(MapPositionX > 408) {
  293.           MapPositionX = 408; // 16 * 64 - 16 * 13
  294.           offsetx = !offsetx;
  295.           }
  296.        }
  297.        }
  298.        break;
  299.  
  300.   case UP:
  301.       HerosDirection = north;
  302.       HERO_Y=HERO_Y-8;
  303.       if(HERO_Y < 24){
  304.       HERO_Y = 24;
  305.        offsety = !offsety;
  306.        if(!offsety){
  307.         MapPositionY = MapPositionY - 8;
  308.         if(MapPositionY < 0) {
  309.           MapPositionY = 0;
  310.           offsety = !offsety;
  311.           }
  312.        }
  313.        }
  314.        break;
  315.  
  316.    case DOWN:
  317.     HerosDirection = south;
  318.     HERO_Y=HERO_Y+8;
  319.     if(HERO_Y > 152){
  320.     HERO_Y = 152;
  321.        offsety = !offsety;
  322.        if(offsety){
  323.         MapPositionY = MapPositionY + 8;
  324.         if(MapPositionY > 424) {
  325.           MapPositionY = 424; // 16 * 64 - 16 * 13
  326.           offsety = !offsety;
  327.           }
  328.         }
  329.      }
  330.        break;
  331.      }// end case
  332. }// end while
  333. } // end playdemo()
  334.  
  335.  
  336.  
  337. void rebuildbackground(){
  338. unsigned char shifty=0,shiftx=0;
  339. unsigned char tempx,tempy,MAPVALUE;
  340. unsigned char tx,ty;
  341. //  fg_transfer(17,208,22,181,17,181,SCRAPPAGE,WORKPAGE); // copy background mask
  342.   char tempoffsetx = offsetx << 3; // same as multiplying by 8 but faster
  343.   char tempoffsety = offsety << 3;
  344.   blockptrx = MapPositionX / 8;
  345.   blockptry = MapPositionY / 8;
  346.  
  347.   for(unsigned char x=0;x<208;x=x+16){  // 10 16 pixel tiles
  348.    for(unsigned char y=0;y<176;y=y+16){
  349.     MAPVALUE = map[blockptry+shifty][blockptrx+shiftx];
  350.     tempx = TILES[MAPVALUE].x;
  351.     tempy = TILES[MAPVALUE].y;
  352. //   if(MAPVALUE != 0) // don't copy 0 value tiles, let the background mask bleed through .. quicker
  353.        fg_transfer(tempx,tempx+15,tempy,tempy+15,1+x+tempoffsetx,21+y+tempoffsety,TILEPAGE,WORKPAGE);
  354.     shifty++;
  355.    }
  356.     shifty=0;
  357.     shiftx++;
  358.    }
  359.  
  360.     // clip a copy of the static background around the working page
  361.     // building the scoreboard frame
  362.     fg_transfer(17,209,0,21,17,21,SCRAPPAGE,WORKPAGE); // top
  363.     fg_transfer(0,16,0,199,0,199,SCRAPPAGE,WORKPAGE); // left
  364.     fg_transfer(17,209,182,199,17,199,SCRAPPAGE,WORKPAGE); // bottom
  365.     fg_transfer(208,319,0,199,208,199,SCRAPPAGE,WORKPAGE); // right
  366.  
  367. }
  368.  
  369. // simulate page flipping by modifying the vga page register
  370. // ultra fast page flipping
  371. void flippage(){
  372. if(WORKPAGE)
  373.    {
  374.       fg_pan(0,200);
  375.       WORKPAGE = 0;
  376.    } else
  377.    {
  378.       fg_pan(0,0);
  379.       WORKPAGE=1;
  380.     }
  381. }
  382.  
  383.