📄 gameshell.java
字号:
* @param MsgType The type of message box. C: Was enum MessageBox_t. * @param MsTimeout The timeout before the messagebox go away. C: Was uint16. * */ void GAMsgBox(int msg, byte msgType, int msTimeout) { int w = g.getFont().stringWidth("Message")+10; int h = g.getFont().getHeight()+10; int x = (DISPLAY_WIDTH-w)/2; int y = (DISPLAY_HEIGHT-h)/2; g.setColor(GACOLOR_BLACK); g.fillRect(x+2,y+2,w,h); g.setColor(GACOLOR_WHITE); g.fillRect(x,y,w,h); g.setColor(GACOLOR_BLACK); g.drawRect(x,y,w,h); g.drawString("Message",x+5,y+5,ALIGN_TOP_LEFT); timerThread.paintNotScheduled = false; repaint(); } void GAMsgBox(String msg, byte msgType, int msTimeout) { int w = g.getFont().stringWidth(msg)+10; int h = g.getFont().getHeight()+10; int x = (DISPLAY_WIDTH-w)/2; int y = (DISPLAY_HEIGHT-h)/2; g.setColor(GACOLOR_BLACK); g.fillRect(x+2,y+2,w,h); g.setColor(GACOLOR_WHITE); g.fillRect(x,y,w,h); g.setColor(GACOLOR_BLACK); g.drawRect(x,y,w,h); g.drawString(msg,x+5,y+5,ALIGN_TOP_LEFT); timerThread.paintNotScheduled = false; repaint(); } // enum messagebox_t public static final byte MessageBoxWrappedText = 0; public static final byte MessageBoxSmall = 1; public static final byte MessageBoxBig = 2; /** * Prints a textlabel on the screen * * <p> * <i>This is only partially implemented. Does not print the</i> * <i>text label specified by msg, but instead prints "TextLabel"</i> * * @param msg The text id to be shown * @param alignment The alignment of the text * @param x The upper left corner of the area * @param y The upper left corner of the area * * @returns None * */ void GAPrintTextLabel(int msg, byte alignment, int x, int y) { switch(alignment) { case alignCenter: x = (x + DISPLAY_WIDTH)/2; g.drawString("TextLabel",x,y,Graphics.TOP|Graphics.HCENTER); break; } } // enum TextAlignment public static final byte alignCenter = 0; /* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% Gapiutil %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% */ /** * This is the maximum number that can be returned by GARand. * * @see GARand * @see GASeed * */ protected static final int GA_RAND_MAX=Integer.MAX_VALUE; /** * Emulate a simple sprintf(s, "Level %019ld", tt); * <p> * If Text is null, chString remains unchanged. * <p> * <i> Dummy. Just returns input string. </i> * * @param chString the space where the result shall be placed. In C, this was char *chString. * @param Text The formatting text. In C, this was const char *Text. * @param lNr The number to be converted. In C, this was sint32 lNr. * * @returns The number of converted parameters * */ int GAsprintf(StringBuffer chString, String Text,int lNr) { if(Text == null || chString == null) return 0; //System.out.println("GASprintf: the number to format is "+lNr); int i; int t; int o; int p; int State; int ChL; int tl1; int tl2; char ChFill; char CurrCh; StringBuffer s = new StringBuffer(); // // sprint(s, "Level %019d", tt); // chString.delete(0,chString.length()); ChFill = '\0'; t = Text.length(); //System.out.println("t = "+t); ChL = 0; State = 0; for (i = 0; i < t; i++) { CurrCh = Text.charAt(i); if (CurrCh == '%') { State = 1; } switch (State) { case 0: chString.append(CurrCh); break; case 1: // Skip the % State = 2; break; case 2: // Fill character State = 3; if (CurrCh == '0') { ChFill = '0'; break; } case 3: // Check the rest if (CurrCh == 'i') { State = 5; // Ok, the %xxx is analysed } else if (CurrCh == 'd') { State = 5; // Ok, the %xxx is analysed } else if (CurrCh == 'l') { State = 3; // Just skip it, this method uses sint32 in its method } else if ((CurrCh >= '0') && (CurrCh <= '9')) { if (ChFill != '0') // this part take care of the 'width' in %5d experssion { ChFill = ' '; } ChL = 10 * ChL; ChL = ChL + ((int)CurrCh - (int)'0'); } break; } // switch (State) if (State == 5) { // Fix the number, we now know the width and the fill character. if (lNr < 0) { ChL--; // Since minus sign will occupy one character tl2 = -lNr; } else { tl2 = lNr; } // // Create the string backwards // if (tl2 > 0) { while (tl2 > 0) { tl1 = tl2 % 10; // Do modulo and create the string "backwards" // ts = (sint16) (tl1 + 48); s.insert(0,(char) (tl1 + 48)); tl2 = tl2 / 10; } } p = ChL - s.length(); if (ChFill != '\0') { if ((ChFill == '0') && (lNr < 0)) { chString.append('-'); // Add the minus sign } for (o = 0; o < p; o++) // Add the fill character. { chString.append(ChFill); } if ((ChFill == ' ') && (lNr < 0)) { chString.append('-'); // Add the minus sign } } else { if (lNr < 0) { chString.append('-'); // Add the minus sign } } chString.append(s); State = 0; // Continue to add charachers, if any } // if (State == 5) } // for(i = 0; i < t; i++) return 1; } //GASprintf(..., String, ...) /** * This method start a timer to generate periodic event * * @param timerId The id of the timer. This was enum GATimerId in C. * @param millis This was uint16 in C. * * @returns TRUE if successfull. */ void GASetTimer(int timerId, int millis) { //System.out.println("GameShell.GASetTimer("+timerId+", "+millis+")"); if(timerId < 3) { timerThread.period[timerId]=millis; timerThread.periodUpdated[timerId]=true; } else { for(int i = 0; i<3; i++) { timerThread.period[i]=millis; timerThread.periodUpdated[i]=true; } } //System.out.println(" RETURNING"); } /** * This method stops a timer to generate events * * @param timerId The id of the timer. This was enum GATimerId in C. * * @returns TRUE if successfull. */ void GAKillTimer(int timerId) { //System.out.println("GameShell.GAKillTimer("+timerId+")"); long millis = Long.MAX_VALUE; switch(timerId){ case GATimer1: timerThread.period[0]=millis; break; case GATimer2: timerThread.period[1]=millis; break; case GATimer3: timerThread.period[2]=millis; break; case GATimerAll: timerThread.period[0]=millis; timerThread.period[1]=millis; timerThread.period[2]=millis; break; } } /** * Returns a pseudo-random number in the interval [0, GA_RAND_MAX]. * * @returns The pseudo-random number. In C, this was uint16. * * @see GASeed * @see GA_RAND_MAX * */ int GARand() { int tmp = randomGenerator.nextInt(); if( tmp >= 0 ) return tmp; else return -tmp; } // GARand /** * This method seeds the random number generator. * * A seed of zero means that the generator is seeded with the clock. * This is automatically done when GameShell is instantiated. * * @param seed Sets the random seed. In C, this was uint32. * * @see GARand * */ void GASeed(long seed) { if (seed == 0) { randomGenerator.setSeed(System.currentTimeMillis()); } else { randomGenerator.setSeed(seed); } } // GASeed /** * Loads data from a persistent storage (a RecordStore). * <p> * Calling GALoadNVData without ever having called GASaveNVData does nothing. This is * because the record for the game is created in GASaveNVData if needed. * <p> * It is assumed that if nothing exists to load, a newly instantiated GameSaveable * will be such that the games will behave as if they were running for the very * first time on a new phone. Thus the GameSaveable is left untouched if there is * nothing to load. * * @param gameID The id of the game. <i>Obsolete</i>. In C, this was typedef unsigned int MenuItemId_t. * @param gameData The buffer where the data will be placed. In C, this was unsigned char * This area shall already be allocated when calling this method * @param maxSize <i>Obsolete</i>. The GAPI will try to load the number of bytes defined * internal in the game book into GameData, however the number of bytes * will never be more than MaxSize. In C, this was uint16. * * * Example GALoadNVData(Menu_Item_Tetris, (unsigned char *)&SaveData, sizeof(SaveData)); * * @returns TRUE if succesfull, that is the menu id exist. * * @see GAGameID * @see GASaveNVData * */ boolean GALoadNVData(GameSaveable gameData) { RecordStore store; byte[] data; ByteArrayInputStream bis; DataInputStream dis; try{ store = RecordStore.openRecordStore(getGameName(),true); if(store == null) return false; try{ data = store.getRecord(1); } catch( InvalidRecordIDException rse ) { return true; // See documentation above } if(data == null) return true; // See documentation above bis = new ByteArrayInputStream(data); dis = new DataInputStream(bis); gameData.unSerialize(dis); dis.close(); bis.close(); } catch( Exception exc ) { exc.printStackTrace(); return false; // exc could be RecordStoreExc or IOExc } return true; } /** * Saves the data in a persistent storage. * * The method will not save more data than the game has been * allocated for. * <p> * <i>Dummy method. This means it does nothing yet, or is at best partially implemented.</i> * * @param GameID The id of the game. <i>Obsolete</i>. In C, this was typedef unsigned int MenuItemId_t. * @param gameData The buffer where the data is allocated which should be "saved". In C, this was const unsigned char *. * This area shall already be allocated when calling this method * * @returns TRUE if succesfull * * @see GAGameID * @see GALoadNVData * */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -