Counter Strike : Global Offensive Source Code
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

162 lines
4.6 KiB

  1. // minimalSqPlus.cpp
  2. // A minimal example and an example with a simple class.
  3. // Example for use with the remote debugger is also shown.
  4. // Created 10/08/05, John Schultz
  5. // Free for any use.
  6. #include <stdio.h>
  7. #include <stdarg.h>
  8. #include "sqplus.h"
  9. using namespace SqPlus;
  10. // Define MINIMAL_EXAMPLE for minimal HelloWorld example.
  11. //#define MINIMAL_EXAMPLE
  12. // Define USE_REMOTE_DEBUGGER to use the remote debugger. See the example and docs in the sqdbg directory for
  13. // more information.
  14. //#define USE_REMOTE_DEBUGGER // Remote debugger must be started after the program is launched.
  15. #ifdef USE_REMOTE_DEBUGGER
  16. #include "sqrdbg.h"
  17. #endif
  18. // The following pragmas can be used to link the correct library for Debug/Release builds
  19. // without requiring the application to include the associated Squirrel projects.
  20. // (The libraries must be created before compiling this example).
  21. // Set SQ_REL_PATH as needed for your application.
  22. #define SQ_REL_PATH "../"
  23. #ifdef UNICODE
  24. #define LIB "U.lib"
  25. #else
  26. #define LIB ".lib"
  27. #endif
  28. #ifdef _MSC_VER
  29. #ifdef _DEBUG
  30. #pragma comment(lib,SQ_REL_PATH "lib/squirrelD" LIB)
  31. #pragma comment(lib,SQ_REL_PATH "lib/sqstdlibD" LIB)
  32. #ifdef USE_REMOTE_DEBUGGER
  33. #pragma comment(lib,SQ_REL_PATH "lib/sqdbglibD" LIB)
  34. #endif
  35. #pragma comment(lib,SQ_REL_PATH "lib/sqplusD" LIB)
  36. #else // Release
  37. #pragma comment(lib,SQ_REL_PATH "lib/squirrel" LIB)
  38. #pragma comment(lib,SQ_REL_PATH "lib/sqstdlib" LIB)
  39. #ifdef USE_REMOTE_DEBUGGER
  40. #pragma comment(lib,SQ_REL_PATH "lib/sqdbglib" LIB)
  41. #endif
  42. #pragma comment(lib,SQ_REL_PATH "lib/sqplus" LIB)
  43. #endif
  44. #endif
  45. #ifdef USE_REMOTE_DEBUGGER
  46. void printSQDBGError(HSQUIRRELVM v) {
  47. const SQChar *err;
  48. sq_getlasterror(v);
  49. if(SQ_SUCCEEDED(sq_getstring(v,-1,&err))) {
  50. scprintf(_T("SQDBG error : %s"),err);
  51. }else {
  52. scprintf(_T("SQDBG error"),err);
  53. } // if
  54. sq_poptop(v);
  55. } // printSQDBGError
  56. #endif
  57. static void printFunc(HSQUIRRELVM v,const SQChar * s,...) {
  58. static SQChar temp[2048];
  59. va_list vl;
  60. va_start(vl,s);
  61. scvsprintf( temp,s,vl);
  62. SCPUTS(temp);
  63. va_end(vl);
  64. } // printFunc
  65. class MyClass {
  66. public:
  67. int classVal;
  68. // See examples in testSqPlus2.cpp for passing arguments to the constructor (including variable arguments).
  69. MyClass() : classVal(123) {}
  70. bool process(int iVal,const SQChar * sVal) {
  71. scprintf(_T("classVal: %d, iVal: %d, sVal %s\n"),classVal,iVal,sVal);
  72. classVal += iVal;
  73. return iVal > 1;
  74. } // process
  75. };
  76. #ifdef MINIMAL_EXAMPLE
  77. int main(int argc,char * argv[]) {
  78. SquirrelVM::Init();
  79. SquirrelObject helloWorld = SquirrelVM::CompileBuffer(_T("print(\"Hello World\");"));
  80. SquirrelVM::RunScript(helloWorld);
  81. SquirrelVM::Shutdown();
  82. scprintf(_T("Press RETURN to exit."));
  83. getchar();
  84. } // main
  85. #else
  86. int main(int argc,char * argv[]) {
  87. SquirrelVM::Init();
  88. // This example shows how to redirect print output to your own custom
  89. // print function (the default handler prints to stdout).
  90. sq_setprintfunc(SquirrelVM::GetVMPtr(),printFunc);
  91. #ifdef USE_REMOTE_DEBUGGER
  92. HSQREMOTEDBG rdbg = sq_rdbg_init(SquirrelVM::GetVMPtr(),1234,SQTrue);
  93. if(rdbg) {
  94. // Enable debug info generation (for the compiler).
  95. sq_enabledebuginfo(SquirrelVM::GetVMPtr(),SQTrue);
  96. scprintf(_T("Waiting for SQDBG connection..."));
  97. // Suspends the app until the debugger client connects.
  98. if(SQ_SUCCEEDED(sq_rdbg_waitforconnections(rdbg))) {
  99. printf("SQDBG: connected.\n");
  100. } // if
  101. } else {
  102. printSQDBGError(SquirrelVM::GetVMPtr());
  103. return 0;
  104. } // if
  105. #endif
  106. // See examples in testSqPlus2.cpp for script read-only vars, constants,
  107. // enums, static/global functions, variable arguments, constructor arguments,
  108. // passing/returning classes/structs by value or by address, etc.
  109. SQClassDef<MyClass>(_T("MyClass")).
  110. func(&MyClass::process,_T("process")).
  111. var(&MyClass::classVal,_T("classVal"));
  112. SquirrelObject helloSqPlus = SquirrelVM::CompileBuffer(_T("\
  113. local myClass = MyClass(); \n\
  114. local rVal = myClass.process(1,\"MyClass1\"); \n\
  115. print(\"Returned: \"+(rVal ? \"true\" : \"false\")); \n\
  116. rVal = myClass.process(2,\"MyClass2\"); \n\
  117. print(\"Returned: \"+(rVal ? \"true\" : \"false\")); \n\
  118. print(\"classVal: \"+myClass.classVal); \n\
  119. "));
  120. try {
  121. SquirrelVM::RunScript(helloSqPlus);
  122. } catch (SquirrelError & e) {
  123. scprintf(_T("Error: %s, %s\n"),e.desc,_T("Squirrel::helloSqPlus"));
  124. } // catch
  125. #ifdef USE_REMOTE_DEBUGGER
  126. if (rdbg) {
  127. sq_rdbg_shutdown(rdbg);
  128. } // if
  129. #endif
  130. SquirrelVM::Shutdown();
  131. scprintf(_T("Press RETURN to exit."));
  132. getchar();
  133. return 0;
  134. } // main
  135. #endif
  136. // minimalSqPlus.cpp