Commit e1d5cdfb authored by Stuart John Watson's avatar Stuart John Watson

Found a solution (not a perfect one) to tiles!

Tiles are loaded into pixmaps
Pixmaps are then drawn instead of the colours
parent e079c3de
......@@ -169,7 +169,7 @@ void Vision::drawLevel(Display*& dpy,Window& w,GC& gc,int scale){
}
}
void Vision::drawTiles(Display *& dpy, Window &w, GC &gc,int scale, unsigned char **tiles){
void Vision::drawTiles(Display *& dpy, Window &w, GC &gc,int scale,Pixmap *tiles){
Colormap colormap;
colormap = DefaultColormap(dpy, 0);
const int VIEW_DIAM = (viewDistance * 2) + 1;
......@@ -181,10 +181,12 @@ void Vision::drawTiles(Display *& dpy, Window &w, GC &gc,int scale, unsigned cha
if(mapX < 0 || mapY < 0 || mapX >= dungeon.getWidth() || mapY >= dungeon.getHeight() ){
continue;
}
std::cerr << "(" << x << "," << y << ")" << " - " << "(" << mapX << "," << mapY << ")" << " - " << fov[ mapY * dungeon.getWidth() + mapX ] << std::endl;
std::cerr << "(" << mapX << "," << mapY << ")" << " - " << fov[ mapY * dungeon.getWidth() + mapX ] << std::endl;
unsigned char *tData = tiles[ fov[ mapY * dungeon.getWidth() + mapX ]];
XCreatePixmapFromBitmapData(dpy, w, (char*) tData, 32, 32, 0, 0, 32);
TILE_TYPES t = fov[ mapY * dungeon.getWidth() + mapX ];
XCopyArea(dpy,tiles[t],w,gc,0,0,32,32,sx*32,sy*32);
}
}
}
......@@ -2,6 +2,7 @@
#define VISION_H
#include "../entity.h"
#include <X11/Xlib.h>
class Vision: public Entity { // This class is supposed to give the hero, mob, anything else that needs vision it's field of view. Therefore everything that needs to see or react to the environment needs to inherit from it.
public:
......@@ -9,7 +10,7 @@ class Vision: public Entity { // This class is supposed to give the hero, mob, a
char canSeeTile(int, int, int, int);
void updateFOW();
void drawLevel(Display*&,Window&,GC&,int);
void drawTiles(Display*&,Window&,GC&,int,unsigned char **);
void drawTiles(Display*&,Window&,GC&,int,Pixmap*);
protected:
TILE_TYPES *fov;
int viewDistance;
......
......@@ -21,11 +21,6 @@ Game::~Game(){
void Game::loop(){
// Start game loop
// Read the tiles from file
std::string tilesFilename("SimpleTiles.png");
int notUsingTiles = readTiles(tilesFilename);
//Setup the display and window
Display *dpy = XOpenDisplay(NULL);
int blackColor = WhitePixel(dpy, DefaultScreen(dpy));
......@@ -38,13 +33,19 @@ void Game::loop(){
XStoreName(dpy,w,"Idle Crawl");
XMapWindow(dpy, w);
GC gc = XCreateGC(dpy, w, 0, NULL);
// Read the tiles from file
std::string tilesFilename("SimpleTiles.png");
int notUsingTiles = readTiles(tilesFilename,dpy,w,gc);
//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
......@@ -69,7 +70,7 @@ void Game::loop(){
//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*/);
BasicSolverHero levelHero = BasicSolverHero( *levels.back() ,std::get<0>(heroStart), std::get<1>(heroStart),5 /*vision*/);
//Hero levelHero = Hero(levels.back(),std::get<0>(heroStart), std::get<1>(heroStart),20 /*vision*/);
theHero = &levelHero;
......@@ -100,7 +101,7 @@ void Game::loop(){
while(theHero->getCurrentHealth() > 0){
usleep(10000);
usleep(100000);
// Take hero turn.
theHero->turn();
// Now all the other Entities take turns but none extra for now
......@@ -142,7 +143,8 @@ void Game::loop(){
}
int Game::readTiles(std::string tilesFilename){
int Game::readTiles(std::string tilesFilename,Display*& dpy,Drawable d,GC gc){
unsigned int error, tileFileWidth;
unsigned char* image;
......@@ -155,25 +157,32 @@ int Game::readTiles(std::string tilesFilename){
// Now we need to split up the image into it's individual tiles
nTiles = floor(tileFileWidth/tileHeight);
// Allocate the memory for the tiles
tiles = new unsigned char *[nTiles];
for(unsigned int n = 0; n < nTiles; n++){
tiles[n] = new unsigned char[4 * tileHeight * tileHeight];
}
tiles = new Pixmap[nTiles];
std::cerr << image << std::endl;
for(int n = 0; n < nTiles; n++){
//char *rawData = new char[tileHeight*tileHeight*4];
tiles[n] = XCreatePixmap(dpy,d,tileHeight,tileHeight,24);
for(unsigned int i = 0; i < tileHeight; i++){
for(unsigned int j = 0; j < tileHeight; j++){
// Find the element in the tile char array for the start of the current pixel
int tileOffset = 4 * j + 4 * (i * tileHeight); //
// Find the respective element in the image char array for the start of the current pixel
int imageOffset = ((4*j) + (n * tileHeight * 4)) + (4 * i * tileFileWidth);//
// Memcpy 4 unsigned chars from the imageOffset address to the tileOffset address
memcpy(&tiles[n][tileOffset],&image[imageOffset],4 * sizeof(unsigned char));
int imageOffset = ((4*j) + (n * tileHeight * 4)) + (4 * i * tileFileWidth);
int colour = (image[imageOffset]<<24) | (image[imageOffset+1]<<16) | (image[imageOffset+2]<<8);
std::cerr << n << "," << i << "," << j << "," << (colour) << std::endl;
XSetForeground(dpy, gc, colour>>8);
XFillRectangle (dpy, tiles[n], gc, j, i, 1, 1);
}
}
}
// Now the individual tiles are stored we can free the original image
free(image);
return(0);
}
......@@ -11,13 +11,14 @@
#include <vector>
#include <memory>
#include <X11/Xlib.h>
class Game {
public:
Game(int);
~Game();
int readTiles(std::string);
int readTiles(std::string,Display*&,Drawable,GC);
void loop();
protected:
int nTiles;
......@@ -26,7 +27,7 @@ protected:
std::vector<std::shared_ptr<Dungeon>> levels;
std::vector<std::shared_ptr<Entity>> entities;
Hero *theHero;
unsigned char **tiles;
Pixmap *tiles;
int dungeonLevel;
};
......
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