Skip to content

Commit f085bf5

Browse files
committed
Refactor: Build emscripten
1 parent 7233729 commit f085bf5

34 files changed

Lines changed: 1279 additions & 813 deletions

.github/workflows/build-web.yml

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
name: Build Web
2+
3+
on:
4+
push:
5+
branches:
6+
- "trunk"
7+
- "dev"
8+
9+
permissions:
10+
contents: read
11+
pages: write
12+
id-token: write
13+
14+
jobs:
15+
build-web:
16+
runs-on: ubuntu-latest
17+
steps:
18+
- uses: actions/checkout@v3
19+
20+
- name: Build emscripten using docker image
21+
run: |
22+
docker run --rm -v "$(pwd):/src" emscripten/emsdk:4.0.6 sh ./web/build.sh
23+
24+
- name: List build output
25+
run: ls -R build-web/dist
26+
27+
- name: Upload built site
28+
uses: actions/upload-artifact@v4
29+
with:
30+
name: web-dist
31+
path: build-web/dist

.github/workflows/deploy-web.yml

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
name: Deploy Web
2+
3+
on:
4+
workflow_run:
5+
workflows: [ "Build" ] # This must match build.yml's name exactly
6+
branches: [ "trunk" ]
7+
types:
8+
- completed
9+
10+
permissions:
11+
contents: read
12+
actions: read
13+
pages: write
14+
id-token: write
15+
16+
# Allow one concurrent deployment
17+
concurrency:
18+
group: "pages"
19+
cancel-in-progress: true
20+
21+
jobs:
22+
deploy:
23+
runs-on: ubuntu-latest
24+
environment:
25+
name: github-pages
26+
url: ${{ steps.deployment.outputs.page_url }}
27+
28+
steps:
29+
- name: Download web build artifact
30+
uses: actions/download-artifact@v4
31+
with:
32+
workflow: build.yml
33+
github-token: ${{ github.token }}
34+
run-id: ${{ github.event.workflow_run.id }}
35+
name: web-dist
36+
path: ./build-web
37+
38+
- name: Upload site artifact
39+
uses: actions/upload-pages-artifact@v3
40+
with:
41+
path: "build-web${{ steps.pages.outputs.base_path }}"
42+
43+
- name: Deploy to GitHub Pages
44+
id: deployment
45+
uses: actions/deploy-pages@v4

.github/workflows/emscripten-build.yml

Lines changed: 0 additions & 42 deletions
This file was deleted.

.gitignore

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,3 +40,7 @@ compile_commands.json
4040
# JetBrains Fleet
4141
.fleet
4242
.cache
43+
44+
# Keep
45+
!.github/workflows/build-web.yml
46+
!.github/workflows/deploy-web.yml

CMakeLists.txt

Lines changed: 92 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -4,22 +4,24 @@ set_property(GLOBAL PROPERTY USE_FOLDERS ON) # Group CMake targets inside a fol
44
set(CMAKE_EXPORT_COMPILE_COMMANDS ON) # Generate compile_commands.json for language servers
55

66
include(FetchContent)
7-
option(GLFW_BUILD_EXAMPLES OFF)
8-
option(GLFW_BUILD_TESTS OFF)
9-
option(GLFW_BUILD_DOCS OFF)
10-
option(GLFW_INSTALL OFF)
11-
FetchContent_Declare(
12-
glfw
13-
GIT_REPOSITORY https://github.com/glfw/glfw.git
14-
GIT_TAG 3.3.8
15-
)
16-
list(APPEND FETCH_CONTENTS glfw)
17-
FetchContent_Declare(
18-
glad
19-
GIT_REPOSITORY https://github.com/mononerv/glad.git
20-
GIT_TAG f4759d7c5143c0a23391ab05caaf43052cefdd65
21-
)
22-
list(APPEND FETCH_CONTENTS glad)
7+
if (NOT EMSCRIPTEN)
8+
option(GLFW_BUILD_EXAMPLES OFF)
9+
option(GLFW_BUILD_TESTS OFF)
10+
option(GLFW_BUILD_DOCS OFF)
11+
option(GLFW_INSTALL OFF)
12+
FetchContent_Declare(
13+
glfw
14+
GIT_REPOSITORY https://github.com/glfw/glfw.git
15+
GIT_TAG 3.4
16+
)
17+
list(APPEND FETCH_CONTENTS glfw)
18+
FetchContent_Declare(
19+
glad
20+
GIT_REPOSITORY https://github.com/mononerv/glad.git
21+
GIT_TAG f4759d7c5143c0a23391ab05caaf43052cefdd65
22+
)
23+
list(APPEND FETCH_CONTENTS glad)
24+
endif()
2325
FetchContent_Declare(
2426
fmt
2527
GIT_REPOSITORY https://github.com/fmtlib/fmt.git
@@ -50,6 +52,12 @@ FetchContent_Declare(
5052
GIT_TAG 698c6fb9889c71494b49c9187d249af5fc87b211
5153
)
5254
list(APPEND FETCH_CONTENTS stb)
55+
FetchContent_Declare(
56+
asio
57+
GIT_REPOSITORY https://github.com/mononerv/asio.git
58+
GIT_TAG bcb8a933b27021d77f2d183991d815ea6c8cdc97
59+
)
60+
list(APPEND FETCH_CONTENTS asio)
5361
FetchContent_MakeAvailable(${FETCH_CONTENTS})
5462

5563
# Group dependencies in Visual Studio and Xcode
@@ -63,22 +71,44 @@ if (CMAKE_GENERATOR MATCHES "Visual Studio" OR CMAKE_GENERATOR MATCHES "Xcode")
6371
endif()
6472

6573
if (APPLE)
74+
message("Platform: macOS")
6675
set(PLATFORM_LINK_LIBRARIES
6776
"-framework Cocoa"
6877
"-framework IOKit"
6978
"-framework CoreVideo"
7079
"-framework OpenGL"
7180
)
72-
elseif (UNIX AND NOT APPLE) # Linux, BSD, Solaris, Minix
81+
elseif (UNIX AND NOT APPLE AND NOT EMSCRIPTEN) # Linux, BSD, Solaris, Minix
82+
message("Platform: Linux")
7383
set(PLATFORM_LINK_LIBRARIES
7484
"dl"
7585
"m"
86+
"GL"
7687
"X11"
7788
)
7889
elseif (WIN32)
90+
message("Platform: Windows")
7991
set(PLATFORM_LINK_LIBRARIES "OpenGL32.lib")
92+
set(PLATFORM_DEFINTIONS
93+
${PLATFORM_DEFINTIONS}
94+
"_WIN32_WINNT=0x0A00"
95+
)
96+
elseif(EMSCRIPTEN)
97+
message("Platform: Emscripten")
98+
add_compile_definitions(
99+
"GL_GLEXT_PROTOTYPES"
100+
"GL3_PROTOTYPES"
101+
)
102+
add_compile_options("-pthread" "-fexceptions")
103+
add_link_options(
104+
"-sLEGACY_GL_EMULATION=0"
105+
"-sUSE_WEBGL2=1"
106+
"-sFULL_ES3=1"
107+
"-fexceptions"
108+
"-sALLOW_MEMORY_GROWTH"
109+
)
80110
else()
81-
message(FATAL_ERROR "Unkown platform!")
111+
message(FATAL_ERROR "Platform: Unkown!")
82112
endif()
83113

84114
# Compiler specific options
@@ -98,7 +128,22 @@ if (NOT MSVC)
98128
# glm warnings
99129
"-Wno-nested-anon-types"
100130
"-Wno-gnu-anonymous-struct"
131+
# clang on windows
132+
"-Wno-microsoft-include"
133+
# asio
134+
"-Wno-sign-conversion"
135+
"-Wno-implicit-int-conversion"
101136
)
137+
if (EMSCRIPTEN)
138+
set(BASE_OPTIONS
139+
${BASE_OPTIONS}
140+
# asio
141+
"-Wno-shadow"
142+
"-Wno-shorten-64-to-32"
143+
"-Wno-unused-private-field"
144+
"-Wno-deprecated-declarations"
145+
)
146+
endif()
102147
else()
103148
set(BASE_OPTIONS
104149
"/W4"
@@ -112,29 +157,34 @@ endif()
112157

113158
# libtxtr
114159
set(HEADERS
115-
txt/buffer.hpp
160+
txt/graphics/buffer.hpp
161+
txt/graphics/image.hpp
162+
txt/graphics/shader.hpp
163+
txt/graphics/texture.hpp
164+
txt/text/engine.hpp
165+
txt/text/fonts.hpp
166+
txt/input/keyboard.hpp
167+
txt/input/mouse.hpp
168+
txt/input/touch.hpp
116169
txt/event.hpp
117-
txt/fonts.hpp
118-
txt/image.hpp
119170
txt/input.hpp
120171
txt/renderer.hpp
121-
txt/shader.hpp
122-
txt/text_engine.hpp
123-
txt/texture.hpp
124172
txt/types.hpp
125-
txt/utility.hpp
173+
txt/utils.hpp
126174
txt/window.hpp
127175
)
128176
set(SOURCES
129-
txt/buffer.cpp
130-
txt/fonts.cpp
131-
txt/image.cpp
132-
txt/input.cpp
133-
txt/renderer.cpp
134-
txt/shader.cpp
135-
txt/text_engine.cpp
136-
txt/texture.cpp
137-
txt/window.cpp
177+
txt/graphics/buffer.cpp
178+
txt/graphics/image.cpp
179+
txt/graphics/renderer.cpp
180+
txt/graphics/shader.cpp
181+
txt/graphics/texture.cpp
182+
txt/input/input.cpp
183+
txt/text/engine.cpp
184+
txt/text/fonts.cpp
185+
txt/window/window.cpp
186+
$<$<NOT:$<BOOL:${EMSCRIPTEN}>>:txt/window/glfw_window.cpp>
187+
$<$<BOOL:${EMSCRIPTEN}>:txt/window/web_window.cpp>
138188
)
139189
add_library(libtxtr STATIC ${HEADERS} ${SOURCES})
140190
target_include_directories(libtxtr
@@ -145,21 +195,24 @@ target_include_directories(libtxtr
145195
)
146196
target_compile_features(libtxtr PRIVATE cxx_std_23)
147197
target_compile_options(libtxtr PRIVATE ${BASE_OPTIONS})
198+
target_compile_definitions(libtxtr PRIVATE ${PLATFORM_DEFINTIONS})
148199
target_link_libraries(libtxtr
149200
PRIVATE
150201
${PLATFORM_LINK_LIBRARIES}
151-
glfw
152-
fmt
153202
utf8::cpp
154203
stb::stb
204+
fmt
205+
$<$<NOT:$<BOOL:${EMSCRIPTEN}>>:glfw>
155206

156207
PUBLIC
208+
asio::asio
157209
freetype
158-
glad
210+
$<$<NOT:$<BOOL:${EMSCRIPTEN}>>:glad::glad>
159211
glm
160212
)
161-
source_group(TREE "${CMAKE_CURRENT_LIST_DIR}" FILES ${HEADERS} ${SOURCES})
213+
# source_group(TREE "${CMAKE_CURRENT_LIST_DIR}" FILES ${HEADERS} ${SOURCES})
162214

215+
# hellotext app
163216
set(HEADERS
164217
demos/app.hpp
165218
demos/text_bounce.hpp
@@ -171,6 +224,7 @@ add_executable(hellotext ${HEADERS} ${SOURCES})
171224
target_include_directories(hellotext PRIVATE ${PROJECT_SOURCE_DIR})
172225
target_compile_features(hellotext PRIVATE cxx_std_20)
173226
target_compile_options(hellotext PRIVATE ${BASE_OPTIONS})
227+
target_compile_definitions(hellotext PRIVATE ${PLATFORM_DEFINTIONS})
174228
target_link_libraries(hellotext
175229
PRIVATE
176230
${PLATFORM_LINK_LIBRARIES}

hellotext.cpp

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
#include <span>
22

3-
#include "fmt/format.h"
4-
53
#include "txt/window.hpp"
6-
#include "txt/image.hpp"
74
#include "txt/renderer.hpp"
8-
#include "txt/fonts.hpp"
5+
#include "txt/text/fonts.hpp"
6+
7+
using namespace txt::types;
98

109
static auto entry([[maybe_unused]]std::span<char const*> const& args) -> void {
10+
fmt::print("hello\n");
1111
auto win = txt::make_window({"Hello, Text!"});
1212
auto ren = txt::make_renderer(win);
1313
ren->load_font({
@@ -21,18 +21,18 @@ static auto entry([[maybe_unused]]std::span<char const*> const& args) -> void {
2121

2222
win->add_event_listener([&](txt::mouse_move_event const& e) {
2323
fmt::print("{}\n", e.str());
24-
mouse_x = (float)e.x();
25-
mouse_y = (float)win->buffer_height() - (float)e.y();
24+
mouse_x = f32(e.x());
25+
mouse_y = f32(win->buffer_height() - e.y());
2626
});
2727
win->add_event_listener([&](txt::key_down_event const& e) {
2828
fmt::print("{}\n", e.str());
2929
});
3030
win->add_event_listener([&](txt::key_up_event const& e) {
3131
if (e.keycode() == txt::keycode::Q) win->close();
32-
if (e.keycode() == txt::keycode::F) win->fullscreen();
32+
if (e.keycode() == txt::keycode::F) win->toggle_fullscreen();
3333
});
3434

35-
while (!win->should_close()) {
35+
txt::loop(win, [&] {
3636
ren->begin();
3737
ren->viewport(0, 0, win->buffer_width(), win->buffer_height());
3838
ren->clear_color(0x000000);
@@ -43,8 +43,7 @@ static auto entry([[maybe_unused]]std::span<char const*> const& args) -> void {
4343
ren->end();
4444

4545
win->swap();
46-
win->poll();
47-
}
46+
});
4847
}
4948

5049
auto main(int argc, char const* argv[]) -> int {

0 commit comments

Comments
 (0)