|
| 1 | +""" |
| 2 | +_processing_cpp_packaging.py -- shared engine-file and example-program |
| 3 | +content used by both generate_dragdrop.py and generate_cmake.py. |
| 4 | +
|
| 5 | +Not run directly. This exists so the two generator scripts don't each |
| 6 | +carry their own copy of "which files make up the engine" or "what the |
| 7 | +example sketches look like" -- there is exactly one place that lists |
| 8 | +ENGINE_HEADERS/ENGINE_SOURCES and one place that defines each example |
| 9 | +program, imported by both generators, so they can never drift apart |
| 10 | +from each other on those specifics. Everything that's genuinely |
| 11 | +PACKAGE-specific (the README, the build script, the CMakeLists.txt) |
| 12 | +stays in each generator's own file, not here. |
| 13 | +""" |
| 14 | +import shutil |
| 15 | +import sys |
| 16 | +import zipfile |
| 17 | +from pathlib import Path |
| 18 | + |
| 19 | +REPO_ROOT = Path(__file__).resolve().parent.parent |
| 20 | +SRC_DIR = REPO_ROOT / "src" |
| 21 | +DIST_DIR = REPO_ROOT / "dist" |
| 22 | + |
| 23 | +# Every one of these must exist in src/ -- if the engine's file layout |
| 24 | +# ever changes, check_source_layout() fails loudly rather than silently |
| 25 | +# shipping a stale/incomplete package. |
| 26 | +# |
| 27 | +# Notably absent: Processing_api.h and Platform.h. Both exist in the real |
| 28 | +# CppMode source tree, but neither is included by Processing.h or |
| 29 | +# Processing.cpp -- the engine itself never needs them. Processing_api.h |
| 30 | +# is only ever included by CODE THE TRANSPILER GENERATES (CppBuild.java |
| 31 | +# writes that #include into every sketch it emits, to support the |
| 32 | +# free-function setup()/draw() style); Platform.h isn't included by |
| 33 | +# anything in src/ at all. A hand-written PApplet subclass -- which is |
| 34 | +# the only way either of these packages is meant to be used -- needs |
| 35 | +# neither, confirmed by actually compiling examples/ without them before |
| 36 | +# removing them from this list. |
| 37 | +ENGINE_HEADERS = [ |
| 38 | + "Processing.h", |
| 39 | + "stb_image.h", |
| 40 | + "stb_image_write.h", |
| 41 | + "stb_truetype.h", |
| 42 | +] |
| 43 | +ENGINE_SOURCES = [ |
| 44 | + "Processing.cpp", |
| 45 | + "Processing_defaults.cpp", |
| 46 | +] |
| 47 | + |
| 48 | + |
| 49 | +def die(msg: str) -> None: |
| 50 | + print(f"error: {msg}", file=sys.stderr) |
| 51 | + sys.exit(1) |
| 52 | + |
| 53 | + |
| 54 | +def check_source_layout() -> None: |
| 55 | + missing = [f for f in ENGINE_HEADERS + ENGINE_SOURCES if not (SRC_DIR / f).is_file()] |
| 56 | + if missing: |
| 57 | + die( |
| 58 | + "src/ is missing expected engine file(s): " + ", ".join(missing) + "\n" |
| 59 | + "This script must be run from a CppMode checkout with an intact src/ directory." |
| 60 | + ) |
| 61 | + |
| 62 | + |
| 63 | +def copy_engine_files(out_dir: Path) -> None: |
| 64 | + include_dir = out_dir / "include" |
| 65 | + src_out_dir = out_dir / "src" |
| 66 | + include_dir.mkdir(parents=True, exist_ok=True) |
| 67 | + src_out_dir.mkdir(parents=True, exist_ok=True) |
| 68 | + for name in ENGINE_HEADERS: |
| 69 | + shutil.copy2(SRC_DIR / name, include_dir / name) |
| 70 | + for name in ENGINE_SOURCES: |
| 71 | + shutil.copy2(SRC_DIR / name, src_out_dir / name) |
| 72 | + |
| 73 | + |
| 74 | +def write_examples(out_dir: Path) -> None: |
| 75 | + examples_dir = out_dir / "examples" |
| 76 | + examples_dir.mkdir(parents=True, exist_ok=True) |
| 77 | + (examples_dir / "bouncing_ball.cpp").write_text(EXAMPLE_BOUNCING_BALL) |
| 78 | + embed_dir = examples_dir / "embedding" |
| 79 | + embed_dir.mkdir(exist_ok=True) |
| 80 | + (embed_dir / "app.h").write_text(EXAMPLE_EMBED_APP_H) |
| 81 | + (embed_dir / "app.cpp").write_text(EXAMPLE_EMBED_APP_CPP) |
| 82 | + (embed_dir / "main.cpp").write_text(EXAMPLE_EMBED_MAIN_CPP) |
| 83 | + |
| 84 | + |
| 85 | +def zip_directory(src_dir: Path, zip_path: Path) -> Path: |
| 86 | + """Zips src_dir's contents under a top-level folder named after src_dir, |
| 87 | + e.g. zipping .../dist/processing-cpp-dragdrop/ produces a zip where |
| 88 | + every entry starts with processing-cpp-dragdrop/ -- so unzipping it |
| 89 | + anywhere drops a single clean folder, not a pile of loose files.""" |
| 90 | + if zip_path.exists(): |
| 91 | + zip_path.unlink() |
| 92 | + zip_path.parent.mkdir(parents=True, exist_ok=True) |
| 93 | + with zipfile.ZipFile(zip_path, "w", zipfile.ZIP_DEFLATED) as zf: |
| 94 | + for path in sorted(src_dir.rglob("*")): |
| 95 | + if path.is_file(): |
| 96 | + zf.write(path, arcname=src_dir.name / path.relative_to(src_dir)) |
| 97 | + return zip_path |
| 98 | + |
| 99 | + |
| 100 | +# ============================================================================= |
| 101 | +# Example programs -- identical content in both packages. Only the build |
| 102 | +# instructions in each package's own README differ, which is genuinely |
| 103 | +# package-specific, unlike the engine-usage code itself. |
| 104 | +# ============================================================================= |
| 105 | + |
| 106 | +EXAMPLE_BOUNCING_BALL = '''\ |
| 107 | +// bouncing_ball.cpp -- a complete sketch, no Processing IDE involved. |
| 108 | +// Build: see this package's README.md. |
| 109 | +#include "Processing.h" |
| 110 | +
|
| 111 | +struct BouncingBall : public Processing::PApplet { |
| 112 | + float x = 0, y = 0; |
| 113 | + float vx = 180, vy = 140; |
| 114 | + float radius = 30; |
| 115 | +
|
| 116 | + void settings() override { |
| 117 | + size(640, 360); |
| 118 | + } |
| 119 | +
|
| 120 | + void setup() override { |
| 121 | + windowTitle("processing-cpp standalone example"); |
| 122 | + x = width / 2.0f; |
| 123 | + y = height / 2.0f; |
| 124 | + } |
| 125 | +
|
| 126 | + void draw() override { |
| 127 | + x += vx * deltaTime; |
| 128 | + y += vy * deltaTime; |
| 129 | +
|
| 130 | + if (x < radius || x > width - radius) vx = -vx; |
| 131 | + if (y < radius || y > height - radius) vy = -vy; |
| 132 | +
|
| 133 | + background(20); |
| 134 | + noStroke(); |
| 135 | + fill(255, 140, 0); |
| 136 | + circle(x, y, radius * 2); |
| 137 | + } |
| 138 | +
|
| 139 | + void keyPressed() override { |
| 140 | + if (key == ' ') { vx = -vx; vy = -vy; } |
| 141 | + } |
| 142 | +}; |
| 143 | +
|
| 144 | +int main() { |
| 145 | + BouncingBall sketch; |
| 146 | + sketch.run(); |
| 147 | + return 0; |
| 148 | +} |
| 149 | +''' |
| 150 | + |
| 151 | +EXAMPLE_EMBED_APP_H = '''\ |
| 152 | +// app.h -- the "drop into an existing project" pattern: keep the PApplet |
| 153 | +// subclass and the Processing.h include confined to app.cpp. The rest of |
| 154 | +// your codebase only ever needs to see this plain function declaration. |
| 155 | +#pragma once |
| 156 | +
|
| 157 | +void run_particle_view(); |
| 158 | +''' |
| 159 | + |
| 160 | +EXAMPLE_EMBED_APP_CPP = '''\ |
| 161 | +// app.cpp -- the only file in this example that touches Processing.h. |
| 162 | +#include "Processing.h" |
| 163 | +#include "app.h" |
| 164 | +#include <algorithm> |
| 165 | +#include <vector> |
| 166 | +
|
| 167 | +using namespace Processing; |
| 168 | +
|
| 169 | +namespace { |
| 170 | +
|
| 171 | +struct Particle { |
| 172 | + PVector pos; |
| 173 | + PVector vel; |
| 174 | + float life = 1.0f; |
| 175 | +
|
| 176 | + void draw() { |
| 177 | + fill(255, 200, 60, life * 255); |
| 178 | + circle(pos.x, pos.y, 8); |
| 179 | + } |
| 180 | +}; |
| 181 | +
|
| 182 | +struct ParticleView : public PApplet { |
| 183 | + std::vector<Particle> particles; |
| 184 | +
|
| 185 | + void settings() override { size(800, 500); } |
| 186 | + void setup() override { windowTitle("processing-cpp -- embedded in an existing project"); } |
| 187 | +
|
| 188 | + void spawn(float x, float y) { |
| 189 | + Particle p; |
| 190 | + p.pos = PVector(x, y); |
| 191 | + p.vel = PVector::random2D() * random(60.0f, 200.0f); |
| 192 | + particles.push_back(p); |
| 193 | + } |
| 194 | +
|
| 195 | + void draw() override { |
| 196 | + if (isMousePressed()) spawn(mouseX, mouseY); |
| 197 | +
|
| 198 | + background(0); |
| 199 | + noStroke(); |
| 200 | + for (auto& p : particles) { |
| 201 | + p.pos += p.vel * deltaTime; |
| 202 | + p.life -= deltaTime * 0.6f; |
| 203 | + p.draw(); |
| 204 | + } |
| 205 | + particles.erase( |
| 206 | + std::remove_if(particles.begin(), particles.end(), |
| 207 | + [](const Particle& p) { return p.life <= 0.0f; }), |
| 208 | + particles.end()); |
| 209 | + } |
| 210 | +}; |
| 211 | +
|
| 212 | +} // namespace |
| 213 | +
|
| 214 | +void run_particle_view() { |
| 215 | + ParticleView view; |
| 216 | + view.run(); |
| 217 | +} |
| 218 | +''' |
| 219 | + |
| 220 | +EXAMPLE_EMBED_MAIN_CPP = '''\ |
| 221 | +// main.cpp -- knows nothing about Processing, PApplet, GLFW, or GLEW. |
| 222 | +#include "app.h" |
| 223 | +
|
| 224 | +int main() { |
| 225 | + run_particle_view(); |
| 226 | + return 0; |
| 227 | +} |
| 228 | +''' |
0 commit comments