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

Last change on this file since 122 was 122, checked in by sam, 11 years ago

Implement the profiling system.

  • Property svn:keywords set to Id
File size: 4.1 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 int ticking = 0;
25static float const FPS = 30.0f;
26
27static gint main_quit(GtkWidget *widget, GdkEventExpose *event)
28{
29    (void)widget;
30    (void)event;
31
32    quit = 1;
33    gtk_main_quit();
34    return FALSE;
35}
36
37static gboolean tick(void *widget)
38{
39    // FIXME: do not do anything if the previous tick was too recent?
40
41    // FIXME: only quit if all assets have been cleaned
42    if (quit)
43        return FALSE;
44
45    ticking = 1;
46
47    /* Tick the game */
48    Ticker::TickGame();
49
50    gtk_widget_draw(GTK_WIDGET(widget), NULL);
51
52    return TRUE;
53}
54
55static gint init(GtkWidget *widget)
56{
57    if (gtk_gl_area_make_current(GTK_GL_AREA(widget)))
58        Video::Setup(widget->allocation.width, widget->allocation.height);
59
60    return TRUE;
61}
62
63static gint reshape(GtkWidget *widget, GdkEventConfigure *event)
64{
65    (void)event;
66
67    return init(widget);
68}
69
70static gint draw(GtkWidget *widget, GdkEventExpose *event)
71{
72    if (event->count > 0)
73        return TRUE;
74
75    /* OpenGL functions can be called only if make_current returns true */
76    if (ticking && gtk_gl_area_make_current(GTK_GL_AREA(widget)))
77    {
78        ticking = 0;
79
80        /* Clear the screen, tick the renderer, show the frame and
81         * clamp to desired framerate. */
82        Video::Clear();
83        Ticker::TickRender();
84        gtk_gl_area_swapbuffers(GTK_GL_AREA(widget));
85        Ticker::ClampFps(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    GtkBuilder *builder = gtk_builder_new();
104    if (!gtk_builder_add_from_file(builder, "src/gtk/editor.xml", NULL))
105    {
106        g_print("Cannot build from XML\n");
107        return EXIT_FAILURE;
108    }
109
110    /* Create new top level window. */
111    GtkWidget *window = GTK_WIDGET(gtk_builder_get_object(builder, "window1"));
112
113    /* You should always delete gtk_gl_area widgets before exit or else
114       GLX contexts are left undeleted, this may cause problems (=core dump)
115       in some systems.
116       Destroy method of objects is not automatically called on exit.
117       You need to manually enable this feature. Do gtk_quit_add_destroy()
118       for all your top level windows unless you are certain that they get
119       destroy signal by other means.
120    */
121    gtk_quit_add_destroy(1, GTK_OBJECT(window));
122
123    /* Create new OpenGL widget. */
124    int attrlist[] =
125    {
126        GDK_GL_RGBA,
127        GDK_GL_RED_SIZE, 1,
128        GDK_GL_GREEN_SIZE, 1,
129        GDK_GL_BLUE_SIZE, 1,
130        GDK_GL_DOUBLEBUFFER,
131        GDK_GL_NONE
132    };
133
134    GtkWidget *glarea = gtk_gl_area_new(attrlist);
135
136    gtk_widget_set_events(GTK_WIDGET(glarea),
137                          GDK_EXPOSURE_MASK | GDK_BUTTON_PRESS_MASK);
138
139    gtk_signal_connect(GTK_OBJECT(window), "delete_event",
140                       GTK_SIGNAL_FUNC(main_quit), NULL);
141    gtk_signal_connect(GTK_OBJECT(glarea), "expose_event",
142                       GTK_SIGNAL_FUNC(draw), NULL);
143    gtk_signal_connect(GTK_OBJECT(glarea), "configure_event",
144                       GTK_SIGNAL_FUNC(reshape), NULL);
145    gtk_signal_connect(GTK_OBJECT(glarea), "realize",
146                       GTK_SIGNAL_FUNC(init), NULL);
147
148    // Create a scrolled window around our GL widget
149    GtkWidget *sw = GTK_WIDGET(gtk_builder_get_object(builder, "scrolledwindow1"));
150    gtk_scrolled_window_add_with_viewport(GTK_SCROLLED_WINDOW(sw), glarea);
151
152    /* Put scrolled window into main window */
153    gtk_widget_show_all(GTK_WIDGET(window));
154
155    // FIXME: detect when the game is killed
156    new Game("maps/testmap.tmx");
157    new DebugFps();
158
159    //gtk_idle_add(tick, glarea);
160    gtk_timeout_add(1000 / FPS, tick, glarea);
161
162    gtk_main();
163
164    return EXIT_SUCCESS;
165}
166
Note: See TracBrowser for help on using the repository browser.