-
Notifications
You must be signed in to change notification settings - Fork 1
tutorials examples
Getting Started with Echlib In this section, we will learn how to use Echlib by creating simple projects. First, we will make a basic window to get started. Let's dive in!
- Setting Up the Window To begin with, you need to include Echlib in your project. To do this, use the following line of code at the top of your file:
#include "echlib.h"
📌 Note:
When including libraries, Echlib should be included with double quotes like this: "echlib.h"
, not with angle brackets (< >). The angle brackets (< >) are reserved for system libraries like #include <iostream
> or #include <cmath>
.
- Creating the Window
Once Echlib is included, we can create the window using the function
ech::MakeWindow
Here’s how you can do it:
ech::MakeWindow(width, height, "Window Name");
For example, if you want to create a 640x480 window named "My First Window", you would write:
ech::MakeWindow(640, 480, "My First Window");
This will initialize the window with the specified width, height, and title.
- Setting the Maximum FPS Now that we have the window, it’s important to set a target frame rate. Why? If you don’t set the target FPS, some devices might run the game faster than others, which can lead to inconsistent performance.
To avoid this, we use the SetTargetFps
function. Let’s set the FPS to 60 for smooth gameplay. Here's how:
ech::SetTargetFps(60);
This ensures that your game will run at a constant 60 frames per second, regardless of the device it is running on.
- Main Game Loop With the window created and FPS set, it's time to create the main game loop. The game loop will keep the window open and continually update the screen. You can use the following structure:
while (!ech::WindowShouldClose()) {
// Start drawing
ech::StartDrawing();
// Clear the background with a color
ech::ClearBackground(ech::WHITE);
// Add your game logic and drawing code here
// End drawing
ech::EndDrawing();
}
In this loop:
ech::StartDrawing() tells the program to start rendering the scene.
ech::ClearBackground(ech::WHITE) clears the screen with a white background.
ech::EndDrawing() finishes the drawing process for this frame.
5. Closing the Window
Once you exit the game loop, it's time to close the window properly. Use ```ech::CloseWindow()``` to clean up resources and close the window:
```ech::CloseWindow();
Full Example: Here's the complete code for creating a basic window:
#include "echlib.h" // Include Echlib
int main() {
// Initialize the window
ech::MakeWindow(640, 480, "My First Window");
// Set the target FPS to 60
ech::SetTargetFps(60);
// Main game loop
while (!ech::WindowShouldClose()) {
ech::StartDrawing(); // Start drawing
ech::ClearBackground(ech::WHITE); // Clear the background
// Additional game logic and rendering can go here
ech::EndDrawing(); // End drawing
}
// Close the window
ech::CloseWindow();
return 0;
}
This example creates a simple window and keeps it open until you close it. Now that you know how to create a window, you can start building more features into your game! 🎮
next:
Yo