文章目錄
- 搭建一個基本的GTK4應用
搭建一個基本的GTK4應用
MinGW下安裝GTK4傳送門
官方檔案可能還沒有更新,顯示的命令還是gtk3,小伙伴可以將gtk3改為gtk4即可安裝GTK4
/*
* 初始化并啟動GTK4應用程式
*/
int
main (int argc,
char **argv)
{
GtkApplication *app;
int status;
//創建一個GTK4應用
app = gtk_application_new ("org.gtk.example", G_APPLICATION_FLAGS_NONE);
//系結應用激活回呼函式,用于初始化應用程式
g_signal_connect (app, "activate", G_CALLBACK (activate), NULL);
//運行GTK4應用
status = g_application_run (G_APPLICATION (app), argc, argv);
g_object_unref (app);
return status;
}
在介紹activate函式之前,我們先看一GTK4應用的UI檔案
gtk.opengl.font.gresource.xml
<?xml version="1.0" encoding="UTF-8"?>
<interface>
<object class="GtkBox" id="box">
<property name="visible">True</property>
<property name="margin-start">12</property>
<property name="margin-end">12</property>
<property name="margin-top">12</property>
<property name="margin-bottom">12</property>
<property name="orientation">vertical</property>
<property name="spacing">6</property>
<child>
<!--定義gl_area用于顯示文字,且gl_area是box的孩子-->
<object class="GtkGLArea" id="gl_area">
<property name="visible">True</property>
<property name="vexpand">True</property>
<property name="hexpand">True</property>
</object>
</child>
<child>
<object class="GtkButton" id="button">
<property name="label" translatable="yes">Quit</property>
<property name="visible">True</property>
<property name="can-focus">True</property>
</object>
</child>
</object>
</interface>
效果圖如下👇

/* We need to set up our state when we realize the GtkGLArea widget */
static void
activate (GtkApplication *app,
gpointer user_data)
{
GtkWidget *window;
GtkBuilder *builder;
GtkWidget *box, *button;
//定義一個應用視窗,并設定部分屬性
window = gtk_application_window_new (app);
gtk_window_set_title (GTK_WINDOW (window), "GTK_OPENGL_FONT");
gtk_window_set_default_size (GTK_WINDOW (window), 800, 600);
//獲取UI檔案
//為什么路徑是"/gtk_opengl_font/gtk.opengl.font.ui"下面會介紹
builder = gtk_builder_new_from_resource("/gtk_opengl_font/gtk.opengl.font.ui");
//獲取UI檔案中的box物件
box = GTK_WIDGET(gtk_builder_get_object(builder,"box"));
//獲取UI檔案中的gl_area 物件
gl_area = GTK_WIDGET(gtk_builder_get_object(builder,"gl_area"));
//獲取UI檔案中的button 物件
button = GTK_WIDGET(gtk_builder_get_object(builder,"button"));
//將UI檔案中設計的視窗,設定為應用視窗的孩子
gtk_window_set_child (GTK_WINDOW (window), box);
/* We need to initialize and free GL resources, so we use
* the realize and unrealize signals on the widget
*/
//系結信號回呼函式
//realize和unrealize一般是成對出現,realize用于在控制元件顯示之前對控制元件進行
//一些客制化的初始化任務
g_signal_connect (gl_area, "realize", G_CALLBACK (realize), NULL);
g_signal_connect (gl_area, "unrealize", G_CALLBACK (unrealize), NULL);
/* 用于在GL區域顯示文字,在創建GL Area時會觸發一次render回呼函式 */
g_signal_connect (gl_area, "render", G_CALLBACK (render), NULL);
//這里系結的gtk_window_destroy函式是系統內置的函式
g_signal_connect_swapped (button, "clicked", G_CALLBACK (gtk_window_destroy), window);
g_object_unref(builder);
//顯示應用視窗
gtk_widget_show(window);
}
為了方便檔案管理,我們將檔案統一登記到一個XML檔案中,然后一起編譯到code中,之后便可以使用相對地址訪問檔案,不需要關心檔案的實際位置了
gtk.opengl.font.gresource.xml(這個檔案必須以“.gresource.xml”結尾)
<?xml version="1.0" encoding="UTF-8"?>
<gresources>
<!--"/gtk_opengl_font"是路徑前綴,必須以'/'開頭-->
<gresource prefix="/gtk_opengl_font">
<!--子視窗UI檔案-->
<file>gtk.opengl.font.ui</file>
<!--OpenGL頂點檔案-->
<file>gtk.opengl.font.vs.glsl</file>
<!--OpenGL片段檔案-->
<file>gtk.opengl.font.fs.glsl</file>
</gresource>
</gresources>
有了這個檔案還需生成一個“.c”檔案一起編譯到code中,在MinGW中命令格式如下(默認所有檔案,包括組態檔都在一個目錄,且下面命令也在此目錄中執行):
#我這里生成的"resources.c"源檔案
glib-compile-resources.exe gtk.opengl.font.gresource.xml --target=resources.c --generate-source
關于realize、unrealize、render三個函式的詳解會在介紹OpenGL與FreeType2的時候介紹
回傳系列目錄頁

轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/279910.html
標籤:其他
上一篇:BF演算法代碼(超詳細)
