Пример #1
0
bool CSurface::ResizeSprite(SDL_Surface* image, SDL_Rect* clip, bool maintainRatio)
{
	double scaleX, scaleY;
	if (m_SpriteImage) SDL_FreeSurface(m_SpriteImage);		// free old image
	m_SpriteImage = 0;
	

	if (maintainRatio == true)
	{

		if (clip->w - image->w <= clip->h - image->h)
			scaleX = scaleY = ((double)clip->w / (double)image->w);
		else	// assume the height is larger
			scaleX = scaleY = ((double)clip->h / (double)image->h);
		m_SpriteImage = zoomSurface(image, scaleX, scaleY, 1);
	}
	else
	{
		if (clip->w != image->w || clip->h != image->h)
		{
			scaleX = ((double)clip->w / (double)image->w);
			scaleY = ((double)clip->h / (double)image->h);
			m_SpriteImage = zoomSurface(image, scaleX, scaleY, 1);
		}
	}

	return true;
}
void AccuracyTest2(SDL_Surface *screen, SDL_Surface *picture)
{
	SDL_Surface *zoomed1, *zoomed2, *zoomed3, *zoomed4;
	int factor;
	int neww, newh;
	SDL_Rect target;

	printf("%s\n", messageText);
	for (factor = 2; factor < 64; factor += 1)
	{		
		HandleEvent();
		ClearScreen(screen);

		stringRGBA(screen, 8, 8, messageText, 255, 255, 255, 255);

		neww = picture->w * factor;
		newh = picture->h * factor;
		printf ("  zoom %ix%i to %ix%i\n", picture->w, picture->h, neww, newh);

		zoomed1 = zoomSurface(picture,  (float)factor,  (float)factor, 0);
		zoomed2 = zoomSurface(picture,  (float)factor,  (float)factor, 1);
		zoomed3 = zoomSurface(picture,  (float)factor, -(float)factor, 1);
		zoomed4 = zoomSurface(picture, -(float)factor,  (float)factor, 1);

		target.x = screen->w/2 - zoomed1->w;
		target.y = screen->h/2 - zoomed1->h;
		target.w = zoomed1->w;
		target.h = zoomed1->h;
		SDL_BlitSurface(zoomed1, 0, screen, &target);
		target.x = screen->w/2;
		target.y = screen->h/2;
		SDL_BlitSurface(zoomed2, 0, screen, &target);
		target.x = screen->w/2 - zoomed3->w;
		target.y = screen->h/2;
		SDL_BlitSurface(zoomed4, 0, screen, &target);
		target.x = screen->w/2;
		target.y = screen->h/2 - zoomed4->h;
		SDL_BlitSurface(zoomed3, 0, screen, &target);

		SDL_FreeSurface(zoomed1);
		SDL_FreeSurface(zoomed2);
		SDL_FreeSurface(zoomed3);
		SDL_FreeSurface(zoomed4);

		/* Display by flipping screens */
		SDL_Flip(screen);

		/* Always delay */
		SDL_Delay(250);

		/* Maybe add extra delay */
		if (delay>0) {
			SDL_Delay(delay);
		}
	}

	SDL_Delay(1000);
}
Пример #3
0
SDL_Surface* ResourceCache::LoadTexture(
   const std::string& file,
   const int width,
   const int height
)
{
   const auto full_path = mResDir + "/" + file;

   SDL_Surface* img_loaded = IMG_Load(full_path.c_str());
   if (!img_loaded) {
      throw "Failed to load texture";
   }

   SDL_Surface* img_compat = SDL_DisplayFormat(img_loaded);
   if (!img_compat) {
      throw "Failed to convert animation frame to display format";
   }
   SDL_FreeSurface(img_loaded);
   img_loaded = nullptr;

   const auto x_zoom = static_cast<double>(width) / img_compat->w;
   const auto y_zoom = static_cast<double>(height) / img_compat->h;
   const auto img_zoomed = zoomSurface(img_compat, x_zoom, y_zoom, 1);
   SDL_FreeSurface(img_compat);
   img_compat = nullptr;

   const auto colorkey = SDL_MapRGB(img_zoomed->format, 0xff, 0, 0xff);
   if (SDL_SetColorKey(img_zoomed, SDL_RLEACCEL | SDL_SRCCOLORKEY, colorkey)) {
      throw "SDL_SetColorKey failed";
   }

   mSurfaceCache.push_back(img_zoomed);
   return img_zoomed;
}
void DisplayScreen::display_screen(IntMatrix& screen_matrix, int screen_width, int screen_height) {
    Uint32 rmask, gmask, bmask, amask;
#if SDL_BYTEORDER == SDL_BIG_ENDIAN
    rmask = 0xff000000;
    gmask = 0x00ff0000;
    bmask = 0x0000ff00;
    amask = 0x000000ff;
#else
    rmask = 0x000000ff;
    gmask = 0x0000ff00;
    bmask = 0x00ff0000;
    amask = 0xff000000;
#endif

    SDL_Surface* my_surface = SDL_CreateRGBSurface(SDL_SWSURFACE,screen_width,screen_height,32,rmask,gmask,bmask,amask);

    int r, g, b;
    for (int y=0; y<screen_height; ++y) {
        for (int x=0; x<screen_width; ++x) {
            export_screen->get_rgb_from_palette(screen_matrix[y][x], r, g, b);
            pixelRGBA(my_surface,x,y,r,g,b,255);
        }
    }

    SDL_Surface* zoomed = zoomSurface(my_surface,screen->w/(double)screen_width,screen->h/(double)screen_height,0);
    SDL_BlitSurface(zoomed, NULL, screen, NULL);

    SDL_Flip(screen);
    SDL_FreeSurface(my_surface);
    SDL_FreeSurface(zoomed);
    poll(); // Check for quit event
}
Пример #5
0
CAMLprim value ml_zoomSurface(value src,value zoomx,value zoomy,value smooth)
{
  SDL_Surface *ssur= SDL_SURFACE(src);
  SDL_Surface *dsur;
  dsur=zoomSurface(ssur,Double_val(zoomx),Double_val(zoomy),Bool_val(smooth));
  return ML_SURFACE(dsur);
}
Пример #6
0
void RefreshScreen(SDL_Surface* tmp)
{
	SDL_Surface* doble;
	doble = zoomSurface(tmp,screen_scale.w_scale,screen_scale.h_scale,0);
	SDL_BlitSurface(doble,NULL,real_screen,&screen_position);
	SDL_Flip(real_screen);
	SDL_FreeSurface(doble);
}
Пример #7
0
void DrawTurnGraph(int x, int y,
			int graphid, int nouse) // 画面左右反转
{
	SDL_Surface *graph = (SDL_Surface*)graphid;
	SDL_Surface *graph2 = zoomSurface(graph, 
							-1.0, 1.0, false);
	DrawGraph(x, y, (int)graph2, 0);
	SDL_FreeSurface(graph2);
}
Пример #8
0
 bool Texture::loadFromFile(const std::string& path, SDL_Renderer*& renderer, const double zoomX, const double zoomY)
 {
     destroy();
     
     SDL_Texture* newTexture = nullptr;
     SDL_Surface* loadedSurface = IMG_Load( path.c_str() );
     if( loadedSurface == nullptr )
     {
         SDL_LogMessage(SDL_LOG_CATEGORY_VIDEO, SDL_LOG_PRIORITY_ERROR, "Unable to load image %s! SDL_image Error: %s\n", path.c_str(), IMG_GetError());
     }
     else
     {
         if (zoomX != 1 && zoomY != 1)
         {
             SDL_Surface* zoomedSurface = zoomSurface( loadedSurface, zoomX, zoomY, SMOOTHING_OFF );
             if (zoomedSurface != nullptr)
             {
                 SDL_FreeSurface( loadedSurface );
                 loadedSurface = nullptr;
                 loadedSurface = zoomedSurface;
             }
             else
             {
                 SDL_LogMessage(SDL_LOG_CATEGORY_VIDEO, SDL_LOG_PRIORITY_WARN, "Unable to resize image %s! SDL_image Error: %s\n", path.c_str(), IMG_GetError());
             }
         }
         
         if (SDL_SetColorKey( loadedSurface, SDL_TRUE, SDL_MapRGB( loadedSurface->format, transparentColor.r, transparentColor.g, transparentColor.b ) ) != 0)
         {
             SDL_LogMessage(SDL_LOG_CATEGORY_VIDEO, SDL_LOG_PRIORITY_WARN, "Unable to set transparent color.");
         }
         
         newTexture = SDL_CreateTextureFromSurface( renderer, loadedSurface );
         if( newTexture == nullptr )
         {
             SDL_LogMessage(SDL_LOG_CATEGORY_VIDEO, SDL_LOG_PRIORITY_ERROR, "Unable to create texture from %s! SDL Error: %s\n",
                            path.c_str(), SDL_GetError() );
         }
         else
         {
             pixelWidth = loadedSurface->w;
             pixelHeight = loadedSurface->h;
         }
         
         SDL_FreeSurface( loadedSurface );
         loadedSurface = nullptr;
     }
     
     texture = newTexture;
     return texture != NULL;
 }
Пример #9
0
int main(int argc, char **argv) {
    SDL_Init(SDL_INIT_VIDEO);

    const int width = 400;
    const int height = 200 * (numSprites + 1) / 2;
    screen = SDL_SetVideoMode(width, height, 32, SDL_SWSURFACE);
    SDL_Rect rect = { 0, 0, width, height };
    SDL_FillRect(screen, &rect, SDL_MapRGBA(screen->format, 0, 0, 0, 0xff));

    sprite[0] = IMG_Load("screenshot.png");
    sprite[1] = SDL_CreateRGBSurface(SDL_SWSURFACE, 100, 100, 32, 0xFF000000, 0xFF0000, 0xFF00, 0xFF);
    SDL_FillRect(sprite[1], 0, 0xA0A0A0A0);
    sprite[2] = zoomSurface(sprite[0], 0.5, 0.5, SMOOTHING_ON);
    sprite[3] = zoomSurface(sprite[1], 0.5, 0.5, SMOOTHING_ON);
    sprite[4] = rotozoomSurface(sprite[0], -20, 0.3, SMOOTHING_ON);
    sprite[5] = rotozoomSurface(sprite[1], 20, 1, SMOOTHING_ON);
    sprite[6] = zoomSurface(sprite[0], -0.5, 0.5, SMOOTHING_ON);
    sprite[7] = zoomSurface(sprite[0], -0.5, -0.5, SMOOTHING_ON);
    sprite[8] = rotozoomSurface(sprite[1], 0, 0.5, SMOOTHING_ON);

    mainloop();

#ifndef __EMSCRIPTEN__
    SDL_Event evt;
    SDL_SaveBMP(screen, "native_output.bmp");
    while (1) {
       if (SDL_PollEvent(&evt) != 0 && evt.type == SDL_QUIT) break;
       //mainloop();
       SDL_Delay(33);
    }
#endif

    SDL_Quit();

    return 1;
}
Пример #10
0
void ZoomPicture (SDL_Surface *screen, SDL_Surface *picture, int smooth) 
{
	SDL_Surface *rotozoom_picture;
	SDL_Rect dest;
	int framecount, framemax, frameinc;
	double zoomxf,zoomyf;

	fprintf(stderr, "%s\n", messageText);

	/* Zoom and display the picture */
	framemax=4*360; frameinc=1;
	for (framecount=360; framecount<framemax; framecount += frameinc) {
		if ((framecount % 360)==0) frameinc++;
		HandleEvent();
		ClearScreen(screen);
		zoomxf=(float)framecount/(float)framemax;
		zoomxf=1.5*zoomxf*zoomxf;
		zoomyf=0.5+fabs(1.0*sin((double)framecount/80.0));
		if ((framecount % 120)==0) {
			printf ("  Frame: %i   Zoom: x=%.2f y=%.2f\n",framecount,zoomxf,zoomyf);
		}
		if ((rotozoom_picture=zoomSurface (picture, zoomxf, zoomyf, smooth))!=NULL) {
			dest.x = (screen->w - rotozoom_picture->w)/2;;
			dest.y = (screen->h - rotozoom_picture->h)/2;
			dest.w = rotozoom_picture->w;
			dest.h = rotozoom_picture->h;
			if ( SDL_BlitSurface(rotozoom_picture, NULL, screen, &dest) < 0 ) {
				fprintf(stderr, "Blit failed: %s\n", SDL_GetError());
				break;
			}
			SDL_FreeSurface(rotozoom_picture);
		}

		stringRGBA(screen, 8, 8, messageText, 255, 255, 255, 255);
		
		/* Display by flipping screens */
		SDL_Flip(screen);

		/* Maybe delay */
		if (delay>0) {
			SDL_Delay(delay);
		}
	}

	/* Pause for a sec */
	SDL_Delay(1000);
}
Пример #11
0
/**
 * Renders the buffer's contents onto the screen, applying
 * any necessary filters or conversions in the process.
 * If the scaling factor is bigger than 1, the entire contents
 * of the buffer are resized by that factor (eg. 2 = doubled)
 * before being put on screen.
 */
void Screen::flip()
{
	if (getWidth() != BASE_WIDTH || getHeight() != BASE_HEIGHT)
	{
		SDL_Surface* zoom = zoomSurface(_surface->getSurface(), _scaleX, _scaleY, 0);
		SDL_BlitSurface(zoom, 0, _screen, 0);
		SDL_FreeSurface(zoom);
	}
	else
	{
		SDL_BlitSurface(_surface->getSurface(), 0, _screen, 0);
	}

	if (SDL_Flip(_screen) == -1)
    {
        throw Exception(SDL_GetError());
    }
}
void DisplayScreen::display_png(const string& filename) {
    image = IMG_Load(filename.c_str());
    if ( !image ) {
        fprintf (stderr, "IMG_Load: %s\n", IMG_GetError () );
    } 

    // Draws the image on the screen:
    SDL_Rect rcDest = { 0,0, (Uint16)(2*image->w), (Uint16)(2*image->h) };
    SDL_Surface *image2 = zoomSurface(image, 2.0, 2.0, 0);
    SDL_BlitSurface ( image2, NULL, screen, &rcDest );
    // something like SDL_UpdateRect(surface, x_pos, y_pos, image->w, image->h); is missing here

    SDL_Flip(screen);

    SDL_FreeSurface(image);
    SDL_FreeSurface(image2);
    SDL_FreeSurface(screen);
    poll(); // Check for quit event
}
Пример #13
0
int Skin::load(char *name, bool quiet) {
    char d[512];
    setpath(name);
    makepath(d,"colors.conf");
    if (!Colors.load(d)) {
        sprintf(d, "Skin[%s]: Cannot load colors.conf", name); 
        if (!quiet) MessageBox(NULL,d,"Error",MB_ICONERROR | MB_OK);
        return 0;
    }
    SDL_Surface *s;
    pngLoad("toolbar.png");
    bmToolbar = new Bitmap( s , true );
    pngLoad("load.png");
    bmLoad = new Bitmap( s , true );
    pngLoad("save.png");
    bmSave = new Bitmap( s , true );
    pngLoad("buttons.png");
    bmButtons = new Bitmap( s , true );

    pngLoad("about.png");
    bmAbout = new Bitmap( s , true );
    if (640 != CONSOLE_WIDTH && 480 != CONSOLE_HEIGHT) {

        double xscale = (double)CONSOLE_WIDTH / 640;
        double yscale = (double)CONSOLE_HEIGHT / 480;
        Drawable ss( zoomSurface(bmAbout->surface, xscale, yscale ,SMOOTHING_ON) , false );
//        S->copy(&ss,5,row(12));
        SDL_FreeSurface( bmAbout->surface );
        bmAbout->surface = ss.surface;
    }    
    pngLoad("logo.png");
    SDL_FreeSurface(s);
    bmLogo = NULL;

    makepath(d,"font.fnt");
    if (font_load(d)) {
        sprintf(d, "Skin[%s]: Cannot load font.fnt", name); 
        if (!quiet) MessageBox(NULL,d,"Error",MB_ICONERROR | MB_OK);
        return 0;
    }
    
    return 1;
}
Пример #14
0
SDL_Surface *zoomOut(SDL_Surface *image, SDL_Rect *pos, double *scale)
{
    *scale /= ZOOMSTEP;

    SDL_Surface *result = zoomSurface(image, *scale, *scale, SMOOTHING_ON);
    if (result == (SDL_Surface *) (NULL))
    {
        fprintf(stderr, "\n Error from zoomSurface()\n\n");
        quit();
    }

    pos->x += SCREENWIDTH * (1-ZOOMSTEP) * 0.5;
    pos->x /= ZOOMSTEP;
    pos->y += SCREENHEIGHT * (1-ZOOMSTEP) * 0.5;
    pos->y /= ZOOMSTEP;
    pan(result, pos, 0, 0);

    return result;
}
Пример #15
0
SDL_Surface *zoomIn(SDL_Surface *image, SDL_Rect *pos, double *scale)
{
    *scale *= ZOOMSTEP;

    SDL_Surface *result = zoomSurface(image, *scale, *scale, SMOOTHING_ON);
    if (result == (SDL_Surface *) (NULL))
    {
        fprintf(stderr, "\n Error from zoomSurface()\n\n");
        quit();
    }

    pos->x *= ZOOMSTEP;
    int dx = SCREENWIDTH * (ZOOMSTEP-1) * 0.5;
    pos->y *= ZOOMSTEP;
    int dy = SCREENHEIGHT * (ZOOMSTEP-1) * 0.5;
    pan(result, pos, dx, dy);

    return result;
}
Пример #16
0
void Meteor::draw() const { 
  if (explosion) 
  {
    explosion->draw();
    return;
  }
  if( !hasExploded )
  {
    Uint32 x = static_cast<Uint32>(X());
    Uint32 y = static_cast<Uint32>(Y());
    SDL_Surface* surf = frame->getSurface();
    surf = zoomSurface(surf, zoom, zoom, SMOOTHING_ON);
    std::string name = "meteor";
    Frame* immediate = new Frame(name, surf);
    immediate->draw(x, y);
    delete immediate;
    SDL_FreeSurface(surf);
  }
}
Пример #17
0
void scale(slide *sl)
{
	double f;
	
	if(scalep == 1)
		sl->scaled = sl->render;
	else
	{
		if(sl->scaled != NULL)
			error("Paranoia: scaled surface not freed up");
		f = (double)scalep / (double)scaleq;
		sl->scaled = zoomSurface(sl->render, f, f, 1);
		if(sl->scaled == NULL)
			error("zoomSurface returned NULL");
	}
	
	// Create zoomed out version of everything we've just drawn:
	f = 1.0 / 3.0;
	if(sl->mini != NULL)
		error("Paranoia: mini surface not freed up");
	// sl->mini = SDL_ResizeFactor(sl->scaled, (float)f, 1);
	sl->mini = zoomSurface(sl->scaled, f, f, 1);
	if(sl->mini == NULL)
		error("zoomSurface returned NULL");
	
	if(sl->deck_size > 1)
	{
		if(sl->decor.top != NULL)
			sl->mini_decor.top = zoomSurface(sl->decor.top, f, f, 1);
		if(sl->decor.bottom != NULL)
			sl->mini_decor.bottom = zoomSurface(sl->decor.bottom, f, f, 1);
		if(sl->decor.left != NULL)
			sl->mini_decor.left = zoomSurface(sl->decor.left, f, f, 1);
		if(sl->decor.right != NULL)
			sl->mini_decor.right = zoomSurface(sl->decor.right, f, f, 1);
	}
	
	if(sl->micro != NULL)
		error("Paranoia: micro surface not freed up");
	sl->micro = zoomSurface(sl->mini, f, f, 1);
	if(sl->micro == NULL)
		error("zoomSurface returned NULL");
}
Пример #18
0
SDL_Surface *zoomFit(SDL_Surface *image, SDL_Rect *pos, double *scale)
{
    pos->x = 0;
    pos->y = 0;
    double scale_x = (double) (SCREENWIDTH) / (double) (image->w);
    double scale_y = (double) (SCREENHEIGHT) / (double) (image->h);
    if (scale_y < scale_x) {
        *scale = scale_y;
    } else {
        *scale = scale_x;
    }

    SDL_Surface *result = zoomSurface(image, *scale, *scale, SMOOTHING_ON);
    if (result == (SDL_Surface *) (NULL))
    {
        fprintf(stderr, "\n Error from zoomSurface()\n\n");
        quit();
    }

    return result;
}
Пример #19
0
void parseXEventsNonBlocking()
{
	XEvent e;
	XButtonEvent *peB;

	while (XPending(g_x11Display))
	{
		XNextEvent(g_x11Display, &e);
		switch (e.type)
		{
			case ButtonPress:
				peB = (XButtonEvent *) &e;
				// Make something visible to the end user
				if (peB->button == 2)  // Middle Click
				{
					moveSurface();
				}
				else if (peB->button == 1)  // Left Click
				{
					zoomSurface(peB->x, peB->y);
				}
				else if (peB->button == 3)  // Right Click
				{
					unzoomSurface();
				}
				break;

			case KeyPress:
				std::cout << "Key press : " << ((XKeyEvent *) &e)->keycode << std::endl;
				break;

			case KeyRelease:
				std::cout << "Key release : " << ((XKeyEvent *) &e)->keycode << std::endl;
				break;

			default:
				break;
		}
	}
}
Пример #20
0
static PyObject*
_gfx_zoom (PyObject *self, PyObject *args)
{
    PyObject *surface, *retval, *aa = NULL;
    SDL_Surface *orig, *result;
    double zoomx, zoomy;
    int smooth = 0;

    if (!PyArg_ParseTuple (args, "Odd|O:zoom", &surface, &zoomx, &zoomy, &aa))
        return NULL;

    if (!PySDLSurface_Check (surface))
    {
        PyErr_SetString (PyExc_TypeError, "surface must be a Surface");
        return NULL;
    }
    orig = ((PySDLSurface*)surface)->surface;

    if (aa)
    {
        smooth = PyObject_IsTrue (aa);
        if (smooth == -1)
            return NULL;
    }
    
    result = zoomSurface (orig, zoomx, zoomy, smooth);
    if (!result)
    {
        PyErr_SetString (PyExc_PyGameError, SDL_GetError ());
        return NULL;
    }
    
    retval = PySDLSurface_NewFromSDLSurface (result);
    if (!retval)
    {
        SDL_FreeSurface (result);
        return NULL;
    }
    return retval;
}
Пример #21
0
void BlitAndWait(int cycles)
{
	SDL_Rect dst;

	dst.x = 0;
	dst.y = 20;

#ifdef SCALE_SCREEN
	SDL_Surface *tmp = zoomSurface(gamescreen, 2, 2, 0);
	SDL_FillRect(screen, NULL, 0);
	SDL_BlitSurface(tmp, NULL, screen, NULL);
	SDL_FreeSurface(tmp);
#else
	SDL_BlitSurface(gamescreen, NULL, screen, &dst);
#endif
	SDL_Flip(screen);
	for(int j = 0; j < cycles; j++) {
		SDL_PumpEvents();
		Wait();
	}

}
Пример #22
0
void resize(int width, int height){
	long sclx, scly;

	long screenx = width;
	long screeny = height;
	screen = SDL_SetVideoMode(screenx, screeny, 0, SDL_SWSURFACE | SDL_RESIZABLE);

	sclx = ALG_MAX_X / screen->w;
	scly = ALG_MAX_Y / screen->h;

	scl_factor = sclx > scly ? sclx : scly;

	offx = (screenx - ALG_MAX_X / scl_factor) / 2;
	offy = (screeny - ALG_MAX_Y / scl_factor) / 2;

	if(overlay_original){
		if(overlay)
			SDL_FreeSurface(overlay);
		double overlay_scale = ((double)ALG_MAX_X / (double)scl_factor) / (double)overlay_original->w;
		overlay = zoomSurface(overlay_original, overlay_scale, overlay_scale, 0);
	}
}
Пример #23
0
void lcd_cs(uint8_t on)
{
    if (!on)
    {
        SDL_Surface *scaled;
        scaled = zoomSurface(offscreen, SCALEX, SCALEY, 0);
        SDL_BlitSurface(scaled, NULL, screen, NULL);
        SDL_FreeSurface(scaled);
        SDL_UpdateRect(screen, 0, 0, screen->w, screen->h);

        if (NULL != cmdline_saveframes())
        {
            char fname[1024];
            snprintf(fname, sizeof(fname), "%s-%08d.bmp", cmdline_saveframes(), frameIndex++);
            if (0 != SDL_SaveBMP(screen, fname))
            {
                fprintf(stderr, "Failed to save %s\n", fname);
                exit(1);
            }
        }
    }
}
Пример #24
0
Image* Image::resize(const int width, const int height)
{
    // Don't return anything for bad height or width values
    if (width <= 0 || height <= 0)
        return NULL;

    // Don't scale at all if the image would get the same dimensions
    if (width == getWidth() && height == getHeight())
        return this;
    
    if (mImage)
    {
        SDL_Surface* scaledSurface = NULL;
        Uint8* imageAlphas = NULL;

        const double scaleX = (double) width / (double) getWidth();
        const double scaleY = (double) height / (double) getHeight();

        scaledSurface = zoomSurface(mImage, scaleX, scaleY, 1);

        imageAlphas = new Uint8[scaledSurface->w * scaledSurface->h];
        if (scaledSurface->format->BitsPerPixel == 32)
        {
            // Recalculate the alpha layers
            for (int i = 0; i < scaledSurface->w * scaledSurface->h; ++i)
            {
                Uint8 r, g, b, a;
                SDL_GetRGBA(((Uint32*) scaledSurface->pixels)[i],
                              scaledSurface->format, &r, &g, &b, &a);

                imageAlphas[i] = a;
            }
        }

        return new Image(scaledSurface, imageAlphas);
    }

    return this;
}
Пример #25
0
void Texplosion::drawSprite(SDL_Surface* imageSurface, SDL_Surface* screenSurface, int srcX, int srcY, int dstX, int dstY, int width, int height,int id)
{
	SDL_Rect srcRect;
	srcRect.x = srcX;
	srcRect.y = srcY;
	srcRect.w = width;
	srcRect.h = height;

	SDL_Rect dstRect;
	dstRect.x = dstX;
	dstRect.y = dstY;
	dstRect.w = width;
	dstRect.h = height;

    if ((item_store[id].zx==1) && (item_store[id].zy==1))
    {
        SDL_BlitSurface(imageSurface, &srcRect, screenSurface, &dstRect);
    }
    else
    {
        SDL_Rect tempRect;
        tempRect.x = 0;
        tempRect.y = 0;
        tempRect.w = width;
        tempRect.h = height;
        SDL_Surface* tempSurface=SDL_CreateRGBSurface(SDL_SRCCOLORKEY | SDL_SWSURFACE,width,height,16,0,0,0,0);
        SDL_FillRect(tempSurface,NULL,SDL_MapRGB(tempSurface->format, 255, 0, 255));
        SDL_BlitSurface(imageSurface, &srcRect, tempSurface, &tempRect);
        SDL_Surface *zoomimage=zoomSurface(tempSurface,(double)item_store[id].zx,(double)item_store[id].zy,0);
        //SDL_SetColorKey( zoomimage, SDL_SRCCOLORKEY, SDL_MapRGB(zoomimage->format, 0, 0, 0) );
        SDL_SetAlpha(zoomimage,SDL_SRCCOLORKEY,255);
        SDL_SetColorKey( zoomimage, SDL_SRCCOLORKEY, SDL_MapRGB(zoomimage->format, 255, 0, 255) );
        tempRect.w = zoomimage->w;
        tempRect.h = zoomimage->h;
        SDL_BlitSurface(zoomimage, &tempRect, screenSurface, &dstRect);
    }
}
Пример #26
0
int resizeSurface(SDL_Surface** img, int w, int h)
{
	SDL_Surface* ret;

	double zoomx = w / (double)(*img)->w;
	double zoomy = h / (double)(*img)->h;

	if(!(ret = zoomSurface(*img, zoomx, zoomy, SMOOTHING_ON))) {
		return(-1);
	}

	if((*img)->flags & SDL_SRCCOLORKEY) {
		if(-1 == SDL_SetColorKey(ret, SDL_SRCCOLORKEY, (*img)->format->colorkey)) {
			SDL_FreeSurface(ret);

			return(-1);
		}
	}

	SDL_FreeSurface(*img);
	*img = ret;

	return(0);
}
Пример #27
0
bool Texture::Load(const std::string & path, const TextureInfo & info, std::ostream & error)
{
	if (m_id)
	{
		error << "Tried to double load texture " << path << std::endl;
		return false;
	}

	if (!info.data && path.empty())
	{
		error << "Tried to load a texture with an empty name" << std::endl;
		return false;
	}

	if (!info.data && LoadDDS(path, info, error))
		return true;

	m_cube = info.cube;
	if (m_cube)
	{
		return LoadCube(path, info, error);
	}

	SDL_Surface * orig_surface = 0;
	if (info.data)
	{
		Uint32 rmask, gmask, bmask, amask;
#if SDL_BYTEORDER == SDL_BIG_ENDIAN
		rmask = 0xff000000;
		gmask = 0x00ff0000;
		bmask = 0x0000ff00;
		amask = 0x000000ff;
#else
		rmask = 0x000000ff;
		gmask = 0x0000ff00;
		bmask = 0x00ff0000;
		amask = 0xff000000;
#endif
		orig_surface = SDL_CreateRGBSurfaceFrom(
			info.data, info.width, info.height,
			info.bytespp * 8, info.width * info.bytespp,
			rmask, gmask, bmask, amask);
	}
	if (!orig_surface)
	{
		orig_surface = IMG_Load(path.c_str());
		if (!orig_surface)
		{
			error << "Error loading texture file: " << path << std::endl;
			error << IMG_GetError() << std::endl;
			return false;
		}
	}

	SDL_Surface * surface = orig_surface;
	if (surface)
	{
		m_scale = Scale(info.maxsize, orig_surface->w, orig_surface->h);
		float scalew = m_scale;
		float scaleh = m_scale;

		// scale to power of two if necessary
		bool norescale = (IsPowerOfTwo(orig_surface->w) && IsPowerOfTwo(orig_surface->h)) ||
					(info.npot && (GLEW_VERSION_2_0 || GLEW_ARB_texture_non_power_of_two));

		if (!norescale)
		{
			int maxsize = 2048;
			int new_w = orig_surface->w;
			int new_h = orig_surface->h;

			if (!IsPowerOfTwo(orig_surface->w))
			{
				for (new_w = 1; new_w <= maxsize && new_w <= orig_surface->w * m_scale; new_w = new_w * 2);
			}

			if (!IsPowerOfTwo(orig_surface->h))
			{
				 for (new_h = 1; new_h <= maxsize && new_h <= orig_surface->h * m_scale; new_h = new_h * 2);
			}

			scalew = ((float)new_w + 0.5) / orig_surface->w;
			scaleh = ((float)new_h + 0.5) / orig_surface->h;
		}

		// scale texture down if necessary
		if (scalew < 1.0 || scaleh < 1.0)
		{
			surface = zoomSurface(orig_surface, scalew, scaleh, SMOOTHING_ON);
		}

		// store dimensions
		m_w = surface->w;
		m_h = surface->h;

		GenTexture(surface, info, m_id, m_alpha, error);
	}

	// free the texture surface separately if it's a scaled copy of the original
	if (surface && surface != orig_surface )
	{
		SDL_FreeSurface(surface);
	}

	// free the original surface if it's not a custom surface (used for the track map)
	if (!info.data && orig_surface)
	{
		SDL_FreeSurface(orig_surface);
	}

	return true;
}
Пример #28
0
bool TEXTURE::Load(const std::string & path, const TEXTUREINFO & info, std::ostream & error)
{
	if (id)
	{
		error << "Tried to double load texture " << path << std::endl;
		return false;
	}

	if (path.empty() && !info.data)
	{
		error << "Tried to load a texture with an empty name" << std::endl;
		return false;
	}

	id = 0;
	cube = info.cube;
	if (info.cube && info.verticalcross)
	{
		return LoadCubeVerticalCross(path, info, error);
	}
	else if (info.cube)
	{
		return LoadCube(path, info, error);
	}

	SDL_Surface * orig_surface = 0;
	if (info.data)
	{
		Uint32 rmask, gmask, bmask, amask;
#if SDL_BYTEORDER == SDL_BIG_ENDIAN
		rmask = 0xff000000;
		gmask = 0x00ff0000;
		bmask = 0x0000ff00;
		amask = 0x000000ff;
#else
		rmask = 0x000000ff;
		gmask = 0x0000ff00;
		bmask = 0x00ff0000;
		amask = 0xff000000;
#endif
		orig_surface = SDL_CreateRGBSurfaceFrom(
							info.data, info.width, info.height,
							info.bytespp * 8, info.width * info.bytespp,
							rmask, gmask, bmask, amask);
	}
	if (!orig_surface)
	{
		orig_surface = IMG_Load(path.c_str());
		if (!orig_surface)
		{
			error << "Error loading texture file: " << path << std::endl;
			return false;
		}
	}

	SDL_Surface * texture_surface = orig_surface;
	if (orig_surface)
	{
	    origw = texture_surface->w;
        origh = texture_surface->h;

		//scale to power of two if necessary
		bool norescale = (IsPowerOfTwo(orig_surface->w) && IsPowerOfTwo(orig_surface->h)) ||
					(info.npot && (GLEW_VERSION_2_0 || GLEW_ARB_texture_non_power_of_two));
		if (!norescale)
	    {
			int maxsize = 2048;
	        int newx = GetPowerOfTwo(orig_surface->w, maxsize);
	        int newy = GetPowerOfTwo(orig_surface->h, maxsize);
	        float scalew = ((float)newx+0.5) / orig_surface->w;
	        float scaleh = ((float)newy+0.5) / orig_surface->h;

	        SDL_Surface * pot_surface = zoomSurface(orig_surface, scalew, scaleh, SMOOTHING_ON);

	        assert(IsPowerOfTwo(pot_surface->w));
	        assert(IsPowerOfTwo(pot_surface->h));

	        SDL_FreeSurface(orig_surface);
	        orig_surface = pot_surface;
	        texture_surface = orig_surface;
	    }

		//scale texture down if necessary
		scale = Scale(info.maxsize, orig_surface->w, orig_surface->h);
		if (scale < 1.0)
		{
			texture_surface = zoomSurface(orig_surface, scale, scale, SMOOTHING_ON);
		}

		//store dimensions
		w = texture_surface->w;
		h = texture_surface->h;

		GenTexture(texture_surface, info, id, alpha, error);
	}

	//free the texture surface separately if it's a scaled copy of the original
	if (texture_surface != orig_surface && texture_surface)
	{
		SDL_FreeSurface(texture_surface);
	}

	//free the original surface if it's not a custom surface (used for the track map)
	if (!info.data && orig_surface)
	{
		SDL_FreeSurface(orig_surface);
	}

	return true;
}
Пример #29
0
bool CSurface::DrawSurface(int x, int y, SDL_Surface* destination, SDL_Rect* clip, bool resize, bool maintainRatio)
{
	double scaleX = 0;	double scaleY = 0;

	if (m_Surface == 0)
	{
		if (m_ColoredSurface == true)
		{
			g_LogFile.ss() << "ERROR - Colored surface null: '" << m_Filename << "'"; g_LogFile.ssend();
			return false;
		}

		if (m_Temp) SDL_FreeSurface(m_Temp);
		m_Temp = 0;
		if (!loaded)
		{
			if (m_Filename == ButtonPath("").c_str())	// fix disabled buttons
				m_Filename = ImagePath("blank.png").c_str();
			if (!LoadImage(m_Filename))
			{
				g_LogFile.ss() << "ERROR - Loading Image '" << m_Filename << "'"; g_LogFile.ssend();
				return false;
			}
			if (m_Cached)
			{
				loaded = true;
			}
		}
	}

	m_TimeUsed = g_Graphics.GetTicks();

	if (clip && resize)
	{
		if (m_Temp == 0)
		{
			if (maintainRatio == true)
			{
				if (clip->w != m_Surface->w && clip->h != m_Surface->h)
				{
					if (m_Surface->w > m_Surface->h)	// if the width is larger so scale down based on the width but keep aspect ratio
						scaleX = scaleY = ((double)clip->w / (double)m_Surface->w);
					else	// assume the height is larger
						scaleX = scaleY = ((double)clip->h / (double)m_Surface->h);
					m_Temp = zoomSurface(m_Surface, scaleX, scaleY, 1);
				}
			}
			else
			{
				if (clip->w != m_Surface->w || clip->h != m_Surface->h)
				{
					scaleX = ((double)clip->w / (double)m_Surface->w);
					scaleY = ((double)clip->h / (double)m_Surface->h);
					m_Temp = zoomSurface(m_Surface, scaleX, scaleY, 1);
				}
			}
		}
		else
		{
			if (maintainRatio == true)
			{
				if (m_Temp->w != clip->w && m_Temp->h != clip->h)
				{
					if (m_Temp) SDL_FreeSurface(m_Temp);// free old image
					m_Temp = 0;
					if (m_Surface->w > m_Surface->h)	// if the width is larger so scale down based on the width but keep aspect ratio
						scaleX = scaleY = ((double)clip->w / (double)m_Surface->w);
					else	// assume the height is larger
						scaleX = scaleY = ((double)clip->h / (double)m_Surface->h);
					m_Temp = zoomSurface(m_Surface, scaleX, scaleY, 1);
				}
			}
			else
			{
				if (m_Temp->w != clip->w || m_Temp->h != clip->h)
				{
					if (m_Temp)	SDL_FreeSurface(m_Temp);	// free old image
					m_Temp = 0;

					scaleX = ((double)clip->w / (double)m_Surface->w);
					scaleY = ((double)clip->h / (double)m_Surface->h);
					m_Temp = zoomSurface(m_Surface, scaleX, scaleY, 1);
				}
			}
		}
	}

	// create and setup a SDL offset rectangle
	SDL_Rect offset;
	offset.x = x;
	offset.y = y;

	// Draw the source surface onto the destination
	int error = 0;
	if (destination)
	{
		error = m_Temp ?
			SDL_BlitSurface(m_Temp, clip, destination, &offset) :
			SDL_BlitSurface(m_Surface, clip, destination, &offset);
	}
	else	// blit to the screen
	{
		error = m_Temp ?
			SDL_BlitSurface(m_Temp, clip, g_Graphics.GetScreen(), &offset) :
			SDL_BlitSurface(m_Surface, clip, g_Graphics.GetScreen(), &offset);
	}

	if (error == -1)
	{
		g_LogFile.ss() << "Error Blitting surface (" << m_Filename << ") - " << SDL_GetError(); g_LogFile.ssend();
		return false;
	}

	return true;
}
Пример #30
0
void render_background(slide *sl, SDL_Surface *surface)
{
	SDL_Rect dst;
	style *st = sl->st;
	
	// Set slide area to the background colour:
	dst.x = 0;
	dst.y = 0;
	dst.w = sl->des_w;
	dst.h = sl->des_h;
	SDL_FillRect(surface, &dst, colour->fills->item(st->bgcolour));

	// Fill in the	titlebar area:
	if(st->enablebar)
	{
		dst.h = st->titlespacing - TITLE_EDGE;
		SDL_FillRect(surface, &dst, colour->fills->item(st->barcolour));
	}

	// Draw background image or texture:
	if(st->background != NULL)
	{
		int src_w, src_h, dst_w, dst_h;
		SDL_Surface *scaled;
		SDL_Rect dst;
		
		// Scale background image to fit this slide:
		src_w = st->background->w;
		src_h = st->background->h;
		dst_w = sl->des_w;
		if(st->bgbar || !st->enablebar)
		{
			dst_h = sl->des_h;
			dst.y = 0;
		}
		else
		{
			dst_h = sl->des_h - (st->titlespacing - TITLE_EDGE);
			dst.y = st->titlespacing - TITLE_EDGE;
		}
		scaled = zoomSurface(st->background, (double)dst_w / (double)src_w,
				(double)dst_h / (double)src_h, 1);
		
		// Blit:
		dst.x = 0;
		SDL_BlitSurface(scaled, NULL, surface, &dst);
	
		SDL_FreeSurface(scaled);
	}
	else if(st->texture != NULL)
	{
		SDL_Rect dst;
		int w, h;
		int xrepeats, yrepeats;
		int vert_offset;
		
		w = st->texture->w;
		h = st->texture->h;
		if(st->bgbar || !st->enablebar)
			vert_offset = 0;
		else
			vert_offset = st->titlespacing - TITLE_EDGE;
		xrepeats = SCREEN_WIDTH / w;
		if(SCREEN_WIDTH % w > 0)
			xrepeats++;
		yrepeats = (SCREEN_HEIGHT - vert_offset) / h;
		if((SCREEN_HEIGHT - vert_offset) % h > 0)
			yrepeats++;
		for(int x = 0; x < xrepeats; x++)
		{
			for(int y = 0; y < yrepeats; y++)
			{
				dst.x = x * w;
				dst.y = vert_offset + y * h;
				SDL_BlitSurface(st->texture, NULL, surface, &dst);
			}
		}
	}
	
	// Draw the slide border:
	for(int i = 0; i < st->slideborder; i++)
	{
		rectangleColor(surface, i, i, sl->des_w - 1 - i, sl->des_h - 1 - i,
				colour->pens->item(st->bordercolour));
	}
	
	// Draw dividing line between titlebar and slide body, and the slide title:
	if(st->enablebar)
	{
		char *title = sl->content->line;
		int xbase = st->leftmargin + st->foldmargin;
		
		for(int i = 0; i < st->barborder; i++)
		{
			hlineColor(surface, 1, sl->des_w - 2,
					st->titlespacing - TITLE_EDGE - 1 + i,
					colour->pens->item(st->bordercolour));
		}

		render_text(title, st->title_font, colour->inks->item(st->titlecolour),
				surface, xbase, (st->titlespacing - TITLE_EDGE -
				(st->title_ascent - st->title_descent)) / 2 - 1 + st->topmargin);
	}
}