#include "pixmaps.h"
#include "global.h"
#include "player.h"
#include "graph.h"

CPixmap::CPixmap() {handles = NULL;}

CPixmap::~CPixmap() {
  if (handles != NULL) {
    for (int i = 0; i <= numPlayers; i++)
      if (handles[i] != NULL)
	screen->FreePixmap(handles[i]);
    
    delete handles;
  }
}

CPixmap::CPixmap(char **data) {
  xpm = data;
  handles = new PixmapInfo *[numPlayers+1];
  for (int i = 0; i <= numPlayers; i++)
    handles[i] = NULL;
#ifdef PRECOMPILEPIXMAPS
  Compile(-1);
#endif
}

CPixmap &
CPixmap::operator=(char **data) {
  xpm = data;
  handles = new PixmapInfo *[numPlayers+1];
  for (int i = 0; i <= numPlayers; i++)
    handles[i] = NULL;
#ifdef PRECOMPILEPIXMAPS
  Compile(-1);
#endif
 return *this;
}

PixmapInfo*
CPixmap::Handle(int ownerId) {
  if (handles[ownerId+1] == NULL)
    Compile(ownerId);

  return handles[ownerId+1];
}

void CPixmap::Compile(int ownerId)
{
  if (ownerId == -1)
    handles[0] = screen->CompilePixmap(xpm);
  else
    {
      char *ptr1 = xpm[1], *ptr2 = xpm[2];
      players[ownerId]->color1Str[0] = ptr1[0];
      players[ownerId]->color2Str[0] = ptr2[0];
      xpm[1] = players[ownerId]->color1Str;
      xpm[2] = players[ownerId]->color2Str;
      handles[ownerId+1] = screen->CompilePixmap(xpm);
      xpm[1] = ptr1;
      xpm[2] = ptr2;
    }
  if (handles[ownerId+1] == NULL)
    Error("Compile Pixmap failed");
}

void CPixmap::GetInfo(int &x, int &y)
{
  for (int i = 0; i < numPlayers; i++)
    if (handles[i] != NULL) {
      screen->GetPixmapInfo(handles[i], x, y);
      return;
    }
  Compile(-1);
  screen->GetPixmapInfo(handles[0], x, y);
}

CPixmap city_pic;
CPixmap bulb_pic;
CPixmap elvis_pic;
CPixmap food_pic;
CPixmap goonda_pic;
CPixmap lux_pic;
CPixmap money_pic;
CPixmap shield_pic;
CPixmap trade_pic;
CPixmap corruption_pic;
CPixmap worker_pic;
CPixmap scientist_pic;
CPixmap taxman_pic;
CPixmap happy_pic;
CPixmap unhappy_pic;
CPixmap smokestack_pic;
CPixmap disorder_pic;

CPixmap ocean_pic;
CPixmap mountains_pic;
CPixmap hills_pic;
CPixmap desert_pic;
CPixmap swamp_pic;
CPixmap grass_pic;
CPixmap forest_pic;
CPixmap jungle_pic;
CPixmap plains_pic;

CPixmap gold_pic;
CPixmap coal_pic;
CPixmap oasis_pic;
CPixmap oil_pic;
CPixmap horses_pic;
CPixmap gems_pic;
CPixmap mining_pic;
CPixmap irrig_pic;
CPixmap pollution_pic;
CPixmap basket_pic;
CPixmap fish_pic;
CPixmap rivgood_pic;

CPixmap armor_pic;
CPixmap artillery_pic;
CPixmap battleship_pic;
CPixmap bomber_pic;
CPixmap cannon_pic;
CPixmap caravan_pic;
CPixmap carrier_pic;
CPixmap catapult_pic;
CPixmap cavalry_pic;
CPixmap chariot_pic;
CPixmap cruiser_pic;
CPixmap diplomat_pic;
CPixmap fighter_pic;
CPixmap frigate_pic;
CPixmap ironclad_pic;
CPixmap knights_pic;
CPixmap legion_pic;
CPixmap mechinf_pic;
CPixmap militia_pic;
CPixmap musketeers_pic;
CPixmap nuclear_pic;
CPixmap phalanx_pic;
CPixmap riflemen_pic;
CPixmap sail_pic;
CPixmap settler_pic;
CPixmap submarine_pic;
CPixmap transport_pic;
CPixmap trireme_pic;

CPixmap *roads_pic[3][3];
CPixmap *rails_pic[3][3];

CPixmap river0_pic;
CPixmap river1_pic;
CPixmap river2_pic;
CPixmap river3_pic;
CPixmap river01_pic;
CPixmap river02_pic;
CPixmap river03_pic;
CPixmap river12_pic;
CPixmap river13_pic;
CPixmap river23_pic;
CPixmap river012_pic;
CPixmap river013_pic;
CPixmap river023_pic;
CPixmap river123_pic;
CPixmap river0123_pic;

#include "../pic/city.xpm"
#include "../pic/bulb.xpm"
#include "../pic/elvis.xpm"
#include "../pic/food.xpm"
#include "../pic/goonda.xpm"
#include "../pic/lux.xpm"
#include "../pic/money.xpm"
#include "../pic/shield.xpm"
#include "../pic/trade.xpm"
#include "../pic/corruption.xpm"
#include "../pic/worker.xpm"
#include "../pic/scientist.xpm"
#include "../pic/taxman.xpm"
#include "../pic/happy.xpm"
#include "../pic/unhappy.xpm"
#include "../pic/smokestack.xpm"
#include "../pic/disorder.xpm"

#include "../pic/ocean.xpm"
#include "../pic/mountain.xpm"
#include "../pic/hills.xpm"
#include "../pic/desert.xpm"
#include "../pic/swamp.xpm"
#include "../pic/grass.xpm"
#include "../pic/forest.xpm"
#include "../pic/jungle.xpm"
#include "../pic/plains.xpm"

#include "../pic/gold.xpm"
#include "../pic/coal.xpm"
#include "../pic/oasis.xpm"
#include "../pic/oil.xpm"
#include "../pic/horses.xpm"
#include "../pic/gems.xpm"
#include "../pic/mining.xpm"
#include "../pic/irrig.xpm"
#include "../pic/road.xpm"
#include "../pic/rail.xpm"
#include "../pic/pollution.xpm"
#include "../pic/basket.xpm"
#include "../pic/fish.xpm"
#include "../pic/rivgood.xpm"

#include "../pic/armor.xpm"
#include "../pic/artillery.xpm"
#include "../pic/battleship.xpm"
#include "../pic/bomber.xpm"
#include "../pic/cannon.xpm"
#include "../pic/caravan.xpm"
#include "../pic/carrier.xpm"
#include "../pic/catapult.xpm"
#include "../pic/cavalry.xpm"
#include "../pic/chariot.xpm"
#include "../pic/cruiser.xpm"
#include "../pic/diplomat.xpm"
#include "../pic/fighter.xpm"
#include "../pic/frigate.xpm"
#include "../pic/ironclad.xpm"
#include "../pic/knights.xpm"
#include "../pic/legion.xpm"
#include "../pic/mechinf.xpm"
#include "../pic/militia.xpm"
#include "../pic/musketeers.xpm"
#include "../pic/nuclear.xpm"
#include "../pic/phalanx.xpm"
#include "../pic/riflemen.xpm"
#include "../pic/sail.xpm"
#include "../pic/settler.xpm"
#include "../pic/submarine.xpm"
#include "../pic/transport.xpm"
#include "../pic/trireme.xpm"

#include "../pic/river0.xpm"
#include "../pic/river1.xpm"
#include "../pic/river2.xpm"
#include "../pic/river3.xpm"
#include "../pic/river01.xpm"
#include "../pic/river02.xpm"
#include "../pic/river03.xpm"
#include "../pic/river12.xpm"
#include "../pic/river13.xpm"
#include "../pic/river23.xpm"
#include "../pic/river012.xpm"
#include "../pic/river013.xpm"
#include "../pic/river023.xpm"
#include "../pic/river123.xpm"
#include "../pic/river4.xpm"

void CompilePixmaps() {
  city_pic = city_xpm;
  bulb_pic = bulb_xpm;
  elvis_pic = elvis_xpm;
  food_pic = food_xpm;
  goonda_pic = goonda_xpm;
  lux_pic = lux_xpm;
  money_pic = money_xpm;
  shield_pic = shield_xpm;
  trade_pic = trade_xpm;
  corruption_pic = corruption_xpm;
  worker_pic = worker_xpm;
  scientist_pic = scientist_xpm;
  taxman_pic = taxman_xpm;
  happy_pic = happy_xpm;
  unhappy_pic = unhappy_xpm;
  smokestack_pic = smokestack_xpm;
  disorder_pic = disorder_xpm;

  ocean_pic = ocean_xpm;
  mountains_pic = mountains_xpm;
  hills_pic = hills_xpm;
  desert_pic = desert_xpm;
  swamp_pic = swamp_xpm;
  grass_pic = grass_xpm;
  forest_pic = forest_xpm;
  jungle_pic = jungle_xpm;
  plains_pic = plains_xpm;

  gold_pic = gold_xpm;
  coal_pic = coal_xpm;
  oasis_pic = oasis_xpm;
  oil_pic = oil_xpm;
  horses_pic = horses_xpm;
  gems_pic = gems_xpm;
  mining_pic = mining_xpm;
  irrig_pic = irrig_xpm;
  pollution_pic = pollution_xpm;
  basket_pic = basket_xpm;
  fish_pic = fish_xpm;
  rivgood_pic = rivgood_xpm;

  armor_pic = armor_xpm;
  artillery_pic = artillery_xpm;
  battleship_pic = battleship_xpm;
  bomber_pic = bomber_xpm;
  cannon_pic = cannon_xpm;
  caravan_pic = caravan_xpm;
  carrier_pic = carrier_xpm;
  catapult_pic = catapult_xpm;
  cavalry_pic = cavalry_xpm;
  chariot_pic = chariot_xpm;
  cruiser_pic = cruiser_xpm;
  diplomat_pic = diplomat_xpm;
  fighter_pic = fighter_xpm;
  frigate_pic = frigate_xpm;
  ironclad_pic = ironclad_xpm;
  knights_pic = knights_xpm;
  legion_pic = legion_xpm;
  mechinf_pic = mechinf_xpm;
  militia_pic = militia_xpm;
  musketeers_pic = musketeers_xpm;
  nuclear_pic = nuclear_xpm;
  phalanx_pic = phalanx_xpm;
  riflemen_pic = riflemen_xpm;
  sail_pic = sail_xpm;
  settler_pic = settler_xpm;
  submarine_pic = submarine_xpm;
  transport_pic = transport_xpm;
  trireme_pic = trireme_xpm;

  river0_pic = river0_xpm;
  river1_pic = river1_xpm;
  river2_pic = river2_xpm;
  river3_pic = river3_xpm;
  river01_pic = river01_xpm;
  river02_pic = river02_xpm;
  river03_pic = river03_xpm;
  river12_pic = river12_xpm;
  river13_pic = river13_xpm;
  river23_pic = river23_xpm;
  river012_pic = river012_xpm;
  river013_pic = river013_xpm;
  river023_pic = river023_xpm;
  river123_pic = river123_xpm;
  river0123_pic = river0123_xpm;

  for (int i = 0; i < 3; i++)
    for (int j = 0; j < 3; j++) {
      roads_pic[i][j] = new CPixmap(roads_xpm[i][j]);
      rails_pic[i][j] = new CPixmap(rails_xpm[i][j]);
    }
};
