NHDT->ANH, nethack->anethack, nhdat->anhdat
[aNetHack.git] / win / gnome / gnglyph.c
blob111db4c02186ddd9c05c8162f710b4a816991c17
1 /* aNetHack 0.0.1 gnglyph.c $ANH-Date: 1432512806 2015/05/25 00:13:26 $ $ANH-Branch: master $:$ANH-Revision: 1.10 $ */
2 /* Copyright (C) 1998 by Erik Andersen <andersee@debian.org> */
3 /* aNetHack may be freely redistributed. See license for details. */
5 #include "gnglyph.h"
6 #include "tile2x11.h"
8 /* from tile.c */
9 extern int total_tiles_used;
11 static GHackGlyphs ghack_glyphs;
12 static GdkImlibImage **ghack_tiles = NULL;
14 /* NAME:
15 * ghack_init_glyphs(char* xpm_file)
17 * ARGUMENTS:
18 * char *xpm_file -- The name of the image file.
19 * May be any image format imlib recognizes.
20 * Does not have to be XPM.
22 * RETURNS:
23 * TRUE upon successful loading of the glyphs.
24 * FALSE upon failure.
26 * PURPOSE:
27 * Constructor for the Glyph object. Well, really each glyph
28 * object is a collection of glyphs, or tiles. This constructor
29 * takes a single argument: the name of the image file that contains
30 * the tile images.
32 * NOTES:
33 * The glyphs (tiles) must be in the image in a certain way: the
34 * glyphs must be stacked such that the resultant image is
35 * TILE_X * TILES_PER_ROW wide, and
36 * TILE_Y * (number of glyphs) / TILES_PER_ROW high (rounded up).
37 * In this sense, TILE_X == TILE_Y, and can be any reasonable integer
38 * say, 16 <= TILE_X <= 64. Because the glyph number is tightly
39 * coupled to the Nethack object it represents, the order of the
40 * glyphs in the image is imporant: Glyph 1 is at the top of the
41 * image, while Glyph N (the last glyph) is at the bottom.
43 * What's the difference between a glyph and a tile? Well, a
44 * tile is just an image. A glyph is a tile that knows its
45 * place in line.
47 * This initializer relies heavily on gdk_imlib. Thanks, Rasterman.
50 int
51 ghack_init_glyphs(const char *xpmFile)
53 ghack_glyphs.im = gdk_imlib_load_image((char *) xpmFile);
54 if (!ghack_glyphs.im) {
55 g_error("Couldn't load required xpmFile!");
56 return -1;
59 gdk_imlib_render(ghack_glyphs.im, ghack_glyphs.im->rgb_width,
60 ghack_glyphs.im->rgb_height);
62 if ((ghack_glyphs.im->rgb_width % TILES_PER_ROW) != 0
63 || ghack_glyphs.im->rgb_width <= TILES_PER_ROW) {
64 g_error(
65 "%s is not a multiple of %d (number of tiles/row) pixels wide",
66 xpmFile, TILES_PER_ROW);
67 return -1;
69 ghack_glyphs.count = total_tiles_used;
70 if ((ghack_glyphs.count % TILES_PER_ROW) != 0) {
71 ghack_glyphs.count +=
72 TILES_PER_ROW - (ghack_glyphs.count % TILES_PER_ROW);
74 ghack_glyphs.width = ghack_glyphs.im->rgb_width / TILES_PER_ROW;
75 ghack_glyphs.height =
76 ghack_glyphs.im->rgb_height / (ghack_glyphs.count / TILES_PER_ROW);
78 /* Assume the tiles are organized in rows of TILES_PER_ROW */
79 ghack_tiles = g_new0(GdkImlibImage *, ghack_glyphs.count);
80 return (ghack_tiles == NULL) ? -1 : 0;
83 void
84 ghack_free_glyphs()
86 int i;
87 for (i = 0; i < ghack_glyphs.count; i++)
88 gdk_imlib_destroy_image(ghack_tiles[i]);
89 g_free(ghack_tiles);
90 gdk_imlib_destroy_image(ghack_glyphs.im);
91 ghack_glyphs.im = NULL;
94 /* NAME:
95 * ghack_glyph_count( )
97 * ARGUMENTS:
98 * None.
100 * RETURNS:
101 * int -- The number of glyphs in this object.
103 * PURPOSE:
104 * Simply reports the number of glyphs in this object.
108 ghack_glyph_count()
110 return ghack_glyphs.count;
113 /* NAME:
114 * ghack_glyph_height()
116 * ARGUMENTS:
117 * None
119 * RETURNS:
120 * int -- The glyph height.
122 * PURPOSE:
123 * Returns the standard glyph height.
127 ghack_glyph_height()
129 return ghack_glyphs.height;
132 /* NAME:
133 * ghack_glyph_width()
135 * ARGUMENTS:
136 * None
138 * RETURNS:
139 * int -- The glyph width.
141 * PURPOSE:
142 * Returns the standard glyph width.
146 ghack_glyph_width()
148 return ghack_glyphs.width;
151 /* NAME:
152 * ghack_image_from_glyph( int glyph, gboolean force)
154 * ARGUMENTS:
155 * int glyph -- The glyph number.
156 * gboolean force -- force it to re-render.
158 * RETURNS:
159 * GdkImlibImage* -- The glyph image, as a GdkImlibImage.
161 * PURPOSE:
162 * Decodes the glyph into an image suitable for manipulation
165 GdkImlibImage *
166 ghack_image_from_glyph(int glyph, gboolean force)
168 int tile = glyph2tile[glyph];
170 if (tile >= ghack_glyphs.count || tile < 0) {
171 g_warning(
172 "Aiiee! I've was asked for a tile outside the allowed range!\n"
173 "Email this to other-gnomehack@lists.debian.org");
174 g_warning("Max tile: %d Tile asked for: %d", ghack_glyphs.count,
175 tile);
176 return NULL;
179 if (ghack_glyphs.im == NULL) {
180 g_warning("Aiiee! I've been asked to clone from a null image.\n"
181 "Email this to other-gnomehack@lists.debian.org");
182 g_warning("making image from tile %d, force=%s\n", tile,
183 (force == TRUE) ? "TRUE" : "FALSE");
186 if (force == TRUE) {
187 g_warning("Aiiee! I've been asked to force rendering.\n"
188 "Email this to other-gnomehack@lists.debian.org");
189 g_warning("making image from tile %d, force=%s\n", tile,
190 (force == TRUE) ? "TRUE" : "FALSE");
193 if (!ghack_tiles[tile] || force) {
194 int src_x, src_y;
195 #if 0
196 fprintf( stderr, "crop_and_clone: glyph=%d, tile=%d, ptr=%p, x=%d, y=%d, w=%d, h=%d\n", glyph, tile,
197 (void*)&(ghack_tiles[tile]), 0,
198 tile * ghack_glyphs.width,
199 ghack_glyphs.height,
200 ghack_glyphs.width);
201 #endif
202 if (ghack_glyphs.im->pixmap == NULL)
203 g_warning("Aiiee! ghack_glyphs.im->pixmap==NULL!!!!\n");
204 src_x = (tile % TILES_PER_ROW) * ghack_glyphs.width;
205 src_y = (tile / TILES_PER_ROW) * ghack_glyphs.height;
206 ghack_tiles[tile] = gdk_imlib_crop_and_clone_image(
207 ghack_glyphs.im, src_x, src_y, ghack_glyphs.width,
208 ghack_glyphs.height);
211 if (ghack_tiles[tile] && (!ghack_tiles[tile]->pixmap || force)) {
212 if (gdk_imlib_render(ghack_tiles[tile], ghack_tiles[tile]->rgb_width,
213 ghack_tiles[tile]->rgb_height) == 0) {
214 g_error("GLYPH: couldn't create tile # %d", tile);
216 if (!ghack_tiles[tile]->pixmap)
217 g_error("Strange, tile # %d didn't get rendered???", tile);
220 return ghack_tiles[tile];