/* display.c
 *
 * Renders the displays.
 *
 * (c) 2006, Joseph Curtis
 */

#include <stdlib.h>
#include <stdio.h>
#include <string.h>

#include "world.h"
#include "player.h"
#include "display.h"
//#include "input.h"

#ifndef x86
#include <pspdebug.h>
#include <pspthreadman.h>
//#define printf pspDebugScreenPrintf
#endif

/* font spacings */
short int font_width[96] = {6,3,7,10,9,10,11,3,6,6,8,8,3,6,3,10,
			    7,3,7,6,8,7,7,7,7,7,3,3,6,6,6,6,
			    14,15,12,8,13,9,12,10,14,6,4,16,12,14,14,10,
			    13,10,13,9,12,10,13,15,11,11,10,5,9,5,5,9,
			    16,8,7,7,9,7,6,7,8,4,4,8,4,13,9,7,
			    8,8,8,7,5,9,9,13,8,8,9,4,1,4,7,16};


/* The default display */
struct display *default_display;


int update_view(struct display *d, int v, void *data) {
  //  struct world *w = (struct world *)data;
  //  int x_offset, y_offset;
#ifdef x86
  SDL_Rect offset;
#endif

  printf("update_view:\n");
  //  d->views[v].func(data);
  if(d->views[v].type == VIEW_MAIN) {
    printf("About to call update main!\n");
    update_main(d, data);
  } else if (d->views[v].type == VIEW_MENU) {
    update_menu(d, data);
  } else if (d->views[v].type == VIEW_MAP) {
    update_map(d, data);
  } else if (d->views[v].type == VIEW_PLAYERS) {
    update_players(d, data);
  }

  /* blits the current view to the screen */

  //  x_offset = w->vis_x - ((w->vis_x / CELL_WIDTH) * CELL_WIDTH);
  //  y_offset = w->vis_y - ((w->vis_y / CELL_HEIGHT) * CELL_HEIGHT);

  printf("blitting to screen: (current view %d)\n", d->current_view);
  blit_image_screen(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT,
		    d->views[d->current_view].image, 0, 0);

  flip();

  return 1;
}

void flip() {
#ifdef x86
  SDL_Flip(default_display->screen);
#else
  flipScreen();
#endif

}

int update(struct display *d, void *data) {
  printf("update:\n");
  return update_view(d, d->current_view, data);
}

int update_menu(struct display *d, void *data) {
#ifdef x86
  printf("update_menu:\n");
#endif
  return 1;
}

int update_map(struct display *d, void *data) {
  struct world *w = (struct world *)data;
  int i, j, c;

  for (j=0;j<w->height;j++) {
    for (i=0;i<w->width;i++) {
      switch(w->map[I(i, j, w->width)].image) {
      case 9:
	c = 0xFF00AA00;
	break;
      case 8:
	c = 0xFF0000BB;
	break;
      default:
	c = 0xFFFFFFFF;
      }
#ifndef x86
      fillImageRect(c, i*2, j*2, 2, 2, d->views[VIEW_MAP].image);
#endif
    }
  }
  

  return 1;
}

int update_players(struct display *d, void *data) {
  struct world *w = (struct world *)data;
  int blitx, blity;
  int x, y;
  int i;

  for (i=0;i<w->num_players;i++) {
    
    blitx = X(w->players[i].type, GRAPH_WIDTH) * CELL_WIDTH;
    blity = Y(w->players[i].type, GRAPH_WIDTH) * CELL_HEIGHT;
    
    if (i<4) {
      x = (i * SP_SPACING) + SP_FIRST_X;
      y = SP_FIRST_Y;
    } else {
      x = ((i-4) * SP_SPACING) + SP_SECOND_X;
      y = SP_SECOND_Y;
    }

    /* blank out */
    blit_image_image(X(NUM_TYPES, GRAPH_WIDTH) * CELL_WIDTH,
		     0, CELL_WIDTH, CELL_HEIGHT, w->graphics2,
		     x, y, d->views[VIEW_PLAYERS].image);
    blit_image_image(blitx, blity, CELL_WIDTH, CELL_HEIGHT, w->graphics2,
		     x, y, d->views[VIEW_PLAYERS].image);

    putText(SCREEN_WIDTH-FONT_WIDTH*9,
	    (SCREEN_HEIGHT/10 + (FONT_HEIGHT + FONT_HEIGHT/2)*i),
	    w->players[i].name, w, d, VIEW_PLAYERS);

  }
  
  return 1;
}

int not_better_render_terrain(struct display *d, struct world *w) {
  int i, j, blitx, blity;
  int vis_cellx, vis_celly;
  int viswidth, visheight;
  int mapwidth = w->width * CELL_WIDTH;
  int mapheight = w->height * CELL_HEIGHT;
  int x=0, y=0;

  vis_cellx = (w->vis_x / CELL_WIDTH);
  vis_celly = (w->vis_y / CELL_HEIGHT);

  viswidth = mapwidth < SCREEN_WIDTH ? w->width : VIS_WIDTH + vis_cellx;
  visheight = mapheight < SCREEN_HEIGHT ? w->height : VIS_HEIGHT + vis_celly;
  
  for(j=vis_celly;j<=visheight;j++) {
    for(i=vis_cellx;i<=viswidth;i++) {

      blitx = X(w->map[I(i, j, w->width)].image, GRAPH_WIDTH) * CELL_WIDTH;
      blity = Y(w->map[I(i, j, w->width)].image, GRAPH_WIDTH) * CELL_HEIGHT;

      blit_image_image(blitx, blity, CELL_WIDTH, CELL_HEIGHT, w->graphics,
		       x, y, d->views[VIEW_MAIN].image);
      
      x += CELL_WIDTH;
    }
    x = 0;
    y += CELL_HEIGHT;
  }

  return 1;
}

int render_terrain(struct display *d, struct world *w) {
  int vis_cellx, vis_celly, blitx, blity, blitw, blith;
  int i, j, x_offset=0, y_offset=0, x=0, y=0;
  int mapwidth = w->width * CELL_WIDTH;
  int mapheight = w->height * CELL_HEIGHT;
  int viswidth, visheight;

  vis_cellx = (w->vis_x / CELL_WIDTH);
  vis_celly = (w->vis_y / CELL_HEIGHT);

  x_offset = w->vis_x - (vis_cellx * CELL_WIDTH);
  y_offset = w->vis_y - (vis_celly * CELL_HEIGHT);

  viswidth = mapwidth < SCREEN_WIDTH ? w->width : VIS_WIDTH + vis_cellx;
  visheight = mapheight < SCREEN_HEIGHT ? w->height : VIS_HEIGHT + vis_celly;
  
  for(j=vis_celly;j<=visheight;j++) {
    for(i=vis_cellx;i<=viswidth;i++) {

      blitx = X(w->map[I(i, j, w->width)].image, GRAPH_WIDTH) * CELL_WIDTH;
      blity = Y(w->map[I(i, j, w->width)].image, GRAPH_WIDTH) * CELL_HEIGHT;
      
      if(j == vis_celly) {
	blity += y_offset;
	blith = CELL_HEIGHT - y_offset;
      } else {
	blith = CELL_HEIGHT;
      }
      
      if(i == vis_cellx) {
	blitx += x_offset;
	blitw = CELL_WIDTH - x_offset;
      } else {
	blitw = CELL_WIDTH;
      }

      if(j == VIS_HEIGHT+vis_celly) {
	if(mapheight > SCREEN_HEIGHT)
	  blith = SCREEN_HEIGHT - y;
      }

      if(i == VIS_WIDTH+vis_cellx)
	if(mapwidth > SCREEN_WIDTH)
	blitw = SCREEN_WIDTH - x;

      if(blith >= 0 && blitx >= 0)
	blit_image_image(blitx, blity, blitw, blith, w->graphics,
			 x, y, d->views[VIEW_MAIN].image);
      

      if(i == vis_cellx)
	x += (CELL_WIDTH - x_offset);
      else
	x += CELL_WIDTH;

    }
    x=0;

    if(j == vis_celly)
      y += (CELL_HEIGHT - y_offset);
    else 
      y += CELL_HEIGHT;
    
  }

  return 1;
}
int render_castles(struct display *d, struct world *w) {
  int vis_cellx, vis_celly, blitx, blity, blitw, blith;
  int i, x_offset=0, y_offset=0, x=0, y=0;
  int mapwidth = w->width * CELL_WIDTH;
  int mapheight = w->height * CELL_HEIGHT;
  int viswidth, visheight;

  vis_cellx = (w->vis_x / CELL_WIDTH);
  vis_celly = (w->vis_y / CELL_HEIGHT);

  x_offset = w->vis_x - (vis_cellx * CELL_WIDTH);
  y_offset = w->vis_y - (vis_celly * CELL_HEIGHT);


  for(i=0;i<w->num_towns;i++) {
    if(((w->towns[i].x+CELL_WIDTH*2) >= vis_cellx && w->towns[i].x <= (vis_cellx + VIS_WIDTH)) && ((w->towns[i].y+CELL_HEIGHT*2) >= vis_celly && w->towns[i].y <= (vis_celly + VIS_HEIGHT))) {
      
      //      fprintf(stderr, "town in view\n");

      blitw = CELL_WIDTH * 2;
      blith = CELL_HEIGHT * 2;

      blitx = (X(w->towns[i].image, GRAPH_WIDTH) * CELL_WIDTH);
      blity = (Y(w->towns[i].image, GRAPH_WIDTH) * CELL_HEIGHT);
      x = (w->towns[i].x * CELL_WIDTH) - w->vis_x;
      y = (w->towns[i].y * CELL_HEIGHT) - w->vis_y;
      //      printf("town blitx: %d, blity: %d, x: %d, y: %d\n", blitx, blity, x, y);

      if(x < 0) {
	blitx -= x;
	blitw = (CELL_WIDTH*2) + x;
	x = 0;
      }
      if (x >= (SCREEN_WIDTH - CELL_WIDTH * 2)) {
	blitw -= (x - (SCREEN_WIDTH - CELL_WIDTH * 2));
      }


      if(y < 0) {
	blity -= y;
	blith = (CELL_HEIGHT*2) + y;
	y = 0;
      }
      if (y >= (SCREEN_HEIGHT - CELL_HEIGHT * 2)) {
	blith -= (y - (SCREEN_HEIGHT - CELL_HEIGHT * 2));
      }


      //      if(x >= 0 && y >= 0 && x < (SCREEN_WIDTH - CELL_WIDTH * 2) && 
      //	 y < (SCREEN_HEIGHT - CELL_HEIGHT * 2))
      blit_image_image(blitx, blity, blitw, blith, 
		       w->graphics, x, y,
		       d->views[VIEW_MAIN].image);
    }
  }
  //  fprintf(stderr, "\n");
  return 1;
}

/*
int old_update_main(struct display *d, void *data) {

  vis_cellx = w->vis_x / CELL_WIDTH;
  vis_celly = w->vis_y / CELL_HEIGHT;

  vis_cellx1 = ((w->vis_x + (VIS_WIDTH * CELL_WIDTH)) / CELL_WIDTH);
  vis_celly1 = ((w->vis_y + (VIS_HEIGHT * CELL_HEIGHT)) / CELL_HEIGHT);

  x_offset = w->vis_x - (vis_cellx * CELL_WIDTH);
  y_offset = w->vis_y - (vis_celly * CELL_HEIGHT);

  for(j=vis_celly;j<vis_celly1;j++) {
    for(i=vis_cellx;i<vis_cellx1;i++) {
      blitx = X(w->map[I(i, j, w->width)].image, GRAPH_WIDTH) * CELL_WIDTH; // in cells
      blity = Y(w->map[I(i, j, w->width)].image, GRAPH_WIDTH) * CELL_HEIGHT; // in cells
      
      blit_image_image(blitx, blity, CELL_WIDTH, CELL_HEIGHT, w->graphics,
		       (x*CELL_WIDTH) - x_offset, (y*CELL_HEIGHT) - y_offset,
		       d->views[VIEW_MAIN].image);
      x++;
    }
    x=0;
    y++;
  }
  // end cell blitting
  printf("blitted cells\n");

  // check if a castle is in view and blit if necessary
  for(i=0;i<w->num_towns;i++) {
    if(((w->towns[i].x+CELL_WIDTH*2) >= vis_cellx && w->towns[i].x <= (vis_cellx + VIS_WIDTH)) && ((w->towns[i].y+CELL_HEIGHT*2) >= vis_celly && w->towns[i].y <= (vis_celly + VIS_HEIGHT))) {

      blitx = X(w->towns[i].image, GRAPH_WIDTH) * CELL_WIDTH;
      blity = Y(w->towns[i].image, GRAPH_WIDTH) * CELL_HEIGHT;
      x = (w->towns[i].x * CELL_WIDTH) - w->vis_x;
      y = (w->towns[i].y * CELL_HEIGHT) - w->vis_y;
      //      printf("town blitx: %d, blity: %d, x: %d, y: %d\n", blitx, blity, x, y);

      blit_image_image(blitx, blity, CELL_WIDTH * 2, CELL_HEIGHT * 2, 
		       w->graphics, x, y,
		       d->views[VIEW_MAIN].image);
    }
  }
  // end castling
  printf("blitted castles\n");

  //  return 1;

}
*/
int update_main(struct display *d, void *data) {
  struct world *w = (struct world *)data;
  int vis_cellx, vis_celly;
  int vis_cellx1, vis_celly1;
  int x_offset, y_offset;
  int i, j, k, x=0, y=0;
  int blitx, blity;
  char buf[128];
  static char box = 0;
  struct player p = w->players[w->turn%w->num_players];

  printf("update_main:\n");

  //  print_world(w);

  // blit my stuff to my image then return.
  // we have the world and the coords of the top-left of the visible window.
  // determine which cells are visible or partially visible

  render_terrain(d, w);

  render_castles(d, w);

  
  // check if an army is in view and blit if necessary
  for (i=0;i<w->num_armies;i++) {
    if( (w->armies[i].a[w->armies[i].on_top].x >= vis_cellx && w->armies[i].a[w->armies[i].on_top].x <= (vis_cellx + VIS_WIDTH)) && (w->armies[i].a[w->armies[i].on_top].y >= vis_celly && w->armies[i].a[w->armies[i].on_top].y <= (vis_celly + VIS_HEIGHT)) ) {
      // yay, what a long condition list!
      
      blitx = X(w->armies[i].a[w->armies[i].on_top].type.t, ARMY_WIDTH)
	* TROOP_WIDTH;
      blity = w->armies[i].a[0].team * TROOP_HEIGHT;
      
      x = ((w->armies[i].a[w->armies[i].on_top].x * CELL_WIDTH) - w->vis_x
	   + (CELL_WIDTH-TROOP_WIDTH) / 2) + (CELL_WIDTH-TROOP_WIDTH)/2;
      y = ((w->armies[i].a[w->armies[i].on_top].y * CELL_HEIGHT) - w->vis_y
	   + (CELL_HEIGHT-TROOP_HEIGHT) / 2) + (CELL_HEIGHT-TROOP_HEIGHT)/2;

      blit_image_image(blitx, blity, TROOP_WIDTH, TROOP_HEIGHT,
		       w->army_graphics, x, y, d->views[VIEW_MAIN].image);

      /* now blit the flagstaff */
      x -= (CELL_WIDTH-TROOP_WIDTH);
      blit_image_image(FLAGSTAFF_X, FLAGSTAFF_Y, FLAGSTAFF_WIDTH,
			    FLAGSTAFF_HEIGHT,
			    w->army_graphics, x, y, d->views[VIEW_MAIN].image);

      /* Blit the flag head */
      blitx = FLAG_X + ((w->armies[i].num_armies - 1)%4) * FLAG_WIDTH;
      blity = FLAG_Y + (w->armies[i].a[0].team) * FLAG_HEIGHT;

      y -= (CELL_HEIGHT-TROOP_HEIGHT)-2; // TODO: get rid of the magix
      blit_image_image(blitx, blity, FLAG_WIDTH, FLAG_HEIGHT,
			    w->army_graphics, x, y, d->views[VIEW_MAIN].image);

      /* if the number of troops is over 4 then blit the nub */
      if (w->armies[i].num_armies > 4) {
	blitx = FLAGNUB_X;
	blity = FLAGNUB_Y + (w->armies[i].a[0].team) * FLAGNUB_HEIGHT;
	//#ifndef x86
	blit_image_image(blitx, blity, FLAGNUB_WIDTH, FLAGNUB_HEIGHT,
			      w->army_graphics, x, y += TROOP_HEIGHT/2, d->views[VIEW_MAIN].image);
      }
      /* end flagstaff */
      

      if(w->selected_army == i)
	if(w->armies[i].num_armies > 1)
	  w->armies[i].selected = SELECT_GROUP;
	else
	  w->armies[i].selected = SELECT_ONE;
      else
	w->armies[i].selected = SELECT_NONE;

      /* blit selection box if needed */
      switch (w->armies[i].selected) {
      case SELECT_ONE:
	/* draw a small selection box */

	y = (((w->armies[i].a[w->armies[i].on_top].y * CELL_HEIGHT) - w->vis_y
	      + (CELL_HEIGHT-TROOP_HEIGHT) / 2)) + 4;

	blitx = SSELECT_X + (SSELECT_WIDTH * 4) 
	  + (w->armies[i].a[0].team * SSELECT_WIDTH);

	blity = SSELECT_Y;
	blit_image_image(blitx, blity, SSELECT_WIDTH, SSELECT_HEIGHT, w->army_graphics, x, y, d->views[VIEW_MAIN].image);

	blitx = SSELECT_X + (SSELECT_WIDTH * box);
	blity = SSELECT_Y;

	blit_image_image(blitx, blity, SSELECT_WIDTH, SSELECT_HEIGHT, w->army_graphics, x, y, d->views[VIEW_MAIN].image);

	box = box < 3 ? box+1 : 0;
	break;
      case SELECT_GROUP:
	/* draw a large selection box */
	
	y = (((w->armies[i].a[w->armies[i].on_top].y * CELL_HEIGHT) - w->vis_y
	      + (CELL_HEIGHT-TROOP_HEIGHT) / 2)) - 5;
	
	blitx = SELECT_X + (SELECT_WIDTH*4)
	  + (w->armies[i].a[0].team * SELECT_WIDTH);
	blity = SELECT_Y;
	blit_image_image(blitx, blity, SELECT_WIDTH, SELECT_HEIGHT, w->army_graphics, x, y, d->views[VIEW_MAIN].image);
	
	
	blitx = SELECT_X + (SELECT_WIDTH * box);
	blity = SELECT_Y;
	blit_image_image(blitx, blity, SELECT_WIDTH, SELECT_HEIGHT, w->army_graphics, x, y, d->views[VIEW_MAIN].image);
	box = box < 3 ? box+1 : 0;
	break;
      }    
      /* end selection box */      
    }
  }
  // end armying
  printf("blitted armies\n");
  

  
  /* check for the current command and print status as necessary */
  switch (w->command) {
  case CMD_NONE:
    sprintf(buf, "%s", p.name);
    putText(2, (SCREEN_HEIGHT-(FONT_HEIGHT * 2))-2,
	    buf, w, d, VIEW_MAIN);
    sprintf(buf, "Income: %dgp  Gold: %dgp  Upkeep: %dgp  Cities: %d  Turn: %d",    	    p.income, p.gold, p.upkeep, p.num_cities, w->turn);
    putText(2, (SCREEN_HEIGHT-FONT_HEIGHT)-2,
	    buf, w, d, VIEW_MAIN);
    //    sprintf(buf, "Upkeep: %d gp  Cities: %d  Turn: %d",  p.upkeep,
    //	    p.num_cities, w->turn);
    //    putText(16, (SCREEN_HEIGHT-STATUS_BAR_HEIGHT)+18,
    //    buf, w, d, VIEW_MAIN);
    break;
  case CMD_INFO:
    break;
  case CMD_MOVE_ARMY:
    break;
  case CMD_DIALOGUE:
    /* the status bar */
    //    blit_image_image(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT, d->views[3].image, 0, 0, d->views[VIEW_MAIN].image);

    x = (SCREEN_WIDTH/2)-(text_width(d->status_bar_text)/2);
    y = (SCREEN_HEIGHT-STATUS_BAR_HEIGHT)+(STATUS_BAR_HEIGHT/2)
      -(FONT_HEIGHT/2);
    putText(x, y, d->status_bar_text, w, d, VIEW_MAIN);
    break;
  case CMD_PROD:
    //    fprintf(stderr, "Num_production: %d\n", w->towns[w->selected_town].num_production);
    for(i=0;i<w->towns[w->selected_town].num_production;i++) {
      blitx = X(w->towns[w->selected_town].production[i].type, ARMY_WIDTH)
	* TROOP_WIDTH;
      blity = p.team * TROOP_HEIGHT;
      
      x = i < 2 ? FONT_WIDTH + 2: TROOP_WIDTH * 4.5;
      y = SCREEN_HEIGHT - (TROOP_HEIGHT * 1.1 * ((i%2)+1));

      blit_image_image(blitx, blity, TROOP_WIDTH, TROOP_HEIGHT,
		       w->army_graphics, x, y, d->views[VIEW_MAIN].image);

      y += (TROOP_HEIGHT - FONT_HEIGHT) / 2;
      switch(i) {
      case 0:
	putText(x-FONT_WIDTH/1.8, y, "X", w, d, VIEW_MAIN);
	break;
      case 1:
	putText(x-FONT_WIDTH/1.8, y, "O", w, d, VIEW_MAIN);
	break;
      case 2:
	putText(x-FONT_WIDTH/1.8, y, "A", w, d, VIEW_MAIN);
	break;
      case 3:
	putText(x-FONT_WIDTH/1.8, y, "[]", w, d, VIEW_MAIN);
	break;
      }
      sprintf(buf, "%dt/%dgp", w->towns[w->selected_town].production[i].time,
	      w->towns[w->selected_town].production[i].cost);
      putText(x+TROOP_WIDTH, y, buf, w, d, VIEW_MAIN);
    }
    
    if(w->towns[w->selected_town].current_production < 0) {
      sprintf(buf, "Current: None",w->towns[w->selected_town].current_production);
    } else {
      sprintf(buf, "Current:       %dt",
	      w->towns[w->selected_town].current_production_turns);
    }        
    x = SCREEN_WIDTH - 140; // TODO: magicks and fix the following line!
    y = SCREEN_HEIGHT - TROOP_HEIGHT * 1.1 + (TROOP_HEIGHT - FONT_HEIGHT) / 2;
    putText(x, y, buf, w, d, VIEW_MAIN);
    
    if(w->towns[w->selected_town].current_production >= 0) {
      /* blitting image of current production */
      x += text_width("Current: ");
      y -= (TROOP_HEIGHT-FONT_HEIGHT) / 2;

      i = w->towns[w->selected_town].current_production;
      blitx = X(w->towns[w->selected_town].production[i].type, ARMY_WIDTH)
	* TROOP_WIDTH;
      blity = p.team * TROOP_HEIGHT;
      blit_image_image(blitx, blity, TROOP_WIDTH, TROOP_HEIGHT,
		       w->army_graphics, x, y, d->views[VIEW_MAIN].image);
    }
    break;
    /* end of CMD_PROD */
  }
    

  return 1;
}

void blit_selection_box(struct world *w, struct display *d, int i) {
  /*  static char box = 0;
  int blitx, blity;
  int x, y;

#ifdef x86
  SDL_Rect offset;
  SDL_Rect srcrect;
#endif
  */
}

#ifdef x86
SDL_Surface * load_surface(char * filename) {
  SDL_Surface *tmp;
  SDL_Surface *img;

  tmp = IMG_Load(filename);
  img = SDL_DisplayFormat(tmp);
  SDL_FreeSurface(tmp);

  return img;
}
#endif

// TODO: this properly, and frees!
int setup_default_display() {

  default_display = malloc(sizeof(struct display));
  default_display->current_view = 0;
  default_display->num_views = 6;
  default_display->views = malloc(sizeof(struct view) * 6);
  default_display->views[0].type = VIEW_MENU;
  default_display->views[1].type = VIEW_MAIN;
  default_display->views[2].type = VIEW_MAP;
  default_display->views[3].type = VIEW_BORDER;
  default_display->views[4].type = VIEW_PLAYERS;
  default_display->views[5].type = VIEW_LOAD;

#ifndef x86
  default_display->views[0].image = loadImage("images/castle.png");
  default_display->views[1].image = createImage(SCREEN_WIDTH, SCREEN_HEIGHT);
  //  default_display->views[1].image = createImage(VIS_WIDTH*CELL_WIDTH, VIS_HEIGHT*CELL_HEIGHT);

  default_display->views[2].image = loadImage("images/map.png");
  default_display->views[3].image = loadImage("images/border1.png");
  default_display->views[4].image = loadImage("images/forest.png");
  default_display->views[5].image = default_display->views[0].image;
  default_display->views[6].image = default_display->views[1].image;
  
#endif


#ifdef x86
  default_display->screen = SDL_SetVideoMode(SCREEN_WIDTH, SCREEN_HEIGHT, SCREEN_BPP, SDL_SWSURFACE);//|SDL_FULLSCREEN);
  if(default_display->screen == NULL) {
    printf("Screen failed to initiate!\n");
    return 1;
  }

  default_display->views[0].image = load_surface("images/castle.png");
  default_display->views[1].image = SDL_CreateRGBSurface(SDL_SWSURFACE, SCREEN_WIDTH, SCREEN_HEIGHT, 32, 0, 0, 0, 0);
  //default_display->views[1].image = SDL_CreateRGBSurface(SDL_SWSURFACE, VIS_WIDTH*CELL_WIDTH, VIS_HEIGHT*CELL_HEIGHT, 32, 0, 0, 0, 0);

  default_display->views[2].image = load_surface("images/map.png");
  default_display->views[3].image = load_surface("images/borderl.png");
  /* select players dialogue */
  default_display->views[4].image = load_surface("images/forest.png");
  /* load game dialogue, possibly also save game dialogue */
  default_display->views[5].image = default_display->views[0].image;

#endif

  return 1;
}

int destroy_default_display() {
#ifndef x86
  freeImage(default_display->views[0].image);
  freeImage(default_display->views[1].image);
  freeImage(default_display->views[2].image);
#endif
#ifdef x86
  SDL_FreeSurface(default_display->views[0].image);
  SDL_FreeSurface(default_display->views[1].image);
  SDL_FreeSurface(default_display->views[2].image);
  SDL_FreeSurface(default_display->screen);
#endif
  free(default_display->views);
  free(default_display);
  return 1;
}

#ifndef x86
void beveled_box(int x, int y, int width, int height, Image *i) {
  /* black border */
  fillImageRect(0x00000000, x, y, width, height, i);
  fillImageRect(0xFFDDDDDD, x+1, y+1, width-1, height-1, i);
  fillImageRect(0xFF999999, x+3, y+3, width-3, height-3, i);
  fillImageRect(0xFFBBBBBB, x+3, y+3, width-6, width-6, i);
}
#endif


void putText(int x, int y, char *text, struct world *w, struct display *d, int view) {
  int i;
  int blitx, blity;
  int m=0; /* marker */
#ifndef x86
  printf("put_text:\n");
#endif

#ifndef x86
  //  printTextImage(x, y, text, 0xFF000000, d->views[view].image);
  //  return;
#endif  

  for (i=0;i<strlen(text);i++) {
    blitx = X((text[i]-ASCII_SPACE), FONT_GRAPHICS_WIDTH) * FONT_WIDTH;
    blity = Y((text[i]-ASCII_SPACE), FONT_GRAPHICS_WIDTH) * FONT_HEIGHT;

    blit_image_image(blitx, blity, FONT_WIDTH, FONT_HEIGHT, w->font,
		     x + m, y, d->views[view].image);

    m += (1 + (font_width[text[i]-ASCII_SPACE]*FONT_MUL));
  }
  
}


#ifdef x86
void blit_image_image(int x, int y, int w, int h, SDL_Surface *srcimage,
		       int x1, int y1, SDL_Surface *destimage) {
  SDL_Rect src;
  SDL_Rect dest;
  
  src.x = x; src.y = y; src.w = w; src.h = h;
  dest.x = x1, dest.y = y1;

  SDL_BlitSurface(srcimage, &src, destimage, &dest);
}

void blit_image_screen(int x, int y, int w, int h, SDL_Surface *srcimage,
		       int x1, int y1) {
  blit_image_image(x, y, w, h, srcimage, x1, y1, default_display->screen);
}

#endif


void dialogue_x(char *txt, struct world *w, struct display *d) {
  int x, y;

  /* the status bar */
  blit_image_image(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT, d->views[3].image, 0, 0, d->views[VIEW_MAIN].image);

  x = (SCREEN_WIDTH/2)-(text_width(txt)/2);
  y = (SCREEN_HEIGHT-STATUS_BAR_HEIGHT)+6;

  putText(x, y, txt, w, d, VIEW_MAIN);

  
  blit_image_screen(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT, d->views[d->current_view].image, 0, 0);
  flip();
    

  //wait_on_x();
}

int text_width(char *txt) {
  int i;
  int width = 0;

  for(i=0;i<strlen(txt);i++) {
    width += (1 + (font_width[txt[i]-ASCII_SPACE]*FONT_MUL));
  }

  return width;
}


int draw_box(int x, int y, int width, int height, struct world *w, struct display *d) {
  return 1;
}

