I've writed a Image viewer using SDL, but it crash when I try to resize the image!!!!
Here is the code:
Code: Select all
int Image_viewer(char* target, char* est)
{
SDL_Surface* screen = SDL_SetVideoMode( SCREEN_WIDTH, SCREEN_HEIGHT, SCREEN_BPP, SDL_ASYNCBLIT);
SDL_ShowCursor(0);
SDL_Surface* back = NULL;
SDL_RWops *rwfile = NULL;
SDL_Surface* background = NULL;
FILE *fp = NULL;
Uint32 black;
black = SDL_MapRGB(screen->format, 0, 0, 0);
SDL_Rect offset;
SceCtrlLatch pad1;
SceCtrlData pad2;
sceCtrlSetSamplingCycle(0);
sceCtrlSetSamplingMode(PSP_CTRL_MODE_ANALOG);
fp = fopen(target, "rb");
rwfile = SDL_RWFromFP(fp, 1);
if(rwfile==NULL)
{
Init();
printf("\nErrore nella lettura del file!\n%s", SDL_GetError());
sleep(3000000);
SDL_FreeSurface(screen);
SDL_FreeRW(rwfile);
SDL_Quit();
return 1;
}
//Load the image
if(strcmp(est, "BMP")==0) back = SDL_LoadBMP_RW(rwfile, 1);
if((strcmp(est, "PNM")==0)
|| (strcmp(est, "PPM")==0)
|| (strcmp(est, "PGM")==0)
|| (strcmp(est, "PBM")==0)) back = IMG_LoadPNM_RW(rwfile);
if(strcmp(est, "XPM")==0) back = IMG_LoadXPM_RW(rwfile);
if(strcmp(est, "XCF")==0) back = IMG_LoadXCF_RW(rwfile);
if(strcmp(est, "PCX")==0) back = IMG_LoadPCX_RW(rwfile);
if(strcmp(est, "GIF")==0) back = IMG_LoadGIF_RW(rwfile);
if((strcmp(est, "JPG")==0) || (strcmp(est, "JPEG")==0)
|| (strcmp(est, "JPE")==0)) back = IMG_LoadJPG_RW(rwfile);
if((strcmp(est, "TIF")==0) || (strcmp(est, "TIFF")==0)) back = IMG_LoadTIF_RW(rwfile);
if(strcmp(est, "PNG")==0) back = IMG_LoadPNG_RW(rwfile);
if(strcmp(est, "TGA")==0) back = IMG_LoadTGA_RW(rwfile);
if(strcmp(est, "LBM")==0) back = IMG_LoadLBM_RW(rwfile);
if(back == NULL )
{
Init();
printf("\nErrore durante il caricamento dell'immagine\n o formato non supportato\n\n%s", SDL_GetError());
sleep(3000000);
SDL_FreeSurface(back);
SDL_FreeSurface(screen);
SDL_RWclose(rwfile);
SDL_FreeRW(rwfile);
SDL_Quit();
return 1;
}
SDL_RWclose(rwfile);
SDL_FreeRW(rwfile);
int off_x=0;
int off_y=0;
/*
char* new_target = (char*)malloc(256);
char* new_est = (char*)malloc(8);
strcpy(new_target, target);
strcpy(new_est, est);
int immagine = 0;
*/
double angle=0.000000;
double scale_x=1.000000;
double scale_y=1.000000;
int custom_param=0;
int custom_pos_x=0;
int custom_pos_y=0;
int custom_angle=0;
int rotate_param=0;
int start_scale_x=0;
int start_scale_y=0;
int first_boot=1;
int moving=0;
int allow_custom_pos_x=0;
int allow_custom_pos_y=0;
int refresh=1;
// int newimage=1;
while(running())
{
#include "Image_viewer/pressione_tasto.c"
SDL_FillRect(screen, NULL, black);
if(!moving)
{
if(custom_param!=1)
{
if(rotate_param==0)
{
scale_x=(272/scale_y);
scale_y=(272/scale_y);
}
else
{
scale_x=(272/scale_x);
scale_y=(272/scale_x);
}
}
if(first_boot)
{
start_scale_x = scale_x;
start_scale_y = scale_y;
}
if(custom_angle!=1)
{
angle = 0.000000;
}
background = rotozoomSurfaceXY(back, angle, scale_x, scale_y, 0); // <---- Here the programm crash!!!!!!!!!!!!!
if(custom_pos_x==0)
{
off_x = ( screen->w - background->w ) / 2;
}
if(custom_pos_y==0)
{
off_y = ( screen->h - background->h ) / 2;
}
}
if(background->w > screen->w)
{
allow_custom_pos_x=1;
}
else
{
allow_custom_pos_x=0;
}
if(background->h > screen->h)
{
allow_custom_pos_y=1;
}
else
{
allow_custom_pos_y=0;
}
offset.x = off_x;
offset.y = off_y;
//Blit the surface
SDL_BlitSurface( background, NULL, screen, &offset );
SDL_Flip(screen);
SDL_FreeSurface(background);
first_boot=0;
}
SDL_FreeSurface(screen);
SDL_FreeSurface(back);
//free(new_target);
//free(new_est);
SDL_Quit();
return 0;
}
Code: Select all
sceCtrlReadLatch(&pad1);
sceCtrlReadBufferPositive(&pad2, 1);
if(pad1.uiMake==PSP_CTRL_CIRCLE)
{
SDL_FreeSurface(background);
printf("sdkofjhdskfh");
break;
}
if(pad1.uiMake==PSP_CTRL_CROSS)
{
moving=0;
custom_param=0;
custom_pos_x=0;
custom_pos_y=0;
refresh=1;
}
if(pad1.uiMake==PSP_CTRL_UP)
{
custom_angle=1;
if(rotate_param==0) rotate_param=1;
else rotate_param=0;
angle+=90;
refresh=1;
moving=0;
}
if(pad1.uiMake==PSP_CTRL_DOWN)
{
custom_angle=1;
if(rotate_param==0) rotate_param=1;
else rotate_param=0;
angle-=90;
refresh=1;
moving=0;
}
if(pad1.uiMake==PSP_CTRL_LTRIGGER)
{
if(scale_x>=0.2)
{
custom_param=1;
scale_x-=0.2;
scale_y-=0.2;
refresh=1;
moving=0;
}
}
if(pad1.uiMake==PSP_CTRL_RTRIGGER)
{
if(scale_x<start_scale_x+1.500000)
{
custom_param=1;
scale_x+=0.2;
scale_y+=0.2;
refresh=1;
moving=0;
}
}
if(pad2.Lx > 128 + 80)
{
if(allow_custom_pos_x==1)
{
custom_pos_x=1;
if(off_x>(background->w - screen->w)-((background->w - screen->w)*2)) off_x-=10;
refresh=1;
moving=1;
}
}
else if(pad2.Ly > 128 + 80)
{
if(allow_custom_pos_y==1)
{
custom_pos_y=1;
if(off_y>(background->h - screen->h)-((background->h - screen->h)*2)) off_y-=10;
refresh=1;
moving=1;
}
}
else if(pad2.Lx < 128 - 80)
{
if(allow_custom_pos_x==1)
{
custom_pos_x=1;
if(off_x<0) off_x+=10;
refresh=1;
moving=1;
}
}
else if(pad2.Ly < 128 - 80)
{
if(allow_custom_pos_y==1)
{
custom_pos_y=1;
if(off_y<0) off_y+=10;
refresh=1;
moving=1;
}
}
else moving = 0;
I've to call this function everytime that I want to use it!
( yesterday this code works great, but now it crash!!! Maybe I've modified somethings... I don't remember! and I don't know why now I get a error! )
Please help... :(
Thanks