Added 0.030 of PicoDrive and moved license files into root
[cyclone68000.git] / PicoDrive / Debug.cpp
1 \r
2 #include "stdafx.h"\r
3 \r
4 static int ScreenNum=0;\r
5 \r
6 int DebugScreenGrab()\r
7 {\r
8   unsigned char *screen=NULL;\r
9   FILE *file=NULL;\r
10   unsigned char *line=NULL;\r
11   int x=0,y=0;\r
12   char filename[64];\r
13   unsigned char head[0x12];\r
14 \r
15   memset(filename,0,sizeof(filename));\r
16   memset(head,0,sizeof(head));\r
17 \r
18   // Allocate memory for one line\r
19   line=(unsigned char *)malloc(GXDisp.cxWidth*3); if (line==NULL) return 1;\r
20   memset(line,0,GXDisp.cxWidth*3);\r
21 \r
22   // Get pointer to screen:\r
23   screen=(unsigned char *)GXBeginDraw(); if (screen==NULL) { free(line); return 1; }\r
24 \r
25   // Open screenshot file:\r
26   for (;;)\r
27   {\r
28     sprintf(filename,"\\Screen%.3d.tga",ScreenNum);\r
29 \r
30     // Does this file exist?\r
31     file=fopen(filename,"rb"); if (file==NULL) break; // No - use this\r
32     \r
33     // Exists, try next file\r
34     fclose(file); ScreenNum++;\r
35     if (ScreenNum>=1000) break;\r
36   }\r
37 \r
38   // Use this filename\r
39   file=fopen(filename,"wb"); if (file==NULL) { GXEndDraw(); free(line); return 1; }\r
40 \r
41   head[0x02]=0x02; //?\r
42   head[0x0c]=(unsigned char) GXDisp.cxWidth;\r
43   head[0x0d]=(unsigned char)(GXDisp.cxWidth>>8);\r
44   head[0x0e]=(unsigned char) GXDisp.cyHeight;\r
45   head[0x0f]=(unsigned char)(GXDisp.cyHeight>>8);\r
46   head[0x10]=24; // Number of bits per pixel\r
47 \r
48   // Write header:\r
49   fwrite(head,1,sizeof(head),file);\r
50 \r
51   for (y=0;y<(int)GXDisp.cyHeight;y++)\r
52   {\r
53     unsigned char *ps=NULL,*pd=NULL;\r
54     int ry=0;\r
55     int pix=0;\r
56 \r
57     ry=GXDisp.cyHeight-y-1;\r
58     ps=screen+ry*GXDisp.cbyPitch;\r
59     pd=line;\r
60 \r
61     // Copy pixel to our line buffer\r
62     for (x=0;x<(int)GXDisp.cxWidth; x++,ps+=GXDisp.cbxPitch,pd+=3)\r
63     {\r
64       pix=*(unsigned short *)ps;\r
65 \r
66       pd[0]=(unsigned char)((pix&0x001f)<<3); // Red\r
67       pd[1]=(unsigned char)((pix&0x07e0)>>3); // Green\r
68       pd[2]=(unsigned char)((pix&0xf800)>>8); // Blue\r
69     }\r
70 \r
71     fwrite(line,1,GXDisp.cxWidth*3,file);\r
72   }\r
73 \r
74   fclose(file); file=NULL;\r
75   \r
76   GXEndDraw();\r
77   free(line);\r
78 \r
79   return 0;\r
80 }\r