Write TGA's with C-Script

From GameStudio Wiki

Jump to: navigation, search

this script saves the canvas bitmap to the file output.tga once you press the "s" key. the canvas bitmap has to be a 32bit bitmap with alpha channel. if you want to save 24bit tga files you simply have to change the color depth in the tga header and remove the line which writes the alpha value.

Code:

Just insert the following code to your main function:

bmap* canvas;
savedir="output";
var_nsave fhandle;

//write tga
function write8(byte) // write char
{
   file_asc_write(fhandle,byte);
}

function write16(short) // write unsigned short
{
  file_asc_write(fhandle,short&255);
  file_asc_write(fhandle,(short>>8)&255);
}

function write_tga()
{
  var i;

  var format;
  var pixel;
  var pixelalpha;
  var canvas_size[2]; 

  canvas_size.x=bmap_width(canvas);
  canvas_size.y=bmap_height(canvas);
  format=bmap_lock(canvas,0);

  fhandle=file_open_write("output.tga");
  //------------------------------------write header
  write8(0);
  write8(0);
  write8(2); // image type
  write16(0);
  write16(0);
  write8(0);
  write16(0);
  write16(0);
  write16(canvas_size.x); // width
  write16(canvas_size.y); // height
  write8(32); // color depth
  write8(0);
  //------------------------------------write image data
  i=0;
  while(i<canvas_size.x*canvas_size.y)
  {
  	pixel=pixel_for_bmap(canvas,i%canvas_size.x,(canvas_size.y-1)-int(i/canvas_size.x));
  	pixel_to_vec(temp,pixelalpha,format,pixel);

  	write8(temp.x); // b
  	write8(temp.y); // g
  	write8(temp.z); // r
  	write8(pixelalpha*2.55); // a (0..100 -> 0..255)  

  	i+=1;
  }
  file_close(fhandle);

  bmap_unlock(canvas);
}

on_s=write_tga;
Personal tools