Some messing around with the emscripten file API

Co-authored-by: sigonasr2 <sigonasr2@gmail.com>
master
sigonasr2 2 years ago
parent 9ad2eba8eb
commit 9f5acef9cf
  1. 4
      C++/scripts/web.sh
  2. BIN
      C++ProjectTemplate
  3. BIN
      C++ProjectTemplate.data
  4. 2
      C++ProjectTemplate.js
  5. BIN
      C++ProjectTemplate.wasm
  6. 37
      main.cpp
  7. 36
      pixelGameEngine.h

@ -1,9 +1,9 @@
#Compiles emscripten instance of this project for the web.
#C++
if [ -d "assets" ]; then
em++ -std=c++17 -O2 -s ALLOW_MEMORY_GROWTH=1 -s MAX_WEBGL_VERSION=2 -s MIN_WEBGL_VERSION=2 -s USE_SDL_MIXER=2 -s USE_LIBPNG=1 $(find . -type f -name "*.cpp") -o ${PROJECT_NAME}.html -I pixelGameEngine.h --preload-file ./assets
em++ -lidbfs.js -std=c++17 -O2 -s ALLOW_MEMORY_GROWTH=1 -s MAX_WEBGL_VERSION=2 -s MIN_WEBGL_VERSION=2 -s USE_SDL_MIXER=2 -s USE_LIBPNG=1 $(find . -type f -name "*.cpp") -o ${PROJECT_NAME}.html -I pixelGameEngine.h --preload-file ./assets
else
em++ -std=c++17 -O2 -s ALLOW_MEMORY_GROWTH=1 -s MAX_WEBGL_VERSION=2 -s MIN_WEBGL_VERSION=2 -s USE_SDL_MIXER=2 -s USE_LIBPNG=1 $(find . -type f -name "*.cpp") -o ${PROJECT_NAME}.html -I pixelGameEngine.h
em++ -lidbfs.js -std=c++17 -O2 -s ALLOW_MEMORY_GROWTH=1 -s MAX_WEBGL_VERSION=2 -s MIN_WEBGL_VERSION=2 -s USE_SDL_MIXER=2 -s USE_LIBPNG=1 $(find . -type f -name "*.cpp") -o ${PROJECT_NAME}.html -I pixelGameEngine.h
fi
cp buildtemplate.html ${PROJECT_NAME}.html

Binary file not shown.

Binary file not shown.

File diff suppressed because one or more lines are too long

Binary file not shown.

@ -14,7 +14,7 @@ public:
}
public:
int frameCount=0;
float accumulatedTime=0;
const float UPDATE_RATE=1/60.0F;
char partyMemberCount=1;
@ -45,6 +45,7 @@ public:
se1.LoadAudioWaveform("./assets/sample-9s.wav");
player_hp[0]=30;
//WriteEmscriptenFile("test","Test?\nThis has more\nthan two lines!");
return true;
}
@ -71,6 +72,16 @@ public:
}
void updateGame(){
frameCount++;
if (frameCount==180) {
std::vector<std::string> data = ReadEmscriptenFile("test");
for (int i=0;i<data.size();i++) {
cout << data[i] << "\n";
}
for (int i=0;i<data[0].length();i++) {
player_name[0][i]=data[0][i];
}
}
for (int i=0;i<partyMemberCount;i++) {
if (player_rollhp[i]>player_hp[i]) {
if (player_rollhp_counter[i][0]<=0) {
@ -109,7 +120,7 @@ public:
}
void drawBox(const olc::vi2d &pos, const olc::vi2d &size, olc::Pixel p = olc::WHITE, olc::Pixel p2 = olc::DARK_GREY, olc::Pixel p3 = olc::VERY_DARK_GREY) {
FillRectDecal({pos.x+3,pos.y+3},{size.x-5,size.y-5},p);
FillRectDecal({(float)(pos.x+3),(float)(pos.y+3)},{(float)(size.x-5),(float)(size.y-5)},p);
DrawRect({pos.x,pos.y},size,p2);
DrawRect({pos.x+2,pos.y+2},{size.x-4,size.y-4},p3);
DrawRect({pos.x+1,pos.y+1},{size.x-2,size.y-2},p);
@ -124,20 +135,20 @@ public:
for (int x=3;x<size.x;x+=checkerboardSize.x*2) {
for (int y=3;y<size.y;y+=checkerboardSize.y*2) {
if (x+checkerboardSize.x<size.x&&y+checkerboardSize.y<size.y) {
FillRectDecal({x+pos.x,y+pos.y},{checkerboardSize.x,checkerboardSize.y},alternatingCol);
FillRectDecal({(float)(x+pos.x),(float)(y+pos.y)},{(float)checkerboardSize.x,(float)checkerboardSize.y},alternatingCol);
}
}
}
}
void DrawRollingCounter(const olc::vi2d &pos,int val,int*rollcounter,int*rolloffset) {
FillRectDecal({pos.x+20,pos.y-3},{30,13},olc::DARK_GREY);
FillRectDecal({pos.x+21,pos.y-2},{28,11},olc::GREY);
DrawLineDecal({pos.x+30,pos.y-3},{pos.x+30,pos.y+10},olc::DARK_GREY);
DrawLineDecal({pos.x+40,pos.y-3},{pos.x+40,pos.y+10},olc::DARK_GREY);
DrawPartialDecal({pos.x+22,pos.y-1},{7,9},rollingCounter,{0,rollcounter[2]*13+rolloffset[2]},{7,9});
DrawPartialDecal({pos.x+31,pos.y-1},{7,9},rollingCounter,{0,rollcounter[1]*13+rolloffset[1]},{7,9});
DrawPartialDecal({pos.x+41,pos.y-1},{7,9},rollingCounter,{0,rollcounter[0]*13+rolloffset[0]},{7,9});
FillRectDecal({(float)(pos.x+20),(float)(pos.y-3)},{30,13},olc::DARK_GREY);
FillRectDecal({(float)(pos.x+21),(float)(pos.y-2)},{28,11},olc::GREY);
DrawLineDecal({(float)(pos.x+30),(float)(pos.y-3)},{(float)(pos.x+30),(float)(pos.y+10)},olc::DARK_GREY);
DrawLineDecal({(float)(pos.x+40),(float)(pos.y-3)},{(float)(pos.x+40),(float)(pos.y+10)},olc::DARK_GREY);
DrawPartialDecal({(float)(pos.x+22),(float)(pos.y-1)},{7,9},rollingCounter,{0,(float)(rollcounter[2]*13+rolloffset[2])},{7,9});
DrawPartialDecal({(float)(pos.x+31),(float)(pos.y-1)},{7,9},rollingCounter,{0,(float)(rollcounter[1]*13+rolloffset[1])},{7,9});
DrawPartialDecal({(float)(pos.x+41),(float)(pos.y-1)},{7,9},rollingCounter,{0,(float)(rollcounter[0]*13+rolloffset[0])},{7,9});
}
void drawGame(){
@ -145,12 +156,12 @@ public:
for (int i=0;i<partyMemberCount;i++) {
const olc::vi2d box = {(128-32*partyMemberCount)+i*64,160};
drawCheckerboardBox(box,{59,59},olc::Pixel(180,159,194),olc::Pixel(200,179,214),{6,6});
DrawStringDecal({box.x+6,box.y+6},player_name[i],olc::BLACK);
DrawStringDecal({(float)(box.x+6),(float)(box.y+6)},player_name[i],olc::BLACK);
const olc::vi2d hpTextPos = {box.x+5,box.y+25};
for (int x=-1;x<=1;x++) {
for (int y=-1;y<=1;y++) {
if (x!=0&&y!=0) {
DrawStringDecal({hpTextPos.x+x,hpTextPos.y+y},"HP",olc::WHITE);
DrawStringDecal({(float)(hpTextPos.x+x),(float)(hpTextPos.y+y)},"HP",olc::WHITE);
}
}
}
@ -160,7 +171,7 @@ public:
for (int x=-1;x<=1;x++) {
for (int y=-1;y<=1;y++) {
if (x!=0&&y!=0) {
DrawStringDecal({mpTextPos.x+x,mpTextPos.y+y},"PP",olc::WHITE);
DrawStringDecal({(float)(mpTextPos.x+x),(float)(mpTextPos.y+y)},"PP",olc::WHITE);
}
}
}

@ -951,6 +951,8 @@ namespace olc
const olc::vi2d& GetWindowMouse() const;
// Gets the mouse as a vector to keep Tarriest happy
const olc::vi2d& GetMousePos() const;
const void WriteEmscriptenFile(std::string filepath,std::string data);
const std::vector<std::string> ReadEmscriptenFile(std::string filepath);
static const std::map<size_t, uint8_t>& GetKeyMap() { return mapKeys; }
@ -1983,6 +1985,12 @@ namespace olc
return false;
}
#ifndef OLC_PLATFORM_EMSCRIPTEN
const void PixelGameEngine::WriteEmscriptenFile(std::string filepath,std::string data) {
}
const std::vector<std::string> PixelGameEngine::ReadEmscriptenFile(std::string filepath,std::string data) {
}
#endif
void PixelGameEngine::DrawLine(const olc::vi2d& pos1, const olc::vi2d& pos2, Pixel p, uint32_t pattern)
{ DrawLine(pos1.x, pos1.y, pos2.x, pos2.y, p, pattern); }
@ -4275,6 +4283,13 @@ namespace olc
//eglSwapInterval is currently a NOP, plement anyways in case it becomes supported
locSwapInterval = &eglSwapInterval;
locSwapInterval(olc_Display, bVSYNC ? 1 : 0);
EM_ASM(
FS.mkdir('/working1');
FS.mount(IDBFS, {}, '/working1');
FS.syncfs(true, function (err) {
assert(!err);});
);
#endif
#if defined(OLC_PLATFORM_GLUT)
@ -5676,6 +5691,27 @@ extern "C"
namespace olc
{
const void PixelGameEngine::WriteEmscriptenFile(std::string filepath,std::string data) {
std::ofstream file;
file.open("/working1/"+filepath);
file<<data;
file.close();
EM_ASM(
FS.syncfs(function(err){})
);
}
const std::vector<std::string> PixelGameEngine::ReadEmscriptenFile(std::string filepath) {
std::string line;
std::ifstream filer("/working1/"+filepath);
std::vector<std::string> data;
if (filer.is_open()) {
while (getline(filer,line)) {
data.push_back(line);
}
filer.close();
}
return data;
}
class Platform_Emscripten : public olc::Platform
{
public:

Loading…
Cancel
Save