From 8a467bbeaed1a70f79a14a95f7d62e9bd57ff863 Mon Sep 17 00:00:00 2001 From: Aaron Mueller Date: Tue, 6 Dec 2011 22:35:48 +0100 Subject: [PATCH] First working GTK + GL window --- editor/Makefile | 20 +++--- editor/src/config.h | 7 +++ editor/src/display.c | 18 +++++- editor/src/display.h | 1 - editor/src/event_callbacks.c | 47 ++++++++++++++ editor/src/input.c | 7 ++- editor/src/main.c | 71 +++++++++++++--------- editor/src/main_gui.c | 26 -------- editor/src/{main_gui.ui => main_gui.glade} | 61 ++++++++++--------- 9 files changed, 157 insertions(+), 101 deletions(-) create mode 100644 editor/src/event_callbacks.c delete mode 100644 editor/src/main_gui.c rename editor/src/{main_gui.ui => main_gui.glade} (71%) diff --git a/editor/Makefile b/editor/Makefile index 4cc5fc2..70ae65f 100644 --- a/editor/Makefile +++ b/editor/Makefile @@ -1,22 +1,18 @@ CC=gcc CFLAGS=-Wall -INCLUDES=-I/usr/include/GL -LIBS=-lglut -lGLU -lGL -lm -GTKLIBS=`pkg-config --cflags --libs gtk+-2.0` -export-dynamic - +LINKER_FLAGS=-lglut -export-dynamic +GTKLIBS=`pkg-config --cflags --libs gtkglext-1.0 libglade-2.0 gmodule-export-2.0` SRCDIR=src all: - $(CC) $(CFLAGS) $(INCLUDES) -c $(SRCDIR)/display.c - $(CC) $(CFLAGS) $(INCLUDES) -c $(SRCDIR)/input.c - $(CC) $(CFLAGS) $(INCLUDES) -c $(SRCDIR)/main.c + $(CC) $(CFLAGS) $(GTKLIBS) -c $(SRCDIR)/display.c + $(CC) $(CFLAGS) $(GTKLIBS) -c $(SRCDIR)/input.c + $(CC) $(CFLAGS) $(GTKLIBS) -c $(SRCDIR)/event_callbacks.c + $(CC) $(CFLAGS) $(GTKLIBS) -c $(SRCDIR)/main.c - $(CC) $(CFLAGS) $(LIBS) -o ledcube-edit main.o display.o input.o + $(CC) $(CFLAGS) $(LINKER_FLAGS) $(GTKLIBS) -o ledcube-edit main.o display.o input.o chmod +x ledcube-edit - strip ledcube-edit - -gui: - $(CC) $(CFLAGS) $(GTKLIBS) -o gui $(SRCDIR)/main_gui.c + #strip ledcube-edit clean: rm -f *.o diff --git a/editor/src/config.h b/editor/src/config.h index ae1ea1b..54a79f0 100644 --- a/editor/src/config.h +++ b/editor/src/config.h @@ -22,6 +22,7 @@ extern float ledOnMaterial[]; extern float ledOffMaterial[]; extern float wireMaterial[]; extern float innerWireMaterial[]; +extern float backgroundColor[]; // Movement extern float lookX, lookZ; @@ -30,6 +31,12 @@ extern int ledOrientation; // Objects extern GLUquadricObj *quadric; +extern GdkGLConfig *glConfig; +extern GdkGLWindow *glWindow; +extern GdkGLContext *glContext; + +// Dimensions, positions +extern float light0Pos[]; // LED data extern int currentFrame[27]; diff --git a/editor/src/display.c b/editor/src/display.c index b009d74..887024d 100644 --- a/editor/src/display.c +++ b/editor/src/display.c @@ -1,5 +1,8 @@ #include -#include +#include +#include +#include +#include #include "config.h" #include "display.h" @@ -66,7 +69,6 @@ void drawWires() { } void setScene() { - glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glMatrixMode(GL_PROJECTION); glLoadIdentity(); gluPerspective(ZOOM_LEVEL, WINDOW_WIDTH/WINDOW_HEIGHT, 1.0, 350.0); @@ -78,10 +80,20 @@ void setScene() { // OpenGL Display function void display() { + glClearColor(0.0, 0.0, 0.0, 1.0); + glShadeModel(GL_SMOOTH); + + glEnable(GL_LIGHTING); + glEnable(GL_DEPTH_TEST); + glEnable(GL_LIGHT0); + glLightfv(GL_LIGHT0, GL_POSITION, light0Pos); + glLightfv(GL_LIGHT0, GL_AMBIENT, backgroundColor); + + glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); + setScene(); drawLEDs(RENDER_MODE); drawWires(); - glutSwapBuffers(); } // Picking function diff --git a/editor/src/display.h b/editor/src/display.h index 54e4cf2..0397f85 100644 --- a/editor/src/display.h +++ b/editor/src/display.h @@ -1,7 +1,6 @@ #ifndef _DISPLAY_H #define _DISPLAY_H - void drawLEDs(int mode); void drawWires(); diff --git a/editor/src/event_callbacks.c b/editor/src/event_callbacks.c new file mode 100644 index 0000000..45fa5ab --- /dev/null +++ b/editor/src/event_callbacks.c @@ -0,0 +1,47 @@ +#include +#include +#include + +#include "config.h" +#include "display.h" + +void on_main_window_delete_event(GtkObject *object, gpointer userData) { + gtk_main_quit(); +} + +gboolean on_drawing_area_configure_event(GtkWidget *widget, GdkEventConfigure *event, gpointer data) { + GdkGLContext *glContext = gtk_widget_get_gl_context(widget); + GdkGLDrawable *glDrawable =gtk_widget_get_gl_drawable(widget); + + if (!gdk_gl_drawable_gl_begin(glDrawable, glContext)) return FALSE; + + setScene(); + + gdk_gl_drawable_gl_end(glDrawable); + return FALSE; +} + +gboolean on_drawing_area_expose_event(GtkWidget *widget, GdkEventExpose *event, gpointer data) { + GdkGLContext *glContext = gtk_widget_get_gl_context(widget); + GdkGLDrawable *glDrawable =gtk_widget_get_gl_drawable(widget); + + if (!gdk_gl_drawable_gl_begin(glDrawable, glContext)) return FALSE; + glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); + + display(); + + if (gdk_gl_drawable_is_double_buffered(glDrawable)) + gdk_gl_drawable_swap_buffers(glDrawable); + else + glFlush(); + gdk_gl_drawable_gl_end(glDrawable); + return FALSE; +} + +void on_drawing_area_realize(GtkWidget *widget, gpointer data) { + GdkGLContext *glContext = gtk_widget_get_gl_context(widget); + GdkGLDrawable *glDrawable =gtk_widget_get_gl_drawable(widget); + if (!gdk_gl_drawable_gl_begin(glDrawable, glContext)) return; + + gdk_gl_drawable_gl_end(glDrawable); +} diff --git a/editor/src/input.c b/editor/src/input.c index 31cf7b5..79392cf 100644 --- a/editor/src/input.c +++ b/editor/src/input.c @@ -1,6 +1,8 @@ #include #include -#include +#include +#include +#include #include "config.h" #include "input.h" @@ -63,7 +65,8 @@ void mouse(int button, int state, int x, int y) { currentFrame[position] = currentFrame[position] == 0 ? 1 : 0; printf("%d\n", currentFrame[position]); - display(); + // FIXME: Redraw the gl-container + //display(); } } diff --git a/editor/src/main.c b/editor/src/main.c index 83c5f2c..fc12acb 100644 --- a/editor/src/main.c +++ b/editor/src/main.c @@ -1,11 +1,16 @@ #include #include -#include + +#include +#include +#include + +#include #include "config.h" #include "display.h" #include "input.h" - +#include "event_callbacks.c" // Materials float ledOnMaterial[] = {0.0, 0.0, 1.0, 1.0}; @@ -24,43 +29,53 @@ int ledOrientation = TOP_ORIENTATION; // Objects GLUquadricObj *quadric; +GdkGLConfig *glConfig; +GdkGLContext *glContext; +GtkWidget *window, *drawingArea; // LED data int currentFrame[27] = {0}; -int main(int argc, char* argv[]) { +int main(int argc, char *argv[]) { + GladeXML *xml; + + gtk_init(&argc, &argv); + gdk_gl_init(&argc, &argv); glutInit(&argc, argv); - glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH); - glutInitWindowSize(WINDOW_WIDTH, WINDOW_HEIGHT); - glutCreateWindow("CTHN LEDCube Editor v0.1"); - - glutDisplayFunc(display); - glutKeyboardFunc(keyboard); - glutSpecialFunc(keyboard_special); - glutMouseFunc(mouse); - - glClearColor(0.0, 0.0, 0.0, 1.0); - glShadeModel(GL_SMOOTH); - - // Lighting - glEnable(GL_LIGHTING); - glEnable(GL_DEPTH_TEST); - - glEnable(GL_LIGHT0); - glLightfv(GL_LIGHT0, GL_POSITION, light0Pos); - glLightfv(GL_LIGHT0, GL_AMBIENT, backgroundColor); - - glMatrixMode(GL_MODELVIEW); - moveCameraPosition(0); // Init the Position - - quadric = gluNewQuadric(); gluQuadricNormals(quadric, GLU_SMOOTH); gluQuadricDrawStyle(quadric, GLU_FILL); - glutMainLoop(); + glEnable(GL_LIGHTING); + glShadeModel(GL_SMOOTH); + moveCameraPosition(0); + + currentFrame[0] = 1; // TODO: remove + + // Configure the OpenGL widget + glConfig = gdk_gl_config_new_by_mode(GDK_GL_MODE_RGB | GDK_GL_MODE_DEPTH | GDK_GL_MODE_DOUBLE); + if (glConfig == NULL) { + g_print("EEE Double buffer not available, trying single buffer."); + glConfig = gdk_gl_config_new_by_mode(GDK_GL_MODE_RGB | GDK_GL_MODE_DEPTH); + if (glConfig == NULL) { + g_print("EEE Sorry, can't configure the OpenGL window. Giving up."); + exit(1); + } + } + + xml = glade_xml_new("src/main_gui.glade", NULL, NULL); + glade_xml_signal_autoconnect(xml); + + window = glade_xml_get_widget(xml, "main_window"); + gtk_container_set_reallocate_redraws(GTK_CONTAINER(window), TRUE); + drawingArea = glade_xml_get_widget(xml, "drawing_area"); + gtk_widget_set_gl_capability(drawingArea, glConfig, NULL, TRUE, GDK_GL_RGBA_TYPE); + + + gtk_widget_show(window); + gtk_main(); return 0; } diff --git a/editor/src/main_gui.c b/editor/src/main_gui.c deleted file mode 100644 index 406f6c2..0000000 --- a/editor/src/main_gui.c +++ /dev/null @@ -1,26 +0,0 @@ -#include - -void on_window_destroy(GtkObject *object, gpointer userData) { - gtk_main_quit(); -} - -int main (int argc, char *argv[]) { - GtkBuilder *builder; - GtkWidget *window; - - gtk_set_locale(); - gtk_init(&argc, &argv); - - builder = gtk_builder_new(); - gtk_builder_add_from_file(builder, "src/main_gui.ui", NULL); - gtk_builder_connect_signals(builder, NULL); - window = GTK_WIDGET(gtk_builder_get_object(builder, "main_window")); - - g_object_unref(G_OBJECT(builder)); - - gtk_widget_show_all(window); - gtk_main(); - - return 0; -} - diff --git a/editor/src/main_gui.ui b/editor/src/main_gui.glade similarity index 71% rename from editor/src/main_gui.ui rename to editor/src/main_gui.glade index 1e92e31..0f4f4ce 100644 --- a/editor/src/main_gui.ui +++ b/editor/src/main_gui.glade @@ -1,111 +1,114 @@ - - + + - - True + + cursor False center-always - + - + True - + True - + True both-horiz - + True 7 - + True - + - + False True - + True Connect to cube True gtk-connect - + False True - + True - + False True - + True toolbutton3 True gtk-add - + False True - + True toolbutton3 True gtk-delete - + False True - + - + False 0 - + True True - + False 1 - + 500 500 True - + + + + 2 - + - - + +