PolyCore.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369
  1. /*
  2. Copyright (C) 2011 by Ivan Safrin
  3. Permission is hereby granted, free of charge, to any person obtaining a copy
  4. of this software and associated documentation files (the "Software"), to deal
  5. in the Software without restriction, including without limitation the rights
  6. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  7. copies of the Software, and to permit persons to whom the Software is
  8. furnished to do so, subject to the following conditions:
  9. The above copyright notice and this permission notice shall be included in
  10. all copies or substantial portions of the Software.
  11. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  12. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  13. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  14. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  15. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  16. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  17. THE SOFTWARE.
  18. */
  19. #pragma once
  20. #include "PolyGlobals.h"
  21. #include "PolyString.h"
  22. #include "PolyRectangle.h"
  23. #include "PolyVector2.h"
  24. #include "PolyEventDispatcher.h"
  25. #include "PolyCoreInput.h"
  26. #include "PolyCoreServices.h"
  27. #include "PolyThreaded.h"
  28. #define CURSOR_ARROW 0
  29. #define CURSOR_TEXT 1
  30. #define CURSOR_POINTER 2
  31. #define CURSOR_CROSSHAIR 3
  32. #define CURSOR_RESIZE_LEFT_RIGHT 4
  33. #define CURSOR_RESIZE_UP_DOWN 5
  34. long getThreadID();
  35. namespace Polycode {
  36. class Renderer;
  37. class _PolyExport CoreMutex {
  38. public:
  39. int mutexID;
  40. };
  41. class _PolyExport CoreFileExtension {
  42. public:
  43. String extension;
  44. String description;
  45. };
  46. class _PolyExport PolycodeViewBase {
  47. public:
  48. PolycodeViewBase() { windowData = NULL; }
  49. virtual ~PolycodeViewBase(){}
  50. void *windowData;
  51. };
  52. class _PolyExport TimeInfo {
  53. public:
  54. TimeInfo();
  55. int seconds;
  56. int minutes;
  57. int hours;
  58. int month;
  59. int monthDay;
  60. int weekDay;
  61. int year;
  62. int yearDay;
  63. };
  64. /**
  65. * The main core of the framework. The core deals with system-level functions, such as window initialization and OS interaction. Each platform has its own implementation of this base class. NOTE: SOME OF THE FUNCTIONALITY IN THE CORE IS NOT FULLY IMPLEMENTED!!
  66. */
  67. class _PolyExport Core : public EventDispatcher {
  68. public:
  69. /**
  70. * Constructor.
  71. * @param xRes Inital horizontal resolution of the renderer.
  72. * @param yRes Inital vertical resolution of the renderer.
  73. * @param fullScreen True to launch in fullscreen, false to launch in window.
  74. * @param aaLevel Level of anti-aliasing. Possible values are 2,4 and 6.
  75. * @param frameRate Frame rate that the core will update and render at.
  76. * @param monitorIndex If fullScreen is true, the monitor index to fullscreen to. Pass -1 to use primary monitor.
  77. */
  78. Core(int xRes, int yRes, bool fullScreen, bool vSync, int aaLevel, int anisotropyLevel, int frameRate, int monitorIndex);
  79. virtual ~Core();
  80. virtual bool Update() = 0;
  81. /**
  82. * Show or hide cursor.
  83. * @param newval True to show mouse, false to hide it.
  84. */
  85. virtual void enableMouse(bool newval);
  86. /**
  87. * Sets the cursor the application is using.
  88. * @param cursorType Type of cursor to use. Possible values are CURSOR_ARROW, CURSOR_TEXT, CURSOR_POINTER, CURSOR_CROSSHAIR, CURSOR_RESIZE_LEFT_RIGHT, CURSOR_RESIZE_UP_DOWN
  89. */
  90. virtual void setCursor(int cursorType) = 0;
  91. /**
  92. * Warps the cursor to a specified point in the window.
  93. * @param x New cursor x position
  94. * @param y New cursor y position
  95. */
  96. virtual void warpCursor(int x, int y) {}
  97. /**
  98. * Launches a Threaded class into its own thread. See the documentation for Threaded for information on how to crated threaded classes.
  99. * @param target Target threaded class.
  100. * @see Threaded
  101. */
  102. virtual void createThread(Threaded *target);
  103. /**
  104. * Locks a mutex.
  105. * @param mutex Mutex to lock.
  106. */
  107. virtual void lockMutex(CoreMutex *mutex) = 0;
  108. /**
  109. * Unlocks a mutex.
  110. * @param mutex Mutex to lock.
  111. */
  112. virtual void unlockMutex(CoreMutex *mutex) = 0;
  113. /**
  114. * Creates a mutex
  115. * @return Newly created mutex.
  116. */
  117. virtual CoreMutex *createMutex() = 0;
  118. /**
  119. * Copies the specified string to system clipboard.
  120. * @param str String to copy to clipboard.
  121. */
  122. virtual void copyStringToClipboard(const String& str) = 0;
  123. /**
  124. * Returns the system clipboard as a string.
  125. * @return String from clipboard.
  126. */
  127. virtual String getClipboardString() = 0;
  128. /**
  129. * Returns the core services. See CoreServices for a detailed explanation of services.
  130. * @return Core services.
  131. @see CoreServices
  132. */
  133. CoreServices *getServices();
  134. /**
  135. * Returns the current average frames per second.
  136. * @return Current average frames per second.
  137. */
  138. Number getFPS();
  139. /**
  140. * Shuts down the core and quits the application.
  141. */
  142. void Shutdown();
  143. /**
  144. * Checks if core is in fullscreen mode.
  145. * @return True if in full screen, false if otherwise.
  146. */
  147. bool isFullscreen(){ return fullScreen; }
  148. /**
  149. * Returns the current anti-aliasing level.
  150. * @return Current anti-aliasing level.
  151. */
  152. int getAALevel() { return aaLevel; }
  153. /**
  154. * Returns the input class. See CoreInput for details in input.
  155. * @return Input class.
  156. * @see CoreInput
  157. */
  158. CoreInput *getInput();
  159. /**
  160. * Returns current horizontal resolution.
  161. * @return Current horizontal resolution.
  162. */
  163. Number getXRes();
  164. /**
  165. * Returns current vertical resolution.
  166. * @return Current vertical resolution.
  167. */
  168. Number getYRes();
  169. // deprecated
  170. int getNumVideoModes();
  171. /**
  172. * Returns the available system video modes.
  173. * @return An STL vector of video modes.
  174. */
  175. virtual std::vector<Rectangle> getVideoModes() = 0;
  176. /**
  177. * Provides the current width, height, and refresh rate of the screen.
  178. * @param width If non-NULL, current screen width will be written here (or 0 if unknown).
  179. * @param hight If non-NULL, current screen height will be written here (or 0 if unknown).
  180. * @param hz If non-NULL, current screen refresh rate will be written here (or 0 if unknown).
  181. */
  182. static void getScreenInfo(int *width, int *height, int *hz);
  183. /**
  184. * Creates a folder on disk with the specified path.
  185. * @param folderPath Path to create the folder in.
  186. */
  187. virtual void createFolder(const String& folderPath) = 0;
  188. /**
  189. * Copies a disk item from one path to another
  190. * @param itemPath Path to the item to copy.
  191. * @param destItemPath Destination path to copy to.
  192. */
  193. virtual void copyDiskItem(const String& itemPath, const String& destItemPath) = 0;
  194. /**
  195. * Moves a disk item from one path to another
  196. * @param itemPath Path to the item to move.
  197. * @param destItemPath Destination path to move to.
  198. */
  199. virtual void moveDiskItem(const String& itemPath, const String& destItemPath) = 0;
  200. /**
  201. * Removes a disk item.
  202. * @param itemPath Path to the item to remove.
  203. */
  204. virtual void removeDiskItem(const String& itemPath) = 0;
  205. /**
  206. * Opens a system folder picker and suspends operation.
  207. * @return The selected path returned from the picker.
  208. */
  209. virtual String openFolderPicker() = 0;
  210. /**
  211. * Opens a system file picker for the specified extensions.
  212. * @param extensions An STL vector containing the allowed file extensions that can be selected.
  213. * @param allowMultiple If set to true, the picker can select multiple files.
  214. * @return An STL vector of the selected file paths.
  215. */
  216. virtual std::vector<String> openFilePicker(std::vector<CoreFileExtension> extensions, bool allowMultiple) = 0;
  217. void setVideoModeIndex(int index, bool fullScreen, bool vSync, int aaLevel, int anisotropyLevel);
  218. /**
  219. * Sets a new video mode.
  220. * @param xRes New horizontal resolution of the renderer.
  221. * @param yRes New vertical resolution of the renderer.
  222. * @param fullScreen True to launch in fullscreen, false to launch in window.
  223. * @param aaLevel Level of anti-aliasing. Possible values are 2,4 and 6.
  224. */
  225. virtual void setVideoMode(int xRes, int yRes, bool fullScreen, bool vSync, int aaLevel, int anisotropyLevel) = 0;
  226. /**
  227. * Resizes the renderer.
  228. * @param xRes New horizontal resolution of the renderer.
  229. * @param yRes New vertical resolution of the renderer.
  230. */
  231. virtual void resizeTo(int xRes, int yRes) = 0;
  232. void doSleep();
  233. /**
  234. * Launches the default browser and directs it to specified URL
  235. * @param url URL to launch.
  236. */
  237. virtual void openURL(String url) = 0;
  238. /**
  239. * Returns the time elapsed since last frame.
  240. * @return Time elapsed since last frame in floating point microseconds.
  241. */
  242. Number getElapsed();
  243. /**
  244. * Returns the total ticks elapsed since launch.
  245. * @return Time elapsed since launch in milliseconds
  246. */
  247. virtual unsigned int getTicks() = 0;
  248. /**
  249. * Returns the total ticks elapsed since launch.
  250. * @return Time elapsed since launch in floating point microseconds.
  251. */
  252. Number getTicksFloat();
  253. void setUserPointer(void *ptr) { userPointer = ptr; }
  254. void *getUserPointer() { return userPointer; }
  255. static const int EVENT_CORE_RESIZE = 0;
  256. /**
  257. * Returns the default working path of the application.
  258. */
  259. String getDefaultWorkingDirectory();
  260. /**
  261. * Returns the default working path of the application.
  262. */
  263. String getUserHomeDirectory();
  264. CoreMutex *getEventMutex();
  265. CoreMutex *eventMutex;
  266. void removeThread(Threaded *thread);
  267. protected:
  268. String userHomeDirectory;
  269. String defaultWorkingDirectory;
  270. void *userPointer;
  271. long refreshInterval;
  272. bool fullScreen;
  273. int aaLevel;
  274. std::vector<Vector2> videoModes;
  275. void updateCore();
  276. int numVideoModes;
  277. bool running;
  278. Number fps;
  279. unsigned int frameTicks;
  280. unsigned int lastFrameTicks;
  281. unsigned int lastFPSTicks;
  282. unsigned int elapsed;
  283. bool mouseEnabled;
  284. unsigned int lastSleepFrameTicks;
  285. std::vector<Threaded*> threads;
  286. CoreMutex *threadedEventMutex;
  287. int xRes;
  288. int yRes;
  289. int monitorIndex;
  290. int frames;
  291. CoreInput *input;
  292. Renderer *renderer;
  293. CoreServices *services;
  294. };
  295. }