Code Snippets, Usefull Stuff For Fenix


Quiest

I like turtles!
Joined
Sep 2, 2004
Messages
3,411
Age
41
Location
Dteuschland ;)
Okay, this is for all new Fenix coders, I`ll try to gather as much useful code snippets together, to show how commands work, basic things like backgrounds, scrolling, layers, stuff that makes Fenix coding easier for you.

For how to set everything up and start coding, look at Racemaniacs post here.
It has links to tutorials and the programs needed.

Feel free to contribute, I try to keep everything in this first post, so I`ll add/update things here now and then :D


Loading fpg/music/sound/fonts into memory:
Code:
 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:
Code:
 unload_fpg(fpg1); //you can use the IDs to unload
 unload_song(song1);
 unload_wav(sound1);
 unload_fnt(font1);
Using the stuff you loaded:
Code:
 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:
Code:
 put_screen("fpg1.fpg",#); 
 //For some reason, 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":
Code:
 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:
Code:
 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
 
Last edited by a moderator:
Woo, hm, what to add what to add.

Declaring a structure
Code:
//Declaration can be placed in global, local or private
struct name;
  int variable;
  string text;
  int array[10];
end

Writing
Code:
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
Code:
//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
Code:
//Mouse coordinates
x = mouse.x;
y = mouse.y;
mouse.graph = graph;
mouse.file = file;
mouse.z = z;
 
Hey Moogle, should I add your snippets to the whole lot in the first post and you edit your post here and remove them, just for a more central info?

Ah, no, better not, there surely is a size limit for a post :D
 
say, i've been programming a bit now, i've made some processes, but now it seems a process can only return an int....
is this true? is there any way to circumvent it? (i can't even return a pointer it seems....) cause that would REALLY suck :s
anyone got an idea?
 
okay, after hours of searching the mistakes (and experimenting to understand how fenix works) i have got a very valuable tip for everybody!!

i have been working with floating point numbers and integers together, now suppose you got an int a(3 for example) and another int b(5147 for all i care) and you want to give to a float f the value a/b
if you just type
Code:
f=a/b;
the result will be 0.0.....
in order to force fenix to work with floating point numbers suring the calculation at least one of the numbers has to be a float!
so make a float fa
Code:
fa = a;
f=fa/b
will work and give a non zero result :)

i've been generating a nice landscape using a bézier interpolation of some randomly generated points, and i've been searching like hell for every single place where due to fenix not making the ints floats before the operation which's result i'll assign to a float the result is zero, making my program completely NOT work.....

i hope this will help other people and their projects :), and if i find out more stuff, i'll certainly post it here :)

ps: another little tip: i've so far not found a way to return floats, but in my case the numbers to be returned were all betwenn 0 and 1, so i just multiplied them by 10.000, and devided them by 10.000 again where i had to use them :), that was enough accuracy :)
 
Not so much babling, more code snippets, please! :D :D :D

This is for snippets of useful code, not for ready programs.
And edit your posts next time please, don`t make triple posts, that`s annoying :(


Okay, if it`s highly documented, it`s good :D
 
Quiest posted on Sep 5 2005 at 10:25 PM said:
Not so much babling, more code snippets, please! :D :D :D
i just posted some fully documented code ^^ (probably awful code too, but who cares :p)
does that count ^^
 
Last edited by a moderator:
A few snippets on process interaction:

Acquiring the process ID
Code:
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
Code:
//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
Code:
//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.

(Oh, I suggest if this topic gets stickied all non-productive posts are removed.)
 
The hard way to do reading/writing is using the fopen/fclose series, the easy way is just use save() and load() :). They are clean, easy and usually fits your needs. Only thing you can't do with save/load that you can do with the fopen series is save multiple arrays/structs in one file. And you better watch out not to save strings. Check it out!

Code:
//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?

When on the subject, a few highscore snippets. What I tend to do is not sort it everytime, just keep it sorted and stick new ones inbetween.

Code:
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
 
Back
Top