forums.ps2dev.org Forum Index forums.ps2dev.org
Homebrew PS2, PSP & PS3 Development Discussions
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

psp crash when I try to resize image with SDL

 
Post new topic   Reply to topic    forums.ps2dev.org Forum Index -> PSP Development
View previous topic :: View next topic  
Author Message
danny_dot_net



Joined: 11 Jul 2008
Posts: 4

PostPosted: Tue Jul 15, 2008 1:41 pm    Post subject: psp crash when I try to resize image with SDL Reply with quote

the program is simply for loading a jpg file each time and displaying it.

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:
#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;
}
Back to top
View user's profile Send private message
Jim



Joined: 02 Jul 2005
Posts: 487
Location: Sydney

PostPosted: Wed Jul 16, 2008 6:38 am    Post subject: Reply with quote

free resizedImage
_________________
http://www.dbfinteractive.com
Back to top
View user's profile Send private message Visit poster's website
danny_dot_net



Joined: 11 Jul 2008
Posts: 4

PostPosted: Wed Jul 16, 2008 5:20 pm    Post subject: Reply with quote

Now I find what makes the program get crashed in PSP, the bug is that

Code:

resizedImage=zoomSurface(Image,ratio,ratio,0);


can not exist at the same time with the following code

Code:

   sprintf(string,"PicID: %d",id);
   Write(string,10,10);
   sprintf(string,"Available Mem: %.2fM",freemem()*1.0/(1024*1024));
   Write(string,200,10);


If one of them is deleted from the code, the whole program runs wll in PSP, but it does not happen when both of them are there.
Back to top
View user's profile Send private message
Jim



Joined: 02 Jul 2005
Posts: 487
Location: Sydney

PostPosted: Thu Jul 17, 2008 8:03 am    Post subject: Reply with quote

What if you just take out the freemem() call from the sprintf? Does it still work then? If so, the heap has got corrupted. But from the size of your images you're going to run out of memory too, hence my first suggestion.

Jim
_________________
http://www.dbfinteractive.com
Back to top
View user's profile Send private message Visit poster's website
danny_dot_net



Joined: 11 Jul 2008
Posts: 4

PostPosted: Fri Jul 18, 2008 1:56 pm    Post subject: Reply with quote

Jim wrote:
What if you just take out the freemem() call from the sprintf? Does it still work then? If so, the heap has got corrupted. But from the size of your images you're going to run out of memory too, hence my first suggestion.

Jim


as you can see freemem() just checks the residual memory size but does not possese any memory after calling it. anyway, I will give a shot.
Back to top
View user's profile Send private message
Jim



Joined: 02 Jul 2005
Posts: 487
Location: Sydney

PostPosted: Sat Jul 19, 2008 10:49 am    Post subject: Reply with quote

I know, but if the heap were corrupted (eg. by some function allocating too little space then overwriting the end of the allocation), then using the heap next time (like freemem does) will likely trigger a crash caused by corrupting heap earlier on.

If taking out the freemem call moves the error back into the next zoomSurface then either you are running out of ram entirely, or zoomSurface is corrupting the heap.

Jim
_________________
http://www.dbfinteractive.com
Back to top
View user's profile Send private message Visit poster's website
Smong



Joined: 04 Sep 2007
Posts: 82

PostPosted: Sat Jul 19, 2008 9:37 pm    Post subject: Reply with quote

This probably isn't related to the error you are expeiencing, but you don't need to be calling SDL_Flip() so often. Try taking it out of Write() and DrawPic(), then call it after DrawPic like this:

Code:

   //first image show
   DrawPic(1);
   SDL_Flip(Screen);
   SDL_Delay(2000);
   
   DrawPic(2);
   SDL_Flip(Screen);
   SDL_Delay(2000);
   
   DrawPic(3);
   SDL_Flip(Screen);
   SDL_Delay(2000);

_________________
(+[__]%)
Back to top
View user's profile Send private message
Jim



Joined: 02 Jul 2005
Posts: 487
Location: Sydney

PostPosted: Mon Jul 21, 2008 11:48 pm    Post subject: Reply with quote

And
http://forums.ps2dev.org/viewtopic.php?t=10687

Jim
_________________
http://www.dbfinteractive.com
Back to top
View user's profile Send private message Visit poster's website
Display posts from previous:   
Post new topic   Reply to topic    forums.ps2dev.org Forum Index -> PSP Development All times are GMT + 10 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group