Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[WIP] Emscripten support #8

Closed
wants to merge 8 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions compile-shaders-emscripten.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#!/bin/bash

# compile shaders

# simple shader
./third-party/build/bin/shaderc \
-f shader/v_simple.sc -o shader/v_simple.bin \
--platform asmjs --type vertex --verbose -i ./

./third-party/build/bin/shaderc \
-f shader/f_simple.sc -o shader/f_simple.bin \
--platform asmjs --type fragment --verbose -i ./
11 changes: 11 additions & 0 deletions configure-make.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#!/bin/bash
decahedron1 marked this conversation as resolved.
Show resolved Hide resolved

# Commands to configure this repo for building after dependencies have been installed

cmake -S . -B build/debug -G "Unix Makefiles" -DCMAKE_BUILD_TYPE=Debug \
-DCMAKE_PREFIX_PATH="$(pwd)/third-party/build" \
-DCMAKE_EXPORT_COMPILE_COMMANDS=ON

cmake -S . -B build/release -G "Unix Makefiles" -DCMAKE_BUILD_TYPE=Release \
-DCMAKE_PREFIX_PATH="$(pwd)/third-party/build" \
-DCMAKE_EXPORT_COMPILE_COMMANDS=ON
18 changes: 18 additions & 0 deletions file-ops.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,11 @@
#include <fstream>
#include <iostream>

#include "bgfx/platform.h"
#if BX_PLATFORM_EMSCRIPTEN
#include <emscripten.h>
#endif
decahedron1 marked this conversation as resolved.
Show resolved Hide resolved

// stream string operations derived from:
// Optimized C++ by Kurt Guntheroth (O’Reilly).
// Copyright 2016 Kurt Guntheroth, 978-1-491-92206-4
Expand Down Expand Up @@ -37,6 +42,7 @@ inline bool stream_read_string(std::istream& file, std::string& fileContents)

inline bool read_file(const std::string& filename, std::string& fileContents)
{
#if !BX_PLATFORM_EMSCRIPTEN
std::ifstream file(filename, std::ios::binary);

if (!file.is_open()) {
Expand All @@ -48,6 +54,18 @@ inline bool read_file(const std::string& filename, std::string& fileContents)
file.close();

return success;
#else
emscripten_fetch_attr_t attr;
emscripten_fetch_attr_init(&attr);
strcpy(attr.requestMethod, "GET");
attr.attributes = EMSCRIPTEN_FETCH_ATTR_LOAD_TO_MEMORY | EMSCRIPTEN_FETCH_SYNCHRONOUS;
emscripten_fetch_t *fetch = emscripten_fetch(&attr, filename.c_str());
if (fetch->status == 200) {
*fileContents = fetch->data;
return true;
} else
return false;
decahedron1 marked this conversation as resolved.
Show resolved Hide resolved
#endif
}

} // namespace fileops
4 changes: 3 additions & 1 deletion main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,8 @@ int main(int argc, char** argv)
#elif BX_PLATFORM_LINUX
pd.ndt = wmi.info.x11.display;
pd.nwh = (void*)(uintptr_t)wmi.info.x11.window;
#elif BX_PLATFORM_EMSCRIPTEN
pd.nwh = (void*)"#canvas";
#endif // BX_PLATFORM_WINDOWS ? BX_PLATFORM_OSX ? BX_PLATFORM_LINUX
decahedron1 marked this conversation as resolved.
Show resolved Hide resolved

bgfx::Init bgfx_init;
Expand All @@ -96,7 +98,7 @@ int main(int argc, char** argv)
ImGui_ImplSDL2_InitForD3D(window);
#elif BX_PLATFORM_OSX
ImGui_ImplSDL2_InitForMetal(window);
#elif BX_PLATFORM_LINUX
#elif BX_PLATFORM_LINUX || BX_PLATFORM_EMSCRIPTEN
ImGui_ImplSDL2_InitForOpenGL(window, nullptr);
#endif // BX_PLATFORM_WINDOWS ? BX_PLATFORM_OSX ? BX_PLATFORM_LINUX
pr0g marked this conversation as resolved.
Show resolved Hide resolved

Expand Down