Skip to content

Commit 737d501

Browse files
committed
Simple Setup
1 parent 30754e8 commit 737d501

File tree

9 files changed

+118
-7
lines changed

9 files changed

+118
-7
lines changed

.gitignore

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
# Created by .ignore support plugin (hsz.mobi)
21
### Linux template
32
*~
43

CMakeLists.txt

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
# This is only for compiling for native usage and test, not to be used to port WebAssembly
2+
cmake_minimum_required(VERSION 2.8)
3+
project(webassembly_opengl)
4+
5+
# Not sure if this is correct, but fixes c++ x64 arch issues with std
6+
set (CMAKE_CXX_STANDARD 11)
7+
# includes cmake/FindSDL2.cmake
8+
set(CMAKE_MODULE_PATH ${PROJECT_SOURCE_DIR}/cmake)
9+
10+
find_package (OpenGL REQUIRED)
11+
# @TODO switch SDL to GLFW
12+
find_package(SDL2 REQUIRED)
13+
find_package(GLEW REQUIRED)
14+
include_directories(${SDL2_INCLUDE_DIR})
15+
include_directories(${OPENGL_INCLUDE_DIR})
16+
include_directories(${GLEW_INCLUDE_DIR})
17+
include_directories(deps/include)
18+
19+
file(GLOB SOURCE_FILES src/*.cpp)
20+
21+
add_executable(webassembly_opengl ${SOURCE_FILES})
22+
target_link_libraries(webassembly_opengl ${SDL2_LIBRARY} ${OPENGL_LIBRARIES} ${GLEW_LIBRARIES} ${GLM_LIBRARIES})
23+
target_compile_features(webassembly_opengl PRIVATE cxx_range_for)

app/counter.wasm

294 Bytes
Binary file not shown.

app/index.html

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,6 @@
66
</head>
77
<body>
88
<div id="app"></div>
9+
910
</body>
1011
</html>

app/index.js

Lines changed: 38 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,42 @@
11
'use strict';
22
import './style.scss';
3+
import Counter from './counter.wasm';
34

4-
// randomly using ES7 object rest spread because it currently raises
5-
// an error in all browsers, but can be transpiled by Babel
6-
const { x, y, ...z } = { x: 1, y: 2, a: 3, b: 4 };
7-
const n = { x, y, ...z };
8-
if (Object.keys(n).map((key) => n[key]).reduce((p,v) => p + v) === 10) {
9-
document.querySelector('#app').insertAdjacentHTML('afterbegin', '<h1>works.</h1>');
5+
// globals
6+
let COUNTER;
7+
8+
function main() {
9+
insertCounterGUI();
10+
loadCounterWasm();
11+
}
12+
13+
function _count() {
14+
if (COUNTER) {
15+
document.getElementById('counter').innerHTML = COUNTER.exports._count();
16+
}
17+
}
18+
19+
function insertCounterGUI() {
20+
// Insert Counter & Global method for UI
21+
document.querySelector('#app').insertAdjacentHTML('afterbegin', '<button id="count">Click me</button><div>Count: <span id="counter"></span></div>');
22+
const count = document.getElementById('count');
23+
count.addEventListener('click', _count);
1024
}
25+
26+
function loadCounterWasm() {
27+
const wasmHelloWorld = () => {
28+
COUNTER = new Counter({
29+
'env': {
30+
'memoryBase': 0,
31+
'tableBase': 0,
32+
'memory': new WebAssembly.Memory({initial: 256}),
33+
'table': new WebAssembly.Table({initial: 0, element: 'anyfunc'})
34+
}
35+
});
36+
// console.log("count function result is : " + COUNTER.exports._count());
37+
};
38+
window.onload = wasmHelloWorld;
39+
}
40+
41+
42+
main();

cmake-build-debug/Project.cbp

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<CodeBlocks_project_file>
3+
<FileVersion major="1" minor="6"/>
4+
<Project>
5+
<Option title="Project"/>
6+
<Option makefile_is_custom="1"/>
7+
<Option compiler="clang"/>
8+
<Option virtualFolders="CMake Files\;"/>
9+
<Build>
10+
<Target title="all">
11+
<Option working_dir="/Users/patrick.opie/Documents/github/WebAssemblyOpenGL/cmake-build-debug"/>
12+
<Option type="4"/>
13+
<MakeCommands>
14+
<Build command="/usr/bin/make -j8 -f &quot;/Users/patrick.opie/Documents/github/WebAssemblyOpenGL/cmake-build-debug/Makefile&quot; VERBOSE=1 all"/>
15+
<CompileFile command="/usr/bin/make -j8 -f &quot;/Users/patrick.opie/Documents/github/WebAssemblyOpenGL/cmake-build-debug/Makefile&quot; VERBOSE=1 &quot;$file&quot;"/>
16+
<Clean command="/usr/bin/make -j8 -f &quot;/Users/patrick.opie/Documents/github/WebAssemblyOpenGL/cmake-build-debug/Makefile&quot; VERBOSE=1 clean"/>
17+
<DistClean command="/usr/bin/make -j8 -f &quot;/Users/patrick.opie/Documents/github/WebAssemblyOpenGL/cmake-build-debug/Makefile&quot; VERBOSE=1 clean"/>
18+
</MakeCommands>
19+
</Target>
20+
<Target title="rebuild_cache">
21+
<Option working_dir="/Users/patrick.opie/Documents/github/WebAssemblyOpenGL/cmake-build-debug"/>
22+
<Option type="4"/>
23+
<MakeCommands>
24+
<Build command="/usr/bin/make -j8 -f &quot;/Users/patrick.opie/Documents/github/WebAssemblyOpenGL/cmake-build-debug/Makefile&quot; VERBOSE=1 rebuild_cache"/>
25+
<CompileFile command="/usr/bin/make -j8 -f &quot;/Users/patrick.opie/Documents/github/WebAssemblyOpenGL/cmake-build-debug/Makefile&quot; VERBOSE=1 &quot;$file&quot;"/>
26+
<Clean command="/usr/bin/make -j8 -f &quot;/Users/patrick.opie/Documents/github/WebAssemblyOpenGL/cmake-build-debug/Makefile&quot; VERBOSE=1 clean"/>
27+
<DistClean command="/usr/bin/make -j8 -f &quot;/Users/patrick.opie/Documents/github/WebAssemblyOpenGL/cmake-build-debug/Makefile&quot; VERBOSE=1 clean"/>
28+
</MakeCommands>
29+
</Target>
30+
<Target title="edit_cache">
31+
<Option working_dir="/Users/patrick.opie/Documents/github/WebAssemblyOpenGL/cmake-build-debug"/>
32+
<Option type="4"/>
33+
<MakeCommands>
34+
<Build command="/usr/bin/make -j8 -f &quot;/Users/patrick.opie/Documents/github/WebAssemblyOpenGL/cmake-build-debug/Makefile&quot; VERBOSE=1 edit_cache"/>
35+
<CompileFile command="/usr/bin/make -j8 -f &quot;/Users/patrick.opie/Documents/github/WebAssemblyOpenGL/cmake-build-debug/Makefile&quot; VERBOSE=1 &quot;$file&quot;"/>
36+
<Clean command="/usr/bin/make -j8 -f &quot;/Users/patrick.opie/Documents/github/WebAssemblyOpenGL/cmake-build-debug/Makefile&quot; VERBOSE=1 clean"/>
37+
<DistClean command="/usr/bin/make -j8 -f &quot;/Users/patrick.opie/Documents/github/WebAssemblyOpenGL/cmake-build-debug/Makefile&quot; VERBOSE=1 clean"/>
38+
</MakeCommands>
39+
</Target>
40+
</Build>
41+
<Unit filename="/Users/patrick.opie/Documents/github/WebAssemblyOpenGL/CMakeLists.txt">
42+
<Option virtualFolder="CMake Files\"/>
43+
</Unit>
44+
</Project>
45+
</CodeBlocks_project_file>

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
"node-sass": "^4.5.3",
3333
"sass-loader": "^6.0.6",
3434
"style-loader": "^0.18.2",
35+
"wasm-loader": "^1.0.0",
3536
"webpack": "^3.0.0",
3637
"webpack-dev-server": "^2.5.0"
3738
}

src/counter.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
int counter = 100;
2+
3+
int count() {
4+
counter += 1;
5+
return counter;
6+
}

webpack.config.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,10 @@ module.exports = {
3333
test: /\.scss$/,
3434
include: __dirname + '/app',
3535
loader: ExtractTextPlugin.extract('css-loader!sass-loader')
36+
},
37+
{
38+
test: /\.wasm$/,
39+
loaders: ['wasm-loader']
3640
}
3741
]
3842
},

0 commit comments

Comments
 (0)