SDL2 - 为什么SDL_CreateTextureFromSurface()需要渲染器*?

时间:2015-12-23 05:42:56

标签: c++ sdl sdl-2

这是SDL_CreateTextureFromSurface函数的语法:

SDL_Texture* SDL_CreateTextureFromSurface(SDL_Renderer* renderer, SDL_Surface*  surface)

但是,我很困惑为什么我们需要传递渲染器*?我认为只有在绘制纹理时才需要渲染器?

2 个答案:

答案 0 :(得分:6)

您需要public static Bitmap scaleImage(Context context, Uri photoUri) throws IOException { InputStream is = context.getContentResolver().openInputStream(photoUri); BitmapFactory.Options dbo = new BitmapFactory.Options(); dbo.inJustDecodeBounds = true; BitmapFactory.decodeStream(is, null, dbo); is.close(); int rotatedWidth, rotatedHeight; int orientation = getOrientation(context, photoUri); if (orientation == 90 || orientation == 270) { rotatedWidth = dbo.outHeight; rotatedHeight = dbo.outWidth; } else { rotatedWidth = dbo.outWidth; rotatedHeight = dbo.outHeight; } Bitmap srcBitmap; is = context.getContentResolver().openInputStream(photoUri); if (rotatedWidth > MAX_IMAGE_DIMENSION || rotatedHeight > MAX_IMAGE_DIMENSION) { float widthRatio = ((float) rotatedWidth) / ((float) MAX_IMAGE_DIMENSION); float heightRatio = ((float) rotatedHeight) / ((float) MAX_IMAGE_DIMENSION); float maxRatio = Math.max(widthRatio, heightRatio); // Create the bitmap from file BitmapFactory.Options options = new BitmapFactory.Options(); options.inSampleSize = (int) maxRatio; srcBitmap = BitmapFactory.decodeStream(is, null, options); } else { srcBitmap = BitmapFactory.decodeStream(is); } is.close(); /* * if the orientation is not 0 (or -1, which means we don't know), we * have to do a rotation. */ if (orientation > 0) { Matrix matrix = new Matrix(); matrix.postRotate(orientation); srcBitmap = Bitmap.createBitmap(srcBitmap, 0, 0, srcBitmap.getWidth(), srcBitmap.getHeight(), matrix, true); } String type = context.getContentResolver().getType(photoUri); ByteArrayOutputStream baos = new ByteArrayOutputStream(); if (type.equals("image/png")) { srcBitmap.compress(Bitmap.CompressFormat.PNG, 100, baos); } else if (type.equals("image/jpg") || type.equals("image/jpeg")) { srcBitmap.compress(Bitmap.CompressFormat.JPEG, 100, baos); } byte[] bMapArray = baos.toByteArray(); baos.close(); return BitmapFactory.decodeByteArray(bMapArray, 0, bMapArray.length); } 才能获得有关适用约束的信息:

  • 支持的最大尺寸
  • 像素格式

可能还有更多......

答案 1 :(得分:2)

除了答案之外......

在幕后,SDL_CreateTextureFromSurface调用SDL_CreateTexture,它本身也需要一个渲染器,以创建一个与传入曲面相同大小的新纹理。

然后在新创建的纹理上调用SDL_UpdateTexture函数,将像素数据从传入的表面加载(复制)到SDL_CreateTextureFromSurface。如果传入表面之间的格式与渲染器支持的格式不同,则会出现更多逻辑以确保正确的行为。

SDL_CreateTexture需要渲染器本身,因为它处理和存储纹理的GPU(大部分时间)和渲染器应该是GPU上的抽象。

表面永远不需要渲染器,因为它加载到RAM中并由CPU处理。

如果从SDL2源代码查看SDL_render.c,您可以找到有关这些调用如何工作的更多信息。

以下是SDL_CreateTextureFromSurface内的一些代码:

texture = SDL_CreateTexture(renderer, format, SDL_TEXTUREACCESS_STATIC,
                            surface->w, surface->h);
if (!texture) {
    return NULL;
}

if (format == surface->format->format) {
    if (SDL_MUSTLOCK(surface)) {
        SDL_LockSurface(surface);
        SDL_UpdateTexture(texture, NULL, surface->pixels, surface->pitch);
        SDL_UnlockSurface(surface);
    } else {
        SDL_UpdateTexture(texture, NULL, surface->pixels, surface->pitch);
    }
}