Problems with screenshot code.

Discuss the development of new homebrew software, tools and libraries.

Moderators: cheriff, TyRaNiD

jason867
Posts: 78
Joined: Sun Jul 24, 2005 1:58 am
Contact:

Problems with screenshot code.

Post by jason867 »

Hi everybody, nice to see you again. I've been trying to get a screenshot function to work and I've been having trouble. I searched through these forums for related threads, and I found a couple. One piece of code developed a .tga screenshot. I managed to get this working, but I didn't like the fact that Quicktime was the only program on my computer that would open this file. So I tried using the .bmp version I found. I've been tinkering around with it but haven't gotten anywhere. Here is my source so far;

M_Header.h

Code: Select all


////////////////////////////////////////////////////////////////////////////////////
// My_Header.h Version 1.0
// By Jason J. Owens
// August 21, 2005 9:24 AM
//////////////////////////////////////////////////////////////////////////////////////////

#ifndef __MY_HEADER__
#define __MY_HEADER__

// Function Prototypes
void screenshot();
///////////////////////////////////////////////////////////////////////////////////////

// Function Definitions
void screenshot()
{ 
   unsigned char  bmpHeader24[] = { 0x42, 0x4d, 0x38, 0xfa, 0x05, 0x00, 0x00, 0x00, 0x00, 0x00, 0x36, 0x00, 0x00, 0x00, 0x28, 0x00, 0x00, 0x00, 0xe0, 0x01, 0x00, 0x00, 0x10, 0x01, 0x00, 0x00, 0x01, 0x00, 0x18, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x12, 0x0b, 0x00, 0x00, 0x12, 0x0b, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }; 
   unsigned char  buffer[SCR_WIDTH*3];
   unsigned char  r,g,b;
   int bufferIndex = 0;
   unsigned short p;
   
   char savePath="ms0:/PSP/PHOTO/Screenshots/screenshot.bmp";

   int file = sceIoOpen(savePath,PSP_O_CREAT | PSP_O_TRUNC | PSP_O_RDWR, 0777);  // savePath should hold the path to your file

   // write bmp header
   sceIoWrite(file,bmpHeader24,54);

   // write bmp data
   unsigned long vptr0 = VRAM;
   int i=0;
   for&#40;i=0; i<272; i++&#41;
   &#123;
     unsigned long vptr=vptr0;
     int e=0;
     for&#40;e=0; e<480; e++&#41;
     &#123;
      p = *&#40;unsigned short *&#41;vptr;
      r = &#40;p<<3&#41;&0xf8;
      g = &#40;p>>2&#41;&0xf8;
      b = &#40;p>>7&#41;&0xf8;

      buffer&#91;bufferIndex&#93; = b;
      bufferIndex++;
      buffer&#91;bufferIndex&#93; = g;
      bufferIndex++;
      buffer&#91;bufferIndex&#93; = r;
      bufferIndex++;

      vptr+=PIXEL_SIZE*2;
     &#125;
    // write chunk
    sceIoWrite&#40;file,buffer,SCR_WIDTH*3&#41;;
    bufferIndex=0;
    vptr0-=BUF_WIDTH*2;
   &#125;

   // bmp end
   unsigned char end&#91;&#93; = &#123; 0x00, 0x00 &#125;;
   sceIoWrite&#40;file,end,2&#41;;

   sceIoClose&#40;file&#41;;
&#125;
///////////////////////////////////////////////////////////////////////////////////////

Main.c

Code: Select all


//////////////////////////////////////////////////////////////////////////////////////////
/* test Version 1.0
 * by Jason J. Owens
 * August 18, 2005 9&#58;00 AM
*/
//////////////////////////////////////////////////////////////////////////////////////////

// #includes
#include <pspkernel.h>
#include <pspdebug.h>
#include <pspdisplay.h>
#include <stdlib.h>
#include <stdio.h>
#include <Graphics.h>
#include <My_Header.h>

PSP_MODULE_INFO&#40;"test", 0x1000, 1, 1&#41;; // Defines module info.
PSP_MAIN_THREAD_ATTR&#40;0&#41;; // Defines the main thread's attribute value &#40;optional&#41;.

//#define printf	pspDebugScreenPrintf // Defines printf, just to make typing easier.

int ExitGame = 0; // Gets set to '1' when getting ready to exit the game.

// Exit callback
int exit_callback&#40;int arg1, int arg2, void *common&#41;
&#123;
	ExitGame = 1;
	return 0;
&#125;

// Callback thread
int CallbackThread&#40;SceSize args, void *argp&#41;
&#123;
	int cbid;

	cbid = sceKernelCreateCallback&#40;"Exit Callback", exit_callback, NULL&#41;;
	sceKernelRegisterExitCallback&#40;cbid&#41;;

	sceKernelSleepThreadCB&#40;&#41;;

	return 0;
&#125;

// Sets up the callback thread and returns its thread id.
int SetupCallbacks&#40;void&#41;
&#123;
	int thid = 0;

	thid = sceKernelCreateThread&#40;"update_thread", CallbackThread, 0x11, 0xFA0, THREAD_ATTR_USER, 0&#41;;
	if&#40;thid >= 0&#41;
	&#123;
		sceKernelStartThread&#40;thid, 0, 0&#41;;
	&#125;

	return thid;
&#125;

int Random&#40;int high&#41;
&#123;
    return &#40;rand&#40;&#41; % &#40;high+1&#41;&#41;;
&#125;

// Here's the Main Function.
int main&#40;void&#41;
&#123;
	pspDebugScreenInit&#40;&#41;; // Initializes the Debug text output screen.
	SetupCallbacks&#40;&#41;; // Sets up the callback thread and returns its thread id.
	srand&#40;sceKernelLibcTime&#40;&#40;void *&#41; 0&#41;&#41;; // seeded with time
	
        InitializeGraphics&#40;1&#41;;
        
        int x=0,y=0,x1=0,y1=0,radius=0,xsize=0,ysize=0,r=0,g=0,b=0,a=0,c=0,three_d=0,rand3d=0,text=0;

	while&#40;!ExitGame&#41;
	&#123;
          x=Random&#40;480&#41;;
          r=Random&#40;255&#41;;
          y=Random&#40;272&#41;;
          x1=Random&#40;480&#41;;
          y1=Random&#40;272&#41;;
          g=Random&#40;255&#41;;
          xsize=Random&#40;25&#41;;
          b=Random&#40;255&#41;;
          ysize=Random&#40;25&#41;;
          rand3d=Random&#40;25&#41;;
          radius=Random&#40;150&#41;;
          text=20;

          switch&#40;Random&#40;5&#41;&#41;
          &#123;
            case 0&#58;
                 PlotPixel&#40;x,y,r,g,b&#41;;
            case 1&#58;
                 PlotPixel&#40;x,y,r,g,b&#41;;
            case 2&#58;
                 PlotLine&#40;x,y,x1,y1,r,g,b&#41;;
            case 3&#58;
                 PlotCircle&#40;x,y,radius,r,g,b&#41;;
            case 4&#58;
                 for&#40;a=0;a<xsize;a++&#41;
                 &#123;
                   for&#40;c=0;c<ysize;c++&#41;
                   &#123;
                     PlotPixel&#40;x+a,y+c,r,g,b&#41;;
                   &#125;
                 &#125;
            case 5&#58;
                 for&#40;three_d=0;three_d<rand3d;three_d++&#41;
                 &#123;
                   for&#40;a=0;a<xsize;a++&#41;
                   &#123;
                     for&#40;c=0;c<ysize;c++&#41;
                     &#123;
                       PlotPixel&#40;x+a+three_d,y+c+three_d,r+three_d,g+three_d,b+three_d&#41;;
                     &#125;
                   &#125;
                 &#125;
          &#125;
        &#125;
        
        screenshot&#40;&#41;;

	sceKernelExitGame&#40;&#41;; // Returns PSP to the Main Menu.

	return 0; // Just for good practice.
&#125;

//////////////////////////////////////////////////////////////////////////////////////////

Does anyone have a clue as to what I'm doing wrong?
Ask not for whom the bell tolls, it tolls for thee, besides, I'm playing my PSP, tee hee!
------------------------------------------------------
Visit my website for my PSP Homebrew!
User avatar
0x0001
Posts: 11
Joined: Thu Jul 21, 2005 10:02 am

Post by 0x0001 »

why is this define commented?

//#define printf pspDebugScreenPrintf // Defines printf, just to make typing easier.
jason867
Posts: 78
Joined: Sun Jul 24, 2005 1:58 am
Contact:

Post by jason867 »

I wasn't using it in the program, so I comented it out...
Ask not for whom the bell tolls, it tolls for thee, besides, I'm playing my PSP, tee hee!
------------------------------------------------------
Visit my website for my PSP Homebrew!
jason867
Posts: 78
Joined: Sun Jul 24, 2005 1:58 am
Contact:

Post by jason867 »

I found this bitmap screenshot code on another thread in this forum;

Code: Select all

unsigned char  bmpHeader24&#91;&#93; = &#123; 0x42, 0x4d, 0x38, 0xfa, 0x05, 0x00, 0x00, 0x00, 0x00, 0x00, 0x36, 0x00, 0x00, 0x00, 0x28, 0x00, 0x00, 0x00, 0xe0, 0x01, 0x00, 0x00, 0x10, 0x01, 0x00, 0x00, 0x01, 0x00, 0x18, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x12, 0x0b, 0x00, 0x00, 0x12, 0x0b, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 &#125;; 
unsigned char  buffer&#91;SCREEN_WIDTH*3&#93;; 
unsigned char  r,g,b; 
int                  bufferIndex = 0; 
unsigned short p; 

file = sceIoOpen&#40;savePath,O_CREAT | O_TRUNC | O_RDWR, 0777&#41;;  // savePath should hold the path to your file 

// write bmp header 
sceIoWrite&#40;file,bmpHeader24,54&#41;; 
    
// write bmp data 
vptr0 = pgGetVramAddr&#40;0,271&#41;;   // you can find this function in NEM's sources 
for&#40;i=0; i<272; i++&#41; 
&#123;       
  vptr=vptr0; 
  for&#40;e=0; e<480; e++&#41; 
  &#123; 
    p = *&#40;unsigned short *&#41;vptr; 
    r = &#40;p<<3&#41;&0xf8; 
    g = &#40;p>>2&#41;&0xf8; 
    b = &#40;p>>7&#41;&0xf8;          
          
    buffer&#91;bufferIndex&#93; = b; 
    bufferIndex++;          
    buffer&#91;bufferIndex&#93; = g; 
    bufferIndex++;          
    buffer&#91;bufferIndex&#93; = r; 
    bufferIndex++; 

    vptr+=PIXELSIZE*2; 
  &#125; 
  // write chunk 
  sceIoWrite&#40;file,buffer,SCREEN_WIDTH*3&#41;; 
  bufferIndex=0; 
  vptr0-=LINESIZE*2; 
&#125; 

// bmp end 
unsigned char end&#91;&#93; = &#123; 0x00, 0x00 &#125;; 
sceIoWrite&#40;file,end,2&#41;; 

sceIoClose&#40;file&#41;;
But I can't find Nem's Sources, as mentioned here;

Code: Select all

vptr0 = pgGetVramAddr&#40;0,271&#41;;   // you can find this function in NEM's sources 
Since it says 'getVramAddr' I figured I could simply replace it with this (440000, I'm not sure if that's right, but I know I had it right in my source) but it would crash as soon as this function was called. I've looked through it and can't find anything that might be the problem except for this one function call. Can anyone point me in the direciton for 'NEM's sources'?

Thanks for your help.
Ask not for whom the bell tolls, it tolls for thee, besides, I'm playing my PSP, tee hee!
------------------------------------------------------
Visit my website for my PSP Homebrew!
rinco
Posts: 255
Joined: Fri Jan 21, 2005 2:12 pm
Location: Canberra, Australia

Post by rinco »

nowadays we get vram address with this:

Code: Select all

pspsdk/sdk/ge/pspge.h&#58;  * Get the address of VRAM.
pspsdk/sdk/ge/pspge.h&#58;void * sceGeEdramGetAddr&#40;void&#41;;
this is unlikely to change ever again.
jason867
Posts: 78
Joined: Sun Jul 24, 2005 1:58 am
Contact:

Post by jason867 »

I changed my code accordingly but my PSP still crashed when calling the screenshot function. It did the same thing when I put the vram address in there too (440000, or whatever it actually is). Is there anything wrong with just putting the address in there? And as far as you can tell, is there anything wrong with this code to be making it crash?
Ask not for whom the bell tolls, it tolls for thee, besides, I'm playing my PSP, tee hee!
------------------------------------------------------
Visit my website for my PSP Homebrew!
Shine
Posts: 728
Joined: Fri Dec 03, 2004 12:10 pm
Location: Germany

Post by Shine »

Your code looks terrible, but what about vptr0+=BUF_WIDTH*2? And take a look at saveImage in graphics.c for a more clean code.
Arwin
Posts: 426
Joined: Tue Jul 12, 2005 7:00 pm

Post by Arwin »

Shine wrote:Your code looks terrible, but what about vptr0+=BUF_WIDTH*2? And take a look at saveImage in graphics.c for a more clean code.
Somehow 'src' disappeared from your path to graphics.c, and since I think you copied it, that's really weird.

Link should be:

http://svn.ps2dev.org/filedetails.php?r ... rev=0&sc=0
Shine
Posts: 728
Joined: Fri Dec 03, 2004 12:10 pm
Location: Germany

Post by Shine »

Arwin wrote:Somehow 'src' disappeared from your path to graphics.c, and since I think you copied it, that's really weird.
This morning (GMT) it was without "src", but Nevyn restructered it a bit :-)
jason867
Posts: 78
Joined: Sun Jul 24, 2005 1:58 am
Contact:

Post by jason867 »

Shine wrote:Your code looks terrible, but what about vptr0+=BUF_WIDTH*2? And take a look at saveImage in graphics.c for a more clean code.
Well, that's not my code, I got that from elsewhere here at the ps2dev forums. But thanks for the graphics.c link, I'll take a look at it.
Ask not for whom the bell tolls, it tolls for thee, besides, I'm playing my PSP, tee hee!
------------------------------------------------------
Visit my website for my PSP Homebrew!
jason867
Posts: 78
Joined: Sun Jul 24, 2005 1:58 am
Contact:

Post by jason867 »

I looked at the code in graphics.c, and I've already tried that version (or something extremely similiar). I really don't like .tga files, the only program I have to view them with is quicktime media player, lol. Is there a way to save to a different format than .tga? I'll see if that one fix for the .bmp code works.
Ask not for whom the bell tolls, it tolls for thee, besides, I'm playing my PSP, tee hee!
------------------------------------------------------
Visit my website for my PSP Homebrew!
Shine
Posts: 728
Joined: Fri Dec 03, 2004 12:10 pm
Location: Germany

Post by Shine »

I've changed the code to save PNG images:

Code: Select all

void saveImage&#40;const char* filename, Color* data, int width, int height, int lineSize, int saveAlpha&#41;
&#123;
	png_structp png_ptr;
	png_infop info_ptr;
	FILE* fp;
	int i, x, y;
	u8* line;
	
	if &#40;&#40;fp = fopen&#40;filename, "wb"&#41;&#41; == NULL&#41; return;
	png_ptr = png_create_write_struct&#40;PNG_LIBPNG_VER_STRING, NULL, NULL, NULL&#41;;
	if &#40;!png_ptr&#41; return;
	info_ptr = png_create_info_struct&#40;png_ptr&#41;;
	if &#40;!info_ptr&#41; &#123;
		png_destroy_write_struct&#40;&png_ptr, &#40;png_infopp&#41;NULL&#41;;
		return;
	&#125;
	png_init_io&#40;png_ptr, fp&#41;;
	png_set_IHDR&#40;png_ptr, info_ptr, width, height, 8,
		saveAlpha ? PNG_COLOR_TYPE_RGBA &#58; PNG_COLOR_TYPE_RGB,
		PNG_INTERLACE_NONE, PNG_COMPRESSION_TYPE_DEFAULT, PNG_FILTER_TYPE_DEFAULT&#41;;
	png_write_info&#40;png_ptr, info_ptr&#41;;
	line = &#40;u8*&#41; malloc&#40;width * &#40;saveAlpha ? 4 &#58; 3&#41;&#41;;
	for &#40;y = 0; y < height; y++&#41; &#123;
		for &#40;i = 0, x = 0; x < width; x++&#41; &#123;
			Color color = data&#91;x + y * lineSize&#93;;
			int r = &#40;color & 0x1f&#41; << 3; 
			int g = &#40;&#40;color >> 5&#41; & 0x1f&#41; << 3 ;
			int b = &#40;&#40;color >> 10&#41; & 0x1f&#41; << 3 ;
			line&#91;i++&#93; = r;
			line&#91;i++&#93; = g;
			line&#91;i++&#93; = b;
			if &#40;saveAlpha&#41; &#123;
				int a = color & 0x8000 ? 0xff &#58; 0; 
				line&#91;i++&#93; = a;
			&#125;
		&#125;
		png_write_row&#40;png_ptr, line&#41;;
	&#125;
	free&#40;line&#41;;
	png_write_end&#40;png_ptr, info_ptr&#41;;
	png_destroy_write_struct&#40;&png_ptr, &#40;png_infopp&#41;NULL&#41;;
	fclose&#40;fp&#41;;
&#125;
You can call it like this for screenshots: saveImage(filename, getVramDisplayBuffer(), SCREEN_WIDTH, SCREEN_HEIGHT, PSP_LINE_SIZE, 0)

In your Makefile you need to add "-lpng -lz" to your "LIBS" and you have to compile zlib and libpng (which is simply a "make" and "make install" in the directories).
jason867
Posts: 78
Joined: Sun Jul 24, 2005 1:58 am
Contact:

Post by jason867 »

Ok, png is just fine with me. Now, compiling zlib and libpng, I'm not sure where those directories are. Are they in the default cygwin installation? Or in the pspsdk installation? Thanks for the code, I haven't tested it yet, but I'm confident it will work for me.
Ask not for whom the bell tolls, it tolls for thee, besides, I'm playing my PSP, tee hee!
------------------------------------------------------
Visit my website for my PSP Homebrew!
Shine
Posts: 728
Joined: Fri Dec 03, 2004 12:10 pm
Location: Germany

Post by Shine »

I don't know which pspsdk installation you are using, I'm using the source from svn. You can checkout it like this:

svn co svn://svn.ps2dev.org/psp/trunk/zlib
svn co svn://svn.ps2dev.org/psp/trunk/libpng
jason867
Posts: 78
Joined: Sun Jul 24, 2005 1:58 am
Contact:

Post by jason867 »

ok, I've installed everything and changed my code and all. But when I compile, it throughs a bunch of errors, which makes me think I need to include some header files or librarys. Would you happen to know which ones these are?
Ask not for whom the bell tolls, it tolls for thee, besides, I'm playing my PSP, tee hee!
------------------------------------------------------
Visit my website for my PSP Homebrew!
Shine
Posts: 728
Joined: Fri Dec 03, 2004 12:10 pm
Location: Germany

Post by Shine »

My crystall sphere is out of order. Compiler errors, linker errors? You have written "include <png.h>" in your file and changed the Makefile as I wrote?
jason867
Posts: 78
Joined: Sun Jul 24, 2005 1:58 am
Contact:

Post by jason867 »

Nice wisecrack, I figured out that I needed to include png.h, but it still threw some errors after typing make. I can't think of the exact wording at the moment, but it was to the effect of 'this is the first time so and so function has been declared' You know, the type of errors you get when you forget to include a header file or something. Oh, I remember one specific problem, in the arguments passed to the saveImage function, the second one (***,color* data,***) is giving problems because the datatype of 'color' hasn't been specified, which I presume is because a certain header wasn't included. I can't remember if this problem was fixed when I figured out that I needed to include png.h. But there were other simliar errors. What other files do I need to include besides png.h? zlib.h?
Ask not for whom the bell tolls, it tolls for thee, besides, I'm playing my PSP, tee hee!
------------------------------------------------------
Visit my website for my PSP Homebrew!
Shine
Posts: 728
Joined: Fri Dec 03, 2004 12:10 pm
Location: Germany

Post by Shine »

The easiest way would be to look at the graphics.c, but perhaps you forgot "#include <stdio.h>" for fopen? If you don't want to write the error messages, I can't help :-)

And you are right, Color is a typedef (see the graphics.h) and you can replace it with u16, if you are using a 16 bit format.
jason867
Posts: 78
Joined: Sun Jul 24, 2005 1:58 am
Contact:

Post by jason867 »

Owner@Pavilion1200 /usr/local/pspdev/psp/sdk/My_Homebrew/test
$ make
psp-gcc -I. -I/usr/local/pspdev/psp/sdk/include -O2 -G0 -Wall -c -o main.o mai
n.c
main.c: In function 'main':
main.c:81: warning: implicit declaration of function 'getVramDisplayBuffer'
main.c:81: warning: passing argument 2 of 'screenshot' makes pointer from intege
r without a cast
psp-gcc -I. -I/usr/local/pspdev/psp/sdk/include -O2 -G0 -Wall -L-lpng -L-lz -L.
-L/usr/local/pspdev/psp/sdk/lib main.o -lpspdebug -lpspdisplay -lpspge -lpsp
ctrl -lpspsdk -lc -lpspuser -lpspkernel -o test.elf
main.o: In function `screenshot':
main.c:(.text+0x4fc): undefined reference to `png_create_write_struct'
main.c:(.text+0x50c): undefined reference to `png_create_info_struct'
main.c:(.text+0x520): undefined reference to `png_init_io'
main.c:(.text+0x54c): undefined reference to `png_set_IHDR'
main.c:(.text+0x558): undefined reference to `png_write_info'
main.c:(.text+0x61c): undefined reference to `png_write_row'
main.c:(.text+0x638): undefined reference to `png_write_end'
main.c:(.text+0x644): undefined reference to `png_destroy_write_struct'
main.c:(.text+0x690): undefined reference to `png_destroy_write_struct'
main.o: In function `main':
main.c:(.text+0x84c): undefined reference to `getVramDisplayBuffer'
collect2: ld returned 1 exit status
make: *** [test.elf] Error 1

Owner@Pavilion1200 /usr/local/pspdev/psp/sdk/My_Homebrew/test
$

There's what I've gotten so far.
P.S. I changed the name of the function to 'screenshot' for my own purposes.
Ask not for whom the bell tolls, it tolls for thee, besides, I'm playing my PSP, tee hee!
------------------------------------------------------
Visit my website for my PSP Homebrew!
Shine
Posts: 728
Joined: Fri Dec 03, 2004 12:10 pm
Location: Germany

Post by Shine »

I quote myself:
In your Makefile you need to add "-lpng -lz" to your "LIBS" and you have to compile zlib and libpng (which is simply a "make" and "make install" in the directories).
and you have to provide getVramDisplayBuffer or use a constant.
jason867
Posts: 78
Joined: Sun Jul 24, 2005 1:58 am
Contact:

Post by jason867 »

I did that makefile stuff, and I did the make and make install's in both directories

By using a constant instead do you mean simply putting a pointer to the beginning of the VRAM there?

So, if I'm not double buffering, then I simply put the begginning address of VRAM at that spot, and it'll work? And if I do use double buffering, then I just need figure out how to switch to the other buffer to take screenshot from, right?
Ask not for whom the bell tolls, it tolls for thee, besides, I'm playing my PSP, tee hee!
------------------------------------------------------
Visit my website for my PSP Homebrew!
Shine
Posts: 728
Joined: Fri Dec 03, 2004 12:10 pm
Location: Germany

Post by Shine »

jason867 wrote:I did that makefile stuff, and I did the make and make install's in both directories
yes, you are right, it is another problem, because your compiler command line looks right, I didn't see the -lpng and -lz first. Perhaps you have an outdated PSPSDK.

Please check, if the /usr/local/pspdev/psp/lib/libpng.a is available and from today and "psp-gcc --version" says "psp-gcc (GCC) 4.0.1 (PSPDEV 20050729)" and rebuilt your psptoolchain, if it is older than a week. I don't have any more ideas, but in general PNG works, at least for Lua Player.

Sometimes there are problems with the order in your LIBS variable, for example in Lua Player it looks like this:

Code: Select all

LIBS= -llua -llualib -lpng -lz -lpspgu -lm -lmikmod -lmmio -lpspaudiolib -lpspaudio -lpspusb -lpspusbstor
jason867 wrote: By using a constant instead do you mean simply putting a pointer to the beginning of the VRAM there?
Yes, but using sceGeEdramGetAddr() is more clean, see the samples in the PSPSDK.
jason867 wrote: So, if I'm not double buffering, then I simply put the begginning address of VRAM at that spot, and it'll work? And if I do use double buffering, then I just need figure out how to switch to the other buffer to take screenshot from, right?
Yes.
jason867
Posts: 78
Joined: Sun Jul 24, 2005 1:58 am
Contact:

Post by jason867 »

I figured it out, I made a small mistake in the makefile. But now it works great! Thanks for everyone's help, especially shine's. If I remember right, this isn't the only thing you've helped me out on...

[Edit} I've now got nice clean screenshots of some of my programs for my website, tee hee!
Ask not for whom the bell tolls, it tolls for thee, besides, I'm playing my PSP, tee hee!
------------------------------------------------------
Visit my website for my PSP Homebrew!
OmahaStylee
Posts: 14
Joined: Mon Aug 29, 2005 5:00 pm
Location: Los Angeles, CA
Contact:

Source?

Post by OmahaStylee »

Can you post the completed source of your screen shot code?
Image
jason867
Posts: 78
Joined: Sun Jul 24, 2005 1:58 am
Contact:

Re: Source?

Post by jason867 »

OmahaStylee wrote:Can you post the completed source of your screen shot code?
Sure, here it is.

Code: Select all

void screenshot&#40;const char* filename&#41;
&#123; 
   png_structp png_ptr;
   png_infop info_ptr; 
   FILE* fp; 
   int i, x, y;
   u16* data = VRAM;
   int width=SCR_WIDTH;
   int height=SCR_HEIGHT;
   int linesize=BUF_WIDTH;
   int saveAlpha=0;
   u8* line;

   if &#40;&#40;fp = fopen&#40;filename, "wb"&#41;&#41; == NULL&#41; return;
   png_ptr = png_create_write_struct&#40;PNG_LIBPNG_VER_STRING, NULL, NULL, NULL&#41;; 
   if &#40;!png_ptr&#41; return;
   info_ptr = png_create_info_struct&#40;png_ptr&#41;; 
   if &#40;!info_ptr&#41; &#123; 
      png_destroy_write_struct&#40;&png_ptr, &#40;png_infopp&#41;NULL&#41;;
      return;
   &#125; 
   png_init_io&#40;png_ptr, fp&#41;;
   png_set_IHDR&#40;png_ptr, info_ptr, width, height, 8,
      saveAlpha ? PNG_COLOR_TYPE_RGBA &#58; PNG_COLOR_TYPE_RGB, 
      PNG_INTERLACE_NONE, PNG_COMPRESSION_TYPE_DEFAULT, PNG_FILTER_TYPE_DEFAULT&#41;; 
   png_write_info&#40;png_ptr, info_ptr&#41;; 
   line = &#40;u8*&#41; malloc&#40;width * &#40;saveAlpha ? 4 &#58; 3&#41;&#41;; 
   for &#40;y = 0; y < height; y++&#41; &#123; 
      for &#40;i = 0, x = 0; x < width; x++&#41; &#123; 
         u16 color = data&#91;x + y * linesize&#93;;
         int r = &#40;color & 0x1f&#41; << 3; 
         int g = &#40;&#40;color >> 5&#41; & 0x1f&#41; << 3 ; 
         int b = &#40;&#40;color >> 10&#41; & 0x1f&#41; << 3 ; 
         line&#91;i++&#93; = r; 
         line&#91;i++&#93; = g; 
         line&#91;i++&#93; = b; 
         if &#40;saveAlpha&#41; &#123; 
            int a = color & 0x8000 ? 0xff &#58; 0; 
            line&#91;i++&#93; = a; 
         &#125; 
      &#125; 
      png_write_row&#40;png_ptr, line&#41;; 
   &#125; 
   free&#40;line&#41;; 
   png_write_end&#40;png_ptr, info_ptr&#41;; 
   png_destroy_write_struct&#40;&png_ptr, &#40;png_infopp&#41;NULL&#41;; 
   fclose&#40;fp&#41;; 
&#125;

in your Cygwin Bash Shell (or whatever you use) enter the folowing lines;
svn co svn://svn.ps2dev.org/psp/trunk/zlib -- press [Enter] and wait for the prompt to show back up.
svn co svn://svn.ps2dev.org/psp/trunk/libpng -- press [Enter] and wait for the prompt to show back up.

[Edit] Those two lines up just above this look like links to websites, but they're not, so don't even bother clicking on them.

then you need to change your directory in the bash shell to your psptoolchain/zlib folder, and then type 'make' and then 'make install'
then change the directory over to your psptoolchain/libpng folder and type 'make' and 'make install' again.

You need to include png.h and you need to add the following to your makefile;

Code: Select all

LIBS = -lpng -lz
and then when you call the screenshot, make sure to type a valid file path, such as;

Code: Select all

screenshot&#40;"ms0&#58;/filepath.png"&#41;;
With filepath being your filepath. You have to have 'ms0:/' at the beginning and '.png' at the end. Once called you'll see your memory stick led flash several times, and then when you hook up your psp to your pc, you'll find your filepath.png screenshot file at the directory of your memory stick that you set it to write to.

Enjoy!

(Sorry if I got a little too in depth and detailed there, I get that ways sometimes)
Ask not for whom the bell tolls, it tolls for thee, besides, I'm playing my PSP, tee hee!
------------------------------------------------------
Visit my website for my PSP Homebrew!
OmahaStylee
Posts: 14
Joined: Mon Aug 29, 2005 5:00 pm
Location: Los Angeles, CA
Contact:

Arg

Post by OmahaStylee »

So its not working....

Here is the error its giving me..

Code: Select all

HP_Owner@Kenya ~/projects/helloworld
$ make kxploit
psp-gcc -I. -I/usr/local/pspdev/psp/sdk/include -O2 -G0 -Wall   -c -o main.o mai
n.c
main.c&#58; In function 'screenshot'&#58;
main.c&#58;55&#58; error&#58; 'VRAM' undeclared &#40;first use in this function&#41;
main.c&#58;55&#58; error&#58; &#40;Each undeclared identifier is reported only once
main.c&#58;55&#58; error&#58; for each function it appears in.&#41;
main.c&#58;56&#58; error&#58; 'SCR_WIDTH' undeclared &#40;first use in this function&#41;
main.c&#58;57&#58; error&#58; 'SCR_HEIGHT' undeclared &#40;first use in this function&#41;
main.c&#58;58&#58; error&#58; 'BUF_WIDTH' undeclared &#40;first use in this function&#41;
main.c&#58;75&#58; warning&#58; implicit declaration of function 'malloc'
main.c&#58;75&#58; warning&#58; incompatible implicit declaration of built-in function 'mall
oc'
main.c&#58;92&#58; warning&#58; implicit declaration of function 'free'
make&#58; *** &#91;main.o&#93; Error 1
Here is my Makefile

Code: Select all

TARGET = hello
OBJS = main.o

CFLAGS = -O2 -G0 -Wall
CXXFLAGS = $&#40;CFLAGS&#41; -fno-exceptions -fno-rtti
ASFLAGS = $&#40;CFLAGS&#41;

EXTRA_TARGETS = EBOOT.PBP
PSP_EBOOT_TITLE = Hello World

LIBS = -lpng -lz
PSPSDK=$&#40;shell psp-config --pspsdk-path&#41;
include $&#40;PSPSDK&#41;/lib/build.mak
And here is my source code...

Code: Select all

// Hello World - My First App for the PSP

/*
          This program was created by OmahaStylee on 8-4-05
          It is a simple "Hello World" Application.
*/

//Nescisary files with
#include <pspkernel.h>
#include <pspdebug.h>
#include <png.h>
#include <stdio.h>

PSP_MODULE_INFO&#40;"Hello World", 0, 1, 2&#41;;

// Rename to printf to make it easier
#define printf pspDebugScreenPrintf

// Nescisary files for exiting
 /* Exit callback */
int exit_callback&#40;int arg1, int arg2, void *common&#41; &#123;
          sceKernelExitGame&#40;&#41;;
          return 0;
&#125;

/* Callback thread */
int CallbackThread&#40;SceSize args, void *argp&#41; &#123;
          int cbid;

          cbid = sceKernelCreateCallback&#40;"Exit Callback", exit_callback, NULL&#41;;
          sceKernelRegisterExitCallback&#40;cbid&#41;;

          sceKernelSleepThreadCB&#40;&#41;;

          return 0;
&#125;

/* Sets up the callback thread and returns its thread id */
int SetupCallbacks&#40;void&#41; &#123;
          int thid = 0;

          thid = sceKernelCreateThread&#40;"update_thread", CallbackThread, 0x11, 0xFA0, 0, 0&#41;;
          if&#40;thid >= 0&#41; &#123;
                    sceKernelStartThread&#40;thid, 0, 0&#41;;
          &#125;

          return thid;
&#125;

void screenshot&#40;const char* filename&#41;
&#123;
   png_structp png_ptr;
   png_infop info_ptr;
   FILE* fp;
   int i, x, y;
   u16* data = VRAM;
   int width=SCR_WIDTH;
   int height=SCR_HEIGHT;
   int linesize=BUF_WIDTH;
   int saveAlpha=0;
   u8* line;

   if &#40;&#40;fp = fopen&#40;filename, "wb"&#41;&#41; == NULL&#41; return;
   png_ptr = png_create_write_struct&#40;PNG_LIBPNG_VER_STRING, NULL, NULL, NULL&#41;;
   if &#40;!png_ptr&#41; return;
   info_ptr = png_create_info_struct&#40;png_ptr&#41;;
   if &#40;!info_ptr&#41; &#123;
      png_destroy_write_struct&#40;&png_ptr, &#40;png_infopp&#41;NULL&#41;;
      return;
   &#125;
   png_init_io&#40;png_ptr, fp&#41;;
   png_set_IHDR&#40;png_ptr, info_ptr, width, height, 8,
      saveAlpha ? PNG_COLOR_TYPE_RGBA &#58; PNG_COLOR_TYPE_RGB,
      PNG_INTERLACE_NONE, PNG_COMPRESSION_TYPE_DEFAULT, PNG_FILTER_TYPE_DEFAULT&#41;;
   png_write_info&#40;png_ptr, info_ptr&#41;;
   line = &#40;u8*&#41; malloc&#40;width * &#40;saveAlpha ? 4 &#58; 3&#41;&#41;;
   for &#40;y = 0; y < height; y++&#41; &#123;
      for &#40;i = 0, x = 0; x < width; x++&#41; &#123;
         u16 color = data&#91;x + y * linesize&#93;;
         int r = &#40;color & 0x1f&#41; << 3;
         int g = &#40;&#40;color >> 5&#41; & 0x1f&#41; << 3 ;
         int b = &#40;&#40;color >> 10&#41; & 0x1f&#41; << 3 ;
         line&#91;i++&#93; = r;
         line&#91;i++&#93; = g;
         line&#91;i++&#93; = b;
         if &#40;saveAlpha&#41; &#123;
            int a = color & 0x8000 ? 0xff &#58; 0;
            line&#91;i++&#93; = a;
         &#125;
      &#125;
      png_write_row&#40;png_ptr, line&#41;;
   &#125;
   free&#40;line&#41;;
   png_write_end&#40;png_ptr, info_ptr&#41;;
   png_destroy_write_struct&#40;&png_ptr, &#40;png_infopp&#41;NULL&#41;;
   fclose&#40;fp&#41;;
&#125;

// Main function
int main&#40;&#41; &#123;

// Start the debug screen
pspDebugScreenInit&#40;&#41;;
SetupCallbacks&#40;&#41;;

// Print "Hello, world!"
printf&#40;"Hello World, Lets take a screen shot\n\n\n\n\n\n\n\n\n\n THIS IS A SCREEN SHOT"&#41;;
// Take screenshot
screenshot&#40;"ms0&#58;/screenshot.png"&#41;;
// Pause program until the home button is pressed
sceKernelSleepThread&#40;&#41;; 
return 0;
&#125;


Thanks for any and all help... Noobs have to start somewhere, lol.
Image
jason867
Posts: 78
Joined: Sun Jul 24, 2005 1:58 am
Contact:

Post by jason867 »

Ok, I don't have much time to elaborate right now, but I'll offer some quick advice. I'll be back on again later tonight.

'VRAM, SCR_WIDTH, SCR_HEIGHT, BUF_WIDTH' are all constants that I used in a graphics.h file that I made for my purposes. But I can tell you what they equal anyways.

"Just type this somewhere before it is used (such as near the beginning of your main.c file).
u16 *VRAM=(void *)(0x44000000);
This is where the screen buffer begins, so that the screenshot function knows where to look.

Put these very close by (above or below the VRAM part)
#define BUF_WIDTH (512)
#define SCR_WIDTH (480)
#define SCR_HEIGHT (272)

Instead of using the three #defines above, you could also just substitute their values in wherever they're needed. But, if you do that it'll be harder to change them to what they need to be if you ever change to a different video mode. (different video modes change the size of the screen in pixels)

As for the other errors, try including stdlib.h
#include <stdlib.h>

I think that might solve the errors. Let me know if it works or not.
Ask not for whom the bell tolls, it tolls for thee, besides, I'm playing my PSP, tee hee!
------------------------------------------------------
Visit my website for my PSP Homebrew!
OmahaStylee
Posts: 14
Joined: Mon Aug 29, 2005 5:00 pm
Location: Los Angeles, CA
Contact:

Almost

Post by OmahaStylee »

Okay, so I got it to compile, and run, and take the screen shot. But its coming out really weird looking.
http://img150.imageshack.us/my.php?image=toolkit7qo.png
Image
jason867
Posts: 78
Joined: Sun Jul 24, 2005 1:58 am
Contact:

Post by jason867 »

Try including this file.
#include <pspdisplay.h>

And then put this line somewhere at the beginning of main.c.
sceDisplaySetMode(1,SCR_WIDTH,SCR_HEIGHT);

Then do your thing and take the picture. I bet that fixes it.
Ask not for whom the bell tolls, it tolls for thee, besides, I'm playing my PSP, tee hee!
------------------------------------------------------
Visit my website for my PSP Homebrew!
OmahaStylee
Posts: 14
Joined: Mon Aug 29, 2005 5:00 pm
Location: Los Angeles, CA
Contact:

Post by OmahaStylee »

HP_Owner@Kenya ~/projects/toolkit
$ make kxploit
psp-gcc -I. -I/usr/local/pspdev/psp/sdk/include -O2 -G0 -Wall -c -o main.o mai
n.c
main.c:23: error: syntax error before numeric constant
main.c:23: warning: type defaults to 'int' in declaration of 'sceDisplaySetMode'

any ideas?
Image
Post Reply