there are altogether 3 jpg files, the sizes of the first two are all 512*340.
When I load, resize and display the first image, it goes fine. but when it comes to the second image, the psp get crashed, but it goes well on pc. I use "zoomSurface" from SDL_rotozoom to resize image. If image is not resized, just simply loading and displaying, the program runs fine in both pc and psp.
the main function is void DrawPic(int id), possible error are hiding there.
Code: Select all
#pragma warning(disable : 4786)
#ifdef WIN32
#else
#include <pspkernel.h>
#endif
#include <stdio.h>
#include <malloc.h>
#include <sdl.h>
#include <sdl_image.h>
#include <sdl_ttf.h>
#include "sdl_rotozoom.h"
#define SCREEN_WIDTH 480
#define SCREEN_HEIGHT 272
#ifdef WIN32
#else
PSP_HEAP_SIZE_KB(20480);
#endif
SDL_Surface *Screen;
SDL_Surface *Image;
SDL_Surface *Message;
TTF_Font *Font;
int freemem()
{
void *ptrs[480];
int mem, x, i;
for (x = 0; x < 480; x++)
{
void *ptr = malloc(51200);
if (!ptr) break;
ptrs[x] = ptr;
}
mem = x * 51200;
for (i = 0; i < x; i++)
free(ptrs[i]);
return mem;
}
void Write(char *content,int x, int y)
{
SDL_Color fontcolor={0,255,0};
SDL_Rect coord={x,y,0,0};
Message=TTF_RenderText_Solid(Font,content,fontcolor);
SDL_BlitSurface(Message,NULL,Screen,&coord);
SDL_FreeSurface(Message);
SDL_Flip( Screen );
}
void DrawPic(int id)
{
char filename[30];
char string[30];
double ratio,ratio1,ratio2;
SDL_RWops *rwop;
SDL_Surface *resizedImage;
if(Image!=NULL)
{
SDL_FreeSurface(Image);
}
//load a image
sprintf(filename,"gamebg%d.jpg",id);
rwop=SDL_RWFromFile(filename,"rb");
Image=IMG_LoadJPG_RW(rwop);
if(Image==NULL)
{
sprintf(string,"Err: Image load error!\n");
Write(string,10,30);
return;
}
rwop->close(rwop);
SDL_FreeRW(rwop);
//-----------------------------------------------
//-----------Error part starts here--------------
//-----------------------------------------------
//zoom the image
ratio1=(double)SCREEN_WIDTH/Image->w;
ratio2=(double)SCREEN_HEIGHT/Image->h;
if(ratio1<ratio2)
ratio=ratio1;
else
ratio=ratio2;
resizedImage=zoomSurface(Image,ratio,ratio,0);
if(resizedImage==NULL)
{
sprintf(string,"Image resize error!\n");
Write(string,10,30);
return;
}
if(Image!=NULL)SDL_FreeSurface(Image);
Image=resizedImage;
//---------------------------------------------
//-----------Error part ends here--------------
//---------------------------------------------
//draw black background
SDL_FillRect(Screen,NULL,SDL_MapRGB(Screen->format,0,0,0));
//draw the image
SDL_BlitSurface(Image, NULL, Screen, NULL);
//update screen
SDL_Flip( Screen );
//Show pic id
sprintf(string,"PicID: %d",id);
Write(string,10,10);
sprintf(string,"Available Mem: %.2fM",freemem()*1.0/(1024*1024));
Write(string,200,10);
}
extern "C"
int main(int argc, char* argv[])
{
//Initialize SDL and other sub-systems
SDL_Init(SDL_INIT_VIDEO);
Screen = SDL_SetVideoMode(SCREEN_WIDTH, SCREEN_HEIGHT, 32, SDL_SWSURFACE|SDL_ANYFORMAT);
SDL_ShowCursor(SDL_DISABLE);
TTF_Init();
Font=TTF_OpenFont("times.ttf",12);
//first image show
DrawPic(1);
SDL_Delay(2000);
DrawPic(2);
SDL_Delay(2000);
DrawPic(3);
SDL_Delay(2000);
//end program
SDL_Quit();
return 0;
}