]> git.draconx.ca Git - upkg.git/blob - src/engine/palette.c
engine: Implement Engine.Palette.
[upkg.git] / src / engine / palette.c
1 /*
2  *  upkg: tool for manipulating Unreal Tournament packages.
3  *  Copyright © 2011 Nick Bowler
4  *
5  *  This program is free software: you can redistribute it and/or modify
6  *  it under the terms of the GNU General Public License as published by
7  *  the Free Software Foundation, either version 3 of the License, or
8  *  (at your option) any later version.
9  *
10  *  This program is distributed in the hope that it will be useful,
11  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  *  GNU General Public License for more details.
14  *
15  *  You should have received a copy of the GNU General Public License
16  *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
17  */
18
19 #include <stdio.h>
20 #include <stdlib.h>
21
22 #include "palette.h"
23 #include "upkg.h"
24
25 G_DEFINE_DYNAMIC_TYPE(EnginePalette, engine_palette, U_OBJECT_TYPE);
26
27 static int deserialize(UObject *uo)
28 {
29         EnginePalette *p = ENGINE_PALETTE(uo);
30         struct upkg_file *f = uo->pkg_file;
31         unsigned char buf[16];
32         size_t rc, buflen;
33         long entries;
34
35         U_OBJECT_CLASS(engine_palette_parent_class)->deserialize(uo);
36
37         buflen = upkg_export_read(f, buf, sizeof buf);
38         rc = upkg_decode_index(&entries, buf, buflen);
39         if (rc == 0 || entries < 0)
40                 return -1;
41
42         upkg_export_seek(f, rc, SEEK_SET);
43
44         p->entries = entries;
45         p->rgba = malloc(entries * sizeof p->rgba[0]);
46         if (!p->rgba)
47                 return -1;
48
49         rc = upkg_export_read(f, p->rgba, entries * sizeof p->rgba[0]);
50         if (rc != entries * sizeof p->rgba[0]) {
51                 free(p->rgba);
52                 return -1;
53         }
54
55         printf("palette opened\n");
56         return 0;
57 }
58
59 void palette_register(GTypeModule *m)
60 {
61         engine_palette_register_type(m);
62 }
63
64 static void engine_palette_init(EnginePalette *p)
65 {
66         p->entries = 0;
67 }
68
69 static void engine_palette_class_init(EnginePaletteClass *class)
70 {
71         UObjectClass *uo = U_OBJECT_CLASS(class);
72
73         uo->deserialize = deserialize;
74 }
75
76 static void engine_palette_class_finalize(EnginePaletteClass *class)
77 {
78 }