view field.c @ 79:376bac6b05f1

*** empty log message ***
author gongo
date Thu, 09 Nov 2006 08:28:48 +0000
parents a1908b83f8fa
children ce9ef7dcf2f0
line wrap: on
line source

/*
 * $Id$
 */
#include <stdio.h>
#include <stdlib.h>
#include "libps2.h"
#include "ps2util.h"
#include "mytype.h"
#include "field.h"
#include "car.h"
#include "quotanion.h"

#define DEFAULT_SET (EFFECT_TEXTURE_USE | EFFECT_ALPHABLEND_UNUSE | EFFECT_SHADING_FLAT)
#define BUFSIZE 256

/* --- main.c --- */
extern FILE *main_fp;

static int field_num_max = 0;
static int field_num = 1;

static FieldPtr field_list;
static FieldPtr field_actual;

static FieldPtr
field_create(char *fieldImg, char *colImg, TEXTURE* tex)
{
    FieldPtr    self;
    OBJECT      *obj;
    PolygonInfo *xml;
    void *free_addr;

    FVECTOR location  = {0, 0, 0, 1};
    FVECTOR direction = {0, 0, 0, 1};

    obj = ps2util_obj_Create_fromXML(fieldImg);
    memcpy(obj->xyz,    location, sizeof(FVECTOR));
    memcpy(obj->angle, direction, sizeof(FVECTOR));
    ps2util_obj_Renew_transMatrix(obj);
    ps2util_obj_Set_effect(obj, DEFAULT_SET);
    ps2util_obj_Set_texture(obj, tex);

    xml = xml_read_file(colImg);    //$B$3$3$G%3!<%9$NAv$l$k>l=j(B($BEv$?$jH=Dj(B)

    if (malloc_align16(&free_addr, &self, sizeof(Field)) == -1) {
	fprintf(stderr, "field.c: malloc_align16 error\n");
	fflush(stderr);
	exit(EXIT_FAILURE);
    }
    self->object = obj;
    self->next   = NULL;
    self->prev   = NULL;
    self->free_addr = free_addr;
#ifdef DEBUG
    fprintf(main_fp, "malloc fieldNode addr = %x(free), %x(obj)\n", (int)free_addr, (int)self);
#endif
    col_init(&self->colface, xml);

#ifdef DEBUG
    fprintf(main_fp, "       facenum = %d\n", self->colface.facenum);
    fprintf(main_fp, "       facenum = %x(free), ", (int)self->colface.free_addr);
    fprintf(main_fp, "facenum = %x(obj)\n", (int)self->colface.colface);
#endif

    xml_free(xml);

    return self;
}

static void
field_new_readCSV(FILE *fp, int id)
{
    FieldPtr fieldHead = NULL;
    FieldPtr newField  = NULL;
    FieldPtr prevField = NULL;
    char buff[BUFSIZE], *bufp;
    char fieldImg[BUFSIZE], colImg[BUFSIZE], texname[BUFSIZE];
    TEXTURE *tex = NULL;
    int course_id;
    Bool flag = FALSE;

    while ((bufp = fgets(buff, BUFSIZE, fp)) != NULL) {
	bufp++;

	switch (buff[0]) {
	case 'n': // $B%3!<%9(BID$B$NFI$_<h$j(B
	    if (!flag) {
		sscanf(bufp, " %d\n", &course_id);
		if (id == course_id) {
		    flag = TRUE;
		}
	    } else {
		goto FINISH_LOAD;
	    }
	    break;
	case 't': // $B%F%/%9%A%c$N%m!<%I(B
	    if (flag) {
		sscanf(bufp, " %s\n", texname);
		tex = read_png_file(texname);
		ps2util_tex_Set(tex);
	    }
	    break;
	case 'c': // $B%3!<%9$N%m!<%I(B
	    if (flag) {
		sscanf(bufp, " %s %s\n", fieldImg, colImg);
		newField = field_create(fieldImg, colImg, tex);
		if (!fieldHead) {
		    fieldHead = prevField = newField;
		} else {
		    prevField->next = newField;
		    newField->prev = prevField;
		    prevField = newField;
		}
		newField->init = fieldHead;	
		field_num_max++;
	    }
	    break;
	default:
	    break;
	}
    }

 FINISH_LOAD:
    // $B@hF,$H:G8eHx$N%j%s%/$rD%$k(B
    newField->next = newField->init;
    newField->init->prev = newField;

    field_list = field_actual = fieldHead;
}


/*---------------------/
  FieldPtr
  field_init:
    $B;XDj$7$?%U%!%$%k$+$i%3!<%9$r@8@.$9$k!#(B
  $BLa$jCM(B
    $B%3!<%9@hF,$N%]%$%s%?(B
/---------------------*/
void
field_init(int id)
{
    FILE *fp;
    char *filename = "course/course.dat";

    if (!(fp = fopen(filename, "r"))) {
        fprintf(main_fp, "error read file %s\n", filename);
	exit(EXIT_FAILURE);
    }
    field_new_readCSV(fp, id);
    fclose(fp);
}


static void
field_draw(FieldPtr p)
{
    ps2_vu0_unit_matrix(p->object->transfer);

    p->object->transfer[3][0] = p->location[0];
    p->object->transfer[3][1] = p->location[1];
    p->object->transfer[3][2] = p->location[2];
    p->object->transfer[3][3] = p->location[3];
    ps2util_obj_Draw(p->object);
}


void
field_update(CarPtr car)
{
    FieldPtr p;

    p = field_actual;
    // current
    p->location[0] = -car->location[0];
    p->location[1] = -car->location[1];
    p->location[2] = -car->location[2];
    p->location[3] = 1;
    field_draw(p);

    // next
    p = field_actual->next;
    if (p) {
	p->location[0] = -car->location[0];
	p->location[1] = -car->location[1];
	p->location[2] = -car->location[2];
	p->location[3] = 1;
	field_draw(p);
    }

    // prev
    p = field_actual->prev;
    if (p) {
	p->location[0] = -car->location[0];
	p->location[1] = -car->location[1];
	p->location[2] = -car->location[2];
	p->location[3] = 1;
	field_draw(p);
    }
}

void
field_destroy()
{
    FieldPtr p = field_list;
    FieldPtr d;
    TEXTURE *t;

    p->prev->next = NULL;

    t = p->object->surfaces->texture;
    ps2util_tex_Exclude(t);
    free_texture(t);

    while (p) {
	d = p;
	p = p->next;
	ps2util_obj_Free(d->object);
	free(d->colface.free_addr);
	free(d->free_addr);
#ifdef DEBUG
	fprintf(main_fp, "free fieldNode addr = %x\n", (int)d->free_addr);
#endif
    }

    field_num_max = 0;
    field_num = 1;
}

/*
 * $B<~2s5-O?!#(B1$B<~$9$kKh$K(B1$B$rJV$9!#(B
 */
int
field_rap_increment(int n)
{    
    field_num += n;
    if (field_num > field_num_max) {
	field_num = 1;
	return 1;
    } else {
	return 0;
    }
}

void
field_set_actual(FieldPtr p)
{
    field_actual = p;
}

FieldPtr
field_get_actual()
{
    return field_actual;
}