Skip to content

Hello World!

Anaël Seghezzi edited this page Mar 22, 2020 · 1 revision

C-Toy expects a main file in src/main.c.
But instead of the standad C "main" function, the entry points are "ctoy_begin", "ctoy_main_loop" and "ctoy_end".

The compulsory "Hello, World!" program is then (in src/main.c):

#include <ctoy.h> // ctoy API (including frequently used ANSI C libs)

void ctoy_begin() // called at the beginning of the program
{
   printf("Hello, World!\n");
}

void ctoy_main_loop() // called at every update of the main loop
{}

void ctoy_end() // called at the end of the program
{}

Let's also change the window's title:

#include <ctoy.h>

void ctoy_begin(void)
{
   printf("Hello World!\n");
   ctoy_window_title("Hello World!"); // change CToy window's title
}

void ctoy_end(void)
{}

void ctoy_main_loop(void)
{}

Now let's do some standard C and write a text file:

#include <ctoy.h>
#include <stdio.h> // optional (stdio.h is already included in ctoy.h)

void ctoy_begin(void)
{
   printf("Hello World!\n");
   ctoy_window_title("Hello World!");

   // create a text file and write "Hello World!"
   FILE *my_file = fopen("data/hello.txt", "wt");
   if (my_file) {
      fprintf(my_file, "Hello World!");
      fclose(my_file);
   }
}

void ctoy_end(void)
{}

void ctoy_main_loop(void)
{}
Clone this wiki locally