view tree_controll.c @ 7:972a7f233b23

g++ compile
author Shinji KONO <kono@ie.u-ryukyu.ac.jp>
date Tue, 07 Dec 2010 23:03:45 +0900
parents 8afbbe129730
children 3b92a4b17049
line wrap: on
line source

#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <SDL.h>
#include <SDL_opengl.h>
#include <SDL_image.h>
#include "object.h"
#include "tree_controll.h"
#include "LoadSprite.h"
#include "xml.h"


static int power_of_two(int input)
{
  int value = 1;

  while ( value < input )
    {
      value <<= 1;
    }
  return value;
}


GLuint SDL_GL_LoadTexture(SDL_Surface *surface, GLfloat *texcoord)
{
  GLuint texture;
  int w, h;
  SDL_Surface *image;
  SDL_Rect area;
  Uint32 saved_flags;
  Uint8  saved_alpha;

  /* Use the surface width and height expanded to powers of 2 */
  w = power_of_two(surface->w);
  h = power_of_two(surface->h);
  texcoord[0] = 0.0f;                     /* Min X */
  texcoord[1] = 0.0f;                     /* Min Y */
  texcoord[2] = (GLfloat)surface->w / w;  /* Max X */
  texcoord[3] = (GLfloat)surface->h / h;  /* Max Y */

  image = SDL_CreateRGBSurface(
			       SDL_SWSURFACE,
			       w, h,
			       32,
#if SDL_BYTEORDER == SDL_LIL_ENDIAN /* OpenGL RGBA masks */
			       0x000000FF, 
			       0x0000FF00, 
			       0x00FF0000, 
			       0xFF000000
#else
			       0xFF000000,
			       0x00FF0000, 
			       0x0000FF00, 
			       0x000000FF
#endif
			       );
  if ( image == NULL )
    {
      return 0;
    }

  /* Save the alpha blending attributes */
  saved_flags = surface->flags&(SDL_SRCALPHA|SDL_RLEACCELOK);
  saved_alpha = surface->format->alpha;
  if ( (saved_flags & SDL_SRCALPHA) == SDL_SRCALPHA )
    {
      SDL_SetAlpha(surface, 0, 0);
    }

  /* Copy the surface into the GL texture image */
  area.x = 0;
  area.y = 0;
  area.w = surface->w;
  area.h = surface->h;
  SDL_BlitSurface(surface, &area, image, &area);

  /* Restore the alpha blending attributes */
  if ( (saved_flags & SDL_SRCALPHA) == SDL_SRCALPHA )
    {
      SDL_SetAlpha(surface, saved_flags, saved_alpha);
    }

  /* Create an OpenGL texture for the image */
  glGenTextures(1, &texture);
  glBindTexture(GL_TEXTURE_2D, texture);
  glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
  glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
  glTexImage2D(GL_TEXTURE_2D,
	       0,
	       GL_RGBA,
	       w, h,
	       0,
	       GL_RGBA,
	       GL_UNSIGNED_BYTE,
	       image->pixels);
  SDL_FreeSurface(image); /* No longer needed */

  return texture;
}


float tes_angle[3] = {0,0,0};


SURFACE *next_draw_node(SURFACE *surfaces)
{
  SURFACE *node = surfaces;
  if(node->child != NULL)
    {
      //glPushMatrix();
      return node->child;
    }
  else if(node->brother != NULL)
    {
      //glPopMatrix();
      //glPushMatrix();
      return node->brother;
    }
  else
    {
      node = node->parent;
      if(node != NULL)
	{
	  //glPopMatrix();
	}
      while(node != NULL)
        {
          if(node->brother != NULL)
            {
	      //glPopMatrix();
	      //glPushMatrix();
	      return node->brother;
            }
          else
            {
              node = node->parent;
	      /*
	      if(node != NULL)
		{
		  //glPopMatrix();
		}
	      */
            }
        }
      return NULL;
    }
}

SURFACE *search_node(OBJECT *top, const char *name)
{
  SURFACE *node;
  SURFACE *ret_node = NULL;
  for(node = top->surfaces;node != NULL;node = next_node(node))
    {
      if(strcmp(node->name, name) == 0)
	{
	  ret_node = node;
	  return ret_node;
	}
    }
  printf("can't not find node\n");
  return ret_node;
}

SURFACE *next_node(SURFACE *surfaces)
{
  SURFACE *node = surfaces;
  if(node->child != NULL)
    {
      return node->child;
    }
  else if(node->brother != NULL)
    {
      return node->brother;
    }
  else
    {
      node = node->parent;
      while(node != NULL)
        {
          if(node->brother != NULL)
            {
	      return node->brother;
            }
          else
            {
              node = node->parent;
            }
        }
      return NULL;
    }
}



void node_prameter_change(OBJECT *top, char *name, float x, float y, float z, float ax, float ay, float az)
{
  SURFACE *node;
  for(node=top->surfaces; node!=NULL; node=next_node(node))
    {
      if(!strcmp(node->name, name))
      {
	node->xyz[0] = x;
	node->xyz[1] = y;
	node->xyz[2] = z;
	node->angle[0] = ax;
	node->angle[1] = ay;
	node->angle[2] = az;
      }
    }
}
	 
void all_object_load_texture(OBJECT *top)
{
  SURFACE *node;
  SDL_Surface *image;
  GLfloat texcoord[4];
  for(node=top->surfaces; node!=NULL; node=next_node(node))
    { 
      //image = SDL_LoadBMP(node->image_name);
      //image = IMG_Load(node->image_name);
      image = LoadSprite(node);
      node->texture = (int *)SDL_GL_LoadTexture(image, texcoord);
      SDL_FreeSurface(image);
    }
}