simpleturtle.c

Go to the documentation of this file.
00001 
00016 #include "../awt/frame.h"
00017 #include "turtlegraphic.h"
00018 #include "simpleturtle.h"
00019 
00020 #ifdef __cplusplus
00021 extern "C" {
00022 #endif
00023 
00024 #ifdef __cplusplus
00025    #define ST_PARAM_FRAME 
00026    #define ST_PARAM_END_FRAME 
00027    #define ST_PARAM_TG 
00028    #define ST_PARAM_END_TG 
00029 #else
00030    #define ST_PARAM_FRAME &st_frame, 
00031    #define ST_PARAM_END_FRAME &st_frame 
00032    #define ST_PARAM_TG &st_tg, 
00033    #define ST_PARAM_END_TG &st_tg 
00034 #endif
00035 
00036 #define ST_DEFAULT_CP "./grubc.jar" 
00037 #define ST_DEFAULT_TITLE "SIMPLE TURTLE" 
00038 #define ST_DEFAULT_WIDTH 600 
00039 #define ST_DEFAULT_HEIGHT 400 
00041 static Jvm st_jvm; 
00042 static Frame st_frame; 
00043 static TurtleGraphic st_tg; 
00044 static jboolean st_init = JNI_FALSE;
00045 static jboolean st_closeing = JNI_FALSE;
00046 
00053 void st_windowListener(jint event, jobject window) {
00054    switch (event) {
00055       case CB_WINDOW_CLOSING:
00056          st_closeing = JNI_TRUE;
00057          break;
00058    }
00059 }
00060 
00070 void initSimpleTurtle(const char* classpath, const char* title, jint framewidth, jint frameheight) {
00071    if (!st_init) { /* init the Simple Turtle only once */
00072       st_init = JNI_TRUE;
00073 
00074       initJvm(&st_jvm, classpath);
00075 
00076       createFrame(&st_frame);
00077       createTurtleGraphic(&st_tg, &st_frame);
00078 
00079       st_frame.addWindowListener(ST_PARAM_FRAME &st_windowListener);
00080 
00081       st_frame.setTitle(ST_PARAM_FRAME title);
00082       st_frame.setSize(ST_PARAM_FRAME framewidth, frameheight);
00083       st_frame.show(ST_PARAM_END_FRAME);
00084    }
00085 }
00086 
00091 void checkSimpleTurtleInit() {
00092    if (!st_init) {
00093       initSimpleTurtle(ST_DEFAULT_CP, ST_DEFAULT_TITLE, ST_DEFAULT_WIDTH, ST_DEFAULT_HEIGHT);
00094    }
00095 }
00096 
00104 void forward(jfloat l) {
00105    checkSimpleTurtleInit();
00106    st_tg.forward(ST_PARAM_TG l);
00107 }
00108 
00116 void backward(jfloat l) {
00117    checkSimpleTurtleInit();
00118    st_tg.backward(ST_PARAM_TG l);
00119 }
00120 
00128 void left(jfloat angle) {
00129    checkSimpleTurtleInit();
00130    st_tg.left(ST_PARAM_TG angle);
00131 }
00132 
00140 void right(jfloat angle) {
00141    checkSimpleTurtleInit();
00142    st_tg.right(ST_PARAM_TG angle);
00143 }
00144 
00150 void penUp() {
00151    checkSimpleTurtleInit();
00152    st_tg.setDraw(ST_PARAM_TG JNI_FALSE);
00153 }
00154 
00160 void penDown() {
00161    checkSimpleTurtleInit();
00162    st_tg.setDraw(ST_PARAM_TG JNI_TRUE);
00163 }
00164 
00170 void print(const char* text) {
00171    checkSimpleTurtleInit();
00172    st_tg.printText(ST_PARAM_TG text);
00173 }
00174 
00180 void setColor(Color c) {
00181    checkSimpleTurtleInit();
00182    st_tg.setColor(ST_PARAM_TG c);
00183 }
00184 
00190 void setBGColor(Color c) {
00191    checkSimpleTurtleInit();
00192    st_frame.setBackgroundColor(ST_PARAM_FRAME c);
00193 }
00194 
00198 void clearScreen() {
00199    checkSimpleTurtleInit();
00200    st_tg.clearScreen(ST_PARAM_END_TG);
00201 }
00202 
00206 void waitForClosingFrame() {
00207    checkSimpleTurtleInit();
00208    while (!st_closeing) {
00209       jnisleep(100);
00210    }
00211 }
00212 
00213 #ifdef __cplusplus
00214 } /* extern "C" */
00215 #endif
00216 
00217 /* end of file simpleturtle.c */

Generated on Sat Nov 19 14:11:11 2005 for GrubC by  doxygen 1.4.4