Advanced C

From Game Editor

Revision as of 18:50, 25 November 2009 by Asmodeus (Talk | contribs)
(diff) ← Older revision | Current revision (diff) | Newer revision → (diff)
Jump to: navigation, search

Before you start reading/using anything from this section, you should already have some coding experience in C scripting as well as some experience using Game Editor.

Math Functions

Here is a few custom made non-standard mathematical algorithms that could be useful.

FINDING A FACTORIAL:

   unsigned long int factorial(unsigned char A) //finding factorials.
   {
       unsigned long int ret = A;               // ret is returned
   
       while(A>1)                               //This loop makes "ret" the factorial value
       {
           A--;                                 //Decrement for next multiplication
           ret *= A;                            //Return value is multiplied.
       }
   
       return ret;                              //Return the value.
   }


DETERMINE IF A NUMBER IS PRIME:

   char is_prime(unsigned long int A)      //Is the input number a prime number?
   
   {
       unsigned long int i = 2;            //i is the loop counter, used with modulus.
       char ret = 1;                       //Assume number is prime initially.
   
       while((A % i) && (i <= (float)A/2)) // when first condition is false, number is not prime.
       {                                   // when second condition is false, number is prime.
           i++;
       }
   
       if (i <= (float)A/2)                // If true, then number is not prime and returns 0.
       {
           ret = 0;
       }
   
       return ret;                         //Prime returns 1. Not prime returns 0.
   }


MAKING DIVISIONS SAFE:

   double safe_division(double A, double B) // Avoids division by 0.
   {
       double ret;
   
       if (B)
       {
           ret = A/B; //Normal Division.
       }
       else
       {
           ret = 0; //Division by 0 will return 0.
       }
   
       return ret;
   }

File Reading

Reading external files. With more advanced knowledge, one can use methods like these to load level maps, inventories, basically anything the creator wants.

This function read the first two bytes and takes them to the x and y coordinates.

FILE *fp = fopen("myfile", "r+b");
x = fgetc(fp);
y = fgetc(fp);
fclose(fp);

You can also write binary files.

FILE *fp = fopen("myfile", "w+b");
fputc(x, fp);
fputc(y, fp);
fclose(fp);

IMPORTANT: GE can only open files, which are in the same directory as the game.

Canvas Specifics

Canvas actors, not very often used by Game Editor users, have enormous potential for more advanced game creators. One can literally design a fully functional image editor, vector games, etc with this feature! It has been done before.


DRAW RPG DIALOGUE WINDOW:

-Works with the dimensions of the canvas. The entire canvas become an algorithmically drawn window.

-Set the window color in the function's parameters.

-Generates a stylish gradient from initial input color.

   void draw_window(unsigned char R, unsigned char G, unsigned char B)
   {
       int i;
       double mem;
   
       erase(0,0,0,1);                          //Erase content first.
       for(i = 2; i < height-3; i++)            //Gradient draw loop.
       {
           mem = 1 - (double)(i-2)/(height-3);
           moveto(height+2, i);
           setpen(R*mem, G*mem, B*mem, 0, 1);
           lineto(width-4, i);
       }
   
       setpen(255,255,255,0,2);                 //The following draws outlines.
       moveto(0,0);
       lineto(width-1, 0);
       lineto(width-1, height-1);
       lineto(0, height-1);
       lineto(0,0);
       moveto(height, 0);
       lineto(height, height-1);
   }