From 9a163c7ebf10c445c8ff9bb1a799585a1117d497 Mon Sep 17 00:00:00 2001 From: OneLoneCoder Date: Mon, 21 Aug 2017 17:37:30 +0100 Subject: [PATCH] Updated with olcSprite Stuff --- olcConsoleGameEngine.h | 149 ++++++++++++++++++++++++++++++++++++++++- 1 file changed, 147 insertions(+), 2 deletions(-) diff --git a/olcConsoleGameEngine.h b/olcConsoleGameEngine.h index a80fdc7..9747033 100644 --- a/olcConsoleGameEngine.h +++ b/olcConsoleGameEngine.h @@ -27,7 +27,7 @@ Video: ~~~~~~ https://youtu.be/cWc0hgYwZyc -Last Updated: 10/07/2017 +Last Updated: 21/08/2017 Usage: ~~~~~~ @@ -117,11 +117,126 @@ enum COLOUR enum PIXEL_TYPE { PIXEL_SOLID = 0x2588, - PIXEL_THREEQUARTERS = 0x2593 + PIXEL_THREEQUARTERS = 0x2593, PIXEL_HALF = 0x2592, PIXEL_QUARTER = 0x2591, }; +class olcSprite +{ +public: + olcSprite() + { + + } + + olcSprite(int w, int h) + { + Create(w, h); + } + + olcSprite(wstring sFile) + { + if (!Load(sFile)) + Create(8, 8); + } + + int nWidth = 0; + int nHeight = 0; + +private: + wchar_t *m_Glyphs = nullptr; + short *m_Colours = nullptr; + + void Create(int w, int h) + { + nWidth = w; + nHeight = h; + m_Glyphs = new wchar_t[w*h]; + m_Colours = new short[w*h]; + for (int i = 0; i < w*h; i++) + { + m_Glyphs[i] = L' '; + m_Colours[i] = FG_BLACK; + } + } + +public: + void SetGlyph(int x, int y, wchar_t c) + { + if (x <0 || x > nWidth || y < 0 || y > nHeight) + return; + else + m_Glyphs[y * nWidth + x] = c; + } + + void SetColour(int x, int y, short c) + { + if (x <0 || x > nWidth || y < 0 || y > nHeight) + return; + else + m_Colours[y * nWidth + x] = c; + } + + wchar_t GetGlyph(int x, int y) + { + if (x <0 || x > nWidth || y < 0 || y > nHeight) + return L' '; + else + return m_Glyphs[y * nWidth + x]; + } + + short GetColour(int x, int y) + { + if (x <0 || x > nWidth || y < 0 || y > nHeight) + return FG_BLACK; + else + return m_Colours[y * nWidth + x]; + } + + bool Save(wstring sFile) + { + FILE *f = nullptr; + _wfopen_s(&f, sFile.c_str(), L"wb"); + if (f == nullptr) + return false; + + fwrite(&nWidth, sizeof(int), 1, f); + fwrite(&nHeight, sizeof(int), 1, f); + fwrite(m_Colours, sizeof(short), nWidth * nHeight, f); + fwrite(m_Glyphs, sizeof(wchar_t), nWidth * nHeight, f); + + fclose(f); + + return true; + } + + bool Load(wstring sFile) + { + delete[] m_Glyphs; + delete[] m_Colours; + nWidth = 0; + nHeight = 0; + + FILE *f = nullptr; + _wfopen_s(&f, sFile.c_str(), L"rb"); + if (f == nullptr) + return false; + + fread(&nWidth, sizeof(int), 1, f); + fread(&nHeight, sizeof(int), 1, f); + + Create(nWidth, nHeight); + + fread(m_Colours, sizeof(short), nWidth * nHeight, f); + fread(m_Glyphs, sizeof(wchar_t), nWidth * nHeight, f); + + fclose(f); + return true; + } + +}; + class olcConsoleGameEngine { @@ -300,6 +415,36 @@ public: } } + void DrawSprite(int x, int y, olcSprite *sprite) + { + if (sprite == nullptr) + return; + + for (int i = 0; i < sprite->nWidth; i++) + { + for (int j = 0; j < sprite->nHeight; j++) + { + if (sprite->GetGlyph(i, j) != L' ') + Draw(x + i, y + j, sprite->GetGlyph(i, j), sprite->GetColour(i, j)); + } + } + } + + void DrawPartialSprite(int x, int y, olcSprite *sprite, int ox, int oy, int w, int h) + { + if (sprite == nullptr) + return; + + for (int i = 0; i < w; i++) + { + for (int j = 0; j < h; j++) + { + if (sprite->GetGlyph(i+ox, j+oy) != L' ') + Draw(x + i, y + j, sprite->GetGlyph(i+ox, j+oy), sprite->GetColour(i+ox, j+oy)); + } + } + } + ~olcConsoleGameEngine()