fractal.cpp 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  1. #include <cstdlib>
  2. #include <iostream>
  3. #include "TestApp.h"
  4. #include "TestScene.h"
  5. using namespace Crown;
  6. Image* gImage = NULL;
  7. TexturePtr gTexture;
  8. Sprite* gSprite;
  9. float aspectRatio;
  10. float minX;
  11. float maxX;
  12. float minY;
  13. float maxY;
  14. float sizeX;
  15. float sizeY;
  16. float panX;
  17. float panY;
  18. float centerX;
  19. float centerY;
  20. class AllWindowsContext: public WindowContext, public WeakReferenced
  21. {
  22. public:
  23. AllWindowsContext(WindowsManager* windowsManager):
  24. WindowContext(windowsManager)
  25. {
  26. sizeX = 2.0f;
  27. centerX = 0.0f;
  28. centerY = 0.0f;
  29. CalculateSizeParams();
  30. RegisterAction("Generate", CreateDelegate(this, &AllWindowsContext::Generate));
  31. RegisterAction("MoveLeft", CreateDelegate(this, &AllWindowsContext::MoveLeft));
  32. RegisterAction("MoveRight", CreateDelegate(this, &AllWindowsContext::MoveRight));
  33. RegisterAction("MoveUp", CreateDelegate(this, &AllWindowsContext::MoveUp));
  34. RegisterAction("MoveDown", CreateDelegate(this, &AllWindowsContext::MoveDown));
  35. RegisterAction("ZoomPlus", CreateDelegate(this, &AllWindowsContext::ZoomPlus));
  36. RegisterAction("ZoomMinus", CreateDelegate(this, &AllWindowsContext::ZoomMinus));
  37. }
  38. virtual ~AllWindowsContext()
  39. {
  40. }
  41. void OnLoad()
  42. {
  43. GetAssociatedWindow()->SetBindingContext(this);
  44. }
  45. void MoveLeft(Widget* /*src*/, List<Str>* /*args*/)
  46. {
  47. centerX -= panX;
  48. CalculateSizeParams();
  49. Regenerate();
  50. }
  51. void MoveRight(Widget* /*src*/, List<Str>* /*args*/)
  52. {
  53. centerX += panX;
  54. CalculateSizeParams();
  55. Regenerate();
  56. }
  57. void MoveUp(Widget* /*src*/, List<Str>* /*args*/)
  58. {
  59. centerY += panY;
  60. CalculateSizeParams();
  61. Regenerate();
  62. }
  63. void MoveDown(Widget* /*src*/, List<Str>* /*args*/)
  64. {
  65. centerY -= panY;
  66. CalculateSizeParams();
  67. Regenerate();
  68. }
  69. void ZoomPlus(Widget* /*src*/, List<Str>* /*args*/)
  70. {
  71. sizeX /= 1.3f;
  72. CalculateSizeParams();
  73. Regenerate();
  74. }
  75. void ZoomMinus(Widget* /*src*/, List<Str>* /*args*/)
  76. {
  77. sizeX *= 1.3f;
  78. CalculateSizeParams();
  79. Regenerate();
  80. }
  81. void Regenerate()
  82. {
  83. Generate(NULL, NULL);
  84. }
  85. void Generate(Widget* /*src*/, List<Str>* /*args*/)
  86. {
  87. /*Color c;
  88. c.r = (rand() % 1000) / 1000.0f;
  89. c.g = (rand() % 1000) / 1000.0f;
  90. c.b = (rand() % 1000) / 1000.0f;
  91. gImage->SetUniformColorImage(c);*/
  92. float cX = 0.0f;
  93. float cY = 0.0f;
  94. int maxIterations = 150;
  95. for(uint py = 0; py < gImage->GetHeight(); py++)
  96. {
  97. cY = minY + py * sizeY / gImage->GetHeight();
  98. for(uint px = 0; px < gImage->GetWidth(); px++)
  99. {
  100. cX = minX + px * sizeX / gImage->GetWidth();
  101. //Iterate some times
  102. float x = 0.0f;
  103. float y = 0.0f;
  104. int i = 0;
  105. while (i < maxIterations && (x * x + y * y < 2))
  106. {
  107. float xTmp = x * x - y * y + cX;
  108. y = 2*x*y + cY;
  109. x = xTmp;
  110. i++;
  111. }
  112. if (i < maxIterations)
  113. {
  114. int comp = (int)(i * 255.0f / maxIterations);
  115. gImage->SetPixel(px, py, Color(comp, comp, 255 - comp));
  116. }
  117. else
  118. {
  119. gImage->SetPixel(px, py, Color::BLACK);
  120. }
  121. }
  122. }
  123. gTexture->LoadFromImage(gImage);
  124. }
  125. void CalculateSizeParams()
  126. {
  127. sizeY = sizeX * aspectRatio;
  128. minX = -sizeX / 2.0f + centerX;
  129. maxX = +sizeX / 2.0f + centerY;
  130. minY = -sizeY / 2.0f + centerY;
  131. maxY = +sizeY / 2.0f + centerY;
  132. panX = sizeX / 10.0f;
  133. panY = sizeY / 10.0f;
  134. }
  135. private:
  136. };
  137. class MainScene: public TestScene, public WeakReferenced
  138. {
  139. public:
  140. MainScene(Crown::uint windowWidth, Crown::uint windowHeight) :
  141. TestScene(windowWidth, windowHeight)
  142. {
  143. aspectRatio = (float)windowHeight / windowWidth;
  144. sizeX = 2.0f;
  145. sizeY = sizeX * aspectRatio;
  146. minX = -sizeX / 2.0f;
  147. maxX = +sizeX / 2.0f;
  148. minY = -sizeY / 2.0f;
  149. maxY = +sizeY / 2.0f;
  150. panX = sizeX / 10.0f;
  151. panY = sizeY / 10.0f;
  152. gImage = new Image(IT_2D, PF_RGB_8, windowWidth, windowHeight, NULL);
  153. bool created;
  154. gTexture = GetDevice()->GetRenderer()->GetTextureManager()->Create("fractal_tex", created);
  155. gTexture->LoadFromImage(gImage);
  156. gSprite = new Sprite();
  157. Frame* f = new Frame();
  158. f->Set(gTexture);
  159. gSprite->AddFrame(f);
  160. }
  161. virtual ~MainScene()
  162. {
  163. }
  164. void LoadXWMLAndLogResponse(Str xwmlFile)
  165. {
  166. XWMLReader xwml;
  167. Window* window;
  168. Filesystem* fs = GetDevice()->GetFilesystem();
  169. FilesystemEntry info;
  170. if (!fs->GetInfo(xwmlFile, info))
  171. {
  172. MessageWindow* mw = new MessageWindow(mWindowsManager, "Load XWML", "Could not find file '" + xwmlFile + "'", MWR_OK, NULL);
  173. mWindowsManager->DoModalWindow(mw);
  174. return;
  175. }
  176. window = xwml.LoadFile(xwmlFile, mWindowsManager, new AllWindowsContext(mWindowsManager));
  177. if (window == NULL)
  178. Log::E("Could not load XWML file '" + info.GetAbsolutePath() + "'");
  179. else
  180. Log::I("Successfully loaded XWML file '" + info.GetAbsolutePath() + "'");
  181. }
  182. virtual void OnLoad()
  183. {
  184. TestScene::OnLoad();
  185. Renderer* renderer = GetDevice()->GetRenderer();
  186. renderer->SetClearColor(Color(0.6f, 0.6f, 0.6f, 1.0f));
  187. mWindowsManager = new WindowsManager(this);
  188. LoadXWMLAndLogResponse("res/window_fractal.xml");
  189. }
  190. virtual void RenderScene()
  191. {
  192. gSprite->draw(0);
  193. TestScene::RenderScene();
  194. }
  195. private:
  196. WindowsManager* mWindowsManager;
  197. };
  198. CROWN_APP_AND_MAIN(TestApp, MainScene, 1024, 768, "Crown Engine v0.1 - XWMLReader Test");