|
|
@ -2,7 +2,7 @@ |
|
|
|
olcPixelGameEngine.h |
|
|
|
olcPixelGameEngine.h |
|
|
|
|
|
|
|
|
|
|
|
+-------------------------------------------------------------+ |
|
|
|
+-------------------------------------------------------------+ |
|
|
|
| OneLoneCoder Pixel Game Engine v1.3 | |
|
|
|
| OneLoneCoder Pixel Game Engine v1.6 | |
|
|
|
| "Like the command prompt console one, but not..." - javidx9 | |
|
|
|
| "Like the command prompt console one, but not..." - javidx9 | |
|
|
|
+-------------------------------------------------------------+ |
|
|
|
+-------------------------------------------------------------+ |
|
|
|
|
|
|
|
|
|
|
@ -118,17 +118,60 @@ |
|
|
|
Thanks |
|
|
|
Thanks |
|
|
|
~~~~~~ |
|
|
|
~~~~~~ |
|
|
|
I'd like to extend thanks to Eremiell, slavka, Phantim, JackOJC, |
|
|
|
I'd like to extend thanks to Eremiell, slavka, Phantim, JackOJC, |
|
|
|
KrossX, Huhlig, Dragoneye, Appa & MagetzUb for advice, ideas and testing, |
|
|
|
KrossX, Huhlig, Dragoneye, Appa, JustinRichardsMusic, SliceNDice |
|
|
|
and I'd like to extend my appreciation to the 13K YouTube followers |
|
|
|
& MagetzUb for advice, ideas and testing, and I'd like to extend
|
|
|
|
and 1K Discord server members who give me the motivation to keep |
|
|
|
my appreciation to the 14K YouTube followers and 1K Discord server |
|
|
|
going with all this :D |
|
|
|
members who give me the motivation to keep going with all this :D |
|
|
|
|
|
|
|
|
|
|
|
Author |
|
|
|
Author |
|
|
|
~~~~~~ |
|
|
|
~~~~~~ |
|
|
|
David Barr, aka javidx9, ©OneLoneCoder 2018 |
|
|
|
David Barr, aka javidx9, ©OneLoneCoder 2018 |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
|
|
|
|
|
|
|
|
#pragma once |
|
|
|
////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/* Example Usage (main.cpp)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#define OLC_PGE_APPLICATION |
|
|
|
|
|
|
|
#include "olcPixelGameEngine.h" |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Override base class with your custom functionality
|
|
|
|
|
|
|
|
class Example : public olc::PixelGameEngine |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
public: |
|
|
|
|
|
|
|
Example() |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
sAppName = "Example"; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public: |
|
|
|
|
|
|
|
bool OnUserCreate() override |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
// Called once at the start, so create things here
|
|
|
|
|
|
|
|
return true; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
bool OnUserUpdate(float fElapsedTime) override |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
// called once per frame, draws random coloured pixels
|
|
|
|
|
|
|
|
for (int x = 0; x < ScreenWidth(); x++) |
|
|
|
|
|
|
|
for (int y = 0; y < ScreenHeight(); y++) |
|
|
|
|
|
|
|
Draw(x, y, olc::Pixel(rand() % 255, rand() % 255, rand()% 255)); |
|
|
|
|
|
|
|
return true; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
int main() |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
Example demo; |
|
|
|
|
|
|
|
if (demo.Construct(256, 240, 4, 4)) |
|
|
|
|
|
|
|
demo.Start(); |
|
|
|
|
|
|
|
return 0; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
*/ |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#ifndef OLC_PGE_DEF |
|
|
|
|
|
|
|
#define OLC_PGE_DEF |
|
|
|
|
|
|
|
|
|
|
|
#ifdef _WIN32 |
|
|
|
#ifdef _WIN32 |
|
|
|
// Link to libraries
|
|
|
|
// Link to libraries
|
|
|
@ -183,8 +226,6 @@ |
|
|
|
#undef min |
|
|
|
#undef min |
|
|
|
#undef max |
|
|
|
#undef max |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
namespace olc // All OneLoneCoder stuff will now exist in the "olc" namespace
|
|
|
|
namespace olc // All OneLoneCoder stuff will now exist in the "olc" namespace
|
|
|
|
{ |
|
|
|
{ |
|
|
|
struct Pixel |
|
|
|
struct Pixel |
|
|
@ -200,19 +241,19 @@ namespace olc // All OneLoneCoder stuff will now exist in the "olc" namespace |
|
|
|
|
|
|
|
|
|
|
|
Pixel(); |
|
|
|
Pixel(); |
|
|
|
Pixel(uint8_t red, uint8_t green, uint8_t blue, uint8_t alpha = 255); |
|
|
|
Pixel(uint8_t red, uint8_t green, uint8_t blue, uint8_t alpha = 255); |
|
|
|
enum Mode {NORMAL, MASK, ALPHA}; |
|
|
|
enum Mode { NORMAL, MASK, ALPHA }; |
|
|
|
}; |
|
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
// Some constants for symbolic naming of Pixels
|
|
|
|
// Some constants for symbolic naming of Pixels
|
|
|
|
static const Pixel |
|
|
|
static const Pixel |
|
|
|
WHITE(255, 255, 255), |
|
|
|
WHITE(255, 255, 255), |
|
|
|
GREY(192, 192, 192), DARK_GREY(128, 128, 128), VERY_DARK_GREY(64, 64, 64), |
|
|
|
GREY(192, 192, 192), DARK_GREY(128, 128, 128), VERY_DARK_GREY(64, 64, 64), |
|
|
|
RED(255, 0, 0), DARK_RED(128, 0, 0), VERY_DARK_RED(64, 0, 0), |
|
|
|
RED(255, 0, 0), DARK_RED(128, 0, 0), VERY_DARK_RED(64, 0, 0), |
|
|
|
YELLOW(255, 255, 0), DARK_YELLOW(128, 128, 0), VERY_DARK_YELLOW(64, 64, 0), |
|
|
|
YELLOW(255, 255, 0), DARK_YELLOW(128, 128, 0), VERY_DARK_YELLOW(64, 64, 0), |
|
|
|
GREEN(0, 255, 0), DARK_GREEN(0, 128, 0), VERY_DARK_GREEN(0, 64, 0), |
|
|
|
GREEN(0, 255, 0), DARK_GREEN(0, 128, 0), VERY_DARK_GREEN(0, 64, 0), |
|
|
|
CYAN(0, 255, 255), DARK_CYAN(0, 128, 128), VERY_DARK_CYAN(0, 64, 64), |
|
|
|
CYAN(0, 255, 255), DARK_CYAN(0, 128, 128), VERY_DARK_CYAN(0, 64, 64), |
|
|
|
BLUE(0, 0, 255), DARK_BLUE(0, 0, 128), VERY_DARK_BLUE(0, 0, 64), |
|
|
|
BLUE(0, 0, 255), DARK_BLUE(0, 0, 128), VERY_DARK_BLUE(0, 0, 64), |
|
|
|
MAGENTA(255, 0, 255), DARK_MAGENTA(128, 0, 128), VERY_DARK_MAGENTA(64, 0, 64), |
|
|
|
MAGENTA(255, 0, 255), DARK_MAGENTA(128, 0, 128), VERY_DARK_MAGENTA(64, 0, 64), |
|
|
|
BLACK(0, 0, 0), |
|
|
|
BLACK(0, 0, 0), |
|
|
|
BLANK(0, 0, 0, 0); |
|
|
|
BLANK(0, 0, 0, 0); |
|
|
|
|
|
|
|
|
|
|
@ -260,6 +301,11 @@ namespace olc // All OneLoneCoder stuff will now exist in the "olc" namespace |
|
|
|
private: |
|
|
|
private: |
|
|
|
Pixel *pColData = nullptr; |
|
|
|
Pixel *pColData = nullptr; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#ifdef OLC_DBG_OVERDRAW |
|
|
|
|
|
|
|
public: |
|
|
|
|
|
|
|
static int nOverdrawCount; |
|
|
|
|
|
|
|
#endif |
|
|
|
|
|
|
|
|
|
|
|
}; |
|
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
//=============================================================
|
|
|
|
//=============================================================
|
|
|
@ -349,10 +395,10 @@ namespace olc // All OneLoneCoder stuff will now exist in the "olc" namespace |
|
|
|
// Flat fills a triangle between points (x1,y1), (x2,y2) and (x3,y3)
|
|
|
|
// Flat fills a triangle between points (x1,y1), (x2,y2) and (x3,y3)
|
|
|
|
void FillTriangle(int32_t x1, int32_t y1, int32_t x2, int32_t y2, int32_t x3, int32_t y3, Pixel p = olc::WHITE); |
|
|
|
void FillTriangle(int32_t x1, int32_t y1, int32_t x2, int32_t y2, int32_t x3, int32_t y3, Pixel p = olc::WHITE); |
|
|
|
// Draws an entire sprite at location (x,y)
|
|
|
|
// Draws an entire sprite at location (x,y)
|
|
|
|
void DrawSprite(int32_t x, int32_t y, Sprite *sprite); |
|
|
|
void DrawSprite(int32_t x, int32_t y, Sprite *sprite, uint32_t scale = 1); |
|
|
|
// Draws an area of a sprite at location (x,y), where the
|
|
|
|
// Draws an area of a sprite at location (x,y), where the
|
|
|
|
// selected area is (ox,oy) to (ox+w,oy+h)
|
|
|
|
// selected area is (ox,oy) to (ox+w,oy+h)
|
|
|
|
void DrawPartialSprite(int32_t x, int32_t y, Sprite *sprite, int32_t ox, int32_t oy, int32_t w, int32_t h); |
|
|
|
void DrawPartialSprite(int32_t x, int32_t y, Sprite *sprite, int32_t ox, int32_t oy, int32_t w, int32_t h, uint32_t scale = 1); |
|
|
|
// Draws a single line of text
|
|
|
|
// Draws a single line of text
|
|
|
|
void DrawString(int32_t x, int32_t y, std::string sText, Pixel col = olc::WHITE, uint32_t scale = 1); |
|
|
|
void DrawString(int32_t x, int32_t y, std::string sText, Pixel col = olc::WHITE, uint32_t scale = 1); |
|
|
|
// Clears entire draw target to Pixel
|
|
|
|
// Clears entire draw target to Pixel
|
|
|
@ -382,6 +428,7 @@ namespace olc // All OneLoneCoder stuff will now exist in the "olc" namespace |
|
|
|
float fFramePeriod = 0.0f; |
|
|
|
float fFramePeriod = 0.0f; |
|
|
|
Sprite *fontSprite = nullptr; |
|
|
|
Sprite *fontSprite = nullptr; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
static std::map<uint16_t, uint8_t> mapKeys; |
|
|
|
static std::map<uint16_t, uint8_t> mapKeys; |
|
|
|
bool pKeyNewState[256]{ 0 }; |
|
|
|
bool pKeyNewState[256]{ 0 }; |
|
|
|
bool pKeyOldState[256]{ 0 }; |
|
|
|
bool pKeyOldState[256]{ 0 }; |
|
|
@ -441,25 +488,32 @@ namespace olc // All OneLoneCoder stuff will now exist in the "olc" namespace |
|
|
|
//=============================================================
|
|
|
|
//=============================================================
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#endif // OLC_PGE_DEF
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
|
|
|
Object Oriented Mode |
|
|
|
|
|
|
|
~~~~~~~~~~~~~~~~~~~~ |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
If the olcPixelGameEngine.h is called from several sources it can cause |
|
|
|
|
|
|
|
multiple definitions of objects. To prevent this, ALL but ONE of the pathways |
|
|
|
|
|
|
|
to including this file must have OLC_OOP_MODE defined before it. This prevents |
|
|
|
|
|
|
|
the definitions being duplicated. |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Consider the following project structure: |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Class1.h - Includes olcPixelGameEngine.h, overrides olc::PixelGameEngine |
|
|
|
|
|
|
|
Class1.cpp - #define OLC_OOP_MODE #include "Class1.h" |
|
|
|
|
|
|
|
Class2.h - Includes Class1.h, which includes olcPixelGameEngine.h |
|
|
|
|
|
|
|
Class2.cpp - #define OLC_OOP_MODE #include "Class2.h" |
|
|
|
|
|
|
|
main.cpp - Includes Class1.h and Class2.h |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
*/ |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#ifdef OLC_PGE_APPLICATION |
|
|
|
|
|
|
|
#undef OLC_PGE_APPLICATION |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// For OOP heavy implementations, move below this line to a seperate "olcSimpleGameEngineOOP.cpp"
|
|
|
|
|
|
|
|
// file, and include the above in to a seperate "olcSimpleGameEngineOOP.h" file
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
namespace olc |
|
|
|
namespace olc |
|
|
|
{ |
|
|
|
{ |
|
|
@ -630,6 +684,11 @@ namespace olc |
|
|
|
|
|
|
|
|
|
|
|
void Sprite::SetPixel(int32_t x, int32_t y, Pixel p) |
|
|
|
void Sprite::SetPixel(int32_t x, int32_t y, Pixel p) |
|
|
|
{ |
|
|
|
{ |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#ifdef OLC_DBG_OVERDRAW |
|
|
|
|
|
|
|
nOverdrawCount++; |
|
|
|
|
|
|
|
#endif |
|
|
|
|
|
|
|
|
|
|
|
if (x >= 0 && x < width && y >= 0 && y < height) |
|
|
|
if (x >= 0 && x < width && y >= 0 && y < height) |
|
|
|
pColData[y*width + x] = p; |
|
|
|
pColData[y*width + x] = p; |
|
|
|
} |
|
|
|
} |
|
|
@ -785,6 +844,7 @@ namespace olc |
|
|
|
{ |
|
|
|
{ |
|
|
|
if (!pDrawTarget) return; |
|
|
|
if (!pDrawTarget) return; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (nPixelMode == Pixel::NORMAL) |
|
|
|
if (nPixelMode == Pixel::NORMAL) |
|
|
|
{ |
|
|
|
{ |
|
|
|
pDrawTarget->SetPixel(x, y, p); |
|
|
|
pDrawTarget->SetPixel(x, y, p); |
|
|
@ -939,6 +999,9 @@ namespace olc |
|
|
|
Pixel* m = GetDrawTarget()->GetData(); |
|
|
|
Pixel* m = GetDrawTarget()->GetData(); |
|
|
|
for (int i = 0; i < pixels; i++) |
|
|
|
for (int i = 0; i < pixels; i++) |
|
|
|
m[i] = p; |
|
|
|
m[i] = p; |
|
|
|
|
|
|
|
#ifdef OLC_DBG_OVERDRAW |
|
|
|
|
|
|
|
olc::Sprite::nOverdrawCount += pixels; |
|
|
|
|
|
|
|
#endif |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
void PixelGameEngine::FillRect(int32_t x, int32_t y, int32_t w, int32_t h, Pixel p) |
|
|
|
void PixelGameEngine::FillRect(int32_t x, int32_t y, int32_t w, int32_t h, Pixel p) |
|
|
@ -1106,32 +1169,45 @@ namespace olc |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
void PixelGameEngine::DrawSprite(int32_t x, int32_t y, Sprite *sprite) |
|
|
|
void PixelGameEngine::DrawSprite(int32_t x, int32_t y, Sprite *sprite, uint32_t scale) |
|
|
|
{ |
|
|
|
{ |
|
|
|
if (sprite == nullptr) |
|
|
|
if (sprite == nullptr) |
|
|
|
return; |
|
|
|
return; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (scale > 1) |
|
|
|
for (int i = 0; i < sprite->width; i++) |
|
|
|
|
|
|
|
{ |
|
|
|
{ |
|
|
|
for (int j = 0; j < sprite->height; j++) |
|
|
|
for (int32_t i = 0; i < sprite->width; i++) |
|
|
|
{ |
|
|
|
for (int32_t j = 0; j < sprite->height; j++) |
|
|
|
Draw(x + i, y + j, sprite->GetPixel(i, j)); |
|
|
|
for (uint32_t is = 0; is < scale; is++) |
|
|
|
} |
|
|
|
for (uint32_t js = 0; js < scale; js++) |
|
|
|
|
|
|
|
Draw(x + (i*scale) + is, y + (j*scale) + js, sprite->GetPixel(i, j)); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
else |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
for (int32_t i = 0; i < sprite->width; i++) |
|
|
|
|
|
|
|
for (int32_t j = 0; j < sprite->height; j++) |
|
|
|
|
|
|
|
Draw(x + i, y + j, sprite->GetPixel(i, j)); |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
void PixelGameEngine::DrawPartialSprite(int32_t x, int32_t y, Sprite *sprite, int32_t ox, int32_t oy, int32_t w, int32_t h) |
|
|
|
void PixelGameEngine::DrawPartialSprite(int32_t x, int32_t y, Sprite *sprite, int32_t ox, int32_t oy, int32_t w, int32_t h, uint32_t scale) |
|
|
|
{ |
|
|
|
{ |
|
|
|
if (sprite == nullptr) |
|
|
|
if (sprite == nullptr) |
|
|
|
return; |
|
|
|
return; |
|
|
|
|
|
|
|
|
|
|
|
for (int i = 0; i < w; i++) |
|
|
|
if (scale > 1) |
|
|
|
{ |
|
|
|
{ |
|
|
|
for (int j = 0; j < h; j++) |
|
|
|
for (int32_t i = 0; i < w; i++) |
|
|
|
{ |
|
|
|
for (int32_t j = 0; j < h; j++) |
|
|
|
Draw(x + i, y + j, sprite->GetPixel(i+ox, j+oy)); |
|
|
|
for (uint32_t is = 0; is < scale; is++) |
|
|
|
} |
|
|
|
for (uint32_t js = 0; js < scale; js++) |
|
|
|
|
|
|
|
Draw(x + (i*scale) + is, y + (j*scale) + js, sprite->GetPixel(i + ox, j + oy)); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
else |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
for (int32_t i = 0; i < w; i++) |
|
|
|
|
|
|
|
for (int32_t j = 0; j < h; j++) |
|
|
|
|
|
|
|
Draw(x + i, y + j, sprite->GetPixel(i + ox, j + oy)); |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
@ -1339,6 +1415,10 @@ namespace olc |
|
|
|
pMouseOldState[i] = pMouseNewState[i]; |
|
|
|
pMouseOldState[i] = pMouseNewState[i]; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#ifdef OLC_DBG_OVERDRAW |
|
|
|
|
|
|
|
olc::Sprite::nOverdrawCount = 0; |
|
|
|
|
|
|
|
#endif |
|
|
|
|
|
|
|
|
|
|
|
// Handle Frame Update
|
|
|
|
// Handle Frame Update
|
|
|
|
if (!OnUserUpdate(fElapsedTime)) |
|
|
|
if (!OnUserUpdate(fElapsedTime)) |
|
|
|
bAtomActive = false; |
|
|
|
bAtomActive = false; |
|
|
@ -1657,6 +1737,10 @@ namespace olc |
|
|
|
std::atomic<bool> PixelGameEngine::bAtomActive{ false }; |
|
|
|
std::atomic<bool> PixelGameEngine::bAtomActive{ false }; |
|
|
|
std::map<uint16_t, uint8_t> PixelGameEngine::mapKeys; |
|
|
|
std::map<uint16_t, uint8_t> PixelGameEngine::mapKeys; |
|
|
|
olc::PixelGameEngine* olc::PGEX::pge = nullptr; |
|
|
|
olc::PixelGameEngine* olc::PGEX::pge = nullptr; |
|
|
|
|
|
|
|
#ifdef OLC_DBG_OVERDRAW |
|
|
|
|
|
|
|
int olc::Sprite::nOverdrawCount = 0; |
|
|
|
|
|
|
|
#endif |
|
|
|
//=============================================================
|
|
|
|
//=============================================================
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#endif |