]> Pileus Git - grits/blob - src/plugins/example.c
dd9f905274d61732ab28e20163a9167f64835e95
[grits] / src / plugins / example.c
1 /*
2  * Copyright (C) 2009 Andy Spencer <spenceal@rose-hulman.edu>
3  * 
4  * This program is free software: you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation, either version 3 of the License, or
7  * (at your option) any later version.
8  * 
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  * 
14  * You should have received a copy of the GNU General Public License
15  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
16  */
17
18 #include <config.h>
19 #include <gtk/gtk.h>
20 #include <gtk/gtkgl.h>
21 #include <GL/gl.h>
22
23 #include "aweather-gui.h"
24 #include "example.h"
25
26 /****************
27  * GObject code *
28  ****************/
29 /* Plugin init */
30 static void aweather_example_plugin_init(AWeatherPluginInterface *iface);
31 static void aweather_example_expose(AWeatherPlugin *_self);
32 G_DEFINE_TYPE_WITH_CODE(AWeatherExample, aweather_example, G_TYPE_OBJECT,
33                 G_IMPLEMENT_INTERFACE(AWEATHER_TYPE_PLUGIN,
34                         aweather_example_plugin_init));
35 static void aweather_example_plugin_init(AWeatherPluginInterface *iface)
36 {
37         g_debug("AWeatherExample: plugin_init");
38         /* Add methods to the interface */
39         iface->expose = aweather_example_expose;
40 }
41 /* Class/Object init */
42 static void aweather_example_init(AWeatherExample *self)
43 {
44         g_debug("AWeatherExample: init");
45         /* Set defaults */
46         self->gui      = NULL;
47         self->button   = NULL;
48         self->rotation = 30.0;
49 }
50 static void aweather_example_dispose(GObject *gobject)
51 {
52         g_debug("AWeatherExample: dispose");
53         AWeatherExample *self = AWEATHER_EXAMPLE(gobject);
54         /* Drop references */
55         G_OBJECT_CLASS(aweather_example_parent_class)->dispose(gobject);
56 }
57 static void aweather_example_finalize(GObject *gobject)
58 {
59         g_debug("AWeatherExample: finalize");
60         AWeatherExample *self = AWEATHER_EXAMPLE(gobject);
61         /* Free data */
62         G_OBJECT_CLASS(aweather_example_parent_class)->finalize(gobject);
63
64 }
65 static void aweather_example_class_init(AWeatherExampleClass *klass)
66 {
67         g_debug("AWeatherExample: class_init");
68         GObjectClass *gobject_class = (GObjectClass*)klass;
69         gobject_class->dispose  = aweather_example_dispose;
70         gobject_class->finalize = aweather_example_finalize;
71 }
72
73 /***********
74  * Helpers *
75  ***********/
76 static gboolean rotate(gpointer _self)
77 {
78         AWeatherExample *self = _self;
79         if (gtk_toggle_button_get_active(self->button)) {
80                 self->rotation += 1.0;
81                 gis_opengl_redraw(aweather_gui_get_opengl(self->gui));
82         }
83         return TRUE;
84 }
85
86 /***********
87  * Methods *
88  ***********/
89 AWeatherExample *aweather_example_new(AWeatherGui *gui)
90 {
91         g_debug("AWeatherExample: new");
92         AWeatherExample *self = g_object_new(AWEATHER_TYPE_EXAMPLE, NULL);
93         self->gui = gui;
94
95         GtkWidget *drawing = aweather_gui_get_widget(gui, "drawing");
96         GtkWidget *config  = aweather_gui_get_widget(gui, "tabs");
97
98         /* Add configuration tab */
99         GtkWidget *label = gtk_label_new("Example");
100         self->button = GTK_TOGGLE_BUTTON(gtk_toggle_button_new_with_label("Rotate"));
101         gtk_notebook_append_page(GTK_NOTEBOOK(config), GTK_WIDGET(self->button), label);
102
103         /* Set up OpenGL Stuff */
104         //g_signal_connect(drawing, "expose-event", G_CALLBACK(expose), NULL);
105         g_timeout_add(1000/60, rotate, self);
106
107         return self;
108 }
109
110 static void aweather_example_expose(AWeatherPlugin *_self)
111 {
112         AWeatherExample *self = AWEATHER_EXAMPLE(_self);
113         g_debug("AWeatherExample: expose");
114         glDisable(GL_TEXTURE_2D);
115         glMatrixMode(GL_PROJECTION); glPushMatrix(); glLoadIdentity();
116         glOrtho(-1,1,-1,1,-10,10);
117         glMatrixMode(GL_MODELVIEW ); glPushMatrix(); glLoadIdentity();
118
119         float light_ambient[]  = {0.1f, 0.1f, 0.0f};
120         float light_diffuse[]  = {0.9f, 0.9f, 0.9f};
121         float light_position[] = {-30.0f, 50.0f, 40.0f, 1.0f};
122         glLightfv(GL_LIGHT0, GL_AMBIENT,  light_ambient);
123         glLightfv(GL_LIGHT0, GL_DIFFUSE,  light_diffuse);
124         glLightfv(GL_LIGHT0, GL_POSITION, light_position);
125         glEnable(GL_LIGHT0);
126         glEnable(GL_LIGHTING);
127         glEnable(GL_COLOR_MATERIAL);
128
129         glTranslatef(0.5, -0.5, -2);
130         glRotatef(self->rotation, 1, 0, 1);
131         glColor4f(0.9, 0.9, 0.7, 1.0);
132         gdk_gl_draw_teapot(TRUE, 0.25);
133         gdk_gl_draw_cube(TRUE, 0.25);
134         glColor4f(1.0, 1.0, 1.0, 1.0);
135
136         glDisable(GL_LIGHT0);
137         glDisable(GL_LIGHTING);
138         glDisable(GL_COLOR_MATERIAL);
139
140         glMatrixMode(GL_PROJECTION); glPopMatrix(); 
141         glMatrixMode(GL_MODELVIEW ); glPopMatrix();
142         return;
143 }
144