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

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

New MapViewer class and some GTK+ refactoring.

  • Property svn:keywords set to Id
File size: 3.7 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 "core.h"
18#include "debugfps.h"
19
20static volatile int quit = 0;
21
22static int ticking = 0;
23static float const FPS = 30.0f;
24
25static gint main_quit(GtkWidget *widget, GdkEventExpose *event)
26{
27    (void)widget;
28    (void)event;
29
30    quit = 1;
31    gtk_main_quit();
32    return FALSE;
33}
34
35static gboolean tick(void *widget)
36{
37    // FIXME: do not do anything if the previous tick was too recent?
38
39    // FIXME: only quit if all entities have been cleaned
40    if (quit)
41        return FALSE;
42
43    ticking = 1;
44
45    /* Tick the game */
46    Ticker::TickGame();
47
48    gtk_widget_draw(GTK_WIDGET(widget), NULL);
49
50    return TRUE;
51}
52
53static gint init(GtkWidget *widget)
54{
55    if (gtk_gl_area_make_current(GTK_GL_AREA(widget)))
56        Video::Setup(widget->allocation.width, widget->allocation.height);
57
58    return TRUE;
59}
60
61static gint reshape(GtkWidget *widget, GdkEventConfigure *event)
62{
63    (void)event;
64
65    return init(widget);
66}
67
68static gint draw(GtkWidget *widget, GdkEventExpose *event)
69{
70    if (event->count > 0)
71        return TRUE;
72
73    /* OpenGL functions can be called only if make_current returns true */
74    if (ticking && gtk_gl_area_make_current(GTK_GL_AREA(widget)))
75    {
76        ticking = 0;
77
78        /* Clear the screen, tick the renderer, show the frame and
79         * clamp to desired framerate */
80        Video::Clear();
81        Ticker::TickDraw();
82        gtk_gl_area_swapbuffers(GTK_GL_AREA(widget));
83        while (g_main_context_iteration(NULL, FALSE))
84            ;
85        Ticker::ClampFps(1000.0f / FPS);
86    }
87
88    return TRUE;
89}
90
91int main(int argc, char **argv)
92{
93    /* Initialize GTK */
94    g_thread_init(NULL);
95    gtk_init(&argc, &argv);
96
97    if (gdk_gl_query() == FALSE)
98    {
99        g_print("OpenGL not supported\n");
100        return EXIT_FAILURE;
101    }
102
103    /* Build the rest of the application */
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    GtkWidget *window = GTK_WIDGET(gtk_builder_get_object(builder, "window1"));
112    GtkWidget *sw = GTK_WIDGET(gtk_builder_get_object(builder, "sw1"));
113    g_object_unref(G_OBJECT(builder));
114
115    /* Create new OpenGL widget */
116    int attrlist[] =
117    {
118        GDK_GL_RGBA,
119        GDK_GL_RED_SIZE, 1,
120        GDK_GL_GREEN_SIZE, 1,
121        GDK_GL_BLUE_SIZE, 1,
122        GDK_GL_DOUBLEBUFFER,
123        GDK_GL_NONE
124    };
125
126    GtkWidget *glarea = gtk_gl_area_new(attrlist);
127    gtk_widget_set_events(glarea, GDK_EXPOSURE_MASK | GDK_BUTTON_PRESS_MASK);
128    gtk_scrolled_window_add_with_viewport(GTK_SCROLLED_WINDOW(sw), glarea);
129
130    /* Connect signals and show window */
131    gtk_signal_connect(GTK_OBJECT(window), "delete_event",
132                       GTK_SIGNAL_FUNC(main_quit), NULL);
133    gtk_signal_connect(GTK_OBJECT(glarea), "expose_event",
134                       GTK_SIGNAL_FUNC(draw), NULL);
135    gtk_signal_connect(GTK_OBJECT(glarea), "configure_event",
136                       GTK_SIGNAL_FUNC(reshape), NULL);
137    gtk_signal_connect(GTK_OBJECT(glarea), "realize",
138                       GTK_SIGNAL_FUNC(init), NULL);
139
140    gtk_widget_show_all(window);
141
142    // FIXME: detect when the map viewer is killed
143    new MapViewer("maps/testmap.tmx");
144    new DebugFps();
145
146    /* We tick from the idle function instead of a timeout to avoid
147     * stealing time from the GTK loop when the callback time exceeds
148     * the timeout value. */
149    gtk_idle_add(tick, glarea);
150    gtk_main();
151
152    return EXIT_SUCCESS;
153}
154
Note: See TracBrowser for help on using the repository browser.