示例#1
0
std::shared_ptr<SDL_Surface> CSprite::createCopySDLSurface(
        const std::shared_ptr<SDL_Surface>& original)
{
    std::shared_ptr<SDL_Surface> surface;

    if(original->format->BitsPerPixel < 16)
    {
        surface.reset(SDL_CreateRGBSurface( 0, m_xsize, m_ysize, 8, 0, 0, 0, 0), &SDL_FreeSurface);
#if SDL_VERSION_ATLEAST(2, 0, 0)
        SDL_SetPaletteColors(surface->format->palette, original->format->palette->colors, 0, 255);
        SDL_SetColorKey(surface.get(), SDL_TRUE, COLORKEY);
#else
        SDL_SetColors( surface.get(), original->format->palette->colors, 0, 255);
        SDL_SetColorKey( surface.get(), SDL_SRCCOLORKEY, COLORKEY ); // One black is the color key. There is another black, as normal color
#endif

        auto *origSfcPtr = original.get();

        SDL_FillRect( surface.get(), NULL, COLORKEY );
        SDL_BlitSurface( origSfcPtr, NULL, surface.get(), NULL);
    }
    else
    {
        surface.reset(SDL_ConvertSurface(original.get(),original->format,0));
    }

    return surface;
}
示例#2
0
文件: texture.cpp 项目: m06x/wesnoth
ttexture::ttexture(SDL_Renderer& renderer,
				   const int access,
				   const surface& surface)
	: reference_count_(new unsigned(1))
	, texture_(NULL)
	, rotation_(0)
	, hscale_(1)
	, vscale_(1)
	, smooth_scaling_(false)
	, flip_(SDL_FLIP_NONE)
	, clip_()
	, mod_r_(255)
	, mod_g_(255)
	, mod_b_(255)
	, alpha_(255)
	, source_surface_(
			SDL_ConvertSurface(surface, surface->format, surface->flags))
{
	if(source_surface_ == NULL) {
		throw texception("Invalid source_surface__ argument passed, failed to "
						 "create SDL_Texture object.",
						 false);
	}

	initialise_from_surface(renderer, access);
}
示例#3
0
文件: 06.cpp 项目: Fonzki/SDLBoys
SDL_Surface* loadSurface( std::string path )
{
	//The final optimized image
	SDL_Surface* optimizedSurface = NULL;

	//Load image at specified path
	SDL_Surface* loadedSurface = IMG_Load( path.c_str() );
	if( loadedSurface == NULL )
	{
		printf( "Unable to load image %s! SDL_image Error: %s\n", path.c_str(), IMG_GetError() );
	}
	else
	{
		//Convert surface to screen format
		optimizedSurface = SDL_ConvertSurface( loadedSurface, gScreenSurface->format, NULL );
		if( optimizedSurface == NULL )
		{
			printf( "Unable to optimize image %s! SDL Error: %s\n", path.c_str(), SDL_GetError() );
		}

		//Get rid of old loaded surface
		SDL_FreeSurface( loadedSurface );
	}

	return optimizedSurface;
}
示例#4
0
Texture::Texture(unsigned dsize, const char* data, const char *extension, const char *mimeType)
{
  SDL_PixelFormat RGB_PixelFormat = {
    NULL,
    18,3,
    0,0,0,8,
    0,8,10,0,
    //! \todo bug with big endian?!
    0xff,0xff00,0xff0000,0x0,
    0,
    0xff
  };
    
  SDL_PixelFormat RGBA_PixelFormat = {
    NULL,
    32,4,
    0,0,0,0,
    0,8,16,24,
    //! \todo bug with big endian?!
    0xff,0xff00,0xff0000,0xff000000,
    0,
    0xff
  };

  SDL_Surface* image=loadImage(dsize, data, extension, mimeType);
  GLint internalformat;
  GLint format;
  GLenum type=GL_UNSIGNED_BYTE;
  SDL_PixelFormat* pixelformat;
  if (image->format->Amask) {
    // We have got a alpha channel !
    internalformat=GL_RGBA8;
    format=GL_RGBA;
    haveAlphaChannel=true;
    pixelformat=&RGBA_PixelFormat;
  }else{
    // as fallback convert to rgb
    internalformat=GL_RGB8;
    format=GL_RGB;
    haveAlphaChannel=false;
    pixelformat=&RGB_PixelFormat;
  }
  SDL_Surface* tmp=SDL_ConvertSurface(image,pixelformat,SDL_SWSURFACE);
  SDL_FreeSurface(image);
  image=tmp;
  tmp=NULL;
  width=image->w;
  height=image->h;
  
  glGenTextures(1,&textureID);
  glBindTexture(GL_TEXTURE_2D,textureID);
  int q=(2>1) ? GL_LINEAR : GL_NEAREST;
  glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,q); 
  glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,q);
  // 2d texture, level of detail 0 (normal), 3 components (red, green, blue), x size from image, y size from image,
  // border 0 (normal), rgb color data, unsigned byte data, and finally the data itself.
  glTexImage2D(GL_TEXTURE_2D, 0, internalformat, width, height, 0, format , type, image->pixels);
  SDL_FreeSurface(image);
}
    //------------------------------------------------------------------------
    bool platform_support::load_img(unsigned idx, const char* file)
    {
        if(idx < max_images)
        {
            if(m_specific->m_surf_img[idx]) SDL_FreeSurface(m_specific->m_surf_img[idx]);

            char fn[1024];
            strcpy(fn, file);
            int len = strlen(fn);
            if(len < 4 || strcmp(fn + len - 4, ".bmp") != 0)
            {
                strcat(fn, ".bmp");
            }

            SDL_Surface* tmp_surf = SDL_LoadBMP(fn);
            if (tmp_surf == 0) 
            {
                fprintf(stderr, "Couldn't load %s: %s\n", fn, SDL_GetError());
                return false;
            }

            SDL_PixelFormat format;
            format.palette = 0;
            format.BitsPerPixel = m_bpp;
            format.BytesPerPixel = m_bpp >> 8;
            format.Rmask = m_specific->m_rmask;
            format.Gmask = m_specific->m_gmask;
            format.Bmask = m_specific->m_bmask;
            format.Amask = m_specific->m_amask;
            format.Rshift = 0;
            format.Gshift = 0;
            format.Bshift = 0;
            format.Ashift = 0;
            format.Rloss = 0;
            format.Gloss = 0;
            format.Bloss = 0;
            format.Aloss = 0;
            format.colorkey = 0;
            format.alpha = 0;

            m_specific->m_surf_img[idx] = 
                SDL_ConvertSurface(tmp_surf, 
                                   &format, 
                                   SDL_SWSURFACE);

            SDL_FreeSurface(tmp_surf);
            
            if(m_specific->m_surf_img[idx] == 0) return false;

            m_rbuf_img[idx].attach((unsigned char*)m_specific->m_surf_img[idx]->pixels, 
                                   m_specific->m_surf_img[idx]->w, 
                                   m_specific->m_surf_img[idx]->h, 
                                   m_flip_y ? -m_specific->m_surf_img[idx]->pitch : 
                                               m_specific->m_surf_img[idx]->pitch);
            return true;

        }
        return false;
    }
Image *SDL2SoftwareImageHelper::_SDLload(SDL_Surface *tmpImage)
{
    if (!tmpImage)
        return nullptr;

    SDL_Surface *image = SDL_ConvertSurface(tmpImage, mFormat, 0);
    return new Image(image, false, nullptr);
}
示例#7
0
SDL_Surface *image_sdl_clone(const SDL_Surface *src)
{
	assert(src);
	assert(src->w > 0);
	assert(src->h > 0);

	return SDL_ConvertSurface((SDL_Surface *)src, src->format, SDL_SWSURFACE);
}
示例#8
0
void loginWindowUpdate(const string &username, int pwdLen){
	if(loginSelectTexture[0] == 0) loginSelectTexture[0] = loadImg("res/loginsel0.png");
	if(loginSelectTexture[1] == 0) loginSelectTexture[1] = loadImg("res/loginsel1.png");
	if(loginSelectTexture[2] == 0) loginSelectTexture[2] = loadImg("res/loginsel2.png");
		
	if(loginSurface == NULL){
		loginSurface = IMG_Load("res/login.png");
	
		SDL_Color white, black;
		white.r = white.g = white.b = 0xFF;
		black.r = black.g = black.b = 0x00;
		
		SDL_Surface *titleText = TTF_RenderUTF8_Blended(font, "Login", white);
		SDL_Rect rTitle = {6,5,0,0};	
		SDL_BlitSurface(titleText, NULL, loginSurface, &rTitle);
		SDL_FreeSurface(titleText);	

		SDL_Surface *userLblText = TTF_RenderUTF8_Blended(font, "Usuario:", black);
		SDL_Rect rUserLbl = {8,35,0,0};
		SDL_BlitSurface(userLblText, NULL, loginSurface, &rUserLbl);
		SDL_FreeSurface(userLblText);		
		
		SDL_Surface *passLblText = TTF_RenderUTF8_Blended(font, "Password:", black);
		SDL_Rect rPassLbl = {8,67,0,0};
		SDL_BlitSurface(passLblText, NULL, loginSurface, &rPassLbl);
		SDL_FreeSurface(passLblText);		
		
		SDL_Surface *buttonText = TTF_RenderUTF8_Blended(font, "Aceptar", black);
		SDL_Rect rButton = {128,107,0,0};
		rButton.x -= buttonText->w/2;
		rButton.y -= buttonText->h/2;
		SDL_BlitSurface(buttonText, NULL, loginSurface, &rButton);
		SDL_FreeSurface(buttonText);			
		
		loginTexture = genTexture(loginSurface, loginTexture);	
	}
	
	SDL_Color black;
	black.r = black.g = black.b = 0x00;
	
	SDL_Surface *tempSurface = SDL_ConvertSurface(loginSurface, loginSurface->format, loginSurface->flags);
	
	SDL_Surface *userNameText = TTF_RenderUTF8_Blended(font, username.c_str(), black);
	SDL_Rect rUserName = {91,35,0,0};
	SDL_BlitSurface(userNameText, NULL, tempSurface, &rUserName);
	SDL_FreeSurface(userNameText);
	
	string tempPass;
	for(int i=0;i<pwdLen;i++) tempPass += '*';
	
	SDL_Surface *passWordText = TTF_RenderUTF8_Blended(font, tempPass.c_str(), black);
	SDL_Rect rPassWord = {91,67,0,0};
	SDL_BlitSurface(passWordText, NULL, tempSurface, &rPassWord);
	SDL_FreeSurface(passWordText);	
	
	loginTexture = genTexture(tempSurface, loginTexture);	
	SDL_FreeSurface(tempSurface);
}
示例#9
0
bool Texture::CreateFromSurface(SDL_Surface *s, bool forceRGBA)
{
	bool freeSurface = false;

	SDL_PixelFormat *pixfmt = s->format;

	GLenum targetGLformat;
	SDL_PixelFormat *targetPixfmt;
	bool needConvert = !GetTargetFormat(pixfmt, &targetGLformat, &targetPixfmt, forceRGBA);

	if (needConvert) {
		s = SDL_ConvertSurface(s, targetPixfmt, SDL_SWSURFACE);
		freeSurface = true;
	}

	// store incoming 24-bit as GL_RGB to save on texture memory
	if (targetGLformat == GL_RGB && m_format.internalFormat == GL_RGBA) {
		m_format.internalFormat = GL_RGB;
		m_format.dataFormat = GL_RGB;
	}

	unsigned int width = s->w;
	unsigned int height = s->h;

	// extend to power-of-two if necessary
	int width2 = ceil_pow2(s->w);
	int height2 = ceil_pow2(s->h);
	if (s->w != width2 || s->h != height2) {
		SDL_Surface *s2 = SDL_CreateRGBSurface(SDL_SWSURFACE, width2, height2, targetPixfmt->BitsPerPixel, targetPixfmt->Rmask, targetPixfmt->Gmask, targetPixfmt->Bmask, targetPixfmt->Amask);

		SDL_SetAlpha(s, 0, 0);
		SDL_SetAlpha(s2, 0, 0);
		SDL_BlitSurface(s, 0, s2, 0);

		if (freeSurface)
			SDL_FreeSurface(s);

		s = s2;
		freeSurface = true;

		m_texWidth = float(width) / float(width2);
		m_texHeight = float(height) / float(height2);
	}
	else
		m_texWidth = m_texHeight = 1.0f;

	SDL_LockSurface(s);
	CreateFromArray(s->pixels, s->w, s->h);
	SDL_UnlockSurface(s);

	m_width = width;
	m_height = height;

	if (freeSurface)
		SDL_FreeSurface(s);

	return true;
}
示例#10
0
	void ImageLoader::load(IResource* res) {
		VFS* vfs = VFS::instance();

		Image* img = dynamic_cast<Image*>(res);

		//Have to save the images x and y shift or it gets lost when it's
		//loaded again.
		int32_t xShiftSave = img->getXShift();
		int32_t yShiftSave = img->getYShift();

		if(!img->isSharedImage()) {
			const std::string& filename = img->getName();
			boost::scoped_ptr<RawData> data (vfs->open(filename));
			size_t datalen = data->getDataLength();
			boost::scoped_array<uint8_t> darray(new uint8_t[datalen]);
			data->readInto(darray.get(), datalen);
			SDL_RWops* rwops = SDL_RWFromConstMem(darray.get(), static_cast<int>(datalen));

			SDL_Surface* surface = IMG_Load_RW(rwops, false);

			if (!surface) {
				throw SDLException(std::string("Fatal Error when loading image into a SDL_Surface: ") + SDL_GetError());
			}

			RenderBackend* rb = RenderBackend::instance();
			// in case of SDL we don't need to convert the surface
			if (rb->getName() == "SDL") {
				img->setSurface(surface);
			// in case of OpenGL we need a 32bit surface
			} else {
				SDL_PixelFormat dst_format = rb->getPixelFormat();
				SDL_PixelFormat src_format = *surface->format;
				uint8_t dstbits = dst_format.BitsPerPixel;
				uint8_t srcbits = src_format.BitsPerPixel;

				if (srcbits != 32 || dst_format.Rmask != src_format.Rmask || dst_format.Gmask != src_format.Gmask ||
					dst_format.Bmask != src_format.Bmask || dst_format.Amask != src_format.Amask) {
					dst_format.BitsPerPixel = 32;
					SDL_Surface* conv = SDL_ConvertSurface(surface, &dst_format, 0);
					dst_format.BitsPerPixel = dstbits;

					if (!conv) {
						throw SDLException(std::string("Fatal Error when converting surface to the screen format: ") + SDL_GetError());
					}

					img->setSurface(conv);
					SDL_FreeSurface(surface);
				} else {
					img->setSurface(surface);
				}
			}

			SDL_FreeRW(rwops);
		}
		//restore saved x and y shifts
		img->setXShift(xShiftSave);
		img->setYShift(yShiftSave);
	}
示例#11
0
文件: Game.cpp 项目: Qata/TopDoom
int Game::run()
{
	int retVal = 0;

	if (msurf_Screen)
	{
		if (!msurf_MapSurface)
		{
			msurf_MapSurface = SDL_ConvertSurface(msurf_Screen, msurf_Screen->format, msurf_Screen->flags);
		}

		if (msurf_MapSurface->w != Graphics::getScreenWidth() || msurf_MapSurface->h != Graphics::getScreenHeight())
		{
			if (msurf_MapSurface)
			{
				Graphics::free(msurf_MapSurface);
			}

			msurf_MapSurface = Graphics::zoom(msurf_Screen, Graphics::getScreenWidth(), Graphics::getScreenHeight(), true);
		}
	}

	SDL_Surface * screen = SDL_ConvertSurface(msurf_MapSurface, msurf_MapSurface->format, msurf_MapSurface->flags);

	Sound::resumeMusic();

	if (!Sound::musicPlaying())
	{
		//Sound::playMusic("GreenPillarAmbience.wav");
	}

	player->move(1, map->width(), map->height(), keyboard);
	
	player->draw(screen);
	Graphics::blit(screen);
	Graphics::free(screen);

	if (Input::keyHeld(SDLK_ESCAPE))
	{
		Sound::pauseMusic();
		retVal = MENU;
	}

	return retVal;
}
示例#12
0
/* use default 8x8 font */
GUI_Font::GUI_Font()
{
  SDL_Surface *temp=GUI_DefaultFont();
  fontStore=SDL_ConvertSurface(temp,temp->format,SDL_SWSURFACE);
  charh = fontStore->h/16;
  charw = fontStore->w/16;
  freefont=1;
  SetTransparency(1);
}
示例#13
0
Texture::Texture (const Texture & rhs) throw (std::runtime_error) {
	Name = rhs.Name;
	if (rhs.Loaded) {
		Image = SDL_ConvertSurface(rhs.Image, rhs.Image->format, rhs.Image->flags);
	} else {
		Image = NULL;
	}
	Loaded = rhs.Loaded;
}
SDL_Surface *slideshow::convert_to_true_color(SDL_Surface *in)
{
	static const Uint32 surface_convert_mask 
		= SDL_SWSURFACE | SDL_HWSURFACE | SDL_SRCCOLORKEY | SDL_SRCALPHA;

	Uint32 flags = in->flags & surface_convert_mask;

	return SDL_ConvertSurface(in, &m_true_color_format, surface_convert_mask);
}
示例#15
0
Surface::Surface(SDL_Surface *s, SDL_PixelFormat *fmt, Uint32 flags) {
	dblbuffer = NULL;
	this->operator =(s);
	if (fmt!=NULL || flags!=0) {
		if (fmt==NULL) fmt = s->format;
		if (flags==0) flags = s->flags;
		raw = SDL_ConvertSurface( s, fmt, flags );
	}
}
示例#16
0
SDL_Surface *
SDL_DisplayFormatAlpha(SDL_Surface * surface)
{
    SDL_PixelFormat *vf;
    SDL_PixelFormat *format;
    SDL_Surface *converted;
    /* default to ARGB8888 */
    Uint32 amask = 0xff000000;
    Uint32 rmask = 0x00ff0000;
    Uint32 gmask = 0x0000ff00;
    Uint32 bmask = 0x000000ff;

    if (!SDL_PublicSurface) {
        SDL_SetError("No video mode has been set");
        return NULL;
    }
    vf = SDL_PublicSurface->format;

    switch (vf->BytesPerPixel) {
    case 2:
        /* For XGY5[56]5, use, AXGY8888, where {X, Y} = {R, B}.
           For anything else (like ARGB4444) it doesn't matter
           since we have no special code for it anyway */
        if ((vf->Rmask == 0x1f) &&
            (vf->Bmask == 0xf800 || vf->Bmask == 0x7c00)) {
            rmask = 0xff;
            bmask = 0xff0000;
        }
        break;

    case 3:
    case 4:
        /* Keep the video format, as long as the high 8 bits are
           unused or alpha */
        if ((vf->Rmask == 0xff) && (vf->Bmask == 0xff0000)) {
            rmask = 0xff;
            bmask = 0xff0000;
        }
        break;

    default:
        /* We have no other optimised formats right now. When/if a new
           optimised alpha format is written, add the converter here */
        break;
    }
    format = SDL_AllocFormat(SDL_MasksToPixelFormatEnum(32, rmask,
                                                            gmask,
                                                            bmask,
                                                            amask));
    if (!format) {
        return NULL;
    }
    converted = SDL_ConvertSurface(surface, format, SDL_RLEACCEL);
    SDL_FreeFormat(format);
    return converted;
}
示例#17
0
void
Surface::ConvertSurface (const Surface & s, Uint32 flag)
{
    SDL_Surface *tmp = NULL;

    tmp = SDL_ConvertSurface (surface, s.surface->format, flag);
    SDL_FreeSurface (surface);
    surface = tmp;
    validPtr (surface);
}
示例#18
0
	void Widget::setSurface(SDL_Surface* source)
	{
		if(source != NULL)
		{
			widgetSurface = SDL_ConvertSurface(source, source->format, source->flags);
			SDL_FreeSurface(source);
		}
		else
			widgetSurface = NULL;
	}
示例#19
0
文件: main.c 项目: abatilo/sdl_play
SDL_Surface *loadSurface(const char *path) {
  SDL_Surface *loadedSurface = IMG_Load(path);
  SDL_Surface *optimizedSurface = NULL;
  if (loadedSurface == NULL) {
    printf("Failed to load surface.\nSDL Error: %s\n", SDL_GetError());
  }
  optimizedSurface = SDL_ConvertSurface(loadedSurface, surface->format, 0);
  SDL_FreeSurface(loadedSurface);
  return optimizedSurface;
}
示例#20
0
SDL_Surface* CSurface::OnLoad(const char* File, const SDL_PixelFormat* fmt) {
	SDL_Surface* tmpSurf = IMG_Load(File);
	SDL_Surface* retSurf = tmpSurf;
	if (tmpSurf && fmt) {
		retSurf = SDL_ConvertSurface(tmpSurf, fmt, 0);
		SDL_FreeSurface(tmpSurf);
	}

	return retSurf;
}
/**
 * Return new cloned surface.
 * @return new surface, free it after use
 * @throws SDLException when function fails
 */
SDL_Surface *
SurfaceTool::createClone(SDL_Surface *surface)
{
    SDL_Surface *clone = SDL_ConvertSurface(surface,
                                            surface->format, surface->flags);
    if (NULL == clone) {
        throw SDLException(ExInfo("ConvertSurface"));
    }
    return clone;
}
示例#22
0
GameEntity::GameEntity(std::string imageFile, SDL_Surface *screen) {
	SDL_Surface *surface_raw = SDL_LoadBMP(imageFile.c_str());
	surface = SDL_ConvertSurface(surface_raw,screen->format,0);
	SDL_FreeSurface(surface_raw);
	surface_raw = nullptr;
	rect.w = surface->w*2;
	rect.h = surface->h*2;
	rect.x = 0;
	rect.y = 0;
}
示例#23
0
文件: GUI_font.cpp 项目: Zabeus/nuvie
/* copy constructor */
GUI_Font::GUI_Font(GUI_Font& font)
{
  SDL_Surface *temp=font.fontStore;
  fontStore=SDL_ConvertSurface(temp,temp->format,SDL_SWSURFACE);
  charh = fontStore->h/16;
  charw = fontStore->w/16;
  freefont=1;
  SetTransparency(1);
  w_data = NULL;
}
示例#24
0
Surf::Surf()
{
	ScreenSurface = 0;
	ImageSurface = 0;
	GolfImageSurface = 0;
	InitPlayerSurface = 0;
	MeterSurface = 0;
	BirdSurface = 0;
	GolferSurface = 0;
	ValueSurface = 0;
	WindSurface = 0;
	FontSurface = 0;
	HighScoreSurface = 0;

	
	ScreenSurface = SDL_SetVideoMode(800,600,32,SDL_SWSURFACE |SDL_DOUBLEBUF  );
	

	if(! ScreenSurface)
	{
		cerr << "Error: SetVideoMode failed" << endl;		
		SDL_Quit();
		
	}
	

	//image = SDL_LoadBMP("gfx/allt.bmp");
	//golfimage = SDL_LoadBMP("gfx/golfallt.bmp");
	//initPlayer = SDL_LoadBMP("gfx/introbild.bmp");
	//matare = SDL_LoadBMP("gfx/matare.bmp");	
	//bird = SDL_LoadBMP("gfx/bird.bmp");
	//golfer = SDL_LoadBMP("gfx/golfer.bmp");
	//varde = SDL_LoadBMP("gfx/varde.bmp");
	//vind = SDL_LoadBMP("gfx/vind.bmp");

	ImageSurface = SDL_LoadBMP("gfx/allt.bmp");
	GolfImageSurface = SDL_LoadBMP("gfx/golfallt.bmp");
	InitPlayerSurface = SDL_LoadBMP("gfx/introbild.bmp");
	MeterSurface = SDL_LoadBMP("gfx/matare.bmp");	
	BirdSurface = SDL_LoadBMP("gfx/bird.bmp");
	GolferSurface = SDL_LoadBMP("gfx/golfer.bmp");
	ValueSurface = SDL_LoadBMP("gfx/varde.bmp");
	WindSurface = SDL_LoadBMP("gfx/vind.bmp");
	
	SDL_Surface *temp = SDL_LoadBMP("gfx/font.bmp");
	surf.FontSurface = SDL_ConvertSurface(temp, surf.ScreenSurface->format, SDL_SWSURFACE);
	SDL_FreeSurface(temp);

	/*
	if(!image || !golfimage || !initPlayer || !matare || !bird || !golfer || !varde || !vind)
	{
		cerr << "Error";		
		SDL_Quit();
	}*/
}
示例#25
0
void TextureBuilder::PrepareSurface()
{
	if (m_prepared) return;

	if (!m_surface && m_filename.length() > 0)
		LoadSurface();

	TextureFormat targetTextureFormat;
	SDL_PixelFormat *targetPixelFormat;
	bool needConvert = !GetTargetFormat(m_surface->format, &targetTextureFormat, &targetPixelFormat, m_forceRGBA);

	if (needConvert) {
		SDL_Surface *s = SDL_ConvertSurface(m_surface, targetPixelFormat, SDL_SWSURFACE);
		SDL_FreeSurface(m_surface);
		m_surface = s;
	}

	unsigned int virtualWidth, actualWidth, virtualHeight, actualHeight;
	virtualWidth = actualWidth = m_surface->w;
	virtualHeight = actualHeight = m_surface->h;

	if (m_potExtend) {
		// extend to power-of-two if necessary
		actualWidth = ceil_pow2(m_surface->w);
		actualHeight = ceil_pow2(m_surface->h);
		if (actualWidth != virtualWidth || actualHeight != virtualHeight) {
			SDL_Surface *s = SDL_CreateRGBSurface(SDL_SWSURFACE, actualWidth, actualHeight, targetPixelFormat->BitsPerPixel,
				targetPixelFormat->Rmask, targetPixelFormat->Gmask, targetPixelFormat->Bmask, targetPixelFormat->Amask);

			SDL_SetAlpha(m_surface, 0, 0);
			SDL_SetAlpha(s, 0, 0);
			SDL_BlitSurface(m_surface, 0, s, 0);

			SDL_FreeSurface(m_surface);
			m_surface = s;
		}
	}

	else if (m_filename.length() > 0) {
		// power-of-to check
		unsigned long width = ceil_pow2(m_surface->w);
		unsigned long height = ceil_pow2(m_surface->h);

		if (width != virtualWidth || height != virtualHeight)
			fprintf(stderr, "WARNING: texture '%s' is not power-of-two and may not display correctly\n", m_filename.c_str());
	}

	m_descriptor = TextureDescriptor(
		targetTextureFormat,
		vector2f(actualWidth,actualHeight),
		vector2f(float(virtualWidth)/float(actualWidth),float(virtualHeight)/float(actualHeight)),
		m_sampleMode, m_generateMipmaps);
	
	m_prepared = true;
}
示例#26
0
/**
 * @brief Copy constructor
 *
 * @param matrix
 */
Matrix::Matrix(const Matrix& matrix)
{
    pthread_mutex_lock((pthread_mutex_t*)&(matrix.m_mutex));
    SDL_UnlockSurface(matrix.m_surface);
    m_surface = SDL_ConvertSurface(matrix.m_surface, matrix.m_surface->format, SDL_SWSURFACE);
    SDL_LockSurface(matrix.m_surface);
    pthread_mutex_unlock((pthread_mutex_t*)&(matrix.m_mutex));

    SDL_LockSurface(m_surface);
    pthread_mutex_init(&m_mutex, NULL);
}
CFinaleStaticScene::CFinaleStaticScene(const std::string &game_path, const std::string &scene_file):
m_mustclose(false),
m_timer(0)
{
    mpSceneSurface.reset(SDL_CreateRGBSurface( 0, 320, 200, 8, 0, 0, 0, 0),
                         &SDL_FreeSurface);

#if SDL_VERSION_ATLEAST(2, 0, 0)
    SDL_SetPaletteColors(mpSceneSurface->format->palette, g_pGfxEngine->Palette.m_Palette, 0, 255);
    SDL_SetColorKey(mpSceneSurface.get(), SDL_TRUE, COLORKEY);
#else
    SDL_SetColors(mpSceneSurface.get(), g_pGfxEngine->Palette.m_Palette, 0, 255);
    SDL_SetColorKey(mpSceneSurface.get(), SDL_SRCCOLORKEY, COLORKEY);
#endif

    if( finale_draw( mpSceneSurface.get(), scene_file, game_path) )
	{
#if SDL_VERSION_ATLEAST(2, 0, 0)
        
#else
        mpSceneSurface.reset(g_pVideoDriver->convertThroughBlitSfc(mpSceneSurface.get()), &SDL_FreeSurface);
#endif

        SDL_Surface *blit = g_pVideoDriver->getBlitSurface();

        std::shared_ptr<SDL_Surface> scaledScene(
                    SDL_ConvertSurface(blit, blit->format, 0),
                    &SDL_FreeSurface);


        SDL_Rect srcRect, dstRect;

        srcRect.x = 0;  srcRect.y = 0;
        srcRect.w = mpSceneSurface->w;
        srcRect.h = mpSceneSurface->h;

        dstRect.x = 0;  dstRect.y = 0;
        dstRect.w = scaledScene->w;
        dstRect.h = scaledScene->h;

        SDL_FillRect(scaledScene.get(), nullptr, SDL_MapRGB(scaledScene->format, 0, 0, 0) );

        SDL_BlitScaledWrap(mpSceneSurface.get(),
                           &srcRect,
                           scaledScene.get(),
                           &dstRect);

        mpSceneSurface = scaledScene;
    }
	else
	{
		m_mustclose = true;
    }
}
static void BrushLoadGuideImage(void *data, int d)
{
	UNUSED(d);
	EditorBrush *b = data;
	b->IsGuideImageNew = true;
	SDL_FreeSurface(b->GuideImageSurface);
	SDL_Surface *s = IMG_Load(b->GuideImage);
	if (s == NULL) return;
	b->GuideImageSurface = SDL_ConvertSurface(s, gGraphicsDevice.Format, 0);
	SDL_FreeSurface(s);
}
示例#29
0
// Horizontal flip
SDL_Surface * CSDL_Ext::hFlip(SDL_Surface * toRot)
{
	SDL_Surface * ret = SDL_ConvertSurface(toRot, toRot->format, toRot->flags);
	int bpl = ret->pitch;

	for(int i=0; i<ret->h; i++) {
		memcpy((char *)ret->pixels + i*bpl, (char *)toRot->pixels + (ret->h-i-1)*bpl, bpl);
	}

	return ret;
};
/**
 * \brief Loads a texture from an image file and fits into a GL-compatible size.
 * \param fileName The name of the image file.
 * \param filtering True for linear filtering, false for nearest-neighbor.
 * \param realW Returns the real width of the texture.
 * \param realH Returns the real height of the texture.
 * \return The OpenGL texture ID.
 */
long SDLGL_LoadTextureFromFileBestFit( std::string fileName, bool filtering, unsigned long &realW, unsigned long &realH ) {
  GLuint theTexture;
  SDL_Surface *loadSurface, *theSurface, *convertedSurface;
  Uint32 rmask, gmask, bmask, amask;

  loadSurface = IMG_Load( fileName.c_str() );

  if ( loadSurface ) {
    #if SDL_BYTEORDER == SDL_BIG_ENDIAN
      rmask = 0xff000000;
      gmask = 0x00ff0000;
      bmask = 0x0000ff00;
      amask = 0x000000ff;
    #else
      rmask = 0x000000ff;
      gmask = 0x0000ff00;
      bmask = 0x00ff0000;
      amask = 0xff000000;
    #endif
    theSurface = SDL_CreateRGBSurface( SDL_SWSURFACE | SDL_SRCALPHA, NextPowerOfTwo(loadSurface->w), NextPowerOfTwo(loadSurface->h), 32, rmask, gmask, bmask, amask );
    SDL_FillRect( theSurface, NULL, SDL_MapRGBA( theSurface->format, 0,0,0,255 ) );
    convertedSurface = SDL_ConvertSurface( loadSurface, theSurface->format, SDL_SWSURFACE | SDL_SRCALPHA );

    MoveTexture( convertedSurface, theSurface );
  }
  else
    theSurface = NULL;

  if ( theSurface ) {
    glGenTextures( 1, &theTexture);

    glBindTexture( GL_TEXTURE_2D, theTexture );

    if ( filtering ) {
      glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR );
      glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR );
    } else {
      glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST );
      glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST );
    }

    realW = loadSurface->w;
    realH = loadSurface->h;
    glTexImage2D( GL_TEXTURE_2D, 0, 4, theSurface->w, theSurface->h, 0, GL_RGBA, GL_UNSIGNED_BYTE, theSurface->pixels );

    SDL_FreeSurface( theSurface );
    SDL_FreeSurface( loadSurface );
    SDL_FreeSurface( convertedSurface );

    return theTexture;
  }
  else
    return 0; // GLspeak for "no texture"
}