can someone explain how some of this code works?

Discuss the development of new homebrew software, tools and libraries.

Moderators: cheriff, TyRaNiD

Post Reply
cruisx
Posts: 8
Joined: Tue Mar 25, 2008 8:26 am

can someone explain how some of this code works?

Post by cruisx »

Code: Select all

/*****************************************************\
 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&#40;"OSLib Sample", 0, 1, 1&#41;;
PSP_MAIN_THREAD_ATTR&#40;THREAD_ATTR_USER | THREAD_ATTR_VFPU&#41;;


//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&#40;&#41;;
void AnimMarche&#40;&#41;;
void Gestion_Map&#40;&#41;;
short GetTile&#40;OSL_MAP *m,int col, int row&#41;;
int oslCollisionMap&#40;OSL_MAP *m,OSL_IMAGE *sprite,int haut,int bas, int gauche, int droit&#41;;

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

Verifie si il y a une colision entre 2 sprites
====================
*/
int main&#40;&#41;
&#123;
	//Initialisation de la librairie
	oslInit&#40;0&#41;;

	//Initialisation du mode graphique
	oslInitGfx&#40;OSL_PF_8888, 1&#41;;

	//chargement de nos images &#40;oui, le "loading" &#58;p&#41;
	Map01_tileset = oslLoadImageFile&#40;"sprite_s.png", OSL_IN_RAM, OSL_PF_5551&#41;;
	collision_tileset = oslLoadImageFile&#40;"collisions_s.png", OSL_IN_RAM, OSL_PF_5551&#41;;

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

	Gestion_Map&#40;&#41;; //Initialisation des map


	//boucle principale
	while &#40;!osl_quit&#41;
	&#123;
		//Permet de dessiner
		oslStartDrawing&#40;&#41;;		
		
		//check des touches
		Touches&#40;&#41;;

		//Affiche la map concerner, mais n'affiche pas la map des collision
		oslDrawMapSimple&#40;Map01&#41;;

		//dessine nos sprite
		oslDrawImage&#40;Joueur&#41;;

		//Fin du dessin
		oslEndDrawing&#40;&#41;;

		//Synchronise l'écran
		oslSyncFrame&#40;&#41;;		
	&#125;
	
	//on quitte l'application
	oslEndGfx&#40;&#41;;
	oslQuit&#40;&#41;;
	return 0;
&#125;


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

Gestion des touches pour le jeux &#58;&#41;
====================
*/
void Touches&#40;&#41;
&#123;
//Lit les touches
oslReadKeys&#40;&#41;;
		
if &#40;osl_keys->held.down&#41;
&#123;
	if &#40;!oslCollisionMap&#40;Map_collision,Joueur,0,1,0,0&#41;&#41;
	&#123;

        //ok on bouge
        Joueur->y += 2;
	&#125;

&#125;

if &#40;osl_keys->held.up&#41;
&#123;
  	if &#40;!oslCollisionMap&#40;Map_collision,Joueur,1,0,0,0&#41;&#41;
	&#123;
		Joueur->y -= 2;
	&#125;

&#125;

if &#40;osl_keys->held.left&#41;
&#123;
	if &#40;!oslCollisionMap&#40;Map_collision,Joueur,0,0,1,0&#41;&#41;
 	&#123;
		Joueur->x -= 2;
	&#125;

&#125;

if &#40;osl_keys->held.right&#41;
&#123;
	if &#40;!oslCollisionMap&#40;Map_collision,Joueur,0,0,0,1&#41;&#41;
	&#123;
		Joueur->x += 2;
	&#125;
&#125;


&#125;




/*
====================
Gestion_Map&#40;&#41;

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

void Gestion_Map&#40;&#41;
&#123;

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

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

&#125;




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

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

short GetTile&#40;OSL_MAP *m,int col, int row&#41;
&#123;
 u16 *map = &#40;u16*&#41;m->map;
 return map&#91;row*m->mapSizeX + col&#93;;
&#125;



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

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

int oslCollisionMap&#40;OSL_MAP *m,OSL_IMAGE *Collision,int haut,int bas, int gauche, int droit&#41;  
&#123;
 int tile;


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

if &#40;haut&#41;
 &#123;
  tile = GetTile&#40;m,&#40;&#40;m->scrollX + Collision->x +Collision->stretchX &#41;&#41;/ m->tileX, &#40;m->scrollY + Collision->y - 2&#41;/ m->tileY&#41;;
  if &#40;!tile&#41;
  &#123;
  tile = GetTile&#40;m,&#40;&#40;m->scrollX + Collision->x&#41;&#41;/ m->tileX, &#40;m->scrollY + Collision->y - 2&#41;/ m->tileY&#41;;  //essaye avec la moitier du personnage pour que la tete passe quoi qu'il arrive                   
  &#125;
 &#125;

if &#40;bas&#41;
 &#123;
  tile = GetTile&#40;m,&#40;&#40;m->scrollX + Collision->x +Collision->stretchX &#41;&#41;/ m->tileX, &#40;m->scrollY + Collision->y + Collision->stretchY + 2&#41;/ m->tileY&#41;;
  if &#40;!tile&#41;
  &#123;
  tile = GetTile&#40;m,&#40;&#40;m->scrollX + Collision->x&#41;&#41;/ m->tileX, &#40;m->scrollY + Collision->y + Collision->stretchY + 2&#41;/ m->tileY&#41;;
  &#125;
 &#125;
if &#40;gauche&#41;
 &#123;
  tile = GetTile&#40;m,&#40;&#40;m->scrollX + Collision->x - 2&#41;&#41;/ m->tileX, &#40;m->scrollY + Collision->y + Collision->stretchY&#41;/ m->tileY&#41;;
  if &#40;!tile&#41;
  &#123;
  tile = GetTile&#40;m,&#40;&#40;m->scrollX + Collision->x - 2&#41;&#41;/ m->tileX, &#40;m->scrollY + Collision->y&#41;/ m->tileY&#41;;
  &#125;
 &#125;

if &#40;droit&#41;
 &#123;
   tile = GetTile&#40;m,&#40;&#40;m->scrollX + Collision->x +Collision->stretchX + 2&#41;&#41;/ m->tileX, &#40;m->scrollY + Collision->y + Collision->stretchY&#41;/ m->tileY&#41;;
   if &#40;!tile&#41;
  &#123;
  tile = GetTile&#40;m,&#40;&#40;m->scrollX + Collision->x +Collision->stretchX + 2&#41;&#41;/ m->tileX, &#40;m->scrollY + Collision->y&#41;/ m->tileY&#41;;
  &#125;
 &#125;


  switch&#40;tile&#41;
  &#123;
   case TILE_NOSOLID&#58;
    // pas de collision sur le sprite retourne donc 0
    return 0;
    break;
   case TILE_SOLID&#58;
    // impassable retourne simplement 1 car impossable
    return 1;
    break;

   //On peut rajouter autant de case que besoin celon ce que on recherche.
  &#125;


return 0;
&#125;






Image


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.



Image

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

Image

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.
Art
Posts: 642
Joined: Wed Nov 09, 2005 8:01 am

Post by Art »

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:
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.
User avatar
jean
Posts: 489
Joined: Sat Jan 05, 2008 2:44 am

Post by jean »

Uh....me!me! can i do it??? .....oo...allright i'll left to someone else :°(
cruisx
Posts: 8
Joined: Tue Mar 25, 2008 8:26 am

Post by cruisx »

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:
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
edepot
Posts: 111
Joined: Sat Apr 09, 2005 3:39 pm

the second map is for internal parsing only

Post by edepot »

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.
Post Reply