here2share

# cmd_game_engine.cpp

Feb 4th, 2021 (edited)
287
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 39.38 KB | None | 0 0
  1. /*
  2. # cmd_game_engine.cpp
  3.  
  4. OneLoneCoder.com - Command Line Game Engine
  5. "Who needs a frame buffer?" - @Javidx9
  6.  
  7. The Original & Best :P
  8.  
  9. One Lone Coder License
  10. ~~~~~~~~~~~~~~~~~~~~~~
  11. - This software is Copyright (C) 2018 Javidx9
  12. - This is free software
  13. - This software comes with absolutely no warranty
  14. - The copyright holder is not liable or responsible for anything
  15.   this software does or does not
  16. - You use this software at your own risk
  17. - You can distribute this software
  18. - You can modify this software
  19. - Redistribution of this software or a derivative of this software
  20.   must attribute the Copyright holder named above, in a manner
  21.   visible to the end user
  22.  
  23. License
  24. ~~~~~~~
  25. One Lone Coder Console Game Engine  Copyright (C) 2018  Javidx9
  26. This program comes with ABSOLUTELY NO WARRANTY.
  27. This is free software, and you are welcome to redistribute it
  28. under certain conditions; See license for details.
  29.  
  30. Original works located at:
  31.     https://www.github.com/onelonecoder
  32.     https://www.onelonecoder.com
  33.     https://www.youtube.com/javidx9
  34.  
  35. GNU GPLv3
  36.     https://github.com/OneLoneCoder/videos/blob/master/LICENSE
  37.  
  38. From Javidx9 :)
  39. ~~~~~~~~~~~~~~~
  40. Hello! Ultimately I don't care what you use this for. It's intended to be
  41. educational, and perhaps to the oddly minded - a little bit of fun.
  42. Please hack this, change it and use it in any way you see fit. You acknowledge
  43. that I am not responsible for anything bad that happens as a result of
  44. your actions. However this code is protected by GNU GPLv3, see the license in the
  45. github repo. This means you must attribute me if you use it. You can view this
  46. license here: https://github.com/OneLoneCoder/videos/blob/master/LICENSE
  47.  
  48. Cheers!
  49.  
  50. Background
  51. ~~~~~~~~~~
  52. If you've seen any of my videos - I like to do things using the windows console. It's quick
  53. and easy, and allows you to focus on just the code that matters - ideal when you're
  54. experimenting. Thing is, I have to keep doing the same initialisation and display code
  55. each time, so this class wraps that up.
  56.  
  57. Author
  58. ~~~~~~
  59. Twitter: @javidx9   http://twitter.com/javidx9
  60. Blog:               http://www.onelonecoder.com
  61. YouTube:            http://www.youtube.com/javidx9
  62.  
  63. Videos:
  64. ~~~~~~
  65. Original:               https://youtu.be/cWc0hgYwZyc
  66. Added mouse support:    https://youtu.be/tdqc9hZhHxM
  67. Beginners Guide:        https://youtu.be/u5BhrA8ED0o
  68.  
  69. Shout Outs!
  70. ~~~~~~~~~~~
  71. Thanks to cool people who helped with testing, bug-finding and fixing!
  72. wowLinh, JavaJack59, idkwid, kingtatgi, Return Null, CPP Guy, MaGetzUb
  73.  
  74. Last Updated: 02/07/2018
  75.  
  76. Usage:
  77. ~~~~~~
  78. This class is abstract, so you must inherit from it. Override the OnUserCreate() function
  79. with all the stuff you need for your application (for thready reasons it's best to do
  80. this in this function and not your class constructor). Override the OnUserUpdate(float fElapsedTime)
  81. function with the good stuff, it gives you the elapsed time since the last call so you
  82. can modify your stuff dynamically. Both functions should return true, unless you need
  83. the application to close.
  84.  
  85.     int main()
  86.     {
  87.         // Use olcConsoleGameEngine derived app
  88.         OneLoneCoder_Example game;
  89.  
  90.         // Create a console with resolution 160x100 characters
  91.         // Each character occupies 8x8 pixels
  92.         game.ConstructConsole(160, 100, 8, 8);
  93.  
  94.         // Start the engine!
  95.         game.Start();
  96.  
  97.         return 0;
  98.     }
  99.  
  100. Input is also handled for you - interrogate the m_keys[] array with the virtual
  101. keycode you want to know about. bPressed is set for the frame the key is pressed down
  102. in, bHeld is set if the key is held down, bReleased is set for the frame the key
  103. is released in. The same applies to mouse! m_mousePosX and Y can be used to get
  104. the current cursor position, and m_mouse[1..5] returns the mouse buttons.
  105.  
  106. The draw routines treat characters like pixels. By default they are set to white solid
  107. blocks - but you can draw any unicode character, using any of the colours listed below.
  108.  
  109. There may be bugs!
  110.  
  111. See my other videos for examples!
  112. http://www.youtube.com/javidx9
  113.  
  114. Lots of programs to try:
  115. http://www.github.com/OneLoneCoder/videos
  116.  
  117. Chat on the Discord server:
  118. https://discord.gg/WhwHUMV
  119.  
  120. Be bored by Twitch:
  121. http://www.twitch.tv/javidx9
  122.  
  123. */
  124.  
  125. #pragma once
  126. #pragma comment(lib, "winmm.lib")
  127.  
  128. #ifndef UNICODE
  129. #error Please enable UNICODE for your compiler! VS: Project Properties -> General -> \
  130. Character Set -> Use Unicode. Thanks! - Javidx9
  131. #endif
  132.  
  133. #include <windows.h>
  134.  
  135. #include <iostream>
  136. #include <chrono>
  137. #include <vector>
  138. #include <list>
  139. #include <thread>
  140. #include <atomic>
  141. #include <condition_variable>
  142.  
  143. enum COLOUR
  144. {
  145.     FG_BLACK        = 0x0000,
  146.     FG_DARK_BLUE    = 0x0001,  
  147.     FG_DARK_GREEN   = 0x0002,
  148.     FG_DARK_CYAN    = 0x0003,
  149.     FG_DARK_RED     = 0x0004,
  150.     FG_DARK_MAGENTA = 0x0005,
  151.     FG_DARK_YELLOW  = 0x0006,
  152.     FG_GREY         = 0x0007, // Thanks MS :-/
  153.     FG_DARK_GREY    = 0x0008,
  154.     FG_BLUE         = 0x0009,
  155.     FG_GREEN        = 0x000A,
  156.     FG_CYAN         = 0x000B,
  157.     FG_RED          = 0x000C,
  158.     FG_MAGENTA      = 0x000D,
  159.     FG_YELLOW       = 0x000E,
  160.     FG_WHITE        = 0x000F,
  161.     BG_BLACK        = 0x0000,
  162.     BG_DARK_BLUE    = 0x0010,
  163.     BG_DARK_GREEN   = 0x0020,
  164.     BG_DARK_CYAN    = 0x0030,
  165.     BG_DARK_RED     = 0x0040,
  166.     BG_DARK_MAGENTA = 0x0050,
  167.     BG_DARK_YELLOW  = 0x0060,
  168.     BG_GREY         = 0x0070,
  169.     BG_DARK_GREY    = 0x0080,
  170.     BG_BLUE         = 0x0090,
  171.     BG_GREEN        = 0x00A0,
  172.     BG_CYAN         = 0x00B0,
  173.     BG_RED          = 0x00C0,
  174.     BG_MAGENTA      = 0x00D0,
  175.     BG_YELLOW       = 0x00E0,
  176.     BG_WHITE        = 0x00F0,
  177. };
  178.  
  179. enum PIXEL_TYPE
  180. {
  181.     PIXEL_SOLID = 0x2588,
  182.     PIXEL_THREEQUARTERS = 0x2593,
  183.     PIXEL_HALF = 0x2592,
  184.     PIXEL_QUARTER = 0x2591,
  185. };
  186.  
  187. class olcSprite
  188. {
  189. public:
  190.     olcSprite()
  191.     {
  192.  
  193.     }
  194.  
  195.     olcSprite(int w, int h)
  196.     {
  197.         Create(w, h);
  198.     }
  199.  
  200.     olcSprite(std::wstring sFile)
  201.     {
  202.         if (!Load(sFile))
  203.             Create(8, 8);
  204.     }
  205.  
  206.     int nWidth = 0;
  207.     int nHeight = 0;
  208.  
  209. private:
  210.     short *m_Glyphs = nullptr;
  211.     short *m_Colours = nullptr;
  212.  
  213.     void Create(int w, int h)
  214.     {
  215.         nWidth = w;
  216.         nHeight = h;
  217.         m_Glyphs = new short[w*h];
  218.         m_Colours = new short[w*h];
  219.         for (int i = 0; i < w*h; i++)
  220.         {
  221.             m_Glyphs[i] = L' ';
  222.             m_Colours[i] = FG_BLACK;
  223.         }
  224.     }
  225.  
  226. public:
  227.     void SetGlyph(int x, int y, short c)
  228.     {
  229.         if (x <0 || x >= nWidth || y < 0 || y >= nHeight)
  230.             return;
  231.         else
  232.             m_Glyphs[y * nWidth + x] = c;
  233.     }
  234.  
  235.     void SetColour(int x, int y, short c)
  236.     {
  237.         if (x <0 || x >= nWidth || y < 0 || y >= nHeight)
  238.             return;
  239.         else
  240.             m_Colours[y * nWidth + x] = c;
  241.     }
  242.  
  243.     short GetGlyph(int x, int y)
  244.     {
  245.         if (x <0 || x >= nWidth || y < 0 || y >= nHeight)
  246.             return L' ';
  247.         else
  248.             return m_Glyphs[y * nWidth + x];
  249.     }
  250.  
  251.     short GetColour(int x, int y)
  252.     {
  253.         if (x <0 || x >= nWidth || y < 0 || y >= nHeight)
  254.             return FG_BLACK;
  255.         else
  256.             return m_Colours[y * nWidth + x];
  257.     }
  258.  
  259.     short SampleGlyph(float x, float y)
  260.     {
  261.         int sx = (int)(x * (float)nWidth);
  262.         int sy = (int)(y * (float)nHeight-1.0f);
  263.         if (sx <0 || sx >= nWidth || sy < 0 || sy >= nHeight)
  264.             return L' ';
  265.         else
  266.             return m_Glyphs[sy * nWidth + sx];
  267.     }
  268.  
  269.     short SampleColour(float x, float y)
  270.     {
  271.         int sx = (int)(x * (float)nWidth);
  272.         int sy = (int)(y * (float)nHeight-1.0f);
  273.         if (sx <0 || sx >= nWidth || sy < 0 || sy >= nHeight)
  274.             return FG_BLACK;
  275.         else
  276.             return m_Colours[sy * nWidth + sx];
  277.     }
  278.  
  279.     bool Save(std::wstring sFile)
  280.     {
  281.         FILE *f = nullptr;
  282.         _wfopen_s(&f, sFile.c_str(), L"wb");
  283.         if (f == nullptr)
  284.             return false;
  285.  
  286.         fwrite(&nWidth, sizeof(int), 1, f);
  287.         fwrite(&nHeight, sizeof(int), 1, f);
  288.         fwrite(m_Colours, sizeof(short), nWidth * nHeight, f);
  289.         fwrite(m_Glyphs, sizeof(short), nWidth * nHeight, f);
  290.  
  291.         fclose(f);
  292.  
  293.         return true;
  294.     }
  295.  
  296.     bool Load(std::wstring sFile)
  297.     {
  298.         delete[] m_Glyphs;
  299.         delete[] m_Colours;
  300.         nWidth = 0;
  301.         nHeight = 0;
  302.  
  303.         FILE *f = nullptr;
  304.         _wfopen_s(&f, sFile.c_str(), L"rb");
  305.         if (f == nullptr)
  306.             return false;
  307.  
  308.         std::fread(&nWidth, sizeof(int), 1, f);
  309.         std::fread(&nHeight, sizeof(int), 1, f);
  310.  
  311.         Create(nWidth, nHeight);
  312.  
  313.         std::fread(m_Colours, sizeof(short), nWidth * nHeight, f);
  314.         std::fread(m_Glyphs, sizeof(short), nWidth * nHeight, f);
  315.  
  316.         std::fclose(f);
  317.         return true;
  318.     }
  319. };
  320.  
  321. class olcConsoleGameEngine
  322. {
  323. public:
  324.     olcConsoleGameEngine()
  325.     {
  326.         m_nScreenWidth = 80;
  327.         m_nScreenHeight = 30;
  328.  
  329.         m_hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
  330.         m_hConsoleIn = GetStdHandle(STD_INPUT_HANDLE);
  331.  
  332.         std::memset(m_keyNewState, 0, 256 * sizeof(short));
  333.         std::memset(m_keyOldState, 0, 256 * sizeof(short));
  334.         std::memset(m_keys, 0, 256 * sizeof(sKeyState));
  335.         m_mousePosX = 0;
  336.         m_mousePosY = 0;
  337.  
  338.         m_bEnableSound = false;
  339.  
  340.         m_sAppName = L"Default";
  341.     }
  342.  
  343.     void EnableSound()
  344.     {
  345.         m_bEnableSound = true;
  346.     }
  347.  
  348.     int ConstructConsole(int width, int height, int fontw, int fonth)
  349.     {
  350.         if (m_hConsole == INVALID_HANDLE_VALUE)
  351.             return Error(L"Bad Handle");
  352.  
  353.         m_nScreenWidth = width;
  354.         m_nScreenHeight = height;
  355.  
  356.         // Update 13/09/2017 - It seems that the console behaves differently on some systems
  357.         // and I'm unsure why this is. It could be to do with windows default settings, or
  358.         // screen resolutions, or system languages. Unfortunately, MSDN does not offer much
  359.         // by way of useful information, and so the resulting sequence is the reult of experiment
  360.         // that seems to work in multiple cases.
  361.         //
  362.         // The problem seems to be that the SetConsoleXXX functions are somewhat circular and
  363.         // fail depending on the state of the current console properties, i.e. you can't set
  364.         // the buffer size until you set the screen size, but you can't change the screen size
  365.         // until the buffer size is correct. This coupled with a precise ordering of calls
  366.         // makes this procedure seem a little mystical :-P. Thanks to wowLinh for helping - Jx9
  367.  
  368.         // Change console visual size to a minimum so ScreenBuffer can shrink
  369.         // below the actual visual size
  370.         m_rectWindow = { 0, 0, 1, 1 };
  371.         SetConsoleWindowInfo(m_hConsole, TRUE, &m_rectWindow);
  372.  
  373.         // Set the size of the screen buffer
  374.         COORD coord = { (short)m_nScreenWidth, (short)m_nScreenHeight };
  375.         if (!SetConsoleScreenBufferSize(m_hConsole, coord))
  376.             Error(L"SetConsoleScreenBufferSize");
  377.  
  378.         // Assign screen buffer to the console
  379.         if (!SetConsoleActiveScreenBuffer(m_hConsole))
  380.             return Error(L"SetConsoleActiveScreenBuffer");
  381.        
  382.         // Set the font size now that the screen buffer has been assigned to the console
  383.         CONSOLE_FONT_INFOEX cfi;
  384.         cfi.cbSize = sizeof(cfi);
  385.         cfi.nFont = 0;
  386.         cfi.dwFontSize.X = fontw;
  387.         cfi.dwFontSize.Y = fonth;
  388.         cfi.FontFamily = FF_DONTCARE;
  389.         cfi.FontWeight = FW_NORMAL;
  390.  
  391.     /*  DWORD version = GetVersion();
  392.         DWORD major = (DWORD)(LOBYTE(LOWORD(version)));
  393.         DWORD minor = (DWORD)(HIBYTE(LOWORD(version)));*/
  394.  
  395.         //if ((major > 6) || ((major == 6) && (minor >= 2) && (minor < 4)))    
  396.         //  wcscpy_s(cfi.FaceName, L"Raster"); // Windows 8 :(
  397.         //else
  398.         //  wcscpy_s(cfi.FaceName, L"Lucida Console"); // Everything else :P
  399.  
  400.         //wcscpy_s(cfi.FaceName, L"Liberation Mono");
  401.         wcscpy_s(cfi.FaceName, L"Consolas");
  402.         if (!SetCurrentConsoleFontEx(m_hConsole, false, &cfi))
  403.             return Error(L"SetCurrentConsoleFontEx");
  404.  
  405.         // Get screen buffer info and check the maximum allowed window size. Return
  406.         // error if exceeded, so user knows their dimensions/fontsize are too large
  407.         CONSOLE_SCREEN_BUFFER_INFO csbi;
  408.         if (!GetConsoleScreenBufferInfo(m_hConsole, &csbi))
  409.             return Error(L"GetConsoleScreenBufferInfo");
  410.         if (m_nScreenHeight > csbi.dwMaximumWindowSize.Y)
  411.             return Error(L"Screen Height / Font Height Too Big");
  412.         if (m_nScreenWidth > csbi.dwMaximumWindowSize.X)
  413.             return Error(L"Screen Width / Font Width Too Big");
  414.  
  415.         // Set Physical Console Window Size
  416.         m_rectWindow = { 0, 0, (short)m_nScreenWidth - 1, (short)m_nScreenHeight - 1 };
  417.         if (!SetConsoleWindowInfo(m_hConsole, TRUE, &m_rectWindow))
  418.             return Error(L"SetConsoleWindowInfo");
  419.  
  420.         // Set flags to allow mouse input      
  421.         if (!SetConsoleMode(m_hConsoleIn, ENABLE_EXTENDED_FLAGS | ENABLE_WINDOW_INPUT | ENABLE_MOUSE_INPUT))
  422.             return Error(L"SetConsoleMode");
  423.  
  424.         // Allocate memory for screen buffer
  425.         m_bufScreen = new CHAR_INFO[m_nScreenWidth*m_nScreenHeight];
  426.         memset(m_bufScreen, 0, sizeof(CHAR_INFO) * m_nScreenWidth * m_nScreenHeight);
  427.  
  428.         SetConsoleCtrlHandler((PHANDLER_ROUTINE)CloseHandler, TRUE);
  429.         return 1;
  430.     }
  431.  
  432.     virtual void Draw(int x, int y, short c = 0x2588, short col = 0x000F)
  433.     {
  434.         if (x >= 0 && x < m_nScreenWidth && y >= 0 && y < m_nScreenHeight)
  435.         {
  436.             m_bufScreen[y * m_nScreenWidth + x].Char.UnicodeChar = c;
  437.             m_bufScreen[y * m_nScreenWidth + x].Attributes = col;
  438.         }
  439.     }
  440.  
  441.     void Fill(int x1, int y1, int x2, int y2, short c = 0x2588, short col = 0x000F)
  442.     {
  443.         Clip(x1, y1);
  444.         Clip(x2, y2);
  445.         for (int x = x1; x < x2; x++)
  446.             for (int y = y1; y < y2; y++)
  447.                 Draw(x, y, c, col);
  448.     }
  449.  
  450.     void DrawString(int x, int y, std::wstring c, short col = 0x000F)
  451.     {
  452.         for (size_t i = 0; i < c.size(); i++)
  453.         {
  454.             m_bufScreen[y * m_nScreenWidth + x + i].Char.UnicodeChar = c[i];
  455.             m_bufScreen[y * m_nScreenWidth + x + i].Attributes = col;
  456.         }
  457.     }
  458.  
  459.     void DrawStringAlpha(int x, int y, std::wstring c, short col = 0x000F)
  460.     {
  461.         for (size_t i = 0; i < c.size(); i++)
  462.         {
  463.             if (c[i] != L' ')
  464.             {
  465.                 m_bufScreen[y * m_nScreenWidth + x + i].Char.UnicodeChar = c[i];
  466.                 m_bufScreen[y * m_nScreenWidth + x + i].Attributes = col;
  467.             }
  468.         }
  469.     }
  470.  
  471.     void Clip(int &x, int &y)
  472.     {
  473.         if (x < 0) x = 0;
  474.         if (x >= m_nScreenWidth) x = m_nScreenWidth;
  475.         if (y < 0) y = 0;
  476.         if (y >= m_nScreenHeight) y = m_nScreenHeight;
  477.     }
  478.  
  479.     void DrawLine(int x1, int y1, int x2, int y2, short c = 0x2588, short col = 0x000F)
  480.     {
  481.         int x, y, dx, dy, dx1, dy1, px, py, xe, ye, i;
  482.         dx = x2 - x1; dy = y2 - y1;
  483.         dx1 = abs(dx); dy1 = abs(dy);
  484.         px = 2 * dy1 - dx1; py = 2 * dx1 - dy1;
  485.         if (dy1 <= dx1)
  486.         {
  487.             if (dx >= 0)
  488.                 { x = x1; y = y1; xe = x2; }
  489.             else
  490.                 { x = x2; y = y2; xe = x1;}
  491.  
  492.             Draw(x, y, c, col);
  493.            
  494.             for (i = 0; x<xe; i++)
  495.             {
  496.                 x = x + 1;
  497.                 if (px<0)
  498.                     px = px + 2 * dy1;
  499.                 else
  500.                 {
  501.                     if ((dx<0 && dy<0) || (dx>0 && dy>0)) y = y + 1; else y = y - 1;
  502.                     px = px + 2 * (dy1 - dx1);
  503.                 }
  504.                 Draw(x, y, c, col);
  505.             }
  506.         }
  507.         else
  508.         {
  509.             if (dy >= 0)
  510.                 { x = x1; y = y1; ye = y2; }
  511.             else
  512.                 { x = x2; y = y2; ye = y1; }
  513.  
  514.             Draw(x, y, c, col);
  515.  
  516.             for (i = 0; y<ye; i++)
  517.             {
  518.                 y = y + 1;
  519.                 if (py <= 0)
  520.                     py = py + 2 * dx1;
  521.                 else
  522.                 {
  523.                     if ((dx<0 && dy<0) || (dx>0 && dy>0)) x = x + 1; else x = x - 1;
  524.                     py = py + 2 * (dx1 - dy1);
  525.                 }
  526.                 Draw(x, y, c, col);
  527.             }
  528.         }
  529.     }
  530.  
  531.     void DrawTriangle(int x1, int y1, int x2, int y2, int x3, int y3, short c = 0x2588, short col = 0x000F)
  532.     {
  533.         DrawLine(x1, y1, x2, y2, c, col);
  534.         DrawLine(x2, y2, x3, y3, c, col);
  535.         DrawLine(x3, y3, x1, y1, c, col);
  536.     }
  537.  
  538.     // https://www.avrfreaks.net/sites/default/files/triangles.c
  539.     void FillTriangle(int x1, int y1, int x2, int y2, int x3, int y3, short c = 0x2588, short col = 0x000F)
  540.     {
  541.         auto SWAP = [](int &x, int &y) { int t = x; x = y; y = t; };
  542.         auto drawline = [&](int sx, int ex, int ny) { for (int i = sx; i <= ex; i++) Draw(i, ny, c, col); };
  543.        
  544.         int t1x, t2x, y, minx, maxx, t1xp, t2xp;
  545.         bool changed1 = false;
  546.         bool changed2 = false;
  547.         int signx1, signx2, dx1, dy1, dx2, dy2;
  548.         int e1, e2;
  549.         // Sort vertices
  550.         if (y1>y2) { SWAP(y1, y2); SWAP(x1, x2); }
  551.         if (y1>y3) { SWAP(y1, y3); SWAP(x1, x3); }
  552.         if (y2>y3) { SWAP(y2, y3); SWAP(x2, x3); }
  553.  
  554.         t1x = t2x = x1; y = y1;   // Starting points
  555.         dx1 = (int)(x2 - x1); if (dx1<0) { dx1 = -dx1; signx1 = -1; }
  556.         else signx1 = 1;
  557.         dy1 = (int)(y2 - y1);
  558.  
  559.         dx2 = (int)(x3 - x1); if (dx2<0) { dx2 = -dx2; signx2 = -1; }
  560.         else signx2 = 1;
  561.         dy2 = (int)(y3 - y1);
  562.  
  563.         if (dy1 > dx1) {   // swap values
  564.             SWAP(dx1, dy1);
  565.             changed1 = true;
  566.         }
  567.         if (dy2 > dx2) {   // swap values
  568.             SWAP(dy2, dx2);
  569.             changed2 = true;
  570.         }
  571.  
  572.         e2 = (int)(dx2 >> 1);
  573.         // Flat top, just process the second half
  574.         if (y1 == y2) goto next;
  575.         e1 = (int)(dx1 >> 1);
  576.  
  577.         for (int i = 0; i < dx1;) {
  578.             t1xp = 0; t2xp = 0;
  579.             if (t1x<t2x) { minx = t1x; maxx = t2x; }
  580.             else { minx = t2x; maxx = t1x; }
  581.             // process first line until y value is about to change
  582.             while (i<dx1) {
  583.                 i++;
  584.                 e1 += dy1;
  585.                 while (e1 >= dx1) {
  586.                     e1 -= dx1;
  587.                     if (changed1) t1xp = signx1;//t1x += signx1;
  588.                     else          goto next1;
  589.                 }
  590.                 if (changed1) break;
  591.                 else t1x += signx1;
  592.             }
  593.             // Move line
  594.         next1:
  595.             // process second line until y value is about to change
  596.             while (1) {
  597.                 e2 += dy2;
  598.                 while (e2 >= dx2) {
  599.                     e2 -= dx2;
  600.                     if (changed2) t2xp = signx2;//t2x += signx2;
  601.                     else          goto next2;
  602.                 }
  603.                 if (changed2)     break;
  604.                 else              t2x += signx2;
  605.             }
  606.         next2:
  607.             if (minx>t1x) minx = t1x; if (minx>t2x) minx = t2x;
  608.             if (maxx<t1x) maxx = t1x; if (maxx<t2x) maxx = t2x;
  609.             drawline(minx, maxx, y);    // Draw line from min to max points found on the y
  610.                                          // Now increase y
  611.             if (!changed1) t1x += signx1;
  612.             t1x += t1xp;
  613.             if (!changed2) t2x += signx2;
  614.             t2x += t2xp;
  615.             y += 1;
  616.             if (y == y2) break;
  617.  
  618.         }
  619.     next:
  620.         // Second half
  621.         dx1 = (int)(x3 - x2); if (dx1<0) { dx1 = -dx1; signx1 = -1; }
  622.         else signx1 = 1;
  623.         dy1 = (int)(y3 - y2);
  624.         t1x = x2;
  625.  
  626.         if (dy1 > dx1) {   // swap values
  627.             SWAP(dy1, dx1);
  628.             changed1 = true;
  629.         }
  630.         else changed1 = false;
  631.  
  632.         e1 = (int)(dx1 >> 1);
  633.  
  634.         for (int i = 0; i <= dx1; i++) {
  635.             t1xp = 0; t2xp = 0;
  636.             if (t1x<t2x) { minx = t1x; maxx = t2x; }
  637.             else { minx = t2x; maxx = t1x; }
  638.             // process first line until y value is about to change
  639.             while (i<dx1) {
  640.                 e1 += dy1;
  641.                 while (e1 >= dx1) {
  642.                     e1 -= dx1;
  643.                     if (changed1) { t1xp = signx1; break; }//t1x += signx1;
  644.                     else          goto next3;
  645.                 }
  646.                 if (changed1) break;
  647.                 else          t1x += signx1;
  648.                 if (i<dx1) i++;
  649.             }
  650.         next3:
  651.             // process second line until y value is about to change
  652.             while (t2x != x3) {
  653.                 e2 += dy2;
  654.                 while (e2 >= dx2) {
  655.                     e2 -= dx2;
  656.                     if (changed2) t2xp = signx2;
  657.                     else          goto next4;
  658.                 }
  659.                 if (changed2)     break;
  660.                 else              t2x += signx2;
  661.             }
  662.         next4:
  663.  
  664.             if (minx>t1x) minx = t1x; if (minx>t2x) minx = t2x;
  665.             if (maxx<t1x) maxx = t1x; if (maxx<t2x) maxx = t2x;
  666.             drawline(minx, maxx, y);                                       
  667.             if (!changed1) t1x += signx1;
  668.             t1x += t1xp;
  669.             if (!changed2) t2x += signx2;
  670.             t2x += t2xp;
  671.             y += 1;
  672.             if (y>y3) return;
  673.         }
  674.     }
  675.  
  676.     void DrawCircle(int xc, int yc, int r, short c = 0x2588, short col = 0x000F)
  677.     {
  678.         int x = 0;
  679.         int y = r;
  680.         int p = 3 - 2 * r;
  681.         if (!r) return;
  682.  
  683.         while (y >= x) // only formulate 1/8 of circle
  684.         {
  685.             Draw(xc - x, yc - y, c, col);//upper left left
  686.             Draw(xc - y, yc - x, c, col);//upper upper left
  687.             Draw(xc + y, yc - x, c, col);//upper upper right
  688.             Draw(xc + x, yc - y, c, col);//upper right right
  689.             Draw(xc - x, yc + y, c, col);//lower left left
  690.             Draw(xc - y, yc + x, c, col);//lower lower left
  691.             Draw(xc + y, yc + x, c, col);//lower lower right
  692.             Draw(xc + x, yc + y, c, col);//lower right right
  693.             if (p < 0) p += 4 * x++ + 6;
  694.             else p += 4 * (x++ - y--) + 10;
  695.         }
  696.     }
  697.  
  698.     void FillCircle(int xc, int yc, int r, short c = 0x2588, short col = 0x000F)
  699.     {
  700.         // Taken from wikipedia
  701.         int x = 0;
  702.         int y = r;
  703.         int p = 3 - 2 * r;
  704.         if (!r) return;
  705.  
  706.         auto drawline = [&](int sx, int ex, int ny)
  707.         {
  708.             for (int i = sx; i <= ex; i++)
  709.                 Draw(i, ny, c, col);
  710.         };
  711.  
  712.         while (y >= x)
  713.         {
  714.             // Modified to draw scan-lines instead of edges
  715.             drawline(xc - x, xc + x, yc - y);
  716.             drawline(xc - y, xc + y, yc - x);
  717.             drawline(xc - x, xc + x, yc + y);
  718.             drawline(xc - y, xc + y, yc + x);
  719.             if (p < 0) p += 4 * x++ + 6;
  720.             else p += 4 * (x++ - y--) + 10;
  721.         }
  722.     };
  723.  
  724.     void DrawSprite(int x, int y, olcSprite *sprite)
  725.     {
  726.         if (sprite == nullptr)
  727.             return;
  728.  
  729.         for (int i = 0; i < sprite->nWidth; i++)
  730.         {
  731.             for (int j = 0; j < sprite->nHeight; j++)
  732.             {
  733.                 if (sprite->GetGlyph(i, j) != L' ')
  734.                     Draw(x + i, y + j, sprite->GetGlyph(i, j), sprite->GetColour(i, j));
  735.             }
  736.         }
  737.     }
  738.  
  739.     void DrawPartialSprite(int x, int y, olcSprite *sprite, int ox, int oy, int w, int h)
  740.     {
  741.         if (sprite == nullptr)
  742.             return;
  743.  
  744.         for (int i = 0; i < w; i++)
  745.         {
  746.             for (int j = 0; j < h; j++)
  747.             {
  748.                 if (sprite->GetGlyph(i+ox, j+oy) != L' ')
  749.                     Draw(x + i, y + j, sprite->GetGlyph(i+ox, j+oy), sprite->GetColour(i+ox, j+oy));
  750.             }
  751.         }
  752.     }
  753.  
  754.     void DrawWireFrameModel(const std::vector<std::pair<float, float>> &vecModelCoordinates, float x, float y, float r = 0.0f, float s = 1.0f, short col = FG_WHITE, short c = PIXEL_SOLID)
  755.     {
  756.         // pair.first = x coordinate
  757.         // pair.second = y coordinate
  758.  
  759.         // Create translated model vector of coordinate pairs
  760.         std::vector<std::pair<float, float>> vecTransformedCoordinates;
  761.         int verts = vecModelCoordinates.size();
  762.         vecTransformedCoordinates.resize(verts);
  763.  
  764.         // Rotate
  765.         for (int i = 0; i < verts; i++)
  766.         {
  767.             vecTransformedCoordinates[i].first = vecModelCoordinates[i].first * cosf(r) - vecModelCoordinates[i].second * sinf(r);
  768.             vecTransformedCoordinates[i].second = vecModelCoordinates[i].first * sinf(r) + vecModelCoordinates[i].second * cosf(r);
  769.         }
  770.  
  771.         // Scale
  772.         for (int i = 0; i < verts; i++)
  773.         {
  774.             vecTransformedCoordinates[i].first = vecTransformedCoordinates[i].first * s;
  775.             vecTransformedCoordinates[i].second = vecTransformedCoordinates[i].second * s;
  776.         }
  777.  
  778.         // Translate
  779.         for (int i = 0; i < verts; i++)
  780.         {
  781.             vecTransformedCoordinates[i].first = vecTransformedCoordinates[i].first + x;
  782.             vecTransformedCoordinates[i].second = vecTransformedCoordinates[i].second + y;
  783.         }
  784.  
  785.         // Draw Closed Polygon
  786.         for (int i = 0; i < verts + 1; i++)
  787.         {
  788.             int j = (i + 1);
  789.             DrawLine((int)vecTransformedCoordinates[i % verts].first, (int)vecTransformedCoordinates[i % verts].second,
  790.                 (int)vecTransformedCoordinates[j % verts].first, (int)vecTransformedCoordinates[j % verts].second, c, col);
  791.         }
  792.     }
  793.  
  794.     ~olcConsoleGameEngine()
  795.     {
  796.         SetConsoleActiveScreenBuffer(m_hOriginalConsole);
  797.         delete[] m_bufScreen;
  798.     }
  799.  
  800. public:
  801.     void Start()
  802.     {  
  803.         // Start the thread
  804.         m_bAtomActive = true;
  805.         std::thread t = std::thread(&olcConsoleGameEngine::GameThread, this);
  806.  
  807.         // Wait for thread to be exited
  808.         t.join();
  809.     }
  810.  
  811.     int ScreenWidth()
  812.     {
  813.         return m_nScreenWidth;
  814.     }
  815.  
  816.     int ScreenHeight()
  817.     {
  818.         return m_nScreenHeight;
  819.     }
  820.  
  821. private:
  822.     void GameThread()
  823.     {
  824.         // Create user resources as part of this thread
  825.         if (!OnUserCreate())
  826.             m_bAtomActive = false;
  827.  
  828.         // Check if sound system should be enabled
  829.         if (m_bEnableSound)
  830.         {
  831.             if (!CreateAudio())
  832.             {
  833.                 m_bAtomActive = false; // Failed to create audio system        
  834.                 m_bEnableSound = false;
  835.             }
  836.         }
  837.  
  838.         auto tp1 = std::chrono::system_clock::now();
  839.         auto tp2 = std::chrono::system_clock::now();
  840.  
  841.         while (m_bAtomActive)
  842.         {
  843.             // Run as fast as possible
  844.             while (m_bAtomActive)
  845.             {
  846.                 // Handle Timing
  847.                 tp2 = std::chrono::system_clock::now();
  848.                 std::chrono::duration<float> elapsedTime = tp2 - tp1;
  849.                 tp1 = tp2;
  850.                 float fElapsedTime = elapsedTime.count();
  851.  
  852.                 // Handle Keyboard Input
  853.                 for (int i = 0; i < 256; i++)
  854.                 {
  855.                     m_keyNewState[i] = GetAsyncKeyState(i);
  856.  
  857.                     m_keys[i].bPressed = false;
  858.                     m_keys[i].bReleased = false;
  859.  
  860.                     if (m_keyNewState[i] != m_keyOldState[i])
  861.                     {
  862.                         if (m_keyNewState[i] & 0x8000)
  863.                         {
  864.                             m_keys[i].bPressed = !m_keys[i].bHeld;
  865.                             m_keys[i].bHeld = true;
  866.                         }
  867.                         else
  868.                         {
  869.                             m_keys[i].bReleased = true;
  870.                             m_keys[i].bHeld = false;
  871.                         }
  872.                     }
  873.  
  874.                     m_keyOldState[i] = m_keyNewState[i];
  875.                 }
  876.  
  877.                 // Handle Mouse Input - Check for window events
  878.                 INPUT_RECORD inBuf[32];
  879.                 DWORD events = 0;
  880.                 GetNumberOfConsoleInputEvents(m_hConsoleIn, &events);
  881.                 if (events > 0)
  882.                     ReadConsoleInput(m_hConsoleIn, inBuf, events, &events);
  883.  
  884.                 // Handle events - we only care about mouse clicks and movement
  885.                 // for now
  886.                 for (DWORD i = 0; i < events; i++)
  887.                 {
  888.                     switch (inBuf[i].EventType)
  889.                     {
  890.                     case FOCUS_EVENT:
  891.                     {
  892.                         m_bConsoleInFocus = inBuf[i].Event.FocusEvent.bSetFocus;
  893.                     }
  894.                     break;
  895.  
  896.                     case MOUSE_EVENT:
  897.                     {
  898.                         switch (inBuf[i].Event.MouseEvent.dwEventFlags)
  899.                         {
  900.                         case MOUSE_MOVED:
  901.                         {
  902.                             m_mousePosX = inBuf[i].Event.MouseEvent.dwMousePosition.X;
  903.                             m_mousePosY = inBuf[i].Event.MouseEvent.dwMousePosition.Y;
  904.                         }
  905.                         break;
  906.  
  907.                         case 0:
  908.                         {
  909.                             for (int m = 0; m < 5; m++)
  910.                                 m_mouseNewState[m] = (inBuf[i].Event.MouseEvent.dwButtonState & (1 << m)) > 0;
  911.  
  912.                         }
  913.                         break;
  914.  
  915.                         default:
  916.                             break;
  917.                         }
  918.                     }
  919.                     break;
  920.  
  921.                     default:
  922.                         break;
  923.                         // We don't care just at the moment
  924.                     }
  925.                 }
  926.  
  927.                 for (int m = 0; m < 5; m++)
  928.                 {
  929.                     m_mouse[m].bPressed = false;
  930.                     m_mouse[m].bReleased = false;
  931.  
  932.                     if (m_mouseNewState[m] != m_mouseOldState[m])
  933.                     {
  934.                         if (m_mouseNewState[m])
  935.                         {
  936.                             m_mouse[m].bPressed = true;
  937.                             m_mouse[m].bHeld = true;
  938.                         }
  939.                         else
  940.                         {
  941.                             m_mouse[m].bReleased = true;
  942.                             m_mouse[m].bHeld = false;
  943.                         }
  944.                     }
  945.  
  946.                     m_mouseOldState[m] = m_mouseNewState[m];
  947.                 }
  948.  
  949.  
  950.                 // Handle Frame Update
  951.                 if (!OnUserUpdate(fElapsedTime))
  952.                     m_bAtomActive = false;
  953.  
  954.                 // Update Title & Present Screen Buffer
  955.                 wchar_t s[256];
  956.                 swprintf_s(s, 256, L"OneLoneCoder.com - Console Game Engine - %s - FPS: %3.2f", m_sAppName.c_str(), 1.0f / fElapsedTime);
  957.                 SetConsoleTitle(s);
  958.                 WriteConsoleOutput(m_hConsole, m_bufScreen, { (short)m_nScreenWidth, (short)m_nScreenHeight }, { 0,0 }, &m_rectWindow);
  959.             }
  960.  
  961.             if (m_bEnableSound)
  962.             {
  963.                 // Close and Clean up audio system
  964.             }
  965.  
  966.             // Allow the user to free resources if they have overrided the destroy function
  967.             if (OnUserDestroy())
  968.             {
  969.                 // User has permitted destroy, so exit and clean up
  970.                 delete[] m_bufScreen;
  971.                 SetConsoleActiveScreenBuffer(m_hOriginalConsole);
  972.                 m_cvGameFinished.notify_one();
  973.             }
  974.             else
  975.             {
  976.                 // User denied destroy for some reason, so continue running
  977.                 m_bAtomActive = true;
  978.             }
  979.         }
  980.     }
  981.  
  982. public:
  983.     // User MUST OVERRIDE THESE!!
  984.     virtual bool OnUserCreate()                         = 0;
  985.     virtual bool OnUserUpdate(float fElapsedTime)       = 0;   
  986.  
  987.     // Optional for clean up
  988.     virtual bool OnUserDestroy()                        { return true; }
  989.  
  990.  
  991.  
  992. protected: // Audio Engine =====================================================================
  993.  
  994.     class olcAudioSample
  995.     {
  996.     public:
  997.         olcAudioSample()
  998.         {
  999.  
  1000.         }
  1001.  
  1002.         olcAudioSample(std::wstring sWavFile)
  1003.         {
  1004.             // Load Wav file and convert to float format
  1005.             FILE *f = nullptr;
  1006.             _wfopen_s(&f, sWavFile.c_str(), L"rb");
  1007.             if (f == nullptr)
  1008.                 return;
  1009.  
  1010.             char dump[4];
  1011.             std::fread(&dump, sizeof(char), 4, f); // Read "RIFF"
  1012.             if (strncmp(dump, "RIFF", 4) != 0) return;
  1013.             std::fread(&dump, sizeof(char), 4, f); // Not Interested
  1014.             std::fread(&dump, sizeof(char), 4, f); // Read "WAVE"
  1015.             if (strncmp(dump, "WAVE", 4) != 0) return;
  1016.  
  1017.             // Read Wave description chunk
  1018.             std::fread(&dump, sizeof(char), 4, f); // Read "fmt "
  1019.             std::fread(&dump, sizeof(char), 4, f); // Not Interested
  1020.             std::fread(&wavHeader, sizeof(WAVEFORMATEX) - 2, 1, f); // Read Wave Format Structure chunk
  1021.                                                                     // Note the -2, because the structure has 2 bytes to indicate its own size
  1022.                                                                     // which are not in the wav file
  1023.  
  1024.             // Just check if wave format is compatible with olcCGE
  1025.             if (wavHeader.wBitsPerSample != 16 || wavHeader.nSamplesPerSec != 44100)
  1026.             {
  1027.                 std::fclose(f);
  1028.                 return;
  1029.             }
  1030.  
  1031.             // Search for audio data chunk
  1032.             long nChunksize = 0;
  1033.             std::fread(&dump, sizeof(char), 4, f); // Read chunk header
  1034.             std::fread(&nChunksize, sizeof(long), 1, f); // Read chunk size
  1035.             while (strncmp(dump, "data", 4) != 0)
  1036.             {
  1037.                 // Not audio data, so just skip it
  1038.                 std::fseek(f, nChunksize, SEEK_CUR);
  1039.                 std::fread(&dump, sizeof(char), 4, f);
  1040.                 std::fread(&nChunksize, sizeof(long), 1, f);
  1041.             }
  1042.  
  1043.             // Finally got to data, so read it all in and convert to float samples
  1044.             nSamples = nChunksize / (wavHeader.nChannels * (wavHeader.wBitsPerSample >> 3));
  1045.             nChannels = wavHeader.nChannels;
  1046.            
  1047.             // Create floating point buffer to hold audio sample
  1048.             fSample = new float[nSamples * nChannels];
  1049.             float *pSample = fSample;
  1050.            
  1051.             // Read in audio data and normalise
  1052.             for (long i = 0; i < nSamples; i++)
  1053.             {
  1054.                 for (int c = 0; c < nChannels; c++)
  1055.                 {
  1056.                     short s = 0;
  1057.                     std::fread(&s, sizeof(short), 1, f);
  1058.                     *pSample = (float)s / (float)(MAXSHORT);
  1059.                     pSample++;
  1060.                 }
  1061.             }
  1062.  
  1063.             // All done, flag sound as valid
  1064.             std::fclose(f);
  1065.             bSampleValid = true;
  1066.         }
  1067.  
  1068.         WAVEFORMATEX wavHeader;
  1069.         float *fSample = nullptr;
  1070.         long nSamples = 0;
  1071.         int nChannels = 0;
  1072.         bool bSampleValid = false;
  1073.     };
  1074.    
  1075.     // This vector holds all loaded sound samples in memory
  1076.     std::vector<olcAudioSample> vecAudioSamples;
  1077.  
  1078.     // This structure represents a sound that is currently playing. It only
  1079.     // holds the sound ID and where this instance of it is up to for its
  1080.     // current playback
  1081.     struct sCurrentlyPlayingSample
  1082.     {
  1083.         int nAudioSampleID = 0;
  1084.         long nSamplePosition = 0;
  1085.         bool bFinished = false;
  1086.         bool bLoop = false;
  1087.     };
  1088.     std::list<sCurrentlyPlayingSample> listActiveSamples;
  1089.  
  1090.     // Load a 16-bit WAVE file @ 44100Hz ONLY into memory. A sample ID
  1091.     // number is returned if successful, otherwise -1
  1092.     unsigned int LoadAudioSample(std::wstring sWavFile)
  1093.     {
  1094.         if (!m_bEnableSound)
  1095.             return -1;
  1096.  
  1097.         olcAudioSample a(sWavFile);
  1098.         if (a.bSampleValid)
  1099.         {
  1100.             vecAudioSamples.push_back(a);
  1101.             return vecAudioSamples.size();
  1102.         }
  1103.         else
  1104.             return -1;
  1105.     }
  1106.  
  1107.     // Add sample 'id' to the mixers sounds to play list
  1108.     void PlaySample(int id, bool bLoop = false)
  1109.     {
  1110.         sCurrentlyPlayingSample a;
  1111.         a.nAudioSampleID = id;
  1112.         a.nSamplePosition = 0;
  1113.         a.bFinished = false;
  1114.         a.bLoop = bLoop;
  1115.         listActiveSamples.push_back(a);
  1116.     }
  1117.  
  1118.     void StopSample(int id)
  1119.     {
  1120.  
  1121.     }
  1122.  
  1123.     // The audio system uses by default a specific wave format
  1124.     bool CreateAudio(unsigned int nSampleRate = 44100, unsigned int nChannels = 1,
  1125.         unsigned int nBlocks = 8, unsigned int nBlockSamples = 512)
  1126.     {
  1127.         // Initialise Sound Engine
  1128.         m_bAudioThreadActive = false;
  1129.         m_nSampleRate = nSampleRate;
  1130.         m_nChannels = nChannels;
  1131.         m_nBlockCount = nBlocks;
  1132.         m_nBlockSamples = nBlockSamples;
  1133.         m_nBlockFree = m_nBlockCount;
  1134.         m_nBlockCurrent = 0;
  1135.         m_pBlockMemory = nullptr;
  1136.         m_pWaveHeaders = nullptr;
  1137.  
  1138.         // Device is available
  1139.         WAVEFORMATEX waveFormat;
  1140.         waveFormat.wFormatTag = WAVE_FORMAT_PCM;
  1141.         waveFormat.nSamplesPerSec = m_nSampleRate;
  1142.         waveFormat.wBitsPerSample = sizeof(short) * 8;
  1143.         waveFormat.nChannels = m_nChannels;
  1144.         waveFormat.nBlockAlign = (waveFormat.wBitsPerSample / 8) * waveFormat.nChannels;
  1145.         waveFormat.nAvgBytesPerSec = waveFormat.nSamplesPerSec * waveFormat.nBlockAlign;
  1146.         waveFormat.cbSize = 0;
  1147.  
  1148.         // Open Device if valid
  1149.         if (waveOutOpen(&m_hwDevice, WAVE_MAPPER, &waveFormat, (DWORD_PTR)waveOutProcWrap, (DWORD_PTR)this, CALLBACK_FUNCTION) != S_OK)
  1150.             return DestroyAudio();
  1151.        
  1152.         // Allocate Wave|Block Memory
  1153.         m_pBlockMemory = new short[m_nBlockCount * m_nBlockSamples];
  1154.         if (m_pBlockMemory == nullptr)
  1155.             return DestroyAudio();
  1156.         ZeroMemory(m_pBlockMemory, sizeof(short) * m_nBlockCount * m_nBlockSamples);
  1157.  
  1158.         m_pWaveHeaders = new WAVEHDR[m_nBlockCount];
  1159.         if (m_pWaveHeaders == nullptr)
  1160.             return DestroyAudio();
  1161.         ZeroMemory(m_pWaveHeaders, sizeof(WAVEHDR) * m_nBlockCount);
  1162.  
  1163.         // Link headers to block memory
  1164.         for (unsigned int n = 0; n < m_nBlockCount; n++)
  1165.         {
  1166.             m_pWaveHeaders[n].dwBufferLength = m_nBlockSamples * sizeof(short);
  1167.             m_pWaveHeaders[n].lpData = (LPSTR)(m_pBlockMemory + (n * m_nBlockSamples));
  1168.         }
  1169.  
  1170.         m_bAudioThreadActive = true;
  1171.         m_AudioThread = std::thread(&olcConsoleGameEngine::AudioThread, this);
  1172.  
  1173.         // Start the ball rolling with the sound delivery thread
  1174.         std::unique_lock<std::mutex> lm(m_muxBlockNotZero);
  1175.         m_cvBlockNotZero.notify_one();
  1176.         return true;
  1177.     }
  1178.  
  1179.     // Stop and clean up audio system
  1180.     bool DestroyAudio()
  1181.     {
  1182.         m_bAudioThreadActive = false;
  1183.         return false;
  1184.     }
  1185.  
  1186.     // Handler for soundcard request for more data
  1187.     void waveOutProc(HWAVEOUT hWaveOut, UINT uMsg, DWORD dwParam1, DWORD dwParam2)
  1188.     {
  1189.         if (uMsg != WOM_DONE) return;
  1190.         m_nBlockFree++;
  1191.         std::unique_lock<std::mutex> lm(m_muxBlockNotZero);
  1192.         m_cvBlockNotZero.notify_one();
  1193.     }
  1194.  
  1195.     // Static wrapper for sound card handler
  1196.     static void CALLBACK waveOutProcWrap(HWAVEOUT hWaveOut, UINT uMsg, DWORD dwInstance, DWORD dwParam1, DWORD dwParam2)
  1197.     {
  1198.         ((olcConsoleGameEngine*)dwInstance)->waveOutProc(hWaveOut, uMsg, dwParam1, dwParam2);
  1199.     }
  1200.  
  1201.     // Audio thread. This loop responds to requests from the soundcard to fill 'blocks'
  1202.     // with audio data. If no requests are available it goes dormant until the sound
  1203.     // card is ready for more data. The block is fille by the "user" in some manner
  1204.     // and then issued to the soundcard.
  1205.     void AudioThread()
  1206.     {
  1207.         m_fGlobalTime = 0.0f;
  1208.         float fTimeStep = 1.0f / (float)m_nSampleRate;
  1209.  
  1210.         // Goofy hack to get maximum integer for a type at run-time
  1211.         short nMaxSample = (short)pow(2, (sizeof(short) * 8) - 1) - 1;
  1212.         float fMaxSample = (float)nMaxSample;
  1213.         short nPreviousSample = 0;
  1214.  
  1215.         while (m_bAudioThreadActive)
  1216.         {
  1217.             // Wait for block to become available
  1218.             if (m_nBlockFree == 0)
  1219.             {
  1220.                 std::unique_lock<std::mutex> lm(m_muxBlockNotZero);
  1221.                 while (m_nBlockFree == 0) // sometimes, Windows signals incorrectly
  1222.                     m_cvBlockNotZero.wait(lm);
  1223.             }
  1224.  
  1225.             // Block is here, so use it
  1226.             m_nBlockFree--;
  1227.  
  1228.             // Prepare block for processing
  1229.             if (m_pWaveHeaders[m_nBlockCurrent].dwFlags & WHDR_PREPARED)
  1230.                 waveOutUnprepareHeader(m_hwDevice, &m_pWaveHeaders[m_nBlockCurrent], sizeof(WAVEHDR));
  1231.  
  1232.             short nNewSample = 0;
  1233.             int nCurrentBlock = m_nBlockCurrent * m_nBlockSamples;
  1234.  
  1235.             auto clip = [](float fSample, float fMax)
  1236.             {
  1237.                 if (fSample >= 0.0)
  1238.                     return fmin(fSample, fMax);
  1239.                 else
  1240.                     return fmax(fSample, -fMax);
  1241.             };
  1242.  
  1243.             for (unsigned int n = 0; n < m_nBlockSamples; n += m_nChannels)
  1244.             {
  1245.                 // User Process
  1246.                 for (unsigned int c = 0; c < m_nChannels; c++)
  1247.                 {
  1248.                     nNewSample = (short)(clip(GetMixerOutput(c, m_fGlobalTime, fTimeStep), 1.0) * fMaxSample);
  1249.                     m_pBlockMemory[nCurrentBlock + n + c] = nNewSample;
  1250.                     nPreviousSample = nNewSample;
  1251.                 }
  1252.  
  1253.                 m_fGlobalTime = m_fGlobalTime + fTimeStep;
  1254.             }
  1255.  
  1256.             // Send block to sound device
  1257.             waveOutPrepareHeader(m_hwDevice, &m_pWaveHeaders[m_nBlockCurrent], sizeof(WAVEHDR));
  1258.             waveOutWrite(m_hwDevice, &m_pWaveHeaders[m_nBlockCurrent], sizeof(WAVEHDR));
  1259.             m_nBlockCurrent++;
  1260.             m_nBlockCurrent %= m_nBlockCount;
  1261.         }
  1262.     }
  1263.  
  1264.     // Overridden by user if they want to generate sound in real-time
  1265.     virtual float onUserSoundSample(int nChannel, float fGlobalTime, float fTimeStep)
  1266.     {
  1267.         return 0.0f;
  1268.     }
  1269.  
  1270.     // Overriden by user if they want to manipulate the sound before it is played
  1271.     virtual float onUserSoundFilter(int nChannel, float fGlobalTime, float fSample)
  1272.     {
  1273.         return fSample;
  1274.     }
  1275.  
  1276.     // The Sound Mixer - If the user wants to play many sounds simultaneously, and
  1277.     // perhaps the same sound overlapping itself, then you need a mixer, which
  1278.     // takes input from all sound sources for that audio frame. This mixer maintains
  1279.     // a list of sound locations for all concurrently playing audio samples. Instead
  1280.     // of duplicating audio data, we simply store the fact that a sound sample is in
  1281.     // use and an offset into its sample data. As time progresses we update this offset
  1282.     // until it is beyound the length of the sound sample it is attached to. At this
  1283.     // point we remove the playing souind from the list.
  1284.     //
  1285.     // Additionally, the users application may want to generate sound instead of just
  1286.     // playing audio clips (think a synthesizer for example) in whcih case we also
  1287.     // provide an "onUser..." event to allow the user to return a sound for that point
  1288.     // in time.
  1289.     //
  1290.     // Finally, before the sound is issued to the operating system for performing, the
  1291.     // user gets one final chance to "filter" the sound, perhaps changing the volume
  1292.     // or adding funky effects
  1293.     float GetMixerOutput(int nChannel, float fGlobalTime, float fTimeStep)
  1294.     {
  1295.         // Accumulate sample for this channel
  1296.         float fMixerSample = 0.0f;
  1297.  
  1298.         for (auto &s : listActiveSamples)
  1299.         {
  1300.             // Calculate sample position
  1301.             s.nSamplePosition += (long)((float)vecAudioSamples[s.nAudioSampleID - 1].wavHeader.nSamplesPerSec * fTimeStep);
  1302.  
  1303.             // If sample position is valid add to the mix
  1304.             if (s.nSamplePosition < vecAudioSamples[s.nAudioSampleID - 1].nSamples)
  1305.                 fMixerSample += vecAudioSamples[s.nAudioSampleID - 1].fSample[(s.nSamplePosition * vecAudioSamples[s.nAudioSampleID - 1].nChannels) + nChannel];
  1306.             else
  1307.                 s.bFinished = true; // Else sound has completed
  1308.         }
  1309.  
  1310.         // If sounds have completed then remove them
  1311.         listActiveSamples.remove_if([](const sCurrentlyPlayingSample &s) {return s.bFinished; });
  1312.  
  1313.         // The users application might be generating sound, so grab that if it exists
  1314.         fMixerSample += onUserSoundSample(nChannel, fGlobalTime, fTimeStep);
  1315.  
  1316.         // Return the sample via an optional user override to filter the sound
  1317.         return onUserSoundFilter(nChannel, fGlobalTime, fMixerSample);
  1318.     }
  1319.  
  1320.     unsigned int m_nSampleRate;
  1321.     unsigned int m_nChannels;
  1322.     unsigned int m_nBlockCount;
  1323.     unsigned int m_nBlockSamples;
  1324.     unsigned int m_nBlockCurrent;
  1325.  
  1326.     short* m_pBlockMemory = nullptr;
  1327.     WAVEHDR *m_pWaveHeaders = nullptr;
  1328.     HWAVEOUT m_hwDevice = nullptr;
  1329.  
  1330.     std::thread m_AudioThread;
  1331.     std::atomic<bool> m_bAudioThreadActive = false;
  1332.     std::atomic<unsigned int> m_nBlockFree = 0;
  1333.     std::condition_variable m_cvBlockNotZero;
  1334.     std::mutex m_muxBlockNotZero;
  1335.     std::atomic<float> m_fGlobalTime = 0.0f;
  1336.  
  1337.    
  1338.  
  1339. protected:
  1340.    
  1341.  
  1342.     struct sKeyState
  1343.     {
  1344.         bool bPressed;
  1345.         bool bReleased;
  1346.         bool bHeld;
  1347.     } m_keys[256], m_mouse[5];
  1348.  
  1349.     int m_mousePosX;
  1350.     int m_mousePosY;
  1351.  
  1352. public:
  1353.     sKeyState GetKey(int nKeyID){ return m_keys[nKeyID]; }
  1354.     int GetMouseX() { return m_mousePosX; }
  1355.     int GetMouseY() { return m_mousePosY; }
  1356.     sKeyState GetMouse(int nMouseButtonID) { return m_mouse[nMouseButtonID]; }
  1357.     bool IsFocused() { return m_bConsoleInFocus; }
  1358.  
  1359.  
  1360. protected:
  1361.     int Error(const wchar_t *msg)
  1362.     {
  1363.         wchar_t buf[256];
  1364.         FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM, NULL, GetLastError(), MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), buf, 256, NULL);
  1365.         SetConsoleActiveScreenBuffer(m_hOriginalConsole);
  1366.         wprintf(L"ERROR: %s\n\t%s\n", msg, buf);
  1367.         return 0;
  1368.     }
  1369.  
  1370.     static BOOL CloseHandler(DWORD evt)
  1371.     {
  1372.         // Note this gets called in a seperate OS thread, so it must
  1373.         // only exit when the game has finished cleaning up, or else
  1374.         // the process will be killed before OnUserDestroy() has finished
  1375.         if (evt == CTRL_CLOSE_EVENT)
  1376.         {
  1377.             m_bAtomActive = false;
  1378.  
  1379.             // Wait for thread to be exited
  1380.             std::unique_lock<std::mutex> ul(m_muxGame);
  1381.             m_cvGameFinished.wait(ul);
  1382.         }
  1383.         return true;
  1384.     }
  1385.  
  1386. protected:
  1387.     int m_nScreenWidth;
  1388.     int m_nScreenHeight;
  1389.     CHAR_INFO *m_bufScreen;
  1390.     std::wstring m_sAppName;
  1391.     HANDLE m_hOriginalConsole;
  1392.     CONSOLE_SCREEN_BUFFER_INFO m_OriginalConsoleInfo;
  1393.     HANDLE m_hConsole;
  1394.     HANDLE m_hConsoleIn;
  1395.     SMALL_RECT m_rectWindow;
  1396.     short m_keyOldState[256] = { 0 };
  1397.     short m_keyNewState[256] = { 0 };
  1398.     bool m_mouseOldState[5] = { 0 };
  1399.     bool m_mouseNewState[5] = { 0 };
  1400.     bool m_bConsoleInFocus = true; 
  1401.     bool m_bEnableSound = false;
  1402.  
  1403.     // These need to be static because of the OnDestroy call the OS may make. The OS
  1404.     // spawns a special thread just for that
  1405.     static std::atomic<bool> m_bAtomActive;
  1406.     static std::condition_variable m_cvGameFinished;
  1407.     static std::mutex m_muxGame;
  1408. };
  1409.  
  1410. // Define our static variables
  1411. std::atomic<bool> olcConsoleGameEngine::m_bAtomActive(false);
  1412. std::condition_variable olcConsoleGameEngine::m_cvGameFinished;
  1413. std::mutex olcConsoleGameEngine::m_muxGame;
  1414.  
Add Comment
Please, Sign In to add comment