📜 ⬆️ ⬇️

Writing “Hello world!” For KolibriOS on C

Many people ask themselves: “Is it possible to write a C program under KolibriOS?”
Answer: “Yes, you can!” , And below I will tell you how to do it.


To write the program, we need:

Some theory

In KolibriOS there are two types of programs: window and console. A GUI program is written using system functions and the BoxLib library. The console program is written using the console.obj library. Work with libraries will be discussed in the following articles; Today we will write a window application based on system functions.

A windowing application consists of a main () function, in which there is a special message handler:
')
while (!0) { switch (_ksys_wait_for_event(10)) { case 2: return 0; case 3: if (_ksys_get_button_id() == 1) return 0; break; default: //  ,      . break; } } } 

In our program, we will handle only keyboard events and button presses. They are processed in a while loop using switch - case.

Let's sort these events:
2 - key is pressed on the keyboard;
3 - the button, the button defined earlier is pressed (in this example it is only a cross of the program’s close, which is the button created together with the window);
More details about the event system can be found on our Wiki or in the documentation supplied with the OS (Docpack program on the desktop).

For ease of development, we will move the commands that are responsible for the appearance of the window into a separate function, draw_window () .

Now it's time to figure out how to draw a window.
Drawing a window starts with the _ksys_window_redraw (1) function, and ends with _ksys_window_redraw (2) . To draw a window, you need to use the _ksys_draw_window function (window coordinate in x, window coordinate in y, width, height, color, window type, window title color, whether the window is not relocatable, window frame color);

Types of windows:
0 - type I - fixed-size window (no skin)
1 - only define the window area, draw nothing
2 - type II - resizable window (without skin)
3 - window with skin (resizable)
4 - window with a fixed size skin

And finally, to draw text, you need to use: _ksys_write_text (x coordinate, y coordinate, font type, string, string length);

Now you can proceed to the code itself

You can write the program directly in KolibriOS. Open Tinypad , and write this code:

 #include <stdio.h> #include <string.h> #include <kolibrisys.h> #define FONT0 0 #define FONT1 0x10000000 char header[] = {"Hello World!"}; #define BT_NORMAL 0 #define BT_DEL 0x80000000 #define BT_HIDE 0x40000000 #define BT_NOFRAME 0x20000000 void draw_window () { _ksys_window_redraw(1); _ksys_draw_window(100, 100, 300, 120, 0xaabbcc, 4, 0x5080d0, 0, 0x5080d0); _ksys_write_text(50,30,FONT0, header, strlen(header)); _ksys_window_redraw(2); } int main (int argc, char **argv) { while (!0) { switch (_ksys_wait_for_event(10)) { case 2: return 0; case 3: if (_ksys_get_button_id() == 1) return 0; break; default: draw_window(); break; } } } 

Now it's time to compile!

If you did not write the code in Hummingbird, reset it to a USB flash drive along with the compiler . For example, I save the file as hello.c .
For people who can not install Hummingbirds, you can connect a USB flash drive to a virtual machine:

image

To compile, open in the console ( Shell ) the directory where the compiler is located, and enter this command
ktcc.kex <__> libck.a –o <__>

If everything is done correctly, we will see our program in the same directory. Now you can run it.
And we get the following picture:

image

For those who do not like to read a lot, video tutorial on the topic:


I repeat that you can discuss ktcc (and programs for it) on our forum .

Source: https://habr.com/ru/post/212169/


All Articles