2006-08-14 Andrew Sayman <lorien420@myrealbox.com>
[crack-attack.git] / src / gtk-gui / persist.cxx
bloba2cfcee9a06aca8502b14e46414fb38ebffc32fd
1 /*
2 * persist.cxx
3 * Kevin Webb - 3/21/05
5 * Copyright (C) 2005 Kevin Webb
6 * Copyright (C) 2006 Andrew Sayman
7 *
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License
10 * as published by the Free Software Foundation; either version 2
11 * of the License, or (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
22 #include <gtk/gtk.h>
23 #include <fstream>
24 #include <cstring>
26 #include "../TextureLoader.h"
27 #include "persist.h"
28 #include "callbacks.h"
29 #include "interface.h"
30 #include "support.h"
32 void
33 gui_data_save (GtkWidget *widget)
36 gchar file_name[256];
37 GtkEntry *entPlayerName;
38 const gchar *tmp = NULL;
39 gchar player_name[GC_PLAYER_NAME_LENGTH];
40 gint resolution = 0;
41 gint quality = 0;
42 gint ai_difficulty;
44 // Get the current name in the name text box.
45 entPlayerName = GTK_ENTRY(lookup_widget(GTK_WIDGET(widget), "entPlayerName"));
46 if (entPlayerName) {
47 tmp = gtk_entry_get_text(entPlayerName);
48 if (tmp) {
49 if (strlen(tmp) == 0) {
50 gtk_entry_set_text(entPlayerName, g_get_user_name());
51 return;
53 g_strlcpy(player_name, tmp, 256);
57 // Get the currently selected resolution.
58 resolution = (gint) gtk_combo_box_get_active((GtkComboBox *) lookup_widget(GTK_WIDGET(widget), "cmbResolution"));
60 // Get the status of the quality combo box.
61 quality = gtk_combo_box_get_active((GtkComboBox *) lookup_widget(GTK_WIDGET(widget), "cmbQuality"));
63 // Get the current AI difficulty setting.
64 ai_difficulty = gtk_combo_box_get_active((GtkComboBox *) lookup_widget(GTK_WIDGET(widget), "cmbAI"));
66 // Generate the file name and write the data,
67 TextureLoader::buildLocalDataFileName(GC_GUI_FILE_NAME, file_name);
69 std::ofstream file(file_name);
70 if (file.fail()) {
71 std::cerr << "Unable to write gui data to file!\n";
72 return;
75 file << player_name << std::endl;
76 file << resolution << std::endl;
77 file << quality << std::endl;
78 file << ai_difficulty << std::endl;
80 file.close();
84 void
85 gui_data_read (GtkWidget *widget)
88 char buffer[256];
89 char file_name[256];
90 GtkEntry *entPlayerName;
91 gchar player_name[GC_PLAYER_NAME_LENGTH];
92 gint resolution = 0;
93 gint quality = 0;
94 gint ai_difficulty;
97 // Generate the name of the file and read the data.
98 TextureLoader::buildLocalDataFileName(GC_GUI_FILE_NAME, file_name);
100 std::ifstream file(file_name);
101 if (file.fail()) {
102 #ifdef DEVELOPMENT
103 std::cerr << "Unable to find gui data file!\n";
104 #endif
105 return;
108 if (!file.eof()) {
109 file.getline(buffer, 256);
110 strcpy(player_name, buffer);
112 file.getline(buffer, 256);
113 resolution = atoi(buffer);
115 if (resolution < 0 || resolution > 4) {
116 resolution = 0;
119 file.getline(buffer, 256);
120 quality = atoi(buffer);
122 if (quality < 0 || quality > 2) {
123 quality = 0;
126 file.getline(buffer, 256);
127 ai_difficulty = atoi(buffer);
129 if (ai_difficulty < 0 || ai_difficulty > 3) {
130 ai_difficulty = 0;
133 } else {
134 file.close();
135 return;
137 file.close();
139 // Set the name.
140 entPlayerName = GTK_ENTRY(lookup_widget(GTK_WIDGET(widget), "entPlayerName"));
141 if (entPlayerName) {
142 gtk_entry_set_text (GTK_ENTRY (entPlayerName), player_name);
145 // Set the resolution.
146 gtk_combo_box_set_active((GtkComboBox *) lookup_widget(GTK_WIDGET(widget), "cmbResolution"), resolution);
148 // Set the AI.
149 gtk_combo_box_set_active((GtkComboBox *) lookup_widget(GTK_WIDGET(widget), "cmbAI"), ai_difficulty);
151 // Set the quality.
152 gtk_combo_box_set_active((GtkComboBox *) lookup_widget(GTK_WIDGET(widget), "cmbQuality"), quality);