Esempio n. 1
0
SpriteSheet::SpriteSheet(const char* filename, int tileDim, bool flip)
{
	_numberOfSprites = 0;
	_indexBuffers = NULL;
	if (flip)
	{
		SDL_Surface* surf = loadSurfaceFromImage(filename);
		if (!surf)
		{
			fprintf(stderr, "Fatal: Failed to load %s\n", filename);
			_tex = 0;
			return;
		}
		surf = reverseSpriteSheet(surf, tileDim);
		_tex = createTextureFromSurface(surf);
		SDL_FreeSurface(surf);
	}
	else
	{
		_tex = loadTexture(filename);
		if (_tex == 0)
		{
			fprintf(stderr, "Fatal: Failed to load %s\n", filename);
			return;
		}
	}
	_tileDim = tileDim;
	glGetTexLevelParameteriv(GL_TEXTURE_2D, 0, GL_TEXTURE_WIDTH, &_width);
	glGetTexLevelParameteriv(GL_TEXTURE_2D, 0, GL_TEXTURE_HEIGHT, &_height);

	_widthInSprites = _width / tileDim;
	unsigned int heightInSprites = _height / tileDim;

	_numberOfSprites = _widthInSprites * heightInSprites;

	_indexBuffers = new GLuint[_numberOfSprites];

	_clips.resize(_numberOfSprites);

	unsigned int i, j;

	for (i = 0; i < _widthInSprites; i++)
	{
		for (j = 0; j < heightInSprites; j++)
		{
			_clips[j * _widthInSprites + i].w = tileDim;
			_clips[j * _widthInSprites + i].h = tileDim;

			_clips[j * _widthInSprites + i].x = i * tileDim;
			_clips[j * _widthInSprites + i].y = j * tileDim;
		}
	}
	LOGF((stdout, "%s is %ix%i pixels and has %i %ix%i sprites.\n", filename, _width, _height, _numberOfSprites, tileDim, tileDim));
}
Esempio n. 2
0
bool PhGraphicImage::init()
{
	_surface = IMG_Load(_filename.toStdString().c_str());
	if(_surface != NULL) {
		if(createTextureFromSurface(_surface)) {
			PHDEBUG << "Loading image";
			return true;
		}
	}

	PHDEBUG<<"Error loading:"<< _filename;
	return false;

}
Esempio n. 3
0
bool FontTexture::loadFromFile(std::string text, SDL_Renderer* renderer, SDL_Color textColor, SDL_Color backgroundColor)
{
  //Get rid of preexisting texture
  free();

  //Success flag
  bool success = true;

  //Render text surface
  SDL_Surface* textSurface = TTF_RenderText_Shaded( mFont, text.c_str(), textColor, backgroundColor );
  if(textSurface == NULL)
  {
    printf("Unable to render text surface! SDL_ttf Error: %s\n", TTF_GetError());
  }
  else
  {
    createTextureFromSurface(renderer, textSurface);
  }
  //Return success
  return mTexture != NULL;
}