view gSprite.cc @ 122:5219bf3d5e71

title_draw
author e085768
date Mon, 27 Jun 2011 19:44:46 +0900
parents f2e2460b4011
children 6ef3b0efdeaf
line wrap: on
line source

//SgoexCerium.ccより
#include "matrix_calc.h"
#include "TaskManager.h"
#include "SceneGraph.h"
#include "Application.h"
#include <iostream>

#include <stdio.h>
#include <stdlib.h>
#include "libps2.h"
#include "ps2util.h"
#include "gSprite.h"
#include <stdbool.h>

#define DEFOBJ		100
#define SPRITE_COL	0x4f
#define SCREEN_HALF_W	320
#define SCREEN_HALF_H	240

static SpriteTable	sptable[DEFOBJ];
static ps2utilSprite	sp[DEFOBJ];	// 表示スプライト用スタック
static unsigned int	sp_counter;
static bool		flag_spriteInited = false;	// スプライトロードフラグ

//static TEXTURE *sprite_tex;

extern Viewer	*droot;

/*static*/ SceneGraphPtr	root;

static float	my_scale = 5;


//SgoexCerium.ccより
void
flip()
{
  CameraPtr	camera = droot->sgroot->getCamera();

  droot->sgroot->flip();
  droot->sgroot->lightCalc();

  root		 = droot->createSceneGraph();
  //root->xyz[0] = screen_w/2;
  //root->xyz[1] = screen_h/2;;
  root->xyz[0]	 = 0;
  root->xyz[1]	 = 0;
  //root->xyz[2] = 30.0f;

  /*親の回転、座標から、子の回転、座標を算出*/
  get_matrix(root->matrix, root->angle, root->xyz, camera->matrix);
  /*法線用の行列。Cameraの行列を抜いている(Cameraのコンストラクタで、単位行列にしている)*/
  get_matrix(root->real_matrix, root->angle, root->xyz, camera->real_matrix);
  //出力して、次のものを出力するための準備
  droot->setSceneData(root);
}



/*void
gSprite_DefSprite(int number, int tx, int ty, int tw, int th)
{
  sptable[number].tx = tx;
  sptable[number].ty = ty;
  sptable[number].tw = tw;
  sptable[number].th = th;
}*/

void
gSprite_DefSprite(int number, const char *name, float w, float h, int color)//, OBJECT *obj)
{
  SpriteTable	*m = &sptable[number];
  m->w		   = w;
  m->h		   = h;
  m->color	   = (color & 32);
  m->mx		   = w / 2;	//画像の中心を座標としたいらしい。
  m->my		   = h / 2;
  m->tex_w	   = power_of_two(m->w);
  m->tex_h	   = power_of_two(m->h);
  m->texture	   = (int *)name;
}

/*void
  gSprite_PutSprite(int number, int x, int y)
  {
  ps2utilSprite *p = &sp[sp_counter];

  ps2util_sprite_Set_basicAttribute(
  p,
  (unsigned short)x, (unsigned short)y,
  (unsigned short)sptable[number].tw*2, (unsigned short)sptable[number].th*2,
  (unsigned short)sptable[number].tx, (unsigned short)sptable[number].ty,
  (unsigned short)sptable[number].tw, (unsigned short)sptable[number].th,
  SPRITE_PRIO_FOREGROUND);

  ps2util_sprite_Request(p);
  sp_counter++;
  }*/


//void gSprite_PutSprite(int number, short x, short y, int zorder)
void gSprite_PutSprite(int number, int x, int y)
{
  SpriteTable *m = &sptable[number];

  char *name = (char *) m->texture;
  if (!name) {
    printf("PutSprite %d unknown\n",number);
    return;
  }

  SceneGraphPtr object = droot->createSceneGraph(name);

  // object->c_xyz[0] = m->mx;
  // object->c_xyz[1] = m->my;

  // テスト初期設定:miya
  /*
    object->xyz[0] = 300;
    object->xyz[1] = 100;
    object->xyz[2] = -100;
  */
  /*
    object->xyz[0] -= object->c_xyz[0]*my_scale;
    object->xyz[1] -= object->c_xyz[1]*my_scale;
    object->xyz[2] -= object->c_xyz[2];
  */
  /*
    object->xyz[0] += x+m->mx;
    object->xyz[1] += y+m->my;
  */
  //  object->xyz[2] += zorder * 0.01;


  object->xyz[0] = x;
  object->xyz[1] = y;
  object->xyz[2] = -500;
  
  root->addChild(object);

  float scale[] = {my_scale,my_scale,1};
  //float c_xyz[] = {0,0,0};
  //scale_matrix(object->matrix, scale, c_xyz);


  /*親の回転、座標から、子の回転、座標を算出*/
  get_matrix_scale(object->matrix, object->angle, object->xyz, scale, root->matrix);

  /*法線用の行列。Cameraの行列を抜いている(Cameraのコンストラクタで、単位行列にしている)*/
  get_matrix(object->real_matrix, object->angle, object->xyz, root->real_matrix);

}


/*void
  gSprite_PutSpriteEx(int number, int x, int y, float w, float h)
  {
  ps2utilSprite *p = &sp[sp_counter];

  ps2util_sprite_Set_basicAttribute(
  p,
  (unsigned short)x, (unsigned short)y,
  (unsigned short)sptable[number].tw*w, (unsigned short)sptable[number].th*h,
  (unsigned short)sptable[number].tx, (unsigned short)sptable[number].ty,
  (unsigned short)sptable[number].tw, (unsigned short)sptable[number].th,
  SPRITE_PRIO_FOREGROUND);

  ps2util_sprite_Request(p);
  sp_counter++;
  }
*/

void
gSprite_PutSpriteEx(int number, int x, int y, float scalex, float scaley, float angle)
{
  if (1) {
    gSprite_PutSprite(number, x, y);
    return;
  }

  SpriteTable *m = &sptable[number];
  char *name = (char *) m->texture;
  if (!name) {
    printf("PutSpriteEx %d unknown\n",number);
    return;
  }
  SceneGraphPtr object = droot->createSceneGraph(name);
  root->addChild(object);

  float scale[] = {my_scale*scalex*4,my_scale*scaley*4,1};

  /*親の回転、座標から、子の回転、座標を算出*/
  object->xyz[0] = x - object->c_xyz[0] * my_scale - m->w*my_scale;
  object->xyz[1] = y - object->c_xyz[1] * my_scale - m->h*my_scale;
  object->xyz[2] = number * 0.01;
  //object->c_xyz[0] = m->mx;
  //object->c_xyz[1] = m->my;
  object->angle[3] = angle * (3.1415926*2/4096);
  get_matrix(object->matrix, object->angle, object->xyz, root->matrix);
  float c_xyz[] = {0,0,0};
  scale_matrix(object->matrix, scale, c_xyz);
  /*法線用の行列。Cameraの行列を抜いている(Cameraのコンストラクタで、単位行列にしている)*/
  // get_matrix(object->real_matrix, object->angle, object->xyz, root->real_matrix);
}





//ここいらはどうやら、スプライトを取得する部分。
/*
  static void
  gSprite_Init_Png(const char *texname)
  {
  if (!(sprite_tex = read_png_file(texname))) {
  fprintf(stderr,
  "cannot creat texture - init_png().\n");
  fflush(stderr);
  exit(EXIT_FAILURE);
  }
  
  
  if (ps2util_tex_Set(sprite_tex) == -1) {
  fprintf(stderr,
  "cannot set texture - init_png().\n");
  fflush(stderr);
  exit(EXIT_FAILURE);
  }

  
  }
*/

 /*
static void
gSprite_Create()
{
  int i;

  for (i=0; i<DEFOBJ; i++) {
    if (ps2util_sprite_Create(&sp[i], sprite_tex) == -1) {
      fprintf(stderr,
	      "cannot create sprite No.%d - create_sprite()\n", i);
      fflush(stderr);
      exit(EXIT_FAILURE);
    }
    ps2util_sprite_Unuse_alpha(&sp[i]);
  }
 
  flag_spriteInited = false;

}
*/
  

void
gSprite_Init()
{
  droot->createFromXMLfile("xml_file/title.xml");

  droot->createFromXMLfile("xml_file/select.xml");
  droot->createFromXMLfile("xml_file/sp_meter.xml");
  droot->createFromXMLfile("xml_file/r_arrow.xml");
  droot->createFromXMLfile("xml_file/l_arrow.xml");
  droot->createFromXMLfile("xml_file/km.xml");
  droot->createFromXMLfile("xml_file/rap.xml");
  droot->createFromXMLfile("xml_file/1p.xml");
  droot->createFromXMLfile("xml_file/2p.xml");
  droot->createFromXMLfile("xml_file/select_car_1.xml");
  droot->createFromXMLfile("xml_file/select_car_2.xml");
  droot->createFromXMLfile("xml_file/select_car_3.xml");
  droot->createFromXMLfile("xml_file/select_corse_1.xml");
  droot->createFromXMLfile("xml_file/select_corse_2.xml");

    
  ps2util_graphic_Init();
  //gSprite_Init_Png("img/main2.png");
  //gSprite_Create(); // スプライト生成

  // testCoeler
  int tc = 1;


  gSprite_DefSprite( TITLE, "title", 512, 666, tc); // タイトル  
  gSprite_DefSprite( SELECT, "select", 33,  33, tc); // 選択マーク
  gSprite_DefSprite( SP_METER, "sp_meter", 106,  34, tc); // スピードメーター
  gSprite_DefSprite( R_ARROW,  "r_arrow", 18,  20, tc); // 右矢印
  gSprite_DefSprite( L_ARROW,  "l_arrow", 17,  20, tc); // 左矢印
  gSprite_DefSprite( KM,  "km",  55,  26, tc); // km/h
  gSprite_DefSprite( RAP,  "rap", 39,  26, tc); // Rap
  gSprite_DefSprite( PLAYER_1, "1p", 53, 46, tc); // 1p
  gSprite_DefSprite( PLAYER_2, "2p", 53,  46, tc); // 2P 
  gSprite_DefSprite( SELECT_CAR_1, "select_car_1", 165,  140, tc); // 選択画面の車 
  gSprite_DefSprite( SELECT_CAR_2, "select_car_2", 165,  140, tc);
  gSprite_DefSprite( SELECT_CAR_3,  "select_car_3", 165,  140, tc);
  gSprite_DefSprite( SELECT_CORSE_1, "select_corse_1", 148, 114, tc); // 選択画面のコース 
  // 本当は (82, 272,...)です。二つ目のコース絵がないので 
  gSprite_DefSprite( SELECT_CORSE_2, "select_corse_2", 148, 114 ,tc);

}


static void
gSprite_Clear()
{
  int i;

  for (i=0; i<DEFOBJ; i++) {
    sprite_disappear(&sp[i].attribute);
  }
  sp_counter = 0;
}



void
gSprite_Draw_Reset()
{
  if (flag_spriteInited == true) {
    gSprite_Clear();
  }
}



void
gSprite_PutSprite_Pause()
{
}