PageRenderTime 47ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

/mythplugins/mythmusic/mythmusic/goom/mythgoom.cpp

https://github.com/freedenizen/mythtv
C++ | 202 lines | 158 code | 43 blank | 1 comment | 26 complexity | b035b3be0bcc3eaf00270ded6acb2afb MD5 | raw file
Possible License(s): GPL-2.0, LGPL-2.0, LGPL-3.0
  1. #include "mythgoom.h"
  2. #ifdef SDL_SUPPORT
  3. #include <cmath>
  4. #include <cstdlib>
  5. #include <iostream>
  6. using namespace std;
  7. #include <QPainter>
  8. #include <compat.h>
  9. #include <mythcontext.h>
  10. extern "C" {
  11. #include "goom_tools.h"
  12. #include "goom_core.h"
  13. }
  14. Goom::Goom(long int winid)
  15. {
  16. fps = 20;
  17. surface = NULL;
  18. buffer = NULL;
  19. char SDL_windowhack[32];
  20. //char SDL_windowhack[sizeof(long int)];
  21. sprintf(SDL_windowhack, "%ld", winid);
  22. setenv("SDL_WINDOWID", SDL_windowhack, 1);
  23. if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_NOPARACHUTE) < 0)
  24. {
  25. VERBOSE(VB_IMPORTANT, "Unable to init SDL");
  26. return;
  27. }
  28. SDL_ShowCursor(0);
  29. goom_init(800, 600, 0);
  30. scalew = gCoreContext->GetNumSetting("VisualScaleWidth", 2);
  31. scaleh = gCoreContext->GetNumSetting("VisualScaleHeight", 2);
  32. if (scaleh > 2)
  33. scaleh = 2;
  34. if (scaleh < 1)
  35. scaleh = 1;
  36. if (scalew > 2)
  37. scalew = 2;
  38. if (scalew < 1)
  39. scalew = 1;
  40. }
  41. Goom::~Goom()
  42. {
  43. goom_close();
  44. SDL_Quit();
  45. unsetenv("SDL_WINDOWID");
  46. }
  47. void Goom::resize(const QSize &newsize)
  48. {
  49. size = newsize;
  50. size.setHeight((size.height() / 2) * 2);
  51. size.setWidth((size.width() / 2) * 2);
  52. surface = SDL_SetVideoMode(size.width(), size.height(), 32, 0 /* SDL_ANYFORMAT */);
  53. goom_set_resolution(size.width() / scalew, size.height() / scaleh, 0);
  54. }
  55. bool Goom::process(VisualNode *node)
  56. {
  57. if (!node || node->length == 0 || !surface)
  58. return true;
  59. int numSamps = 512;
  60. if (node->length < 512)
  61. numSamps = node->length;
  62. signed short int data[2][512];
  63. int i = 0;
  64. for (i = 0; i < numSamps; i++)
  65. {
  66. data[0][i] = node->left[i];
  67. if (node->right)
  68. data[1][i] = node->right[i];
  69. else
  70. data[1][i] = data[0][i];
  71. }
  72. for (; i < 512; i++)
  73. {
  74. data[0][i] = 0;
  75. data[1][i] = 0;
  76. }
  77. buffer = goom_update(data, 0);
  78. return false;
  79. }
  80. bool Goom::draw(QPainter *p, const QColor &back)
  81. {
  82. (void)p;
  83. (void)back;
  84. if (!surface)
  85. {
  86. VERBOSE(VB_IMPORTANT, "No sdl surface");
  87. return false;
  88. }
  89. if (!buffer)
  90. return false;
  91. if (scalew != 1 || scaleh != 1)
  92. {
  93. SDL_LockSurface(surface);
  94. register int *d = (int*)surface->pixels;
  95. register int *s = (int*)buffer;
  96. int sw = (size.width() / scalew) << 2;
  97. int sw2 = surface->pitch;
  98. int swd = sw2 - sw * scalew;
  99. long fin = (long)s;
  100. long fd = (long)d + (sw2 * size.height());
  101. while ((long)d < fd) {
  102. fin += sw;
  103. if (scalew == 2)
  104. {
  105. while ((long)s < fin) {
  106. register long col = *(s++);
  107. *(d++) = col; *(d++) = col;
  108. }
  109. }
  110. else
  111. {
  112. while ((long)s < fin) {
  113. register long col = *(s++);
  114. *(d++) = col;
  115. }
  116. }
  117. d = (int*)((char*)d + swd);
  118. if (scaleh == 2)
  119. {
  120. memcpy(d, ((char*)d) - sw2, sw2);
  121. d = (int*)((char*)d + sw2);
  122. }
  123. }
  124. }
  125. else
  126. {
  127. SDL_Surface *tmpsurf = SDL_CreateRGBSurfaceFrom(buffer, size.width(),
  128. size.height(), 32,
  129. size.width() * 4,
  130. 0x00ff0000, 0x0000ff00,
  131. 0x000000ff, 0x00000000);
  132. SDL_BlitSurface(tmpsurf, NULL, surface, NULL);
  133. SDL_FreeSurface(tmpsurf);
  134. }
  135. SDL_UnlockSurface(surface);
  136. SDL_Flip(surface);
  137. return false;
  138. }
  139. static class GoomFactory : public VisFactory
  140. {
  141. public:
  142. const QString &name(void) const
  143. {
  144. static QString name("Goom");
  145. return name;
  146. }
  147. uint plugins(QStringList *list) const
  148. {
  149. *list << name();
  150. return 1;
  151. }
  152. VisualBase *create(MainVisual *parent, long int winid, const QString &pluginName) const
  153. {
  154. (void)parent;
  155. (void)pluginName;
  156. return new Goom(winid);
  157. }
  158. }GoomFactory;
  159. #endif