And yes, I'm interested in an Android port.. As Android is in my list of platforms that would be nice to port my iPhone games to
![Smile :-)](./images/smilies/icon_smile.gif)
Code: Select all
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
AssetManager assetManager = getAssets();
String[] files = null;
try
{
files = assetManager.list("");
}catch (IOException e)
{
Log.e(LOG_TAG, e.getMessage());
}
String strSdcardPath = "/sdcard/";
String strDestFile;
int iFileCnt = files.length;
for( int i = 0; i < iFileCnt; i++)
{
String strFileNameSrc = files[i];
if( strFileNameSrc.contentEquals("images" ) ||
strFileNameSrc.contentEquals("sounds" ) ||
strFileNameSrc.contentEquals("skins" ) ||
strFileNameSrc.contentEquals("webkit" ) )
{
// dont copy.. not really a asset file
continue;
}
strDestFile = strSdcardPath + strFileNameSrc;
VxCopyAsssetToSdcard(strFileNameSrc, strDestFile, true);
}
//////////////////////////////////
int VxCopyAsssetToSdcard( String strFileSrc, String strFileDest, boolean bDontCopyIfExistsAndSameSize )
{
int iSrcFileSize = 0;
int iDestFileSize = 0;
InputStream oInputStream = null;
OutputStream oOutputStream = null;
// get src file size and stream
try
{
oInputStream = m_oRes.getAssets().open(strFileSrc);
if( null != oInputStream)
{
iSrcFileSize = oInputStream.available();
}
else
{
Log.e(LOG_TAG, "ERROR: VxCopyResourceToSdcard could not open file " + strFileSrc );
return -1;
}
}
catch (IOException e)
{
Log.e(LOG_TAG, "ERROR: VxCopyResourceToSdcard could not open file " + strFileSrc );
// TODO Auto-generated catch block
e.printStackTrace();
return -1;
}
if( bDontCopyIfExistsAndSameSize )
{
// get dest file size and stream
try
{
InputStream oTmpStream = null;
File readFile = new File( strFileDest);
oTmpStream = new FileInputStream(readFile); ;
if( null != oTmpStream)
{
iDestFileSize = oTmpStream.available();
oTmpStream.close();
}
else
{
Log.e(LOG_TAG, "ERROR: VxCopyAsssetToSdcard could not get size of file " + strFileDest );
return -1;
}
}
catch (IOException e)
{
Log.e(LOG_TAG, "VxCopyAsssetToSdcard dest file doesnt exist.. will copy " + strFileDest );
}
}
if( bDontCopyIfExistsAndSameSize && ( iSrcFileSize == iDestFileSize))
{
Log.i(LOG_TAG, "VxCopyAsssetToSdcard file exists " + strFileDest );
return 0;
}
// Read the entire asset into a local byte buffer.
byte[] buffer = new byte[iSrcFileSize];
try
{
oInputStream.read(buffer);
oInputStream.close();
}
catch (IOException e)
{
Log.e(LOG_TAG, "ERROR: VxCopyAsssetToSdcard could not read file " + strFileSrc );
// TODO Auto-generated catch block
e.printStackTrace();
}
// write buffer to destination file
try
{
File oWriteFile = new File( strFileDest);
try
{
oWriteFile.createNewFile();
}
catch (IOException e)
{
Log.e(LOG_TAG, "error creating file " + strFileDest, e);
e.printStackTrace();
return -1;
}
oOutputStream = new FileOutputStream(oWriteFile);
//Don’t forget to close stream
if (oOutputStream != null)
{
try
{
oOutputStream.write(buffer);
oOutputStream.flush();
oOutputStream.close();
Log.i(LOG_TAG, "Success copying file " + strFileDest + " size " + iSrcFileSize );
}
catch (IOException e)
{
Log.e(LOG_TAG, "ERROR: VxCopyResourceToSdcard could not write file " + strFileDest );
e.printStackTrace();
return -1;
}
}
}
catch (IOException e)
{
Log.e(LOG_TAG, "ERROR: VxCopyAsssetToSdcard could not read file " + strFileSrc );
// TODO Auto-generated catch block
e.printStackTrace();
return -1;
}
return 0;
}
Code: Select all
#pragma once
//============================================================================
// Copyright (C) 2010-2011 Brett R. Jones
// All Rights Reserved
//
// You may redistribute and/or modify for non commercial and commercial uses
// provided this copyright notice remains in place and is not modified
//
// This code is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
//
// brett.r.jones@gmail.com
// http://www.roguetreasure.com
//============================================================================
typedef void (*LOG_FUNCTION )( unsigned long, char * );
/// log message types
#define MSG_TYPE_STATUS 0x00
#define MSG_TYPE_ERR 0x01
#define MSG_TYPE_FATAL_ERR 0x02
#define MSG_TYPE_DEBUG 0x04
#define MSG_TYPE_LOG 0x08
#define MSG_TYPE_ASSERTION 0x10
// log message source flags.. should be or'd with log message types
// Applications should define their own source flags in available bits 17–32
#define MSG_SRC_UNKNOWN 0x00000000
#define MSG_SRC_TC 0x00000100
#define MSG_SRC_IRR 0x00000200
#define MSG_SRC_GAME 0x00000400
// handy macro to determine if log message is a fatal error
#define IS_FATAL_ERR( msgtype ) ( MSG_TYPE_FATAL_ERR & msgtype )
#define TC_STATUS (MSG_SRC_TC | MSG_TYPE_STATUS)
#define TC_ERR (MSG_SRC_TC | MSG_TYPE_ERR)
#define TC_FATAL_ERR (MSG_SRC_TC | MSG_TYPE_FATAL_ERR)
#define TC_DBG (MSG_SRC_TC | MSG_TYPE_DEBUG)
#define TC_LOG (MSG_SRC_TC | MSG_TYPE_LOG)
#define TC_ASSERTION (MSG_SRC_TC | MSG_TYPE_LOG)
#define IRR_STATUS (MSG_SRC_IRR | MSG_TYPE_STATUS)
#define IRR_ERR (MSG_SRC_IRR | MSG_TYPE_ERR)
#define IRR_FATAL_ERR (MSG_SRC_IRR | MSG_TYPE_FATAL_ERR)
#define IRR_DBG (MSG_SRC_IRR | MSG_TYPE_DEBUG)
#define IRR_LOG (MSG_SRC_IRR | MSG_TYPE_LOG)
#define GAME_STATUS (MSG_SRC_GAME | MSG_TYPE_STATUS)
#define GAME_ERR (MSG_SRC_GAME | MSG_TYPE_ERR)
#define GAME_FATAL_ERR (MSG_SRC_GAME | MSG_TYPE_FATAL_ERR)
#define GAME_DBG (MSG_SRC_GAME | MSG_TYPE_DEBUG)
#define GAME_LOG (MSG_SRC_GAME | MSG_TYPE_LOG)
/// set function that logs a message
void set_log_handler( LOG_FUNCTION pfuncLogHandler );
/// log a message
void log_msg( unsigned long u32MsgType, const char* msg, ...);
#define MAX_ERR_MSG_SIZE 2048
#ifdef _DEBUG
/// This function is called by vx_assert() when the assertion fails.
void vx_error_output(char* exp, char * file, int line);
// debug mode throws message if expression is false
#define vx_assert(exp) { if (!(exp)) vx_error_output((char*)#exp,(char*)__FILE__,__LINE__); }
#define vx_verify(exp) { if (!(exp)) vx_error_output((char*)#exp,(char*)__FILE__,__LINE__); }
#else
// release mode evaluates vx_assert ect to null
#define vx_assert(exp)
#define vx_verify(exp) (exp)
#endif // RELEASE_MODE
Code: Select all
//============================================================================
// Copyright (C) 2010-2011 Brett R. Jones
// All Rights Reserved
//
// You may redistribute and/or modify for non commercial and commercial uses
// provided this copyright notice remains in place and is not modified
//
// This code is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
//
// brett.r.jones@gmail.com
// http://www.roguetreasure.com
//============================================================================#include "VxUtilLib.h"
#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
#ifdef WIN_PLATFORM
#include "windows.h"
#endif// WIN_PLATFORM
#ifdef ANDROID_PLATFORM
#include <android/log.h>
#endif// ANDROID_PLATFORM
void default_log_handler( unsigned long u32MsgType, char * pLogMsg );
void vx_error( const char* msg, ...);
LOG_FUNCTION g_pfuncLogHandler = default_log_handler;
//------------------------------------------------------------------------------
/// This function sets the handler of log messages
void set_log_handler( LOG_FUNCTION pfuncLogHandler )
{
g_pfuncLogHandler = pfuncLogHandler;
}
//------------------------------------------------------------------------------
/// This function is called by vx_assert() when the assertion fails.
void vx_error_output(char* exp, char * file, int line)
{
vx_error("** RT ASSERTION **\r\nexpression: %s\r\nfile: %s\r\nline: %d\r\n", exp, file, line);
}
//------------------------------------------------------------------------------
/// This function is called by vx_assert2() when the assertion fails.
void vx_error_output2(char* exp, char* msg, char* file, int line)
{
vx_error("** RT ASSERTION ***\r\nprogrammer says: %s\r\nexpression: %s\r\nfile: %s\r\nline: %d\r\n", msg, exp, file, line);
}
//------------------------------------------------------------------------------
/// This function is called when a serious situation is encountered which
/// requires abortion of the program.
void vx_error( const char* msg, ...)
{
char as8Buf[ MAX_ERR_MSG_SIZE * 2 ];
va_list argList;
va_start(argList, msg);
vsprintf( as8Buf, msg, argList );
va_end(argList);
// append the module name
//strcat( as8Buf, "Module Name: " );
//char as8ModuleName[ MAX_PATH ];
//VxGetExecuteModuleName( as8ModuleName, sizeof( as8ModuleName ) );
//strcat( as8Buf, as8ModuleName );
//strcat( as8Buf, "\r\n\r\n" );
//printf( as8Buf ); // show the error
//std::string strAssertFile = g_strUserAppDataDir + "vx_assert.txt";
//VxWriteWholeFile( strAssertFile.c_str(), as8Buf, (U32)strlen( as8Buf ) );
// fflush(stdout);
log_msg( MSG_TYPE_ASSERTION, as8Buf ); // send to log
//if( wxTheApp )
//{
// // if widgets is running attept to show the error
// wxMessageBox( as8Buf, "Program Error", wxICON_ERROR | wxICON_STOP );
//}
abort();
};
//------------------------------------------------------------------------------
/// default log handler
void default_log_handler( unsigned long u32MsgType, char * pLogMsg )
{
#ifdef ANDROID_PLATFORM
__android_log_print(ANDROID_LOG_INFO, "AndroidNative", pLogMsg);
#else
printf( pLogMsg );
#endif// ANDROID_PLATFORM
}
//------------------------------------------------------------------------------
/// log a message
void log_msg( unsigned long u32MsgType, const char* msg, ... )
{
char as8Buf[ MAX_ERR_MSG_SIZE ];
va_list argList;
va_start(argList, msg);
vsprintf( as8Buf, msg, argList );
va_end(argList);
#ifdef WIN_PLATFORM
#ifdef _DEBUG
OutputDebugStringA(as8Buf);
#endif //_DEBUG
#endif // WIN_PLATFORM
// send to log handler
g_pfuncLogHandler( u32MsgType, as8Buf );
}