20130313
[gdash.git] / src / gtkmain.cpp
blob808ab3d0a4abc02f6f1e4109406ad4476eabe508
1 /*
2 * Copyright (c) 2007-2013, Czirkos Zoltan http://code.google.com/p/gdash/
4 * Permission to use, copy, modify, and distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 #include "config.h"
19 #include <gdk/gdkkeysyms.h>
20 #include <gtk/gtk.h>
21 #include <glib/gi18n.h>
22 #include <fstream>
24 #ifdef HAVE_SDL
25 /* on windows, sdl does some hack on the main function.
26 * therefore we need to include this file, even if it seems
27 * to do nothing. */
28 #include <SDL/SDL.h>
29 #endif
31 #include "cave/caveset.hpp"
32 #include "sound/sound.hpp"
33 #include "misc/util.hpp"
34 #include "misc/logger.hpp"
35 #include "misc/about.hpp"
36 #include "settings.hpp"
37 #include "editor/editor.hpp"
38 #include "editor/editorcellrenderer.hpp"
39 #include "editor/exporthtml.hpp"
40 #include "gtk/gtkpixbuffactory.hpp"
41 #include "gtk/gtkui.hpp"
42 #include "framework/commands.hpp"
43 #include "fileops/loadfile.hpp"
44 #include "fileops/binaryimport.hpp"
46 #include "sdl/sdlmainwindow.hpp"
47 #include "gtk/gtkmainwindow.hpp"
49 /* includes cavesets built in to the executable */
50 #include "levels.cpp"
54 int main(int argc, char *argv[]) {
55 CaveSet caveset;
56 int quit=0;
57 gboolean editor=FALSE;
58 char *gallery_filename=NULL;
59 char *png_filename=NULL, *png_size=NULL;
60 char *save_cave_name=NULL, *save_gds_name;
61 gboolean force_quit_no_gtk;
63 GError *error=NULL;
64 GOptionEntry entries[] = {
65 {"editor", 'e', 0, G_OPTION_ARG_NONE, &editor, N_("Start editor")},
66 {"save-gallery", 'g', 0, G_OPTION_ARG_FILENAME, &gallery_filename, N_("Save caveset in a HTML gallery")},
67 {"stylesheet", 0, 0, G_OPTION_ARG_STRING /* not filename! */, &gd_html_stylesheet_filename, N_("Link stylesheet from file to a HTML gallery, eg. \"../style.css\"")},
68 {"favicon", 0, 0, G_OPTION_ARG_STRING /* not filename! */, &gd_html_favicon_filename, N_("Link shortcut icon to a HTML gallery, eg. \"../favicon.ico\"")},
69 {"save-png", 'p', 0, G_OPTION_ARG_FILENAME, &png_filename, N_("Save image of first cave to PNG")},
70 {"png-size", 0, 0, G_OPTION_ARG_STRING, &png_size, N_("Set PNG image size. Default is 128x96, set to 0x0 for unscaled")},
71 {"save-bdcff", 's', 0, G_OPTION_ARG_FILENAME, &save_cave_name, N_("Save caveset in a BDCFF file")},
72 {"save-gds", 'd', 0, G_OPTION_ARG_FILENAME, &save_gds_name, N_("Save imported binary data to a GDS file. An input file name is required.")},
73 {"quit", 'q', 0, G_OPTION_ARG_NONE, &quit, N_("Batch mode: quit after specified tasks")},
74 {NULL}
77 GOptionContext *context=gd_option_context_new();
78 g_option_context_add_main_entries(context, entries, PACKAGE); /* gdash (gtk version) parameters */
79 g_option_context_add_group(context, gtk_get_option_group(FALSE)); /* add gtk parameters */
80 g_option_context_parse(context, &argc, &argv, &error);
81 g_option_context_free(context);
82 if (error) {
83 gd_warning(error->message);
84 g_error_free(error);
87 /* show license? */
88 if (gd_param_license) {
89 /* print license and quit. */
90 g_print("%s %s\n\n%s\n\n", PACKAGE_NAME, PACKAGE_VERSION, About::copyright);
91 std::vector<std::string> wrapped_license = gd_wrap_text(About::license, 72);
92 for (unsigned i = 0; i < wrapped_license.size(); ++i)
93 g_print("%s\n", wrapped_license[i].c_str());
94 return 0;
97 Logger global_logger;
99 gd_settings_init();
100 gd_settings_init_dirs();
102 gd_load_settings();
104 gtk_set_locale();
105 gd_settings_set_locale();
107 force_quit_no_gtk=FALSE;
108 if (!gtk_init_check(&argc, &argv))
109 force_quit_no_gtk=TRUE;
111 gd_settings_init_translation();
113 gd_cave_types_init();
114 gd_cave_objects_init();
116 /* if memory snapshot -> gds file conversion requested */
117 if (save_gds_name != NULL) {
118 Logger thislogger;
119 if (gd_param_cavenames==NULL || g_str_equal(gd_param_cavenames, "")) {
120 g_print("An input filename must be given for GDS conversion.\n");
121 return 1;
123 std::vector<unsigned char> file = load_file_to_vector(gd_param_cavenames[0]);
124 /* -1 because the file loader adds a terminating zero */
125 std::vector<unsigned char> memory = load_memory_dump(&file[0], file.size()-1);
126 std::vector<unsigned char> gds = gdash_binary_import(memory);
127 std::fstream os(save_gds_name, std::ios::out | std::ios::binary);
128 os.write((char *) &gds[0], gds.size());
130 thislogger.clear();
133 /* LOAD A CAVESET FROM A FILE, OR AN INTERNAL ONE */
134 /* if remaining arguments, they are filenames */
135 try {
136 if (gd_param_cavenames && gd_param_cavenames[0]) {
137 caveset=create_from_file(gd_param_cavenames[0]);
139 else {
140 /* if nothing requested, load default */
141 caveset = create_from_buffer(level_pointers[0], -1);
142 caveset.name = level_names[0];
143 caveset.load_highscore(gd_user_config_dir);
145 } catch (std::exception &e) {
146 /// @todo show error to the screen
147 gd_critical(e.what());
150 /* see if generating a gallery. */
151 /* but only if there are any caves at all. */
152 if (caveset.has_caves() && gallery_filename)
153 gd_save_html(gallery_filename, NULL, caveset);
155 /* save cave png */
156 if (png_filename) {
157 unsigned int size_x=128, size_y=96; /* default size */
159 if (png_size && (sscanf(png_size, "%ux%u", &size_x, &size_y) != 2))
160 gd_warning(CPrintf(_("Invalid image size: %s")) % png_size);
161 if (size_x<1 || size_y<1) {
162 size_x=0;
163 size_y=0;
166 /* rendering cave for png: seed=0 */
167 CaveRendered renderedcave(caveset.cave(0), 0, 0);
168 GTKPixbufFactory pf;
169 EditorCellRenderer cr(pf, gd_theme);
171 GdkPixbuf *pixbuf = gd_drawcave_to_pixbuf(&renderedcave, cr, size_x, size_y, true, false);
172 GError *error=NULL;
173 if (!gdk_pixbuf_save(pixbuf, png_filename, "png", &error, "compression", "9", NULL)) {
174 gd_critical(CPrintf("Error saving PNG image %s: %s") % png_filename % error->message);
175 g_error_free(error);
177 g_object_unref(pixbuf);
180 if (save_cave_name)
181 caveset.save_to_file(save_cave_name);
183 /* if batch mode, quit now */
184 if (quit) {
185 global_logger.clear();
186 return 0;
188 if (force_quit_no_gtk) {
189 gd_critical("Cannot initialize GUI");
190 return 1;
193 gd_register_stock_icons();
194 /* set gtk default icon */
195 GdkPixbuf *logo = gd_icon();
196 gtk_window_set_default_icon(logo);
197 g_object_unref(logo);
199 /* select first action */
200 NextAction na;
201 if (editor)
202 na = StartEditor;
203 else
204 na = StartTitle;
206 restart_from_here:
208 gd_sound_init();
209 gd_sound_set_music_volume();
210 gd_sound_set_chunk_volumes();
212 while (na != Quit) {
213 switch (na) {
214 case StartTitle:
215 gd_main_window_sdl_run(&caveset, na);
216 //~ gd_main_window_gtk_run(&caveset, na);
217 break;
218 case StartEditor:
219 na = StartTitle;
220 gd_cave_editor_run(&caveset);
221 break;
222 case Restart:
223 gd_sound_close();
224 na = StartTitle;
225 goto restart_from_here;
226 break;
227 case Quit:
228 break;
232 gd_sound_close();
234 caveset.save_highscore(gd_user_config_dir);
236 gd_save_settings();
238 global_logger.clear();
240 return 0;