void LuaApplication::loadFile(const char* filename, GStatus *status) { StackChecker checker(L, "loadFile", 0); void *pool = application_->createAutounrefPool(); lua_pushcfunction(L, ::callFile); if (luaL_loadfile(L, filename)) { if (exceptionsEnabled_ == true) { if (status) *status = GStatus(1, lua_tostring(L, -1)); } lua_pop(L, 2); application_->deleteAutounrefPool(pool); return; } if (lua_pcall_traceback(L, 1, 0, 0)) { if (exceptionsEnabled_ == true) { if (status) *status = GStatus(1, lua_tostring(L, -1)); } lua_pop(L, 1); application_->deleteAutounrefPool(pool); return; } application_->deleteAutounrefPool(pool); }
int SpriteBinder::removeChildAt(lua_State* L) { StackChecker checker(L, "SpriteBinder::removeChildAt", 0); Binder binder(L); Sprite* sprite = static_cast<Sprite*>(binder.getInstance("Sprite", 1)); int index = luaL_checknumber(L, 2); if (index < 1 || index > sprite->childCount()) return luaL_error(L, GStatus(2006).errorString()); // Error #2006: The supplied index is out of bounds. Sprite* child = sprite->getChildAt(index - 1); lua_getfield(L, 1, "__children"); // push sprite.__children lua_pushlightuserdata(L, child); lua_rawget(L, -2); // push sprite.__children[child] lua_pushnil(L); lua_setfield(L, -2, "__parent"); // sprite.__children[child].__parent = nil lua_pop(L, 1); // pop sprite.__children[child] lua_pushlightuserdata(L, child); lua_pushnil(L); lua_rawset(L, -3); // sprite.__children[sprite] = nil lua_pop(L, 1); // pop sprite.__children sprite->removeChildAt(index - 1); return 0; }
int SpriteBinder::swapChildrenAt(lua_State* L) { StackChecker checker(L, "SpriteBinder::swapChildrenAt",0); Binder binder(L); Sprite* sprite = static_cast<Sprite*>(binder.getInstance("Sprite", 1)); int index1 = luaL_checknumber(L, 2); if (index1 < 1 || index1 > sprite->childCount()) return luaL_error(L, GStatus(2006).errorString()); // Error #2006: The supplied index1 is out of bounds. int index2 = luaL_checknumber(L, 3); if (index2 < 1 || index2 > sprite->childCount()) return luaL_error(L, GStatus(2006).errorString()); // Error #2006: The supplied index2 is out of bounds. sprite->swapChildrenAt(index1-1,index2-1); return 0; }
void MovieClip::addFrame(int start, int end, Sprite* sprite, const std::vector<Parameter>& parameters, GStatus* status/* = NULL*/) { switch (type_) { case eFrame: if (start < 1 || end < 1) { if (status) *status = GStatus(2100); // Error #2100: Start and end frames must be greater than or equal to 1. return; } break; case eTime: if (start < 0 || end < 0) { if (status) *status = GStatus(2104); // Error #2100: Start and end times must be greater than or equal to 0. return; } break; } if (start > end) { if (status) *status = GStatus(2101); // Error #2101: End frame/time must be greater than or equal to start frame/time. return; } Frame frame; frame.start = start; frame.end = end; frame.parameters = parameters; #if 0 frame.sprite = new Sprite; frame.sprite->addChild(sprite); #else frame.sprite = sprite; frame.sprite->ref(); #endif frames_.push_back(frame); }
void TTFont::constructor(const char *filename, float size, bool smoothing) { face_ = NULL; G_FILE* fis = g_fopen(filename, "rb"); if (fis == NULL) { throw GiderosException(GStatus(6000, filename)); // Error #6000: %s: No such file or directory. return; } memset(&stream_, 0, sizeof(stream_)); g_fseek(fis, 0, SEEK_END); stream_.size = g_ftell(fis); g_fseek(fis, 0, SEEK_SET); stream_.descriptor.pointer = fis; stream_.read = read; stream_.close = close; FT_Open_Args args; memset(&args, 0, sizeof(args)); args.flags = FT_OPEN_STREAM; args.stream = &stream_; if (FT_Open_Face(FT_Library_Singleton::instance(), &args, 0, &face_)) throw GiderosException(GStatus(6012, filename)); // Error #6012: %s: Error while reading font file. float scalex = application_->getLogicalScaleX(); float scaley = application_->getLogicalScaleY(); const int RESOLUTION = 72; if (FT_Set_Char_Size(face_, 0L, (int)floor(size * 64 + 0.5f), (int)floor(RESOLUTION * scalex + 0.5f), (int)floor(RESOLUTION * scaley + 0.5f))) { FT_Done_Face(face_); face_ = NULL; throw GiderosException(GStatus(6017, filename)); // Error #6017: Invalid font size. } ascender_ = face_->size->metrics.ascender >> 6; height_ = face_->size->metrics.height >> 6; smoothing_ = smoothing; }
int Font::getTextureGlyphsFormat(const char *file) { G_FILE *fis = g_fopen(file, "rt"); if (!fis) throw GiderosException(GStatus(6000, file)); // Error #6000: %s: No such file or directory. bool old = (g_fgetc(fis) == '0'); g_fclose(fis); return old ? 0 : 1; }
void Font::readTextureGlyphsOld(const char* file) { G_FILE* fis = g_fopen(file, "rt"); if (!fis) throw GiderosException(GStatus(6000, file)); // Error #6000: %s: No such file or directory. fontInfo_.textureGlyphs.clear(); int doMipmaps_; g_fscanf(fis, "%d", &doMipmaps_); while (1) { TextureGlyph glyph; int chr; g_fscanf(fis, "%d", &chr); if (g_feof(fis)) break; glyph.chr = chr; g_fscanf(fis, "%d %d", &glyph.x, &glyph.y); g_fscanf(fis, "%d %d", &glyph.width, &glyph.height); g_fscanf(fis, "%d %d", &glyph.left, &glyph.top); g_fscanf(fis, "%d %d", &glyph.advancex, &glyph.advancey); fontInfo_.textureGlyphs[chr] = glyph; } g_fclose(fis); fontInfo_.kernings.clear(); fontInfo_.isSetTextColorAvailable = true; // mimic height & ascender int ascender = 0; int descender = 0; std::map<wchar32_t, TextureGlyph>::iterator iter, e = fontInfo_.textureGlyphs.end(); for (iter = fontInfo_.textureGlyphs.begin(); iter != e; ++iter) { ascender = std::max(ascender, iter->second.top); descender = std::max(descender, iter->second.height - iter->second.top); } fontInfo_.height = (ascender + descender) * 1.25; fontInfo_.ascender = ascender * 1.25; }
void TileMap::set(int x, int y, int tx, int ty, int flip, GStatus *status/* = NULL*/) { if (x < 0 || y < 0 || x >= width_ || y >= height_) { if (status) *status = GStatus(2006); // Error #2006: The supplied index is out of bounds. return; } // int id = (tx < 0 || ty < 0) ? -1 : (ty << 8 | tx); int index = x + y * width_; tileids_[index].x = tx; tileids_[index].y = ty; tileids_[index].flip = flip; }
void TileMap::get(int x, int y, int* tx, int* ty, int *flip, GStatus *status/* = NULL*/) const { if (x < 0 || y < 0 || x >= width_ || y >= height_) { if (status) *status = GStatus(2006); // Error #2006: The supplied index is out of bounds. return; } int index = x + y * width_; if (tx) *tx = tileids_[index].x; if (ty) *ty = tileids_[index].y; if (flip) *flip = tileids_[index].flip; }
void LuaApplication::tick(GStatus *status) { void *pool = application_->createAutounrefPool(); lua_pushlightuserdata(L, &key_tickFunction); lua_rawget(L, LUA_REGISTRYINDEX); if (lua_pcall_traceback(L, 0, 0, 0)) { if (exceptionsEnabled_ == true) { if (status) *status = GStatus(1, lua_tostring(L, -1)); } lua_pop(L, 1); } application_->deleteAutounrefPool(pool); }
void LuaApplication::broadcastEvent(Event* event, GStatus *status) { void *pool = application_->createAutounrefPool(); lua_pushcfunction(L, ::broadcastEvent); lua_pushlightuserdata(L, event); if (lua_pcall_traceback(L, 1, 0, 0)) { if (exceptionsEnabled_ == true) { if (status) *status = GStatus(1, lua_tostring(L, -1)); } lua_pop(L, 1); } application_->deleteAutounrefPool(pool); }
int SpriteBinder::getChildAt(lua_State* L) { StackChecker checker(L, "SpriteBinder::getChildAt", 1); Binder binder(L); Sprite* sprite = static_cast<Sprite*>(binder.getInstance("Sprite")); int index = luaL_checkinteger(L, 2); if (index < 1 || index > sprite->childCount()) return luaL_error(L, GStatus(2006).errorString()); // Error #2006: The supplied index is out of bounds. Sprite* child = sprite->getChildAt(index - 1); lua_getfield(L, 1, "__children"); // push sprite.__children lua_pushlightuserdata(L, child); lua_rawget(L, -2); // push sprite.__children[child] lua_remove(L, -2); // pop sprite.__children return 1; }
void TexturePack::readTextureList(const char* texturelistfile, std::vector<Rect>& textures_, std::map<std::string, int>& filenameMap_, int* pwidth, int* pheight) { G_FILE* fis = g_fopen(texturelistfile, "rt"); if (fis == NULL) { throw GiderosException(GStatus(6000, texturelistfile)); // Error #6000: %s: No such file or directory. return; } textures_.clear(); filenameMap_.clear(); int width = 0; int height = 0; char line[1024]; while (true) { line[0] = line[1023] = 0; if (g_fgets(line, 1024, fis) == NULL) break; char* c; if ((c = strrchr(line, 0xa))) *c = '\0'; if ((c = strrchr(line, 0xd))) *c = '\0'; if (line[0] == '\0') break; std::vector<std::string> result; pystring::split(line, result, ","); for (std::size_t i = 0; i < result.size(); ++i) { if (result[i].empty() == false) result[i] = pystring::strip(result[i]); } if (result.size() >= 9) { Rect rect; rect.x = atoi(result[1].c_str()); rect.y = atoi(result[2].c_str()); rect.width = atoi(result[3].c_str()); rect.height = atoi(result[4].c_str()); rect.dx1 = atoi(result[5].c_str()); rect.dy1 = atoi(result[6].c_str()); rect.dx2 = atoi(result[7].c_str()); rect.dy2 = atoi(result[8].c_str()); filenameMap_[result[0]] = textures_.size(); textures_.push_back(rect); width += rect.width + rect.dx1 + rect.dx2; height += rect.height + rect.dy1 + rect.dy2; } } g_fclose(fis); if (pwidth) *pwidth = width; if (pheight) *pheight = height; if (textures_.empty() == true) { throw GiderosException(GStatus(6008, texturelistfile)); // Error #6008: %s: File does not contain texture region information. return; } }
void Font::readTextureGlyphsNew(const char *file) { G_FILE *fis = g_fopen(file, "rt"); if (!fis) throw GiderosException(GStatus(6000, file)); // Error #6000: %s: No such file or directory. std::string line; while (readLine(fis, &line)) { if (startsWith(line, "common")) { if (!getArg(line, "lineHeight", &fontInfo_.height)) goto error; if (!getArg(line, "base", &fontInfo_.ascender)) goto error; fontInfo_.isSetTextColorAvailable = false; int alphaChnl, redChnl, greenChnl, blueChnl; if (getArg(line, "alphaChnl", &alphaChnl) && getArg(line, "redChnl", &redChnl) && getArg(line, "greenChnl", &greenChnl) && getArg(line, "blueChnl", &blueChnl)) { if (alphaChnl == 0 && redChnl == 4 && greenChnl == 4 && blueChnl == 4) { fontInfo_.isSetTextColorAvailable = true; } } } else if (startsWith(line, "char")) { TextureGlyph textureGlyph; if (!getArg(line, "id", &textureGlyph.chr)) goto error; if (!getArg(line, "x", &textureGlyph.x)) goto error; if (!getArg(line, "y", &textureGlyph.y)) goto error; if (!getArg(line, "width", &textureGlyph.width)) goto error; if (!getArg(line, "height", &textureGlyph.height)) goto error; if (!getArg(line, "xoffset", &textureGlyph.left)) goto error; if (!getArg(line, "yoffset", &textureGlyph.top)) goto error; if (!getArg(line, "xadvance", &textureGlyph.advancex)) goto error; textureGlyph.top = fontInfo_.ascender - textureGlyph.top; textureGlyph.advancex <<= 6; fontInfo_.textureGlyphs[textureGlyph.chr] = textureGlyph; } else if (startsWith(line, "kerning")) { wchar32_t first, second; int amount; if (!getArg(line, "first", &first)) goto error; if (!getArg(line, "second", &second)) goto error; if (!getArg(line, "amount", &amount)) goto error; amount <<= 6; fontInfo_.kernings[std::make_pair(first, second)] = amount; } } g_fclose(fis); return; error: g_fclose(fis); throw GiderosException(GStatus(6016, file)); // Error #6016: %s: Error while reading FNT file. }
GStatus& GStatus::operator=(const GStatus& status) { GStatus(status).swap(*this); return *this; }