view field.cc @ 139:00d7de7fe9df default tip

fix collision
author Atuto SHIROMA <e095729@ie.u-ryukyu.ac.jp>
date Thu, 21 Jul 2011 22:49:57 +0900
parents 1a92b58b5d01
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>

#include "gSprite.h"
#include "getStrFromFile.h"


#define BUFSIZE 256

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

// コース一周分のfieldの数
static int field_num_max = 0;

// 自分が走ったfieldの数のカウント
// field_num_max > field_num になったら一周って意味かも。違うかも
static int field_num = 1;

// FieldPtrのroot部分を指し示してるんだともう
static FieldPtr field_list;

// 現在走ってるfield
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, int number )
{
  FieldPtr    self;
  OBJECT      *obj;

  //  PolygonInfo *xml;
  void *free_addr;

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

  obj = (OBJECT*)malloc(sizeof(OBJECT));
    
  // xmlから読み込み
  call_createFromXMLfile(fieldImg);

  /*sStr から eStrまでの間の文字列を取得する */
  const char    *sStr = "surface name=\"";
  const char    *eStr = "\" size=";
  
  // xmlファイルから surface name と 数を取得する
  obj->partNum = getStrFromFile( obj->partName, fieldImg, sStr, eStr );

  memcpy(obj->xyz,    location, sizeof(FVECTOR));
  memcpy(obj->angle, direction, sizeof(FVECTOR));

  ps2util_obj_Renew_transMatrix(obj);

  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, colImg);

#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

  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 == false) {
	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);

	// field_num_max 番目のコース
	newField = field_create(fieldImg, colImg, field_num_max );//, 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)
{
  printf("field_init()\n");
  FILE *fp;
  const 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);
  printf("field_init()\n");
  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];


  // 下の行はしないで良いのかな?
  //memcpy(p->object->xyz,   p->location,  sizeof(FVECTOR));
  // memcpy(p->object->angle, p->direction, sizeof(FVECTOR));

  gSprite_PutObject( COURSE1_1, p->object );
  //  ps2util_obj_Draw(p->object);

  //  ps2_vu0_unit_matrix(&(p->object->matrix));

  /*
    p->object->matrix[3][0] = p->location[0];
    p->object->matrix[3][1] = p->location[1];
    p->object->matrix[3][2] = p->location[2];
    p->object->matrix[3][3] = p->location[3];
  */

  /*
  // 上の5この処理、このやり方でも同じか確かめて
  for( int i = 0; i < 4; i ++ )
    p->object->matrix[12+i] = p->location[0]; //p->object->matrix[11+i] = p->location[0];

  // 今ちょっとコメント
  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( void )
{
  return field_actual;
}