/*
 *                               BROKEN HELIX
 *
 *   File: scrnsht.c
 *   Author: Scott A. Krotz
 *   Function: This function makes screenshots.
 *             
 *
 *   Copyright (C) 1996 by Konami Computer Entertainment Chicago
 *
 */

//#include <sys/types.h>
//#include <libetc.h>
//#include <libgte.h>
//#include <libgpu.h>
//#include <libsn.h>

//-------------------------------------
typedef struct
{
   unsigned char id_field_length;
   unsigned char color_map_type;
   unsigned char image_type;

   unsigned char first_color_map_entry_l;
   unsigned char first_color_map_entry_h;

   unsigned char color_map_length_l;
   unsigned char color_map_length_h;

   unsigned char color_map_entry_size;

   unsigned char image_x_origin_l;
   unsigned char image_x_origin_h;

   unsigned char image_y_origin_l;
   unsigned char image_y_origin_h;

   unsigned char image_width_l;
   unsigned char image_width_h;

   unsigned char image_height_l;
   unsigned char image_height_h;

   unsigned char bits_per_pixel;
   unsigned char image_descriptor_bits;
} TARGA_HEADER;

/* image type codes */
#define NO_IMAGE 0
#define COLOR_MAPPED_UNCOMPRESSED 1
#define TRUE_COLOR_UNCOMPRESSED 2

/* image descriptor codes */
#define LRTB 0x20 /* left right top to bottom */

//-------------------------------------
//
//-------------------------------------
void screenshot (int id)
{
   RECT            screen_rect;
   int             file_id, n, i;
   unsigned short  buffer[320];
   unsigned short  color, r, g, b;
   TARGA_HEADER    targa_header;

   screen_rect.x = 0;
   if (id)
      screen_rect.y = 240;
   else
      screen_rect.y = 0;
   screen_rect.w = 320;
   screen_rect.h = 1;

//   PCinit ();
//   file_id = PCopen ("c:\\temp\\screen.tga", 1, 0);
//   if (file_id == -1)
   file_id = PCcreat ("c:\\temp\\screen.tga", 1);

   targa_header.id_field_length = 0;
   targa_header.color_map_type = 0;
   targa_header.image_type = TRUE_COLOR_UNCOMPRESSED;

   targa_header.image_x_origin_h = 0;
   targa_header.image_x_origin_l = 0;

   targa_header.image_y_origin_h = 0;
   targa_header.image_y_origin_l = 0;

   targa_header.image_width_l = (320 & 0xff);
   targa_header.image_width_h = (320 >> 8);

   targa_header.image_height_l = (240 & 0xff);
   targa_header.image_height_h = (240 >> 8);

   targa_header.bits_per_pixel = 16;

   targa_header.image_descriptor_bits = LRTB;

   PCwrite (file_id, (char *)&targa_header, sizeof (TARGA_HEADER));

   for (n = 0; n < 240; n++)
   {
      StoreImage (&screen_rect, (u_long *)buffer);

      for (i = 0; i < 320; i++)
      {
         b = (buffer[i] >> 10) & 31;
         g = (buffer[i] >> 5) & 31;
         r = (buffer[i] >> 0) & 31;

         color = (r << 10);
         color |= (g << 5);
         color |= b;

         buffer[i] = color;
      }

      PCwrite (file_id, (char *)buffer, (320 * 2));
      screen_rect.y++;
   }

   PCclose (file_id);
}
