tb_system_android.cpp 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. // ================================================================================
  2. // == This file is a part of Turbo Badger. (C) 2011-2014, Emil Segerås ==
  3. // == See tb_core.h for more information. ==
  4. // ================================================================================
  5. #include "tb_system.h"
  6. #ifdef TB_SYSTEM_ANDROID_DISABLED
  7. #include <android/log.h>
  8. #include <sys/time.h>
  9. #include <stdio.h>
  10. // for native asset manager
  11. #include <sys/types.h>
  12. #include <android/asset_manager.h>
  13. #include <android/asset_manager_jni.h>
  14. #include <android/configuration.h>
  15. #ifdef TB_RUNTIME_DEBUG_INFO
  16. #define LOG_TAG "TB"
  17. #define LOGI(...) __android_log_print(ANDROID_LOG_INFO, LOG_TAG, __VA_ARGS__)
  18. #define LOGE(...) __android_log_print(ANDROID_LOG_ERROR ,LOG_TAG, __VA_ARGS__)
  19. void TBDebugOut(const char *str)
  20. {
  21. LOGI(str);
  22. }
  23. #endif // TB_RUNTIME_DEBUG_INFO
  24. AAssetManager *g_pManager = NULL;
  25. void SetAssetManager(AAssetManager *pManager)
  26. {
  27. g_pManager = pManager;
  28. }
  29. namespace tb {
  30. // == TBSystem ========================================
  31. double TBSystem::GetTimeMS()
  32. {
  33. struct timeval now;
  34. gettimeofday(&now, NULL);
  35. return now.tv_usec / 1000 + now.tv_sec * 1000;
  36. }
  37. void TBSystem::RescheduleTimer(double fire_time)
  38. {
  39. }
  40. int TBSystem::GetLongClickDelayMS()
  41. {
  42. return 500;
  43. }
  44. int TBSystem::GetPanThreshold()
  45. {
  46. return 5 * GetDPI() / 120;
  47. }
  48. int TBSystem::GetPixelsPerLine()
  49. {
  50. return 40 * GetDPI() / 120;
  51. }
  52. int TBSystem::GetDPI()
  53. {
  54. AConfiguration *config = AConfiguration_new();
  55. AConfiguration_fromAssetManager(config, g_pManager);
  56. int32_t density = AConfiguration_getDensity(config);
  57. AConfiguration_delete(config);
  58. if (density == 0 || density == ACONFIGURATION_DENSITY_NONE)
  59. return 120;
  60. return density;
  61. }
  62. // == TBFile =====================================
  63. class TBAndroidFile : public TBFile
  64. {
  65. public:
  66. TBAndroidFile(AAsset* f) : file(f) {}
  67. virtual ~TBAndroidFile() { AAsset_close(file); }
  68. virtual long Size()
  69. {
  70. return AAsset_getLength(file);
  71. }
  72. virtual size_t Read(void *buf, size_t elemSize, size_t count)
  73. {
  74. return AAsset_read(file, buf, elemSize * count);
  75. }
  76. private:
  77. AAsset *file;
  78. };
  79. TBFile *TBFile::Open(const char *filename, TBFileMode mode)
  80. {
  81. AAsset *f = nullptr;
  82. switch (mode)
  83. {
  84. case MODE_READ:
  85. f = AAssetManager_open(g_pManager, filename, AASSET_MODE_UNKNOWN);
  86. break;
  87. default:
  88. break;
  89. }
  90. if (!f)
  91. return nullptr;
  92. TBAndroidFile *tbf = new TBAndroidFile(f);
  93. if (!tbf)
  94. AAsset_close(f);
  95. return tbf;
  96. }
  97. }; // namespace tb
  98. #endif // TB_SYSTEM_ANDROID