[DM-dev] Dungeon Rendering :)
Brought to you by:
acdalton,
henningsen
From: Stephan B. <st...@wa...> - 2001-04-29 00:39:15
|
Okay, Now I've made some progress in the dungeon rendering. The whole thing is done in about 12 lines of code. So far the default rendering code is working for Rooms and Walls for all subclasses (no subclass-specific rendering has been needed so far!!!) This is literally all of the rendering code: void DungeonElement::renderTo( DMGrid &grid, int relativeX, int relativeY ) { //CERR << "rendering..."; IntCoordinate pos = getPosition(); int rx = getPosition().x()+relativeX; int ry = getPosition().y()+relativeY; int sq = getSquareType(); //CERR << " width="<<getWidth()<<", height="<<getHeight()<<", xoff="<<rx<<", yoff="<<ry<<endl; for( int x = 0; x < getWidth(); x++ ) { for( int y = 0; y < getHeight(); y++ ) { sq = getSquareType( x, y ); grid.set( rx+x,ry+y, sq ); } } for( iter = childs.begin(); iter != childs.end(); ++iter ) { //CERR << "asking child "<<(*iter)->getName()<<endl; (*iter)->renderTo( grid, this ); } } That code turns this: DungeonElement top( "dungeon" ); DungeonElement *de = 0; top.add( de = new Room( "roomX" ) ); de->move( 3, 5 ); top.add( de = new Room( 7,3, "wide room" ) ); de->move( 17,4 ); top.add( de = new Room( 5,8, "tall room" ) ); de->move( 10,2 ); top.add( de = new Wall( 1,1, 3 ) ); de->setSquareType( DM::sq_Open ); de->setGeometry( 10, 15, 7, 3 ); top.toCerr(); DMGrid grid( 40, 20 ); top.renderTo( grid ); into: 0111111111111111111111111111111111111111 1000000000000000000000000000000000000001 1000000000111110000000000000000000000001 1000000000100010000000000000000000000001 1000000000100010011111111111100000000001 1001111100100010010000000000100000000001 1001000100100010010000000000100000000001 1001000100100010010000000000100000000001 1001000100100010011111111111100000000001 1001111100111110000000000000000000000001 1000000000000000000000000000000000000001 1000000000000000000000000000000000000001 1000000000000000000000000000000000000001 1000000000000000000000000000000000000001 1000000000000000000000000000000000000001 1000000000333333300000000000000000000001 1000000000333333300000000000000000000001 1000000000333333300000000000000000000001 1000000000000000000000000000000000000001 1111111111111111111111111111111111111111 I don't know what's up with that 0 in the corner. It shouldn't be there. Anyway... Note that walls are only 1 unit wide by default, but they may be resized, to make filled-in areas, as I've done near the bottom. A user can dynamically change these, too. Note how I've set one wall to be a large area with SquareType "open" (int id 3). And you want to lay down tunnel through that? Just make a new DungeonElement and setSquareType( DM::sq_Floor ), then lay it down so that it goes over the desired area. They will be rendered in the order they are added, so the last-added will show up "over" the first-added. Still no doors, but they're coming. Here's a trick which I thought was particularly clever for reusing the same pointer variable: DungeonElement top( "dungeon" ); DungeonElement *de = top.add( new Room( "roomX" ) ); de->move( 3, 5 ); de = top.add( new Room( 12,5, "wide room" ) ); de->move( 17,4 ); de = de->add( new DungeonElement("blank strip" ) ); de->setGeometry( 5,0, 2, 5 ); // relative to "wide room"!!! de->setSquareType( DM::sq_Floor ); // we want to paint double-doors. add() returns the child it adds, so we can keep using 'de' over and over. :) This draws double-doors in the wide room, by the way: 1000000000100010000000000000000000000001 1000000000100010011111001111100000000001 1001111100100010010000000000100000000001 1001000100100010010000000000100000000001 1001000100100010010000000000100000000001 1001000100100010011111001111100000000001 1001111100111110000000000000000000000001 --------------------^here^ (if you're using a monospaced font, anyway ;) Pretty simple, I think. Now once I can load my serialized data, we can store and user-created load dungeons. It wouldn't be hard to implement a small scripting language, like: room( x, y, w, h ) wall( x, y, orientation, length ) Should probably just use that for the config files, I guess. It's just that parsing that stuff is such a pain in the butt in C. I can be done, however, and I wouldn't mind doing it, if it makes sense to use a "script" instead of a "config file". The objects would simply save themselves in "source code" format: room( 12, 47, 7, 7 ) I could build hierarchies that way, with something like: room( 12, 47, 7, 7 ) { .... list of children... } But it'd be a bitch to write the parser. Or we just add each line's output object to some top-level object, then render that object. Good night! ----- Stephan Beal - st...@wa... http://qub.sourceforge.net - http://stephan.rootonfire.org http://dungeonmaker.sourceforge.net "Now I'm not normally the kind of person who likes to trespass, but sometimes you just find yourself over the line." -- Bob Dylan |