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

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

Proper delta time computation in both the game and the editor.

  • 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 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    /* Create new top level window. */
105    GtkWidget *window = gtk_window_new( GTK_WINDOW_TOPLEVEL);
106    gtk_window_set_title(GTK_WINDOW(window), "Simple");
107    gtk_container_set_border_width(GTK_CONTAINER(window), 5);
108
109    /* Quit form main if got delete event */
110    gtk_signal_connect(GTK_OBJECT(window), "delete_event",
111                       GTK_SIGNAL_FUNC(main_quit), NULL);
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(glarea), "expose_event",
140                       GTK_SIGNAL_FUNC(draw), NULL);
141    gtk_signal_connect(GTK_OBJECT(glarea), "configure_event",
142                       GTK_SIGNAL_FUNC(reshape), NULL);
143    gtk_signal_connect(GTK_OBJECT(glarea), "realize",
144                       GTK_SIGNAL_FUNC(init), NULL);
145
146    // FIXME: is this needed?
147    gtk_widget_set_usize(GTK_WIDGET(glarea), 400, 300);
148
149    /* Put glarea into window and show it all */
150    gtk_container_add(GTK_CONTAINER(window), GTK_WIDGET(glarea));
151    gtk_widget_show(GTK_WIDGET(glarea));
152    gtk_widget_show(GTK_WIDGET(window));
153
154    // FIXME: detect when the game is killed
155    new Game("maps/testmap.tmx");
156    new DebugFps();
157
158    //gtk_idle_add(tick, glarea);
159    gtk_timeout_add(33, tick, glarea);
160
161    timer = g_timer_new();
162    gtk_main();
163
164    return EXIT_SUCCESS;
165}
Note: See TracBrowser for help on using the repository browser.