PVRShellOS.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455
  1. /******************************************************************************
  2. @File PVRShellOS.cpp
  3. @Title Qt/PVRShellOS
  4. @Version
  5. @Copyright Copyright (C) Imagination Technologies Limited.
  6. @Platform Non-windowed support for Symbian
  7. @Description Makes programming for 3D APIs easier by wrapping window creation
  8. and other functions for use by a demo.
  9. ******************************************************************************/
  10. #include "PVRShell.h"
  11. #include "PVRShellAPI.h"
  12. #include "PVRShellOS.h"
  13. #include "PVRShellImpl.h"
  14. #include <stdio.h>
  15. #include <stdarg.h>
  16. #include <QApplication>
  17. #include <QMessageBox>
  18. #include <QDesktopWidget>
  19. #include <QKeyEvent>
  20. /*!***************************************************************************
  21. Defines
  22. *****************************************************************************/
  23. /*!***************************************************************************
  24. Consts
  25. *****************************************************************************/
  26. /*!***************************************************************************
  27. Declarations
  28. *****************************************************************************/
  29. /*!***************************************************************************
  30. Class: PVRShellInit
  31. *****************************************************************************/
  32. /*!***********************************************************************
  33. @Function PVRShellOutputDebug
  34. @Input format printf style format followed by arguments it requires
  35. @Description Writes the resultant string to the debug output (e.g. using
  36. printf(), OutputDebugString(), ...). Check the SDK release notes for
  37. details on how the string is output.
  38. *************************************************************************/
  39. void PVRShell::PVRShellOutputDebug(char const * const format, ...) const
  40. {
  41. if(!format)
  42. return;
  43. va_list arg;
  44. char buf[1024];
  45. va_start(arg, format);
  46. vsprintf(buf, format, arg);
  47. va_end(arg);
  48. // Passes the data to a platform dependant function
  49. m_pShellInit->OsDisplayDebugString(buf);
  50. }
  51. /*!***********************************************************************
  52. @Function OsInit
  53. @description Initialisation for OS-specific code.
  54. *************************************************************************/
  55. void PVRShellInit::OsInit()
  56. {
  57. m_Time.restart();
  58. }
  59. /*!***********************************************************************
  60. @Function OsInitOS
  61. @description Saves instance handle and creates main window
  62. In this function, we save the instance handle in a global variable and
  63. create and display the main program window.
  64. *************************************************************************/
  65. bool PVRShellInit::OsInitOS()
  66. {
  67. // Create our window
  68. m_pWindow = new PVRShellMainWindow(this);
  69. if(!m_pWindow)
  70. return false;
  71. m_pWindow->setWindowTitle((const char*) m_pShell->PVRShellGet(prefAppName));
  72. // Resize to fit the screen
  73. QDesktopWidget *desktop = QApplication::desktop();
  74. m_pWindow->resize(desktop->width(), desktop->height());
  75. //m_pWindow->resize(800,600);
  76. //TODO
  77. m_pWindow->showFullScreen();
  78. //m_pWindow->show();
  79. m_pShell->PVRShellSet(prefWidth, desktop->width());
  80. m_pShell->PVRShellSet(prefHeight, desktop->height());
  81. //m_pShell->PVRShellSet(prefWidth, 800);
  82. //m_pShell->PVRShellSet(prefHeight, 600);
  83. return true;
  84. }
  85. /*!***********************************************************************
  86. @Function OsReleaseOS
  87. @description Destroys main window
  88. *************************************************************************/
  89. void PVRShellInit::OsReleaseOS()
  90. {
  91. delete m_pWindow;
  92. m_pWindow = 0;
  93. }
  94. /*!***********************************************************************
  95. @Function OsExit
  96. @description Destroys main window
  97. *************************************************************************/
  98. void PVRShellInit::OsExit()
  99. {
  100. // Show the exit message to the user
  101. m_pShell->PVRShellOutputDebug((const char*)m_pShell->PVRShellGet(prefExitMessage));
  102. const char* pExitMessage = (const char*) m_pShell->PVRShellGet(prefExitMessage);
  103. if(pExitMessage)
  104. {
  105. QMessageBox msg;
  106. msg.setText("Exit message");
  107. msg.setInformativeText(pExitMessage);
  108. msg.exec();
  109. }
  110. }
  111. /*!***********************************************************************
  112. @Function OsDoInitAPI
  113. @Return true on success
  114. @description Perform API initialisation and bring up window / fullscreen
  115. *************************************************************************/
  116. bool PVRShellInit::OsDoInitAPI()
  117. {
  118. if(!ApiInitAPI())
  119. return false;
  120. // Give our window focus so we can capture the key events
  121. m_pWindow->setFocus();
  122. // No problem occured
  123. return true;
  124. }
  125. /*!***********************************************************************
  126. @Function OsDoReleaseAPI
  127. @description Clean up after we're done
  128. *************************************************************************/
  129. void PVRShellInit::OsDoReleaseAPI()
  130. {
  131. ApiReleaseAPI();
  132. }
  133. /*!***********************************************************************
  134. @Function OsRenderComplete
  135. @Returns false when the app should quit
  136. @description Main message loop / render loop
  137. *************************************************************************/
  138. void PVRShellInit::OsRenderComplete()
  139. {
  140. }
  141. /*!***********************************************************************
  142. @Function OsPixmapCopy
  143. @Return true if the copy succeeded
  144. @description When using pixmaps, copy the render to the display
  145. *************************************************************************/
  146. bool PVRShellInit::OsPixmapCopy()
  147. {
  148. return false;
  149. }
  150. /*!***********************************************************************
  151. @Function OsGetNativeDisplayType
  152. @Return The 'NativeDisplayType' for EGL
  153. @description Called from InitAPI() to get the NativeDisplayType
  154. *************************************************************************/
  155. void *PVRShellInit::OsGetNativeDisplayType()
  156. {
  157. return (void*) 0;
  158. }
  159. /*!***********************************************************************
  160. @Function OsGetNativePixmapType
  161. @Return The 'NativePixmapType' for EGL
  162. @description Called from InitAPI() to get the NativePixmapType
  163. *************************************************************************/
  164. void *PVRShellInit::OsGetNativePixmapType()
  165. {
  166. // Pixmap support: return the pixmap
  167. return 0;
  168. }
  169. /*!***********************************************************************
  170. @Function OsGetNativeWindowType
  171. @Return The 'NativeWindowType' for EGL
  172. @description Called from InitAPI() to get the NativeWindowType
  173. *************************************************************************/
  174. void *PVRShellInit::OsGetNativeWindowType()
  175. {
  176. return m_pWindow;
  177. }
  178. /*!***********************************************************************
  179. @Function OsGet
  180. @Input prefName Name of value to get
  181. @Modified pn A pointer set to the value asked for
  182. @Returns true on success
  183. @Description Retrieves OS-specific data
  184. *************************************************************************/
  185. bool PVRShellInit::OsGet(const prefNameIntEnum prefName, int *pn)
  186. {
  187. PVRSHELL_UNREFERENCED_PARAMETER(prefName);
  188. PVRSHELL_UNREFERENCED_PARAMETER(pn);
  189. return false;
  190. }
  191. /*!***********************************************************************
  192. @Function OsGet
  193. @Input prefName Name of value to get
  194. @Modified pp A pointer set to the value asked for
  195. @Returns true on success
  196. @Description Retrieves OS-specific data
  197. *************************************************************************/
  198. bool PVRShellInit::OsGet(const prefNamePtrEnum prefName, void **pp)
  199. {
  200. switch(prefName)
  201. {
  202. case prefPointerLocation:
  203. if(m_pWindow->IsTouched())
  204. *pp = m_pWindow->GetTouchPosition();
  205. else
  206. return false;
  207. return true;
  208. default:
  209. return false;
  210. }
  211. return false;
  212. }
  213. /*!***********************************************************************
  214. @Function OsSet
  215. @Input prefName Name of value to set
  216. @Input i32Value The value to set our named value to
  217. @Returns true on success
  218. @Description Sets OS-specific data
  219. *************************************************************************/
  220. bool PVRShellInit::OsSet(const prefNameIntEnum prefName, const int i32Value)
  221. {
  222. PVRSHELL_UNREFERENCED_PARAMETER(prefName);
  223. PVRSHELL_UNREFERENCED_PARAMETER(i32Value);
  224. return false;
  225. }
  226. /*!***********************************************************************
  227. @Function OsDisplayDebugString
  228. @Input str string to output
  229. @Description Prints a debug string
  230. *************************************************************************/
  231. void PVRShellInit::OsDisplayDebugString(char const * const str)
  232. {
  233. fprintf(stderr, "%s", str);
  234. }
  235. /*!***********************************************************************
  236. @Function OsGetTime
  237. @Return Time in milliseconds since the beginning of the application
  238. @Description Gets the time in milliseconds since the beginning of the application
  239. *************************************************************************/
  240. unsigned long PVRShellInit::OsGetTime()
  241. {
  242. return m_Time.elapsed();
  243. }
  244. /*****************************************************************************
  245. Class: PVRShellInitOS
  246. *****************************************************************************/
  247. PVRShellMainWindow::PVRShellMainWindow(PVRShellInit *pInit) : m_pInit(pInit),
  248. m_bTouch(false)
  249. {
  250. setFocusPolicy(Qt::StrongFocus);
  251. }
  252. void PVRShellMainWindow::keyPressEvent(QKeyEvent *keyEvent)
  253. {
  254. if(m_pInit)
  255. {
  256. switch(keyEvent->key())
  257. {
  258. // Exit
  259. case Qt::Key_Q: m_pInit->KeyPressed(PVRShellKeyNameQUIT); return;
  260. // Arrow keys
  261. case Qt::Key_Up: m_pInit->KeyPressed(m_pInit->m_eKeyMapUP); return;
  262. case Qt::Key_Down: m_pInit->KeyPressed(m_pInit->m_eKeyMapDOWN); return;
  263. case Qt::Key_Left: m_pInit->KeyPressed(m_pInit->m_eKeyMapLEFT); return;
  264. case Qt::Key_Right: m_pInit->KeyPressed(m_pInit->m_eKeyMapRIGHT);return;
  265. // Action and select keys
  266. case Qt::Key_Return: m_pInit->KeyPressed(PVRShellKeyNameSELECT); return;
  267. case Qt::Key_Space: m_pInit->KeyPressed(PVRShellKeyNameACTION1); return;
  268. case Qt::Key_At: m_pInit->KeyPressed(PVRShellKeyNameACTION2); return;
  269. // Screenshot
  270. case Qt::Key_S: m_pInit->KeyPressed(PVRShellKeyNameScreenshot); return;
  271. }
  272. }
  273. QMainWindow::keyPressEvent(keyEvent);
  274. }
  275. void PVRShellMainWindow::mousePressEvent(QMouseEvent *mouseEvent)
  276. {
  277. m_bTouch = mouseEvent->button() == Qt::LeftButton;
  278. if(m_bTouch)
  279. {
  280. QPoint pos = mouseEvent->pos();
  281. QSize s = size();
  282. m_fVec2PointerLocation[0] = pos.x() / (float) s.width();
  283. m_fVec2PointerLocation[1] = pos.y() / (float) s.height();
  284. }
  285. }
  286. void PVRShellMainWindow::mouseReleaseEvent(QMouseEvent *mouseEvent)
  287. {
  288. m_bTouch = !(mouseEvent->button() == Qt::LeftButton);
  289. }
  290. void PVRShellMainWindow::mouseMoveEvent(QMouseEvent *mouseEvent)
  291. {
  292. if(m_bTouch)
  293. {
  294. QPoint pos = mouseEvent->pos();
  295. QSize s = size();
  296. m_fVec2PointerLocation[0] = pos.x() / (float) s.width();
  297. m_fVec2PointerLocation[1] = pos.y() / (float) s.height();
  298. }
  299. }
  300. QSize PVRShellMainWindow::sizeHint()
  301. {
  302. // Return the screen dimensions as our size hint
  303. QDesktopWidget *desktop = QApplication::desktop();
  304. return QSize(desktop->width(), desktop->height());
  305. }
  306. bool PVRShellMainWindow::IsTouched()
  307. {
  308. return m_bTouch;
  309. }
  310. float* PVRShellMainWindow::GetTouchPosition()
  311. {
  312. return &m_fVec2PointerLocation[0];
  313. }
  314. /*****************************************************************************
  315. Global code
  316. *****************************************************************************/
  317. PVRShellTimer::PVRShellTimer(QObject *parent) : QTimer(parent),
  318. m_pInit(0)
  319. {
  320. connect(this, SIGNAL(timeout()), this, SLOT(Run()));
  321. }
  322. PVRShellTimer::~PVRShellTimer()
  323. {
  324. DeInit();
  325. }
  326. void PVRShellTimer::Run()
  327. {
  328. // Initialise/run/shutdown
  329. if(!m_pInit->Run())
  330. {
  331. stop();
  332. DeInit();
  333. QApplication::exit(0); // We wish to exit our app
  334. }
  335. }
  336. bool PVRShellTimer::Init(int argc, char **argv)
  337. {
  338. m_pInit = new PVRShellInit();
  339. if(!m_pInit)
  340. return false;
  341. // Initialise our demo
  342. if(!m_pInit->Init())
  343. {
  344. fprintf(stderr, "Failed to initialise the demo\n");
  345. delete m_pInit;
  346. return false;
  347. }
  348. // Set the apps command-line
  349. m_pInit->CommandLine((argc-1),&argv[1]);
  350. m_pInit->SetAppName(argv[0]);
  351. m_pInit->SetReadPath(argv[0]); // TODO: Find the real read path
  352. m_pInit->SetWritePath(argv[0]); // TODO: Find the real write path
  353. return true;
  354. }
  355. void PVRShellTimer::DeInit()
  356. {
  357. // Tidy up our demo
  358. delete m_pInit;
  359. m_pInit = 0;
  360. }
  361. /*!***************************************************************************
  362. @function main
  363. @input argc count of args from OS
  364. @input argv array of args from OS
  365. @returns result code to OS
  366. @description Second Main function of the program, called by main to allow for
  367. efficient heap memory debugging.
  368. *****************************************************************************/
  369. int main(int argc, char **argv)
  370. {
  371. QApplication a( argc, argv );
  372. PVRShellTimer timer;
  373. timer.Init(argc, argv);
  374. timer.start(0);
  375. // call exec()
  376. return a.exec();
  377. }
  378. /*****************************************************************************
  379. End of file (PVRShellOS.cpp)
  380. *****************************************************************************/