Added Text Rendering

pull/113/head
Javidx9 7 years ago committed by GitHub
parent 6ae3d4f88f
commit 964f596c8e
  1. 86
      olcPixelGameEngine.h

@ -2,7 +2,7 @@
olcPixelGameEngine.h olcPixelGameEngine.h
+-------------------------------------------------------------+ +-------------------------------------------------------------+
| OneLoneCoder Pixel Game Engine v0.4 | | OneLoneCoder Pixel Game Engine v0.5 |
| "Like the command prompt console one, but not..." - javidx9 | | "Like the command prompt console one, but not..." - javidx9 |
+-------------------------------------------------------------+ +-------------------------------------------------------------+
@ -118,7 +118,7 @@
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 & MagetzUb for advice, ideas and testing, KrossX, Huhlig, Dragoneye, Appa & MagetzUb for advice, ideas and testing,
and I'd like to extend my appreciation to the 13K YouTube followers 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 and 1K Discord server members who give me the motivation to keep
going with all this :D going with all this :D
@ -341,6 +341,8 @@ namespace olc // All OneLoneCoder stuff will now exist in the "olc" namespace
// 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);
// Draws a single line of text
void DrawString(int32_t x, int32_t y, std::string sText);
public: // Branding public: // Branding
std::string sAppName; std::string sAppName;
@ -356,6 +358,9 @@ namespace olc // All OneLoneCoder stuff will now exist in the "olc" namespace
uint32_t nMousePosX = 0; uint32_t nMousePosX = 0;
uint32_t nMousePosY = 0; uint32_t nMousePosY = 0;
bool bHasInputFocus = false; bool bHasInputFocus = false;
float fFrameTimer = 1.0f;
int nFrameCount = 0;
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 };
@ -384,6 +389,7 @@ namespace olc // All OneLoneCoder stuff will now exist in the "olc" namespace
// Common initialisation functions // Common initialisation functions
void olc_UpdateMouse(uint32_t x, uint32_t y); void olc_UpdateMouse(uint32_t x, uint32_t y);
bool olc_OpenGLCreate(); bool olc_OpenGLCreate();
void olc_ConstructFontSheet();
#ifdef _WIN32 #ifdef _WIN32
// Windows specific window handling // Windows specific window handling
@ -624,6 +630,9 @@ namespace olc
wsAppName = converter.from_bytes(sAppName); wsAppName = converter.from_bytes(sAppName);
#endif #endif
#endif #endif
// Load the default font sheet
olc_ConstructFontSheet();
// Create a sprite that represents the primary drawing target // Create a sprite that represents the primary drawing target
pDefaultDrawTarget = new Sprite(nScreenWidth, nScreenHeight); pDefaultDrawTarget = new Sprite(nScreenWidth, nScreenHeight);
SetDrawTarget(nullptr); SetDrawTarget(nullptr);
@ -1068,6 +1077,18 @@ namespace olc
} }
} }
void PixelGameEngine::DrawString(int32_t x, int32_t y, std::string sText)
{
int32_t s = 0;
for (auto c : sText)
{
int32_t ox = (c - 32) % 16;
int32_t oy = (c - 32) / 16;
DrawPartialSprite(x + s, y, fontSprite, ox*8, oy*8, 8, 8);
s+=8;
}
}
void PixelGameEngine::SetPixelMode(Pixel::Mode m) void PixelGameEngine::SetPixelMode(Pixel::Mode m)
{ {
nPixelMode = m; nPixelMode = m;
@ -1248,21 +1269,32 @@ namespace olc
#endif #endif
// Update Title Bar // Update Title Bar
fFrameTimer += fElapsedTime;
nFrameCount++;
if (fFrameTimer >= 1.0f)
{
fFrameTimer -= 1.0f;
#ifdef _WIN32 #ifdef _WIN32
#ifdef UNICODE #ifdef UNICODE
wchar_t sTitle[256]; wchar_t sTitle[256];
swprintf(sTitle, 256, L"OneLoneCoder.com - Pixel Game Engine - %s - FPS: %3.2f", wsAppName.c_str(), 1.0f / fElapsedTime); swprintf(sTitle, 256, L"OneLoneCoder.com - Pixel Game Engine - %s - FPS: %d", wsAppName.c_str(), nFrameCount);
SetWindowText(olc_hWnd, sTitle);
#else #else
#ifndef __MINGW32__
char sTitle[256]; char sTitle[256];
sprintf_s(sTitle, 256, "OneLoneCoder.com - Pixel Game Engine - %s - FPS: %3.2f", sAppName.c_str(), 1.0f / fElapsedTime); sprintf_s(sTitle, 256, "OneLoneCoder.com - Pixel Game Engine - %s - FPS: %d", sAppName.c_str(), nFrameCount);
SetWindowText(olc_hWnd, sTitle); #else
char sTitle[256];
sprintf(sTitle, "OneLoneCoder.com - Pixel Game Engine - %s - FPS: %d", sAppName.c_str(), nFrameCount);
#endif
#endif #endif
SetWindowText(olc_hWnd, sTitle);
#else #else
char sTitle[256]; char sTitle[256];
sprintf(sTitle, "OneLoneCoder.com - Pixel Game Engine - %s - FPS: %3.2f", sAppName.c_str(), 1.0f / fElapsedTime); sprintf(sTitle, "OneLoneCoder.com - Pixel Game Engine - %s - FPS: %d", sAppName.c_str(), nFrameCount);
XStoreName(olc_Display, olc_Window, sTitle); XStoreName(olc_Display, olc_Window, sTitle);
#endif #endif
nFrameCount = 0;
}
} }
@ -1291,6 +1323,46 @@ namespace olc
} }
void PixelGameEngine::olc_ConstructFontSheet()
{
std::string data;
data += "?Q`0001oOch0o01o@F40o0<AGD4090LAGD<090@A7ch0?00O7Q`0600>00000000";
data += "O000000nOT0063Qo4d8>?7a14Gno94AA4gno94AaOT0>o3`oO400o7QN00000400";
data += "Of80001oOg<7O7moBGT7O7lABET024@aBEd714AiOdl717a_=TH013Q>00000000";
data += "720D000V?V5oB3Q_HdUoE7a9@DdDE4A9@DmoE4A;Hg]oM4Aj8S4D84@`00000000";
data += "OaPT1000Oa`^13P1@AI[?g`1@A=[OdAoHgljA4Ao?WlBA7l1710007l100000000";
data += "ObM6000oOfMV?3QoBDD`O7a0BDDH@5A0BDD<@5A0BGeVO5ao@CQR?5Po00000000";
data += "Oc``000?Ogij70PO2D]??0Ph2DUM@7i`2DTg@7lh2GUj?0TO0C1870T?00000000";
data += "70<4001o?P<7?1QoHg43O;`h@GT0@:@LB@d0>:@hN@L0@?aoN@<0O7ao0000?000";
data += "OcH0001SOglLA7mg24TnK7ln24US>0PL24U140PnOgl0>7QgOcH0K71S0000A000";
data += "00H00000@Dm1S007@DUSg00?OdTnH7YhOfTL<7Yh@Cl0700?@Ah0300700000000";
data += "<008001QL00ZA41a@6HnI<1i@FHLM81M@@0LG81?O`0nC?Y7?`0ZA7Y300080000";
data += "O`082000Oh0827mo6>Hn?Wmo?6HnMb11MP08@C11H`08@FP0@@0004@000000000";
data += "00P00001Oab00003OcKP0006@6=PMgl<@440MglH@000000`@000001P00000000";
data += "Ob@8@@00Ob@8@Ga13R@8Mga172@8?PAo3R@827QoOb@820@0O`0007`0000007P0";
data += "O`000P08Od400g`<3V=P0G`673IP0`@3>1`00P@6O`P00g`<O`000GP800000000";
data += "?P9PL020O`<`N3R0@E4HC7b0@ET<ATB0@@l6C4B0O`H3N7b0?P01L3R000000020";
fontSprite = new olc::Sprite(128, 48);
int px = 0, py = 0;
for (int b = 0; b < 1024; b += 4)
{
uint32_t sym1 = (uint32_t)data[b + 0] - 48;
uint32_t sym2 = (uint32_t)data[b + 1] - 48;
uint32_t sym3 = (uint32_t)data[b + 2] - 48;
uint32_t sym4 = (uint32_t)data[b + 3] - 48;
uint32_t r = sym1 << 18 | sym2 << 12 | sym3 << 6 | sym4;
for (int i = 0; i < 24; i++)
{
int k = r & (1 << i) ? 255 : 0;
fontSprite->SetPixel(px, py, olc::Pixel(k, k, k));
if (++py == 48) { px++; py = 0; }
}
}
}
#ifdef _WIN32 #ifdef _WIN32
HWND PixelGameEngine::olc_WindowCreate() HWND PixelGameEngine::olc_WindowCreate()
{ {

Loading…
Cancel
Save