/*
 * In-memory resource file fixup routine.
 * Copyright (C) 1987 by R.E. Janssen.
 *
 * Insert proper include filename and compile it as a separate module.
 * Make sure the name of the external vars agrees with the main program.
 *
 * Call rsrc_fixup during init, before using the resource.
 *
 * The number of dirty type-conversions and recasts is really awful!!
 *
 */

#include <gemstuff.h>			/* everything we need to know about GEM */

#include "atari_rs.c"			/* get resource definitions */


/*** GLOBAL VARIABLES FROM MAIN PROGRAM ***/

extern WORD gr_hhchar,gr_hwchar,gr_hhbox,gr_hwbox;/* system font sizes */


/*
 * fixup the complete resource, as loaded with the includes
 *
 * this consists of:
 * - changing indices to addresses (pointers)
 * - converting units from characters to pixels
 *
 * dirty trick:
 * any resource tree that's OUTLINED will be converted to screen
 * resolution, others will get a standard conversion.
 * this way, one can display icons in a window on all resolutions,
 * and still get dialog boxes converted properly.
 */

#define fixsize(x,s,t)	   x = ((x & 0xff) * s + (x >> 8) / t)

void rsrc_fixup ()

{
   register int i;
   register OBJECT *object;
   WORD xscale,yscale,ytrim;		/* object scaling factor */

   /* start converting all indices to addresses, type by type */

#if NUM_BB != 0
   for (i = 0; i < NUM_BB; i++)		/* put bitblock addresses */
      rs_bitblk[i].bi_pdata = rs_imdope[(int) rs_bitblk[i].bi_pdata].image;
#endif

#if NUM_FRIMG != 0
   for (i = 0; i < NUM_FRIMG; i++)	/* put image addresses */
      rs_frimg[i] = rs_imdope[(int) rs_frimg[i]].image;
#endif

#if NUM_IB != 0
   for (i = 0; i < NUM_IB; i++)		/* put icon addresses */
   {
      rs_iconblk[i].ib_pmask = rs_imdope[(int) rs_iconblk[i].ib_pmask].image;
      rs_iconblk[i].ib_pdata = rs_imdope[(int) rs_iconblk[i].ib_pdata].image;
      rs_iconblk[i].ib_ptext = rs_strings[(int) rs_iconblk[i].ib_ptext];
   }
#endif

#if NUM_TI != 0
   for (i = 0; i < NUM_TI; i++)		/* tedinfo addresses */
   {
      rs_tedinfo[i].te_ptext  = rs_strings[(int) rs_tedinfo[i].te_ptext];
      rs_tedinfo[i].te_ptmplt = rs_strings[(int) rs_tedinfo[i].te_ptmplt];
      rs_tedinfo[i].te_pvalid = rs_strings[(int) rs_tedinfo[i].te_pvalid];
   }
#endif

#if NUM_OBS != 0
   xscale = yscale = ytrim = 1;		/* initial values - not used */

   for (i = 0; i < NUM_OBS; i++)	     /* objects */
   {
      object = &rs_object[i];		     /* get object address */

      if (object->ob_next == NIL)	     /* root of a tree? */
      {
	 if (object->ob_state & OUTLINED)    /* is it outlined? */
	 {
	    xscale = gr_hwchar;		     /* set proper x scale */
	    yscale = gr_hhchar;		     /* same for y scale */
	    ytrim  = 16 / yscale;	     /* compensate for bad scale */
	 }
	 else
	 {
	    /* use constants instead of gr_hxchar to get a nice picture */
	    /* on a color monitor (although it is bigger, it looks ok)	*/

	    xscale = 8;
	    yscale = 16;
	    ytrim  = 1;
	 }
      }

      fixsize(object->ob_x,xscale,1);	       /* fix x position */
      fixsize(object->ob_y,yscale,ytrim);      /* same for y */
      fixsize(object->ob_width,xscale,1);      /* horizontal size */
      fixsize(object->ob_height,yscale,ytrim); /* vertical size */

      switch (object->ob_type)		/* action depends on type */
      {
      case G_BUTTON:			/* these use a string */
      case G_STRING:
      case G_TITLE:
	 object->ob_spec = rs_strings[(long) object->ob_spec];
	 break;

      case G_IMAGE:
	 object->ob_spec = (char *) &rs_bitblk[(long) object->ob_spec];
	 break;

      case G_ICON:
	 object->ob_spec = (char *) &rs_iconblk[(long) object->ob_spec];
	 break;

      case G_TEXT:			/* these all use a tedinfo */
      case G_BOXTEXT:
      case G_FTEXT:
      case G_FBOXTEXT:
	 object->ob_spec = (char *) &rs_tedinfo[(long) object->ob_spec];
	 break;
      }
   }
#endif

#if NUM_TREE != 0
   for (i = 0; i < NUM_TREE; i++)	/* fix tree address */
      rs_trindex[i] = (long) &rs_object[(int) rs_trindex[i]];
#endif
}

