diff --git a/C++/Doom Fire Algorithm/main.cpp b/C++/Doom Fire Algorithm/main.cpp index e78bf51..8d968a5 100644 --- a/C++/Doom Fire Algorithm/main.cpp +++ b/C++/Doom Fire Algorithm/main.cpp @@ -3,24 +3,23 @@ #define OLC_PGE_APPLICATION #include "olcPixelGameEngine.h" -int WINDOW_HEIGHT = 720; -int WINDOW_WIDTH = 1280; +int WINDOW_HEIGHT = 720 / 4; +int WINDOW_WIDTH = 1280 / 4; -int fireSize = 4; -int* firePixelsArray = new int[(WINDOW_WIDTH / fireSize) * (WINDOW_HEIGHT / fireSize)]; -int numberOfPixels = (WINDOW_WIDTH / fireSize) * (WINDOW_HEIGHT / fireSize); +int* firePixelsArray = new int[(WINDOW_WIDTH) * (WINDOW_HEIGHT)]; +int numberOfPixels = (WINDOW_WIDTH) * (WINDOW_HEIGHT); int fireColoursPalette[37][3] = {{7, 7, 7}, {31, 7, 7}, {47, 15, 7}, {71, 15, 7}, {87, 23, 7}, {103, 31, 7}, {119, 31, 7}, {143, 39, 7}, {159, 47, 7}, {175, 63, 7}, {191, 71, 7}, {199, 71, 7}, {223, 79, 7}, {223, 87, 7}, {223, 87, 7}, {215, 95, 7}, {215, 95, 7}, {215, 103, 15}, {207, 111, 15}, {207, 119, 15}, {207, 127, 15}, {207, 135, 23}, {199, 135, 23}, {199, 143, 23}, {199, 151, 31}, {191, 159, 31}, {191, 159, 31}, {191, 167, 39}, {191, 167, 39}, {191, 175, 47}, {183, 175, 47}, {183, 183, 47}, {183, 183, 55}, {207, 207, 111}, {223, 223, 159}, {239, 239, 199}, {255, 255, 255}}; class FireSim : public olc::PixelGameEngine { public: FireSim() { - sAppName = "Doom Fire Simulator"; + sAppName = "Doom Fire Simulator"; } bool OnUserCreate() override { for (int i = 0; i < numberOfPixels; i++) { - firePixelsArray[i] = 20; + firePixelsArray[i] = 36; } return true; }; @@ -38,18 +37,36 @@ public: if (GetMouse(0).bHeld) { Vec2 m = {GetMouseX(), GetMouseY()}; - int index = (m.y * WINDOW_WIDTH + m.x) / fireSize; - if (index >= numberOfPixels) { - return true; - } - firePixelsArray[index] = 36; + auto fillCircle = [&](int x, int y, int radius, int val) { + // Taken from wikipedia + int x0 = 0; + int y0 = radius; + int d = 3 - 2 * radius; + if (!radius) return; + + auto drawline = [&](int sx, int ex, int ny) { + for (int i = sx; i <= ex; i++) + firePixelsArray[ny * WINDOW_WIDTH + i] = val; + }; + + while (y0 >= x0) { + // Modified to draw scan-lines instead of edges + drawline(x - x0, x + x0, y - y0); + drawline(x - y0, x + y0, y - x0); + drawline(x - x0, x + x0, y + y0); + drawline(x - y0, x + y0, y + x0); + if (d < 0) d += 4 * x0++ + 6; + else d += 4 * (x0++ - y0--) + 10; + } + }; + fillCircle(m.x, m.y, 4, 36); } return true; } void UpdateFireIntensity(int pixel) { - int pixelBelowIndex = pixel + (WINDOW_WIDTH / fireSize); + int pixelBelowIndex = pixel + WINDOW_WIDTH; if (pixelBelowIndex < numberOfPixels) { int decay = floor(rand() % 3); int pixelBelowIntensity = firePixelsArray[pixelBelowIndex]; @@ -61,16 +78,15 @@ public: void Render() { Clear(olc::BLACK); - for (int x = 0; x < WINDOW_WIDTH; x += fireSize) { - for (int y = 0; y < WINDOW_HEIGHT; y += fireSize) { - int pixel = x + ((WINDOW_WIDTH / fireSize) * y); - int pixelI = pixel / fireSize; - int fireIntensity = firePixelsArray[pixelI]; + for (int x = 0; x < WINDOW_WIDTH; x++) { + for (int y = 0; y < WINDOW_HEIGHT; y++) { + int pixel = x + (WINDOW_WIDTH * y); + int fireIntensity = firePixelsArray[pixel]; uint8_t r = fireColoursPalette[fireIntensity][0]; uint8_t g = fireColoursPalette[fireIntensity][1]; uint8_t b = fireColoursPalette[fireIntensity][2]; olc::Pixel col = {r, g, b, 255}; - FillRect(x, y, fireSize, fireSize, col); + FillRect(x, y, 1, 1, col); } } } @@ -85,7 +101,7 @@ private: int main(int argc, char** argv) { FireSim app; - if (app.Construct(WINDOW_WIDTH, WINDOW_HEIGHT, 1, 1)) + if (app.Construct(WINDOW_WIDTH, WINDOW_HEIGHT, 4, 4)) app.Start(); return 0; } diff --git a/C++/Doom Fire Algorithm/output.exe b/C++/Doom Fire Algorithm/output.exe index e0e166c..8585616 100644 Binary files a/C++/Doom Fire Algorithm/output.exe and b/C++/Doom Fire Algorithm/output.exe differ