Example #1
0
SDL_Surface* flipSurface(SDL_Surface* surf, int flags)
{
    SDL_Surface* flipped = NULL;
    flipped = SDL_CreateRGBSurface( SDL_SWSURFACE, surf->w, surf->h, surf->format->BitsPerPixel, surf->format->Rmask,
                                    surf->format->Gmask, surf->format->Bmask, surf->format->Amask );

    if(SDL_MUSTLOCK(surf)) SDL_LockSurface(surf);

    for(int x=0, rx=flipped->w - 1; x < flipped->w; x++, rx--)
        for(int y=0, ry=flipped->h-1; y<flipped->h; y++, ry--)
        {
            Uint32 pixel = getPixel32(surf, x, y);
            if((flags & FLIP_HORIZONTAL) && (flags & FLIP_VERTICAL)) putPixel32(flipped, rx, ry, pixel);
            else if(flags & FLIP_VERTICAL) putPixel32(flipped, x, ry, pixel);
            else putPixel32(flipped, rx, y, pixel);
        }
    if(SDL_MUSTLOCK(surf)) SDL_UnlockSurface(surf);

    return flipped;
}
Example #2
0
void Puck::fillEnvironment(Uint32 c, Uint32 color) {
	Sint16 xc = _xCenterPixel, yc = _yCenterPixel, r = _radius;
	int id;
	if (c < 0xFF) {
		c = SDL_MapRGB(gEnvironmentImage->format, 0xFF, c, 0x00);
		id = _id;
	} else {id = 0;}
	for (Sint16 xi = xc - r; xi < xc + r; xi++) {
		for (Sint16 yi = yc - r; yi < yc + r; yi++) {
			putPixel32(gEnvironmentImage, xi, yi, c);
			gPuckMap[xi][yi] = id;
		}
	}
	if (!gBatchMode) {
		for (Sint16 xi = xc - r; xi < xc + r; xi++) {
			for (Sint16 yi = yc - r; yi < yc + r; yi++) {
				putPixel32(gForegroundImage, xi, yi, color);
			}
		}
	}
}
Example #3
0
void composite(SDL_Surface* src, SDL_Surface *dest, int offsetX, int offsetY)
{
    if(SDL_MUSTLOCK(dest)) SDL_LockSurface(dest);
    if(SDL_MUSTLOCK(src)) SDL_LockSurface(src);

    for(int x=0; x < src->w; x++)
        for(int y=0; y<src->h; y++)
        {
            Uint32 srcpixel = getPixel32(src, x, y);
            if(srcpixel & 0xff000000)
            {
                putPixel32(dest, x+offsetX, y+offsetY, srcpixel);
                continue;
            }
            Uint32 destpixel = getPixel32(dest, x+offsetX, y+offsetY);
            putPixel32(dest, x+offsetX, y+offsetY, destpixel);
        }

    if(SDL_MUSTLOCK(dest)) SDL_UnlockSurface(dest);
    if(SDL_MUSTLOCK(src)) SDL_UnlockSurface(src);
}
Example #4
0
void Sprite::flipSurface( SDL_Surface* src, SDL_Surface* desk, Vec2i begin, Vec2i end ) {

	if (SDL_MUSTLOCK( src )) {
		SDL_LockSurface( src );
	}

	for ( int y = begin.y ; y < end.y ; y++ ) {
		//Go through columns
		for ( int x = begin.x ; x < end.x ; x++ ) {
			putPixel32( desk, (x - begin.x), (y - begin.y), getPixel32( src, x, y ));
		}
	}

	if ( SDL_MUSTLOCK( src ) ) {
		SDL_UnlockSurface( src );
	}


	//Copy color key 
	if ( src->flags & SDL_SRCCOLORKEY ) {
		SDL_SetColorKey( desk, SDL_RLEACCEL | SDL_SRCCOLORKEY,
				src->format->colorkey );
	}
}
Example #5
0
int TilesLoad(const char *fname)
{
    nil_tile = IMG_Load(NILT_FN);
    if(nil_tile == NULL)
    {
        printf("File`s offline: %s\n", NILT_FN);
        return -1;
    }
    SDL_Surface *t = SDL_DisplayFormatAlpha(nil_tile);
    SDL_FreeSurface(nil_tile);
    SDL_Surface *s = zoomSurface(t, (double)tile_w_/t->w, (double)tile_h_/t->h, SMOOTHING_OFF);
    SDL_FreeSurface(t);
    nil_tile = s;

    FILE *f;
    if((f = fopen(fname, "r")) == NULL)
    {
        printf("Can`t reach the file: %s\n", fname);
        return -1;
    }

    /* Skipping to second line */
    //fscanf(f, "\n");

    int recieved = 0;
    int current_tile = 0;
    while(!feof(f))
    {
#define RESERVED 0x400
        if(tiles == NULL) tiles = malloc(sizeof(tilerecord_t));
        else tiles = realloc(tiles, sizeof(tilerecord_t)*(current_tile+1));
        tiles[current_tile].alright = 1; /* will be set to 0, once any fail happens */
        /* Reserving memory */
        tiles[current_tile].filename = malloc(RESERVED);
        memset(tiles[current_tile].filename, '\0', RESERVED);
        tiles[current_tile].dummy = malloc(RESERVED);
        if(fscanf(f, "%u:%u:%s\n", &tiles[current_tile].id, &tiles[current_tile].type,
                  tiles[current_tile].filename) != 3)
                  {
                      printf("TilesLoad(): WARNING: It seems like the input \
file have something wrong aboard (comments, etc.). Proceeding may result in fault!\n");
                      tiles[current_tile].alright = 0;
                  }
        tiles[current_tile].filename = realloc(tiles[current_tile].filename, strlen(tiles[current_tile].filename));
        free(tiles[current_tile].dummy);
        char *fn = (char*)malloc(strlen(tiles[current_tile].filename)+strlen(TILEDIR EXTEN)+1);
        memset(fn, '\0', strlen(tiles[current_tile].filename)+strlen(TILEDIR EXTEN)+1);
        strcat(fn, TILEDIR);
        strcat(fn, tiles[current_tile].filename);
        strcat(fn, EXTEN);

        SDL_Surface *tmpsurf = SDL_LoadBMP(fn);
        if(tmpsurf == NULL)
        {
            printf("TilesLoad(): Image wasn`t loaded successfully: %s\n", fn);
            tiles[current_tile].alright = 0;
            current_tile++;
            free(fn);
            continue;
        }
        free(fn);

        int x,y;
        tiles[current_tile].tile = SDL_DisplayFormatAlpha(tmpsurf);
        SDL_FreeSurface(tmpsurf);
        for(x = 0; x < tiles[current_tile].tile->w; x++)
        {
            for(y = 0; y < tiles[current_tile].tile->h; y++)
            {
                if(!(getPixel32(tiles[current_tile].tile, x, y)&0xffffff))
                    putPixel32(tiles[current_tile].tile, x, y, 0);
            }
        }

        tiles[current_tile].tile_noscale = tiles[current_tile].tile;

//        if(tiles[current_tile].tile_noscale->w != tile_w_ || tiles[current_tile].tile_noscale->h != tile_h_)
//        {
            tiles[current_tile].tile = zoomSurface(tiles[current_tile].tile_noscale,
                                                   (double)tile_w_/tiles[current_tile].tile_noscale->w,
                                                   (double)tile_h_/tiles[current_tile].tile_noscale->h, SMOOTHING_OFF);
//        }

        current_tile++;
        recieved++;
    }
Example #6
0
bool World::loadFiles()
{
	bool returnValue = true;

    // Load the dot image
    
    gRobotMaskImage = load_image( gRobotMaskImageFilename );
    gRobotDisplayImage = load_image( gRobotDisplayImageFilename );
	
	// Load the agent specifications image
    
	gRobotSpecsImage = load_image( gRobotSpecsImageFilename ); // no jpg (color loss)	

    // Load the foreground image (active borders)
    
    gForegroundImage = load_image( gForegroundImageFilename );   // RECOMMENDED: png rather than jpeg (pb with transparency otw)
	if ( gForegroundImageFilename.compare(gForegroundImageFilename.length()-3, 3, "jpg", 0, 3) == 0 )
	{
		std::cerr << "foreground: PNG format is *mandatory* (JPG may feature transparency problems due to compression with loss)\n";
		returnValue = false;
	}
	
    gEnvironmentImage = load_image( gEnvironmentImageFilename );
	if ( gEnvironmentImageFilename.compare(gEnvironmentImageFilename.length()-3, 3, "jpg", 0, 3) == 0 )
	{
		std::cerr << "environment: PNG format is *mandatory* (JPG may feature transparency problems due to compression with loss)\n";
		returnValue = false;
    }
	
    //gTrajectoryMonitorImage = load_image( gEnvironmentImageFilename ); // prepare for logging trajectories (useful if requested in the config file)   ---- // Created in roborobo::initTrajectoriesMonitor

	// load background image
	
    gBackgroundImage = load_image( gBackgroundImageFilename );

	// Load the ground type image
	
    gGroundSensorImage = load_image( gGroundSensorImageFilename );
	
    // Managing problems with loading files (agent mask and specs)
    
    if( gRobotMaskImage == NULL )
    {
		std::cerr << "Could not load agent mask image\n";
		returnValue = false;
    }
	
    if ( gRobotDisplayImage == NULL )
    {
		std::cerr << "Could not load agent display image\n";
		returnValue = false;
    }
            
    if( gRobotSpecsImage == NULL )
    {
		std::cerr << "Could not load agent specification image\n";
		returnValue = false;
    }
	
    //If there was a problem in loading the foreground image
    if( gForegroundImage == NULL )
    {
		std::cerr << "Could not load foreground image\n";
		returnValue = false;
    }
	
	if ( gEnvironmentImage == NULL )
    {
		std::cerr << "Could not load environment image\n";
		returnValue = false;
    }	
	
	
	//no background image (not a critical error)
	if ( gBackgroundImage == NULL )
	{
		std::cout << "warning: could not load background image (will proceed anyway)\n";
	}
	
	// mandatory: image dimensions must be more than 1024x768 (otw: screen underfitting)
	if ( gForegroundImage->w < gScreenWidth || gForegroundImage->h < gScreenHeight )
	{
		std::cerr << "foreground image dimensions must be " << gScreenWidth << "x" << gScreenHeight << " or higher (given: " << gForegroundImage->w << "x" << gForegroundImage->h << ") \n";
		returnValue = false;
	}
	
	//If there was a problem in loading the ground type image
    if(  gGroundSensorImage == NULL )
    {
		std::cerr << "Could not load ground image\n";
        returnValue = false;    
    }
	else
    {
        if( ( gGroundSensorImage->w != gForegroundImage->w ) || ( gGroundSensorImage->h != gForegroundImage->h ) )
        {
            std::cerr << "Ground image dimensions do not match that of the foreground image\n";
            returnValue = false;
        }
    }
	
	// set reference dimensions
	gRobotWidth = gRobotMaskImage->w ;
	gRobotHeight = gRobotMaskImage->h ;
	
	if ( gMaxTranslationalSpeed > gRobotWidth || gMaxTranslationalSpeed > gRobotHeight )
	{
		std::cerr << "[ERROR] gMaxTranslationalSpeed value *should not* be superior to agent dimensions (image width and/or height) -- may impact collision accuracy (e.g. teleporting through walls)\n";
		returnValue = false;
	}
	
	gAreaWidth = gForegroundImage->w;
	gAreaHeight = gForegroundImage->h;

	// set transparency color
	SDL_SetColorKey( gRobotMaskImage, SDL_SRCCOLORKEY, SDL_MapRGBA( gRobotMaskImage->format, 0xFF, 0xFF, 0xFF,0 ) );
   	SDL_SetColorKey( gRobotDisplayImage, SDL_SRCCOLORKEY, SDL_MapRGBA( gRobotMaskImage->format, 0xFF, 0xFF, 0xFF,0 ) );

	SDL_SetColorKey( gForegroundImage, SDL_SRCCOLORKEY, SDL_MapRGBA( gForegroundImage->format, 0xFF, 0xFF, 0xFF,0 ) );
	SDL_SetColorKey( gEnvironmentImage, SDL_SRCCOLORKEY, SDL_MapRGBA( gEnvironmentImage->format, 0xFF, 0xFF, 0xFF,0 ) );

	// preparing Environment Image (ie. only the BLUE component is used)
	for ( int x = 0 ; x != gEnvironmentImage->w ; x++ )
		for ( int y = 0 ; y != gEnvironmentImage->h ; y++ )
		{
			Uint32 pixel = getPixel32(gEnvironmentImage,x,y);
			if ( pixel != SDL_MapRGBA( gEnvironmentImage->format, 0xFF, 0xFF, 0xFF, 0 ) )
				putPixel32( gEnvironmentImage, x, y,  SDL_MapRGBA( gEnvironmentImage->format, 0, 0, pixel&0x0000FF,0 ) );
		}

    //If everything loaded fine
	if ( returnValue == false ) 
		return false;
	else
		return true;
}