Commit 711a54d1 authored by Robert Scott James's avatar Robert Scott James

DO NOT USE THIS RIPP_ YOU WILL GO MAD

This commit is truely horrible. While it compiles it should never be used. I need to figure out how to extract the indiviual tiles in a usable format for xlib using the lodepng library at the moment the code will generate pngs for the tiles but I'm not certain if the format of the char* for the read image or if my handling of them is the most disturbing bit. AVOID this stuart if you wish to retain any semblance of sanity

 Changes to be committed:
	modified:   src/game/game.cpp
	modified:   src/game/game.h
parent 0e601f87
......@@ -19,113 +19,172 @@ Game::~Game(){
}
void Game::loop(){
// Start game loop
//Setup the display and window
Display *dpy = XOpenDisplay(NULL);
int blackColor = WhitePixel(dpy, DefaultScreen(dpy));
Window w;
if (rootWindow){
w = DefaultRootWindow (dpy);
} else {
w = XCreateSimpleWindow( dpy, DefaultRootWindow(dpy), 0, 0, 400, 400, 0, blackColor, blackColor );
}
XStoreName(dpy,w,"Idle Crawl");
XMapWindow(dpy, w);
//Setup out double buffer
XWindowAttributes wa;
XGetWindowAttributes(dpy, w, &wa);
Pixmap double_buffer;
double_buffer = XCreatePixmap(dpy, w, wa.width, wa.height, wa.depth);
GC gc = XCreateGC(dpy, w, 0, NULL);
// 50 Levels for now
for(dungeonLevel = 0; dungeonLevel < 50; dungeonLevel++){
// Generate a dungeon for the first level
unsigned long milliseconds_since_epoch = std::chrono::system_clock::now().time_since_epoch() / std::chrono::milliseconds(1);
// Print Level Seed
std::cout << "Drawing seed for level " << dungeonLevel << ": " << milliseconds_since_epoch << std::endl;
std::shared_ptr<DungeonGenerator> d(new DungeonGenerator(200,200,milliseconds_since_epoch));
//std::shared_ptr<Loader> d(new Loader(202,202,"../testLevels/maze.lvl"));
levels.emplace_back(d);
// Populate the dungeon with rooms and corridors
d->populate();
// Create hero at coordinates of the dungeon start
std::tuple<int,int> heroStart = levels.back()->upStairPosition();
std::cerr << std::get<0>(heroStart) << ","<< std::get<0>(heroStart) << std::endl;
BasicSolverHero levelHero = BasicSolverHero( *levels.back() ,std::get<0>(heroStart), std::get<1>(heroStart),256 /*vision*/);
//Hero levelHero = Hero(levels.back(),std::get<0>(heroStart), std::get<1>(heroStart),20 /*vision*/);
theHero = &levelHero;
#if 0
// Generate nMobs
std::default_random_engine generator;
generator.seed(milliseconds_since_epoch);
for(int n = 0; n < 0; n++){
// Generate random coordinates for the starting point for each mob
std::uniform_int_distribution<int> x_dist(0, levels.back()->getWidth() );
std::uniform_int_distribution<int> y_dist(0, levels.back()->getHeight() );
int mobx = x_dist(generator);
int moby = y_dist(generator);
while(levels.back()->getTile(mobx,moby) != FLOOR ){
mobx = x_dist(generator);
moby = y_dist(generator);
}
std::shared_ptr<RandomMove> r(new RandomMove( *levels.back() , mobx, moby, 0 /*vision*/, milliseconds_since_epoch+n));
entities.emplace_back( r );
}
#endif
// Start game loop
// Read the tiles from file
std::string tilesFilename("SimpleTiles.png");
int notUsingTiles = readTiles(tilesFilename);
int scale = wa.width/levels.back()->getWidth();
if (wa.height/levels.back()->getHeight() < scale){
scale = wa.height/levels.back()->getHeight();
//Setup the display and window
Display *dpy = XOpenDisplay(NULL);
int blackColor = WhitePixel(dpy, DefaultScreen(dpy));
Window w;
if (rootWindow){
w = DefaultRootWindow (dpy);
} else {
w = XCreateSimpleWindow( dpy, DefaultRootWindow(dpy), 0, 0, 400, 400, 0, blackColor, blackColor );
}
XStoreName(dpy,w,"Idle Crawl");
XMapWindow(dpy, w);
//Setup out double buffer
XWindowAttributes wa;
XGetWindowAttributes(dpy, w, &wa);
Pixmap double_buffer;
double_buffer = XCreatePixmap(dpy, w, wa.width, wa.height, wa.depth);
GC gc = XCreateGC(dpy, w, 0, NULL);
// 50 Levels for now
for(dungeonLevel = 0; dungeonLevel < 50; dungeonLevel++){
// Generate a dungeon for the first level
unsigned long milliseconds_since_epoch = std::chrono::system_clock::now().time_since_epoch() / std::chrono::milliseconds(1);
// Print Level Seed
std::cout << "Drawing seed for level " << dungeonLevel << ": " << milliseconds_since_epoch << std::endl;
std::shared_ptr<DungeonGenerator> d(new DungeonGenerator(200,200,milliseconds_since_epoch));
//std::shared_ptr<Loader> d(new Loader(202,202,"../testLevels/maze.lvl"));
levels.emplace_back(d);
// Populate the dungeon with rooms and corridors
d->populate();
// Create hero at coordinates of the dungeon start
std::tuple<int,int> heroStart = levels.back()->upStairPosition();
std::cerr << std::get<0>(heroStart) << ","<< std::get<0>(heroStart) << std::endl;
BasicSolverHero levelHero = BasicSolverHero( *levels.back() ,std::get<0>(heroStart), std::get<1>(heroStart),256 /*vision*/);
//Hero levelHero = Hero(levels.back(),std::get<0>(heroStart), std::get<1>(heroStart),20 /*vision*/);
theHero = &levelHero;
#if 0
// Generate nMobs
std::default_random_engine generator;
generator.seed(milliseconds_since_epoch);
for(int n = 0; n < 0; n++){
// Generate random coordinates for the starting point for each mob
std::uniform_int_distribution<int> x_dist(0, levels.back()->getWidth() );
std::uniform_int_distribution<int> y_dist(0, levels.back()->getHeight() );
int mobx = x_dist(generator);
int moby = y_dist(generator);
while(theHero->getCurrentHealth() > 0){
usleep(10000);
// Take hero turn.
theHero->turn();
// Now all the other Entities take turns but none extra for now
for(int _nMob = 0; _nMob < entities.size(); _nMob++){
entities[_nMob]->turn();
while(levels.back()->getTile(mobx,moby) != FLOOR ){
mobx = x_dist(generator);
moby = y_dist(generator);
}
std::shared_ptr<RandomMove> r(new RandomMove( *levels.back() , mobx, moby, 0 /*vision*/, milliseconds_since_epoch+n));
entities.emplace_back( r );
}
#endif
// Draw the Dungeon
theHero->drawLevel(dpy,double_buffer,gc,scale);
int scale = wa.width/levels.back()->getWidth();
if (wa.height/levels.back()->getHeight() < scale){
scale = wa.height/levels.back()->getHeight();
}
while(theHero->getCurrentHealth() > 0){
usleep(10000);
// Take hero turn.
theHero->turn();
// Now all the other Entities take turns but none extra for now
for(int _nMob = 0; _nMob < entities.size(); _nMob++){
entities[_nMob]->turn();
}
// Draw the Dungeon
if(notUsingTiles){
theHero->drawLevel(dpy,double_buffer,gc,scale);
// Now draw the hero
theHero->draw(dpy,double_buffer,gc,scale);
// Now draw the hero
theHero->draw(dpy,double_buffer,gc,scale);
} else {
// Need to draw using the tiles in here
for(int _nMob = 0; _nMob < entities.size(); _nMob++){
entities[_nMob]->draw(dpy,double_buffer,gc,scale);
}
for(int _nMob = 0; _nMob < entities.size(); _nMob++){
entities[_nMob]->draw(dpy,double_buffer,gc,scale);
}
XCopyArea(dpy, double_buffer, w, gc, 0, 0, wa.width, wa.height, 0, 0);
XFlush(dpy);
if(theHero->levelComplete() == true){
break;
}
}
XCopyArea(dpy, double_buffer, w, gc, 0, 0, wa.width, wa.height, 0, 0);
XFlush(dpy);
if(theHero->levelComplete() == true){
break;
if(theHero->getCurrentHealth() == 0){
break;
}
// Clean up the Mobs because otherwise you'll end up with loads of old Mobs
entities.clear();
}
}
int Game::readTiles(std::string tilesFilename){
unsigned int error;
unsigned char* image;
error = lodepng_decode32_file(&image, &tileFileWidth, &tileHeight, tilesFilename.c_str());
if(error){
std::cerr << "Error " << error << ": " << lodepng_error_text(error) << std::endl;
return(1);
}
if(theHero->getCurrentHealth() == 0){
break;
/* this is the complete tiles image in an array */
/* Need to split this up into the individual tiles else it's becomes useless */
/* This splitting assumes that each tile in the image is in the order that the types are declared in the types header */
/* Going to be horrible and assume that each tile in the file has a coded counterpart */
/* Each tile is assumed to be horizontally placed next to the next one */
unsigned char *tile;
for(unsigned int img = 0; img < (tileFileWidth/tileHeight); img++){
// Going to try and create split versions of the tiles in the code and go from there
tile = new unsigned char[tileHeight * tileHeight ];
for(unsigned int y = 0; y < tileHeight; y++){ //each line in this new tile calculate it's value in the tiledImage and assign the values to the temporary array
unsigned int offset = y * tileFileWidth;
memcpy (&tile[tileHeight * y], &image[offset], sizeof(char) * tileHeight ); // Assuming tileHeight is the same as tileWidth which it should be because square tiles
/*Encode the image*/
char *tmpfilename;
tmpfilename = new char[20];
snprintf(tmpfilename,20,"tile%u.png",img);
error = 0;
error = lodepng_encode32_file(tmpfilename, tile, tileHeight, tileHeight);
/*if there's an error, display it*/
if(error){
std::cerr << "Error " << error << ": " << lodepng_error_text(error) << std::endl;
return(1);
}
}
delete [] tile;
}
// Clean up the Mobs because otherwise you'll end up with loads of old Mobs
entities.clear();
}
// Now the individual tiles are stored we can free the original image
free(image);
return(0);
}
......@@ -17,10 +17,11 @@ class Game {
public:
Game(int);
~Game();
int readTiles(std::string);
void loop();
protected:
int const rootWindow;
unsigned int tileFileWidth, tileHeight;
std::vector<std::shared_ptr<Dungeon>> levels;
std::vector<std::shared_ptr<Entity>> entities;
Hero *theHero;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment