]> Pileus Git - grits/blobdiff - src/plugin-example.c
* Fixing some async issue (w/ gthread_init)
[grits] / src / plugin-example.c
index a2fb36699aa91c3c8915b848a402a5edadd68ba5..40606423319e2888c49f40479bb9f0b2955bc463 100644 (file)
+/*
+ * Copyright (C) 2009 Andy Spencer <spenceal@rose-hulman.edu>
+ * 
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU 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 General Public License for more details.
+ * 
+ * You should have received a copy of the GNU General Public License
+ * along with this program.  If not, see <http://www.gnu.org/licenses/>.
+ */
+
 #include <config.h>
 #include <gtk/gtk.h>
 #include <gtk/gtkgl.h>
 #include <GL/gl.h>
 
 #include "aweather-gui.h"
+#include "plugin-example.h"
+
+/****************
+ * GObject code *
+ ****************/
+static void aweather_example_plugin_init(AWeatherPluginInterface *iface);
+static void aweather_example_expose(AWeatherPlugin *_example);
+G_DEFINE_TYPE_WITH_CODE(AWeatherExample, aweather_example, G_TYPE_OBJECT,
+               G_IMPLEMENT_INTERFACE(AWEATHER_TYPE_PLUGIN,
+                       aweather_example_plugin_init));
+static void aweather_example_class_init(AWeatherExampleClass *klass)
+{
+       GObjectClass *object_class = (GObjectClass*)klass;
+}
+static void aweather_example_plugin_init(AWeatherPluginInterface *iface)
+{
+       /* Add methods to the interface */
+       iface->expose = aweather_example_expose;
+}
+static void aweather_example_init(AWeatherExample *example)
+{
+       /* Set defaults */
+       example->gui      = NULL;
+       example->button   = NULL;
+       example->rotation = 30.0;
+}
 
-static GtkWidget *rotate_button;
+/***********
+ * Helpers *
+ ***********/
+static gboolean rotate(gpointer _example)
+{
+       AWeatherExample *example = _example;
+       if (gtk_toggle_button_get_active(example->button)) {
+               example->rotation += 1.0;
+               aweather_gui_gl_redraw(example->gui);
+       }
+       return TRUE;
+}
 
-static float ang = 30.;
+/***********
+ * Methods *
+ ***********/
+AWeatherExample *aweather_example_new(AWeatherGui *gui)
+{
+       g_debug("AWeatherExample: new");
+       AWeatherExample *example = g_object_new(AWEATHER_TYPE_EXAMPLE, NULL);
+       example->gui = gui;
 
-static gboolean expose(GtkWidget *da, GdkEventExpose *event, gpointer user_data)
+       GtkWidget *drawing = aweather_gui_get_widget(gui, "drawing");
+       GtkWidget *config  = aweather_gui_get_widget(gui, "tabs");
+
+       /* Add configuration tab */
+       GtkWidget *label = gtk_label_new("Example");
+       example->button = GTK_TOGGLE_BUTTON(gtk_toggle_button_new_with_label("Rotate"));
+       gtk_notebook_append_page(GTK_NOTEBOOK(config), GTK_WIDGET(example->button), label);
+
+       /* Set up OpenGL Stuff */
+       //g_signal_connect(drawing, "expose-event", G_CALLBACK(expose), NULL);
+       g_timeout_add(1000/60, rotate, example);
+
+       return example;
+}
+
+static void aweather_example_expose(AWeatherPlugin *_example)
 {
+       AWeatherExample *example = AWEATHER_EXAMPLE(_example);
+       g_debug("AWeatherExample: expose");
        glDisable(GL_TEXTURE_2D);
-       glMatrixMode(GL_MODELVIEW ); glPushMatrix(); glLoadIdentity();
        glMatrixMode(GL_PROJECTION); glPushMatrix(); glLoadIdentity();
-       //glOrtho(-1,1,-1,1,-10,10);
-
-       glTranslatef(0.5, -0.5, -2);
+       glOrtho(-1,1,-1,1,-10,10);
+       glMatrixMode(GL_MODELVIEW ); glPushMatrix(); glLoadIdentity();
 
        float light_ambient[]  = {0.1f, 0.1f, 0.0f};
        float light_diffuse[]  = {0.9f, 0.9f, 0.9f};
-       float light_position[] = {-20.0f, 40.0f, -40.0f, 1.0f};
+       float light_position[] = {-30.0f, 50.0f, 40.0f, 1.0f};
        glLightfv(GL_LIGHT0, GL_AMBIENT,  light_ambient);
        glLightfv(GL_LIGHT0, GL_DIFFUSE,  light_diffuse);
        glLightfv(GL_LIGHT0, GL_POSITION, light_position);
@@ -28,7 +104,8 @@ static gboolean expose(GtkWidget *da, GdkEventExpose *event, gpointer user_data)
        glEnable(GL_LIGHTING);
        glEnable(GL_COLOR_MATERIAL);
 
-       glRotatef(ang, 1, 0, 1);
+       glTranslatef(0.5, -0.5, -2);
+       glRotatef(example->rotation, 1, 0, 1);
        glColor4f(0.9, 0.9, 0.7, 1.0);
        gdk_gl_draw_teapot(TRUE, 0.25);
        gdk_gl_draw_cube(TRUE, 0.25);
@@ -40,36 +117,6 @@ static gboolean expose(GtkWidget *da, GdkEventExpose *event, gpointer user_data)
 
         glMatrixMode(GL_PROJECTION); glPopMatrix(); 
        glMatrixMode(GL_MODELVIEW ); glPopMatrix();
-       return FALSE;
+       return;
 }
 
-static gboolean rotate(gpointer user_data)
-{
-       if (!gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(rotate_button)))
-               return TRUE;
-
-       GtkWidget *da = GTK_WIDGET (user_data);
-
-       ang++;
-
-       gdk_window_invalidate_rect(da->window, &da->allocation, FALSE);
-       gdk_window_process_updates(da->window, FALSE);
-
-       return TRUE;
-}
-
-gboolean example_init(AWeatherGui *gui)
-{
-       GtkWidget *drawing = aweather_gui_get_widget(gui, "drawing");
-       GtkWidget *config  = aweather_gui_get_widget(gui, "tabs");
-
-       /* Add configuration tab */
-       GtkWidget *label = gtk_label_new("example");
-       rotate_button = gtk_toggle_button_new_with_label("Rotate");
-       gtk_notebook_append_page(GTK_NOTEBOOK(config), rotate_button, label);
-
-       /* Set up OpenGL Stuff */
-       g_signal_connect(drawing, "expose-event", G_CALLBACK(expose), NULL);
-       g_timeout_add(1000/60, rotate, drawing);
-       return TRUE;
-}