]> Pileus Git - grits/blob - src/plugin-example.c
26be63a0c81c571658e9e142b9ba4938d9921b88
[grits] / src / plugin-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 "plugin-example.h"
25
26 /****************
27  * GObject code *
28  ****************/
29 static void aweather_example_plugin_init(AWeatherPluginInterface *iface);
30 static void aweather_example_expose(AWeatherPlugin *_example);
31 G_DEFINE_TYPE_WITH_CODE(AWeatherExample, aweather_example, G_TYPE_OBJECT,
32                 G_IMPLEMENT_INTERFACE(AWEATHER_TYPE_PLUGIN,
33                         aweather_example_plugin_init));
34 static void aweather_example_class_init(AWeatherExampleClass *klass)
35 {
36         GObjectClass *object_class = (GObjectClass*)klass;
37 }
38 static void aweather_example_plugin_init(AWeatherPluginInterface *iface)
39 {
40         /* Add methods to the interface */
41         iface->expose = aweather_example_expose;
42 }
43 static void aweather_example_init(AWeatherExample *example)
44 {
45         /* Set defaults */
46         example->gui      = NULL;
47         example->button   = NULL;
48         example->rotation = 30.0;
49 }
50
51 /***********
52  * Helpers *
53  ***********/
54 static gboolean rotate(gpointer _example)
55 {
56         AWeatherExample *example = _example;
57         if (gtk_toggle_button_get_active(example->button)) {
58                 example->rotation += 1.0;
59                 aweather_gui_gl_redraw(example->gui);
60         }
61         return TRUE;
62 }
63
64 /***********
65  * Methods *
66  ***********/
67 AWeatherExample *aweather_example_new(AWeatherGui *gui)
68 {
69         //g_message("aweather_view_new");
70         AWeatherExample *example = g_object_new(AWEATHER_TYPE_EXAMPLE, NULL);
71         example->gui = gui;
72
73         GtkWidget *drawing = aweather_gui_get_widget(gui, "drawing");
74         GtkWidget *config  = aweather_gui_get_widget(gui, "tabs");
75
76         /* Add configuration tab */
77         GtkWidget *label = gtk_label_new("Example");
78         example->button = GTK_TOGGLE_BUTTON(gtk_toggle_button_new_with_label("Rotate"));
79         gtk_notebook_append_page(GTK_NOTEBOOK(config), GTK_WIDGET(example->button), label);
80
81         /* Set up OpenGL Stuff */
82         //g_signal_connect(drawing, "expose-event", G_CALLBACK(expose), NULL);
83         g_timeout_add(1000/60, rotate, example);
84
85         return example;
86 }
87
88 static void aweather_example_expose(AWeatherPlugin *_example)
89 {
90         AWeatherExample *example = AWEATHER_EXAMPLE(_example);
91         g_message("aweather_example_expose");
92         glDisable(GL_TEXTURE_2D);
93         glMatrixMode(GL_PROJECTION); glPushMatrix(); glLoadIdentity();
94         glOrtho(-1,1,-1,1,-10,10);
95         glMatrixMode(GL_MODELVIEW ); glPushMatrix(); glLoadIdentity();
96
97         float light_ambient[]  = {0.1f, 0.1f, 0.0f};
98         float light_diffuse[]  = {0.9f, 0.9f, 0.9f};
99         float light_position[] = {-30.0f, 50.0f, 40.0f, 1.0f};
100         glLightfv(GL_LIGHT0, GL_AMBIENT,  light_ambient);
101         glLightfv(GL_LIGHT0, GL_DIFFUSE,  light_diffuse);
102         glLightfv(GL_LIGHT0, GL_POSITION, light_position);
103         glEnable(GL_LIGHT0);
104         glEnable(GL_LIGHTING);
105         glEnable(GL_COLOR_MATERIAL);
106
107         glTranslatef(0.5, -0.5, -2);
108         glRotatef(example->rotation, 1, 0, 1);
109         glColor4f(0.9, 0.9, 0.7, 1.0);
110         gdk_gl_draw_teapot(TRUE, 0.25);
111         gdk_gl_draw_cube(TRUE, 0.25);
112         glColor4f(1.0, 1.0, 1.0, 1.0);
113
114         glDisable(GL_LIGHT0);
115         glDisable(GL_LIGHTING);
116         glDisable(GL_COLOR_MATERIAL);
117
118         glMatrixMode(GL_PROJECTION); glPopMatrix(); 
119         glMatrixMode(GL_MODELVIEW ); glPopMatrix();
120         return;
121 }
122