Skip to content

APIReference

Daynlight edited this page Dec 13, 2025 · 4 revisions

Graphite API Reference

This section is an overview of the classes and functions that Graphite has.

TOC

Scripts

All user scripts must implement the following interface:

Interface

class ScriptInterface {
public:
	virtual ~ScriptInterface() = default;
	virtual void Init() = 0;     // Called once at startup
	virtual void Update() = 0;   // Called every frame
	virtual void Draw() = 0;     // Called every frame for rendering
	virtual void Destroy() = 0;  // Called before unloading
};

Export functions for DLL:

extern "C" ScriptInterface* SCRIPT_API GetScript();
extern "C" void SCRIPT_API DeleteScript(ScriptInterface* script);

Script Example

// Graphite
// Copyright 2025 Daynlight
// Licensed under the Apache License, Version 2.0.
// See LICENSE file for details.


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

class Script : ScriptInterface{
	/* ... */ 				// Declarations

  void Init(){
		/* ... */			// Initialization
  };

  void Update(){
		/* ... */			// Update Per Frame
	};

  void Draw(){
		/* ... */			// Drawing on window
	};

  void Destroy(){
		/* ... */			// Destructor
  };
};



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;
};

DLL Loading

  • Scripts are compiled to shared libraries (Graphite.so)
  • Loaded/unloaded at runtime via dlopen, dlsym, dlclose
  • Functions: GetScript, DeleteScript

Error Handling

  • Use sandbox mode for safer script editing.
  • Always clean up in Destroy().
  • Verbose mode for debugging output.

More about writing scripts

Math Classes

Point

Point class is used inside script for drawing single point. It is than added to plot.

namespace Graphite::Math {
	class Point {
		Point(std::array<float, 2> pos, std::array<float, 3> color);
		std::array<float, 2> getPos();
		void setPos(std::array<float, 2> pos);
		std::array<float, 3> getColor();
		void setColor(std::array<float, 3> color);
	};
};

Plot2D

Plot is our 2D canvas where we draw elements. We add others Math classes to cells.

namespace Graphite::Math{
class Plot2D{
public:
  std::unordered_map<std::string, Graphite::Math::Point> point_cell;

private:
  void drawPoint(Graphite::Math::Point point);

public:
  Plot2D();
  ~Plot2D();

  void draw();

};
};

More about Math

AppRenderer

AppRenderer is wrapper for my CWindow library that contains functions require for Graphite.

namespace Graphite{
class AppRenderer{
private:
  CW::Renderer::iRenderer* renderer;

public:
  AppRenderer();
  ~AppRenderer();

  template<typename F>
  void renderFrame(F&& fun);
  bool isRunning();

};
};

ScriptController

ScriptController is used for Script compilation and hot-reloading also for sandbox mode via forks.

namespace Graphite{
class ScriptLoader{
private:
  std::string path = "";
  bool cant_find_file_print = 1;
  std::filesystem::file_time_type lastWriteTime{};
  
  void* script_handler = nullptr; 
  ScriptInterface* script = nullptr;

public:
  ScriptLoader(const std::string& path);
  ~ScriptLoader();
  
  bool checkLastWrite();
  void updateScript();

  int compile();
  int loadModule();
  void removeModule();

  void init();
  void update();
  void draw();
  void destroy();

};
};

App [Graphite.cpp, Graphite.h, Flags.h]

It is entry point for program, loads flags and start Graphite.

namespace Graphite{
class Graphite{
private:
  std::unordered_map<std::string, bool> flags;
  std::filesystem::path path = std::filesystem::path("./");
  
private:
  void initFile(const std::string& filename, const char* data);
  void init();
  void help();
  void verbose();
  void sandbox();

  void longFlags(int* i, int args, const char* argv[]);
  void shortFlags(int* i, int args, const char* argv[]);
  void detectPath(int* i, int args, const char* argv[]);
  void detectFlags(int args, const char* argv[]);
  void executeFlags();

public:
  void run(int args, const char* argv[]);
  void runProgram();

};
};

Flags

  • -h / --help: Show help.
  • -i / --init: Initialize default files.
  • -s / --sandbox: Enable sandbox mode (safe script execution).
  • -v / -d / --verbose / --debug: Enable verbose/debug output.

More Usage Information

Installer

GraphiteInstaller is small program for easy installation process.

Graphite Wiki Sidebar

Getting Started

References

Project & Versions

Community

Clone this wiki locally