EmscriptenConsole.cpp 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. //-----------------------------------------------------------------------------
  2. // Copyright (c) 2013 GarageGames, LLC
  3. // Portions Copyright (c) 2014 James S Urquhart
  4. //
  5. // Permission is hereby granted, free of charge, to any person obtaining a copy
  6. // of this software and associated documentation files (the "Software"), to
  7. // deal in the Software without restriction, including without limitation the
  8. // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  9. // sell copies of the Software, and to permit persons to whom the Software is
  10. // furnished to do so, subject to the following conditions:
  11. //
  12. // The above copyright notice and this permission notice shall be included in
  13. // all copies or substantial portions of the Software.
  14. //
  15. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  18. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  20. // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  21. // IN THE SOFTWARE.
  22. //-----------------------------------------------------------------------------
  23. #include "platformEmscripten/platformEmscripten.h"
  24. #include "platformEmscripten/EmscriptenCOnsole.h"
  25. #include "platform/event.h"
  26. #include "game/gameInterface.h"
  27. #include <stdio.h>
  28. extern "C"
  29. {
  30. extern void js_ConsoleEnabled(const char *prompt);
  31. extern void js_ConsoleDisabled();
  32. extern void js_ConsoleLine(const char *message);
  33. }
  34. EmscriptenConsole *gConsole = NULL;
  35. ConsoleFunction(enableWinConsole, void, 2, 2, "(bool enable)")
  36. {
  37. if (gConsole)
  38. gConsole->enable(dAtob(argv[1]));
  39. }
  40. ConsoleFunction(enableDebugOutput, void, 2, 2, "(bool enable)")
  41. {
  42. if (gConsole)
  43. gConsole->enableDebugOutput(dAtob(argv[1]));
  44. }
  45. static void EmscriptenConsoleConsumer(ConsoleLogEntry::Level, const char *line)
  46. {
  47. if (gConsole)
  48. gConsole->processConsoleLine(line);
  49. }
  50. void EmscriptenConsole::create()
  51. {
  52. gConsole = new EmscriptenConsole();
  53. }
  54. void EmscriptenConsole::destroy()
  55. {
  56. if (gConsole)
  57. delete gConsole;
  58. gConsole = NULL;
  59. }
  60. void EmscriptenConsole::enable(bool enabled)
  61. {
  62. if (gConsole == NULL)
  63. return;
  64. consoleEnabled = enabled;
  65. if(consoleEnabled)
  66. {
  67. printf("Initializing Console...\n");
  68. printf("Console Initialized.\n");
  69. js_ConsoleEnabled(Con::getVariable("Con::Prompt"));
  70. }
  71. else
  72. {
  73. printf("Deactivating Console.");
  74. js_ConsoleDisabled();
  75. }
  76. }
  77. void EmscriptenConsole::enableDebugOutput(bool enabled)
  78. {
  79. if (gConsole != NULL)
  80. debugOutputEnabled = enabled;
  81. }
  82. bool EmscriptenConsole::isEnabled()
  83. {
  84. return gConsole ? gConsole->consoleEnabled : false;
  85. }
  86. EmscriptenConsole::EmscriptenConsole()
  87. {
  88. consoleEnabled = false;
  89. clearInBuf();
  90. Con::addConsumer(EmscriptenConsoleConsumer);
  91. }
  92. EmscriptenConsole::~EmscriptenConsole()
  93. {
  94. Con::removeConsumer(EmscriptenConsoleConsumer);
  95. }
  96. void EmscriptenConsole::processConsoleLine(const char *consoleLine)
  97. {
  98. if(consoleEnabled)
  99. {
  100. printf("%s\n", consoleLine);
  101. }
  102. }
  103. void EmscriptenConsole::clearInBuf()
  104. {
  105. dMemset(inBuf, 0, MaxConsoleLineSize);
  106. inBufPos=0;
  107. }
  108. void Torque2D_postConsole(const char *buffer)
  109. {
  110. if (!gConsole)
  111. return;
  112. dStrncpy(gConsole->postEvent.data, buffer, MaxConsoleLineSize-1);
  113. gConsole->postEvent.data[MaxConsoleLineSize] = '\0';
  114. gConsole->postEvent.size = ConsoleEventHeaderSize + dStrlen(gConsole->postEvent.data) + 1;
  115. Con::printf("=> %s",gConsole->postEvent.data);
  116. Game->postEvent(gConsole->postEvent);
  117. }