// add factory void AssetManager::registerType(int type, AssetFactory* factory) { SCOPE_LOCK; if (type < 0 || type >= Asset::MAX_TYPES) Errorf("out of bound"); if (m_datas[type] != nullptr) { Errorf("already registered"); } int poolsize = factory->getPoolSize(); m_datas[type] = TypeNew<Data>(poolsize); m_datas[type]->factory = factory; m_datas[type]->poolHint = factory->getPoolHint(); Asset* defaulted = factory->getDefaulted(); if (defaulted == nullptr) { defaulted = factory->create(); if (!defaulted->doInit("default", 0)) { factory->destroy(defaulted); defaulted = nullptr; } } m_datas[type]->defaulted = defaulted; if (defaulted != nullptr) { defaulted->m_isDefaulted = true; } else { // TODO Debugf("%s: can't found defaulted asset for type\n", __func__); } }
GameEntity::GameEntity(EntityClass* entityClass, EntityIdentifier entityIdentifier, const MapLocation& initialLocation) : mEntityClass(entityClass) , mEntityIdentifier(entityIdentifier) , mDirection(DEFAULT_DIRECTION) , mAnimationClip() , mLocationOnMap() , mPrevLocationOnMap() , mEntityPlayer() , _ListNodePlayer(this) , _ListNodeUpdate(this) , _ListNodeSelection(this) , mCurrentAction(eEntityAction::aNothing) , mPosition() , mMoveNextTile() , mIsSelected() { assert(mEntityClass); if (!mEntityClass) { Errorf("Error: Entity is initialized"); } assert(mEntityIdentifier != NULL_ENTITY_IDENTIFIER); if (mEntityIdentifier == NULL_ENTITY_IDENTIFIER) { Errorf("Error: Entity instance identifier is null"); } SetMapLocation(initialLocation); }
void AssetManager::checkType(const char* func, int type) { if (type < 0 || type >= Asset::MAX_TYPES) Errorf("%s: out of bound", func); if (m_datas[type] == nullptr) { Errorf("%s: not yet registered", func); } }
void WinInput::stopCapture() { if (!m_hwnd) { return; } SetWindowLong(m_hwnd, GWL_WNDPROC, (LONG)m_oldWndProc); RAWINPUTDEVICE Rid[4]; Rid[0].usUsagePage = 0x01; Rid[0].usUsage = 0x02; // HID mouse Rid[0].dwFlags = RIDEV_REMOVE; // ignore legacy mouse messages Rid[0].hwndTarget = NULL; Rid[1].usUsagePage = 0x01; Rid[1].usUsage = 0x04; // HID joystick Rid[1].dwFlags = RIDEV_REMOVE; Rid[1].hwndTarget = 0; Rid[2].usUsagePage = 0x01; Rid[2].usUsage = 0x05; // HID game pad Rid[2].dwFlags = RIDEV_REMOVE; Rid[2].hwndTarget = 0; Rid[3].usUsagePage = 0x01; Rid[3].usUsage = 0x06; // HID keyboard Rid[3].dwFlags = RIDEV_REMOVE; // ignore legacy keyboard messages Rid[3].hwndTarget = NULL; if (!RegisterRawInputDevices(Rid, 4, sizeof(Rid[0]))) { Errorf("can't register rawinput devices"); } }
// origin in {SEEK_CUR, SEEK_END, SEEK_SET} size_t abfseek(File *f, size_t offset, int origin) { MemFile *mem; switch(f->type) { case FileType_CRT: return _fseeki64(f->fp.crt,offset,origin); break; case FileType_Mem: mem = &f->fp.mem; if(origin == SEEK_SET) mem->i = offset; else { size_t i = mem->i; size_t n; if(origin == SEEK_END) i = mem->n; n = i + offset; if(i < 0) i = 0; if(i > mem->n) i = mem->n; mem->i = i; } default: Errorf("unknown type %i in " __FUNCTION__,f->type); } return 0; }
intstar4 __fortran SETSYSHANDLE( intstar4 *unit, intstar2 *handle ) { //======================================================================== ftnfile *fcb; struct stat stat_buff; fcb = Files; for(;;) { if( fcb == NULL ) return( -1 ); if( *unit == fcb->unitid ) { if( fstat( *handle, &stat_buff ) == -1 ) { return( -1 ); } if( fcb->fileptr != NULL ) { Closef( fcb->fileptr ); if( Errorf( NULL ) != IO_OK ) { return( -1 ); } } fcb->fileptr = _AllocFile( *handle, _FileAttrs( fcb ), 0 ); if( fcb->fileptr == NULL ) { return( -1 ); } else { _AllocBuffer( fcb ); return( 0 ); } } fcb = fcb->link; } }
void GLtexture::uploadSubTexture(const Rect& rect, const void* pixels, TexFormat fmt) { if (rect.isEmpty()) { return; } glBindTexture(GL_TEXTURE_2D, m_object); if (fmt == TexFormat::AUTO) { fmt = m_format; } GLenum format, datatype, internalformat; trTexFormat(fmt, format, datatype, internalformat); if (!format) { Errorf("OpenGL don't support texture format '%s'", m_format.getStringName()); } // if (m_format.isDXTC()) { // return; // } else { glTexSubImage2D(GL_TEXTURE_2D, 0, rect.x, rect.y, rect.width, rect.height, format, datatype, pixels); // } // glThreadState->CheckErrors(); }
GLtarget* GLframebuffer::allocTarget(Target::AllocHint hint, TexFormat texformat) { if (!glFramebufferManager->isFormatSupport(texformat)) { Errorf("GLframebuffer::allocTarget: can't support format %s", texformat.getStringName()); return nullptr; } GLtarget* result = nullptr; AX_FOREACH(GLtarget* target, m_targetpool) { if (target->getHint() != Target::Free) { continue; } if (target->getTextureGL()->getFormat() != texformat) { continue; } result = target; break; } if (!result) { result = new GLtarget(this, m_width, m_height, texformat); m_targetpool.push_back(result); } result->setHint(hint, glFramebufferManager->getFrame()); return result; }
void GLframebuffer::freeTarget(Target* target) { if (!target->isTexture()) { Errorf("GLframebuffer::freeTarget: target isn't a texture"); return; } GLtarget* gltarget = (GLtarget*)target; #if 0 if (gltarget->getHint() != Target::PermanentAlloc) { Errorf("GLframebuffer::freeTarget: only permanent target need to be free"); return; } #endif gltarget->setHint(Target::Free, glFramebufferManager->getFrame()); }
bool Scrtched( ftnfile *fcb ) { //================================ // Erase specified file. Scratchf( fcb->filename ); return( Errorf( NULL ) == IO_OK ); }
void D3D9target::freeRealTarget() { if (!m_realTarget) Errorf("free real target error: not even allocated"); d3d9TargetManager->freeTarget(m_realTarget); m_realTarget = 0; }
bool SDError( file_handle fp, char *buff ) { //============================================= int err; err = Errorf( fp ); if( err != IO_OK ) { strcpy( buff, ErrorMsg( fp ) ); } return( err != IO_OK ); }
Texture* D3D9target::getTexture() { if (!m_isPooled) return m_texture; if (!m_realTarget) { Errorf("need alloc a real target for pooled target"); return 0; } return m_realTarget->getTexture(); }
void GLtarget::attachDepth(GLtarget* depth) { if (!isColorFormat()) { Errorf("GLtarget::attachDepth: attach depth to not a color target"); return; } if (depth == m_depthTarget) { return; } m_depthTarget = depth; }
void WinInput::queWinInput(MSG* msg) { UINT dwSize; static int oldtime = 0; int curtime = OsUtil::milliseconds(); int msgtime = curtime - oldtime; oldtime = curtime; Printf("%d\n", msgtime); GetRawInputData((HRAWINPUT)msg->lParam, RID_INPUT, NULL, &dwSize, sizeof(RAWINPUTHEADER)); LPBYTE lpb = new BYTE[dwSize]; if (lpb == NULL) { return; } if (GetRawInputData((HRAWINPUT)msg->lParam, RID_INPUT, lpb, &dwSize, sizeof(RAWINPUTHEADER)) != dwSize) OutputDebugString (TEXT("GetRawInputData doesn't return correct size !\n")); RAWINPUT* raw = (RAWINPUT*)lpb; if (raw->header.dwType == RIM_TYPEKEYBOARD) { RAWKEYBOARD& rk = raw->data.keyboard; Event e; TypeZero(&e); e.key = m_mapVkey[rk.VKey]; if (!e.key) { goto quit; } if (rk.Message == WM_KEYDOWN) { e.type = Event::KeyDown; } else if (rk.Message == WM_KEYUP) { e.type = Event::KeyUp; } else { Errorf("unknown key message"); } g_inputSystem->queEvent(e); } else if (raw->header.dwType == RIM_TYPEMOUSE) { RAWMOUSE& rm = raw->data.mouse; Printf("%x %x %d %d\n", rm.usFlags, rm.usButtonFlags, rm.lLastX, rm.lLastY); } quit: delete[] lpb; }
bool Errf( ftnfile *fcb ) { //============================ // Determine if an i/o error exists. int err; err = Errorf( fcb->fileptr ); if( err == IO_EOF ) { SetEOF(); err = IO_OK; } return( err != IO_OK ); }
void TextureAtlas::getChunkInfo(int id, Texture*& tex, Vector4& tc) { if (m_atlasHash.find(id) == m_atlasHash.end()) Errorf("TextureAtlas::getChunkInfo: Chunk %d not resident", id); int tex_num; Rect rect; getChunkInfoByIndex(m_atlasHash[id], tex_num, rect); tex = m_textures[tex_num]; float tc_scale = 1.f / m_textureSize; tc[0] =(rect.x) * tc_scale; tc[1] =(rect.y) * tc_scale; tc[2] =(rect.x + rect.width) * tc_scale; tc[3] =(rect.y + rect.height) * tc_scale; }
void AssetManager::addAsset(int type, const String& key, Asset* asset) { SCOPE_LOCK; checkType(__func__, type); asset->m_key = key; Data* d = m_datas[type]; AssetDict::iterator it =d->assetDict.find(asset->m_key); if ( it != d->assetDict.end()) { Errorf("%s: duplicated asset name", __func__); } d->assetDict[asset->m_key] = asset; asset->m_frameId = m_frameId; }
IDirect3DSurface9* D3D9target::getSurface() { if (!m_isPooled) { if (!m_surface) { HRESULT hr; V(m_texture->getObject()->GetSurfaceLevel(0, &m_surface)); } return m_surface; } if (!m_realTarget) { Errorf("can't bind a pooled target when it not alloc a real target"); return 0; } return m_realTarget->getSurface(); }
int abungetc(int c, File *f) { MemFile *mem; switch(f->type) { case FileType_CRT: return ungetc(c,f->fp.crt); break; case FileType_Mem: mem = &f->fp.mem; if(mem->i <= 0 || c == EOF) return EOF; return mem->p[--mem->i] = (char)c; default: Errorf("unknown type %i in " __FUNCTION__,f->type); } return 0; }
void D3D9targetmanager::freeTarget(Target* target) { D3D9_SCOPELOCK; if (target->isWindow()) { Errorf("can't free window target"); } D3D9target* dxtarget = (D3D9target*)target; dxtarget->setHint(Target::Free, m_curFrame); // if is pooled, free it's real target also if (!dxtarget->isPooled()) return; dxtarget = dxtarget->m_realTarget; if (!dxtarget) return; freeTarget(dxtarget); }
int abgetc(File *f) { MemFile *mem; if(!f) return EOF; switch(f->type) { case FileType_CRT: return fgetc(f->fp.crt); break; case FileType_Mem: mem = &f->fp.mem; if(mem->i < mem->n) return mem->p[mem->i++]; return EOF; default: Errorf("unknown type %i in " __FUNCTION__,f->type); } return EOF; }
D3D9target* D3D9targetmanager::allocTargetDX(Target::AllocHint hint, int width, int height, TexFormat texformat) { D3D9_SCOPELOCK; if (!isFormatSupport(texformat)) { Errorf("can't support format %s", texformat.getStringName()); return nullptr; } D3D9targetseq& targets = m_targetPool[width][height]; D3D9target* result = nullptr; AX_FOREACH(D3D9target* target, targets) { if (target->getHint() != Target::Free && target->getHint() != Target::TemporalAlloc) { continue; } if (target->getFormat() != texformat) { continue; } if (hint == Target::PooledAlloc) { if (!target->isPooled()) continue; } else { if (target->isPooled()) continue; } result = target; break; } if (!result) { result = new D3D9target(width, height, texformat, hint == Target::PooledAlloc); targets.push_back(result); } result->setHint(hint, m_curFrame); return result; }
// if texture pool is full, return false bool TextureAtlas::updateChunk(int id, byte_t* data, TexFormat format) { int index; int i; if (m_atlasHash.find(id) != m_atlasHash.end()) index = m_atlasHash[id]; else { int oldest = m_frameNum; for (i=0; i<m_totalAtlas; i++) { if (m_atlasFrame[i] < oldest) { oldest = m_atlasFrame[i]; index = i; } } // texture pool is full, fire error if (oldest == m_frameNum) { Errorf("TextureAtlas::updateChunk: no enough room to store new chunk"); return false; } m_atlasHash[id] = index; // check if overwrite used chunk, delete old chunkid hash int userId = m_atlasId[index]; if (userId != INVALID_Chunk) { m_atlasHash.erase(userId); } } // upload texture int tex; Rect rect; getChunkInfoByIndex(index, tex, rect); m_textures[tex]->uploadSubTexture(rect, data, format); // register this id m_atlasId[index] = id; m_atlasFrame[index] = m_frameNum; return true; }
void MaterialDef::moveDownLayerDef(LayerDef* l) { if (m_numLayers == 0) return; int i; for (i = 0; i < m_numLayers; i++) { if (m_layerDefs[i] == l) break; } if (i == m_numLayers) { // not found, fire error Errorf("%s: cann't found LayerDef", __func__); } // is last layer, can't move if (i == m_numLayers - 1) return; m_layerDefs[i] = m_layerDefs[i+1]; m_layerDefs[i+1] = l; }
void GLframebuffer::checkStatus(GLenum target) { GLenum status = glCheckFramebufferStatusEXT(target); char* error_string; if (status == GL_NO_ERROR || status == GL_FRAMEBUFFER_COMPLETE_EXT) return; switch (status) { case GL_FRAMEBUFFER_COMPLETE_EXT: break; case GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT_EXT: error_string = "Framebuffer incomplete, incomplete attachment"; break; case GL_FRAMEBUFFER_INCOMPLETE_MISSING_ATTACHMENT_EXT: error_string = "Framebuffer incomplete, missing attachment"; break; case GL_FRAMEBUFFER_INCOMPLETE_DIMENSIONS_EXT: error_string = "Framebuffer incomplete, attached images must have same dimensions"; break; case GL_FRAMEBUFFER_INCOMPLETE_FORMATS_EXT: error_string = "Framebuffer incomplete, attached images must have same format"; break; case GL_FRAMEBUFFER_INCOMPLETE_DRAW_BUFFER_EXT: error_string = "Framebuffer incomplete, missing draw buffer"; break; case GL_FRAMEBUFFER_INCOMPLETE_READ_BUFFER_EXT: error_string = "Framebuffer incomplete, missing read buffer"; break; case GL_FRAMEBUFFER_UNSUPPORTED_EXT: error_string = "Unsupported framebuffer format"; break; default: error_string = "Framebuffer incomplete, unknow error"; break; } Errorf("GLframebuffer::checkStatus: %s", error_string); }
void MaterialDef::deleteLayerDef(LayerDef* l) { if (m_numLayers == 0) return; int i; for (i = 0; i < m_numLayers; i++) { if (m_layerDefs[i] == l) break; } if (i == m_numLayers) { // not found, fire error Errorf("%s: cann't found LayerDef", __func__); } delete l; // move layers after this forward for (; i < m_numLayers - 1; i++) { m_layerDefs[i] = m_layerDefs[i+1]; } m_numLayers--; }
bool D3D9texture::loadFile2D() { D3D9_SCOPELOCK; HRESULT hr; const byte_t* data; int flags = 0; if (!(m_initFlags.isSet(IF_NoMipmap))) flags |= Image::Mipmap; flags |= Image::ExpandAlpha; std::auto_ptr<Image> imagefile(new Image); if (!imagefile->loadFile(m_name, flags)) { // Debugf("D3D9texture::loadFile2D: can't find image file for %s\n", m_name.c_str()); return false; } m_width = imagefile->getWidth(); m_height = imagefile->getHeight(); // mDesc.format = imagefile->getFormat(); if (!Math::isPowerOfTwo(m_width) || !Math::isPowerOfTwo(m_height)) { // if (!(mDesc.flags & TexFlag_allowNPOT)) Errorf("GLtexture::loadFile2D: texture %s size isn't power of two", m_name.c_str()); // else // Debugf("GLtexture::loadFile2D: texture %s size isn't power of two\n", mDesc.name.c_str()); } m_format = imagefile->getFormat(); D3DFORMAT d3dformat; trTexFormat(imagefile->getFormat(), d3dformat); DWORD d3dusage = 0; int mipdown = image_mip->getInteger(); if (m_initFlags.isSet(IF_NoMipmap) || imagefile->getNumMipmapLevels() <= 1) { m_isMipmaped = false; mipdown = 0; } else { m_isMipmaped = true; mipdown = Math::clamp(mipdown, 0, imagefile->getNumMipmapLevels()-1); m_width >>= mipdown; m_height >>= mipdown; if (m_width < 1) m_width = 1; if (m_height < 1) m_height = 1; } // m_initFlags = 0; if (m_initFlags.isSet(Texture::IF_RenderTarget)) { Errorf("Can't load render target from a file"); } if (m_initFlags.isSet(Texture::IF_AutoGenMipmap)) { d3dusage |= D3DUSAGE_AUTOGENMIPMAP; m_isMipmaped = true; m_hardwareGenMipmap = checkIfSupportHardwareMipmapGeneration(d3dformat, d3dusage); if (!m_hardwareGenMipmap) { d3dusage &= ~D3DUSAGE_AUTOGENMIPMAP; } } if (m_object == 0) { V(d3d9Device->CreateTexture(m_width, m_height, !m_isMipmaped, d3dusage, d3dformat, D3DPOOL_MANAGED, &m_object, 0)); } int width, height; width = m_width; height = m_height; for (DWORD i = 0; i < m_object->GetLevelCount(); i++) { if (i + mipdown >= (DWORD)imagefile->getNumMipmapLevels()) { // if no image for this level, break break; } uint_t datasize = imagefile->getFormat().calculateDataSize(width, height); m_videoMemoryUsed += datasize; data = imagefile->getData(i + mipdown); #if 0 LPDIRECT3DSURFACE9 surface; V(m_object->GetSurfaceLevel(i, &surface)); D3DXLoadSurfaceFromMemory(surface, 0, 0, data, d3dformat, pitch, 0, rect, D3DX_FILTER_NONE, 0); #else D3DLOCKED_RECT lockedRect; V(m_object->LockRect(i, &lockedRect, 0, 0)); int mypitch = m_format.calculateDataSize(width, 1); if (mypitch != lockedRect.Pitch) { Errorf("mypitch != lockedRect.Pitch"); } memcpy(lockedRect.pBits, data, datasize); V(m_object->UnlockRect(i)); #endif width >>= 1; height >>= 1; if (width < 1) width = 1; if (height < 1) height = 1; if (m_initFlags.isSet(Texture::IF_AutoGenMipmap)) { // break; } } if (m_initFlags.isSet(Texture::IF_AutoGenMipmap)) { generateMipmap(); } if (m_isMipmaped) { setFilterMode(FM_Trilinear); } else { setFilterMode(FM_Linear); } setClampMode(CM_Repeat); setPrivateData(); g_statistic->addValue(stat_textureMemory, m_videoMemoryUsed); return true; }
void nrerror(char error_text[]) { Errorf(error_text); }
void TextureAtlas::initialize(const String& name, int chunk_width, int chunk_height, int side_length, TexFormat format) { if (m_initialized) { Errorf("TextureAtlas::initialize: has initialized"); return; } if (format.isDXTC()) { Errorf("TextureAtlas::initialize: texture pool doesn't support DXT compressed texture"); return; } m_name = name; m_chunkWidth = chunk_width; m_chunkHeight = chunk_height; const IDriver::Info* info = g_renderDriver->getDriverInfo(); if (side_length > info->maxTextureSize) { m_textureSize = info->maxTextureSize; float f = (float)side_length / m_textureSize; m_numTextures = ceil(f * f); } else { m_textureSize = side_length; m_numTextures = 1; } if (m_numTextures > MAX_TEXTURES) { Errorf("TextureAtlas::initialize: mNumTexture > MAX_TEXTURES"); return; } int i; for (i=1; i<m_textureSize; i<<=1) ; m_textureSize = i; m_rowsPerTexture = m_textureSize / m_chunkHeight; m_colsPerTexture = m_textureSize / m_chunkWidth; m_atlasPerTexture = m_rowsPerTexture * m_colsPerTexture; m_totalAtlas = m_atlasPerTexture * m_numTextures; m_texFormat = format; m_atlasId = new int[m_totalAtlas]; memset(m_atlasId, INVALID_Chunk, sizeof(int) * m_totalAtlas); m_atlasFrame = new int[m_totalAtlas]; memset(m_atlasFrame, 0, sizeof(uint_t) * m_totalAtlas); int image_size = format.calculateDataSize(m_textureSize, m_textureSize); String tex_name; for (i=0; i<m_numTextures; i++) { // maybe some machine fast enough so spend time < 1ms, so we add i to // texture name, make sure name is unique StringUtil::sprintf(tex_name, "_texture_pool_%s_%s", m_name.c_str(), Uuid::generateUuid().c_str()); m_textures[i] << dynamic_cast<Texture*>(g_assetManager->createEmptyAsset(Asset::kTexture)); AX_ASSERT(m_textures[i]); m_textures[i]->initialize(format, m_textureSize, m_textureSize); g_assetManager->addAsset(Asset::kTexture, tex_name, m_textures[i]); } return; }