/**********************************************************************
This file is part of Crack dot Com's free source code release of Golgotha.
for information about compiling & licensing issues visit this URL
 If that doesn't help, contact Jonathan Clark at 
  golgotha_source@usa.net (Subject should have "GOLG" in it) 
***********************************************************************/

#include "resources.hh"
#include "window/cursor.hh"
#include "memory/malloc.hh"
#include "string/string.hh"
#include "network/login.hh"
#include "string/string.hh"
#include "loaders/load.hh"
#include "r1_api.hh"

#include "sound/sound.hh"

g1_resource_class g1_resources;

float f_get(char *rname)
{
  i4_const_str::iterator s=i4gets(rname).begin();
  return s.read_float();
}


void g1_resource_class::g_float(i4_float &f, char *res_name)
{
  i4_const_str st=use->get(res_name);
  if (st.null())
    i4_error("Constant missing %s\n",res_name);
  i4_const_str::iterator i=st.begin();
  f=i.read_float();
}

void g1_resource_class::g_cursor(i4_cursor_class *&c, char *res_name, int big, int color)
{
  i4_const_str st=use->get(res_name);
  if (st.null())
    i4_error("Constant missing from res/constants.res %s\n",res_name);

  i4_const_str::iterator i=st.begin();
  i4_str *fmt=i.read_string();

  w32 tcolor=i.read_number();
  int hx=i.read_number();  if (!big) hx/=2;
  int hy=i.read_number();  if (!big) hy/=2;

  i4_str *fn=fmt->sprintf(100,big ? "big" : "small", color ? 16 : 2);
  delete fmt;

  i4_image_class *im=i4_load_image(*fn);
  if (!im)
  {
    char buf[100];
    i4_os_string(*fn, buf, 100);
    i4_error("failed to load image for cursor in constants.res : %s",buf);
  }

  delete fn;

  c=new i4_cursor_class(im, tcolor, hx, hy);

  delete im;
}
  
void g1_resource_class::g_w32(w32 &x, char *res_name)
{
  i4_const_str st=use->get(res_name);
  if (st.null())
    i4_error("Constant missing %s\n",res_name);
  i4_const_str::iterator i=st.begin();
  x=i.read_number();
}

void g1_resource_class::g_sw32(sw32 &x, char *res_name)
{
  i4_const_str st=use->get(res_name);
  if (st.null())
    i4_error("Constant missing %s\n",res_name);
  i4_const_str::iterator i=st.begin();
  x=i.read_number();
}

void g1_resource_class::g_w16(w16 &x, char *res_name)
{
  i4_const_str st=use->get(res_name);
  if (st.null())
    i4_error("Constant missing %s\n",res_name);
  i4_const_str::iterator i=st.begin();
  x=i.read_number();
}

void g1_resource_class::g_sw16(sw16 &x, char *res_name)
{
  i4_const_str st=use->get(res_name);
  if (st.null())
    i4_error("Constant missing %s\n",res_name);
  i4_const_str::iterator i=st.begin();
  x=i.read_number();
}

void g1_resource_class::g_point(i4_3d_point_class &p, char *res_name)
{
  i4_const_str *s,*x=use->get_array(res_name);
  
  s=x;
  i4_const_str::iterator i=x->begin();
  p.x=i.read_float();

  x++;
  i=x->begin();
  p.y=i.read_float();

  x++;
  i=x->begin();
  p.z=i.read_float();

  i4_free(s);
}

void g1_resource_class::g_rect(rect &r, char *res_name)
{
 i4_const_str *s,*x=use->get_array(res_name);
  
  s=x;
  i4_const_str::iterator i=x->begin();
  r.x1=i.read_number();

  x++;
  i=x->begin();
  r.y1=i.read_number();

  x++;
  i=x->begin();
  r.x2=i.read_number();

  x++;
  i=x->begin();
  r.y2=i.read_number();

  i4_free(s);
}

void g1_resource_class::cleanup()
{
  int i;
  for (i=0; inull(); str++)
    t_build_context_helps++;


  render_window_expand_mode=R1_COPY_1x1;
}