After several attempts I still am having problems. Below is the code that I am using.
Code: Select all
#include <pspkernel.h>
#include <pspctrl.h>
#include <pspdisplay.h>
#include <pspdebug.h>
#include <pspkernel.h>
#include <pspsdk.h>
#include <psprtc.h>
#include <string.h>
#include <stdio.h>
#include "graphics.h"
#include "system.h"
#define printf pspDebugScreenPrintf
/* ========================= START PSP CALLBACKS ========================= */
PSP_MODULE_INFO("Test",0,1,1);
PSP_MAIN_THREAD_ATTR(THREAD_ATTR_USER);//|THREAD_ATTR_VFPU); //User Mode
//PSP_MAIN_THREAD_ATTR(0); //Kernel Mode
//PSP_MAIN_THREAD_STACK_SIZE_KB(32);
/* Exit callback */
int exit_callback(int arg1,int arg2,void *common)
{
pspDveMgrSetVideoOut(0,0,480,272,1,15,0);
sceKernelDelayThread(1 * 1000 * 1000);
sceKernelExitGame();
return 0;
}
/* Callback thread */
int CallbackThread(SceSize args,void *argp)
{
int cbid;
cbid = sceKernelCreateCallback("Exit Callback",exit_callback,NULL);
sceKernelRegisterExitCallback(cbid);
sceKernelSleepThreadCB();
return 0;
}
/* Sets up the callback thread and returns its thread id */
int SetupCallbacks(void)
{
int thid = 0;
thid = sceKernelCreateThread("update_thread",CallbackThread,0x11,0xFA0,0,0);
if(thid >= 0)//
{
sceKernelStartThread(thid,0,0);
}
return thid;
}
/* ========================= END PSP CALLBACKS ========================= */
void intro()
{
int inum = 1,a,exit;
char pngfile[20];
Image* intro;
u32 introTickResolution;
u64 introTickNow;
u64 introTickLast;
SceCtrlData pad;
while(inum > 0)
{
exit = 0;
sprintf(pngfile,"intro/intro%i.png",inum);
if(FileExists(pngfile) == 1)
{
intro = loadImage(pngfile);
// Fade In
sceRtcGetCurrentTick(&introTickLast);
introTickResolution = sceRtcGetTickResolution();
a = 0;
while((a < 255) && (exit == 0))
{
sceCtrlReadBufferPositive(&pad,1);
if(pad.Buttons & (PSP_CTRL_START|PSP_CTRL_CROSS|PSP_CTRL_CIRCLE)) { exit = 1; break; }
sceRtcGetCurrentTick(&introTickNow);
a = ((introTickNow - introTickLast) / ((float)introTickResolution)) * 255;
if(a < 0) { a = 0; }
if(a > 255) { a = 255; }
drawImagePart(intro,0,0,0,0,intro->w,intro->h,720,480,0,0);
ScreenFade(0,0,0,255 - a);
flipScreen();
}
// Pause Intro Logo
sceRtcGetCurrentTick(&introTickLast);
introTickResolution = sceRtcGetTickResolution();
a = 0;
while((a < 767) && (exit == 0))
{
sceCtrlReadBufferPositive(&pad,1);
if(pad.Buttons & (PSP_CTRL_START|PSP_CTRL_CROSS|PSP_CTRL_CIRCLE)) { exit = 1; break; }
sceRtcGetCurrentTick(&introTickNow);
a = ((introTickNow - introTickLast) / ((float)introTickResolution)) * 255;
if(a < 0) { a = 0; }
}
// Fade Out
sceRtcGetCurrentTick(&introTickLast);
introTickResolution = sceRtcGetTickResolution();
a = 0;
while((a < 255) && (exit == 0))
{
sceCtrlReadBufferPositive(&pad,1);
if(pad.Buttons & (PSP_CTRL_START|PSP_CTRL_CROSS|PSP_CTRL_CIRCLE)) { exit = 1; break; }
sceRtcGetCurrentTick(&introTickNow);
a = ((introTickNow - introTickLast) / ((float)introTickResolution)) * 255;
if(a < 0) { a = 0; }
if(a > 255) { a = 255; }
drawImagePart(intro,0,0,0,0,intro->w,intro->h,720,480,0,0);
ScreenFade(0,0,0,a);
flipScreen();
}
freeImage(intro);
}
else
{
inum = -1;
}
inum++;
}
}
int main(int argc,char **argv)
{
SceCtrlData pad;
pspDebugScreenInit();
SetupCallbacks();
SceUID mod = pspSdkLoadStartModule("dvemgr.prx",PSP_MEMORY_PARTITION_KERNEL);
if(mod >= 0)
{
printf("X : Component (Progressive)\n");
printf("O : Component (Interlaced)\n");
printf("[]: Composite\n");
printf("/\\: PSP LCD\n");
while(1)
{
sceCtrlReadBufferPositive(&pad,1);
if(pad.Buttons & PSP_CTRL_CROSS) { setVideoMode(3); break; }
if(pad.Buttons & PSP_CTRL_CIRCLE) { setVideoMode(2); break; }
if(pad.Buttons & PSP_CTRL_SQUARE) { setVideoMode(1); break; }
if(pad.Buttons & PSP_CTRL_TRIANGLE) { setVideoMode(0); break; }
if(pad.Buttons & PSP_CTRL_RTRIGGER) { break; }
}
}
initGraphics();
intro();
// while(1)
// {
//
// }
pspDveMgrSetVideoOut(0,0,480,272,1,15,0);
sceKernelDelayThread(1 * 1000 * 1000);
sceKernelExitGame();
return 0;
}
Code: Select all
#include <string.h>
#include <stdlib.h>
#include <pspdisplay.h>
#include <pspgu.h>
#include <pspgum.h>
#include <pspkernel.h>
#include <png.h>
#include "graphics.h"
unsigned int __attribute__((aligned(16))) dList[262144];
int dispNumber;
u32 *vramBase = (u32*)(0x40000000|0x04000000);
void *fbp0 = NULL;
void *fbp1;
void *zbp;
int SCREEN_WIDTH = 480;
int SCREEN_HEIGHT = 272;
int BUFFER_WIDTH = 512;
int interlaced = 0;
int TVMode = 0;
int SCREEN_TOP = 0;
int SCREEN_BOTTOM = 0;
int SCREEN_LEFT = 0;
int SCREEN_RIGHT = 0;
void initGraphics()
{
dispNumber = 0;
fbp0 = (void*)0;
fbp1 = (void*)(BUFFER_WIDTH * SCREEN_HEIGHT * 4);
zbp = (void*)(BUFFER_WIDTH * SCREEN_HEIGHT * 8);
sceGuInit();
sceGuStart(GU_DIRECT,dList);
if(interlaced == 1)
{
sceGuDrawBuffer(GU_PSM_8888,fbp0,BUFFER_WIDTH);
sceGuDispBuffer(SCREEN_WIDTH,SCREEN_HEIGHT,fbp0,BUFFER_WIDTH);
sceGuDepthBuffer(fbp1,BUFFER_WIDTH);
}
else
{
sceGuDrawBuffer(GU_PSM_8888,fbp0,BUFFER_WIDTH);
sceGuDispBuffer(SCREEN_WIDTH,SCREEN_HEIGHT,fbp1,BUFFER_WIDTH);
sceGuDepthBuffer(zbp,BUFFER_WIDTH);
}
sceGuOffset(2048 - ((SCREEN_WIDTH - SCREEN_LEFT - SCREEN_RIGHT)/2),2048 - ((SCREEN_HEIGHT - SCREEN_TOP - SCREEN_BOTTOM)/2));
sceGuViewport(2048 + SCREEN_LEFT,2048 + SCREEN_TOP,SCREEN_WIDTH - SCREEN_LEFT - SCREEN_RIGHT,SCREEN_HEIGHT - SCREEN_TOP - SCREEN_BOTTOM);
sceGuDepthRange(65535,0);
sceGuScissor(0,0,SCREEN_WIDTH,SCREEN_HEIGHT);
sceGuEnable(GU_SCISSOR_TEST);
sceGuFrontFace(GU_CCW);
sceGuShadeModel(GU_SMOOTH);
sceGuAlphaFunc(GU_GREATER,0,0xff);
sceGuEnable(GU_CULL_FACE);
sceGuEnable(GU_DEPTH_TEST);
sceGuDepthFunc(GU_GEQUAL);
sceGuEnable(GU_ALPHA_TEST);
sceGuEnable(GU_TEXTURE_2D);
//sceGuEnable(GU_CLIP_PLANES);
sceGuEnable(GU_BLEND);
sceGuBlendFunc(GU_ADD,GU_SRC_ALPHA,GU_ONE_MINUS_SRC_ALPHA,0,0);
sceGuTexMode(GU_PSM_8888,0,0,1);
sceGuTexFunc(GU_TFX_REPLACE,GU_TCC_RGBA);
sceGuTexFilter(GU_LINEAR,GU_LINEAR);
sceGuTexWrap(GU_CLAMP,GU_CLAMP);
sceGuTexScale(1,1);
sceGuTexOffset(0,0);
sceGuAmbientColor(0xffffffff);
sceGuFinish();
sceGuSync(0,0);
sceDisplayWaitVblankStart();
sceGuDisplay(1);
if(TVMode > 0)
{
// sceDisplaySetFrameBuf((void*)0x44200000,BUFFER_WIDTH,PSP_DISPLAY_PIXEL_FORMAT_8888,PSP_DISPLAY_SETBUF_IMMEDIATE);
sceDisplaySetFrameBuf((void*)0x44200000,BUFFER_WIDTH,PSP_DISPLAY_PIXEL_FORMAT_8888,PSP_DISPLAY_SETBUF_NEXTFRAME);
}
sceKernelDcacheWritebackInvalidateAll();
sceGuStart(GU_DIRECT,dList);
sceGuClearColor(0);
sceGuClearDepth(0);
sceGuClear(GU_COLOR_BUFFER_BIT|GU_DEPTH_BUFFER_BIT);
sceGumMatrixMode(GU_PROJECTION);
sceGumLoadIdentity();
sceGumOrtho(0,720,480,0,-1,1);
sceGumMatrixMode(GU_VIEW);
sceGumLoadIdentity();
sceGumMatrixMode(GU_MODEL);
sceGumLoadIdentity();
}
void endGraphics()
{
sceGuTerm();
}
void setVideoMode(int mode)
{
TVMode = mode;
if(TVMode > 0)
{
SCREEN_TOP = 0; SCREEN_BOTTOM = 0; SCREEN_LEFT = 0; SCREEN_RIGHT = 0;
// SCREEN_TOP = 104; SCREEN_BOTTOM = 104; SCREEN_LEFT = 120; SCREEN_RIGHT = 120; // Test
}
else
{
SCREEN_TOP = 0; SCREEN_BOTTOM = 0; SCREEN_LEFT = 0; SCREEN_RIGHT = 0;
}
switch(mode)
{
case 3:
pspDveMgrSetVideoOut(0,0x1d2,720,480,1,15,0);
SCREEN_WIDTH = 720;
SCREEN_HEIGHT = 480;
BUFFER_WIDTH = 768;
interlaced = 0;
break;
case 2:
pspDveMgrSetVideoOut(0,0x1d1,720,503,1,15,0);
SCREEN_WIDTH = 720;
SCREEN_HEIGHT = 480;
BUFFER_WIDTH = 768;
interlaced = 1;
break;
case 1:
pspDveMgrSetVideoOut(2,0x1d1,720,503,1,15,0);
SCREEN_WIDTH = 720;
SCREEN_HEIGHT = 480;
BUFFER_WIDTH = 768;
interlaced = 1;
break;
case 0:
default:
pspDveMgrSetVideoOut(0,0,480,272,1,15,0);
SCREEN_WIDTH = 480;
SCREEN_HEIGHT = 272;
BUFFER_WIDTH = 512;
interlaced = 0;
break;
}
}
void flipScreen()
{
sceGuFinish();
sceGuSync(0,0);
if(interlaced)
{
sceKernelDcacheWritebackInvalidateAll();
sceGuStart(GU_DIRECT,dList);
sceDisplayWaitVblankStart();
sceGuCopyImage(GU_PSM_8888,0,0,SCREEN_WIDTH,SCREEN_HEIGHT>>1,BUFFER_WIDTH<<1,(void*)0x44000C00,0,0,BUFFER_WIDTH,(void*)0x44200000);
sceGuTexSync();
sceGuCopyImage(GU_PSM_8888,0,0,SCREEN_WIDTH,SCREEN_HEIGHT>>1,BUFFER_WIDTH<<1,(void*)0x44000000,0,0,BUFFER_WIDTH,(void*)0x442C4800);
sceGuTexSync();
sceGuFinish();
sceGuSync(0,0);
}
else
{
sceDisplayWaitVblankStart();
fbp0 = sceGuSwapBuffers();
dispNumber ^= 1;
}
sceKernelDcacheWritebackInvalidateAll();
sceGuStart(GU_DIRECT,dList);
sceGumMatrixMode(GU_MODEL);
sceGumLoadIdentity();
}
void clearScreen()
{
sceGuClearColor(0);
sceGuClearDepth(0);
sceGuClear(GU_COLOR_BUFFER_BIT|GU_DEPTH_BUFFER_BIT);
}
void freeImage(Image* image)
{
free(image->data);
free(image);
}
static int Power2(int dimension)
{
int b = dimension;
int n;
for(n = 0;b != 0;n++) b >>= 1;
b = 1 << n;
if(b == 2 * dimension) b >>= 1;
return b;
}
void swizzleImage(Image* image)
{
unsigned int pitch = image->tW * image->colorMode;
unsigned int i,j;
unsigned int rowblocks = (pitch / 16);
long size = pitch * image->tH;
unsigned char* out = (unsigned char*)malloc(size * sizeof(unsigned char));
for(j = 0;j < image->tH;j++)
{
for(i = 0;i < pitch;i++)
{
unsigned int blockx = i / 16;
unsigned int blocky = j / 8;
unsigned int x = (i - blockx * 16);
unsigned int y = (j - blocky * 8);
unsigned int block_index = blockx + ((blocky) * rowblocks);
unsigned int block_address = block_index * 16 * 8;
out[block_address + x + y * 16] = image->data[i + j * pitch];
}
}
free(image->data);
image->data = out;
}
void unswizzleImage(Image* image)
{
unsigned int pitch = image->tW * image->colorMode;
unsigned int i,j;
unsigned int rowblocks = (pitch / 16);
long size = image->tW * image->tH;
unsigned char* out = (unsigned char*)malloc(size);
for(j = 0;j < image->h;j++)
{
for(i = 0;i < pitch;i++)
{
unsigned int blockx = i / 16;
unsigned int blocky = j / 8;
unsigned int x = (i - blockx * 16);
unsigned int y = (j - blocky * 8);
unsigned int block_index = blockx + ((blocky) * rowblocks);
unsigned int block_address = block_index * 16 * 8;
out[i+j*pitch] = image->data[block_address + x + y * 16];
}
}
free(image->data);
image->data = out;
}
Image* createImage(int width,int height)
{
Image* image = (Image*)malloc(sizeof(Image));
if(!image) return NULL;
image->w = width;
image->h = height;
image->tW = Power2(width);
image->tH = Power2(height);
image->data = (unsigned char*)malloc(image->tW * image->tH * sizeof(u32));
if(!image->data) return NULL;
memset(image->data,0,image->tW * image->tH * sizeof(u32));
return image;
}
void clearImage(Image* image,u32 color)
{
int i;
int size = image->tW * image->tH;
u32* data = (u32*)image->data;
for(i = 0;i < size;i++,data++) *data = color;
}
Image* loadImage(const char fileName[])
{
Image* image = NULL;
png_structp pngPtr;
png_infop infoPtr;
unsigned int sigRead = 0;
png_uint_32 width,height;
int bitDepth,colorType,interlaceType,x,y;
u32* line;
FILE *fileIn;
fileIn = fopen(fileName,"rb");
if(fileIn == NULL) return NULL;
pngPtr = png_create_read_struct(PNG_LIBPNG_VER_STRING,NULL,NULL,NULL);
if(pngPtr == NULL) { fclose(fileIn); return NULL; }
png_set_error_fn(pngPtr,(png_voidp)NULL,(png_error_ptr)NULL,NULL);
infoPtr = png_create_info_struct(pngPtr);
if(infoPtr == NULL)
{
fclose(fileIn);
png_destroy_read_struct(&pngPtr,png_infopp_NULL,png_infopp_NULL);
return NULL;
}
png_init_io(pngPtr,fileIn);
png_set_sig_bytes(pngPtr,sigRead);
png_read_info(pngPtr,infoPtr);
png_get_IHDR(pngPtr,infoPtr,&width,&height,&bitDepth,&colorType,&interlaceType,int_p_NULL,int_p_NULL);
if(width > 512 || height > 512)
{
fclose(fileIn);
png_destroy_read_struct(&pngPtr,png_infopp_NULL,png_infopp_NULL);
return NULL;
}
image = (Image*)malloc(sizeof(Image));
if(image == NULL) return NULL;
image->w = width;
image->h = height;
image->tW = Power2(width);
image->tH = Power2(height);
png_set_strip_16(pngPtr);
png_set_packing(pngPtr);
if(colorType == PNG_COLOR_TYPE_PALETTE) png_set_palette_to_rgb(pngPtr);
if(colorType == PNG_COLOR_TYPE_GRAY && bitDepth < 8) png_set_gray_1_2_4_to_8(pngPtr);
if(png_get_valid(pngPtr,infoPtr,PNG_INFO_tRNS)) png_set_tRNS_to_alpha(pngPtr);
png_set_filler(pngPtr,0xff,PNG_FILLER_AFTER);
image->data = (unsigned char*)malloc(image->tW * image->tH * sizeof(u32));
if(!image->data)
{
fclose(fileIn);
png_destroy_read_struct(&pngPtr,png_infopp_NULL,png_infopp_NULL);
free(image);
return NULL;
}
line = (u32*)malloc(width * 4);
if(!line)
{
fclose(fileIn);
png_destroy_read_struct(&pngPtr,png_infopp_NULL,png_infopp_NULL);
return NULL;
}
u32* data = (u32*)image->data;
for(y = 0;y < height;y++)
{
png_read_row(pngPtr,(u8*)line,png_bytep_NULL);
for(x = 0;x < width;x++)
{
u32 color = line[x];
data[x + y * image->tW] = color;
}
}
image->colorMode = 4;
free(line);
png_read_end(pngPtr,infoPtr);
png_destroy_read_struct(&pngPtr,&infoPtr,png_infopp_NULL);
fclose(fileIn);
swizzleImage(image);
return image;
}
void drawImagePart(Image* image,int sx,int sy,int dx,int dy,int sw,int sh,int dw,int dh,int flip,float rads)
{
Vertex *v = (Vertex*)sceGuGetMemory(4 * sizeof(Vertex));
float l,t,r,b;
sceGumPushMatrix();
{
ScePspFVector3 pos={ dx + dw / 2,dy + dh / 2,0};
sceGumTranslate(&pos);
sceGumRotateZ(rads);
if((image->tW <= 512) && (image->tH <= 512))
{
l = (float)(sx) / (float)(image->tW);
t = (float)(sy) / (float)(image->tH);
r = (float)(sx+sw) / (float)(image->tW);
b = (float)(sy+sh) / (float)(image->tH);
sceGuTexImage(0,image->tW,image->tH,image->tW,image->data);
}
else
{
l = (float)(sx % 16) / 512;
t = (float)(sy % 16) / 512;
r = (float)(sw + (sx % 16)) / 512;
b = (float)(sh + (sy % 16)) / 512;
sceGuTexImage(0,512,512,image->tW,image->data + ((sx / 16) + ((image->tW / 16) * (sy / 8))) * 128);
}
switch(flip)
{
case 3: // HV Flip
v[0].u = r; v[0].v = b;
v[1].u = r; v[1].v = t;
v[2].u = l; v[2].v = b;
v[3].u = l; v[3].v = t;
break;
case 2: // V Flip
v[0].u = l; v[0].v = b;
v[1].u = l; v[1].v = t;
v[2].u = r; v[2].v = b;
v[3].u = r; v[3].v = t;
break;;
case 1: // H Flip
v[0].u = r; v[0].v = t;
v[1].u = r; v[1].v = b;
v[2].u = l; v[2].v = t;
v[3].u = l; v[3].v = b;
break;
case 0: // No Flip
default:
v[0].u = l; v[0].v = t;
v[1].u = l; v[1].v = b;
v[2].u = r; v[2].v = t;
v[3].u = r; v[3].v = b;
break;
}
v[0].color = 0; v[0].x = -dw / 2; v[0].y = -dh / 2; v[0].z = 0;
v[1].color = 0; v[1].x = -dw / 2; v[1].y = dh / 2; v[1].z = 0;
v[2].color = 0; v[2].x = dw / 2; v[2].y = -dh / 2; v[2].z = 0;
v[3].color = 0; v[3].x = dw / 2; v[3].y = dh / 2; v[3].z = 0;
sceGumDrawArray(GU_TRIANGLE_STRIP,GU_COLOR_8888|GU_TEXTURE_32BITF|GU_VERTEX_32BITF|GU_TRANSFORM_3D,4,0,v);
}
sceGumPopMatrix();
}
void ScreenFade(u8 r,u8 g,u8 b,u8 a)
{
sceGuDisable(GU_DEPTH_TEST);
sceGuDisable(GU_TEXTURE_2D);
sceGumPushMatrix();
{
Vertexx* v = (Vertexx*)sceGuGetMemory(2 * sizeof(Vertexx));
v[0].color = GU_RGBA(r,g,b,a); v[0].x = 0; v[0].y = 0; v[0].z = 0;
v[1].color = GU_RGBA(r,g,b,a); v[1].x = 720; v[1].y = 480; v[1].z = 0;
sceGumDrawArray(GU_SPRITES,GU_COLOR_8888|GU_VERTEX_32BITF|GU_TRANSFORM_3D,2,0,v);
}
sceGumPopMatrix();
sceGuEnable(GU_TEXTURE_2D);
sceGuEnable(GU_DEPTH_TEST);
}
Code: Select all
#ifndef GRAPHICS_H
#define GRAPHICS_H
#include <psptypes.h>
#define RGB(r,g,b) ((r) | ((g)<<8) | ((b)<<16) | (0xff<<24))
#define RGBA(r,g,b,a) ((r) | ((g)<<8) | ((b)<<16) | ((a)<<24))
extern int SCREEN_WIDTH;
extern int SCREEN_HEIGHT;
extern int BUFFER_WIDTH;
extern int interlaced;
extern int TVMode;
typedef struct
{
float u,v;
unsigned int color;
float x,y,z;
} Vertex;
typedef struct
{
unsigned int color;
float x,y,z;
} Vertexx;
typedef struct
{
int w,h,tW,tH;
int colorMode;
unsigned char* data;
} Image;
int pspDveMgrCheckVideoOut();
int pspDveMgrSetVideoOut(int,int,int,int,int,int,int);
void initGraphics();
void endGraphics();
void setVideoMode(int mode);
void flipScreen();
void clearScreen();
void freeImage(Image* image);
Image* loadImage(const char fileName[]);
Image* createImage(int width,int height);
void clearImage(Image* image,u32 color);
void drawImagePart(Image* image,int sx,int sy,int dx,int dy,int sw,int sh,int dw,int dh,int flip,float rads);
void ScreenFade(u8 r,u8 g,u8 b,u8 a);
#endif