Maths

Macros

#define oslAbs(x)   (((x)<0)?(-(x)):(x))
 
#define oslMin(x, y)   (((x)<(y))?(x):(y))
 
#define oslMax(x, y)   (((x)>(y))?(x):(y))
 
#define oslMinMax(x, min, max)   ((x) < (max) ? ((x) > (min) ? (x) : (min)) : (max))
 
#define oslNumberof(n)   (sizeof(n)/sizeof(*(n)))
 

Functions

float oslSin (float angle, float dist)
 
float oslCos (float angle, float dist)
 
int oslSini (int angle, int dist)
 
int oslCosi (int angle, int dist)
 
int oslGetNextPower2 (int val)
 

Detailed Description

Main OSLib section. Contains the main routines to initialize and terminate the library.

Macro Definition Documentation

#define oslAbs (   x)    (((x)<0)?(-(x)):(x))

Returns the absolute value of a number (that is, the positive part of the number).

//Prints 2
oslPrintf("%i", oslAbs(2));
//Prints 2 as well (the minus sign is ignored in an absolute value)
oslPrintf("%i", oslAbs(-2));
#define oslMin (   x,
 
)    (((x)<(y))?(x):(y))

Returns the smallest value between the two.

#define oslMax (   x,
 
)    (((x)>(y))?(x):(y))

Returns the greatest value between the two.

//Move the object 1 pixel to the left but make sure it will never go beyond the left corner of the screen.
posX = oslMax(posX - 1, 0);
#define oslMinMax (   x,
  min,
  max 
)    ((x) < (max) ? ((x) > (min) ? (x) : (min)) : (max))

Returns a value clamped between a minimum and maximum.

int addColor(int colValue, int addValue) {
//We add addValue to colValue, but we must check the color component is always between 0 and 255.
colValue = oslMinMax(colValue + addValue, 0, 255);
return colValue;
}
#define oslNumberof (   n)    (sizeof(n)/sizeof(*(n)))

Returns the number of objects in an array.

OSL_VIRTUALFILENAME ram_files[] = {
{"ram:/file1.jpg", (void*)file1_jpg, file1_jpg_size, &VF_MEMORY},
{"ram:/file2.png", (void*)file2_png, file2_png_size, &VF_MEMORY},
};
//This will display 2, as 2 elements are contained in the table ram_files.
oslPrintf("%i", oslNumberof(ram_files));

Function Documentation

float oslSin ( float  angle,
float  dist 
)

Calculates the sine of an angle in degrees multiplicated by a radius. Returns the result as a float. oslSin and oslCos use the VFPU to compute the result, and thus it's very fast and very precise.

Parameters
angleAngle in degrees (360° means a full circle)
distRadius of the circle
Returns
sin(angle * PI / 180) * dist
//Character coordinates
float cX, cY;
//Character speed
float cSpeed;
//Character angle
float cAngle;
//Move the character depending on its instant speed and angle
cX += oslCos(cAngle, cSpeed);
//In mathematics, positive y coordinates are directed upwards, in informatics it's directed
//downwards, as if we were writing on a sheet of paper, so we need to substract the sine.
cY -= oslSin(cAngle, cSpeed);
float oslCos ( float  angle,
float  dist 
)

Calculates the cosine of an angle in degrees multiplicated by a radius. Returns the result as a float.

Parameters
angleAngle in degrees (360° means a full circle)
distRadius of the circle
Returns
cos(angle * PI / 180) * dist
int oslSini ( int  angle,
int  dist 
)

Returns the sine of an angle in degrees (0 to 360) multiplicated by an integer radius. Returns the result as an integer. These routines use a precalculated lookup table to compute the result. Don't think oslSini and oslCosi are much faster than oslSin and oslCos, it's not the case.

//This code draws a line in a circle which has a radius of 20 pixels. You can play with the angle to move the line.
int angle = 40, radius = 20;
//Draw our line. Same remark as for oslSin applies.
oslDrawLine(240, 136, 240 + oslCosi(angle, radius), 136 - oslSini(angle, radius));
int oslCosi ( int  angle,
int  dist 
)

Returns the cosine of an angle in degrees (0 to 360) multiplicated by an integer radius. Returns the result as an integer.

int oslGetNextPower2 ( int  val)

Returns the next (upper) power of two of a number.

//Prints 256, as 256 = 2^8 (2^7 = 128, too low).