⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 torinocanvas.java

📁 国外人写的操作感挺好的一款滑雪游戏.结构也比较清楚
💻 JAVA
📖 第 1 页 / 共 5 页
字号:
//		int iEvent = getKeyEvent();
//		while (iEvent != 0)
//		{
//			if ( (iEvent & 0x1000000/*@VKEY_RELEASED*/) == 0 )
//			{
//				handleKeyPressed( iEvent );
//			}			
//			else
//			{
//				handleKeyReleased( iEvent & ~0x1000000/*@VKEY_RELEASED*/ );
//			}			
//			iEvent = getKeyEvent();
//		}
//	}
//#endif

	protected void keyPressed( int keyCode )
	{
		if (m_bPaused)
			return;
			
		// Translate and store
		int iKey = getTranslatedKey( keyCode );
		if (iKey != 0)
		{
//#if defined(_USE_THREAD) || defined(_USE_KEY_QUEUE)
//			addKeyEvent( iKey );
//#else
			try
			{
				handleKeyPressed( iKey );
			}
			catch ( Exception e )
			{
//				DUMP_EXCEPTION_MSG(e, "*** Error during keyPressed ***");
//#ifdef _DISPLAY_MEM_ERROR
//				m_midlet.m_bCriticalError = true;
//#endif
				m_midlet.exitRequested();
			}
//#endif
		}	
	}
	
	protected void keyReleased( int keyCode )
	{
		if (m_bPaused)
			return;
			
		// Translate and store
		int iKey = getTranslatedKey( keyCode );
		if (iKey != 0)
		{
//#if defined(_USE_THREAD) || defined(_USE_KEY_QUEUE)
//			addKeyEvent( iKey | 0x1000000/*@VKEY_RELEASED*/ );
//#else
			try
			{
				handleKeyReleased( iKey );
			}			
			catch ( Exception e )
			{
//				DUMP_EXCEPTION_MSG(e, "*** Error during keyPressed ***");
//#ifdef _DISPLAY_MEM_ERROR
//				m_midlet.m_bCriticalError = true;
//#endif
				m_midlet.exitRequested();
			}
//#endif
		}
	}

	private final int getTranslatedKey( int keyCode )
	{
		int iVKey = 0;
		int gameAction = getGameAction( keyCode );
		switch( gameAction )
		{
			case UP: 		iVKey |= 0x4000/*@VKEY_UP*/; break;
			case DOWN: 		iVKey |= 0x8000/*@VKEY_DOWN*/; break;
			case LEFT: 		iVKey |= 0x1000/*@VKEY_LEFT*/; break;
			case RIGHT:		iVKey |= 0x2000/*@VKEY_RIGHT*/; break;
			case FIRE:		iVKey |= 0x10000/*@VKEY_FIRE*/; break;
		}		
		
		// override these keys
		switch (keyCode)
		{
//#ifdef _IS_Nokia3650
//			case KEY_NUM1:	iVKey = 0x2/*@VKEY_1*/ | 0x4000/*@VKEY_UP*/; break;
//			case KEY_NUM2:	iVKey = 0x4/*@VKEY_2*/ | 0x1000/*@VKEY_LEFT*/; break;
//			case KEY_NUM3:	iVKey = 0x8/*@VKEY_3*/ | 0x8000/*@VKEY_DOWN*/; break;
//			case KEY_NUM4:	iVKey = 0x10/*@VKEY_4*/; break;
//			case KEY_NUM5:	iVKey = 0x20/*@VKEY_5*/ | 0x10000/*@VKEY_FIRE*/; break;
//			case KEY_NUM6:	iVKey = 0x40/*@VKEY_6*/; break;
//			case KEY_NUM7:	iVKey = 0x80/*@VKEY_7*/; break;
//			case KEY_NUM8:	iVKey = 0x100/*@VKEY_8*/; break;
//			case KEY_NUM9:	iVKey = 0x200/*@VKEY_9*/; break;
//			case KEY_NUM0:	iVKey = 0x1/*@VKEY_0*/; break;
//			case KEY_POUND:	iVKey = 0x800/*@VKEY_POUND*/;break;
//			case KEY_STAR:	iVKey = 0x400/*@VKEY_STAR*/ | 0x2000/*@VKEY_RIGHT*/; break;
//#else
			case KEY_NUM1:	iVKey = 0x2/*@VKEY_1*/; break;
			case KEY_NUM2:	iVKey = 0x4/*@VKEY_2*/ | 0x4000/*@VKEY_UP*/; break;
			case KEY_NUM3:	iVKey = 0x8/*@VKEY_3*/; break;
			case KEY_NUM4:	iVKey = 0x10/*@VKEY_4*/ | 0x1000/*@VKEY_LEFT*/; break;
			case KEY_NUM5:	iVKey = 0x20/*@VKEY_5*/ | 0x10000/*@VKEY_FIRE*/; break;
			case KEY_NUM6:	iVKey = 0x40/*@VKEY_6*/ | 0x2000/*@VKEY_RIGHT*/; break;
			case KEY_NUM7:	iVKey = 0x80/*@VKEY_7*/; break;
			case KEY_NUM8:	iVKey = 0x100/*@VKEY_8*/ | 0x8000/*@VKEY_DOWN*/; break;
			case KEY_NUM9:	iVKey = 0x200/*@VKEY_9*/; break;
			case KEY_NUM0:	iVKey = 0x1/*@VKEY_0*/; break;
			case KEY_POUND:	iVKey = 0x800/*@VKEY_POUND*/;break;
			case KEY_STAR:	iVKey = 0x400/*@VKEY_STAR*/; break;
//#endif
//#ifdef _FULL_CANVAS
			case KEY_SOFTKEY1: iVKey = 0x200000/*@VKEY_SELECT*/; break;
			case KEY_SOFTKEY2: iVKey = 0x400000/*@VKEY_BACK*/; break;
//#endif
		}
		
		return iVKey;
	}

	private void handleKeyPressed( int keyCode ) throws IOException
	{
		if (m_currentWindow != null)
	    	m_currentWindow.keyPressed(
	    		keyCode & (0x1000/*@VKEY_LEFT*/-1)/*@VKEY_NUM_MASK*/,
	    		keyCode & (~(0x1000/*@VKEY_LEFT*/-1)/*@VKEY_NUM_MASK*/)/*@VKEY_ACTION_MASK*/);
	    	
//#ifdef _DEBUG
//	    if ( (keyCode & (0x1000/*@VKEY_LEFT*/-1)/*@VKEY_NUM_MASK*/) == 0x400/*@VKEY_STAR*/ )
//	    {
//	    	m_iDebugInfo = (m_iDebugInfo+1)%DEBUG_COUNT;
//	    	m_bNeedPaint = true;
//	    }	    
//#endif
	}

	private void handleKeyReleased( int keyCode ) throws IOException
	{
		if (m_currentWindow != null)
	    	m_currentWindow.keyReleased(
	    		keyCode & (0x1000/*@VKEY_LEFT*/-1)/*@VKEY_NUM_MASK*/,
	    		keyCode & (~(0x1000/*@VKEY_LEFT*/-1)/*@VKEY_NUM_MASK*/)/*@VKEY_ACTION_MASK*/);
	}
	
//#ifndef _NO_ONLINE
//#ifdef _IS_SPRINT
//	public JCISprint m_jci;
//	public void createOnlineService()
//	{
//		m_jci = new JCISprint( m_midlet, this );
//	}
//
//	public void destroyOnlineService()
//	{
//		m_jci = null;
//	}
//#else
//	public JCI m_jci;
//	public void createOnlineService()
//	{
//		m_jci = new JCI( m_midlet );
//	}
//
//	public void destroyOnlineService()
//	{
//		m_jci = null;
//	}
//#endif
//#endif

// _DEBUG text
//#ifdef _DEBUG
//	private final static int	DEBUG_NONE = 0;
//	private final static int	DEBUG_FRAME = 1;
//	private final static int	DEBUG_PAINT = 2;
//	private final static int	DEBUG_RUN = 3;
//	private final static int	DEBUG_UPDATE = 4;
//	private final static int	DEBUG_MEM = 5;
//	private final static int	DEBUG_COUNT = 6;
//
//	private int 		m_iDebugInfo = DEBUG_NONE;
//	public int			m_iFrameTime;
//	public int			m_iLastFrameTime;
//	public int			m_iPaintTime;
//	public int			m_iRunTime;
//	public int			m_iUpdateCount;
//	public int			m_iUpdateTime;
//#endif

	public void paint( Graphics g )
	{
                    sleep(6);
//#ifdef _DEBUG
//		try
//		{
//#endif
//#ifdef _USE_THREAD
//			synchronized (s_paintLock)
//			{
//#endif
			if (m_bNeedPaint)
			{
//#ifdef _FULL_CANVAS
//#ifdef _IS_NokiaS60
//				setClip( g, 0, 0, getWidth(), getHeight()-m_keyLabels.m_iHeight );
//#else
				g.setClip( 0, 0, getWidth(), getHeight()-m_keyLabels.m_iHeight );
//#endif
//#endif
				m_bNeedPaint = false;
				if (m_iLoadStatus < 100)
				{
					paintLoadScreen( g );
				}		
				else
				{
//#ifdef _DEBUG
//					int iTime = getCurrentTime();
//#endif
						
					if (m_currentWindow != null)
				    	m_currentWindow.paint(g);
			    	
//#ifdef _FULL_CANVAS
//#ifdef _IS_NokiaS60
//					setClip( g, 0, getHeight()-m_keyLabels.m_iHeight, getWidth(), m_keyLabels.m_iHeight );
//#else
					g.setClip( 0, getHeight()-m_keyLabels.m_iHeight, getWidth(), m_keyLabels.m_iHeight );
//#endif
				    // draw softkey labels
				    if ( m_keyLabels != null && m_bKeyLabelsDirty )
				    {
				    	m_keyLabels.paint(g);
				    	m_bKeyLabelsDirty = false;
				    }				    
//#endif
	    
//#ifdef _DEBUG
//					int iTime2 = getCurrentTime();
//				    m_iPaintTime = iTime2-iTime;
//				    m_iFrameTime = iTime2 - m_iLastFrameTime;
//				    m_iLastFrameTime = iTime2;
//		
//					String s = null;
//					
//					// draw some debug text
//					switch (m_iDebugInfo)
//					{
//					case DEBUG_FRAME:
//						s = "Frame: " + m_iFrameTime;
//						break;
//					case DEBUG_PAINT:
//						s = "Paint: " + m_iPaintTime;
//						break;
//					case DEBUG_RUN:
//						s = "Run: " + m_iRunTime;
//						break;
//					case DEBUG_UPDATE:
//						s = "Update: " + m_iUpdateCount + "/" + m_iUpdateTime;
//						break;
//					case DEBUG_MEM:
//						System.gc();
//						s = "Mem: " + (Runtime.getRuntime().totalMemory() - Runtime.getRuntime().freeMemory());
//						break;
//					}
//								
//					if (s != null)
//					{
//						g.setColor(0xFFFFFF/*@COLOR_WHITE*/);
//						g.setFont(m_menuFont);
//						
//						int iWidth = m_menuFont.stringWidth( s )+2;
//						int iHeight = m_menuFont.getHeight() + 2;
//						g.fillRect( getWidth()-iWidth, getHeight()-iHeight, iWidth, iHeight );
//			
//						g.setColor(0x000000/*@COLOR_BLACK*/);
//						g.drawString( s, getWidth()-1, getHeight()-1, Graphics.BOTTOM|Graphics.RIGHT );
//					}					
//		
//					m_iPaintTime = 0;
//					m_iRunTime = 0;
//					m_iUpdateCount = 0;
//					m_iUpdateTime = 0;
//#endif
				}
			}
			
//#ifdef _USE_THREAD
			m_bPaintScheduled = false;
//			
//			s_paintLock.notify();
//			}
//			//m_bPaintLock = false;
//#endif
//#ifdef _DEBUG
//		}
//		catch ( Exception e )
//		{
////			DUMP_EXCEPTION_MSG(e, "*** Error during paint ***");
//		}
//#endif
	}
	

//*****************************************************************************
//	Record Store functions

	public boolean loadData() throws IOException
	{
		boolean bSuccess = false;
		ByteArrayInputStream bs = null;
		DataInputStream ds = null;
		RecordStore rs = null;
		try
		{
			rs = RecordStore.openRecordStore( "torino.dat"/*@DATA_FILE_NAME*/, false );
			if (rs != null)
			{
				byte aBytes[] = new byte[rs.getRecordSize(1)];
				rs.getRecord(1,aBytes,0);

				bs = new ByteArrayInputStream(aBytes);
				ds = new DataInputStream(bs);
				m_gameData.read( ds );
				bSuccess = true;
			}
		}
		catch ( RecordStoreException e )
		{
		}
		finally
		{
			try
			{
				if (rs != null)
					rs.closeRecordStore();
			}
			catch (RecordStoreException e)
			{
			}
//			CLOSE_IS(ds);
//			CLOSE_IS(bs);
		}

		return bSuccess;
	}

	public boolean saveData( boolean bCreateNew ) throws IOException
	{
		boolean bSuccess = false;

		// save the record store
		RecordStore rs = null;
		ByteArrayOutputStream bs = null;
		DataOutputStream ds = null;
		try
		{
			if (bCreateNew)
			{
				try
				{
					RecordStore.deleteRecordStore( "torino.dat"/*@DATA_FILE_NAME*/ );
				}
				catch (RecordStoreException e)
				{
				}
			}
			
			rs = RecordStore.openRecordStore( "torino.dat"/*@DATA_FILE_NAME*/, bCreateNew );
			if (rs != null)		
			{
				bs = new ByteArrayOutputStream();
				ds = new DataOutputStream(bs);
				m_gameData.write( ds );
				byte aBytes[] = bs.toByteArray();
				if (bCreateNew)
					rs.addRecord(aBytes,0,aBytes.length);
				else
					rs.setRecord(1,aBytes,0,aBytes.length);
				bSuccess = true;
			}
		}
		catch ( RecordStoreException e)
		{
		}
		finally
		{
			try
			{
				if (rs != null)
					rs.closeRecordStore();
			}
			catch (RecordStoreException e)
			{
			}
//			CLOSE_OS(ds);
//			CLOSE_OS(bs);
		}		

		return bSuccess;
	}

//*****************************************************************************

//*****************************************************************************
// Image loading functions

	private final static int PNG_CHUNK_OVERHEAD = 12;
	private final static int PNG_HEADER_SIZE = 8 + PNG_CHUNK_OVERHEAD + 13;
	private final static int PNG_PALETTE_SIZE = PNG_CHUNK_OVERHEAD + 768;

//#ifdef _BUFFER_IO
//	// _BUFFER_IO must be at least as large as the largest resource file
//	private static byte m_readBuffer[] = new byte[/*@_BUFFER_IO*/];
//#ifdef _KEEP_FILE_DATA
//	private static Hashtable m_resFiles = new Hashtable();
//#else
//	private static int m_iLastResFile = -1;
//#endif
//#endif
	
// HACK!
//#ifdef _IS_NokiaS60
//	public final static short[] loadRawPixels( String sFile ) throws IOException
//	{
////		DEBUG_MSG( "loadRawPixels: " + sFile );
//		InputStream is = null;
//		ByteArrayInputStream bs = null;
//		DataInputStream ds = null;
//		short[] pixels = null;
//#ifdef _CLOSE_STREAMS
//		try
//		{	
//#endif
//			if (m_readBuffer == null)
//			{
//				m_readBuffer = new byte[/*@_BUFFER_IO*/];
//			}
//			
//			is = sFile.getClass().getResourceAsStream( sFile );
//			is.read(m_readBuffer);
//			bs = new ByteArrayInputStream( m_readBuffer );
//			ds = new DataInputStream( bs );
//			
//			short width = ds.readShort();
//			short height = ds.readShort();
////			DEBUG_MSG( "wxh: " + width + "x" + height );
//			int iCount = 2+width*height;
//			pixels = new short[iCount];
//			pixels[0] = width;
//			pixels[1] = height;
//			for (int i=2; i< iCount; i++)
//				pixels[i] = ds.readShort();
//#ifdef _CLOSE_STREAMS
//		}
//		finally
//		{
////			CLOSE_IS(ds);
////			CLOSE_IS(bs);
////			CLOSE_IS(is);
//		}		
//#endif
//		return pixels;
//	}	
//#endif
	
//#ifdef _BUFFER_IO
//#ifdef _KEEP_FILE_DATA
//	private final static byte[] getFile( int iFile ) throws IOException
//	{
//		InputStream is = null;
//		String sFile = getString( iFile );
//		byte[] abyBuffer = null;
//#ifdef _CLOSE_STREAMS
//		try
//		{	
//#endif
//			abyBuffer = (byte[]) m_resFiles.get(sFile);
//			if (abyBuffer == null)
//			{
//				is = sFile.getClass().getResourceAsStream( sFile );
//				int iBytes = is.read(m_readBuffer);
////				DEBUG_MSG( "allocating " + iBytes + " for file " + sFile );
//				abyBuffer = new byte[iBytes];
//				System.arraycopy( m_readBuffer, 0, abyBuffer, 0, iBytes );

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -