Fire.cpp 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  1. /*
  2. * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright notice, this
  9. * list of conditions and the following disclaimer.
  10. *
  11. * 2. Redistributions in binary form must reproduce the above copyright notice,
  12. * this list of conditions and the following disclaimer in the documentation
  13. * and/or other materials provided with the distribution.
  14. *
  15. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  16. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  17. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  18. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  19. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  20. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  21. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  22. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  23. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  24. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  25. */
  26. /* Fire.cpp - a (classic) graphics demo for Serenity, by pd.
  27. * heavily based on the Fabien Sanglard's article:
  28. * http://fabiensanglard.net/doom_fire_psx/index.html
  29. *
  30. * Future directions:
  31. * [X] This does suggest the need for a palletized graphics surface. Thanks kling!
  32. * [X] alternate column updates, or vertical interlacing. this would certainly alter
  33. * the effect, but the update load would be halved.
  34. * [/] scaled blit
  35. * [ ] dithering?
  36. * [X] inlining rand()
  37. * [/] precalculating and recycling random data
  38. * [ ] rework/expand palette
  39. * [ ] switch to use tsc values for perf check
  40. * [ ] handle mouse events differently for smoother painting (queue)
  41. * [ ] handle fire bitmap edges better
  42. */
  43. #include <LibDraw/GraphicsBitmap.h>
  44. #include <LibDraw/PNGLoader.h>
  45. #include <LibGUI/GApplication.h>
  46. #include <LibGUI/GLabel.h>
  47. #include <LibGUI/GPainter.h>
  48. #include <LibGUI/GWidget.h>
  49. #include <LibGUI/GWindow.h>
  50. #include <stdio.h>
  51. #include <stdlib.h>
  52. #include <time.h>
  53. #define FIRE_WIDTH 320
  54. #define FIRE_HEIGHT 168
  55. #define FIRE_MAX 29
  56. static const Color s_palette[] = {
  57. Color(0x07, 0x07, 0x07), Color(0x1F, 0x07, 0x07), Color(0x2F, 0x0F, 0x07),
  58. Color(0x47, 0x0F, 0x07), Color(0x57, 0x17, 0x07), Color(0x67, 0x1F, 0x07),
  59. Color(0x77, 0x1F, 0x07), Color(0x9F, 0x2F, 0x07), Color(0xAF, 0x3F, 0x07),
  60. Color(0xBF, 0x47, 0x07), Color(0xC7, 0x47, 0x07), Color(0xDF, 0x4F, 0x07),
  61. Color(0xDF, 0x57, 0x07), Color(0xD7, 0x5F, 0x07), Color(0xD7, 0x5F, 0x07),
  62. Color(0xD7, 0x67, 0x0F), Color(0xCF, 0x6F, 0x0F), Color(0xCF, 0x7F, 0x0F),
  63. Color(0xCF, 0x87, 0x17), Color(0xC7, 0x87, 0x17), Color(0xC7, 0x8F, 0x17),
  64. Color(0xC7, 0x97, 0x1F), Color(0xBF, 0x9F, 0x1F), Color(0xBF, 0xA7, 0x27),
  65. Color(0xBF, 0xAF, 0x2F), Color(0xB7, 0xAF, 0x2F), Color(0xB7, 0xB7, 0x37),
  66. Color(0xCF, 0xCF, 0x6F), Color(0xEF, 0xEF, 0xC7), Color(0xFF, 0xFF, 0xFF)
  67. };
  68. /* Random functions...
  69. * These are from musl libc's prng/rand.c
  70. */
  71. static uint64_t seed;
  72. void my_srand(unsigned s)
  73. {
  74. seed = s - 1;
  75. }
  76. static int my_rand(void)
  77. {
  78. seed = 6364136223846793005ULL * seed + 1;
  79. return seed >> 33;
  80. }
  81. /*
  82. * Fire Widget
  83. */
  84. class Fire : public GWidget {
  85. C_OBJECT(Fire)
  86. public:
  87. virtual ~Fire() override;
  88. void set_stat_label(GLabel* l) { stats = l; };
  89. private:
  90. explicit Fire(GWidget* parent = nullptr);
  91. RefPtr<GraphicsBitmap> bitmap;
  92. GLabel* stats;
  93. virtual void paint_event(GPaintEvent&) override;
  94. virtual void timer_event(Core::TimerEvent&) override;
  95. virtual void mousedown_event(GMouseEvent& event) override;
  96. virtual void mousemove_event(GMouseEvent& event) override;
  97. virtual void mouseup_event(GMouseEvent& event) override;
  98. bool dragging;
  99. int timeAvg;
  100. int cycles;
  101. int phase;
  102. };
  103. Fire::Fire(GWidget* parent)
  104. : GWidget(parent)
  105. {
  106. bitmap = GraphicsBitmap::create(GraphicsBitmap::Format::Indexed8, { 320, 200 });
  107. /* Initialize fire palette */
  108. for (int i = 0; i < 30; i++)
  109. bitmap->set_palette_color(i, s_palette[i]);
  110. /* Set remaining entries to white */
  111. for (int i = 30; i < 256; i++)
  112. bitmap->set_palette_color(i, Color::White);
  113. dragging = false;
  114. timeAvg = 0;
  115. cycles = 0;
  116. phase = 0;
  117. my_srand(time(nullptr));
  118. stop_timer();
  119. start_timer(20);
  120. /* Draw fire "source" on bottom row of pixels */
  121. for (int i = 0; i < FIRE_WIDTH; i++)
  122. bitmap->bits(bitmap->height() - 1)[i] = FIRE_MAX;
  123. /* Set off initital paint event */
  124. //update();
  125. }
  126. Fire::~Fire()
  127. {
  128. }
  129. void Fire::paint_event(GPaintEvent& event)
  130. {
  131. Core::ElapsedTimer timer;
  132. timer.start();
  133. GPainter painter(*this);
  134. painter.add_clip_rect(event.rect());
  135. /* Blit it! */
  136. painter.draw_scaled_bitmap(event.rect(), *bitmap, bitmap->rect());
  137. timeAvg += timer.elapsed();
  138. cycles++;
  139. }
  140. void Fire::timer_event(Core::TimerEvent&)
  141. {
  142. /* Update only even or odd columns per frame... */
  143. phase++;
  144. if (phase > 1)
  145. phase = 0;
  146. /* Paint our palettized buffer to screen */
  147. for (int px = 0 + phase; px < FIRE_WIDTH; px += 2) {
  148. for (int py = 1; py < 200; py++) {
  149. int rnd = my_rand() % 3;
  150. /* Calculate new pixel value, don't go below 0 */
  151. u8 nv = bitmap->bits(py)[px];
  152. if (nv > 0)
  153. nv -= (rnd & 1);
  154. /* ...sigh... */
  155. int epx = px + (1 - rnd);
  156. if (epx < 0)
  157. epx = 0;
  158. else if (epx > FIRE_WIDTH)
  159. epx = FIRE_WIDTH;
  160. bitmap->bits(py - 1)[epx] = nv;
  161. }
  162. }
  163. if ((cycles % 50) == 0) {
  164. dbgprintf("%d total cycles. finished 50 in %d ms, avg %d ms\n", cycles, timeAvg, timeAvg / 50);
  165. stats->set_text(String::format("%d ms", timeAvg / 50));
  166. timeAvg = 0;
  167. }
  168. update();
  169. }
  170. /*
  171. * Mouse handling events
  172. */
  173. void Fire::mousedown_event(GMouseEvent& event)
  174. {
  175. if (event.button() == GMouseButton::Left)
  176. dragging = true;
  177. return GWidget::mousedown_event(event);
  178. }
  179. /* FIXME: needs to account for the size of the window rect */
  180. void Fire::mousemove_event(GMouseEvent& event)
  181. {
  182. if (dragging) {
  183. if (event.y() >= 2 && event.y() < 398 && event.x() <= 638) {
  184. int ypos = event.y() / 2;
  185. int xpos = event.x() / 2;
  186. bitmap->bits(ypos - 1)[xpos] = FIRE_MAX + 5;
  187. bitmap->bits(ypos - 1)[xpos + 1] = FIRE_MAX + 5;
  188. bitmap->bits(ypos)[xpos] = FIRE_MAX + 5;
  189. bitmap->bits(ypos)[xpos + 1] = FIRE_MAX + 5;
  190. }
  191. }
  192. return GWidget::mousemove_event(event);
  193. }
  194. void Fire::mouseup_event(GMouseEvent& event)
  195. {
  196. if (event.button() == GMouseButton::Left)
  197. dragging = false;
  198. return GWidget::mouseup_event(event);
  199. }
  200. /*
  201. * Main
  202. */
  203. int main(int argc, char** argv)
  204. {
  205. GApplication app(argc, argv);
  206. auto window = GWindow::construct();
  207. window->set_double_buffering_enabled(false);
  208. window->set_title("Fire");
  209. window->set_resizable(false);
  210. window->set_rect(100, 100, 640, 400);
  211. auto fire = Fire::construct();
  212. window->set_main_widget(fire);
  213. auto time = GLabel::construct(fire);
  214. time->set_relative_rect({ 0, 4, 40, 10 });
  215. time->move_by({ window->width() - time->width(), 0 });
  216. time->set_foreground_color(Color::from_rgb(0x444444));
  217. fire->set_stat_label(time);
  218. window->show();
  219. window->set_icon(load_png("/res/icons/16x16/app-demo.png"));
  220. return app.exec();
  221. }