view field.c @ 96:0b65ca27f113

compile on Mac OS X 2
author Atuto SHIROMA <e095729@ie.u-ryukyu.ac.jp>
date Thu, 02 Jun 2011 14:56:11 +0900
parents cb6c6de125dc
children
line wrap: on
line source

#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <string.h>
#include "libps2.h"
#include "ps2util.h"
#include "mytype.h"
#include "field.h"
#include "car.h"
#include "quotanion.h"
#include <stdbool.h>

#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;

// 適当に自作:miya
PolygonInfo  xml_read_file(char *colImg)
{
  PolygonInfo test;
  printf("xml_read_file");

  return test;
}


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);    //ここでコースの走れる場所(当たり判定)

  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': // コースIDの読み取り
      if (!flag) {
	sscanf(bufp, " %d\n", &course_id);
	if (id == course_id) {
	  flag = true;
	}
      } else {
	goto FINISH_LOAD;
      }
      break;
    case 't': // テクスチャのロード
      if (flag) {
	sscanf(bufp, " %s\n", texname);
	//tex = read_png_file(texname);
	ps2util_tex_Set(tex);
      }
      break;
    case 'c': // コースのロード
      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:
  // 先頭と最後尾のリンクを張る
  newField->next = newField->init;
  newField->init->prev = newField;

  field_list = field_actual = fieldHead;
}


/*---------------------/
  FieldPtr
  field_init:
    指定したファイルからコースを生成する。
  戻り値
    コース先頭のポインタ
/---------------------*/
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;
}

/*
 * 周回記録。1周する毎に1を返す。
 */
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;
}