Skip to content

Commit 3be6919

Browse files
committed
Emscripten
1 parent 2e67865 commit 3be6919

File tree

13 files changed

+351
-95
lines changed

13 files changed

+351
-95
lines changed

.gitignore

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,8 @@
1919
*.app
2020
ImStudio
2121
out
22-
build
2322
imgui.ini
24-
pu.sh
25-
ge.sh
23+
build/
24+
web/
2625
.vscode/
2726
cmake/wixpatch.xml

CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ set(CMAKE_CXX_STANDARD_REQUIRED ON)
44

55
SET(PROJECT_VERSION_MAJOR 1)
66
SET(PROJECT_VERSION_MINOR 1)
7-
SET(PROJECT_VERSION_PATCH 1)
7+
SET(PROJECT_VERSION_PATCH 2)
88

99
project(ImStudio C CXX)
1010

Makefile.emscripten

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
# EMSCRIPTEN MAKEFILE
2+
# Emscripten environment should be loaded
3+
4+
CC = emcc
5+
CXX = em++
6+
WEB_DIR = web
7+
EXE = $(WEB_DIR)/index.html
8+
9+
SRC_DIR = src
10+
IMGUI_DIR = $(SRC_DIR)/third-party/imgui
11+
FMT_DIR = $(SRC_DIR)/third-party/fmt
12+
13+
##---------------------------------------------------------------------
14+
## SOURCE FILES
15+
##---------------------------------------------------------------------
16+
17+
SOURCES = $(IMGUI_DIR)/imgui.cpp $(IMGUI_DIR)/imgui_demo.cpp $(IMGUI_DIR)/imgui_draw.cpp $(IMGUI_DIR)/imgui_tables.cpp $(IMGUI_DIR)/imgui_widgets.cpp
18+
SOURCES += $(IMGUI_DIR)/backends/imgui_impl_sdl.cpp $(IMGUI_DIR)/backends/imgui_impl_opengl3.cpp
19+
SOURCES += $(IMGUI_DIR)/misc/cpp/imgui_stdlib.cpp
20+
21+
SOURCES += $(FMT_DIR)/src/format.cc
22+
23+
SOURCES += $(SRC_DIR)/em.cpp
24+
SOURCES += $(wildcard $(SRC_DIR)/utils/*.cpp)
25+
SOURCES += $(wildcard $(SRC_DIR)/sources/*.cpp)
26+
27+
##---------------------------------------------------------------------
28+
29+
OBJS = $(addsuffix .o, $(basename $(notdir $(SOURCES))))
30+
UNAME_S := $(shell uname -s)
31+
CPPFLAGS =
32+
LDFLAGS =
33+
EMS =
34+
35+
##---------------------------------------------------------------------
36+
## EMSCRIPTEN OPTIONS
37+
##---------------------------------------------------------------------
38+
39+
EMS += -s USE_SDL=2
40+
EMS += -s DISABLE_EXCEPTION_CATCHING=1
41+
LDFLAGS += -s WASM=1 -s ALLOW_MEMORY_GROWTH=1 -s NO_EXIT_RUNTIME=0 -s ASSERTIONS=1
42+
43+
LDFLAGS += -s NO_FILESYSTEM=1
44+
CPPFLAGS += -DIMGUI_DISABLE_FILE_FUNCTIONS
45+
46+
##---------------------------------------------------------------------
47+
## FINAL BUILD FLAGS
48+
##---------------------------------------------------------------------
49+
50+
CPPFLAGS += -I$(IMGUI_DIR) -I$(IMGUI_DIR)/backends -I$(IMGUI_DIR)/misc/cpp -I$(FMT_DIR)/include -I$(SRC_DIR)/sources
51+
CPPFLAGS += -Wall -Wformat -Os $(EMS)
52+
LDFLAGS += --shell-file shell.html $(EMS)
53+
54+
##---------------------------------------------------------------------
55+
## BUILD RULES
56+
##---------------------------------------------------------------------
57+
58+
%.o:$(IMGUI_DIR)/%.cpp
59+
$(CXX) $(CPPFLAGS) $(CXXFLAGS) -c -o $@ $<
60+
61+
%.o:$(IMGUI_DIR)/backends/%.cpp
62+
$(CXX) $(CPPFLAGS) $(CXXFLAGS) -c -o $@ $<
63+
64+
%.o:$(IMGUI_DIR)/misc/cpp/%.cpp
65+
$(CXX) $(CPPFLAGS) $(CXXFLAGS) -c -o $@ $<
66+
67+
%.o:$(FMT_DIR)/src/%.cc
68+
$(CXX) $(CPPFLAGS) $(CXXFLAGS) -c -o $@ $<
69+
70+
%.o:$(SRC_DIR)/%.cpp
71+
$(CXX) $(CPPFLAGS) $(CXXFLAGS) -c -o $@ $<
72+
73+
%.o:$(SRC_DIR)/utils/%.cpp
74+
$(CXX) $(CPPFLAGS) $(CXXFLAGS) -c -o $@ $<
75+
76+
%.o:$(SRC_DIR)/sources/%.cpp
77+
$(CXX) $(CPPFLAGS) $(CXXFLAGS) -c -o $@ $<
78+
79+
all: $(EXE)
80+
@echo Build complete for $(EXE)
81+
82+
$(WEB_DIR):
83+
mkdir $@
84+
85+
serve: all
86+
python3 -m http.server -d $(WEB_DIR)
87+
88+
$(EXE): $(OBJS) $(WEB_DIR)
89+
$(CXX) -o $@ $(OBJS) $(LDFLAGS)
90+
rm -rf $(OBJS)
91+
92+
clean:
93+
rm -rf $(OBJS) $(WEB_DIR)

_Makefile renamed to Makefile.old

File renamed without changes.

cmake/third-party.cmake

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,8 @@ add_dependencies(imgui glfw)
1818

1919
list(APPEND IMGUI_INCLUDE_DIRS
2020
${CMAKE_SOURCE_DIR}/src/third-party/imgui
21-
${CMAKE_SOURCE_DIR}/src/third-party/imgui/backends)
21+
${CMAKE_SOURCE_DIR}/src/third-party/imgui/backends
22+
${CMAKE_SOURCE_DIR}/src/third-party/imgui/misc/cpp)
2223
list(APPEND IMGUI_LIBRARIES imgui)
2324

2425
target_include_directories(imgui PRIVATE SYSTEM ${IMGUI_INCLUDE_DIRS})

shell.html

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
<!doctype html>
2+
<html lang="en-us">
3+
<head>
4+
<meta charset="utf-8">
5+
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, minimum-scale=1, user-scalable=no"/>
6+
<title>ImStudio</title>
7+
<style>
8+
body { margin: 0; background-color: black }
9+
.emscripten {
10+
position: absolute;
11+
top: 0px;
12+
left: 0px;
13+
margin: 0px;
14+
border: 0;
15+
width: 100%;
16+
height: 100%;
17+
overflow: hidden;
18+
display: block;
19+
image-rendering: optimizeSpeed;
20+
image-rendering: -moz-crisp-edges;
21+
image-rendering: -o-crisp-edges;
22+
image-rendering: -webkit-optimize-contrast;
23+
image-rendering: optimize-contrast;
24+
image-rendering: crisp-edges;
25+
image-rendering: pixelated;
26+
-ms-interpolation-mode: nearest-neighbor;
27+
}
28+
</style>
29+
</head>
30+
<body>
31+
<canvas class="emscripten" id="canvas" oncontextmenu="event.preventDefault()"></canvas>
32+
<script type='text/javascript'>
33+
34+
var Module = {
35+
preRun: [],
36+
postRun: [],
37+
print: (function() {
38+
return function(text) {
39+
text = Array.prototype.slice.call(arguments).join(' ');
40+
console.log(text);
41+
};
42+
})(),
43+
printErr: function(text) {
44+
text = Array.prototype.slice.call(arguments).join(' ');
45+
console.error(text);
46+
},
47+
canvas: (function() {
48+
var canvas = document.getElementById('canvas');
49+
//canvas.addEventListener("webglcontextlost", function(e) { alert('FIXME: WebGL context lost, please reload the page'); e.preventDefault(); }, false);
50+
return canvas;
51+
})(),
52+
setStatus: function(text) {
53+
console.log("status: " + text);
54+
},
55+
monitorRunDependencies: function(left) {
56+
// no run dependencies to log
57+
}
58+
};
59+
window.onerror = function() {
60+
console.log("onerror: " + event);
61+
};
62+
</script>
63+
{{{ SCRIPT }}}
64+
</body>
65+
</html>

src/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ set(TARGET ImStudio)
33
include(third-party)
44

55
file(GLOB_RECURSE SOURCES ${CMAKE_CURRENT_SOURCE_DIR}/sources/*.cpp ${CMAKE_CURRENT_SOURCE_DIR}/utils/*.cpp)
6-
file(GLOB_RECURSE HEADERS ${CMAKE_CURRENT_SOURCE_DIR}/headers/*.h ${CMAKE_CURRENT_SOURCE_DIR}/utils/*.h)
6+
file(GLOB_RECURSE HEADERS ${CMAKE_CURRENT_SOURCE_DIR}/sources/*.h ${CMAKE_CURRENT_SOURCE_DIR}/utils/*.h)
77

88
add_executable(${TARGET} WIN32 MACOSX_BUNDLE
99
${SOURCES}

src/em.cpp

Lines changed: 178 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,178 @@
1+
#include "imgui.h"
2+
#include "imgui_impl_sdl.h"
3+
#include "imgui_impl_opengl3.h"
4+
5+
#include <emscripten.h>
6+
#include <SDL.h>
7+
#include <SDL_opengles2.h>
8+
9+
#include "includes.h"
10+
#include "sources/object.h"
11+
#include "sources/buffer.h"
12+
#include "sources/gui.h"
13+
14+
SDL_Window *g_Window = NULL;
15+
SDL_GLContext g_GLContext = NULL;
16+
struct State
17+
{
18+
ImStudio::GUI gui;
19+
std::mt19937 rng;
20+
int w_w = 900;
21+
int w_h = 600;
22+
};
23+
24+
static void main_loop(void *);
25+
26+
int main(int, char **)
27+
{
28+
State state;
29+
state.gui.bw.objects.reserve(2048);
30+
state.rng.seed(time(NULL));
31+
32+
if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_TIMER | SDL_INIT_GAMECONTROLLER) != 0)
33+
{
34+
printf("Error: %s\n", SDL_GetError());
35+
return -1;
36+
}
37+
38+
const char *glsl_version = "#version 100";
39+
// const char* glsl_version = "#version 300 es";
40+
SDL_GL_SetAttribute(SDL_GL_CONTEXT_FLAGS, 0);
41+
SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_ES);
42+
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 2);
43+
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 0);
44+
45+
SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
46+
SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 24);
47+
SDL_GL_SetAttribute(SDL_GL_STENCIL_SIZE, 8);
48+
SDL_DisplayMode current;
49+
SDL_GetCurrentDisplayMode(0, &current);
50+
SDL_WindowFlags window_flags =
51+
(SDL_WindowFlags)(SDL_WINDOW_OPENGL | SDL_WINDOW_RESIZABLE | SDL_WINDOW_ALLOW_HIGHDPI);
52+
g_Window = SDL_CreateWindow("ImStudio", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, 1280, 720, window_flags);
53+
g_GLContext = SDL_GL_CreateContext(g_Window);
54+
if (!g_GLContext)
55+
{
56+
fprintf(stderr, "Failed to initialize WebGL context!\n");
57+
return 1;
58+
}
59+
SDL_GL_SetSwapInterval(1); // Enable vsync
60+
61+
IMGUI_CHECKVERSION();
62+
ImGui::CreateContext();
63+
ImGuiIO &io = ImGui::GetIO();
64+
(void)io;
65+
66+
io.IniFilename = NULL;
67+
68+
ImGuiStyle &style = ImGui::GetStyle();
69+
70+
style.WindowPadding = ImVec2(12.00f, 8.00f);
71+
style.ItemSpacing = ImVec2(15.00f, 4.00f);
72+
style.GrabMinSize = 20.00f;
73+
style.WindowRounding = 8.00f;
74+
style.FrameBorderSize = 1.00f;
75+
style.FrameRounding = 4.00f;
76+
style.GrabRounding = 12.00f;
77+
78+
ImVec4 *colors = ImGui::GetStyle().Colors;
79+
80+
colors[ImGuiCol_Text] = ImVec4(0.92f, 0.92f, 0.92f, 1.00f);
81+
colors[ImGuiCol_WindowBg] = ImVec4(0.45f, 0.45f, 0.45f, 1.00f);
82+
colors[ImGuiCol_Border] = ImVec4(0.12f, 0.12f, 0.12f, 0.50f);
83+
colors[ImGuiCol_Button] = ImVec4(0.59f, 0.59f, 0.59f, 1.00f);
84+
85+
ImGui_ImplSDL2_InitForOpenGL(g_Window, g_GLContext);
86+
ImGui_ImplOpenGL3_Init(glsl_version);
87+
88+
/////////////////////////////////////////////////////////
89+
emscripten_set_main_loop_arg(main_loop, &state, 0, true);
90+
/////////////////////////////////////////////////////////
91+
}
92+
93+
static void main_loop(void *arg)
94+
{
95+
State &state = *(State *)arg;
96+
ImStudio::GUI &gui = state.gui;
97+
std::mt19937 &rng = state.rng;
98+
int &w_w = state.w_w;
99+
int &w_h = state.w_h;
100+
101+
std::uniform_int_distribution<int> gen(999, 9999);
102+
103+
ImGuiIO &io = ImGui::GetIO();
104+
105+
static ImVec4 bg = ImVec4(0.123f, 0.123f, 0.123, 1.00f);
106+
107+
SDL_Event event;
108+
while (SDL_PollEvent(&event))
109+
{
110+
ImGui_ImplSDL2_ProcessEvent(&event);
111+
}
112+
w_w = io.DisplaySize.x;
113+
w_h = io.DisplaySize.y;
114+
115+
ImGui_ImplOpenGL3_NewFrame();
116+
ImGui_ImplSDL2_NewFrame();
117+
ImGui::NewFrame();
118+
119+
ImGui::SetNextWindowPos(ImVec2(0, 0));
120+
ImGui::SetNextWindowSize(ImGui::GetIO().DisplaySize);
121+
ImGui::SetNextWindowBgAlpha(0.00f);
122+
123+
// window-menubar
124+
gui.mb_P = ImVec2(0, 0);
125+
gui.mb_S = ImVec2(w_w, 46);
126+
if (gui.menubar) gui.ShowMenubar();
127+
128+
// workspace-create
129+
if (gui.wksp_create)
130+
{
131+
{ // create-main
132+
// create-sidebar
133+
gui.sb_P = ImVec2(0, gui.mb_S.y);
134+
gui.sb_S = ImVec2(170, w_h - gui.mb_S.y);
135+
if (gui.sidebar) gui.ShowSidebar();
136+
137+
// create-properties
138+
gui.pt_P = ImVec2(w_w - 300, gui.mb_S.y);
139+
gui.pt_S = ImVec2(300, w_h - gui.mb_S.y);
140+
if (gui.properties) gui.ShowProperties();
141+
142+
// create-viewport
143+
gui.vp_P = ImVec2(gui.sb_S.x, gui.mb_S.y);
144+
gui.vp_S = ImVec2(gui.pt_P.x - gui.sb_S.x, w_h - gui.mb_S.y);
145+
if (gui.viewport) gui.ShowViewport(gen(rng));
146+
147+
}
148+
{ // create-children
149+
if (gui.child_style) utils::ShowStyleEditorWindow(&gui.child_style);
150+
151+
if (gui.child_demo) ImGui::ShowDemoWindow(&gui.child_demo);
152+
153+
if (gui.child_metrics) ImGui::ShowMetricsWindow(&gui.child_metrics);
154+
155+
if (gui.child_stack) ImGui::ShowStackToolWindow(&gui.child_stack);
156+
157+
if (gui.child_color) utils::ShowColorExportWindow(&gui.child_color);
158+
159+
if (gui.child_resources) utils::ShowResourcesWindow(&gui.child_resources);
160+
161+
if (gui.child_about) utils::ShowAboutWindow(&gui.child_about);
162+
}
163+
}
164+
165+
// workspace-output
166+
gui.ot_P = ImVec2(0, gui.mb_S.y);
167+
gui.ot_S = ImVec2(w_w, w_h - gui.mb_S.y);
168+
if (gui.wksp_output) gui.ShowOutputWorkspace();
169+
170+
// Rendering
171+
ImGui::Render();
172+
SDL_GL_MakeCurrent(g_Window, g_GLContext);
173+
glViewport(0, 0, (int)io.DisplaySize.x, (int)io.DisplaySize.y);
174+
glClearColor(bg.x * bg.w, bg.y * bg.w, bg.z * bg.w, bg.w);
175+
glClear(GL_COLOR_BUFFER_BIT);
176+
ImGui_ImplOpenGL3_RenderDrawData(ImGui::GetDrawData());
177+
SDL_GL_SwapWindow(g_Window);
178+
}

src/includes.h

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,8 @@
1212
#include <memory>
1313
#include <stdexcept>
1414

15-
#include "third-party/imgui/imgui.h"
16-
#include "third-party/imgui/backends/imgui_impl_glfw.h"
17-
#include "third-party/imgui/backends/imgui_impl_opengl3.h"
18-
#include "third-party/imgui/misc/cpp/imgui_stdlib.h"
19-
#include "third-party/imgui/imgui_internal.h"
20-
#include "third-party/fmt/include/fmt/format.h"
15+
#include "imgui.h"
16+
#include "imgui_stdlib.h"
17+
#include "imgui_internal.h"
18+
#include "fmt/format.h"
2119
#include "utils/utils.h"

0 commit comments

Comments
 (0)