int luab_ParallaxLayer_new(lua_State *L) {
    ParallaxLayer_userdata *ud = NULL;
    Engine *engine = luaL_get_engine(L);

    ud = lua_newuserdata(L, sizeof(ParallaxLayer_userdata));
    check(ud != NULL, "Could not make ParralaxLayer userdata");

    luaL_getmetatable(L, luab_ParallaxLayer_metatable);
    lua_setmetatable(L, -2);

    const char *texname = lua_tostring(L, -2);
    GfxTexture *tex = Graphics_texture_from_image(engine->graphics,
            (char *)texname);
    check(tex != NULL, "Couldn't load image %s", texname);

    ParallaxLayer *layer = ParallaxLayer_create(tex);

    luaL_register_ud(L, -1, (void **)&ud->p, layer);
    return 1;
error:
    return 0;
}
Example #2
0
TileMapParseStatus TileMap_parse_tileset(xmlTextReaderPtr reader,
                                         Engine *engine, TileMap *map,
                                         Tileset **out_tileset) {
  TileMapParseStatus status = TILEMAP_PARSE_OK;
  Tileset *tileset = calloc(1, sizeof(Tileset));
  check(tileset != NULL, "Couldn't create tileset");
  
  while (xmlTextReaderMoveToNextAttribute(reader)) {
    xmlChar *attrName = xmlTextReaderName(reader);
    xmlChar *attrVal = xmlTextReaderValue(reader);
    
    if (streq(attrName, "firstgid")) {
      tileset->first_gid = atoi((const char *)attrVal);
    } else if (streq(attrName, "tilewidth")) {
      tileset->tile_size.w = atoi((const char *)attrVal);
    } else if (streq(attrName, "tileheight")) {
      tileset->tile_size.h = atoi((const char *)attrVal);
    } else if (streq(attrName, "name")) {
      tileset->name = calloc(1, strlen((const char *)attrVal) + 1);
      strcpy(tileset->name, (const char *)attrVal);
    }
  }
  while (xmlTextReaderRead(reader)) {
    xmlChar *childName = xmlTextReaderName(reader);
    if (xmlTextReaderNodeType(reader) == XML_ELEMENT_DECL &&
        streq(childName, "tileset")) {
      break;
    } else if (streq(childName, "image")) {
      while (xmlTextReaderMoveToNextAttribute(reader)) {
        xmlChar *attrName = xmlTextReaderName(reader);
        xmlChar *attrVal = xmlTextReaderValue(reader);
        
        if (streq(attrName, "source")) {
          // Check for existence of image
          bstring imgpath = bfromcstr("media/tilesets/");
          bstring src = bfromcstr((const char *)attrVal);
          bconcat(imgpath, src);
          char *cpath = bstr2cstr(imgpath, '\0');
          bdestroy(imgpath);
          bdestroy(src);
          
          FILE *fileexists = load_resource(cpath);
          if (fileexists == NULL) {
            free(cpath);
            Tileset_destroy(tileset);
            Engine_log("Cannot open map. Missing tileset <%s>", attrVal);
            return TILEMAP_PARSE_MISSING_IMAGE;
          }
          fclose(fileexists);
          
          tileset->texture = Graphics_texture_from_image(engine->graphics,
                                                         cpath);
          tileset->img_src = cpath;
        }
      }
    }
  }
  
  *out_tileset = tileset;
  
  return status;
error:
  if (tileset) Tileset_destroy(tileset);
  if (status == TILEMAP_PARSE_OK) status = TILEMAP_PARSE_UNKNOWN_ERR;
  return status;
}