-
Notifications
You must be signed in to change notification settings - Fork 0
Examples
Ryan Srichai edited this page Sep 17, 2025
·
12 revisions
This page contains the full source code for examples using turtle's features. See this page to learn how to build and run a project.
#define TURTLE_IMPLEMENTATION // exclude this line if compiling with -lturtle
#include "turtle.h"
int main(int argc, char *argv[]) {
int32_t numberOfPrimes = 100;
list_t *primes = list_init();
list_append(primes, (unitype) 2, 'i');
int32_t checkNumber = 3;
while (primes -> length < numberOfPrimes) {
int8_t isPrime = 1;
for (int32_t j = 0; j < primes -> length; j++) {
if (primes -> data[j].i > sqrt(checkNumber)) {
break;
}
if (checkNumber % primes -> data[j].i == 0) {
isPrime = 0;
break;
}
}
if (isPrime) {
list_append(primes, (unitype) checkNumber, 'i');
}
checkNumber++;
}
list_print(primes);
}#define TURTLE_IMPLEMENTATION // exclude this line if compiling with -lturtle
#include "turtle.h"
#include <time.h>
int main(int argc, char *argv[]) {
/* Initialise glfw */
if (!glfwInit()) {
return -1;
}
glfwWindowHint(GLFW_SAMPLES, 4); // MSAA (Anti-Aliasing) with 4 samples (must be done before window is created)
/* Create a windowed mode window and its OpenGL context */
const GLFWvidmode *monitorSize = glfwGetVideoMode(glfwGetPrimaryMonitor());
int32_t windowHeight = monitorSize -> height;
GLFWwindow *window = glfwCreateWindow(windowHeight * 16 / 9, windowHeight, "turtle demo", NULL, NULL);
if (!window) {
glfwTerminate();
return -1;
}
glfwMakeContextCurrent(window);
glfwSetWindowSizeLimits(window, windowHeight * 16 / 9 * 0.4, windowHeight * 0.4, windowHeight * 16 / 9, windowHeight);
/* initialise turtle */
turtleInit(window, -320, -180, 320, 180);
glfwSetWindowSize(window, windowHeight * 16 / 9 * 0.85, monitorSize -> height * 0.85); // doing it this way ensures the window spawns in the top left of the monitor and fixes resizing limits
uint32_t tps = 60; // ticks per second (locked to fps in this case)
uint64_t tick = 0; // count number of ticks since application started
clock_t start, end;
turtlePenColor(0, 0, 0); // set color to black
turtlePenSize(10); // set size to 10
while (turtle.close == 0) {
start = clock();
turtleGetMouseCoords();
turtleGoto(turtle.mouseX, turtle.mouseY);
if (turtleMouseDown()) {
turtlePenDown();
} else {
turtlePenUp();
}
turtleUpdate(); // update the screen
end = clock();
while ((double) (end - start) / CLOCKS_PER_SEC < (1.0 / tps)) {
end = clock();
}
tick++;
}
turtleFree();
glfwTerminate();
}#define TURTLE_IMPLEMENTATION // exclude this line if compiling with -lturtle
#include "turtle.h"
#include <time.h>
int main(int argc, char *argv[]) {
/* Initialise glfw */
if (!glfwInit()) {
return -1;
}
glfwWindowHint(GLFW_SAMPLES, 4); // MSAA (Anti-Aliasing) with 4 samples (must be done before window is created)
/* Create a windowed mode window and its OpenGL context */
const GLFWvidmode *monitorSize = glfwGetVideoMode(glfwGetPrimaryMonitor());
int32_t windowHeight = monitorSize -> height;
GLFWwindow *window = glfwCreateWindow(windowHeight * 16 / 9, windowHeight, "turtle demo", NULL, NULL);
if (!window) {
glfwTerminate();
return -1;
}
glfwMakeContextCurrent(window);
glfwSetWindowSizeLimits(window, windowHeight * 16 / 9 * 0.4, windowHeight * 0.4, windowHeight * 16 / 9, windowHeight);
/* initialise turtle */
turtleInit(window, -320, -180, 320, 180);
glfwSetWindowSize(window, windowHeight * 16 / 9 * 0.85, monitorSize -> height * 0.85); // doing it this way ensures the window spawns in the top left of the monitor and fixes resizing limits
uint32_t tps = 60; // ticks per second (locked to fps in this case)
uint64_t tick = 0; // count number of ticks since application started
clock_t start, end;
turtlePenColor(0, 0, 0); // set color to black
turtlePenSize(50); // set size to 10
double ballX = 0;
double ballY = 0;
double ballSpeed = 5.0;
while (turtle.close == 0) {
start = clock();
if (turtleKeyPressed(GLFW_KEY_UP)) {
ballY += ballSpeed;
}
if (turtleKeyPressed(GLFW_KEY_DOWN)) {
ballY -= ballSpeed;
}
if (turtleKeyPressed(GLFW_KEY_RIGHT)) {
ballX += ballSpeed;
}
if (turtleKeyPressed(GLFW_KEY_LEFT)) {
ballX -= ballSpeed;
}
turtleClear(); // clear the screen
turtleGoto(ballX, ballY);
turtlePenDown();
turtlePenUp();
turtleUpdate(); // update the screen
end = clock();
while ((double) (end - start) / CLOCKS_PER_SEC < (1.0 / tps)) {
end = clock();
}
tick++;
}
turtleFree();
glfwTerminate();
}#define TURTLE_IMPLEMENTATION // exclude this line if compiling with -lturtle
#include "turtle.h"
#include <time.h>
int main(int argc, char *argv[]) {
/* Initialise glfw */
if (!glfwInit()) {
return -1;
}
glfwWindowHint(GLFW_SAMPLES, 4); // MSAA (Anti-Aliasing) with 4 samples (must be done before window is created)
/* Create a windowed mode window and its OpenGL context */
const GLFWvidmode *monitorSize = glfwGetVideoMode(glfwGetPrimaryMonitor());
int32_t windowHeight = monitorSize -> height;
GLFWwindow *window = glfwCreateWindow(windowHeight * 16 / 9, windowHeight, "turtle demo", NULL, NULL);
if (!window) {
glfwTerminate();
return -1;
}
glfwMakeContextCurrent(window);
glfwSetWindowSizeLimits(window, windowHeight * 16 / 9 * 0.4, windowHeight * 0.4, windowHeight * 16 / 9, windowHeight);
/* initialise turtle */
turtleInit(window, -320, -180, 320, 180);
glfwSetWindowSize(window, windowHeight * 16 / 9 * 0.85, monitorSize -> height * 0.85); // doing it this way ensures the window spawns in the top left of the monitor and fixes resizing limits
/* initialise turtleText */
turtleTextInit("config/roberto.tgl");
uint32_t tps = 60; // ticks per second (locked to fps in this case)
uint64_t tick = 0; // count number of ticks since application started
clock_t start, end;
turtlePenColor(0, 0, 0);
turtlePenSize(0.5);
while (turtle.close == 0) {
start = clock();
turtleClear(); // clear the screen
turtleGetMouseCoords();
turtleGoto(turtle.mouseX - 3, turtle.mouseY);
turtlePenDown();
turtleGoto(turtle.mouseX + 3, turtle.mouseY);
turtlePenUp();
turtleGoto(turtle.mouseX, turtle.mouseY - 3);
turtlePenDown();
turtleGoto(turtle.mouseX, turtle.mouseY + 3);
turtlePenUp();
turtleTextWriteStringf(turtle.mouseX + 2, turtle.mouseY + 6, 5, 0, "%.2lf, %.2lf", turtle.mouseX, turtle.mouseY);
turtleUpdate(); // update the screen
end = clock();
while ((double) (end - start) / CLOCKS_PER_SEC < (1.0 / tps)) {
end = clock();
}
tick++;
}
turtleFree();
glfwTerminate();
}#define TURTLE_IMPLEMENTATION // exclude this line if compiling with -lturtle
#include "turtle.h"
#include <time.h>
int main(int argc, char *argv[]) {
/* Initialise glfw */
if (!glfwInit()) {
return -1;
}
glfwWindowHint(GLFW_SAMPLES, 4); // MSAA (Anti-Aliasing) with 4 samples (must be done before window is created)
/* Create a windowed mode window and its OpenGL context */
const GLFWvidmode *monitorSize = glfwGetVideoMode(glfwGetPrimaryMonitor());
int32_t windowHeight = monitorSize -> height;
GLFWwindow *window = glfwCreateWindow(windowHeight * 16 / 9, windowHeight, "turtle demo", NULL, NULL);
if (!window) {
glfwTerminate();
return -1;
}
glfwMakeContextCurrent(window);
glfwSetWindowSizeLimits(window, windowHeight * 16 / 9 * 0.4, windowHeight * 0.4, windowHeight * 16 / 9, windowHeight);
/* initialise turtle */
turtleInit(window, -320, -180, 320, 180);
glfwSetWindowSize(window, windowHeight * 16 / 9 * 0.85, monitorSize -> height * 0.85); // doing it this way ensures the window spawns in the top left of the monitor and fixes resizing limits
/* initialise turtleText */
turtleTextInit("config/roberto.tgl");
uint32_t tps = 60; // ticks per second (locked to fps in this case)
uint64_t tick = 0; // count number of ticks since application started
clock_t start, end;
char dialog[][128] = {
"Hey it's nice to meet you",
"This world is imperfect.",
"What",
"If only I could wipe away the impurities,",
"Is anybody else listening to this",
"and make it as beautiful as me!",
"14 hours of gameplay later",
"Lysanderoth! You were behind all this?",
"Yes it was I. My machinations lay undetected for years for I am a master of deception",
};
int32_t dialogLength = sizeof(dialog) / 128;
int32_t dialogIndex = 0;
int32_t mouseFlag = 0;
while (turtle.close == 0) {
start = clock();
turtleClear(); // clear the screen
turtlePenColor(60, 60, 60);
turtleRectangle(-320, -110, 320, -180);
if (dialogIndex == 0 || dialogIndex == 2 || dialogIndex == 4) {
turtlePenColor(140, 140, 255);
} else if (dialogIndex == 6) {
turtlePenColor(160, 160, 160);
} else if (dialogIndex == 7) {
turtlePenColor(255, 40, 140);
} else {
turtlePenColor(255, 80, 80);
}
turtleTextWriteString(dialog[dialogIndex], -305, -130, 12, 0);
if (turtleMouseDown()) {
if (mouseFlag == 0) {
dialogIndex++;
mouseFlag = 1;
if (dialogIndex >= dialogLength) {
dialogIndex = 0;
}
}
} else {
mouseFlag = 0;
}
turtleUpdate(); // update the screen
end = clock();
while ((double) (end - start) / CLOCKS_PER_SEC < (1.0 / tps)) {
end = clock();
}
tick++;
}
turtleFree();
glfwTerminate();
}