PolySDLCore.cpp 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. #include "PolySDLCore.h"
  2. using namespace Polycode;
  3. SDLCore::SDLCore(PolycodeViewBase *view, int xRes, int yRes, bool fullScreen,int aaLevel, int frameRate) : Core(xRes, yRes, fullScreen,aaLevel, frameRate) {
  4. String *windowTitle = (String*)view->windowData;
  5. putenv("SDL_VIDEO_CENTERED=1");
  6. if(SDL_Init(SDL_INIT_VIDEO) < 0) {
  7. }
  8. renderer = new OpenGLRenderer();
  9. services->setRenderer(renderer);
  10. setVideoMode(xRes, yRes, fullScreen, aaLevel);
  11. SDL_WM_SetCaption(windowTitle->c_str(), windowTitle->c_str());
  12. SDL_EnableUNICODE(1);
  13. SDL_EnableKeyRepeat(SDL_DEFAULT_REPEAT_DELAY, SDL_DEFAULT_REPEAT_INTERVAL);
  14. }
  15. void SDLCore::setVideoMode(int xRes, int yRes, bool fullScreen, int aaLevel) {
  16. SDL_GL_SetAttribute( SDL_GL_DEPTH_SIZE, 24);
  17. SDL_GL_SetAttribute( SDL_GL_DOUBLEBUFFER, 1);
  18. SDL_GL_SetAttribute( SDL_GL_RED_SIZE, 8);
  19. SDL_GL_SetAttribute( SDL_GL_GREEN_SIZE, 8);
  20. SDL_GL_SetAttribute( SDL_GL_BLUE_SIZE, 8);
  21. SDL_GL_SetAttribute( SDL_GL_ALPHA_SIZE, 8);
  22. if(aaLevel > 0) {
  23. SDL_GL_SetAttribute( SDL_GL_MULTISAMPLEBUFFERS, 1);
  24. SDL_GL_SetAttribute( SDL_GL_MULTISAMPLESAMPLES, aaLevel); //0, 2, 4
  25. } else {
  26. SDL_GL_SetAttribute( SDL_GL_MULTISAMPLEBUFFERS, 0);
  27. SDL_GL_SetAttribute( SDL_GL_MULTISAMPLESAMPLES, 0);
  28. }
  29. SDL_GL_SetAttribute(SDL_GL_SWAP_CONTROL, 0);
  30. if(fullScreen) {
  31. if( SDL_SetVideoMode(xRes, yRes, 0, SDL_OPENGL|SDL_FULLSCREEN) == NULL ) {
  32. }
  33. } else {
  34. if( SDL_SetVideoMode(xRes, yRes, 0, SDL_OPENGL) == NULL ) {
  35. }
  36. }
  37. renderer->Resize(xRes, yRes);
  38. CoreServices::getInstance()->getMaterialManager()->reloadProgramsAndTextures();
  39. }
  40. vector<Polycode::Rectangle> SDLCore::getVideoModes() {
  41. vector<Polycode::Rectangle> retVector;
  42. SDL_Rect **modes;
  43. modes=SDL_ListModes(NULL, SDL_FULLSCREEN);
  44. for(int i=0;modes[i];++i) {
  45. Rectangle res;
  46. res.w = modes[i]->w;
  47. res.h = modes[i]->h;
  48. retVector.push_back(res);
  49. }
  50. return retVector;
  51. }
  52. SDLCore::~SDLCore() {
  53. SDL_Quit();
  54. }
  55. int SDLThreadFunc(void *data) {
  56. Threaded *target = (Threaded*)data;
  57. target->runThread();
  58. return 1;
  59. }
  60. void SDLCore::createThread(Threaded *target) {
  61. SDL_CreateThread(SDLThreadFunc, (void*)target);
  62. }
  63. unsigned int SDLCore::getTicks() {
  64. return SDL_GetTicks();
  65. }
  66. void SDLCore::enableMouse(bool newval) {
  67. if(newval) {
  68. SDL_ShowCursor(1);
  69. SDL_WM_GrabInput(SDL_GRAB_OFF);
  70. } else {
  71. SDL_ShowCursor(0);
  72. SDL_WM_GrabInput(SDL_GRAB_ON);
  73. }
  74. Core::enableMouse(newval);
  75. }
  76. bool SDLCore::Update() {
  77. if(!running)
  78. return false;
  79. renderer->BeginRender();
  80. updateCore();
  81. renderer->EndRender();
  82. SDL_GL_SwapBuffers();
  83. SDL_Event event;
  84. while ( SDL_PollEvent(&event) ) {
  85. switch (event.type) {
  86. case SDL_QUIT:
  87. running = false;
  88. break;
  89. case SDL_JOYBUTTONDOWN:
  90. // input->setKeyState((PolyKEY)(event.key.keysym.sym), true);
  91. break;
  92. case SDL_KEYDOWN:
  93. input->setKeyState((PolyKEY)(event.key.keysym.sym), (char)event.key.keysym.unicode, true, getTicks());
  94. break;
  95. case SDL_KEYUP:
  96. input->setKeyState((PolyKEY)(event.key.keysym.sym), (char)event.key.keysym.unicode, false, getTicks());
  97. break;
  98. case SDL_MOUSEBUTTONDOWN:
  99. if(event.button.button == SDL_BUTTON_WHEELUP) {
  100. input->mouseWheelUp(getTicks());
  101. } else if(event.button.button == SDL_BUTTON_WHEELDOWN) {
  102. input->mouseWheelDown(getTicks());
  103. } else {
  104. input->setMouseButtonState(CoreInput::MOUSE_BUTTON1, true, getTicks());
  105. }
  106. break;
  107. case SDL_MOUSEBUTTONUP:
  108. if(event.button.button == SDL_BUTTON_WHEELUP || event.button.button == SDL_BUTTON_WHEELDOWN) {
  109. } else {
  110. input->setMouseButtonState(CoreInput::MOUSE_BUTTON1, false, getTicks());
  111. }
  112. break;
  113. case SDL_MOUSEMOTION:
  114. input->setDeltaPosition(event.motion.xrel, event.motion.yrel);
  115. input->setMousePosition(event.motion.x, event.motion.y, getTicks());
  116. break;
  117. default:
  118. break;
  119. }
  120. }
  121. doSleep();
  122. return running;
  123. }
  124. void SDLCore::setCursor(int cursorType) {
  125. }
  126. void SDLCore::lockMutex(CoreMutex *mutex) {
  127. SDLCoreMutex *smutex = (SDLCoreMutex*)mutex;
  128. SDL_mutexP(smutex->pMutex);
  129. }
  130. void SDLCore::unlockMutex(CoreMutex *mutex) {
  131. SDLCoreMutex *smutex = (SDLCoreMutex*)mutex;
  132. SDL_mutexV(smutex->pMutex);
  133. }
  134. CoreMutex *SDLCore::createMutex() {
  135. SDLCoreMutex *mutex = new SDLCoreMutex();
  136. mutex->pMutex = SDL_CreateMutex();
  137. return mutex;
  138. }
  139. void SDLCore::copyStringToClipboard(String str) {
  140. }
  141. String SDLCore::getClipboardString() {
  142. }
  143. void SDLCore::createFolder(String folderPath) {
  144. }
  145. void SDLCore::copyDiskItem(String itemPath, String destItemPath) {
  146. }
  147. void SDLCore::moveDiskItem(String itemPath, String destItemPath) {
  148. }
  149. void SDLCore::removeDiskItem(String itemPath) {
  150. }
  151. String SDLCore::openFolderPicker() {
  152. }
  153. vector<string> SDLCore::openFilePicker(vector<CoreFileExtension> extensions, bool allowMultiple) {
  154. }
  155. void SDLCore::resizeTo(int xRes, int yRes) {
  156. renderer->Resize(xRes, yRes);
  157. }