Debian Codegraph @

src/EditorMap.cpp

raw
1 /**
2 * Map - group of screen references (dink.dat)
3
4 * Copyright (C) 1997, 1998, 1999, 2002, 2003 Seth A. Robinson
5 * Copyright (C) 2005, 2007, 2008, 2009, 2010, 2012, 2014, 2015 Sylvain Beucler
6
7 * This file is part of GNU FreeDink
8
9 * GNU FreeDink is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU General Public License as
11 * published by the Free Software Foundation; either version 3 of the
12 * License, or (at your option) any later version.
13
14 * GNU FreeDink is distributed in the hope that it will be useful, but
15 * WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * General Public License for more details.
18
19 * You should have received a copy of the GNU General Public License
20 * along with this program. If not, see
21 * <http://www.gnu.org/licenses/>.
22 */
23
24 #ifdef HAVE_CONFIG_H
25 #include <config.h>
26 #endif
27
28 #include <stdio.h>
29
30 #include "EditorMap.h"
31
32 #include "paths.h"
33 #include "io_util.h"
34
35 using namespace std;
36
37 EditorMap::EditorMap(string dink_dat, string map_dat) :
38 dink_dat(dink_dat), map_dat(map_dat) {}
39
40 /**
41 * Load dink.dat, an offsets index to screens stored in map.dat, with
42 * some metadata (midi #, indoor/outdoor)
43 */
44 bool EditorMap::load() {
45 FILE *f = NULL;
46
47 f = paths_dmodfile_fopen(dink_dat.c_str(), "rb");
48 if (!f)
49 return false;
50
51 /* Portably load EditorMap from disk */
52 int i = 0;
53 fseek(f, 20, SEEK_CUR); // unused 'name' field
54 for (i = 0; i < 769; i++)
55 loc[i] = read_lsb_int(f);
56 for (i = 0; i < 769; i++)
57 music[i] = read_lsb_int(f);
58 for (i = 0; i < 769; i++)
59 indoor[i] = read_lsb_int(f);
60 fseek(f, 2240, SEEK_CUR); // unused space
61
62 fclose(f);
63
64 memset(&ts_loc_mem, 0, sizeof(ts_loc_mem));
65
66 return true;
67 }
68
69 /**
70 * Save dink.dat (index of map offsets + midi# + indoor/outdoor)
71 * Only used by the editor
72 */
73 void EditorMap::save() {
74 FILE *f = paths_dmodfile_fopen(dink_dat.c_str(), "wb");
75 if (f == NULL)
76 {
77 perror("Cannot save dink.dat");
78 return;
79 }
80
81 /* Portably dump EditorMap to disk */
82 int i = 0;
83 char name[20] = "Smallwood";
84 fwrite(name, 20, 1, f);
85 for (i = 0; i < 769; i++)
86 write_lsb_int(loc[i], f);
87 for (i = 0; i < 769; i++)
88 write_lsb_int(music[i], f);
89 for (i = 0; i < 769; i++)
90 write_lsb_int(indoor[i], f);
91 fseek(f, 2240, SEEK_CUR); // unused field
92
93 fclose(f);
94 }