bool Texture::CreateFromSurface(SDL_Surface *s, bool forceRGBA) { bool freeSurface = false; SDL_PixelFormat *pixfmt = s->format; GLenum targetGLformat; SDL_PixelFormat *targetPixfmt; bool needConvert = !GetTargetFormat(pixfmt, &targetGLformat, &targetPixfmt, forceRGBA); if (needConvert) { s = SDL_ConvertSurface(s, targetPixfmt, SDL_SWSURFACE); freeSurface = true; } // store incoming 24-bit as GL_RGB to save on texture memory if (targetGLformat == GL_RGB && m_format.internalFormat == GL_RGBA) { m_format.internalFormat = GL_RGB; m_format.dataFormat = GL_RGB; } unsigned int width = s->w; unsigned int height = s->h; // extend to power-of-two if necessary int width2 = ceil_pow2(s->w); int height2 = ceil_pow2(s->h); if (s->w != width2 || s->h != height2) { SDL_Surface *s2 = SDL_CreateRGBSurface(SDL_SWSURFACE, width2, height2, targetPixfmt->BitsPerPixel, targetPixfmt->Rmask, targetPixfmt->Gmask, targetPixfmt->Bmask, targetPixfmt->Amask); SDL_SetAlpha(s, 0, 0); SDL_SetAlpha(s2, 0, 0); SDL_BlitSurface(s, 0, s2, 0); if (freeSurface) SDL_FreeSurface(s); s = s2; freeSurface = true; m_texWidth = float(width) / float(width2); m_texHeight = float(height) / float(height2); } else m_texWidth = m_texHeight = 1.0f; SDL_LockSurface(s); CreateFromArray(s->pixels, s->w, s->h); SDL_UnlockSurface(s); m_width = width; m_height = height; if (freeSurface) SDL_FreeSurface(s); return true; }
void TextureBuilder::PrepareSurface() { if (m_prepared) return; if (!m_surface && m_filename.length() > 0) LoadSurface(); TextureFormat targetTextureFormat; SDL_PixelFormat *targetPixelFormat; bool needConvert = !GetTargetFormat(m_surface->format, &targetTextureFormat, &targetPixelFormat, m_forceRGBA); if (needConvert) { SDL_Surface *s = SDL_ConvertSurface(m_surface, targetPixelFormat, SDL_SWSURFACE); SDL_FreeSurface(m_surface); m_surface = s; } unsigned int virtualWidth, actualWidth, virtualHeight, actualHeight; virtualWidth = actualWidth = m_surface->w; virtualHeight = actualHeight = m_surface->h; if (m_potExtend) { // extend to power-of-two if necessary actualWidth = ceil_pow2(m_surface->w); actualHeight = ceil_pow2(m_surface->h); if (actualWidth != virtualWidth || actualHeight != virtualHeight) { SDL_Surface *s = SDL_CreateRGBSurface(SDL_SWSURFACE, actualWidth, actualHeight, targetPixelFormat->BitsPerPixel, targetPixelFormat->Rmask, targetPixelFormat->Gmask, targetPixelFormat->Bmask, targetPixelFormat->Amask); SDL_SetAlpha(m_surface, 0, 0); SDL_SetAlpha(s, 0, 0); SDL_BlitSurface(m_surface, 0, s, 0); SDL_FreeSurface(m_surface); m_surface = s; } } else if (m_filename.length() > 0) { // power-of-to check unsigned long width = ceil_pow2(m_surface->w); unsigned long height = ceil_pow2(m_surface->h); if (width != virtualWidth || height != virtualHeight) fprintf(stderr, "WARNING: texture '%s' is not power-of-two and may not display correctly\n", m_filename.c_str()); } m_descriptor = TextureDescriptor( targetTextureFormat, vector2f(actualWidth,actualHeight), vector2f(float(virtualWidth)/float(actualWidth),float(virtualHeight)/float(actualHeight)), m_sampleMode, m_generateMipmaps); m_prepared = true; }
void FaceVideoLink::Draw() { float size[2]; GetSize(size); Uint32 now = SDL_GetTicks(); if (now - m_created < 1500) { glBegin(GL_QUADS); glColor3f(0,0,0); glVertex2f(0,0); glVertex2f(0,size[1]); glVertex2f(size[0],size[1]); glVertex2f(size[0],0); glEnd(); m_message->SetText(Lang::VID_CONNECTING); DrawMessage(); return; } glEnable(GL_TEXTURE_2D); glBindTexture(GL_TEXTURE_2D, m_tex); glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE); glBegin(GL_QUADS); float w = float(FACE_WIDTH) / ceil_pow2(FACE_WIDTH); float h = float(FACE_HEIGHT) / ceil_pow2(FACE_HEIGHT); glColor3f(0,0,0); glTexCoord2f(0,h); glVertex2f(0,size[1]); glTexCoord2f(w,h); glVertex2f(size[0],size[1]); glTexCoord2f(w,0); glVertex2f(size[0],0); glTexCoord2f(0,0); glVertex2f(0,0); glEnd(); glDisable(GL_TEXTURE_2D); glPushMatrix(); glTranslatef(0.f, size[1]- size[1] * 0.16f, 0.f); m_characterInfo->Draw(); glPopMatrix(); }
static size_t alloc_block_class(size_t *len) { if (*len <= alloc_block_min_len) { *len = alloc_block_min_len; return 0; } // ]8, 256] we go by increments of 16 bytes. if (*len <= alloc_block_mid_len) { size_t class = ceil_div(*len, alloc_block_mid_inc); *len = class * alloc_block_mid_inc; return class; } // ]256, 2048] we go by powers of 2. *len = ceil_pow2(*len); size_t bits = ctz(*len) - ctz(alloc_block_mid_len); return bits + (alloc_block_mid_len / alloc_block_mid_inc); } // ----------------------------------------------------------------------------- // tag // ----------------------------------------------------------------------------- static const size_t alloc_block_tag_bits = 16; static ilka_off_t alloc_block_tag(struct alloc_blocks *blocks, ilka_off_t off) { ilka_off_t tag = ilka_atomic_fetch_add(&blocks->tags, 1, morder_relaxed); return off | (tag << (64 - alloc_block_tag_bits));
void TextureBuilder::PrepareSurface() { if (m_prepared) return; if (!m_surface && !m_filename.empty()) { std::string filename = m_filename; std::transform(filename.begin(), filename.end(), filename.begin(), ::tolower); if (ends_with_ci(filename, ".dds")) { LoadDDS(); } else { LoadSurface(); } } TextureFormat targetTextureFormat; unsigned int virtualWidth, actualWidth, virtualHeight, actualHeight, numberOfMipMaps = 0, numberOfImages = 1; if( m_surface ) { SDL_PixelFormat *targetPixelFormat; bool needConvert = !GetTargetFormat(m_surface->format, &targetTextureFormat, &targetPixelFormat, m_forceRGBA); if (needConvert) { if(m_textureType == TEXTURE_2D) { SDL_Surface *s = SDL_ConvertSurface(m_surface.Get(), targetPixelFormat, SDL_SWSURFACE); m_surface = SDLSurfacePtr::WrapNew(s); } else if(m_textureType == TEXTURE_CUBE_MAP) { assert(m_cubemap.size() == 6); for(unsigned int i = 0; i < 6; ++i) { SDL_Surface *s = SDL_ConvertSurface(m_cubemap[i].Get(), targetPixelFormat, SDL_SWSURFACE); m_cubemap[i] = SDLSurfacePtr::WrapNew(s); } } else { // Unknown texture type assert(0); } } virtualWidth = actualWidth = m_surface->w; virtualHeight = actualHeight = m_surface->h; if (m_potExtend) { // extend to power-of-two if necessary actualWidth = ceil_pow2(m_surface->w); actualHeight = ceil_pow2(m_surface->h); if (actualWidth != virtualWidth || actualHeight != virtualHeight) { if(m_textureType == TEXTURE_2D) { SDL_Surface *s = SDL_CreateRGBSurface(SDL_SWSURFACE, actualWidth, actualHeight, targetPixelFormat->BitsPerPixel, targetPixelFormat->Rmask, targetPixelFormat->Gmask, targetPixelFormat->Bmask, targetPixelFormat->Amask); SDL_SetSurfaceBlendMode(m_surface.Get(), SDL_BLENDMODE_NONE); SDL_BlitSurface(m_surface.Get(), 0, s, 0); m_surface = SDLSurfacePtr::WrapNew(s); } else if(m_textureType == TEXTURE_CUBE_MAP) { assert(m_cubemap.size() == 6); for(unsigned int i = 0; i < 6; ++i) { SDL_Surface *s = SDL_CreateRGBSurface(SDL_SWSURFACE, actualWidth, actualHeight, targetPixelFormat->BitsPerPixel, targetPixelFormat->Rmask, targetPixelFormat->Gmask, targetPixelFormat->Bmask, targetPixelFormat->Amask); SDL_SetSurfaceBlendMode(m_cubemap[i].Get(), SDL_BLENDMODE_NONE); SDL_BlitSurface(m_cubemap[i].Get(), 0, s, 0); m_cubemap[i] = SDLSurfacePtr::WrapNew(s); } } else { assert(0); } } } else if (! m_filename.empty()) { // power-of-two check unsigned long width = ceil_pow2(m_surface->w); unsigned long height = ceil_pow2(m_surface->h); if (width != virtualWidth || height != virtualHeight) Output("WARNING: texture '%s' is not power-of-two and may not display correctly\n", m_filename.c_str()); } } else { switch(m_dds.GetTextureFormat()) { case PicoDDS::FORMAT_DXT1: targetTextureFormat = TEXTURE_DXT1; break; case PicoDDS::FORMAT_DXT5: targetTextureFormat = TEXTURE_DXT5; break; default: Output("ERROR: DDS texture with invalid format '%s' (only DXT1 and DXT5 are supported)\n", m_filename.c_str()); assert(false); return; } virtualWidth = actualWidth = m_dds.imgdata_.width; virtualHeight = actualHeight = m_dds.imgdata_.height; numberOfMipMaps = m_dds.imgdata_.numMipMaps; numberOfImages = m_dds.imgdata_.numImages; if(m_textureType == TEXTURE_CUBE_MAP) { // Cube map must be fully defined (6 images) to be used correctly assert(numberOfImages == 6); } } m_descriptor = TextureDescriptor( targetTextureFormat, vector2f(actualWidth,actualHeight), vector2f(float(virtualWidth)/float(actualWidth),float(virtualHeight)/float(actualHeight)), m_sampleMode, m_generateMipmaps, m_compressTextures, numberOfMipMaps, m_textureType); m_prepared = true; }
FaceVideoLink::FaceVideoLink(float w, float h, Uint32 flags, Uint32 seed, const std::string &name, const std::string &title) : VideoLink(w, h) { m_created = SDL_GetTicks(); m_message = new Gui::ToolTip(Lang::VID_LINK_ESTABLISHED); if (!seed) seed = time(NULL); MTRand rand(seed); m_flags = flags; m_seed = seed; int race = rand.Int32(0,2); int gender; switch (flags & GENDER_MASK) { case GENDER_MALE: gender = 0; break; case GENDER_FEMALE: gender = 1; break; case GENDER_RAND: default: gender = rand.Int32(0,1); break; } std::string charname = name; if (charname.empty()) charname = Pi::luaNameGen->FullName((gender != 0), rand); m_characterInfo = new CharacterInfoText(w * 0.8f, h * 0.15f, charname, title); int head = rand.Int32(0,MAX_HEAD); int eyes = rand.Int32(0,MAX_EYES); int nose = rand.Int32(0,MAX_NOSE); int mouth = rand.Int32(0,MAX_MOUTH); int hair = rand.Int32(0,MAX_HAIR); int clothes = rand.Int32(0,MAX_CLOTHES); int armour = rand.Int32(0,MAX_ARMOUR); int accessories = rand.Int32(0,MAX_ACCESSORIES); int background = rand.Int32(0,MAX_BACKGROUND); char filename[1024]; SDL_Surface *s = SDL_CreateRGBSurface(SDL_SWSURFACE, ceil_pow2(FACE_WIDTH), ceil_pow2(FACE_HEIGHT), 32, 0xff, 0xff00, 0xff0000, 0xff000000); snprintf(filename, sizeof(filename), PIONEER_DATA_DIR "/facegen/backgrounds/background_%d.png", background); //printf("%s\n", filename); _blit_image(s, filename, 0, 0); snprintf(filename, sizeof(filename), PIONEER_DATA_DIR "/facegen/race_%d/head/head_%d_%d.png", race, gender, head); //printf("%s\n", filename); _blit_image(s, filename, 0, 0); if (!(flags & ARMOUR)) { snprintf(filename, sizeof(filename), PIONEER_DATA_DIR "/facegen/clothes/cloth_%d_%d.png", gender, clothes); //printf("%s\n", filename); _blit_image(s, filename, 0, 135); } snprintf(filename, sizeof(filename), PIONEER_DATA_DIR "/facegen/race_%d/eyes/eyes_%d_%d.png", race, gender, eyes); //printf("%s\n", filename); _blit_image(s, filename, 0, 41); snprintf(filename, sizeof(filename), PIONEER_DATA_DIR "/facegen/race_%d/nose/nose_%d_%d.png", race, gender, nose); //printf("%s\n", filename); _blit_image(s, filename, 1, 89); snprintf(filename, sizeof(filename), PIONEER_DATA_DIR "/facegen/race_%d/mouth/mouth_%d_%d.png", race, gender, mouth); //printf("%s\n", filename); _blit_image(s, filename, 0, 155); if (!(flags & ARMOUR)) { snprintf(filename, sizeof(filename), PIONEER_DATA_DIR "/facegen/accessories/acc_%d.png", accessories); //printf("%s\n", filename); if (rand.Int32(0,1)>0) _blit_image(s, filename, 0, 0); snprintf(filename, sizeof(filename), PIONEER_DATA_DIR "/facegen/race_%d/hair/hair_%d_%d.png", race, gender, hair); //printf("%s\n", filename); _blit_image(s, filename, 0, 0); } else { snprintf(filename, sizeof(filename), PIONEER_DATA_DIR "/facegen/clothes/armour_%d.png", armour); _blit_image(s, filename, 0, 0); } glEnable(GL_TEXTURE_2D); glGenTextures(1, &m_tex); glBindTexture(GL_TEXTURE_2D, m_tex); glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, s->w, s->h, 0, GL_RGBA, GL_UNSIGNED_BYTE, s->pixels); glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); glDisable(GL_TEXTURE_2D); SDL_FreeSurface(s); }