forums.ps2dev.org Forum Index forums.ps2dev.org
Homebrew PS2, PSP & PS3 Development Discussions
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

can someone explain how some of this code works?

 
Post new topic   Reply to topic    forums.ps2dev.org Forum Index -> PSP Development
View previous topic :: View next topic  
Author Message
cruisx



Joined: 25 Mar 2008
Posts: 8

PostPosted: Sat Mar 29, 2008 12:30 pm    Post subject: can someone explain how some of this code works? Reply with quote

Code:
/*****************************************************\
 Auteur : Foury Jean-francois
 Auteur de : Super Mario toy
 Jour 10 collision entre 1 sprite et une map
\*****************************************************/
//La librairie principale OSLib
#include <oslib/oslib.h>
#include "collisions.h"
#include "sprite.h"

//les callbacks
PSP_MODULE_INFO("OSLib Sample", 0, 1, 1);
PSP_MAIN_THREAD_ATTR(THREAD_ATTR_USER | THREAD_ATTR_VFPU);


//definition des pointeurs vers notre map
OSL_MAP *Map01, *Map_collision;
OSL_IMAGE *Map01_tileset, *collision_tileset;

#define  TILE_NOSOLID  0
#define  TILE_SOLID  1

OSL_IMAGE *Joueur ;


//fonctions
void Touches();
void AnimMarche();
void Gestion_Map();
short GetTile(OSL_MAP *m,int col, int row);
int oslCollisionMap(OSL_MAP *m,OSL_IMAGE *sprite,int haut,int bas, int gauche, int droit);

/*
====================
colision

Verifie si il y a une colision entre 2 sprites
====================
*/
int main()
{
   //Initialisation de la librairie
   oslInit(0);

   //Initialisation du mode graphique
   oslInitGfx(OSL_PF_8888, 1);

   //chargement de nos images (oui, le "loading" :p)
   Map01_tileset = oslLoadImageFile("sprite_s.png", OSL_IN_RAM, OSL_PF_5551);
   collision_tileset = oslLoadImageFile("collisions_s.png", OSL_IN_RAM, OSL_PF_5551);

   Joueur= oslLoadImageFile("player.png", OSL_IN_RAM, OSL_PF_5551);
   //place Le joueur vers le centre de l'écran et dans sa position de départ
   Joueur->x = 240;
   Joueur->y = 130;

   Gestion_Map(); //Initialisation des map


   //boucle principale
   while (!osl_quit)
   {
      //Permet de dessiner
      oslStartDrawing();      
      
      //check des touches
      Touches();

      //Affiche la map concerner, mais n'affiche pas la map des collision
      oslDrawMapSimple(Map01);

      //dessine nos sprite
      oslDrawImage(Joueur);

      //Fin du dessin
      oslEndDrawing();

      //Synchronise l'écran
      oslSyncFrame();      
   }
   
   //on quitte l'application
   oslEndGfx();
   oslQuit();
   return 0;
}


/*
====================
Touches

Gestion des touches pour le jeux :)
====================
*/
void Touches()
{
//Lit les touches
oslReadKeys();
      
if (osl_keys->held.down)
{
   if (!oslCollisionMap(Map_collision,Joueur,0,1,0,0))
   {

        //ok on bouge
        Joueur->y += 2;
   }

}

if (osl_keys->held.up)
{
     if (!oslCollisionMap(Map_collision,Joueur,1,0,0,0))
   {
      Joueur->y -= 2;
   }

}

if (osl_keys->held.left)
{
   if (!oslCollisionMap(Map_collision,Joueur,0,0,1,0))
    {
      Joueur->x -= 2;
   }

}

if (osl_keys->held.right)
{
   if (!oslCollisionMap(Map_collision,Joueur,0,0,0,1))
   {
      Joueur->x += 2;
   }
}


}




/*
====================
Gestion_Map()

Initialilsation des map
====================
*/

void Gestion_Map()
{

    //configuration de la map
    Map01= oslCreateMap(
        Map01_tileset,                        //Tileset
        (void *)sprite_map,                            //Map
        16,16,                                //Taille des tiles
        30,17,                                //Taille de la Map
        OSL_MF_U16);                        //Format de la Map

    //configuration de la map
    Map_collision= oslCreateMap(
        collision_tileset,                        //Tileset
        (void *)collisions_map,                            //Map
        16,16,                                //Taille des tiles
        30,17,                                //Taille de la Map
        OSL_MF_U16);                        //Format de la Map

}




/*
====================
GetTile

Retourne sur quel sprite le player et possitionner
====================
*/

short GetTile(OSL_MAP *m,int col, int row)
{
 u16 *map = (u16*)m->map;
 return map[row*m->mapSizeX + col];
}



/*
====================
oslCollisionMap

Gestion des collisions avec les map et les perso pour le RPG
====================
*/

int oslCollisionMap(OSL_MAP *m,OSL_IMAGE *Collision,int haut,int bas, int gauche, int droit) 
{
 int tile;


//Test de collition pour la partie Demander suivant la touche appuyer haut, bas, gauche, droit.

if (haut)
 {
  tile = GetTile(m,((m->scrollX + Collision->x +Collision->stretchX ))/ m->tileX, (m->scrollY + Collision->y - 2)/ m->tileY);
  if (!tile)
  {
  tile = GetTile(m,((m->scrollX + Collision->x))/ m->tileX, (m->scrollY + Collision->y - 2)/ m->tileY);  //essaye avec la moitier du personnage pour que la tete passe quoi qu'il arrive                   
  }
 }

if (bas)
 {
  tile = GetTile(m,((m->scrollX + Collision->x +Collision->stretchX ))/ m->tileX, (m->scrollY + Collision->y + Collision->stretchY + 2)/ m->tileY);
  if (!tile)
  {
  tile = GetTile(m,((m->scrollX + Collision->x))/ m->tileX, (m->scrollY + Collision->y + Collision->stretchY + 2)/ m->tileY);
  }
 }
if (gauche)
 {
  tile = GetTile(m,((m->scrollX + Collision->x - 2))/ m->tileX, (m->scrollY + Collision->y + Collision->stretchY)/ m->tileY);
  if (!tile)
  {
  tile = GetTile(m,((m->scrollX + Collision->x - 2))/ m->tileX, (m->scrollY + Collision->y)/ m->tileY);
  }
 }

if (droit)
 {
   tile = GetTile(m,((m->scrollX + Collision->x +Collision->stretchX + 2))/ m->tileX, (m->scrollY + Collision->y + Collision->stretchY)/ m->tileY);
   if (!tile)
  {
  tile = GetTile(m,((m->scrollX + Collision->x +Collision->stretchX + 2))/ m->tileX, (m->scrollY + Collision->y)/ m->tileY);
  }
 }


  switch(tile)
  {
   case TILE_NOSOLID:
    // pas de collision sur le sprite retourne donc 0
    return 0;
    break;
   case TILE_SOLID:
    // impassable retourne simplement 1 car impossable
    return 1;
    break;

   //On peut rajouter autant de case que besoin celon ce que on recherche.
  }


return 0;
}











what i am haveing trouble figuring out is how he used this image to make the whole map? i dont get it, how you can use 1 image and make duplicates.





also what do this image have anything to do with collision? like how does this work.



ya so im just wondering how he creates the whole map with those 2 sprites(like what part of the code and how) and what the second images has anything to do with collision.
Back to top
View user's profile Send private message
Art



Joined: 09 Nov 2005
Posts: 647

PostPosted: Sat Mar 29, 2008 2:16 pm    Post subject: Reply with quote

You didn't really think you'd have 25 identical images of the same purple
image on the Memory Stick did you?
I can't see where the images are drawn in that code, but as with anything else,
You use a master image that contains everything, and just specify the start
coordinates and size of the section of that master image you want to print to
the screen.
You can do this a number of times before flipping the screen to display a frame.

There's actually a bit too much green stuff in that image.
You'd get by with just the first green square.

As for collision, a_noob gave a good method in your other thread:
Quote:

you could make a collision map which is a shilouette of the image and you just check you see that you are on the designated color that allows walking. You can do similar on a tile based map. just check is the character is on a walkable tile.


Having said that, someone's going to come along and say "this isn't the place to learn basic programming",
and they'll be right. It isn't, and it really shouldn't be.
_________________
If not actually, then potentially.
Back to top
View user's profile Send private message
jean



Joined: 05 Jan 2008
Posts: 489

PostPosted: Sat Mar 29, 2008 10:28 pm    Post subject: Reply with quote

Uh....me!me! can i do it??? .....oo...allright i'll left to someone else :°(
Back to top
View user's profile Send private message
cruisx



Joined: 25 Mar 2008
Posts: 8

PostPosted: Sun Mar 30, 2008 1:21 am    Post subject: Reply with quote

Art wrote:
You didn't really think you'd have 25 identical images of the same purple
image on the Memory Stick did you?
I can't see where the images are drawn in that code, but as with anything else,
You use a master image that contains everything, and just specify the start
coordinates and size of the section of that master image you want to print to
the screen.
You can do this a number of times before flipping the screen to display a frame.

There's actually a bit too much green stuff in that image.
You'd get by with just the first green square.

As for collision, a_noob gave a good method in your other thread:
Quote:

you could make a collision map which is a shilouette of the image and you just check you see that you are on the designated color that allows walking. You can do similar on a tile based map. just check is the character is on a walkable tile.


Having said that, someone's going to come along and say "this isn't the place to learn basic programming",
and they'll be right. It isn't, and it really shouldn't be.



oh i see thx =) and ya i know how to do collision, i just learned it last night, i just wanted to know why he used the second picture =). again thankyou
Back to top
View user's profile Send private message
edepot



Joined: 09 Apr 2005
Posts: 111

PostPosted: Sun Mar 30, 2008 4:56 am    Post subject: the second map is for internal parsing only Reply with quote

the second map is for internal parsing, and is not displayed.
in this case he probably used it for testing of collisions. you can use it for other purposes, like finding shortest path.

try this: http://www.edepot.com/va2007.zip

it lets you get the hang of how the tile system works.
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    forums.ps2dev.org Forum Index -> PSP Development All times are GMT + 10 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group