unit CWMaps;

interface

uses
windows,Graphics,classes,sysUtils,strUtils,textImages,Types,light;

type
TCWMap=class(TObject)
public
 dirInf:TDirInf;
 procedure loadMap(fileName:string;var bitmap:TBitmap);
end;

implementation

procedure TCWMap.loadMap(fileName:string;var bitmap:TBitmap);
var
 subBitmap:TBitmap;
 x,y,i,j,count,sizex,sizey,countx,county,n,pos:Integer;
 chipRect:TRect;
 ImageFileName,s:string;
 loadable:boolean;
 SList,cList:TStringList;
const
chipx=100;
begin

  dirInf.currentFile:=fileName;

  SList:=TStringList.Create();
  cLIst:=TStringList.Create();
  subBitmap:=TBitmap.Create();
  sizex:=0;
  sizey:=0;
  count:=0;
  countx:=0;
  county:=0;
  loadable:=false;
  dirInf.mode:=1;
  pos:=1;


 try
  SList.LoadFromFile(fileName);

  (*設定読み込み*)
  if(trim(sList.Strings[0])='[jpmp:init]')then
   for i:=1 to SList.Count-1 do
   begin
   s:=trim(SList.Strings[i]);

   if (s[1]<>';')and(s<>'')then
   if(s='[jpmp:begin]')then
   begin
    ImageFileName:=TTagText.serachFileName(dirInf);

    loadable:=(dirinf.fileName<>ImageFileName)and
     FileExists(ImageFileName)and(countx*county*sizex*sizey<>0);
    break;
   end

   else if(AnsiStartsText('countx=',s))then
   begin
    s:=copy(s,length('countx=')+1,length(s));
    countX:=TTagText.getInteger(s,0);
   end

   else if(AnsiStartsText('county=',s))then
   begin
    s:=copy(s,length('countx=')+1,length(s));
    countY:=TTagText.getInteger(s,0);
   end

   else if(AnsiStartsText('filename=',s))then
   begin
    dirInf.fileName:=copy(s,length('filetname='),length(s)+1);
   end

   else if(AnsiStartsText('dirtype=',s))then
   begin
    s:=copy(s,length('dirtype=')+1,length(s));
    dirinf.mode:=TTagText.getInteger(s,1);
   end

   else if(AnsiStartsText('sizex=',s))then
   begin
    s:=copy(s,length('sizex=')+1,length(s));
    sizex:=TTagText.getInteger(s,1);
   end

   else if(AnsiStartsText('sizey=',s))then
   begin
    s:=copy(s,length('sizey=')+1,length(s));
    sizey:=TTagText.getInteger(s,1);
   end

   else if(AnsiStartsText('transparentline=',s))then
   begin
    s:=copy(s,length('dirtype=')+1,length(s));
    pos:=TTagText.getInteger(s,1);
   end;

   count:=i;
  end;

 if(loadable)then
 begin

 (*チップセットの画像をロードする*)
  subBitmap.LoadFromFile(ImageFileName);
  subBitmap.Transparent:=true;
  subBitmap.TransparentColor:=subBitmap.Canvas.Pixels[0,0];
  subBitmap.Canvas.Brush.Color:=clred;


 (*キャンバスを生成する*)
  bitmap.Height:=sizey*county+pos;
  bitmap.Width:=sizex*countx;
  bitmap.Canvas.Brush.Color:=subBitmap.Canvas.Pixels[0,0];

  fillBMP(bitmap,bitmap.Canvas.Brush.Color,false);
 // bitmap.Canvas.FillRect(bitmap.Canvas.ClipRect);

  y:=pos;

  for i:=count+2 to sList.Count-1 do
  begin
   if('[jpmp:end]'=trim(sList.Strings[i]))then break
   else if(''<>sList.Strings[i])then
   begin
    cList.CommaText:=sList.Strings[i];
    x:=0;
    for j:=0 to cList.Count-1 do
    begin
     n:=TTagText.getInteger(cList.Strings[j],0);

     if(n>0)then
     begin
      chipRect.Top:=(n div chipx)*sizey;
      chipRect.Bottom:= chipRect.Top+sizey;
      chipRect.Left:=(n mod chipx)*sizex;
      chipRect.Right := chipRect.Left+sizex;
      bitmap.Canvas.CopyRect
      (Rect(x,y,x+sizex,y+sizey),subBitmap.Canvas,chipRect);
     end;
     x:=x+sizex;
     if(x>=bitmap.Width)then x:=0;
    end;
    y:=y+sizey;
    if(y>=bitmap.Height)then y:=pos;
   end;
   
  end;

 end;
 // bitmap.Canvas.Draw(0,0,bitmap);
 finally
  sList.Free();
  cList.Free();
  subBitmap.Free();

 end;
 end;



end.
