Skip to content

Commit

Permalink
Add optimization tips (Application Behavior.md).
Browse files Browse the repository at this point in the history
  • Loading branch information
m1maker committed Oct 26, 2024
1 parent 8bce787 commit 4d4fac8
Show file tree
Hide file tree
Showing 2 changed files with 123 additions and 0 deletions.
93 changes: 93 additions & 0 deletions docs/General/Advance.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
# NGT Advance Tutorial
Once you have read the getting started tutorial, here is the Tutorial with more advance engine knowledge. I made 2 parts so any of you can decide whether to stop at getting started Tutorial or to take your engine knowledge to the advance level.

## Engine exceptions
engine exceptions are errors that catched using runtime error. You cannot possibly imagine that you can catch any runtime errors and avoid from being stopped execution, however here you really can!

### Throwing errors
The first thing we gonna learn is how to throw an exception, meaning to execute a runtime error.

To execute a heavy attack, I mean, the exception, you will use the throw function, like this.

```NGT copy
void main()
{
throw("Engine died so go away");
}
```

After execution of the code, you will see that a runtime error had occured. To avoid this, we will catch it with try catch block.

```
void main()
{
try
{
vector@ v;
@v = null;
alert("vector",""+v.x);
}
catch
{
alert("error", get_exception_info());
}
alert("engine","now exiting...");
}
```

Without try catch block, this will throw runtime error, because we're accessing the null object. But as you can see, the engine does not cause the runtime error and stop execution. Instead, it skippeds to the next code, more like continue we learned in the loops chapter, and manually exits the program.

## Any, datatype
An `any` type is used to point to any type of value. This is best to use in areas where you don't know exactly what type or value will be stored.

An `any` is an object

The following functions should be note before you read into examples.

* `void store(?value);` which stores the value of any kind.
* `bool retrieve(?&out value);` which trys to retrieve the value of the object.

Constructor

`any(?value);`

Now, lets dive into example. Lets say we have to use the speak function provided by NGT, but we stuck here because you want to speak many types, including strings, ints, floats, or possibly objects.

We will first implement the custom_speak function that supports string, int, float, bool.
Please note that in latest development, string accepts numbers passing too, so you don't have to actually implement this logic.

Note. If you want numbers of all types, using int and double is enough.

```
void custom_speak(any@ t, bool interrupt=false)
{
if(t is null) return; //Null shouldn't even used here.
string text; //Actual variable of a converted string, since the speak function accepts string.
string r_str; //Lets retrieve the string.
if(t.retrieve(r_str)) text=r_str;
//If the string fail to retrieve, lets go to int and double, one after the other.
int r_int;
if(t.retrieve(r_int)) text=""+r_int;
double r_db;
if(t.retrieve(r_db)) text=""+r_db;
//If either of above types is failed, lets retrieve bool.
bool r_bool;
if(t.retrieve(r_bool)) text=(r_bool?"true":"false");
screen_reader::speak(text, interrupt); //Finally speaks the text.
}
```

Now, we've defined the speak custom function, with supporting string, bool, intiger and floatingpoint. Next, we'll use `any` object before specifying the t parameter in that speak function.

```
void main()
{
custom_speak(any("hello"),true); //String.
custom_speak(any(233),false); //Int
custom_speak(any(23.21),false); //Floatingpoint.
custom_speak(any(true),false); //Bool.
}
```

Yep, you found that useful, aren't you?

30 changes: 30 additions & 0 deletions docs/General/Application Behavior.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
# Application Behavior

## Window

To display a window, use the show_window function. If you plan to utilize rendering, make sure to set the enable_renderer flag to true in show_window; otherwise, no graphics will be displayed.

## Application Loop

Use
```
while (!quit_requested);
```
to create a loop that continues until the user exits the program. To optimize the application's loop and prevent the CPU from being overloaded, consider using
```
wait(5);
```
if background operations need to be performed alongside window management. Alternatively, use
```
wait_event();
```
if your program should completely wait for user events.

Important: It is essential to call one of these functions if rendering is enabled; failing to do so may cause the window to become unresponsive.

You can modify the value of `update_window_freq` (which controls the screen refresh rate in milliseconds) at any time. However, be aware that increasing this frequency may slow down event processing. Use window_present when you have completed all scenes for rendering.

## Exiting

When a quit request event is received from the user, the loop is terminated gracefully. This is the preferred method for exiting the program, as it allows automatic control objects to call their destructors and free resources properly. Avoid overusing the exit function unless there is a compelling reason to terminate the program unexpectedly. If you need to return a return code, declare main as int main.

0 comments on commit 4d4fac8

Please sign in to comment.