chomik.tech

SDL Chomik

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

It is also possible to run it as follows:

sdl_chomik -w 1024 -h 768 program.sdl_chomik

The longer form of the command line arguments is also allowed:

sdl_chomik --width 1024 --height 768 program.sdl_chomik

You may use -w/--width to set the width of the graphic window and -h/--height to set its height. These values are available in your SDL Chomik program through the built-in integer variables:
  • the sdl window width
  • the sdl window height
You don't need to declare them, they simply exist.

Writing an SDL Chomik program

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


The above snippet opens a window and enters the 'sdl loop'. On the other hand the 'sdl loop' checks for the built-in code variable 'sdl loop body'. You may redefine the latter. A typical SDL Chomik program will first assign the 'sdl loop body' and then call the 'sdl loop'.


let draw background = value code
{
};

let sdl loop body = value code
{
    <draw background>;
};

<sdl loop>;


In the above snippet the code 'draw background' (the name is arbitrary) is empty, and so there is no background in the window.

Loading images

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


Handling keys in SDL Chomik

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


Note that the messages will NOT be printed in the graphical window but in the terminal window. The other keys you can handle are return (enter), backspace, space and escape. You can handle them similarly defining the built-in code variables 'on key return', 'on key backspace', 'on key space' and 'on key escape'.

Fonts and text in SDL Chomik

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


Putting the hamster image in the left top corner is recommended, but not necessary. The image 'chomik.png' is public domain and is distributed with chomik (in the folder 'image').

sdl quit

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


Mouse buttons handling



let on mouse button left = value code
{
    <print "mouse button left">;
};

let on mouse button right = value code
{
    <print "mouse button right">;
};

<sdl loop>;


If you want to know the coordinates x and y (where on the screen the left or right mouse button was clicked) you can use the built-in integer variables 'the mouse x' and 'the mouse y'.

Mouse motion handling



let on mouse motion = value code
{
    <print "mouse motion at" <the mouse x> <the mouse y>>;
};

<sdl loop>;