PythonInterface.cpp 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. /*
  2. * This source file is part of libRocket, the HTML/CSS Interface Middleware
  3. *
  4. * For the latest information, see http://www.librocket.com
  5. *
  6. * Copyright (c) 2008-2010 CodePoint Ltd, Shift Technology Ltd
  7. *
  8. * Permission is hereby granted, free of charge, to any person obtaining a copy
  9. * of this software and associated documentation files (the "Software"), to deal
  10. * in the Software without restriction, including without limitation the rights
  11. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  12. * copies of the Software, and to permit persons to whom the Software is
  13. * furnished to do so, subject to the following conditions:
  14. *
  15. * The above copyright notice and this permission notice shall be included in
  16. * all copies or substantial portions of the Software.
  17. *
  18. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  19. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  20. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  21. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  22. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  23. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  24. * THE SOFTWARE.
  25. *
  26. */
  27. #include "PythonInterface.h"
  28. #include <Shell.h>
  29. #include "GameDetails.h"
  30. #include "ElementGame.h"
  31. #include "HighScores.h"
  32. void SubmitHighScore()
  33. {
  34. int score = GameDetails::GetScore();
  35. if (score > 0)
  36. {
  37. // Submit the score the player just got to the high scores chart.
  38. HighScores::SubmitScore(GameDetails::GetDefenderColour(), GameDetails::GetWave(), GameDetails::GetScore());
  39. // Reset the score so the chart won't get confused next time we enter.
  40. GameDetails::ResetScore();
  41. }
  42. }
  43. BOOST_PYTHON_MODULE(game)
  44. {
  45. python::def("Shutdown", &Shell::RequestExit);
  46. python::def("SetPaused", &GameDetails::SetPaused);
  47. python::def("SetDifficulty", &GameDetails::SetDifficulty);
  48. python::def("SetDefenderColour", &GameDetails::SetDefenderColour);
  49. python::def("SubmitHighScore", &SubmitHighScore);
  50. python::def("SetHighScoreName", &HighScores::SubmitName);
  51. python::enum_<GameDetails::Difficulty>("difficulty")
  52. .value("HARD", GameDetails::HARD)
  53. .value("EASY", GameDetails::EASY)
  54. ;
  55. ElementGame::InitialisePythonInterface();
  56. }
  57. PythonInterface::PythonInterface()
  58. {
  59. }
  60. PythonInterface::~PythonInterface()
  61. {
  62. }
  63. bool PythonInterface::Initialise(const char* path)
  64. {
  65. // Initialise Python.
  66. Py_Initialize();
  67. // Setup the Python search path.
  68. const char* python_path = Py_GetPath();
  69. char buffer[1024];
  70. snprintf(buffer, 1024, "%s%s%s", path, PATH_SEPARATOR, python_path);
  71. buffer[1023] = '\0';
  72. PySys_SetPath(buffer);
  73. // Import Rocket.
  74. if (!Import("rocket"))
  75. return false;
  76. // Define our game specific interface.
  77. initgame();
  78. return true;
  79. }
  80. void PythonInterface::Shutdown()
  81. {
  82. Py_Finalize();
  83. }
  84. bool PythonInterface::Import(const Rocket::Core::String& name)
  85. {
  86. PyObject* module = PyImport_ImportModule(name.CString());
  87. if (!module)
  88. {
  89. PrintError(true);
  90. return false;
  91. }
  92. Py_DECREF(module);
  93. return true;
  94. }
  95. // Print the pending python error to stderr, optionally clearing it
  96. void PythonInterface::PrintError(bool clear_error)
  97. {
  98. // Print the error and restore it to the caller
  99. PyObject *type, *value, *traceback;
  100. PyErr_Fetch(&type, &value, &traceback);
  101. Py_XINCREF(type);
  102. Py_XINCREF(value);
  103. Py_XINCREF(traceback);
  104. PyErr_Restore(type, value, traceback);
  105. PyErr_Print();
  106. if (!clear_error)
  107. PyErr_Restore(type, value, traceback);
  108. }