godot_win.cpp 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. /*************************************************************************/
  2. /* godot_win.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* http://www.godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
  9. /* */
  10. /* Permission is hereby granted, free of charge, to any person obtaining */
  11. /* a copy of this software and associated documentation files (the */
  12. /* "Software"), to deal in the Software without restriction, including */
  13. /* without limitation the rights to use, copy, modify, merge, publish, */
  14. /* distribute, sublicense, and/or sell copies of the Software, and to */
  15. /* permit persons to whom the Software is furnished to do so, subject to */
  16. /* the following conditions: */
  17. /* */
  18. /* The above copyright notice and this permission notice shall be */
  19. /* included in all copies or substantial portions of the Software. */
  20. /* */
  21. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  22. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  23. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
  24. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  25. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  26. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  27. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  28. /*************************************************************************/
  29. #include "os_windows.h"
  30. #include "main/main.h"
  31. #include <stdio.h>
  32. PCHAR*
  33. CommandLineToArgvA(
  34. PCHAR CmdLine,
  35. int* _argc
  36. )
  37. {
  38. PCHAR* argv;
  39. PCHAR _argv;
  40. ULONG len;
  41. ULONG argc;
  42. CHAR a;
  43. ULONG i, j;
  44. BOOLEAN in_QM;
  45. BOOLEAN in_TEXT;
  46. BOOLEAN in_SPACE;
  47. len = strlen(CmdLine);
  48. i = ((len+2)/2)*sizeof(PVOID) + sizeof(PVOID);
  49. argv = (PCHAR*)GlobalAlloc(GMEM_FIXED,
  50. i + (len+2)*sizeof(CHAR));
  51. _argv = (PCHAR)(((PUCHAR)argv)+i);
  52. argc = 0;
  53. argv[argc] = _argv;
  54. in_QM = FALSE;
  55. in_TEXT = FALSE;
  56. in_SPACE = TRUE;
  57. i = 0;
  58. j = 0;
  59. while( (a = CmdLine[i]) ) {
  60. if(in_QM) {
  61. if(a == '\"') {
  62. in_QM = FALSE;
  63. } else {
  64. _argv[j] = a;
  65. j++;
  66. }
  67. } else {
  68. switch(a) {
  69. case '\"':
  70. in_QM = TRUE;
  71. in_TEXT = TRUE;
  72. if(in_SPACE) {
  73. argv[argc] = _argv+j;
  74. argc++;
  75. }
  76. in_SPACE = FALSE;
  77. break;
  78. case ' ':
  79. case '\t':
  80. case '\n':
  81. case '\r':
  82. if(in_TEXT) {
  83. _argv[j] = '\0';
  84. j++;
  85. }
  86. in_TEXT = FALSE;
  87. in_SPACE = TRUE;
  88. break;
  89. default:
  90. in_TEXT = TRUE;
  91. if(in_SPACE) {
  92. argv[argc] = _argv+j;
  93. argc++;
  94. }
  95. _argv[j] = a;
  96. j++;
  97. in_SPACE = FALSE;
  98. break;
  99. }
  100. }
  101. i++;
  102. }
  103. _argv[j] = '\0';
  104. argv[argc] = NULL;
  105. (*_argc) = argc;
  106. return argv;
  107. }
  108. int main(int argc, char** argv) {
  109. OS_Windows os(NULL);
  110. Main::setup(argv[0], argc - 1, &argv[1]);
  111. if (Main::start())
  112. os.run();
  113. Main::cleanup();
  114. return os.get_exit_code();
  115. };
  116. HINSTANCE godot_hinstance = NULL;
  117. int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow) {
  118. int argc;
  119. char** argv;
  120. char* arg;
  121. int index;
  122. int result;
  123. // count the arguments
  124. argc = 1;
  125. arg = lpCmdLine;
  126. while (arg[0] != 0) {
  127. while (arg[0] != 0 && arg[0] == ' ') {
  128. arg++;
  129. }
  130. if (arg[0] != 0) {
  131. argc++;
  132. while (arg[0] != 0 && arg[0] != ' ') {
  133. arg++;
  134. }
  135. }
  136. }
  137. // tokenize the arguments
  138. argv = (char**)malloc(argc * sizeof(char*));
  139. arg = lpCmdLine;
  140. index = 1;
  141. while (arg[0] != 0) {
  142. while (arg[0] != 0 && arg[0] == ' ') {
  143. arg++;
  144. }
  145. if (arg[0] != 0) {
  146. argv[index] = arg;
  147. index++;
  148. while (arg[0] != 0 && arg[0] != ' ') {
  149. arg++;
  150. }
  151. if (arg[0] != 0) {
  152. arg[0] = 0;
  153. arg++;
  154. }
  155. }
  156. }
  157. // put the program name into argv[0]
  158. char filename[_MAX_PATH];
  159. GetModuleFileName(NULL, filename, _MAX_PATH);
  160. argv[0] = filename;
  161. // call the user specified main function
  162. result = main(argc, argv);
  163. free(argv);
  164. return result;
  165. }