WIP: obtained a window
This commit is contained in:
parent
dce3310970
commit
4b6383bf0c
32
include/ui.h
32
include/ui.h
|
@ -1,32 +0,0 @@
|
|||
/*
|
||||
* Gem-graph OpenGL experiments
|
||||
*
|
||||
* Desc: User interface header
|
||||
*
|
||||
* Copyright (C) 2023 Arthur Menges <arthur.menges@a-lec.org>
|
||||
* Copyright (C) 2023 Adrien Bourmault <neox@a-lec.org>
|
||||
*
|
||||
* This file is part of Gem-graph.
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Affero General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU Affero General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Affero General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
#include <unistd.h>
|
||||
#include <gtk-4.0/gtk/gtk.h>
|
||||
|
||||
extern float rotation_angles[N_AXIS];
|
||||
|
||||
void on_activate(GtkApplication *app, gpointer user_data);
|
||||
|
|
@ -33,7 +33,7 @@
|
|||
#include <math.h>
|
||||
|
||||
#include "../include/base.h"
|
||||
#include "../include/ui.h"
|
||||
#include "../include/window.h"
|
||||
|
||||
#define VERTEX_SHADER_FILE "data/shader.vert"
|
||||
#define FRAG_SHADER_FILE "data/shader.frag"
|
||||
|
|
17
src/main.c
17
src/main.c
|
@ -26,17 +26,26 @@
|
|||
#include <gtk-4.0/gtk/gtk.h>
|
||||
|
||||
#include "../include/base.h"
|
||||
#include "../include/ui.h"
|
||||
#include "../include/window.h"
|
||||
#include "../include/application.h"
|
||||
|
||||
/* -------------------------------------------------------------------------- */
|
||||
|
||||
int main(int argc, char **argv)
|
||||
{
|
||||
GtkApplication *app;
|
||||
g_autoptr(GemGraphClientApplication) app = NULL;
|
||||
int res;
|
||||
|
||||
app = gtk_application_new(NULL, G_APPLICATION_DEFAULT_FLAGS);
|
||||
g_signal_connect(app, "activate", G_CALLBACK(on_activate), NULL);
|
||||
// bindtextdomain (GETTEXT_PACKAGE, LOCALEDIR);
|
||||
// bind_textdomain_codeset (GETTEXT_PACKAGE, "UTF-8");
|
||||
// textdomain (GETTEXT_PACKAGE);
|
||||
|
||||
//app = gtk_application_new(NULL, G_APPLICATION_DEFAULT_FLAGS);
|
||||
app = gem_graph_client_application_new("org.alec.gemgraph",
|
||||
G_APPLICATION_DEFAULT_FLAGS);
|
||||
|
||||
//g_signal_connect(app, "activate", G_CALLBACK(on_activate), NULL);
|
||||
|
||||
res = g_application_run(G_APPLICATION(app), argc, argv);
|
||||
g_object_unref(app);
|
||||
|
||||
|
|
191
src/ui.c
191
src/ui.c
|
@ -1,191 +0,0 @@
|
|||
/*
|
||||
* Gem-graph OpenGL experiments
|
||||
*
|
||||
* Desc: User interface functions
|
||||
*
|
||||
* Copyright (C) 2023 Arthur Menges <arthur.menges@a-lec.org>
|
||||
* Copyright (C) 2023 Adrien Bourmault <neox@a-lec.org>
|
||||
*
|
||||
* This file is part of Gem-graph.
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Affero General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU Affero General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Affero General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#include <unistd.h>
|
||||
#include <gtk-4.0/gtk/gtk.h>
|
||||
#include <glib-2.0/glib.h>
|
||||
|
||||
#include "../include/base.h"
|
||||
#include "../include/graphics.h"
|
||||
|
||||
/* -------------------------------------------------------------------------- */
|
||||
|
||||
float rotation_angles[N_AXIS] = { 0.0 }; // Rotation angles on each axis
|
||||
|
||||
static GtkWidget *gl_area = NULL;
|
||||
|
||||
/* -------------------------------------------------------------------------- */
|
||||
|
||||
static void on_axis_value_change(GtkAdjustment *adjustment, gpointer data);
|
||||
|
||||
static inline GtkWidget *create_axis_slider(int axis)
|
||||
{
|
||||
GtkWidget *box, *label, *slider;
|
||||
GtkAdjustment *adj;
|
||||
const char *text;
|
||||
|
||||
box = gtk_box_new(GTK_ORIENTATION_HORIZONTAL, 0);
|
||||
|
||||
switch (axis) {
|
||||
case X_AXIS:
|
||||
text = "X";
|
||||
break;
|
||||
|
||||
case Y_AXIS:
|
||||
text = "Y";
|
||||
break;
|
||||
|
||||
case Z_AXIS:
|
||||
text = "Z";
|
||||
break;
|
||||
|
||||
default:
|
||||
g_assert_not_reached();
|
||||
}
|
||||
|
||||
label = gtk_label_new(text);
|
||||
gtk_box_append(GTK_BOX(box), label);
|
||||
gtk_widget_show(label);
|
||||
|
||||
adj = gtk_adjustment_new(0.0, 0.0, 360.0, 1.0, 12.0, 0.0);
|
||||
g_signal_connect(adj, "value-changed",
|
||||
G_CALLBACK(on_axis_value_change),
|
||||
GINT_TO_POINTER(axis));
|
||||
slider = gtk_scale_new(GTK_ORIENTATION_HORIZONTAL, adj);
|
||||
gtk_box_append(GTK_BOX(box), slider);
|
||||
gtk_widget_set_hexpand(slider, TRUE);
|
||||
gtk_widget_show(slider);
|
||||
|
||||
gtk_widget_show(box);
|
||||
|
||||
return box;
|
||||
}
|
||||
|
||||
/* -------------------------------------------------------------------------- */
|
||||
|
||||
static void on_axis_value_change(GtkAdjustment *adjustment, gpointer data)
|
||||
{
|
||||
int axis = GPOINTER_TO_INT(data);
|
||||
|
||||
g_assert(axis >= 0 && axis < N_AXIS);
|
||||
|
||||
/* Update the rotation angle */
|
||||
rotation_angles[axis] = gtk_adjustment_get_value(adjustment);
|
||||
|
||||
/* Update the contents of the GL drawing area */
|
||||
gtk_widget_queue_draw(gl_area);
|
||||
}
|
||||
|
||||
static gboolean on_render(GtkGLArea * area, GdkGLContext * context)
|
||||
{
|
||||
if(gtk_gl_area_get_error(area) != NULL)
|
||||
return FALSE;
|
||||
|
||||
graphicsDraw();
|
||||
|
||||
return TRUE;
|
||||
}
|
||||
|
||||
|
||||
/* We need to set up our state when we realize the GtkGLArea widget */
|
||||
static void on_realize(GtkWidget *widget)
|
||||
{
|
||||
gtk_gl_area_make_current(GTK_GL_AREA(widget));
|
||||
|
||||
if(gtk_gl_area_get_error(GTK_GL_AREA(widget)) != NULL)
|
||||
return;
|
||||
|
||||
graphicsInitGL();
|
||||
}
|
||||
|
||||
/* We should tear down the state when unrealizing */
|
||||
static void on_unrealize(GtkWidget *widget)
|
||||
{
|
||||
gtk_gl_area_make_current(GTK_GL_AREA(widget));
|
||||
|
||||
if(gtk_gl_area_get_error(GTK_GL_AREA(widget)) != NULL)
|
||||
return;
|
||||
|
||||
graphicsShutdownGL();
|
||||
}
|
||||
|
||||
static void on_close_window(GtkWidget *widget)
|
||||
{
|
||||
/* Reset the state */
|
||||
gl_area = NULL;
|
||||
|
||||
rotation_angles[X_AXIS] = 0.0;
|
||||
rotation_angles[Y_AXIS] = 0.0;
|
||||
rotation_angles[Z_AXIS] = 0.0;
|
||||
}
|
||||
|
||||
void on_activate(GtkApplication *app, gpointer user_data)
|
||||
{
|
||||
GtkWidget *window, *box, *button, *controls;
|
||||
int i, minor=4, major=0;
|
||||
|
||||
window = gtk_application_window_new(app);
|
||||
gtk_window_set_default_size(GTK_WINDOW(window), 400, 600);
|
||||
gtk_window_set_title(GTK_WINDOW(window), "GemGL (essais pour Gem-graph en OpenGL)");
|
||||
g_signal_connect(window, "destroy", G_CALLBACK(on_close_window), NULL);
|
||||
|
||||
box = gtk_box_new(GTK_ORIENTATION_VERTICAL, FALSE);
|
||||
gtk_widget_set_margin_start(box, 12);
|
||||
gtk_widget_set_margin_end(box, 12);
|
||||
gtk_widget_set_margin_top(box, 12);
|
||||
gtk_widget_set_margin_bottom(box, 12);
|
||||
gtk_box_set_spacing(GTK_BOX(box), 6);
|
||||
gtk_window_set_child(GTK_WINDOW(window), box);
|
||||
|
||||
gl_area = gtk_gl_area_new();
|
||||
gtk_gl_area_set_required_version( GTK_GL_AREA(gl_area), minor, major);
|
||||
gtk_gl_area_get_required_version( GTK_GL_AREA(gl_area), &minor, &major);
|
||||
gtk_widget_set_hexpand(gl_area, TRUE);
|
||||
gtk_widget_set_vexpand(gl_area, TRUE);
|
||||
gtk_widget_set_size_request(gl_area, 100, 200);
|
||||
gtk_box_append(GTK_BOX(box), gl_area);
|
||||
|
||||
// We need to initialize and free GL resources, so we use
|
||||
// the realize and unrealize signals on the widget
|
||||
//
|
||||
g_signal_connect(gl_area, "realize", G_CALLBACK(on_realize), NULL);
|
||||
g_signal_connect(gl_area, "unrealize", G_CALLBACK(on_unrealize), NULL);
|
||||
|
||||
// The main "draw" call for GtkGLArea
|
||||
g_signal_connect(gl_area, "render", G_CALLBACK(on_render), NULL);
|
||||
|
||||
controls = gtk_box_new(GTK_ORIENTATION_VERTICAL, FALSE);
|
||||
gtk_box_append(GTK_BOX(box), controls);
|
||||
gtk_widget_set_hexpand(controls, TRUE);
|
||||
|
||||
for(i = 0; i < N_AXIS; i++)
|
||||
gtk_box_append(GTK_BOX(controls), create_axis_slider(i));
|
||||
|
||||
button = gtk_button_new_with_label("Fermer");
|
||||
gtk_widget_set_hexpand(button, TRUE);
|
||||
gtk_box_append(GTK_BOX(box), button);
|
||||
g_signal_connect_swapped(button, "clicked", G_CALLBACK(gtk_window_destroy), window);
|
||||
|
||||
gtk_widget_show(window);
|
||||
}
|
|
@ -4,7 +4,7 @@
|
|||
<!-- interface-name gem-graph.ui -->
|
||||
<requires lib="gtk" version="4.6"/>
|
||||
<requires lib="libadwaita" version="1.1"/>
|
||||
<template class="main" parent="GtkApplicationWindow">
|
||||
<template class="GemGraphClientWindow" parent="GtkApplicationWindow">
|
||||
<property name="default-height">720</property>
|
||||
<property name="default-width">1080</property>
|
||||
<property name="icon-name">application-x-executable</property>
|
||||
|
|
Loading…
Reference in New Issue