1 /*
2  * mape - C4 Landscape.txt editor
3  *
4  * Copyright (c) 2005-2009, Armin Burgmeier
5  *
6  * Distributed under the terms of the ISC license; see accompanying file
7  * "COPYING" for details.
8  *
9  * "Clonk" is a registered trademark of Matthes Bender, used with permission.
10  * See accompanying file "TRADEMARK" for details.
11  *
12  * To redistribute this file separately, substitute the full license texts
13  * for the above references.
14  */
15 
16 #include "C4Include.h"
17 #include "landscape/C4Material.h"
18 #include "landscape/C4Texture.h"
19 #include "mape/cpp-handles/material-handle.h"
20 
21 #define MATERIAL_MAP_TO_HANDLE(material_map) (reinterpret_cast<C4MaterialMapHandle*>(material_map))
22 #define HANDLE_TO_MATERIAL_MAP(handle) (reinterpret_cast<C4MaterialMap*>(handle))
23 
24 #define MATERIAL_TO_HANDLE(material) (reinterpret_cast<C4MaterialHandle*>(material))
25 #define HANDLE_TO_MATERIAL(handle) (reinterpret_cast<C4Material*>(handle))
26 
27 #define HANDLE_TO_TEXTURE_MAP(handle) (reinterpret_cast<C4TextureMap*>(handle))
28 
29 #define GROUP_TO_HANDLE(group) (reinterpret_cast<C4GroupHandle*>(group))
30 #define HANDLE_TO_GROUP(handle) (reinterpret_cast<C4Group*>(handle))
31 
32 extern "C" {
33 
c4_material_map_handle_new(void)34 C4MaterialMapHandle* c4_material_map_handle_new(void)
35 {
36   // Simply return a pointer to the global material map. This is a bit stupid,
37   // but when looking up a material-texture combination, C4TextureMap uses the
38   // global material map for the material lookup when the default texture is
39   // requested. This should be changed to get rid of the global variable, but yeah...
40   C4MaterialMap* map = &::MaterialMap;
41   map->Clear();
42   return MATERIAL_MAP_TO_HANDLE(map); //new C4MaterialMap);
43 }
44 
c4_material_map_handle_free(C4MaterialMapHandle * material_map)45 void c4_material_map_handle_free(C4MaterialMapHandle* material_map)
46 {
47   //delete HANDLE_TO_MATERIAL_MAP(material_map);
48 }
49 
c4_material_map_handle_load(C4MaterialMapHandle * material_map,C4GroupHandle * group)50 guint c4_material_map_handle_load(C4MaterialMapHandle* material_map, C4GroupHandle* group)
51 {
52   return HANDLE_TO_MATERIAL_MAP(material_map)->Load(*HANDLE_TO_GROUP(group));
53 }
54 
c4_material_map_crossmap_materials(C4MaterialMapHandle * material_map,C4TextureMapHandle * texture_map)55 void c4_material_map_crossmap_materials(C4MaterialMapHandle* material_map, C4TextureMapHandle* texture_map)
56 {
57   // Don't call the official crossmap function since it tries to load some surfaces.
58   // All we need is the default texture.
59   for(int i = 0; i < HANDLE_TO_MATERIAL_MAP(material_map)->Num; ++i)
60   {
61     C4Material* mat = &HANDLE_TO_MATERIAL_MAP(material_map)->Map[i];
62     const char* overlay = mat->sTextureOverlay.getData();
63     if(!overlay || *overlay == '\0')
64     {
65       int first_tex_map_entry = HANDLE_TO_TEXTURE_MAP(texture_map)->GetIndex(mat->Name, nullptr, false);
66       overlay = HANDLE_TO_TEXTURE_MAP(texture_map)->GetEntry(first_tex_map_entry)->GetTextureName();
67     }
68 
69     if(overlay)
70       mat->DefaultMatTex = HANDLE_TO_TEXTURE_MAP(texture_map)->GetIndex(mat->Name, overlay, true);
71   }
72 }
73 
c4_material_map_handle_get_num(C4MaterialMapHandle * material_map)74 guint c4_material_map_handle_get_num(C4MaterialMapHandle* material_map)
75 {
76   return HANDLE_TO_MATERIAL_MAP(material_map)->Num;
77 }
78 
c4_material_map_handle_get_material(C4MaterialMapHandle * material_map,guint index)79 C4MaterialHandle* c4_material_map_handle_get_material(C4MaterialMapHandle* material_map, guint index)
80 {
81   g_assert(index < (guint)(HANDLE_TO_MATERIAL_MAP(material_map)->Num));
82   return MATERIAL_TO_HANDLE(&HANDLE_TO_MATERIAL_MAP(material_map)->Map[index]);
83 }
84 
c4_material_handle_get_name(C4MaterialHandle * material)85 const gchar* c4_material_handle_get_name(C4MaterialHandle* material)
86 {
87   return HANDLE_TO_MATERIAL(material)->Name;
88 }
c4_material_handle_get_texture_overlay(C4MaterialHandle * material)89 const gchar* c4_material_handle_get_texture_overlay(C4MaterialHandle* material)
90 {
91   return HANDLE_TO_MATERIAL(material)->sTextureOverlay.getData();
92 }
93 
94 } /* extern "C" */
95