Added Shadow Casting Video
This commit is contained in:
parent
ab2e764e2c
commit
27474c4ced
BIN
OneLoneCoder_PGE_ShadowCasting2D.cpp
Normal file
BIN
OneLoneCoder_PGE_ShadowCasting2D.cpp
Normal file
Binary file not shown.
BIN
light_cast.png
Normal file
BIN
light_cast.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 130 KiB |
@ -2,7 +2,7 @@
|
||||
olcPixelGameEngine.h
|
||||
|
||||
+-------------------------------------------------------------+
|
||||
| OneLoneCoder Pixel Game Engine v1.3 |
|
||||
| OneLoneCoder Pixel Game Engine v1.6 |
|
||||
| "Like the command prompt console one, but not..." - javidx9 |
|
||||
+-------------------------------------------------------------+
|
||||
|
||||
@ -118,17 +118,60 @@
|
||||
Thanks
|
||||
~~~~~~
|
||||
I'd like to extend thanks to Eremiell, slavka, Phantim, JackOJC,
|
||||
KrossX, Huhlig, Dragoneye, Appa & MagetzUb for advice, ideas and testing,
|
||||
and I'd like to extend my appreciation to the 13K YouTube followers
|
||||
and 1K Discord server members who give me the motivation to keep
|
||||
going with all this :D
|
||||
KrossX, Huhlig, Dragoneye, Appa, JustinRichardsMusic, SliceNDice
|
||||
& MagetzUb for advice, ideas and testing, and I'd like to extend
|
||||
my appreciation to the 14K YouTube followers and 1K Discord server
|
||||
members who give me the motivation to keep going with all this :D
|
||||
|
||||
Author
|
||||
~~~~~~
|
||||
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
|
||||
// Link to libraries
|
||||
@ -183,8 +226,6 @@
|
||||
#undef min
|
||||
#undef max
|
||||
|
||||
|
||||
|
||||
namespace olc // All OneLoneCoder stuff will now exist in the "olc" namespace
|
||||
{
|
||||
struct Pixel
|
||||
@ -200,7 +241,7 @@ namespace olc // All OneLoneCoder stuff will now exist in the "olc" namespace
|
||||
|
||||
Pixel();
|
||||
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
|
||||
@ -260,6 +301,11 @@ namespace olc // All OneLoneCoder stuff will now exist in the "olc" namespace
|
||||
private:
|
||||
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)
|
||||
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)
|
||||
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
|
||||
// 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
|
||||
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
|
||||
@ -382,6 +428,7 @@ namespace olc // All OneLoneCoder stuff will now exist in the "olc" namespace
|
||||
float fFramePeriod = 0.0f;
|
||||
Sprite *fontSprite = nullptr;
|
||||
|
||||
|
||||
static std::map<uint16_t, uint8_t> mapKeys;
|
||||
bool pKeyNewState[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
|
||||
|
||||
*/
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
// 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
|
||||
#ifdef OLC_PGE_APPLICATION
|
||||
#undef OLC_PGE_APPLICATION
|
||||
|
||||
namespace olc
|
||||
{
|
||||
@ -630,6 +684,11 @@ namespace olc
|
||||
|
||||
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)
|
||||
pColData[y*width + x] = p;
|
||||
}
|
||||
@ -785,6 +844,7 @@ namespace olc
|
||||
{
|
||||
if (!pDrawTarget) return;
|
||||
|
||||
|
||||
if (nPixelMode == Pixel::NORMAL)
|
||||
{
|
||||
pDrawTarget->SetPixel(x, y, p);
|
||||
@ -939,6 +999,9 @@ namespace olc
|
||||
Pixel* m = GetDrawTarget()->GetData();
|
||||
for (int i = 0; i < pixels; i++)
|
||||
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)
|
||||
@ -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)
|
||||
return;
|
||||
|
||||
|
||||
for (int i = 0; i < sprite->width; i++)
|
||||
if (scale > 1)
|
||||
{
|
||||
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++)
|
||||
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)
|
||||
return;
|
||||
|
||||
for (int i = 0; i < w; i++)
|
||||
if (scale > 1)
|
||||
{
|
||||
for (int j = 0; j < h; j++)
|
||||
{
|
||||
Draw(x + i, y + j, sprite->GetPixel(i+ox, j+oy));
|
||||
for (int32_t i = 0; i < w; i++)
|
||||
for (int32_t j = 0; j < h; 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 + 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];
|
||||
}
|
||||
|
||||
#ifdef OLC_DBG_OVERDRAW
|
||||
olc::Sprite::nOverdrawCount = 0;
|
||||
#endif
|
||||
|
||||
// Handle Frame Update
|
||||
if (!OnUserUpdate(fElapsedTime))
|
||||
bAtomActive = false;
|
||||
@ -1657,6 +1737,10 @@ namespace olc
|
||||
std::atomic<bool> PixelGameEngine::bAtomActive{ false };
|
||||
std::map<uint16_t, uint8_t> PixelGameEngine::mapKeys;
|
||||
olc::PixelGameEngine* olc::PGEX::pge = nullptr;
|
||||
#ifdef OLC_DBG_OVERDRAW
|
||||
int olc::Sprite::nOverdrawCount = 0;
|
||||
#endif
|
||||
//=============================================================
|
||||
}
|
||||
|
||||
#endif
|
Loading…
x
Reference in New Issue
Block a user