Loading fpg/music/sound/fonts into memory (Written by:Quiest)
 
global
  fpg1; song1; sound1; font1; //These are the IDs used for loading
begin
   fpg1=load_fpg("fpg1.fpg");
   song1=load_song("song1.mod");
   sound1=load_wav("sound1.wav");
   font1=load_fnt("font1.fnt");    
  end;
 
Unloading fpg/music/sound/fonts from memory: (Written by:Quiest)
 
unload_fpg(fpg1); //you can use the IDs to unload
unload_song(song1);
unload_wav(sound1);
unload_fnt(font1);
 
Using the stuff you loaded: (Written by:Quiest)
 
file=fpg1; //tells a process which fpg to use
play_song(song1,#);
play_wav(sound1,#);
//# is the number of loops, -1==infinite loop,...
//...0==play it once, 1==repeat it once...
 
Setting backgrounds: (Written by:Quiest)
 
put_screen("fpg1.fpg",#); 
//IDs don`t work here,you have to use the full filename for this
//# is the number of the graphic inside the fpg              
 
Using "layers": (Written by:Quiest)
 
z=#;
//this is a value every process has, # can be 1 to 255 (I believe)
//1 is the top layer, the bigger it get, the lower the layer

drawing_z(#);
//the same value except it is not used for processes but for draw commands 
 
Proper use of the write command: (Written by:Quiest)
 
write(*,x,y,#, "text"); //displaying text
write(*,x,y,#, string); //displaying a string variable
write_int(*,x,y,#, &integer); //displaying a integer variable
write_float(*,x,y,#, &float); //displaying a float variable

//* = ID of the loaded font (font1 for example); 0 for standart font
//x,y = the coordinates, where the text should be written
//# = the alignment of the font
  //0==Left superior corner; 1==Superior center;
  //2==Right superior corner; 3==Left center;
  //4==Center; 5==Right center; 6==Left inferior corner;
  //7==Inferior center; 8==Inferior corner
right
 
Declaring a structure (Written by:Moogle)
 
//Declaration can be placed in global, local or private
struct name;
 int variable;
 string text;
 int array[10];
end
 
Writing (Written by:Moogle)
 
int i = 5;
string text = "Yeah";
float boat = 3.214;

//Now write them down:

id0 = write(0,x,y,centering_code,text);
id1 = write_int(0,x,y,centering_code,& i);
id2 = write_float(0,x,y,centering_code,& boat);

//Delete them again:
delete_text(id0);
delete_text(id1);
delete_text(id2);

//Or delete all texts at once
delete_text( 0 );
 
Include other files (Written by:Moogle)
 
//Place in an empty spot(between processes,
//preferably on top before the processes start)
include "file.inc";

//In includes you can put processes and even globals or locals.
 
Mouse (Written by:Moogle)
 
//Mouse coordinates
x = mouse.x;
y = mouse.y;
mouse.graph = graph;
mouse.file = file;
mouse.z = z;

 
Acquiring the process ID (Written by:Moogle)
 
int process_id;

process_id = proc();

//Note that the proc process returns it's ID when the first frame
//statement in the process is reached. If the process returns a 
//value, process_id will hold that returned value and not the ID.
 
Referencing process_id's variables (Written by:Moogle)
 
//Check if the instance exists:
if(exists(process_id))

 //Fetching pre-defined local variables 
 x = process_id.x;
 graph = process_id.graph;

 //Changing variables:
 process_id.x = 3;
 
 //Same works for manually defined local variables(NOT private!)
end

//Note that when you are referencing variables in this way the process 
//you are referring to MUST exists, otherwise Fenix will crash.
 
Changing process_id's state (Written by:Moogle)
 
//Kill process_id
signal(process_id,s_kill);

//Kill all processes of the type 'proc'
signal(type proc,s_kill);

//As s_kill there are a few other constants available:
//s_sleep -> Process is halted untill woken up, all it's duties are... 
//...suspended including drawing to the screen.
//s_freeze -> Process is frozen from executing code, but will perform it's...
//...regular duties.
//s_kill -> Will eliminate the process.
//s_wakeup -> Gets a process out of sleep or frozen status.
 
Save and Load a file (Written by:Moogle)
 
//Oh my, I have a struct that needs saving!
struct highscore[10];
 name[2];
 score;
end

//So save it:
save("highscore.file",highscore);

//And load it again:
load("highscore.file",highscore);

//Peanuts right?
Using Timers (Written by:Moogle)
 
timer[0] = 0;
while(timer[0] < 1000)
 frame;
end
//One second passed
Reading a txt file (Written by:Racemaniac)
 
program textreader;
global
power[4]=1,256,256*256,256*256*256;
textfile[1000];//we'll read up to 4000 chars
string line[100];//up to 100 lines, should be enough?
private
i;
begin
readfile("newdoc.txt");
for(i=0;i<10;i++)
write_string(0,160,5+i*10,4,&line[i]);
end
loop
frame;
end
end;
function readfile(string filename)
private
string chars[100];//up to 98 chars on a line (last two are for end of line)
lasttwo[2];
lastj;
j;
i;
begin
load(filename,textfile);
while(j<4000)
lasttwo[0]= (textfile[j/4]/power[j%4])%256;
chars[j-lastj]=chr(lasttwo[0]);
j++;
lasttwo[1]= (textfile[j/4]/power[j%4])%256;
chars[j-lastj]=chr(lasttwo[1]);
while((lasttwo[1]!=10 or lasttwo[0]!=13)and j<4000 and j-lastj<100);
 j++;                                                            
 lasttwo[0]=lasttwo[1];
 lasttwo[1]= (textfile[j/4]/power[j%4])%256;
 chars[j-lastj]=chr(lasttwo[1]);
end;
j++;
if(j-lastj==101);
 return;
end
line[i++]=join("",&chars,j-lastj-2);
lastj=j;
end
return;
end;
Bouncing ball physics (Written by:Moogle)
 
process ball()

private
ballspeed = 10;

begin

//insert graph, startvalues of variables and other blabla

loop
if(x > 320 or x < 0)
 angle = -angle;
end
if(y > 240 or y < 0)
 angle = -angle + 180000;
end

advance(ballspeed);
frame;
end

end

Setting Up and Using High Scores (Written by:Unkown)
 
global

//So I had this struct to save them in
struct highscore[10];
 score;
end

//Set up the highscore table
process initHighscore()

private
string fileName = "highscore.file";
i;

begin

if(file_exists(fileName))

 //File exists and can be loaded:
 load(fileName, highscore);

else

 //File does not exist, fill the highscore table with standard values:
 for(i = 0;i < 10; i++ )
   //Fill in a generated score(lower index means higher score)
   highscore[i].score = (10-i)*100;
 end

end

end

//Add a score
process addHighscore(int newScore)

private
i, newScorePosition;

begin
for( i = 0; i < 10; i++ )
 if(highscore[i].score < newScore)
   break;
 end
end

if(i == 10)
 //If i equals 10 all scores in the table were higher than the new one.
 return;
else
 //If lower, the score on place 'i' is the first one to be lower than the new score.

 //Save which position the new score is coming
 newScorePosition = i;

 //And move everything one place down, starting with 'i'
 for(;i<9;i++)
   highscore[i+1].score = highscore[i].score;
 end

 //Add in the new score
 highscore[i].score = newScore;
end

//And if you would want to, make it save when changed
save(fileName,highscore);

end

//print to the screen
process printHighscore()

private
i;

begin

for( i = 0; i < 10; i++ )
 write(0,screenx-150,200+i*20,0,i);
 write_int(0, screenx+150,200+i*20,2,&highscore[i].score);
end

end

 
   

Home : Start Here : Sources : Snippets : Tutorials : Faq's : Links : Contact