rendered paste body/**********************************************
* CrashTV for the Sony PSP System *
* *
* Coded by harleyg@linuxlabs.co.uk *
**********************************************/
// Includes
#include <pspdisplay.h>
#include <pspctrl.h>
#include <pspkernel.h>
#include <pspdebug.h>
#include <pspaudio.h>
#include <pspgu.h>
#include <png.h>
#include <stdio.h>
#include <math.h>
#include "screenshot.c"
#include "graphics.h"
#include "mp3player.h"
// Defines
#define printf pspDebugScreenPrintf
#define MAX(X, Y) ((X) > (Y) ? (X) : (Y))
#define RGB(r, g, b) (0xFF000000 | ((b)<<16) | ((g)<<8) | (r))
// PSP module information
PSP_MODULE_INFO("CrashTV v0.1 PRE", 0, 1, 1);
// PSP callbacks etcetc
int exit_callback(int arg1, int arg2, void *common) {
sceKernelExitGame();
return 0;
}
int CallbackThread(SceSize args, void *argp) {
int cbid;
cbid = sceKernelCreateCallback("Exit Callback", exit_callback, NULL);
sceKernelRegisterExitCallback(cbid);
sceKernelSleepThreadCB();
return 0;
}
int SetupCallbacks(void) {
int thid = 0;
thid = sceKernelCreateThread("update_thread", CallbackThread, 0x11, 0xFA0, 0, 0);
if(thid >= 0) {
sceKernelStartThread(thid, 0, 0);
}
return thid;
}
// Collision and collider - by skylark
struct rect *colliders;
typedef struct rect {
int X,Y,W,H;
struct rect *next;
} rect;
void new_collider(int x, int y, int w, int h) {
struct rect *p=malloc(sizeof(struct rect));
p->X=x;
p->Y=y;
p->W=w;
p->H=h;
p->next=colliders;
colliders=p;
}
int last_x, last_y;
int do_collider(int *x, int *y, struct rect *r) {
int spritewidth = 14, spriteheight = 21;
if(*x+spritewidth < r->X ||
*y+spriteheight < r->Y ||
*x > r->X+r->W ||
*y > r->Y+r->H)
return 0;
*x=last_x;
*y=last_y;
return 1;
}
void do_all_colliders(int *x, int *y) {
struct rect *i;
for(i=colliders;i;i=i->next)
do_collider(x,y,i);
last_x=*x;
last_y=*y;
}
// Fading effects and such
void set_image_alpha(Image *image, unsigned char alpha) {
int i, size;
Color *pixel;
unsigned int alpha_mod;
if(!image || !image->data)
return;
size = image->textureWidth * image->textureHeight;
pixel = image->data;
if(alpha > 100) alpha = 100;
alpha_mod = ((alpha * 255) / 100) << 24;
for(i = 0; i <= size; i++) {
*pixel = (*pixel & 0x00ffffff) | alpha_mod;
pixel++;
}
}
void image_fade(Image *pic, int fade_rate, int timeout, int x, int y) {
int i, delay_time;
delay_time = 40000;
timeout = (timeout * 1000000) / delay_time;
for(i = 0; i <= 100; i += fade_rate) {
clearScreen(0);
set_image_alpha(pic, i);
blitAlphaImageToScreen(0, 0, pic->imageWidth, pic->imageHeight,
pic, x, y);
flipScreen();
}
sceKernelDelayThread(delay_time);
for(i = 100; i >= 0; i -= fade_rate) {
clearScreen(0);
set_image_alpha(pic, i);
blitAlphaImageToScreen(0, 0, pic->imageWidth, pic->imageHeight,
pic, x, y);
flipScreen();
}
sceKernelDelayThread(delay_time);
}
void image_fadein(Image *pic, int fade_rate, int timeout, int x, int y) {
int i, delay_time;
delay_time = 40000;
timeout = (timeout * 1000000) / delay_time;
for(i = 0; i <= 100; i += fade_rate) {
clearScreen(0);
set_image_alpha(pic, i);
blitAlphaImageToScreen(0, 0, pic->imageWidth, pic->imageHeight,
pic, x, y);
flipScreen();
}
sceKernelDelayThread(delay_time);
}
void image_fadeout(Image *pic, int fade_rate, int timeout, int x, int y) {
int i, delay_time;
delay_time = 40000;
timeout = (timeout * 1000000) / delay_time;
for(i = 100; i >= 0; i -= fade_rate) {
clearScreen(0);
set_image_alpha(pic, i);
blitAlphaImageToScreen(0, 0, pic->imageWidth, pic->imageHeight,
pic, x, y);
flipScreen();
}
sceKernelDelayThread(delay_time);
}
// Main Function
int main() {
// Image char's
char mapbuffer[200];
char animationbuffer[800];
char introbuffer[200];
char logobuffer[200];
char pspdevintrobuffer[200];
char pspprogrammingbuffer[200];
// Image Variable's
Image* map;
Image* animation;
Image* intro;
Image* logo;
Image* pspdevintro;
Image* pspprogramming;
// Init's
pspDebugScreenInit();
SetupCallbacks();
pspAudioInit();
initGraphics();
// Load image's into buffer
sprintf(pspdevintrobuffer,"ms0:/crashtv/img/pspdev.png");
pspdevintro = loadImage(pspdevintrobuffer);
sprintf(pspprogrammingbuffer,"ms0:/crashtv/img/psp-programming.png");
pspprogramming = loadImage(pspprogrammingbuffer);
sprintf(introbuffer,"ms0:/crashtv/img/intro.png");
intro = loadImage(introbuffer);
sprintf(logobuffer,"ms0:/crashtv/img/logo.png");
logo = loadImage(logobuffer);
sprintf(mapbuffer,"ms0:/crashtv/img/map.png");
map = loadImage(mapbuffer);
sprintf(animationbuffer,"ms0:/crashtv/img/animation.png");
animation = loadImage(animationbuffer);
// Check image's loaded correctly
if (!logo || !pspdevintro || !map || !animation || !pspprogramming) {
printf("CrashTV: Error, an image cannot load.\nPlease check the ms0:/crashtv directory\n\nUse the HOME button to exit.");
} else {
// Variables
// Sprite start location
int spritex=240, spritey=136;
// Init controls
SceCtrlData pad;
// Screen locations
int topBound = 0;
int bottomBound = 272;
int leftBound = 0;
int rightBound = 480;
// Variables for wall collision detector
int topwallx = 0, topwally = 0;
int topwallw = 480, topwallh = 25;
int bottomwallx = 0, bottomwally = 257;
int bottomwallw = 480, bottomwallh = 15;
int leftwallx = 0, leftwally = 0;
int leftwallw = 30, leftwallh = 272;
int rightwallx = 430, rightwally = 0;
int rightwallw = 40, rightwallh = 272;
// Sprite width and height
int spriteheight = 21;
int spritewidth = 14;
// Default direction declaration
int dir = 0;
// Speed
int speed = 6;
// Last direction variable
int lastDir;
// Default sprite animation variables
int frames[11];
int anim_ctr = 0;
int i;
frames[0] = 126;
frames[1] = 140;
frames[2] = 154;
frames[3] = 42;
frames[4] = 56;
frames[5] = 70;
frames[6] = 84;
frames[7] = 98;
frames[8] = 112;
frames[9] = 0;
frames[10] = 14;
frames[11] = 28;
int u = 1;
MP3_Init(1);
MP3_Load("intro.mp3");
image_fade(pspdevintro, 2, 100, 0, 0);
image_fade(pspprogramming, 2, 100, 0, 0);
while(u) {
sceCtrlReadBufferPositive(&pad, 1);
if(pad.Buttons & PSP_CTRL_SELECT) {
screenshot("ms0:/screenshot.png");
}
blitAlphaImageToScreen(0, 0, 480, 272, intro, 0, 0);
blitAlphaImageToScreen(0, 0, 252, 144, logo, 115, 75);
flipScreen();
MP3_Play();
if(pad.Buttons & PSP_CTRL_CROSS) {
u = 0;
}
if (MP3_EndOfStream() == 1) {
MP3_Stop();
}
}
MP3_Stop();
MP3_FreeTune();
freeImage(intro);
freeImage(logo);
freeImage(pspdevintro);
// Setup the walls and other colliders
new_collider(topwallx, topwally, topwallw, topwallh); // Top Wall
new_collider(bottomwallx, bottomwally, bottomwallw, bottomwallh); // Bottom Wall
new_collider(leftwallx, leftwally, leftwallw, leftwallh); // Left Wall
new_collider(rightwallx, rightwally, rightwallw, rightwallh); // Right Wall
while(1) {
sceCtrlReadBufferPositive(&pad, 1);
dir = 4;
if(pad.Buttons & PSP_CTRL_UP) {
dir = 0;
} else if(pad.Buttons & PSP_CTRL_RIGHT) {
dir = 1;
} else if(pad.Buttons & PSP_CTRL_DOWN) {
dir = 2;
} else if(pad.Buttons & PSP_CTRL_LEFT) {
dir = 3;
}
switch(dir) {
case 0:
do_all_colliders(&spritex,&spritey);
if(spritey >= topBound + speed) {
spritey -= speed;
anim_ctr++;
if(anim_ctr<0 || anim_ctr>2) {
anim_ctr = 0;
}
for(i=0;i<20;i++) sceKernelDelayThread(5000);
break;
}
case 1:
do_all_colliders(&spritex,&spritey);
if(spritex <= rightBound - speed - spritewidth) {
spritex += speed;
anim_ctr++;
if(anim_ctr<3 || anim_ctr>5) {
anim_ctr = 3;
}
for(i=0;i<20;i++) sceKernelDelayThread(5000);
break;
}
case 2:
do_all_colliders(&spritex,&spritey);
if(spritey <= bottomBound - speed - spritewidth) {
spritey += speed;
anim_ctr++;
if(anim_ctr<6 || anim_ctr>8) {
anim_ctr = 6;
}
for(i=0;i<20;i++) sceKernelDelayThread(5000);
break;
}
case 3:
do_all_colliders(&spritex,&spritey);
if(spritex >= leftBound + speed) {
spritex -= speed;
anim_ctr++;
if(anim_ctr<9 || anim_ctr>11) {
anim_ctr = 9;
}
for(i=0;i<20;i++) sceKernelDelayThread(5000);
break;
}
case 4:
break;
default:
printf("ERROR: DIRECTION OUTSIDE OF RANGE");
while(1) {
sceDisplayWaitVblankStart();
}
break;
}
if(dir<4) {
lastDir = dir;
}
clearScreen(RGB(255, 255, 255));
blitAlphaImageToScreen(0, 0, 480, 272, map, 0, 0);
blitAlphaImageToScreen(frames[anim_ctr], 0, spritewidth, spriteheight, animation, spritex, spritey);
flipScreen();
}
}
sceKernelSleepThread();
return 0;
}