Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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
33 changes: 33 additions & 0 deletions .github/workflows/wasm-build.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
name: Build WASM Logic

on:
push:
branches: [ feat-web-optimization-wasm, main ]
pull_request:
branches: [ main ]
workflow_dispatch:

jobs:
build:
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v4

- name: Install Emscripten
uses: mymindstorm/setup-emscripten@v1

- name: Compile C to WASM
run: |
emcc src/pizza.c -o docs/pizza.js \
-s EXPORTED_FUNCTIONS='["_get_random_pizza_name", "_get_random_pizza_ingredients", "_calculate_parallax", "_malloc", "_free"]' \
-s EXPORTED_RUNTIME_METHODS='["ccall", "cwrap", "UTF8ToString"]' \
-s MODULARIZE=1 \
-s EXPORT_NAME="Module" \
-O3

- name: Deploy to GitHub Pages
uses: peaceiris/github-action-gh-pages@v3
with:
github_token: ${{ secrets.GITHUB_TOKEN }}
publish_dir: ./docs
1 change: 1 addition & 0 deletions docs/css/print.min.css

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

158 changes: 158 additions & 0 deletions docs/css/style.css`
Original file line number Diff line number Diff line change
@@ -0,0 +1,158 @@
:root {
--primary: #ff4d4d;
--primary-hover: #ff1a1a;
--bg-dark: #0f172a;
--bg-card: rgba(30, 41, 59, 0.7);
--text-main: #f8fafc;
--text-muted: #94a3b8;
--accent: #fbbf24;
--glass-border: rgba(255, 255, 255, 0.1);
}

* {
box-sizing: border-box;
margin: 0;
padding: 0;
}

body {
font-family: 'Inter', sans-serif;
background-color: var(--bg-dark);
color: var(--text-main);
overflow-x: hidden;
min-height: 100vh;
display: flex;
justify-content: center;
align-items: center;
}

.background-parallax {
position: fixed;
top: 0;
left: 0;
width: 100%;
height: 100%;
z-index: -1;
background: radial-gradient(circle at center, #1e293b 0%, #0f172a 100%);
opacity: 0.8;
}

.container {
width: 100%;
max-width: 800px;
padding: 2rem;
text-align: center;
}

.hero {
margin-bottom: 3rem;
}

.title {
font-family: 'Pacifico', cursive;
font-size: 4rem;
margin-bottom: 0.5rem;
}

.highlight {
color: var(--primary);
}

.subtitle {
font-size: 1.2rem;
color: var(--text-muted);
}

.wasm-badge {
background: linear-gradient(45deg, #6366f1, #a855f7);
color: white;
padding: 2px 8px;
border-radius: 4px;
font-size: 0.9rem;
font-weight: bold;
}

.generator-card {
background: var(--bg-card);
backdrop-filter: blur(12px);
border: 1px solid var(--glass-border);
border-radius: 24px;
padding: 3rem;
box-shadow: 0 25px 50px -12px rgba(0, 0, 0, 0.5);
transition: transform 0.3s ease;
}

.generator-card:hover {
transform: translateY(-5px);
}

.card-header h2 {
font-size: 2rem;
margin-bottom: 1rem;
}

.card-header p {
color: var(--text-muted);
margin-bottom: 2rem;
}

.result-area {
margin-bottom: 2rem;
padding: 2rem;
background: rgba(0, 0, 0, 0.2);
border-radius: 16px;
border: 1px dashed var(--glass-border);
}

.pizza-name {
font-size: 2.5rem;
font-weight: 900;
color: var(--accent);
margin-bottom: 1rem;
min-height: 3rem;
}

.pizza-ingredients {
font-size: 1.1rem;
color: var(--text-main);
line-height: 1.6;
font-style: italic;
}

.btn-primary {
background-color: var(--primary);
color: white;
border: none;
padding: 1rem 2.5rem;
font-size: 1.2rem;
font-weight: bold;
border-radius: 50px;
cursor: pointer;
transition: all 0.2s ease;
box-shadow: 0 10px 15px -3px rgba(255, 77, 77, 0.4);
}

.btn-primary:hover {
background-color: var(--primary-hover);
transform: scale(1.05);
box-shadow: 0 20px 25px -5px rgba(255, 77, 77, 0.5);
}

.info-footer {
margin-top: 3rem;
color: var(--text-muted);
}

.stats-grid {
display: flex;
justify-content: center;
gap: 2rem;
margin-top: 1rem;
font-size: 0.9rem;
}

.stat-item {
background: rgba(255, 255, 255, 0.05);
padding: 0.5rem 1rem;
border-radius: 8px;
}
1 change: 1 addition & 0 deletions docs/css/style.min.css

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions docs/img/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
.DS_Store
Binary file added docs/img/2048.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added docs/img/DeskTop-Score-90+.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added docs/img/Pizzera-FPS.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added docs/img/cam_be_like.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added docs/img/index-page-FPS.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added docs/img/mobile-score-90+.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added docs/img/mobilewebdev.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added docs/img/profilepic.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
49 changes: 49 additions & 0 deletions docs/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>🍕 Cam's Pizzeria | WASM Edition</title>
<link rel="stylesheet" href="css/style.css">
<link rel="preconnect" href="https://fonts.googleapis.com">
<link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
<link href="https://fonts.googleapis.com/css2?family=Inter:wght@400;700;900&family=Pacifico&display=swap" rel="stylesheet">
</head>
<body>
<div class="background-parallax" id="bg-parallax"></div>

<main class="container">
<header class="hero">
<h1 class="title">Cam's <span class="highlight">Pizzeria</span></h1>
<p class="subtitle">High-Performance Pizza Generation powered by <span class="wasm-badge">WebAssembly</span></p>
</header>

<section class="generator-card">
<div class="card-header">
<h2>Create Your Masterpiece</h2>
<p>Click the button to generate a random, high-performance pizza.</p>
</div>

<div class="result-area">
<div id="pizza-name" class="pizza-name">Click Generate!</div>
<div id="pizza-ingredients" class="pizza-ingredients">Your ingredients will appear here...</div>
</div>

<button id="generate-btn" class="btn-primary">🍕 Generate Pizza</button>
</section>

<footer class="info-footer">
<p>Logic calculated in <strong>C</strong> $\rightarrow$ Compiled to <strong>WASM</strong></p>
<div class="stats-grid">
<div class="stat-item"><strong>Speed:</strong> Ultra-Fast</div>
<div class="stat-item"><strong>Safety:</strong> Typed</div>
<div class="stat-item"><strong>Logic:</strong> Native C</div>
</div>
</footer>
</main>

<!-- WASM Glue Code -->
<script src="pizza.js"></script>
<script src="js/main.js"></script>
</body>
</html>
60 changes: 60 additions & 0 deletions docs/js/main.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
/**
* BEGINNER'S GUIDE TO WASM GLUE CODE:
*
* When we compile C to WASM, it doesn't just give us a binary.
* Emscripten creates a 'glue' JS file (pizza.js) that handles
* loading the .wasm file and mapping C functions to JS.
*
* Module: This is the object created by the WASM loader.
* ccall: A helper function provided by Emscripten to call C functions.
* - Param 1: Name of the C function.
* - Param 2: Return type (e.g., 'number' for float/int, 'string' for char*).
* - Param 3: Array of arguments.
*/

document.addEventListener('DOMContentLoaded', () => {
const generateBtn = document.getElementById('generate-btn');
const nameDiv = document.getElementById('pizza-name');
const ingredientsDiv = document.getElementById('pizza-ingredients');
const bgParallax = document.getElementById('bg-parallax');

// We wrap the logic in a function to ensure the WASM module is loaded
const runPizzaGen = () => {
try {
// 1. Get Random Name from C
// get_random_pizza_name() returns a char* in C
const name = Module.ccall('get_random_pizza_name', 'string', [], []);
nameDiv.innerText = name;

// 2. Get Random Ingredients from C
// In C, we pass a buffer. In JS/WASM, the easiest way is often
// returning a string or using a shared buffer.
// For this project's current C implementation, we need to allocate
// a buffer in WASM memory.

const bufferSize = 512;
const bufferPtr = Module._malloc(bufferSize);
Module.ccall('get_random_pizza_ingredients', null, ['number'], [bufferPtr]);
const ingredients = Module.UTF8ToString(bufferPtr);
Module._free(bufferPtr);

ingredientsDiv.innerText = ingredients.replace(/,/g, ', ');
} catch (e) {
console.error("WASM Error:", e);
nameDiv.innerText = "WASM Error!";
ingredientsDiv.innerText = "Check console for details.";
}
};

generateBtn.addEventListener('click', runPizzaGen);

// Parallax Effect
// Use the C function calculate_parallax(scroll_y, index)
window.addEventListener('scroll', () => {
const scrollY = window.scrollY;
const offset = Module.ccall('calculate_parallax', 'number', ['number', 'number'], [scrollY, 0]);

// Move the background slightly to create a depth effect
bgParallax.style.transform = `translateY(${offset * 20}px)`;
});
});
14 changes: 14 additions & 0 deletions docs/js/perfmatters.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
// Measuring the Critical Rendering Path with Navigation Timing
// https://developers.google.com/web/fundamentals/performance/critical-rendering-path/measure-crp

function logCRP() {
var t = window.performance.timing,
dcl = t.domContentLoadedEventStart - t.domLoading,
complete = t.domComplete - t.domLoading;
var stats = document.getElementById("crp-stats");
stats.textContent = 'DCL: ' + dcl + 'ms, onload: ' + complete + 'ms';
}

window.addEventListener("load", function(event) {
logCRP();
});
2 changes: 2 additions & 0 deletions docs/js/perfmatters.min.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
function logCRP(){var o=window.performance.timing,n=o.domContentLoadedEventStart-o.domLoading,t=o.domComplete-o.domLoading
document.getElementById("crp-stats").textContent="DCL: "+n+"ms, onload: "+t+"ms"}window.addEventListener("load",function(o){logCRP()})
39 changes: 39 additions & 0 deletions tests/test_pizza.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
#include "../src/pizza.c"
#include <assert.h>
#include <stdio.h>
#include <string.h>

void test_random_name() {
printf("Testing random name generation... ");
const char* name = get_random_pizza_name();
assert(name != NULL);
assert(strlen(name) > 0);
printf("PASSED\n");
}

void test_random_ingredients() {
printf("Testing ingredient generation... ");
char buffer[512];
get_random_pizza_ingredients(buffer, 512);
assert(strlen(buffer) > 0);
printf("PASSED\n");
}

void test_parallax() {
printf("Testing parallax calculation... ");
float p1 = calculate_parallax(0, 0);
float p2 = calculate_parallax(1250, 0);
// sin(0) = 0, sin(1) = 0.84
assert(p1 >= -1.0 && p1 <= 1.0);
assert(p2 >= -1.0 && p2 <= 1.0);
printf("PASSED\n");
}

int main() {
printf("Running Native C Tests...\n");
test_random_name();
test_random_ingredients();
test_parallax();
printf("All tests passed successfully!\n");
return 0;
}
Loading