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

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

The frame counter is now an asset object.

  • Property svn:keywords set to Id
File size: 4.0 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
22volatile int quit = 0;
23
24static gint main_quit(GtkWidget *widget, GdkEventExpose *event)
25{
26    (void)widget;
27    (void)event;
28
29    quit = 1;
30    gtk_main_quit();
31    return FALSE;
32}
33
34static gboolean tick(void *widget)
35{
36    float const delta_time = 33.33333f;
37
38    // FIXME: do not do anything if the previous tick was too recent?
39
40    // FIXME: only quit if all assets have been cleaned
41    if (quit)
42        return FALSE;
43
44    /* Tick the game */
45    Ticker::TickGame(delta_time);
46
47    gtk_widget_draw(GTK_WIDGET(widget), NULL);
48
49    return TRUE;
50}
51
52static gint init(GtkWidget *widget)
53{
54    if (gtk_gl_area_make_current(GTK_GL_AREA(widget)))
55        Video::Setup(widget->allocation.width, widget->allocation.height);
56
57    return TRUE;
58}
59
60static gint reshape(GtkWidget *widget, GdkEventConfigure *event)
61{
62    (void)event;
63
64    return init(widget);
65}
66
67static gint draw(GtkWidget *widget, GdkEventExpose *event)
68{
69    if (event->count > 0)
70        return TRUE;
71
72    /* OpenGL functions can be called only if make_current returns true */
73    if (gtk_gl_area_make_current(GTK_GL_AREA(widget)))
74    {
75        // FIXME: do not do anything if the game tick wasn't called?
76        float const delta_time = 33.33333f;
77
78        /* Clear the screen, tick the renderer, and show the frame */
79        Video::Clear();
80        Ticker::TickRender(delta_time);
81        gtk_gl_area_swapbuffers(GTK_GL_AREA(widget));
82    }
83
84    return TRUE;
85}
86
87int main(int argc, char **argv)
88{
89    /* Initialize GTK */
90    gtk_init(&argc, &argv);
91
92    if (gdk_gl_query() == FALSE)
93    {
94        g_print("OpenGL not supported\n");
95        return EXIT_FAILURE;
96    }
97
98    /* Create new top level window. */
99    GtkWidget *window = gtk_window_new( GTK_WINDOW_TOPLEVEL);
100    gtk_window_set_title(GTK_WINDOW(window), "Simple");
101    gtk_container_set_border_width(GTK_CONTAINER(window), 5);
102
103    /* Quit form main if got delete event */
104    gtk_signal_connect(GTK_OBJECT(window), "delete_event",
105                       GTK_SIGNAL_FUNC(main_quit), NULL);
106
107    /* You should always delete gtk_gl_area widgets before exit or else
108       GLX contexts are left undeleted, this may cause problems (=core dump)
109       in some systems.
110       Destroy method of objects is not automatically called on exit.
111       You need to manually enable this feature. Do gtk_quit_add_destroy()
112       for all your top level windows unless you are certain that they get
113       destroy signal by other means.
114    */
115    gtk_quit_add_destroy(1, GTK_OBJECT(window));
116
117    /* Create new OpenGL widget. */
118    int attrlist[] =
119    {
120        GDK_GL_RGBA,
121        GDK_GL_RED_SIZE, 1,
122        GDK_GL_GREEN_SIZE, 1,
123        GDK_GL_BLUE_SIZE, 1,
124        GDK_GL_DOUBLEBUFFER,
125        GDK_GL_NONE
126    };
127
128    GtkWidget *glarea = gtk_gl_area_new(attrlist);
129
130    gtk_widget_set_events(GTK_WIDGET(glarea),
131                          GDK_EXPOSURE_MASK | GDK_BUTTON_PRESS_MASK);
132
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    // FIXME: is this needed?
141    gtk_widget_set_usize(GTK_WIDGET(glarea), 400, 300);
142
143    /* Put glarea into window and show it all */
144    gtk_container_add(GTK_CONTAINER(window), GTK_WIDGET(glarea));
145    gtk_widget_show(GTK_WIDGET(glarea));
146    gtk_widget_show(GTK_WIDGET(window));
147
148    // FIXME: detect when the game is killed
149    new Game("maps/testmap.tmx");
150    new DebugFps();
151
152    //gtk_idle_add(tick, glarea);
153    gtk_timeout_add(33, tick, glarea);
154    gtk_main();
155
156    return EXIT_SUCCESS;
157}
Note: See TracBrowser for help on using the repository browser.