The easiest way to do graphics programming in Chomik is to use the sdl_chomik program. It is delivered together with with chomik. When called only with the filename parameter it opens a window of the size 800x600 and executes the SDL_Chomik program stored in the file.
sdl_chomik program.sdl_chomik
sdl_chomik -w 1024 -h 768 program.sdl_chomik
sdl_chomik --width 1024 --height 768 program.sdl_chomik
There is an 'sdl loop' code variable. This variable is predefined and is actually implemented in C++. The most simple SDL Chomik program is:
<sdl loop>;
let draw background = value code
{
};
let sdl loop body = value code
{
<draw background>;
};
<sdl loop>;
In a real program you will first load some images and use them to provide a background, maybe some players and other objects. Remember, SDL Chomik inherits all the commands from Chomik, and so you can mix the SDL Chomik specific commands with the normal Chomik commands. In order to create an image from a PNG file we use the 'create new image "..."' family of variables. But still, you can use 'create ...' to create streams and other things.
The 'show image' family of code variables is also implemented in C++. You can use it in 'sdl loop body' (or any code called by it) to show the image of the given index at the coordinates X and Y.
<create new image "background_tile.png">;
variable background tile image index: integer;
let background tile image index = <the created image index>;
let draw background = value code
{
<show image <background tile image index> 0 0>;
};
let sdl loop body = value code
{
<draw background>;
};
<sdl loop>;
You can assign the build-in code variables 'on key left','on key right','on key up' and 'on key down' to handle the key pressed events.
let on key left = value code
{
<print "left">;
};
let on key right = value code
{
<print "right">;
};
let on key up = value code
{
<print "up">;
};
let on key down = value code
{
<print "down">;
};
<sdl loop>;
what if we want the text to appear in the graphical window? We can do it, but first we need to load a font from a file. This is done using 'create new font "..."' family of variables. A text is shown when you use 'show text <font index> "..." <x coordinate> <y coordinate>' family of variables.
<create new image "title_screen.png">;
let title screen image index = <the created image index>;
<create new image "chomik.png">; # "chomik" means a 'hamster' in Polish
chomik image index: integer;
let chomik image index = <the created image index>;
<create new font "PlayfairDisplay-Regular.ttf" 32>; # creating a font occurs here!!!
let my font index = <the created font index>;
let sdl loop body = value code
{
<show image <title screen image index> 0 0>;
<show image <chomik image index> 0 0>;
<show text <my font index> "hello world!" 50 100>; # here we show the text "hello world"
};
<sdl loop>;
You can use the built-in variable 'sdl quit' in order to quit the sdl loop, for example (as shown below) on the escape key pressed.
let on key escape = value code { <sdl quit>; };
<sdl loop>;
let on mouse button left = value code
{
<print "mouse button left">;
};
let on mouse button right = value code
{
<print "mouse button right">;
};
<sdl loop>;
let on mouse motion = value code
{
<print "mouse motion at" <the mouse x> <the mouse y>>;
};
<sdl loop>;