View Single Post
 
Old 07-02-2007, 05:10 AM
pirata nervo pirata nervo is offline
Moderator
PSP Titan
 
Join Date: Mar 2007
Location: www.consoleworld.net
Posts: 5,008
pirata nervo is on a distinguished road
Default

Ok here i will teach how to use the analog in C/C++.
We will use the graphics.h library.(you must download the Pirata Nervo's Package)

Pirata Nervo's Package include:
- Eboot and Images
- Source and Makefile

Download links:
Rapidshare - NEW LINK



Makefile:
Code:
TARGET = cursor
OBJS = main.o graphics.o framebuffer.o

CFLAGS = -O2 -G0 -Wall
CXXFLAGS = $(CFLAGS) -fno-exceptions -fno-rtti
ASFLAGS = $(CFLAGS)

LIBDIR =
LIBS = -lpspgu -lpng -lz -lm
LDFLAGS =

EXTRA_TARGETS = EBOOT.PBP
PSP_EBOOT_TITLE = cursor

PSPSDK=$(shell psp-config --pspsdk-path)
include $(PSPSDK)/lib/build.mak
Our Source Code(main.c):
Code:
#include <pspkernel.h>
#include <pspdebug.h>
#include <stdio.h>
#include <stdlib.h>
#include <pspctrl.h>
#include "graphics.h"
#include <pspdisplay.h>
#include <pspgu.h>
#include <png.h>

#define RGB(r, g, b) ((r)|((g)<<8)|((b)<<16))

PSP_MODULE_INFO("analog", 0, 1, 1);

#define printf pspDebugScreenPrintf 

/* 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();

     SceCtrlData pad;
     
     int clickx = 100;
     int clicky = 100;
     int cursorx = 0;
     int cursory = 0;
     int showtext = 0;
     
     char Text[50] = "Press Triangle to quit to XMB";
     
     Color Blue = RGB(0, 0, 255);
     
     Image *click, *cursor, *background;
     cursor = loadImage("cursor.png");
     background = loadImage("background.png");
     click = loadImage("click.png");
     
     if (!cursor || !background || !click)
     {
              //Image load failed
              printf("Image load failed!\n");
              sceKernelDelayThread(1000000 * 5);
     }
     
     while(1){
              
              sceCtrlReadBufferPositive(&pad, 1);
              sceCtrlSetSamplingCycle(0); 
              sceCtrlSetSamplingMode(PSP_CTRL_MODE_ANALOG);
              
              if(pad.Ly < 50)
              {
                        cursory -= 3;
              } 
              
              if(pad.Ly > 200)
              {
                        cursory += 3;
              }
              
              if(pad.Lx < 50)
              {
                        cursorx -= 3;
              }
              
              if(pad.Lx > 200)
              {
                        cursorx += 3;
              }
              
              if (cursorx <= 0) cursorx = 0;
              if (cursory <= 0) cursory = 0;
              if (cursorx >= 448) cursorx = 448;
              if (cursory >= 243) cursory = 243;
              
              blitAlphaImageToScreen(0, 0 , 480, 272, background, 0, 0);
              blitAlphaImageToScreen(0, 0 , 50, 50, click, clickx, clicky);
              blitAlphaImageToScreen(0, 0 , 32, 32, cursor, cursorx, cursory);
              

              if ((cursorx + cursor->imageWidth > clickx) && (cursorx < clickx + click->imageWidth) && (cursory + cursor->imageHeight > clicky) && (cursory < clicky + click->imageHeight) && pad.Buttons & PSP_CTRL_CROSS)
              {
                    if(showtext == 0) showtext = 1;
              }


              if(showtext >= 1)
              {
                       printTextScreen(200, 80, Text, Blue);
                       showtext++;
                       if(showtext == 50) showtext = 0;
                       if(showtext >= 1 && showtext <= 50) if(pad.Buttons & PSP_CTRL_TRIANGLE) sceKernelExitGame();
              }
              
              //display the blit on screen
              sceDisplayWaitVblankStart();
              flipScreen();

     }

     sceKernelSleepThread();
     return (0);
}
Initialization of the libraries
#include <pspkernel.h>
#include <pspdebug.h>
#include <stdio.h>[/color]
#include <stdlib.h>
#include <pspctrl.h>
#include "graphics.h"
#include <pspdisplay.h>
#include <pspgu.h>
#include <png.h>

Here we define the RGB color
#define RGB(r, g, b) ((r)|((g)<<8)|((b)<<16))

PSP_MODULE_INFO("analog", 0, 1, 1);

Defenition of the command printf as the command pspDebugScreenPrintf
example: #Define Cross (as) PSP_CTRL_CROSS

#define printf pspDebugScreenPrintf

Our callbacks
/* 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;
}

Main function
int main () {

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

Declaration of the variable pad
SceCtrlData pad;

Variables declarations
int clickx = 100;
int clicky = 100;
int cursorx = 0;
int cursory = 0;
int showtext = 0;

char Text[50] = "Press Triangle to quit to XMB";

Here we define e blue color as RGB(0,0,255)
Color Blue = RGB(0, 0, 255);

Declaration of the pointers of our images
Image *click, *cursor, *background;

The load of the images
cursor = loadImage("cursor.png");
background = loadImage("background.png");
click = loadImage("click.png");

We use this to verify if any of this images werent loaded.
if (!cursor || !background || !click) if this happen, then
{
//Image load failed
printf("Image load failed!\n");
sceKernelDelayThread(1000000 * 5); and the kernel whait 5 seconds and then quit to the XMB
}

while(1){

sceCtrlReadBufferPositive(&pad, 1);
If you dont use this, then the analog wont work
sceCtrlSetSamplingCycle(0);
sceCtrlSetSamplingMode(PSP_CTRL_MODE_ANALOG);

Here you are using the analog
if(pad.Ly < 50)
{
cursory -= 3;
}

if(pad.Ly > 200)
{
cursory += 3;
}

if(pad.Lx < 50)
{
cursorx -= 3;
}

if(pad.Lx > 200)
{
cursorx += 3;
}

End of analog and now we use a sample collision detection for the border of the screen
if (cursorx <= 0) cursorx = 0;
if (cursory <= 0) cursory = 0;
if (cursorx >= 448) cursorx = 448;
if (cursory >= 243) cursory = 243;

Function: extern void blitAlphaImageToScreen(int sx, int sy, int width, int height, Image* source, int dx, int dy); the sx is used for sprites(sprite x), the sy is used for sprites too(sprite y), the widht is the widht of the image and the height is the height of the image, the Image* source is the pointer of your image, the dx is where you want to blit the image on the screen(x axis) and the dy is where you want to blit the image on the screen(y axis)
blitAlphaImageToScreen(0, 0 , 480, 272, background, 0, 0);
blitAlphaImageToScreen(0, 0 , 50, 50, click, clickx, clicky);
blitAlphaImageToScreen(0, 0 , 32, 32, cursor, cursorx, cursory);


Another collision detection that you will understand when you click on the image that say click here.(we use the collision detection for that image)
if ((cursorx + cursor->imageWidth > clickx) && (cursorx < clickx + click->imageWidth) && (cursory + cursor->imageHeight > clicky) && (cursory < clicky + click->imageHeight) && pad.Buttons & PSP_CTRL_CROSS)
{
if(showtext == 0) showtext = 1;
}


if(showtext >= 1)
{
printTextScreen(200, 80, Text, Blue);
showtext++;
if(showtext == 50) showtext = 0;
if(showtext >= 1 && showtext <= 50)

While the text is showing on the screen you can press triangle to quit to the XMB.
if(pad.Buttons & PSP_CTRL_TRIANGLE) sceKernelExitGame();
}

display the blit on screen
sceDisplayWaitVblankStart();
flipScreen();

}

sceKernelSleepThread();
return (0);
}

Any doubts?
__________________

Upgrade your PSP Slim or FAT now!
NervOS Official Forum
Reply With Quote