-
Notifications
You must be signed in to change notification settings - Fork 0
Your first code with Zap
[This is also used in the Readme.]
Let's start with a simple code:
#include <Zap.h>
int main()
{
zap::Init();
zap::Window window(1290, 720, "Hello Window");
zap::InitGlad();
window.UpdateViewport(true);
while (window.Open())
{
zap::ClearBackground(zap::BackgroundColors::ORANGE);
if (window.GetInput(zap::Key::ESC, zap::State::PRESSED))
{
window.Close();
}
window.Update();
window.Draw();
}
zap::Delete();
}
zap::Init();
zap::Delete();You use these functions to manage the APIs OpenGL resources. Every call from Zap must be called between these two functions. Init selects OpenGL 3.3 if you call it without parameters. If you want to use a newer version you can fill in the major and minor version as first and second parameter.
zap::Init(4, 6) // OpenGL 4.6zap::Window window(1290, 720, "Hello Window");We create a new Window class here. We specify the width and height and the Title of the window. If you also provide a GLFWmonitor the window starts in Fullscreen.
You can also create a a GLFWwindow Instance manually and then give it to the window. (Managing it is then handled by the API) :
// OpenGL includes here...
#include "Zap.h"
int main()
{
zap::Init();
GLFWwindow* native_window = glfwCreateWindow(600, 600, "Native Window", NULL, NULL);
glfwMakeContextCurrent(native_window); // You need to call that yourself
zap::InitGlad();
zap::Window window(native_window);
//Loop...
}
Please be aware of the base resolution of your window. For example we created a window with a resolution/ scale of 1290x720 pixels. So this would be our base resolution. If we maximize the window on a FullHD Monitor the new scale in pixel of our window would be 1920x1080. But the content is upscaled. Zap sometimes needs the original intended resolution of your window to scale some of its elements properly. In the default zap::Window class constructor the base resolution is handled automatically but if you provide a extern GLFWwindow you need to specify the base resolution. If not it will be set to 1920x1080.
Multiple windows:
In OpenGL Only one window can exist per thread, so if you want to create more than one, you have to create and handle them in different threads. But it's important to also call the other functions in the same thread as their window.
zap::InitGlad();Glad is a library that handles the OpenGL function locations. All you need to know is that this function must be called AFTER you create a window class instance.
while (window.Open())
{
zap::ClearBackground(zap::BackgroundColors::ORANGE);
if (window.GetInput(zap::Key::ESC, zap::State::PRESSED))
{
window.Close();
}
window.Update();
window.Draw();
}Now a while loop is created which checks if the window is open. In this loop the window is painted orange with the ClearBackground function.
window.UpdateViewport();In quite a few cases your window gets resized. So we call UpdateViewport to match the new window size.
window.Update();
window.Draw();These functions are one of the most important. Update() manages all the window events like Key Inputs. window.Draw() draws everything on the window. So every write operation from meshes etc. need to be called before. Otherwise these wont work.