/*
#include <exec/types.h>
#include <exec/memory.h>
#include <libraries/asl.h>
#include <proto/exec.h>
#include <proto/intuition.h>
#include <proto/dos.h>
#include <proto/graphics.h>
#include <proto/Warp3D.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>

#include <cybergraphx/cybergraphics.h>
#include <clib/cybergraphics_protos.h>
#include <pragma/cybergraphics_lib.h>
*/
#define STRICT

// Includes standard Windows
#include <windows.h>
#include <windowsx.h>
#include <time.h>
#include <stdlib.h>
#include <malloc.h>
#include <memory.h>
#include <stdio.h>

// Includes D3D
#define  D3D_OVERLOADS
#include <ddraw.h>
#include <d3d.h>

// Includes utilitaires D3D
#include "d3dmath.h"
#include "d3dutil.h"
#include "D3DEnum.h"

#include <d3dx.h>

// Ids Resources
#include "resource.h"

// Constantes
#include "const.h"

// Types
#include "types.h"

// Variables globales projet
#include "vars.h"

// Prototypes fonctions autres modules
#include "proto.h"

// Macros
#include "macros.h"

W3D_Texture *tex = NULL;
void *texmap = NULL;

UBYTE *LoadTextureFromPPM(APTR where, char *filename, UBYTE Opacity, int *w, int *h)
{
	ULONG *map, *map2;
	FILE *f;
	int i,j;
	UBYTE r,g,b;
	unsigned long x,y;
	int opqy, opqx, o;

	f=fopen(filename, "r");

	if (!f) {
		vTrace("Error: Unable to open file '%s'", filename);
		return NULL;
	}

#ifndef __STORM__
	i=fscanf(f, "P6\n%ld %ld\n255\n", &x, &y);
#else
	i=fscanf(f, "P6\n%ld\n%ld\n255\n", &x, &y);
#endif

	if (i!=2) {
		vTrace("Error: This seems to be no PPM file");
		fclose(f);
		return NULL;
	}

	*w = x;
	*h = y;

	if (where==NULL) {
		map2 = map = (ULONG *) AllocVec(x*y*4, MEMF_PUBLIC);
	} else {
		map = map2 = (ULONG *) where;
	}
	if (!map) {
		fclose(f);
		vTrace("Error: Out of memory");
		return NULL;
	}

	opqy = 256/y;
	opqx = 256/x;
	opqx = opqx>>1;
	o=0;
	for (i=0; i<y; i++) {
		for(j=0; j<x; j++) {
			if (o+j*opqy > 255)
				Opacity = 255;
			else
				Opacity = (UBYTE)(o+j*opqy);

			r=(UBYTE)fgetc(f);
			g=(UBYTE)fgetc(f);
			b=(UBYTE)fgetc(f);
			*map = (Opacity<<24) | ((r&0xFF)<<16) | ((g&0xFF)<<8) | (b&0xFF);
			map++;
		}
		o += opqx;
	}

	fclose(f);
	return (UBYTE *)map2;
}

BOOL GenTexture(W3D_Context* context, char* name)
{
	UBYTE *where;
	int w, h;
	ULONG error;

	// This loads a ppm (24 bit) texture file from disk, and sets
	// it to 50% transparency (0x7F)...
	where = LoadTextureFromPPM(NULL, name, 0x7F, &w, &h);
	if (!where) return FALSE;
	vTrace("Size: %ld×%ld", w,h);

	texmap = where;

	tex = W3D_AllocTexObjTags(context, &error,
		W3D_ATO_IMAGE,      (ULONG)where,   // The image data
		W3D_ATO_FORMAT,     W3D_A8R8G8B8,   // This is a 32 bit image
		W3D_ATO_WIDTH,      w,              // Length of one side
		W3D_ATO_HEIGHT,     h,
	TAG_DONE);

	vTrace("Texture created");

	if (!tex || error != W3D_SUCCESS) {
		vTrace("Error generating texture: ");
		switch(error) {
		case W3D_ILLEGALINPUT:
			vTrace("Illegal input");
			break;
		case W3D_NOMEMORY:
			vTrace("Out of memory");
			break;
		case W3D_UNSUPPORTEDTEXSIZE:
			vTrace("Unsupported texture size");
			break;
		case W3D_NOPALETTE:
			vTrace("Chunky texture without palette specified");
			break;
		case W3D_UNSUPPORTEDTEXFMT:
			vTrace("Texture format not supported");
			break;
		default:
			vTrace("ahem... An error has occured");
		}
		return FALSE;
	}

//	  W3D_UploadTexture(context, tex); // AutoTexManagement will do this for me !-)

	return TRUE;
}

void vInitTextures(W3D_Context *context)
{
	if (FALSE == GenTexture(context, "wall.ppm")) {
		vTrace("Error loading/generating default texture");
		return;
	}

	// Set texture wrapping mode to on.
	W3D_SetWrapMode(context, tex, W3D_REPEAT, W3D_REPEAT, NULL);

    // Set bilinear filter
    W3D_SetFilter(context, tex, W3D_LINEAR, W3D_LINEAR);

    // Set perspective correction
//    W3D_SetState(context, W3D_PERSPECTIVE, W3D_ENABLE);
}

void vCloseTextures(W3D_Context *context)
{
	W3D_FlushTextures(context);
    if (tex) { W3D_FreeTexObj(context, tex); tex = NULL; }
	if (texmap) {       FreeVec(texmap); texmap = NULL; }
}

