Skip to content

Examples

Daynlight edited this page Dec 8, 2025 · 5 revisions

Graphite Examples

๐Ÿšง Note: More advanced and 3D examples are planned! See Roadmap.md for future updates.

Here are some example scripts and usage patterns for Graphite. You can find these in the Examples folder of the repository.


Example 1: Sin Curve

Visualizes a sine wave using points.

#define BUILDING_SCRIPT_DLL
#include <Graphite/ScriptInterface.h>
#include <Graphite/Math.h>
#include <math.h>

#define SAMPLES 1000

class Script : ScriptInterface {
	Graphite::Math::Point points[SAMPLES];
	float f(float x) { return (1/2.0f) * sin(x * M_PI * 2); }

	void Init() {
		for(int i = 0; i < SAMPLES; i++) {
			float x = (i/(SAMPLES - 1.0f) * 2) - 1;
			float y = f(x);
			points[i].setPos({x, y});
		}
	}
	void Update() {}
	void Draw() {
		for(int i = 0; i < SAMPLES; i++)
			points[i].drawPoint();
	}
	void Destroy() {}
};

extern "C" ScriptInterface* SCRIPT_API GetScript() {
	Script* script = new Script();
	return (ScriptInterface*)script;
}
extern "C" void SCRIPT_API DeleteScript(ScriptInterface* script) {
	Script* temp_script = (Script*)script;
	delete temp_script;
}

Example 2: Bezier Curve

Visualizes a quadratic Bezier curve.

#define BUILDING_SCRIPT_DLL
#include <Graphite/ScriptInterface.h>
#include <Graphite/Math.h>
#include <math.h>

#define SAMPLES 200

class Script : ScriptInterface {
	Graphite::Math::Point control_points[3] = {{{-0.7, 0.7}, {0.0, 0.0, 1.0}},
																						 {{0.7, -0.5}, {0.0, 0.0, 1.0}},
																						 {{0.9, 0.4}, {0.0, 0.0, 1.0}}};
	Graphite::Math::Point points[SAMPLES];

	std::array<float, 2> bezier(float t) {
		float u = 1.0f - t;
		std::array<float, 2> pos1 = control_points[0].getPos();
		std::array<float, 2> pos2 = control_points[1].getPos();
		std::array<float, 2> pos3 = control_points[2].getPos();
		float x = u*u*pos1[0] + 2*u*t*pos2[0] + t*t*pos3[0];
		float y = u*u*pos1[1] + 2*u*t*pos2[1] + t*t*pos3[1];
		return {x, y};
	}

	void Init() {
		for(int i = 0; i < SAMPLES; i++) {
			auto P = bezier((i * 1.0f) / SAMPLES);
			points[i].setPos(P);
		}
	}
	void Update() {}
	void Draw() {
		for(int i = 0; i < 3; i++)
			control_points[i].drawPoint();
		for(int i = 0; i < SAMPLES; i++)
			points[i].drawPoint();
	}
	void Destroy() {}
};

extern "C" ScriptInterface* SCRIPT_API GetScript() {
	Script* script = new Script();
	return (ScriptInterface*)script;
}
extern "C" void SCRIPT_API DeleteScript(ScriptInterface* script) {
	Script* temp_script = (Script*)script;
	delete temp_script;
}

How to Run

  1. Place your script in the project directory as Graphite.cpp.
  2. Build and run Graphite:
    Graphite <flags> <path>
  3. Use flags like -h, -i, -s, -v for help, init, sandbox, and verbose modes.

See the repository and Examples folder for more sample scripts.

Graphite Wiki Sidebar

Getting Started

References

Project & Versions

Community

Clone this wiki locally