home *** CD-ROM | disk | FTP | other *** search
- //---------------------------------------------------------------------------
- //
- // File: DUNGEON2.CPP
- // Path: ...\REHACK\graphics
- // Version: 1.1 (first C++ version)
- // Author: Written by Chris Lampton, 5/26/91
- // modified 6/26/93 by Dave Boynton
- // All art by Mike Barrs
- // CIS Id:
- // Created On: 6/26/93
- // Modified On: 6/28/93
- // Description: Demonstrates tile scrolling through a dungeon
- // requires: duntil4b.grf & mbs08-b.grf in current directory.
- // Tabs: 4
- //
- //---------------------------------------------------------------------------
- // All contents copyright (c) 1991,1993 by the GAMERS Programming Workshop.
-
- // A brief note on data structures:
- // This program is oriented around two arrays that contain two
- // different forms of a 48 by 48 tile dungeon map. The first array,
- // dungeon[], contains a description of the dungeon in terms of
- // "quadtiles," each of which is constructed out of four of the
- // small tiles contained in the file DUNTIL4B.GRF, arranged in a
- // square. See the #defines below for a list of the 18 types of big
- // tile. During initialization, this array is translated into a
- // second array, dungeon2[], which contains a description of the
- // dungeon in terms of the actual small tiles used on the screen.
- // This system, which may or may not be used in the final version
- // of REHACK, facilitates dungeon design while allowing more versa-
- // tile use of the tile set.
-
- //6/28/93 dgb: this quadtile approach has not been modified in this C++
- // version, however, the underlying tiles are now Bitmaps.
- //
-
- #include <alloc.h>
- #include <conio.h>
- #include <dos.h>
- #include <stdio.h>
- #include <stdlib.h>
- #include "..\general\types.hpp"
- #include "..\graphics\display.hpp"
- #include "..\graphics\bitmap.hpp"
-
- #define TILE_NUM 39 // Number of small tiles
- #define TILE_HEIGHT 21 // Height of small tile in pixels
- #define TILE_WIDTH 21 // Width of small tile in pixels
- #define QUAD_TILE_NUM 18 // Number of quadtiles
- #define QUAD_TILE_WIDTH TILE_WIDTH*2 // Width of quadtile in pixels
- #define QUAD_TILE_HEIGHT TILE_HEIGHT*2 // Height of quadtile in pixels
- #define WIND_WIDTH 7 // Width of dungeon window in small tiles
- #define WIND_HEIGHT 7 // Height of dungeon window in small tiles
- #define DUNGEON_WIDTH 48 // Width of dungeon map in small tiles
- #define DUNGEON_HEIGHT 48 // Height of dungeon map in small tiles
- #define SCROLL_RATE 21 // Number of pixels scrolled per frame
-
- Point windowStart={10,8}; //I know, I know, this is just a demo ...<g>
-
- // The following #defines represent the 18 types of "quadtiles," each
- // constructed out of four small tiles, that are the basic building blocks
- // of the dungeon in this program.
-
- #define UL 0 // Upper left corner
- #define UR 1 // Upper right corner
- #define FL 2 // Floor
- #define LL 3 // Lower left corner
- #define LR 4 // Lower right corner
- #define HO 5 // Horizontal wall
- #define VE 6 // Vertical wall
- #define NT 7 // North 't' intersection
- #define ST 8 // South 't' intersection
- #define WT 9 // West 't' intersection
- #define ET 10 // East 't' intersection
- #define NE 11 // North wall end
- #define SE 12 // South wall end
- #define WE 13 // West wall end
- #define EE 14 // East wall end
- #define CC 15 // Central cross
- #define HD 16 // Horizontal door
- #define VD 17 // Vertical door
-
- // Function prototypes:
-
- void translate();
- int load_screen(Canvas &);
- void draw_window(Canvas &,int,int,int,int);
- void draw_tile(Canvas &,int,int,int,int,int,int,int);
-
- Bitmap *tile[TILE_NUM]; // Array of tile bitmaps
- byte colregs[3*256]; // Array of color register values
- char quadtile[QUAD_TILE_NUM][4]={ // Array of quadtile definitions for translate()
- {0,10,3,4},
- {1,2,6,7},
- {5,5,5,5},
- {8,9,13,14},
- {11,12,15,16},
- {10,10,15,15},
- {3,12,3,12},
- {26,10,6,4},
- {17,9,15,15},
- {19,9,3,4},
- {24,12,6,12},
- {18,2,3,7},
- {25,12,13,27},
- {20,10,13,14},
- {22,2,15,23},
- {21,9,6,4},
- {10,10,29,30},
- {3,32,3,33}
- };
-
- // Dungeon map defined in terms of quadtiles (see above):
-
- char dungeon[DUNGEON_HEIGHT/2][DUNGEON_WIDTH/2]={
- {UL,HO,HO,HO,HO,HO,HO,HO,HO,HO,HO,HO,HO,HO,HO,HO,HO,HO,HO,NT,HO,HO,HO,UR},
- {VD,FL,FL,FL,FL,FL,FL,FL,FL,FL,FL,FL,FL,FL,FL,FL,FL,FL,FL,V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,FL,FL,WT,HO,LR,FL,WE,HO,NT,HO,UR,FL,VE},
- {VE,FL,VE,FL,FL,LL,HO,UR,FL,FL,FL,FL,FL,VD,FL,FL,FL,FL,FL,VE,FL,VE,FL,VE},
- {VE,FL,VE,FL,FL,FL,FL,VE,FL,FL,FL,FL,FL,VE,FL,FL,FL,FL,FL,VE,FL,VE,FL,VE},
- {VE,FL,LL,HO,UR,FL,FL,LL,HO,UR,FL,FL,FL,WT,HO,HO,HO,UR,FL,VE,FL,VE,FL,VE},
- {VE,FL,FL,FL,VE,FL,FL,FL,FL,VD,FL,FL,FL,VE,FL,FL,FL,VE,FL,VE,FL,VE,FL,VE},
- {VE,FL,FL,FL,VE,FL,FL,FL,FL,VE,FL,FL,FL,VE,FL,NE,FL,VE,FL,VE,FL,VE,FL,VE},
- {VE,FL,FL,FL,LL,HO,HO,HO,HO,LR,FL,FL,FL,VE,FL,LL,HO,ST,HO,LR,FL,SE,FL,VE},
- {VE,FL,FL,FL,FL,FL,FL,FL,FL,FL,FL,FL,FL,VE,FL,FL,FL,FL,FL,FL,FL,FL,FL,VE},
- {LL,HO,HO,HO,HO,HO,HO,HO,HO,HO,HO,HO,HO,ST,HO,HO,HO,HO,HO,HO,HO,HO,HO,LR},
- };
-
- char dungeon2[DUNGEON_HEIGHT][DUNGEON_WIDTH]; // Array for small tile map
-
- #pragma argsused
- int main(int argc, char *argv[])
- {
- int i,row=0,column=0,xoff,yoff;
- char ch;
- FILE *fp;
- //
- DisplayManager display(0); //try display(1) to disable waiting for the
- //retraces, no significant speedup on my
- //machine, but no screen-trashing either!
- Canvas theCanvas(display); // returns a full-screen canvas
-
- //set the window Rect
- theCanvas.clipRect.topLeft=windowStart;
- theCanvas.clipRect.bottomRight.x=WIND_WIDTH*TILE_WIDTH+windowStart.x;
- theCanvas.clipRect.bottomRight.y=WIND_HEIGHT*TILE_HEIGHT+windowStart.y;
- //now paints won't affect the whole screen, err.. I hope
- //
-
- // Open file containing bitmaps for tiles
-
- if ( (fp=fopen("duntil4b.grf","rb")) == NULL )
- {
- printf("Cannot open file \"duntil4b.grf\"\n");
- exit(1);
- }
- display.setMode(0x13); // Set graphics mode to 13h
-
- // Load palette from file into colregs[]
- for (i=0; i<(3*256); i++)
- colregs[i]=getc(fp);
- display.setPalette(colregs); // Set palette to palette in tile file
-
- // Load tiles into tile[]
- for (i=0; i<TILE_NUM; i++)
- {
- tile[i]=new Bitmap(TILE_WIDTH, TILE_HEIGHT);
- tile[i]->load(fp); //assumes the file has the bitmaps sequentially,
- //not like PCX, which will be different.
- }
- fclose(fp);
-
- translate(); // Translate quadtiles into small tiles
-
- if (!load_screen(theCanvas)) // Load background screen into graphics buffer
- {
- display.setMode(3);
- fprintf(stderr,"Couldn't load the background screen\n");
- exit(1);
- }
- draw_window(theCanvas,0,0,0,0); // Draw initial contents of dungeon window in graphics buffer
- display.putCanvas(theCanvas); // Move graphics buffer to the screen
-
- // Main program loop
- // Waits for cursor arrow to be pressed, then scrolls dungeon window
-
- do
- {
- ch=getch();
- if (ch==0)
- {
- ch=getch();
- switch (ch)
- {
- case 72: // Up arrow, so scroll up
- if (row>0)
- {
- --row;
- for (yoff=(TILE_HEIGHT-SCROLL_RATE);
- yoff>0; yoff-=SCROLL_RATE)
- draw_window(theCanvas,column,row,0,yoff);
- display.putCanvas(theCanvas);
- }
- break;
-
- case 80: // Down arrow, so scroll down
- if (row<(DUNGEON_HEIGHT-WIND_HEIGHT))
- {
- for (yoff=SCROLL_RATE; yoff<TILE_HEIGHT; yoff+=SCROLL_RATE)
- draw_window(theCanvas,column,row,0,yoff);
- display.putCanvas(theCanvas);
- row++;
- }
- break;
-
- case 75: // Left arrow, so scroll left
- if (column>0)
- {
- --column;
- for (xoff=TILE_WIDTH-SCROLL_RATE; xoff>0; xoff-=SCROLL_RATE)
- draw_window(theCanvas,column,row,xoff,0);
- display.putCanvas(theCanvas);
- }
- break;
-
- case 77: // Right arrow, so scroll right
- if (column<(DUNGEON_WIDTH-WIND_WIDTH))
- {
- for (xoff=SCROLL_RATE; xoff<TILE_WIDTH; xoff+=SCROLL_RATE)
- draw_window(theCanvas,column,row,xoff,0);
- display.putCanvas(theCanvas);
- column++;
- }
- break;
-
- } // end switch (ch)
- draw_window(theCanvas,column,row,0,0);
- display.putCanvas(theCanvas);
- } /* end if (ch==0) */
- } while (ch!=27); // Terminate if ESC pressed
- display.setMode(3); // Restore DOS screen mode
- return 0;
- }
-
- // translate()
- // Translates dungeon array from quadtile format in dungeon[] to
- // small tile format in dungeon2[].
- void translate()
- {
- int x,y;
-
- // Translate quadtiles to small tiles
-
- for (x=0;x<DUNGEON_WIDTH/2;x++)
- for (y=0;y<DUNGEON_HEIGHT/2;y++) {
- dungeon2[y*2][x*2]=quadtile[dungeon[y][x]][0];
- dungeon2[y*2][x*2+1]=quadtile[dungeon[y][x]][1];
- dungeon2[y*2+1][x*2]=quadtile[dungeon[y][x]][2];
- dungeon2[y*2+1][x*2+1]=quadtile[dungeon[y][x]][3];
- }
-
- // Add corners of doors to tiles next to door tiles
-
- for (x=0;x<DUNGEON_WIDTH/2;x++)
- for (y=0;y<DUNGEON_HEIGHT/2;y++) {
- if (dungeon[y][x]==HD) dungeon2[y*2+1][x*2-1]=28;
- if (dungeon[y][x]==VD) dungeon2[y*2+2][x*2+1]=34;
- }
- }
-
- // load_screen()
- // Loads background screen from disk into screen_buffer[]
- int load_screen(Canvas &aCanvas)
- {
- unsigned int i;
- FILE *fp;
-
- if ((fp=fopen("mbs08-b.grf","rb"))==NULL) {
- return 0;
- }
- for (i=0; i<(3*256); i++) getc(fp);
- for (i=0; i<64000U; i++)
- aCanvas.pixels[i]=(unsigned char)getc(fp);
- fclose(fp);
- return 1;
- }
-
- // draw_window(int xtile,int ytile,int xoff,int yoff)
-
- // Draws dungeon window with dimensions (in small tiles) of WIND_WIDTH,
- // WIND_HEIGHT at screen coordinates xpos, ypos. The first row and
- // column of tiles is offset by xoff, yoff pixels.
-
- void draw_window(Canvas &aCanvas,int xtile,int ytile,int xoff,int yoff)
- {
- int x,y,xpos,ypos,xoff1,yoff1,xext,yext;
-
- for (x=0; x<(WIND_WIDTH+1);x++)
- for (y=0; y<(WIND_HEIGHT+1);y++) {
- xpos= (x==0) ? windowStart.x : windowStart.x+x*TILE_WIDTH-xoff;
- ypos= (y==0) ? windowStart.y : windowStart.y+y*TILE_HEIGHT-yoff;
- if (x==0) {
- xoff1=xoff;
- xext=TILE_WIDTH-xoff;
- }
- else {
- xoff1=0;
- xext=TILE_WIDTH;
- };
- if (y==0) {
- yoff1=yoff;
- yext=TILE_HEIGHT-yoff;
- }
- else {
- yoff1=0;
- yext=TILE_HEIGHT;
- };
- if (x==WIND_WIDTH) xext=xoff;
- if (y==WIND_HEIGHT) yext=yoff;
- if (!( ((x==WIND_WIDTH)&&(xoff==0)) ||
- ((y==WIND_HEIGHT)&&(yoff==0)) ))
- {
- draw_tile(aCanvas,(int)dungeon2[ytile+y][xtile+x],
- xpos,ypos,xoff1,yoff1,xext,yext);
- }
- }
- }
-
- // draw_tile(int tnum,int xpos,int ypos,int xoff,int yoff,int xext,int yext)
- // Draw xext,yext pixels of tile tnum starting at pixel xoff,yoff at
- // screen coordinates xpos,ypos
- #pragma argsused
- void draw_tile(Canvas &aCanvas,int tnum,int xpos,int ypos,
- int xoff,int yoff,int xext,int yext)
- {
- Point at= { xpos, ypos };
- tile[tnum]->paint(aCanvas, at);
- }
-
-