gl_dev.c 4.37 KB
#include <stdio.h>
#include <bstring.h>
#include "graphic.h"
#include <gl/gl.h>
#include <gl/device.h>
#include <gl/image.h>

static int repl;	/* pixel replication factor */

/*---------------------------------------------*/
open_display( xsize, ysize, zoom)
int xsize, ysize, zoom;
{
	repl = (zoom < 1) ? 1 : zoom;
	/*
	 * set window style preferences
	 */

/*	noborder();*/
	prefsize( xsize*repl, ysize*repl);
	prefposition(0, xsize*repl-1, 0, ysize*repl-1);

	/*
	 * open and configure a RGB or CI window
	 * window mapped to screen space, (0,0) = lower left of window
	 */

	winopen("test");
	RGBmode();			/* for CI, cmode(); */
	gconfig();
}
/*---------------------------------------------*/
clear_display( color)
int color;
{
	cpack( color);
	clear();
}
/*---------------------------------------------*/
idle_display()
{
	while (!getbutton(ESCKEY))
		;
	while (getbutton(ESCKEY))
		;
}
/*---------------------------------------------*/
put_pixel( x, y, color)
int x, y, color;
{
	cpack(color);	
	if (repl <= 1)
		pnt2i( x, y);
	else {
		x *= repl;
		y *= repl;
		rectfi( x, y, x + repl - 1, y + repl - 1);
		}
}
/*---------------------------------------------*/
get_pixel( x, y, color)
int x, y;
unsigned int *color;
{
if (repl > 1) {
	x *= repl;
	y *= repl;
	}
lrectread( x, y, x, y, color);
}
/*---------------------------------------------*/
make_movie(name, xsize, ysize)
char	*name;
int	xsize, ysize;
{
        static int      num = 1;
        char            filename[80];
	IMAGE		*img;
	unsigned long	*lb;
	unsigned short	*cb;
	int		x, y;

        sprintf(filename, "%s%03d", name, num++);
	img = iopen(filename, "w", RLE(1), 3, xsize, ysize, 3);
	if (img == (IMAGE *)0) {
		fprintf(stderr, "error in creating image %s\n", filename);
		return;
	}

	lb = (unsigned long *)malloc(sizeof(unsigned long) * xsize);
	cb = (unsigned short *)malloc(sizeof(unsigned short) * xsize);
	for (y=0; y<ysize; y++) {
		lrectread(0, y, xsize-1, y, lb);
		for (x=0; x<xsize; x++) cb[x] = lb[x] & 0xff;
		putrow(img, cb, y, 0);
		for (x=0; x<xsize; x++) cb[x] = (lb[x] >> 8) & 0xff;
		putrow(img, cb, y, 1);
		for (x=0; x<xsize; x++) cb[x] = (lb[x] >> 16) & 0xff;
		putrow(img, cb, y, 2);
	}
	free(lb);
	free(cb);
	iclose(img);
}
/*---------------------------------------------*/
struct Image *load_texfile(char *file, int type)
{
	struct Image	*texture;
	IMAGE		*img;
	int		x, y;
	unsigned short	*cp, *sp;

	int avg_red=0, avg_grn=0, avg_blu=0, avg_alp=0; 

	if ((img = iopen(file, "r")) == (IMAGE *)NULL) {
		fprintf(stderr, "can't open texture file %s\n", file);
		exit(EXIT_FAILURE);
	}

	texture = (struct Image *)malloc(sizeof(struct Image));
	texture->xsize = img->xsize;
	texture->ysize = img->ysize;
	texture->type = type;

	texture->lsize = texture->xsize * IM_BYTE(texture->type);
	texture->base = (ubyte *)malloc(texture->ysize * texture->lsize);
	bzero(texture->base, texture->lsize * texture->ysize);

	cp = (unsigned short *)malloc(texture->xsize * sizeof(unsigned short));
	sp = (unsigned short *)texture->base;

	/*
	 * XXX for now, 4444 4 component textures
	 */
	for (y=0; y<texture->ysize; y++) {
		getrow(img, cp, y, 0);
		for (x=0; x<texture->xsize; x++) sp[x] = (cp[x] & 0xf0) >> 4;
		for (x=0; x<texture->xsize; x++) avg_red += cp[x];
		getrow(img, cp, y, 1);
		for (x=0; x<texture->xsize; x++) sp[x] |= (cp[x] & 0xf0);
		for (x=0; x<texture->xsize; x++) avg_grn += cp[x];
		getrow(img, cp, y, 2);
		for (x=0; x<texture->xsize; x++) sp[x] |= (cp[x] & 0xf0) << 4;
		for (x=0; x<texture->xsize; x++) avg_blu += cp[x];
/* XXXX hack to test antialiasing on black background */
if (en_antialias) {
	for (x=0; x<texture->xsize; x++) 
		if (sp[x] == 0) { sp[x] = 0x444;
			avg_red += 0x40; 
			avg_grn += 0x40; 
			avg_blu += 0x40; 
			}
	}
		if (img->zsize > 3) {
			getrow(img, cp, y, 3);
			for (x=0; x<texture->xsize; x++) sp[x] |= (cp[x] & 0xf0) << 8;
			for (x=0; x<texture->xsize; x++) avg_alp += cp[x];
		}
		else {
			for (x=0; x<texture->xsize; x++)
				if (sp[x]) sp[x] |= 0xf0 << 8;
			for (x=0; x<texture->xsize; x++) avg_alp += 0xf0;
		}

		sp += texture->xsize;
	}
	avg_red = (int)((float)avg_red/(texture->xsize*texture->ysize));
	avg_grn = (int)((float)avg_grn/(texture->xsize*texture->ysize));
	avg_blu = (int)((float)avg_blu/(texture->xsize*texture->ysize));
	avg_alp = (int)((float)avg_alp/(texture->xsize*texture->ysize));
	texture->avg_color =
		pack_colora( avg_red, avg_grn, avg_blu, avg_alp);

	free(cp);
	iclose(img);
	return (texture);
}