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; }
bool NPSafeArray::InvokeDefault(NPObject *npobj, const NPVariant *args, uint32_t argCount, NPVariant *result) { NPSafeArray *safe = static_cast<NPSafeArray*>(npobj); if (safe->arr_.m_psa != NULL) return false; if (argCount < 1) return false; if (!NPVARIANT_IS_OBJECT(*args)) { return false; } NPObject *obj = NPVARIANT_TO_OBJECT(*args); if (obj->_class != &NPSafeArray::npClass) { return false; } NPSafeArray *safe_original = static_cast<NPSafeArray*>(obj); if (safe_original->arr_.m_psa == NULL) { return false; } NPSafeArray *ret = CreateFromArray(safe->instance, safe_original->arr_); OBJECT_TO_NPVARIANT(ret, *result); return true; }