Go Back   Game Tagg Forums - PSP Games, PSP Downloads, PSP News, and PSP Themes > Playstation Chat > PSP Homebrew Discussion

Reply
 
LinkBack Thread Tools
Old 04-12-2006   #1 (permalink)
GamingCrazy Novice
 
Join Date: Nov 2005
Posts: 215
Credits: 318
Send a message via AIM to Slasher
C Snippets/Help Section

So...your new to coding and you don't know where to start? Some great tutorials by 'Yeldarb' have been written for people in your position. His tutorials cover everything from setting up your development environment, right into displaying your first image on-screen. His tutorials even go into enough depth as to show you how to get MP3s running within your games!

His tutorials can be seen on the following pages:
[Only registered and activated users can see links. ]
[Only registered and activated users can see links. ]
[Only registered and activated users can see links. ]
[Only registered and activated users can see links. ]
[Only registered and activated users can see links. ]
[Only registered and activated users can see links. ]



And now...some snippets of code in C you may find useful:
(more to come)

USB MODE
By: Slasher

Headers:
Code:
 
#include <pspkernel.h>
#include <pspdisplay.h>
#include <pspusb.h>
#include <pspusbstor.h>
#include <string.h>
Functions:
Code:
 
// Defines
#define     printf     pspDebugScreenPrintf

// Functions
int LoadStartModule(char *path) {
     u32 loadResult;
     u32 startResult;
     int status;
     loadResult = sceKernelLoadModule(path, 0, NULL);
 
     if (loadResult & 0x80000000) return -1;
     else
     startResult = sceKernelStartModule(loadResult, 0, NULL, &status, NULL);
 
     if (loadResult != startResult) return -2;
 
     return 0;
}
 
void initUSB() {
     u32 retVal;
     LoadStartModule("flash0:/kd/semawm.prx");
     LoadStartModule("flash0:/kd/usbstor.prx");
     LoadStartModule("flash0:/kd/usbstormgr.prx");
     LoadStartModule("flash0:/kd/usbstorms.prx");
     LoadStartModule("flash0:/kd/usbstorboot.prx");
 
     retVal = sceUsbStart(PSP_USBBUS_DRIVERNAME, 0, 0);
 
     if (retVal != 0) {
          printf("Error starting USB Bus driver (0x%08X)\n", retVal);
          sceKernelSleepThread();
     }
 
     retVal = sceUsbStart(PSP_USBSTOR_DRIVERNAME, 0, 0);
 
     if (retVal != 0) {
          printf("Error starting USB Mass Storage driver (0x%08X)\n", retVal);
          sceKernelSleepThread();
     }
 
     retVal = sceUsbstorBootSetCapacity(0x800000);
 
     if (retVal != 0) {
          printf("Error setting capacity with USB Mass Storage driver (0x%08X)\n", retVal);
          sceKernelSleepThread();
     }
 
     retVal = 0;
}
 
void usbMode(char * what) {
     u32 retVal;
     if (stricmp(what, "on")==0) {
          retVal = sceUsbActivate(0x1c8);
     }
     else if (stricmp(what, "off")==0) {
          retVal = sceUsbDeactivate();
     }
}
Call this just inside of your int main:
Code:
initUSB();
Example:
Code:
usbMode("on");
 
or
 
usbMode("off");
Put this in your 'Makefile':
Code:
In LIBS=
-lpspusb -lpspusbstor


FADE SPLASH SCREEN
By: Samstag

Headers:
Your going to need to follow the tutorials [Only registered and activated users can see links. ] to get basic images blitting with graphics.c/h and framebuffer.c/h
Your going to also need [Only registered and activated users can see links. ] files.
Code:
#include <pspdisplay.h>
#include <pspctrl.h>
#include <pspkernel.h>
#include <pspdebug.h>
#include <pspgu.h>
#include <png.h>
#include <stdio.h>
#include "graphics.h"
#include "framebuffer.h"
Functions:
Code:
 
/*
Features:
- Modifies an entire image for fading effects 
- Takes a percentage input of 0 (transparent) to 100 (opaque)
 
Inputs:
- Image* image - valid Image pointer from the loadImage(filename) function in Yeldarb's tutorial #4 
- unsigned char alpha - a number from 0-100
*/
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++;
     }
}
 
/*
Features:
- Centers undersized images on a black background 
- Fades in the image at a rate you set 
- After fade-in, it will wait X seconds or any button press
 
Inputs:
- Image* pic - a valid Image pointer from the loadImage(filename) function in Yeldarb's tutorial #4 
- int fade_rate - the speed to fade the image in (higher = faster) 
- int timeout - time in seconds to display the image after the fade is complete
*/
void show_splash(Image *pic, int fade_rate, int timeout) {
int i, delay_time; int x_offset = 0, y_offset = 0; SceCtrlData input; delay_time = 40000; timeout = (timeout * 1000000) / delay_time; if(pic->imageWidth < 480) x_offset = (480 - pic->imageWidth) / 2; if(pic->imageHeight < 480) y_offset = (272 - pic->imageHeight) / 2; if(pic) { for(i = 0; i <= 100; i += fade_rate) { set_image_alpha(pic, i); clearScreen(0); blitAlphaImageToScreen(0, 0, pic->imageWidth, pic->imageHeight, pic, x_offset, y_offset); flipScreen(); sceKernelDelayThread(delay_time); } for(i = 0; i < timeout; i++) { sceCtrlReadBufferPositive(&input, 1); if(input.Buttons) break; sceKernelDelayThread(delay_time); } } }
Example:
Code:
Image* intro1;
Image* intro2;
 
intro1 = loadImage("title_screen.png");
intro2 = loadImage("credits_screen.png");
 
if(intro1) {
     show_splash(intro1, 8, 4);
     freeImage(intro1);
}
 
if(intro2) {
     show_splash(intro2, 8, 4);
     freeImage(intro2);
}

Put this in your 'Makefile':
Code:
 
In OBJS=
graphics.o framebuffer.o
 
In LIBS=
-lpspgu -lpng -lz -lm





Last edited by Slasher; 04-24-2006 at 10:08 PM..
Slasher is offline  
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Sponsored Links
Old 04-22-2006   #2 (permalink)
GamingCrazy Newbie
 
Join Date: Mar 2006
Posts: 12
Credits: 150
Re: Source Codes

here's a simple code.

this will load a bg pic for your program


#include<pspdisplay.h>
#include<pspctrl.h>
#include<pspkernel.h>
#include<pspdebug.h>
#include<pspgu.h>
#include<png.h>
#include<stdio.h>
#include"graphics.h"

#define printf pspDebugScreenPrintf
#define MAX(X, Y) ((X) > (Y) ? (X) : (Y))
PSP_MODULE_INFO(
"Image Display Program", 0, 1, 1);

/* Exit callback */
int exit_callback(int arg1, int arg2, void *common) {
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;
}
int main() {

pspDebugScreenInit();
SetupCallbacks();
initGraphics();

// Draw BG Pic
char buffer[200];
Image* ourImage;
sprintf(buffer,
"ms0:/loader/backpic.png"); // this prevents a buffer overflow
ourImage = loadImage(buffer);
sceDisplayWaitVblankStart();
blitAlphaImageToScreen(0 ,0 ,480 , 272, ourImage, 0, 0);
flipScreen();
// Next
sceKernelSleepThread();
return 0;
}



regards

chossy

Last edited by Slasher; 04-24-2006 at 10:00 PM..
Chossy. is offline  
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Old 07-18-2006   #3 (permalink)
GamingCrazy Rookie
 
Join Date: Mar 2006
Posts: 195
Credits: 150
Re: Source Codes

Ahhh, damn! Yeldarb's tut aren't loading... anyone have a backup?
bronxbomber92 is offline  
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Old 09-17-2006   #4 (permalink)
GamingCrazy Novice
 
CT_Bolt's Avatar
 
Join Date: Apr 2006
Location: In your head
Posts: 228
My Mood:
Credits: 353
Re: C Snippets/Help Section

Quote:
Originally Posted by Bronxbomber92
Ahhh, damn! Yeldarb's tut aren't loading... anyone have a backup?
[Only registered and activated users can see links. ]

Last edited by CT_Bolt; 12-12-2006 at 06:49 PM..
CT_Bolt is offline  
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply

  Game Tagg Forums - PSP Games, PSP Downloads, PSP News, and PSP Themes > Playstation Chat > PSP Homebrew Discussion


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

visitor stats
All times are GMT. The time now is 08:08 AM.


Powered by vBulletin
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.2.0
© 2005-2007 Condor Media Network
vB Ad Management by =RedTyger=