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

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

Minor cleanup in the editor main code.

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