source: trunk/src/gtk/editor.cpp @ 114

Last change on this file since 114 was 114, checked in by sam, 10 years ago

Switch editor building scheme to GtkBuilder.

  • Property svn:keywords set to Id
File size: 4.2 KB
Line 
1//
2// Deus Hax (working title)
3// Copyright (c) 2010 Sam Hocevar <sam@hocevar.net>
4//
5
6#if defined HAVE_CONFIG_H
7#   include "config.h"
8#endif
9
10#include <cstdio>
11#include <cstdlib>
12#include <cmath>
13
14#include <gtk/gtk.h>
15#include <gtkgl/gtkglarea.h>
16
17#include "ticker.h"
18#include "debugfps.h"
19#include "video.h"
20#include "game.h"
21
22static volatile int quit = 0;
23
24static GTimer *timer;
25static float delta_time;
26static int ticking = 0;
27
28static gint main_quit(GtkWidget *widget, GdkEventExpose *event)
29{
30    (void)widget;
31    (void)event;
32
33    quit = 1;
34    gtk_main_quit();
35    return FALSE;
36}
37
38static gboolean tick(void *widget)
39{
40    // FIXME: do not do anything if the previous tick was too recent?
41    delta_time = 1000.0f * g_timer_elapsed(timer, NULL);
42    g_timer_start(timer);
43
44    // FIXME: only quit if all assets have been cleaned
45    if (quit)
46        return FALSE;
47
48    ticking = 1;
49
50    /* Tick the game */
51    Ticker::TickGame(delta_time);
52
53    gtk_widget_draw(GTK_WIDGET(widget), NULL);
54
55    return TRUE;
56}
57
58static gint init(GtkWidget *widget)
59{
60    if (gtk_gl_area_make_current(GTK_GL_AREA(widget)))
61        Video::Setup(widget->allocation.width, widget->allocation.height);
62
63    return TRUE;
64}
65
66static gint reshape(GtkWidget *widget, GdkEventConfigure *event)
67{
68    (void)event;
69
70    return init(widget);
71}
72
73static gint draw(GtkWidget *widget, GdkEventExpose *event)
74{
75    if (event->count > 0)
76        return TRUE;
77
78    /* OpenGL functions can be called only if make_current returns true */
79    if (ticking && gtk_gl_area_make_current(GTK_GL_AREA(widget)))
80    {
81        ticking = 0;
82
83        /* Clear the screen, tick the renderer, and show the frame */
84        Video::Clear();
85        Ticker::TickRender(delta_time);
86        gtk_gl_area_swapbuffers(GTK_GL_AREA(widget));
87    }
88
89    return TRUE;
90}
91
92int main(int argc, char **argv)
93{
94    /* Initialize GTK */
95    g_thread_init(NULL);
96    gtk_init(&argc, &argv);
97
98    if (gdk_gl_query() == FALSE)
99    {
100        g_print("OpenGL not supported\n");
101        return EXIT_FAILURE;
102    }
103
104    GtkBuilder *builder = gtk_builder_new();
105    if (!gtk_builder_add_from_file(builder, "src/gtk/editor.xml", NULL))
106    {
107        g_print("Cannot build from XML\n");
108        return EXIT_FAILURE;
109    }
110
111    /* Create new top level window. */
112    GtkWidget *window = GTK_WIDGET(gtk_builder_get_object(builder, "window1"));
113
114    /* You should always delete gtk_gl_area widgets before exit or else
115       GLX contexts are left undeleted, this may cause problems (=core dump)
116       in some systems.
117       Destroy method of objects is not automatically called on exit.
118       You need to manually enable this feature. Do gtk_quit_add_destroy()
119       for all your top level windows unless you are certain that they get
120       destroy signal by other means.
121    */
122    gtk_quit_add_destroy(1, GTK_OBJECT(window));
123
124    /* Create new OpenGL widget. */
125    int attrlist[] =
126    {
127        GDK_GL_RGBA,
128        GDK_GL_RED_SIZE, 1,
129        GDK_GL_GREEN_SIZE, 1,
130        GDK_GL_BLUE_SIZE, 1,
131        GDK_GL_DOUBLEBUFFER,
132        GDK_GL_NONE
133    };
134
135    GtkWidget *glarea = gtk_gl_area_new(attrlist);
136
137    gtk_widget_set_events(GTK_WIDGET(glarea),
138                          GDK_EXPOSURE_MASK | GDK_BUTTON_PRESS_MASK);
139
140    gtk_signal_connect(GTK_OBJECT(window), "delete_event",
141                       GTK_SIGNAL_FUNC(main_quit), NULL);
142    gtk_signal_connect(GTK_OBJECT(glarea), "expose_event",
143                       GTK_SIGNAL_FUNC(draw), NULL);
144    gtk_signal_connect(GTK_OBJECT(glarea), "configure_event",
145                       GTK_SIGNAL_FUNC(reshape), NULL);
146    gtk_signal_connect(GTK_OBJECT(glarea), "realize",
147                       GTK_SIGNAL_FUNC(init), NULL);
148
149    // Create a scrolled window around our GL widget
150    GtkWidget *sw = GTK_WIDGET(gtk_builder_get_object(builder, "scrolledwindow1"));
151    gtk_scrolled_window_add_with_viewport(GTK_SCROLLED_WINDOW(sw), glarea);
152
153    /* Put scrolled window into main window */
154    gtk_widget_show_all(GTK_WIDGET(window));
155
156    // FIXME: detect when the game is killed
157    new Game("maps/testmap.tmx");
158    new DebugFps();
159
160    //gtk_idle_add(tick, glarea);
161    gtk_timeout_add(33, tick, glarea);
162
163    timer = g_timer_new();
164    gtk_main();
165
166    return EXIT_SUCCESS;
167}
168
Note: See TracBrowser for help on using the repository browser.