Esempio n. 1
0
Sprite *LoadSwappedSprite(char *filename, int sizex, int sizey, int c1, int c2, int c3)
{
	int i;
	SDL_Surface *temp;
	int n;

	/*first search to see if the requested sprite image is already loaded*/
	for(i = 0; i < MaxSprites; i++)
	{
		if((strncmp(filename,SpriteList[i].filename,80)==0)&&(SpriteList[i].loaded == 1)&&(sizex == SpriteList[i].w)&&(sizey == SpriteList[i].h))
		{
			SpriteList[i].used++;
			return &SpriteList[i];
		}
	}
	/*make sure we have the room for a new sprite*/
	/*if it's not already in memory, then load it*/
	n = -1;
	for(i = 0; i < MaxSprites; i++)
	{
		if(!SpriteList[i].loaded)
		{
			n = i;
			break;
		}
	}
	/*if every slot has been loaded, find one that is no longer in use*/
	if(n == -1)
	{
		for(i = 0; i < MaxSprites; i++)
		{
			if(SpriteList[i].used <= 0)
			{
				n = 1;
				break;
			}
		}
		if(n == -1)
		{
			fprintf(stderr,"Ran out of places for sprites!\n");
			return NULL;
		}
		strcpy(SpriteList[n].filename,"\0");
		if(SpriteList[n].surface != NULL)SDL_FreeSurface(SpriteList[n].surface);
		SpriteList[n].surface = NULL;
	}
	temp = IMG_Load(filename);
	if(temp == NULL)
	{
        fprintf(stderr,"unable to load a vital sprite: %s\n",SDL_GetError());
        return NULL;
	}
	SDL_SetColorKey(temp, SDL_SRCCOLORKEY, SDL_MapRGB(temp->format, 255,255,255));
	SpriteList[i].surface = SDL_DisplayFormat(temp);
	SDL_FreeSurface(temp);
	/*sets a transparent color for blitting.*/
	//SDL_SetColorKey(SpriteList[i].image, SDL_SRCCOLORKEY , SDL_MapRGB(SpriteList[i].image->format, 255,255,255));
	//fprintf(stderr,"asked for colors: %d,%d,%d \n",c1,c2,c3);
	SwapSprite(SpriteList[i].surface,c1,c2,c3);

	strncpy(SpriteList[i].filename,filename,80);
	SpriteList[i].framesperline = 16;

	if((sizex) && (sizey))/*as long as neither x and y are zero*/
	{
		SpriteList[i].numframes = (SpriteList[i].surface->w / sizex) * (SpriteList[i].surface->h / sizey);
	}
	else SpriteList[i].numframes = -1;
	SpriteList[i].w = sizex;
	SpriteList[i].h = sizey;
	SpriteList[i].color1 = c1;
	SpriteList[i].color2 = c2;
	SpriteList[i].color3 = c3;
	SpriteList[i].used++;
	SpriteList[i].loaded = 1;
	return &SpriteList[i];
}
Esempio n. 2
0
Sprite *LoadSwappedSprite( char *filename, int sizex, int sizey, int c1, int c2, int c3 , int mipmapping)
{
  int i;
  int k,j;
  SDL_Surface *temp;
  SDL_Surface *temp1;
  SDL_Surface *tempsurface;
  int w, h;
  Uint8 r,g,b,a;
  /*first search to see if the requested sprite image is alreday loaded*/
  for(i = 0; i < MaxSprites; i++)
  {
    if((strncmp(filename,SpriteList[i].filename,80)==0)&&(SpriteList[i].used >= 1)&&(c1 == SpriteList[i].color1)&&(c2 == SpriteList[i].color2)&&(c3 == SpriteList[i].color3))
    {
      SpriteList[i].used++;
      return &SpriteList[i];
    }
  }
  /*makesure we have the room for a new sprite*/
  if(NumSprites + 1 >= MaxSprites)
  {
    fprintf(stderr, "Maximum Sprites Reached.\n");
    exit(1);
  }
  /*if its not already in memory, then load it.*/
  NumSprites++;
  for(i = 0;i <= MaxSprites;i++)
  {
    if(!SpriteList[i].used)break;
  }
  temp = IMG_Load(filename);
  if(temp == NULL)
  {
    fprintf(stderr,"unable to load a vital sprite: %s\n",SDL_GetError());
    exit(0);
  }
  
  temp1 = SDL_DisplayFormatAlpha(temp);
  strncpy(SpriteList[i].filename,filename,80);

  SpriteList[i].framesperline = 16;
  /*makes sure the width and height of each frame is opengl compatable*/
  SpriteList[i].w = sizex;
  SpriteList[i].h = sizey;
  SpriteList[i].used++;
  /*makes sure the width and height of the entire surface is opengl compatable*/
  w = temp1->w;
  h = temp1->h;
  SpriteList[i].texWidth = (float)sizex/(float)w;
  SpriteList[i].texHeight = (float)sizey/(float)h;
  /*Creates an opengl compatable RGBA surface*/
  tempsurface = SDL_CreateRGBSurface(SDL_HWSURFACE|SDL_SRCALPHA,w, h,S_Data.depth,rmask,gmask,bmask,amask);

  /*This goes through the pixel data copying it and checking each pixel for the clear color, when found sets the alpha to 0*/
  for(k=0;k < tempsurface->h; k++)
  {
    for(j=0;j < tempsurface->w; j++)
    {
      SDL_GetRGBA(getpixel(temp1, j, k), temp1->format, &r, &g, &b, &a);
      putpixel(tempsurface, j, k, SDL_MapRGBA(tempsurface->format, r, g, b, a));

    }
  }

  SpriteList[i].color1 = c1;
  SpriteList[i].color2 = c2;
  SpriteList[i].color3 = c3;
  SwapSprite(tempsurface,c1,c2,c3);
    glGenTextures(1, &SpriteList[i].image);
    glBindTexture(GL_TEXTURE_2D,SpriteList[i].image);
    if(mipmapping == 1)
    {
        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST_MIPMAP_LINEAR);
        gluBuild2DMipmaps(GL_TEXTURE_2D, GL_RGBA, w, h, GL_RGBA, GL_UNSIGNED_BYTE, tempsurface->pixels);
    }else
    { 
        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
        glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, w, h, 0, GL_RGBA, GL_UNSIGNED_BYTE, tempsurface->pixels);
    }
  SDL_FreeSurface(tempsurface);
  tempsurface = NULL;
  SDL_FreeSurface(temp1);
  temp1 = NULL;
  SDL_FreeSurface(temp);
  temp = NULL;
  return &SpriteList[i];
}