commit
27e2fcc416
@ -0,0 +1,114 @@ |
||||
#define OLC_PGE_APPLICATION |
||||
#include "olcPixelGameEngine.h" |
||||
|
||||
// Override base class with your custom functionality
|
||||
class Example : public olc::PixelGameEngine |
||||
{ |
||||
public: |
||||
Example() |
||||
{ |
||||
// Name your application
|
||||
sAppName = "Console Example"; |
||||
} |
||||
|
||||
public: |
||||
|
||||
std::vector<std::string>console; |
||||
const float BLINK_TIME=1.f; |
||||
float cursorTime=0.0f; |
||||
bool cursorOn=false; |
||||
|
||||
olc::Pixel color = olc::DARK_BLUE; |
||||
|
||||
bool OnUserCreate() override |
||||
{ |
||||
// Called once at the start, so create things here
|
||||
return true; |
||||
} |
||||
|
||||
void OnTextEntryComplete(const std::string& sText) override |
||||
{ |
||||
console.clear(); |
||||
console.push_back("You typed:"); |
||||
console.push_back(" "+sText); |
||||
console.push_back("==================="); |
||||
console.push_back("This is the part where I tell you that something might happen. Or it might not..."); |
||||
console.push_back("If you press F1 something interesting might happen."); |
||||
} |
||||
|
||||
std::string WrapText(std::string str,int width,bool proportional=false,olc::vf2d scale={1,1}) { |
||||
std::string newStr; |
||||
while (true) { |
||||
std::string word; |
||||
if (str.find(" ")==std::string::npos) { |
||||
word=str; |
||||
} else { |
||||
word = str.substr(0,str.find(" ")); |
||||
} |
||||
olc::vi2d newSize = (proportional?GetTextSizeProp(newStr+(newStr.size()>0?" ":"")+word):GetTextSize(newStr+(newStr.size()>0?" ":"")+word))*scale; |
||||
if (newSize.x>width) { |
||||
newStr+="\n"+word; |
||||
} else { |
||||
newStr+=(newStr.size()>0?" ":"")+word; |
||||
} |
||||
if (str.find(" ")==std::string::npos) { |
||||
break; |
||||
} else { |
||||
str.erase(0,str.find(" ")+1); |
||||
} |
||||
} |
||||
return newStr; |
||||
} |
||||
|
||||
bool OnUserUpdate(float fElapsedTime) override |
||||
{ |
||||
if (!IsTextEntryEnabled()){ |
||||
TextEntryEnable(true,""); |
||||
} |
||||
|
||||
cursorTime+=fElapsedTime; |
||||
if (cursorTime > BLINK_TIME){ |
||||
cursorTime-=BLINK_TIME; |
||||
cursorOn=!cursorOn; |
||||
} |
||||
|
||||
if (GetKey(olc::F1).bPressed){ |
||||
console.clear(); |
||||
console.push_back("Nice!"); |
||||
color=olc::DARK_GREEN; |
||||
} |
||||
|
||||
Clear(color); |
||||
DrawRect(0,0,ScreenWidth()-1, ScreenHeight()-1, olc::YELLOW); |
||||
|
||||
std::string mouseText="Cursor Position: "+GetMousePos().str(); |
||||
FillRectDecal({1,1},{(float)ScreenWidth()-2,20},olc::BLACK); |
||||
|
||||
int drawPosY=ScreenHeight()-32; |
||||
|
||||
DrawStringDecal({8,4},mouseText,olc::WHITE); |
||||
|
||||
for (int i=console.size()-1;i>=0;i--){ |
||||
if (drawPosY>=24){ |
||||
std::string wrappedVersion=WrapText(console[i],ScreenWidth()-16); |
||||
DrawStringDecal({8,(float)drawPosY - GetTextSize(wrappedVersion).y},wrappedVersion); |
||||
drawPosY-=GetTextSize(wrappedVersion).y+12; |
||||
} |
||||
drawPosY-=12; |
||||
} |
||||
|
||||
std::string displayText = "> " + TextEntryGetString() + (cursorOn?"|":" "); |
||||
|
||||
DrawStringDecal({4,(float)ScreenHeight()-12},displayText); |
||||
|
||||
return true; |
||||
} |
||||
}; |
||||
|
||||
int main() |
||||
{ |
||||
Example demo; |
||||
if (demo.Construct(256, 240, 2, 2)) |
||||
demo.Start(); |
||||
return 0; |
||||
} |
Loading…
Reference in new issue