Built-in libraries
The current imprementation of Mogemoge includes some simple built-in libraries.
Basic methods
sleep(n)
Sleep for the specified number of milliseconds.
example:
sleep(3); # sleep for 3 milliseconds
exit(n)
Terminate a current executing program.
example:
exit(-1); # stops and return the -1 value to a parent process.
PI
The value of PI.
example:
print PI; # print 3.14159265...
sin(r)
Returns the value of a sine function of the specified value as same as Java's Math.sin()
example:
d = sin(PI/2); # about 1.0
cos(r)
Returns the value of a cosine function of the specified value as same as Java's Math.cos()
example:
d = cos(PI/2); # about 0.0
atan2(y,x)
Returns the value of a arc tangent function of the specified value as same as Java's Math.cos()
example:
d = atan(1,1); # about PI/4
rand()
Returns the random value from 0.0 to 1.0.
example:
d = rand() * 100; # 0.0 <= d <= 100.0
irand(n)
Returns the integer random value less than the specified value.
example:
n = irand(10); # 0 <= n <= 9
sqrt(d)
Returns the the square root of the specified value.
example:
n = sqrt(9); # n = 3.0
abs(d)
Returns the absolute value of the specified value.
example:
n = abs(-5); # returns 5
d = abs(-1.3); # returns 1.3
max(a,b)
Returns the greater value of the specified 2 values.
example:
n = max(1,3); # returns 3
min(a,b)
Returns the smaller value of the specified 2 values.
example:
n = min(1,3); # returns 1
Graphics methods
The current implementation of Mogemoge contains a graphics library. It is very simple, but, at least, has enough functions to implement sample games.
guiInit() and guiSwapBuf()
The guiInit() is for initializing graphics library and creating a window. You have to invoke this method preceding other graphic methods.
The guiSwapBuf() is for swapping drawing/displaying image buffers. The current graphic library is based on double buffering for real time action games. The drawing buffer is cleared by the color specified by guiSetClearColor when guiSwapBuf() is invoked.
The following is the example code of a typical game main program.
guiInit(256, 256); # open a window 256x256.
while (true) {
# update and draw game objects ...
guiSwapBuf();
sleep(20);
}
guiDrawCircle(x,y,d)
Draw a filled circle. Its center is (x,y) and diameter is d. Its color is specified by guiSetColor.
guiDrawCircle(x0,y0,x1,y1)
Draw a line from (x0,y0) to (x1,y1). Its color is specified by guiSetColor. Its width is specified by guiSetWidth.
guiDrawArrowhead(x,y,s,d)
Draw a arrowhead shape at the (x,y). Its size and direction is specified by 's' and 'd' parameter. Its color is specified by guiSetColor.
guiDrawSquare(x,y,s)
Draw a filled square which center is (x,y) and size is 's'. Its color is specified by guiSetColor.
guiFillRect(x,y,w,h)
Draw a filled rectangle. Its upper-left courner is specified (x,y) and its size is specified (w,h). Its color is specified by guiSetColor.
guiSetColor(red,green,blue)
Set a color to draw for other drawing methods.
guiLineWidth(w)
Set a width of a line for the method guiDrawLine.
guiLineClearColor(w)
Set a color to clear a screen.
guiSetClip(x,y,w,h)
Set the clipping area to draw. After this function is invoked, Nothing will be drawn out of this area.
guiClearClip()
Reset the clipping area specified by guiSetClip.
guiKeyPressed(k)
Return the boolean value whether the specified key code is pressed or not. The 'k' parameter is an ascii code. Some key codes are pre-defined by built-in library such as KEY_DOWN, KEY_ESC, and KEY_SPACE.
guiKeyOn(k)
Return the boolean value of the key trigger.
guiKeyOff(k)
Return the boolean value of the key release trigger.
game_loop(w,h,init,update,draw,millisecond)
The game_loop() is a method to write a game main
loop simply.
(w,h) specifies a window size same as
guiInit.
'init','update' and 'draw' is passed as a method object
which is in a game main loop.
'millisecond' specifies a sleep time in a main loop.
This method has a pause function by a enter key and has a exit function by a escape key.
The most simple example is here:
init = method() {
# invoked once before entring a main loop
};
update = method() {
# invoked in a main loop except for pausing
};
draw_game = method() {
# invoked in a main loop
};
# 1. open 512x512 window
# 2. invoke 'init' method
# 3. enter main loop
# 4. invoke update/draw method in a main loop every 40 msec.
game_loop( 512, 512, init, update, draw, 40 );
This method is defined in Mogemoge. The following is its definition:
frame_timer = 0;
game_loop = method( w, h, init, update, draw, sleep_time ) {
guiInit( w, h );
init();
pause = false;
while ( true ) {
frame_timer = frame_timer + 1;
if ( guiKeyPressed( KEY_ESC ) ) {
exit( 0 );
} elif ( guiKeyOn( KEY_CR ) ) {
pause = not pause;
__dump__;
}
if ( not pause ) {
update();
}
draw();
sleep( sleep_time );
guiSwapBuf();
}
};
Container library
The built-in library includes a simple object container class named ObjectList. Its implementation is a single linked list.
The following is an example of usage:
# create a some object
a = new Something;
# add an object to a object list.
ObjectList.add(a);
# remove an object from a list.
ObjectList.remove(a);
ObjectList has 2 iteration methods.
# create a method to apply objects in ObjectList.
m = method(e) { print e };
# apply the method 'm' to all objects
ObjectList.iterate(m);
# apply the method 'm' to all objects in reverse order.
ObjectList.reverse_iterate(m);
ObjectList is an object as same as other Mogemoge objects.
So you can create another container by a new operator.
list1 = new ObjectList;
list1.add( new Something );
list1.add( new Something );
How to write you own (Java) library for Mogemoge
It is not difficult to write your own library.
Sorry, this section is under construction.