Handler<JoystickManager> World::joystickManager() const { if(Handler<Universe> univ = this->universe_.lock()){ return univ->joystickManager(); } CINAMO_EXCEPTION(Exception, "[BUG] Universe is already dead."); }
XArchiver& XArchiver::binary(char* const bin, std::size_t const& len) { if(decode_now_){ if(!bin){ CINAMO_EXCEPTION(Exception, "[BUG] Oops. Binary Area is not allocated."); } XBinary const& b(array_->get<XBinary>(this->array_index_++)); if(len != b.size()){ CINAMO_EXCEPTION(Exception, "[BUG] Binary size does not match. requested: %d archived: %d", len, b.size()); } std::copy(b.begin(), b.end(), bin); }else{ array_->append( XValue(bin, len) ); } return *this; }
void MarginCombo::addChild(std::size_t const& idx, Handler<Element> const& h, Context const& ctx) { if(getChildCount() > 0){ CINAMO_EXCEPTION(Exception, "[BUG] Margin Combo can hold only one element."); } this->Super::addChild(idx,h,ctx); }
Handler<Quartet> World::quartet() const { if(Handler<Universe> univ = this->universe_.lock()){ return univ->quartet(); } CINAMO_EXCEPTION(Exception, "[BUG] Universe is already dead."); }
Handler<World> ElementObject::world() const { Handler<World> w( world_.lock() ); if(!w){ CINAMO_EXCEPTION(Exception, "[BUG] Oops. World is already dead."); } return w; }
void Canvas::translate(geom::Point const& pt) { glTranslatef(pt.x(),pt.y(),0.0f); #ifdef DEBUG const GLenum err = glGetError(); if(err != GL_NO_ERROR){ CINAMO_EXCEPTION(Exception, "[BUG] Failed to exec glTranslatef: 0x%08x", err); } #endif }
void Canvas::rotate(const float angle) { glRotatef(angle, 0, 0, 1.0f); #ifdef DEBUG const GLenum err = glGetError(); if(err != GL_NO_ERROR){ CINAMO_EXCEPTION(Exception, "[BUG] Failed to exec glRotetef: 0x%08x", err); } #endif }
void Canvas::scale(geom::ScaleVector const& scale) { glScalef(scale.x(),scale.y(),1.0f); #ifdef DEBUG const GLenum err = glGetError(); if(err != GL_NO_ERROR){ CINAMO_EXCEPTION(Exception, "[BUG] Failed to exec glScalef: 0x%08x", err); } #endif }
Canvas::AffineScope::~AffineScope() { canvas_.flushGL(); glPopMatrix(); #ifdef DEBUG const GLenum err = glGetError(); if(err != GL_NO_ERROR){ CINAMO_EXCEPTION(Exception, "[BUG] Failed to pop matrix: 0x%08x", err); } #endif }
void Canvas::scissor(geom::Area const& area) { flushGL(); glScissor(area.x(), this->height_-area.height()-area.y(),area.width(), area.height()); #ifdef DEBUG const GLenum err = glGetError(); if(err != GL_NO_ERROR){ CINAMO_EXCEPTION(Exception, "[BUG] Failed to exec glScissor: 0x%08x", err); } #endif }
void Canvas::enableVertexArray() { if(!vertexArrayEnabled_) { glEnableClientState(GL_VERTEX_ARRAY); #ifdef DEBUG const GLenum err = glGetError(); if(err != GL_NO_ERROR){ CINAMO_EXCEPTION(Exception, "[BUG] Failed to enable vertex array: 0x%08x", err); } #endif vertexArrayEnabled_ = true; } }
void Canvas::bindTexture(GLuint texId) { if(nowTexId_ != texId){ flushGL(); glBindTexture(GL_TEXTURE_2D, this->nowTexId_ = texId); #ifdef DEBUG const GLenum err = glGetError(); if(err != GL_NO_ERROR){ CINAMO_EXCEPTION(Exception, "[BUG] Failed to flush texture: 0x%08x", err); } #endif } }
Font* FontManager::seachDefaultFont() { FT_Face face; std::vector<std::string> files(file::enumFiles(this->fontdir_)); for(std::string const& fname : files){ if(FT_New_Face(this->freetype_->raw(), fname.c_str(), 0, &face) == 0){ return new Font(this, this->freetype_, face); }else{ this->log().e(TAG, "Failed to open font: %s", fname.c_str()); } } CINAMO_EXCEPTION(Exception, "[BUG] Failed open default font: %s", this->fontdir_.c_str()); }
void Canvas::ortho(const float left, const float right, const float bottom, const float top, const float near_val, const float far_val) { #if IS_GLES glOrthof(left, right, bottom, top, near_val, far_val); #else glOrtho(left, right, bottom, top, near_val, far_val); #endif const GLenum err = glGetError(); #ifdef DEBUG if(err != GL_NO_ERROR){ CINAMO_EXCEPTION(Exception, "[BUG] Failed to exec glOrtho: 0x%08x", err); } #endif }
void Canvas::enableTexture() { if(!textureEnabled_){ glEnable(GL_TEXTURE_2D); glEnableClientState(GL_TEXTURE_COORD_ARRAY); #ifdef DEBUG const GLenum err = glGetError(); if(err != GL_NO_ERROR){ CINAMO_EXCEPTION(Exception, "[BUG] Failed to enable texture: 0x%08x", err); } #endif textureEnabled_=true; } }
void Canvas::setColor(Color const& color) { if( color != nowColor_ ){ flushGL(); this->nowColor_ = color; glColor4f(nowColor_.red(), nowColor_.green(), nowColor_.blue(), nowColor_.alpha()); #ifdef DEBUG const GLenum err = glGetError(); if(err != GL_NO_ERROR){ CINAMO_EXCEPTION(Exception, "[BUG] Failed to flush color: 0x%08x", err); } #endif } }
FreeType::FreeType() :library_(nullptr) ,cache_(nullptr) ,cmap_(nullptr) ,image_(nullptr) { if(FT_Init_FreeType(&this->library_) != 0){ CINAMO_EXCEPTION(Exception, "[BUG] Failed to init Freetype."); } if(FTC_Manager_New(this->library_, 16, 0, 1024*1024*10, face_requester, nullptr, &this->cache_ )){ FT_Done_FreeType(this->library_); CINAMO_EXCEPTION(Exception, "[BUG] Failed to init cache manager."); } if(FTC_CMapCache_New(this->cache_, &this->cmap_)) { FTC_Manager_Done(this->cache_); FT_Done_FreeType(this->library_); CINAMO_EXCEPTION(Exception, "[BUG] Failed to init cmap cache."); } if(FTC_ImageCache_New(this->cache_, &this->image_)) { FTC_Manager_Done(this->cache_); FT_Done_FreeType(this->library_); CINAMO_EXCEPTION(Exception, "[BUG] Failed to init image cache."); } }
void Canvas::setLineWidth( float const& lineWidth ) { if( std::fabs(nowLineWidth_-lineWidth) >= 1.0f ) { flushGL(); this->nowLineWidth_ = lineWidth; glLineWidth(nowLineWidth_); #ifdef DEBUG const GLenum err = glGetError(); if(err != GL_NO_ERROR){ CINAMO_EXCEPTION(Exception, "[BUG] Failed to flush line width: 0x%08x", err); } #endif } }
void Canvas::resize2d(geom::Box const& box) { this->width_ = box.width(); this->height_ = box.height(); glMatrixMode(GL_PROJECTION); glLoadIdentity(); this->ortho(0, box.width(), box.height(), 0, -100, 100); glMatrixMode(GL_MODELVIEW); glLoadIdentity(); glViewport(0, 0, box.width(), box.height()); glScissor(0,0,box.width(),box.height()); #ifdef DEBUG const GLenum err = glGetError(); if(err != GL_NO_ERROR){ CINAMO_EXCEPTION(Exception, "[BUG] Failed to exec resize2d: 0x%08x", err); } #endif }
Heading::Heading(int const level) :level_(level) { //デフォルトマージンの設定 switch(this->level()){ case 1: this->margin(geom::Space(gl::TextDrawable::DefaultFontSize*1.5, gl::TextDrawable::DefaultFontSize, 0.0f,0.0f)); break; case 2: this->margin(geom::Space(gl::TextDrawable::DefaultFontSize*1.25, gl::TextDrawable::DefaultFontSize*0.75, 0.0f,0.0f)); break; case 3: this->margin(geom::Space(gl::TextDrawable::DefaultFontSize*1, gl::TextDrawable::DefaultFontSize*0.5, 0.0f,0.0f)); break; default: CINAMO_EXCEPTION(Exception, "[BUG] Unknwon heading level: %d", this->level()); } }
void BlockSession::extendBlock(BlockNode* blockNode) { if(this->inlineObjects_.size() > 0){ this->newInline(); } //ここまでで、インライン要素が一切挿入されていないことが保証される geom::Box const size = blockNode->areaInBlock().box() + blockNode->margin().totalSpace(); if(size.width() > this->calcBlockLimit()){ this->newBlockLine(); } if(this->dir_ == BlockNode::Direction::Unspecified){ this->dir_ = blockNode->direction(); } switch (this->dir_) { case BlockNode::Direction::None: this->newBlockLine(); blockNode->areaInBlock(geom::Area(0, this->consumedHeight_, limitSize_.width(), size.height())); this->consumedHeight_ += size.height(); break; case BlockNode::Direction::Right: if(blockNode->direction() != BlockNode::Direction::Right){ this->newBlockLine(); this->dir_ = blockNode->direction(); } blockNode->areaInBlock(geom::Area(this->blockPosX_, this->consumedHeight_, size.width(), size.height())); this->blockConsumedHeight_ = std::max(this->blockConsumedHeight_, size.height()); this->blockPosX_ += size.width(); break; case BlockNode::Direction::Left: if(blockNode->direction() != BlockNode::Direction::Left){ this->newBlockLine(); this->dir_ = blockNode->direction(); } blockNode->areaInBlock(geom::Area(limitSize_.width() - this->blockPosX_ - size.width(), this->consumedHeight_, size.width(), size.height())); this->blockConsumedHeight_ = std::max(this->blockConsumedHeight_, size.height()); this->blockPosX_ += size.width(); break; default: CINAMO_EXCEPTION(Exception, "[BUG] Unknown direction: %d", this->dir_); } }
void PredefinedSymRenderer::registerSymbol( unsigned int symbol, std::string const& str ) { if( unlikely(!this->renderBuffer_.empty()) ) { CINAMO_EXCEPTION(Exception, "[BUG] Oops. PredefinedSymRenderer already compiled."); } auto it = this->entryTable_.find(symbol); if(it != this->entryTable_.end()){ this->log().w(TAG, "Symbol: %d is already defined, and replaced with: %s", symbol, str.c_str()); this->entryTable_.erase(it); } Entry ent; ent.str = str; ent.glyphs = font_->lookupGlyph(str, size_, ascent_, descent_, height_); int totalX = 0; for(Handler<BitmapGlyph> const& g : ent.glyphs){ totalX += g->get()->root.advance.x; } float const width = FLOAT_FROM_16_16(totalX); ent.areaInSprite.width(width); ent.areaInSprite.height(height_); this->maxWidth_ = geom::max(width, maxWidth_); this->entryTable_.insert( std::pair<unsigned int, Entry >( symbol, ent ) ); }
Handler<Sprite> ImageManager::loadPNG(std::string const& filename) { FILE* fp = fopen(filename.c_str(), "rb"); if(!fp){ CINAMO_EXCEPTION(Exception, "[BUG] oops. failed to open: %s",filename.c_str()); } unsigned char header[8]; fread(header, 1, 8, fp); if(png_sig_cmp(header,0,8)){ CINAMO_EXCEPTION(Exception, "[BUG] oops. File \"%s\" is not a PNG file.",filename.c_str()); } png_structp png = png_create_read_struct (PNG_LIBPNG_VER_STRING, nullptr, nullptr, nullptr); if(!png){ fclose(fp); CINAMO_EXCEPTION(Exception, "[BUG] oops. failed to create PNG read struct!!"); } png_infop info = png_create_info_struct(png); if(!info){ fclose(fp); png_destroy_read_struct(&png, (png_infopp)nullptr, (png_infopp)nullptr); CINAMO_EXCEPTION(Exception, "[BUG] oops. failed to create PNG info struct!!"); } png_infop end_info = png_create_info_struct(png); if (!end_info) { fclose(fp); png_destroy_read_struct(&png, &info, (png_infopp)NULL); CINAMO_EXCEPTION(Exception, "[BUG] oops. failed to create PNG info struct!!"); } if (setjmp(png_jmpbuf(png))) { //例外処理 fclose(fp); CINAMO_EXCEPTION(Exception, "[BUG] oops. failed read PNG file: \"%s\"!", filename.c_str()); } //ファイルポインタの設定 png_init_io(png, fp); png_set_sig_bytes(png, 8); if( ! (png_get_color_type(png, info) & PNG_COLOR_MASK_ALPHA)){ png_set_filler( png, 0xff, 1 ); } //情報を読み込んでサイズだけ調べる png_read_info(png, info); const int width = png_get_image_width(png, info); const int height = png_get_image_height(png, info); Handler<Sprite> spr; if(Handler<SpriteManager> mgr = this->spriteManager_.lock()){ spr = mgr->queryRawSprite(ImageFormat::RGBA8, width, height); { Sprite::Session session(spr); unsigned char* data = session.data(); const int stride = session.stride(); unsigned char* ptr[height]; for(int i=0;i<height;++i){ ptr[i] = &data[i*stride]; } png_read_image(png, ptr); } }else{ this->log().e(TAG, "Oops. SpriteManager already dead!!"); } png_read_end(png, end_info); png_destroy_read_struct(&png, &info, &end_info); fclose(fp); return spr; }
void PredefinedSymRenderer::compile() { if( unlikely(!this->renderBuffer_.empty()) ) { CINAMO_EXCEPTION(Exception, "[BUG] Oops. PredefinedSymRenderer already compiled."); } int nowSprite = 0; int const maxSize = drawableManager_->maxTextureSize(); this->renderBuffer_.resize(1); Image* image = &renderBuffer_.back(); image->sprite = this->drawableManager_->queryRawSprite(ImageFormat::RGBA8, maxSize, maxSize); int nowX = 0; int nowY = 0; for(std::pair<const Symbol, Entry>& p : entryTable_) { Entry& ent = p.second; if(ent.areaInSprite.width() + FLOAT_FROM_16_16(nowX) > image->sprite->width()) { nowX = 0; nowY += height_; } if( nowY + height_ > image->sprite->height()){ nowX = 0; nowY = 0; renderBuffer_.resize((++nowSprite)+1); image = &renderBuffer_.back(); image->sprite = this->drawableManager_->queryRawSprite(ImageFormat::RGBA8, maxSize, maxSize); } ent.spriteNo = nowSprite; ent.areaInSprite.x(FLOAT_FROM_16_16(nowX)); ent.areaInSprite.y(nowY); Sprite::Session ss(image->sprite); int const spriteAlign = ss.align(); int const spriteStride = ss.stride(); unsigned char* const spriteBuf = ss.data(); for( Handler<BitmapGlyph> const& glyph : ent.glyphs ) { FT_BitmapGlyph g = glyph->get(); int const startX = INT_FROM_16_16_FLOOR(nowX)+g->left; int const bmpPitch = g->bitmap.pitch; int const bmpWidth = g->bitmap.width; int const startY = ascent_-g->top; int const endY = g->bitmap.rows+startY; int bmpY = 0; for(int y=startY;y<endY;++y,++bmpY) { unsigned int* const sprBuf = reinterpret_cast<unsigned int*>(&spriteBuf[((nowY+y)*spriteStride)+(startX*spriteAlign)]); unsigned char* const bmpBuf = &g->bitmap.buffer[bmpPitch*bmpY]; for(int x=0;x<bmpWidth; ++x) { #if IS_BIG_ENDIAN sprBuf[x] = bmpBuf[x] | 0xffffff00; #else sprBuf[x] = bmpBuf[x] << 24 | 0xffffff; #endif } } nowX += g->root.advance.x; } ent.glyphs.clear(); ent.glyphs.shrink_to_fit(); } }
void Canvas::flushGL() { switch( this->glOperation_ ) { case None: break; case Lines: { enableVertexArray(); disableTexture(); glVertexPointer(3, GL_FLOAT, 0, this->vertexs_.data()); glDrawArrays(GL_LINES, 0, this->vertexs_.size()/3); #ifdef DEBUG { const GLenum err = glGetError(); if(err != GL_NO_ERROR){ CINAMO_EXCEPTION(Exception, "[BUG] Failed to draw arrays: 0x%08x", err); } } #endif break; } case LineStrip: { enableVertexArray(); disableTexture(); glVertexPointer(3, GL_FLOAT, 0, this->vertexs_.data()); #ifdef DEBUG { const GLenum err = glGetError(); if(err != GL_NO_ERROR){ CINAMO_EXCEPTION(Exception, "[BUG] Failed to set vertex pointer: 0x%08x", err); } } #endif glDrawArrays(GL_LINE_STRIP, 0, this->vertexs_.size()/3); #ifdef DEBUG { const GLenum err = glGetError(); if(err != GL_NO_ERROR){ CINAMO_EXCEPTION(Exception, "[BUG] Failed to draw arrays: 0x%08x", err); } } #endif break; } case Texture: { enableVertexArray(); enableTexture(); glVertexPointer(3, GL_FLOAT, 0, this->vertexs_.data()); #ifdef DEBUG { const GLenum err = glGetError(); if(err != GL_NO_ERROR){ CINAMO_EXCEPTION(Exception, "[BUG] Failed to set vertex pointer: 0x%08x", err); } } #endif glTexCoordPointer(2, GL_FLOAT, 0, this->texCoords_.data()); #ifdef DEBUG { const GLenum err = glGetError(); if(err != GL_NO_ERROR){ CINAMO_EXCEPTION(Exception, "[BUG] Failed to set vertex pointer: 0x%08x", err); } } #endif glDrawArrays(GL_TRIANGLES, 0, this->vertexs_.size()/3); #ifdef DEBUG { const GLenum err = glGetError(); if(err != GL_NO_ERROR){ CINAMO_EXCEPTION(Exception, "[BUG] Failed to draw arrays: 0x%08x", err); } } #endif break; } case Rect: { enableVertexArray(); disableTexture(); glVertexPointer(3, GL_FLOAT, 0, this->vertexs_.data()); #ifdef DEBUG { const GLenum err = glGetError(); if(err != GL_NO_ERROR){ CINAMO_EXCEPTION(Exception, "[BUG] Failed to set vertex pointer: 0x%08x", err); } } #endif glDrawArrays(GL_TRIANGLES, 0, this->vertexs_.size()/3); #ifdef DEBUG { const GLenum err = glGetError(); if(err != GL_NO_ERROR){ CINAMO_EXCEPTION(Exception, "[BUG] Failed to draw arrays: 0x%08x", err); } } #endif break; } default: CINAMO_EXCEPTION(Exception, "Unknown Operation", this->glOperation_); break; } vertexs_.clear(); texCoords_.clear(); this->glOperation_ = None; }