← All chapters
Cookbook · Chapter 1

Getting started

The shape of every sketch, windows, the coordinate system, math helpers, and input.

Umfeld is a C++ environment with a Processing-style API. A program is built from the same three functions you know from Processing — only the language underneath is C++.

The sketch skeleton

settings() configures the window, setup() runs once, draw() runs every frame.

#include "Umfeld.h"

using namespace umfeld;

void settings() {
    size(1024, 768);   // set the window size here, not in setup()
}

void setup() {
    // runs once
}

void draw() {
    background(216);    // clear to light grey each frame
    circle(mouseX, mouseY, 80);
}

Two lines do the bridging: #include "Umfeld.h" pulls in the API, and using namespace umfeld; lets you write circle() instead of umfeld::circle(). Build it with CMake — see Build and run at the end, or the install guide.

Note for Processing users: Umfeld is available as an official mode in the Processing PDE; see the installation guide. You can also use the standalone workflow shown here: edit .cpp files in any editor (VS Code, CLion, …) and build with CMake. In both workflows the code is C++, so types are explicit — you write int, float, and const where Java Mode may infer or omit them.

The coordinate system

The origin (0, 0) is the top-left corner. X grows right, Y grows down. width and height hold the window size, so the centre is always (width / 2, height / 2).

line(0, 0, width, height);          // corner to corner
circle(width / 2, height / 2, 100); // centred

Printing to the console

println() and console() both take several comma-separated values:

println("frame:", frameCount, " mouse:", mouseX, mouseY);

Math functions

The Processing math helpers are all here:

float m = map(mouseX, 0, width, 0, 100);  // remap one range to another
float c = constrain(m, 0, 50);            // clamp into [0, 50]
float r = random(width);                  // 0 .. width
float r2 = random(-1, 1);                 // a range
float d = dist(x1, y1, x2, y2);           // distance
float a = atan2(dy, dx);                  // angle of a vector
float n = noise(xoff);                    // Perlin noise (1D / 2D / 3D)

Angles are radians. Convert with radians() / degrees(), and reach for the constants PI, HALF_PI, QUARTER_PI, TWO_PI.

Responding to the mouse

mouseX / mouseY track the cursor; isMousePressed is true while a button is held. Override the callbacks for one-off events — the names match Processing:

void mousePressed()  { /* a button went down */ }
void mouseReleased() { /* came back up        */ }
void mouseDragged()  { /* moved while pressed  */ }

Responding to the keyboard

void keyPressed() {
    println("pressed:", (char) key);
    if (key == ' ') saveFrame();
    if (key == '1') background(255);
}

void keyReleased() {
    println("released:", (char) key);
}

The global key holds the character; isKeyPressed is true while any key is down. To stop the OS auto-repeating a held key, set this in settings():

void settings() {
    size(1024, 768);
    config.ignore_key_repeat(true);
}

Opening additional windows

The optional multi-window library creates secondary windows that share the main sketch’s draw loop and graphics resources. Create them from setup() or later, then draw through each window’s graphics canvas:

#include "MultiWindow.h"

PWindow* panel = nullptr;

void setup() {
    panel = createWindow(420, 300, "controls");
}

void draw() {
    background(255);                   // main window

    if (panel != nullptr) {
        panel->graphics->background(0);
        panel->graphics->fill(255);
        panel->graphics->circle(panel->mouseX, panel->mouseY, 40);
    }
}

Always check the returned pointer: unsupported renderers and single-window platforms return nullptr. The library supports OpenGL desktop targets and desktop OpenGL ES, including Raspberry Pi; iOS, Android, WebAssembly, SDL2D, and terminal rendering remain single-window. See the multi-window setup and API guide for CMake integration, callbacks, window placement, and fullscreen controls.

Build and run

Each sketch is a folder with one application.cpp and a CMakeLists.txt. Start from the umfeld-application template so your project uses the same build configuration as Umfeld itself (currently CMake 3.20 and C++17):

git clone https://codeberg.org/Umfeld/umfeld-application my_sketch
cd my_sketch
export UMFELD_PATH="/path/to/umfeld"

Change project(template) to project(my_sketch) in CMakeLists.txt, replace the starter application.cpp with your sketch, then build:

cmake -B build       # configure (once, or after editing CMakeLists.txt)
cmake --build build  # compile
./build/my_sketch    # run

The browser tools use this same application template when exporting projects.