View Full Version: EMAP class

C++ Learning Community > C++ Creations > EMAP class


Title: EMAP class
Description: A C++ play back library to use .emp file


Zaqufant - December 11, 2006 11:44 PM (GMT)
I use mappy maps in allegro to use in my games. I don't like the mappyal.c playback library (used to read the files i make in mappy) because I have to use C instead of C++ in order to use it. So i wrote my own file format (not really a format just a file filled with numbers that I use the ".emp" extenstion). I wrote my own lua script that is used in mappy to write one of my files here it is.
CODE
-- This is for making just reading somthing for lua

function main ()
local w = mappy.getValue(mappy.MAPWIDTH)
local h = mappy.getValue(mappy.MAPHEIGHT)
local file

if(w==0) then
 mappy.msgBox("It's cool","You need to load a map to show the width and height",mappy.MMB_OK,mappy.MMB_ICONINFO)
else
 local isok,asname = mappy.fileRequester (".", "Erics Map File (*.emp)", "*.emp", mappy.MMB_SAVE)
 
 if(isok==mappy.MMB_OK) then
 
  if (not (string.sub (string.lower (asname), -4) == ".emp")) then
   asname = asname..".emp"
  end
 end
 
 --stick somthing in the file
 file=io.open(asname,"wb")
 
 --at the beginging of the file put in the width and height
 file:write(mappy.getValue(mappy.MAPWIDTH)," ",mappy.getValue(mappy.MAPHEIGHT),"\n")
 --now write the number of blocks and animblocks
 file:write(mappy.getValue(mappy.NUMBLOCKSTR)," ",mappy.getValue(mappy.CURANIM)," ")
 --write the num of blocks gfx
 file:write(mappy.getValue(mappy.NUMBLOCKGFX),"\n")
 --write the map block width and height
 file:write(mappy.getValue(mappy.BLOCKWIDTH)," ",mappy.getValue(mappy.BLOCKHEIGHT),"\n")
 
 --loop through the whole map
 local x=0
 local y=0

 while y < h do
  while x < w do
   file:write(mappy.getBlock(x,y)," ")
   x=x+1
  end
  y=y+1
  x=0
  file:write("\n")
 end
 --write the gfx colors of the file
 local i=0

 x=0
 y=0
 local A,R,G,B
 while i<mappy.getValue(mappy.NUMBLOCKGFX) do
  while y<mappy.getValue(mappy.BLOCKHEIGHT) do
   while x<mappy.getValue(mappy.BLOCKWIDTH) do
    A,R,G,B=mappy.getPixel(x,y,i)
    file:write(R," ",G," ",B," ")
    x=x+1
   end
   x=0
   y=y+1
   file:write("\n")
  end
  x=0
  y=0
  i=i+1
 
 end
 --file:write("\n")
 i=0
 while i<mappy.getValue(mappy.NUMBLOCKSTR) do
  file:write(mappy.getBlockValue(i,mappy.BLKFG1)," ")
  file:write(mappy.getBlockValue(i,mappy.BLKUSER1)," ")
  file:write(mappy.getBlockValue(i,mappy.BLKUSER2)," ")
  file:write(mappy.getBlockValue(i,mappy.BLKUSER3)," ")
  file:write(mappy.getBlockValue(i,mappy.BLKUSER4)," ")
  file:write(mappy.getBlockValue(i,mappy.BLKUSER5)," ")
  file:write(mappy.getBlockValue(i,mappy.BLKUSER6)," ")
  file:write(mappy.getBlockValue(i,mappy.BLKUSER7)," ")
  file:write(mappy.getBlockValue(i,mappy.BLKFLAG1)," ")
  file:write(mappy.getBlockValue(i,mappy.BLKFLAG2)," ")
  file:write(mappy.getBlockValue(i,mappy.BLKFLAG3)," ")
  file:write(mappy.getBlockValue(i,mappy.BLKFLAG4)," ")
  file:write(mappy.getBlockValue(i,mappy.BLKFLAG5)," ")
  file:write(mappy.getBlockValue(i,mappy.BLKFLAG6)," ")
  file:write(mappy.getBlockValue(i,mappy.BLKFLAG7)," ")
  file:write(mappy.getBlockValue(i,mappy.BLKFLAG8)," ")
  i=i+1
  file:write("\n")
 end    
 
 file:close()
end
end

test, errormsg = pcall( main )
if not test then
   mappy.msgBox("Error ...", errormsg, mappy.MMB_OK, mappy.MMB_ICONEXCLAMATION)
end

This is used to make the file.

This is the play back class i made to read and implement the file. It is not quite perfect and is the development stage. Tell me how i could make it better :P

CODE
//this is the Emap file used to use the .emp
//created by eric brown

#include <allegro.h>
#include <fstream>
#include <vector>
using namespace std;

#ifndef EMAPCLASS
#define EMAPCLASS
//this is the bitmap
class Bitmap
{
   public:
       BITMAP* bmp;
       
       Bitmap(BITMAP* map)
       {
           bmp=create_bitmap(map->w,map->h);
           blit(map,bmp,0,0,0,0,map->w,map->h);
       }
       
       /*~Bitmap()
       {
           destroy_bitmap(bmp);
       }*/
};
//this is the block used in the EMAP class
class BLOCK
{
   public:
       int index;//the blocks number in the struct
       //the users
       int user1,user2,user3,user4,user5,user6,user7;
       //trigers that either = true or false
       int tl,tr,bl,br,trigger,unused1,unused2,unused3;
       //it's gfx in the bitmap thing
       int blockgfx;
};

//this is the block used for animations
class ANIMBLOCK
{
   public:
       vector<int> blocks;//this is the blocks in the animation
       vector<BLOCK>::iterator blkiter;//the blocks iter
       int x,y;//the block x and y
       int delay;//the delay
       int curblock;//the block the animation is curently at
       int totalblocks;//the total number of blocks in the animation
};
//this is the Emap file class
class EMAP
{
   public:
       //this is the cunstuctor
       EMAP(const char* mapname)
       {
           int i;
           BLOCK tblock;
           //make a file used to read from
           ifstream empfile;
           //open the file
           empfile.open(mapname);
           //see if it was opened
           if(empfile ==NULL)
           {
               allegro_message("The file was not found");
           }
           
           //read through the first bytes
           empfile >> mapblockw;
           empfile >> mapblockh;
           empfile >> numblocks;
           empfile >> numanimblocks;
           empfile >> numblockgfx;
           empfile >> blockw;
           empfile >> blockh;
           //set up the width in pixels
           mapw = mapblockw*blockw;
           maph=mapblockh*blockh;
           int x,y;
           //now loop through the map
           for(y=0;y<mapblockh;y++)
           {
               //push the map back
               map.push_back( vector<int>(mapblockw+1,0) );
               for(x=0;x<mapblockw;x++)
               {
                   //get the block number on the map
                   empfile >> i;
                   //give it the number on it
                   map[y][x]=i;
               }
           }
           
           int r=25,g=25,b=25;
           //Bitmap bmap(32,32);
           //now loop throught and give the gfx the pixels
           for(int l=0;l<numblockgfx;l++)
           {
               blockgfx[l] = create_bitmap(blockw,blockh);
               for(y=0;y<blockh;y++)
               {
                   for(x=0;x<blockw;x++)
                   {
                       empfile >> r;
                       empfile >> g;
                       empfile >> b;
                       //give the bitmap a pixel
                       putpixel(blockgfx[l],x,y,makecol(r,g,b));
                   }
               }
           }
           
           //now give the stuff to the blocks
           for(int l=0;l<numblocks;l++)
           {
               //write the stuff
               tblock.index = l;
               empfile >> tblock.blockgfx;
               empfile >> tblock.user1;
               empfile >> tblock.user2;
               empfile >> tblock.user3;
               empfile >> tblock.user4;
               empfile >> tblock.user5;
               empfile >> tblock.user6;
               empfile >> tblock.user7;
               empfile >> tblock.tl;
               empfile >> tblock.tr;
               empfile >> tblock.bl;
               empfile >> tblock.br;
               empfile >> tblock.trigger;
               empfile >> tblock.unused1;
               empfile >> tblock.unused2;
               empfile >> tblock.unused3;
               //give this object to the vector
               blocks.push_back(tblock);
           }
           empfile.close();
       }
       
       //this realoads the map
       void ReloadMap(const char* mapname)
       {
           //reset the stuff
           //get rid of the bitmaps
           DestroyBitmaps();
           //get rid of the map
           map.clear();
           //get rid of the blocks
           blocks.clear();
           //get rid of the animations
           animblks.clear();
           int i;
           BLOCK tblock;
           //make a file used to read from
           ifstream empfile;
           //open the file
           empfile.open(mapname);
           //see if it was opened
           if(empfile ==NULL)
           {
               allegro_message("The file was not found");
           }
           
           //read through the first bytes
           empfile >> mapblockw;
           empfile >> mapblockh;
           empfile >> numblocks;
           empfile >> numanimblocks;
           empfile >> numblockgfx;
           empfile >> blockw;
           empfile >> blockh;
           //set up the width in pixels
           mapw = mapblockw*blockw;
           maph=mapblockh*blockh;
           int x,y;
           //now loop through the map
           for(y=0;y<mapblockh;y++)
           {
               //push the map back
               map.push_back( vector<int>(mapblockw+1,0) );
               for(x=0;x<mapblockw;x++)
               {
                   //get the block number on the map
                   empfile >> i;
                   //give it the number on it
                   map[y][x]=i;
               }
           }
           
           int r=25,g=25,b=25;
           //Bitmap bmap(32,32);
           //now loop throught and give the gfx the pixels
           for(int l=0;l<numblockgfx;l++)
           {
               blockgfx[l] = create_bitmap(blockw,blockh);
               for(y=0;y<blockh;y++)
               {
                   for(x=0;x<blockw;x++)
                   {
                       empfile >> r;
                       empfile >> g;
                       empfile >> b;
                       //give the bitmap a pixel
                       putpixel(blockgfx[l],x,y,makecol(r,g,b));
                   }
               }
           }
           
           //now give the stuff to the blocks
           for(int l=0;l<numblocks;l++)
           {
               //write the stuff
               tblock.index = l;
               empfile >> tblock.blockgfx;
               empfile >> tblock.user1;
               empfile >> tblock.user2;
               empfile >> tblock.user3;
               empfile >> tblock.user4;
               empfile >> tblock.user5;
               empfile >> tblock.user6;
               empfile >> tblock.user7;
               empfile >> tblock.tl;
               empfile >> tblock.tr;
               empfile >> tblock.bl;
               empfile >> tblock.br;
               empfile >> tblock.trigger;
               empfile >> tblock.unused1;
               empfile >> tblock.unused2;
               empfile >> tblock.unused3;
               //give this object to the vector
               blocks.push_back(tblock);
           }
           empfile.close();
       }
       //the destructor
       /*~EMAP()
       {
           for(int i=0;i<numblockgfx;i++)
               destroy_bitmap(blockgfx[i]);
       }*/
       void DestroyBitmaps()
       {
           for(int i=0;i<numblockgfx;i++)
               destroy_bitmap(blockgfx[i]);
       }
       //this draws the map
       void DrawMap(BITMAP* dest,int screenx,int screeny,int x,int y,int w,int h)
       {
           //get how much is left over when divided
           int cutx=x%blockw,cuty=y%blockh;
           int blkx,blky,blkw,blkh;
           //get what the map would be with blocks
           blkx=x/blockw;
           blky=y/blockh;
           //get what the width would be with blokcs
           blkw=w/blockw;
           blkh=h/blockh;
           
           blkw+=2;
           blkh+=2;
           
           for(int x2=blkx;x2<blkx+blkw;x2++)
           {
               for(int y2=blky;y2<blky+blkh;y2++)
               {
                   //see if you should not draw
                   if(x2 > mapblockw || y2>mapblockh)
                       {return;}
                       
                   if(map[y2][x2] >=0)
                       blit(blockgfx[blocks[map[y2][x2]].blockgfx],dest,0,0,
                       (x2*blockw)-x+screenx,(y2*blockh)-y+screeny,blockw,blockh);
               }
           }  
       }
       void SetBlock(int x,int y,int index)
       {  
           map[y/blockh][x/blockw]=index;
       }
       //these returns values from the block thing
       void AddAnimBlock()
       {
       }
       
       void AddBlockToAnim(int index)
       {
       }
       int GetMapW()
       {
           return mapw;
       }
       int GetMapH()
       {
           return maph;
       }
       int GetBlockW(){return blockw;}
       int GetBlockH(){return blockh;}
       //get some things
       int GetTL(int x,int y) {return blocks[map[y/blockh][x/blockw]].tl;}
       int GetTR(int x,int y) {return blocks[map[y/blockh][x/blockw]].tr;}
       int GetBL(int x,int y) {return blocks[map[y/blockh][x/blockw]].bl;}
       int GetBR(int x,int y) {return blocks[map[y/blockh][x/blockw]].br;}
       
       //get one of the users
       int GetUser1(int x,int y){return blocks[map[y/blockh][x/blockw]].user1;}
       int GetUser2(int x,int y){return blocks[map[y/blockh][x/blockw]].user2;}
       int GetUser3(int x,int y){return blocks[map[y/blockh][x/blockw]].user3;}
       int GetUser4(int x,int y){return blocks[map[y/blockh][x/blockw]].user4;}
       int GetUser5(int x,int y){return blocks[map[y/blockh][x/blockw]].user5;}
       int GetUser6(int x,int y){return blocks[map[y/blockh][x/blockw]].user6;}
       int GetUser7(int x,int y){return blocks[map[y/blockh][x/blockw]].user7;}
       //get the trigger
       int GetTrigger(int x,int y){return blocks[map[y/blockh][x/blockw]].trigger;}
       //return a block
       BLOCK GetBlock(int x,int y);
       BLOCK GetBlockIndex(int index);
       BITMAP* GetGFX(int index);
       
   private:
       int mapw,maph;//width and height in pixels of map
       int mapblockw,mapblockh;//width and height of map in blocks
       int blockw,blockh;//the widht and height of the blocks
       int numblocks,numanimblocks;//this shows the amount of blocks and animblocks
       int numblockgfx;//this is how many blockgfxs there are.
       vector< vector<int> > map;//the map
       vector< vector<int> >::iterator mapiter;//the iterator for the map
       vector<BLOCK> blocks;//the block stucts
       vector<BLOCK>::iterator blockiter;//the blocks iter
       //vector<Bitmap> blockgfx;//the block graphics
       BITMAP* blockgfx[50];
       //vector<Bitmap>::iterator gfxiter;//the graphics iterator
       vector<ANIMBLOCK> animblks;//the animated blocks
       vector<ANIMBLOCK>::iterator animiter;//the animation iterator
};

#endif


Isn't this just awsome. Just give me any ideas on how i could make it better. Or just what you think. It is using allegro.

Shonoby - December 28, 2006 06:25 AM (GMT)
Wow, not bad from what i can see, i have no idea what it means...LOL, but it is impressive,Well it is big so it must be cool, jk it is cool, keep up the good work man..




Hosted for free by InvisionFree