From 2a5d90cc332757df1a052fee2a95f8e1dfd07105 Mon Sep 17 00:00:00 2001 From: Sam Hocevar Date: Sun, 22 Aug 2010 04:28:22 +0000 Subject: [PATCH] New MapViewer class and some GTK+ refactoring. --- src/Makefile.am | 2 +- src/core.h | 1 + src/gtk/editor.cpp | 38 +++++++-------------- src/gtk/editor.xml | 2 +- src/mapviewer.cpp | 85 ++++++++++++++++++++++++++++++++++++++++++++++ src/mapviewer.h | 41 ++++++++++++++++++++++ 6 files changed, 142 insertions(+), 27 deletions(-) create mode 100644 src/mapviewer.cpp create mode 100644 src/mapviewer.h diff --git a/src/Makefile.am b/src/Makefile.am index a75588bd..a8d4a95d 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -8,7 +8,7 @@ libcommon_a_SOURCES = \ scene.cpp scene.h font.cpp font.h layer.cpp layer.h map.cpp map.h \ entity.cpp entity.h ticker.cpp ticker.h tileset.cpp tileset.h \ forge.cpp forge.h video.cpp video.h timer.cpp timer.h bitfield.h \ - profiler.cpp profiler.h input.h input.cpp \ + profiler.cpp profiler.h input.h input.cpp mapviewer.cpp mapviewer.h \ debugfps.cpp debugfps.h debugsprite.cpp debugsprite.h \ debugrecord.cpp debugrecord.h libcommon_a_CXXFLAGS = `pkg-config --cflags sdl gl SDL_image` diff --git a/src/core.h b/src/core.h index 1edabe78..532fef53 100644 --- a/src/core.h +++ b/src/core.h @@ -26,6 +26,7 @@ #include "font.h" #include "game.h" #include "tileset.h" +#include "mapviewer.h" // Other objects #include "dict.h" diff --git a/src/gtk/editor.cpp b/src/gtk/editor.cpp index 9f4fb1b7..1bfc91cf 100644 --- a/src/gtk/editor.cpp +++ b/src/gtk/editor.cpp @@ -76,7 +76,7 @@ static gint draw(GtkWidget *widget, GdkEventExpose *event) ticking = 0; /* Clear the screen, tick the renderer, show the frame and - * clamp to desired framerate. */ + * clamp to desired framerate */ Video::Clear(); Ticker::TickDraw(); gtk_gl_area_swapbuffers(GTK_GL_AREA(widget)); @@ -100,6 +100,7 @@ int main(int argc, char **argv) return EXIT_FAILURE; } + /* Build the rest of the application */ GtkBuilder *builder = gtk_builder_new(); if (!gtk_builder_add_from_file(builder, "src/gtk/editor.xml", NULL)) { @@ -107,20 +108,11 @@ int main(int argc, char **argv) return EXIT_FAILURE; } - /* Create new top level window. */ GtkWidget *window = GTK_WIDGET(gtk_builder_get_object(builder, "window1")); + GtkWidget *sw = GTK_WIDGET(gtk_builder_get_object(builder, "sw1")); + g_object_unref(G_OBJECT(builder)); - /* You should always delete gtk_gl_area widgets before exit or else - GLX contexts are left undeleted, this may cause problems (=core dump) - in some systems. - Destroy method of objects is not automatically called on exit. - You need to manually enable this feature. Do gtk_quit_add_destroy() - for all your top level windows unless you are certain that they get - destroy signal by other means. - */ - gtk_quit_add_destroy(1, GTK_OBJECT(window)); - - /* Create new OpenGL widget. */ + /* Create new OpenGL widget */ int attrlist[] = { GDK_GL_RGBA, @@ -132,10 +124,10 @@ int main(int argc, char **argv) }; GtkWidget *glarea = gtk_gl_area_new(attrlist); + gtk_widget_set_events(glarea, GDK_EXPOSURE_MASK | GDK_BUTTON_PRESS_MASK); + gtk_scrolled_window_add_with_viewport(GTK_SCROLLED_WINDOW(sw), glarea); - gtk_widget_set_events(GTK_WIDGET(glarea), - GDK_EXPOSURE_MASK | GDK_BUTTON_PRESS_MASK); - + /* Connect signals and show window */ gtk_signal_connect(GTK_OBJECT(window), "delete_event", GTK_SIGNAL_FUNC(main_quit), NULL); gtk_signal_connect(GTK_OBJECT(glarea), "expose_event", @@ -145,19 +137,15 @@ int main(int argc, char **argv) gtk_signal_connect(GTK_OBJECT(glarea), "realize", GTK_SIGNAL_FUNC(init), NULL); - // Create a scrolled window around our GL widget - GtkWidget *sw = GTK_WIDGET(gtk_builder_get_object(builder, "scrolledwindow1")); - gtk_scrolled_window_add_with_viewport(GTK_SCROLLED_WINDOW(sw), glarea); - - /* Put scrolled window into main window */ - gtk_widget_show_all(GTK_WIDGET(window)); + gtk_widget_show_all(window); - // FIXME: detect when the game is killed - new Game("maps/testmap.tmx"); + // FIXME: detect when the map viewer is killed + new MapViewer("maps/testmap.tmx"); new DebugFps(); /* We tick from the idle function instead of a timeout to avoid - * stealing time from the GTK loop. */ + * stealing time from the GTK loop when the callback time exceeds + * the timeout value. */ gtk_idle_add(tick, glarea); gtk_main(); diff --git a/src/gtk/editor.xml b/src/gtk/editor.xml index af6e41ec..70c3b6ef 100644 --- a/src/gtk/editor.xml +++ b/src/gtk/editor.xml @@ -153,7 +153,7 @@ True True - + 640 480 True diff --git a/src/mapviewer.cpp b/src/mapviewer.cpp new file mode 100644 index 00000000..144c669a --- /dev/null +++ b/src/mapviewer.cpp @@ -0,0 +1,85 @@ +// +// Deus Hax (working title) +// Copyright (c) 2010 Sam Hocevar +// + +#if defined HAVE_CONFIG_H +# include "config.h" +#endif + +#include + +#include "core.h" + +/* + * MapViewer implementation class + */ + +class MapViewerData +{ + friend class MapViewer; + +private: + Map *map; + int x, y; + int mousex, mousey; + int done; + + Scene *scene; +}; + +/* + * Public MapViewer class + */ + +MapViewer::MapViewer(char const *mapname) +{ + data = new MapViewerData(); + data->map = new Map(mapname); + data->x = data->y = 0; + data->done = 0; + data->scene = NULL; +} + +MapViewer::~MapViewer() +{ + delete data->map; + delete data; +} + +Entity::Group MapViewer::GetGroup() +{ + return Entity::GetGroup(); +} + +void MapViewer::TickGame(float deltams) +{ + Entity::TickGame(deltams); +} + +void MapViewer::TickDraw(float deltams) +{ + Entity::TickDraw(deltams); + + GetScene(); + + data->map->Render(data->scene, -data->mousex, -data->mousey, 0); + data->scene->Render(); + + delete data->scene; + data->scene = NULL; +} + +Scene *MapViewer::GetScene() +{ + if (!data->scene) + data->scene = new Scene(); + return data->scene; +} + +void MapViewer::SetMouse(int x, int y) +{ + data->mousex = x; + data->mousey = y; +} + diff --git a/src/mapviewer.h b/src/mapviewer.h new file mode 100644 index 00000000..61abfd38 --- /dev/null +++ b/src/mapviewer.h @@ -0,0 +1,41 @@ +// +// Deus Hax (working title) +// Copyright (c) 2010 Sam Hocevar +// + +// +// The MapViewer class +// ------------------- +// + +#if !defined __DH_MAPVIEWER_H__ +#define __DH_MAPVIEWER_H__ + +#include "entity.h" +#include "scene.h" + +class MapViewerData; + +class MapViewer : public Entity +{ +public: + MapViewer(char const *mapname); + ~MapViewer(); + +protected: + /* Inherited from Entity */ + virtual Group GetGroup(); + virtual void TickGame(float deltams); + virtual void TickDraw(float deltams); + +public: + /* New methods */ + Scene *GetScene(); + void SetMouse(int x, int y); + +private: + MapViewerData *data; +}; + +#endif // __DH_MAPVIEWER_H__ +