OpenClonk
mapgen.h File Reference
#include <glib.h>
#include <gdk-pixbuf/gdk-pixbuf.h>
#include "mape/material.h"
#include "mape/texture.h"
Include dependency graph for mapgen.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Typedefs

typedef G_BEGIN_DECLS enum _MapeMapgenError MapeMapgenError
 
typedef enum _MapeMapgenType MapeMapgenType
 

Enumerations

enum  _MapeMapgenError { MAPE_MAPGEN_ERROR_COMPILE , MAPE_MAPGEN_ERROR_MEMORY }
 
enum  _MapeMapgenType { MAPE_MAPGEN_NONE , MAPE_MAPGEN_LANDSCAPE_TXT , MAPE_MAPGEN_MAP_C }
 

Functions

gboolean mape_mapgen_init (GError **error)
 
void mape_mapgen_deinit ()
 
void mape_mapgen_set_root_group (MapeGroup *group)
 
GdkPixbuf * mape_mapgen_render (const gchar *filename, const gchar *source, MapeMapgenType type, const gchar *script_path, MapeMaterialMap *material_map, MapeTextureMap *texture_map, guint width, guint height, GError **error)
 

Typedef Documentation

◆ MapeMapgenError

typedef G_BEGIN_DECLS enum _MapeMapgenError MapeMapgenError

MapeMapgenError: @MAPE_MAPGEN_ERROR_COMPILE: An error occured while compiling the Landscape.txt source code. @MAPE_GROUP_ERROR_MEMORY: Insufficient memory was available to render the map.

These errors are from the MAPE_MAPGEN_ERROR error domain. They can occur when rendering a map from a Landscape.txt file.

◆ MapeMapgenType

MapeMapgenType: @MAPE_MAPGEN_NONE: Does not represent a map description. @MAPE_MAPGEN_LANDSCAPE_TXT: Represents a Landscape.txt script. @MAPE_MAPGEN_MAP_C: Represents a Map.c script.

Specifies the different types of maps that can be rendered.

Enumeration Type Documentation

◆ _MapeMapgenError

MapeMapgenError: @MAPE_MAPGEN_ERROR_COMPILE: An error occured while compiling the Landscape.txt source code. @MAPE_GROUP_ERROR_MEMORY: Insufficient memory was available to render the map.

These errors are from the MAPE_MAPGEN_ERROR error domain. They can occur when rendering a map from a Landscape.txt file.

Enumerator
MAPE_MAPGEN_ERROR_COMPILE 
MAPE_MAPGEN_ERROR_MEMORY 

Definition at line 37 of file mapgen.h.

37  {
G_BEGIN_DECLS enum _MapeMapgenError MapeMapgenError
@ MAPE_MAPGEN_ERROR_COMPILE
Definition: mapgen.h:38
@ MAPE_MAPGEN_ERROR_MEMORY
Definition: mapgen.h:39

◆ _MapeMapgenType

MapeMapgenType: @MAPE_MAPGEN_NONE: Does not represent a map description. @MAPE_MAPGEN_LANDSCAPE_TXT: Represents a Landscape.txt script. @MAPE_MAPGEN_MAP_C: Represents a Map.c script.

Specifies the different types of maps that can be rendered.

Enumerator
MAPE_MAPGEN_NONE 
MAPE_MAPGEN_LANDSCAPE_TXT 
MAPE_MAPGEN_MAP_C 

Definition at line 50 of file mapgen.h.

50  {
enum _MapeMapgenType MapeMapgenType
@ MAPE_MAPGEN_MAP_C
Definition: mapgen.h:53
@ MAPE_MAPGEN_LANDSCAPE_TXT
Definition: mapgen.h:52
@ MAPE_MAPGEN_NONE
Definition: mapgen.h:51

Function Documentation

◆ mape_mapgen_deinit()

void mape_mapgen_deinit ( )

mape_mapgen_deinit():

Deinitializes the map generator.

Definition at line 156 of file mapgen.c.

157 {
159 }
void c4_mapgen_handle_deinit_script_engine()

References c4_mapgen_handle_deinit_script_engine().

Here is the call graph for this function:

◆ mape_mapgen_init()

gboolean mape_mapgen_init ( GError **  error)

mape_mapgen_init: @error: Location to store error information, if any.

Initializes the map generator.

Returns: TRUE on success or FALSE on error.

Definition at line 144 of file mapgen.c.

145 {
147  return TRUE;
148 }
void c4_mapgen_handle_init_script_engine()

References c4_mapgen_handle_init_script_engine().

Here is the call graph for this function:

◆ mape_mapgen_render()

GdkPixbuf* mape_mapgen_render ( const gchar *  filename,
const gchar *  source,
MapeMapgenType  type,
const gchar *  script_path,
MapeMaterialMap material_map,
MapeTextureMap texture_map,
guint  width,
guint  height,
GError **  error 
)

mape_mapgen_render:

@filename: The filename of the file that is being parsed. This is only used for display purposes. @source: The map generator source code for the map to generate. @type: Specifies how the text in @source should be interpreted. Must not be MAPE_MAPGEN_NONE. @script_path: Path to the script source for algo=script overlays, or NULL. @material_map: The material map containing the materials to be used during map generation. @texture_map: The texture map containing the textures to be used during map generation. @width: The width of the map to generate. @height: The height of the map to generate. @error: Location to store error information, if any, or NULL.

Renders the map described by @source with the C4MapCreatorS2 into a pixbuf. The pixel color depends on the texture at the corresponding position and is determined by the average color of that texture.

If the source contains one or more algo=script overlays and @script_path is NULL, an error is generated. Otherwise, the file at @script_path is opened and used to look up the relevant script functions.

In case an error occurs, for example when the map generator source code is not valid, @error is set and the function returns NULL.

Return Value: A #GdkPixbuf with the generated map, or NULL. Free with g_object_unref().

Definition at line 227 of file mapgen.c.

236 {
237  C4MapgenHandle* handle;
238  const char* error_message;
239  unsigned int out_width;
240  unsigned int out_height;
241  GdkPixbuf* pixbuf;
242  guint8* out_p;
243  const unsigned char* in_p;
244  guint out_rowstride;
245  unsigned int in_rowstride;
246  guint datawidth;
247  guint8 matclrs[256 * 4];
248  unsigned int x, y;
249  unsigned int matnum;
250 
251  switch(type)
252  {
254  handle = c4_mapgen_handle_new(
255  filename,
256  source,
257  script_path,
258  _mape_material_map_get_handle(material_map),
259  _mape_texture_map_get_handle(texture_map),
260  width,
261  height
262  );
263 
264  break;
265  case MAPE_MAPGEN_MAP_C:
267  filename,
268  source,
269  _mape_material_map_get_handle(material_map),
270  _mape_texture_map_get_handle(texture_map),
271  width,
272  height
273  );
274 
275  break;
276  default:
277  handle = NULL;
278  g_assert_not_reached();
279  break;
280  }
281 
282  error_message = c4_mapgen_handle_get_error(handle);
283  if(error_message)
284  {
285  g_set_error(
286  error,
287  mape_mapgen_error_quark(),
289  "%s",
290  error_message
291  );
292 
293  c4_mapgen_handle_free(handle);
294  return NULL;
295  }
296 
297  out_width = c4_mapgen_handle_get_width(handle);
298  out_height = c4_mapgen_handle_get_height(handle);
299 
300  pixbuf = gdk_pixbuf_new(
301  GDK_COLORSPACE_RGB,
302  FALSE,
303  8,
304  out_width,
305  out_height
306  );
307 
308  if(pixbuf == NULL)
309  {
310  g_set_error(
311  error,
312  mape_mapgen_error_quark(),
314  "Insufficient memory is available"
315  );
316 
317  c4_mapgen_handle_free(handle);
318  return NULL;
319  }
320 
321  out_p = gdk_pixbuf_get_pixels(pixbuf);
322  in_p = c4_mapgen_handle_get_map(handle);
323  out_rowstride = gdk_pixbuf_get_rowstride(pixbuf);
324  in_rowstride = c4_mapgen_handle_get_rowstride(handle);
325  datawidth = gdk_pixbuf_get_width(pixbuf) * 3;
326  memset(matclrs, 0, sizeof(matclrs) );
327 
328  for(y = 0; y < out_height; ++y)
329  {
330  for(x = 0; x < out_width; ++x)
331  {
332  matnum = *in_p;
333 
334  if(matclrs[matnum * 4] == 0)
335  {
336  mape_mapgen_read_color(
337  matclrs,
338  material_map,
339  texture_map,
340  matnum
341  );
342 
343  /* Color has been loaded */
344  matclrs[matnum * 4] = 1;
345  }
346 
347  out_p[0] = matclrs[matnum * 4 + 1];
348  out_p[1] = matclrs[matnum * 4 + 2];
349  out_p[2] = matclrs[matnum * 4 + 3];
350  ++in_p;
351  out_p += 3;
352  }
353 
354  in_p += in_rowstride - out_width;
355  out_p += out_rowstride - datawidth;
356  }
357 
358  c4_mapgen_handle_free(handle);
359  return pixbuf;
360 }
typedefG_BEGIN_DECLS struct _C4MapgenHandle C4MapgenHandle
Definition: log-handle.h:23
unsigned int c4_mapgen_handle_get_height(C4MapgenHandle *mapgen)
const char * c4_mapgen_handle_get_error(C4MapgenHandle *mapgen)
const unsigned char * c4_mapgen_handle_get_map(C4MapgenHandle *mapgen)
C4MapgenHandle * c4_mapgen_handle_new_script(const char *filename, const char *source, C4MaterialMapHandle *material_map, C4TextureMapHandle *texture_map, unsigned int map_width, unsigned int map_height)
unsigned int c4_mapgen_handle_get_width(C4MapgenHandle *mapgen)
void c4_mapgen_handle_free(C4MapgenHandle *mapgen)
C4MapgenHandle * c4_mapgen_handle_new(const char *filename, const char *source, const char *script_path, C4MaterialMapHandle *material_map, C4TextureMapHandle *texture_map, unsigned int map_width, unsigned int map_height)
unsigned int c4_mapgen_handle_get_rowstride(C4MapgenHandle *mapgen)
C4TextureMapHandle * _mape_texture_map_get_handle(MapeTextureMap *map)
Definition: texture.c:591
C4MaterialMapHandle * _mape_material_map_get_handle(MapeMaterialMap *map)
Definition: material.c:396

References _mape_material_map_get_handle(), _mape_texture_map_get_handle(), c4_mapgen_handle_get_error(), c4_mapgen_handle_new(), c4_mapgen_handle_new_script(), MAPE_MAPGEN_LANDSCAPE_TXT, and MAPE_MAPGEN_MAP_C.

Here is the call graph for this function:

◆ mape_mapgen_set_root_group()

void mape_mapgen_set_root_group ( MapeGroup group)

mape_mapgen_set_root_group: @group: The root group.

Sets the root group for the map generator. This group is used to lookup the Library_Map definition.

Definition at line 169 of file mapgen.c.

170 {
171  MapeGroup* objects;
172  MapeGroup* libraries;
173  MapeGroup* map;
174  GError* error;
175 
176  error = NULL;
177  if(!error)
178  objects = mape_group_open_child(group, "Objects.ocd", &error);
179  if(!error)
180  libraries = mape_group_open_child(objects, "Libraries.ocd", &error);
181  if(!error)
182  map = mape_group_open_child(libraries, "Map.ocd", &error);
183 
184  /* TODO: Error reporting? */
185  if(error == NULL)
187 
188  if(error != NULL)
189  {
190  fprintf(stderr, "Failed to load Objects.ocd/Libraries.ocd/Map.ocd/Script.c: %s\n", error->message);
191  g_error_free(error);
192  }
193 }
MapeGroup * mape_group_open_child(MapeGroup *group, const gchar *entry, GError **error)
Definition: group.c:301
void c4_mapgen_handle_set_map_library(C4GroupHandle *group_handle)
C4GroupHandle * _mape_group_get_handle(MapeGroup *group)
Definition: group.c:682

References _mape_group_get_handle(), c4_mapgen_handle_set_map_library(), and mape_group_open_child().

Here is the call graph for this function: