-1

シンプルな画像ビューアを作ろうとしています。基本的に画像をサーフェスにロードし、そこからテクスチャを作成します。

最後に、移行ガイドに従って、通常どおりSDL_RenderClear()に実行します。SDL_RenderCopy()SDL_RenderPresent()

これは問題なく動作SDL_UpdateTexture()しますが、上記の 3 つの render 呼び出しの前に呼び出すと、めちゃくちゃな画像が表示されます。

めちゃくちゃな画像

私は次のように SDL_UpdateTexture() を呼び出しています:

SDL_UpdateTexture(texture, NULL, image->pixels, image->pitch)

image画像用にロードしたサーフェスはどこにあり、そこtextureから作成したテクスチャはどこにありますか。ピッチを変化させようとすると、イメージが異なってめちゃくちゃになります。また、2 番目のパラメーターに rect を使用してみましたが、rect のサイズが画像と同じ場合、結果は同じです。寸法が大きい場合 (ウィンドウと同じなど)、更新は行われませんが、エラーは発生しません。

完全なコードが利用可能です。

表面のピクセルを直接操作してから呼び出したいimage->pixelsのですが、改ざんせずSDL_UpdateTexture()に呼び出すだけで問題が発生します。SDL_UpdateTexture()

4

2 に答える 2

2

ピッチまたはパラメーターに何か問題があると思いますがSDL_Rect、別の SDL 関数が役立つ可能性があります。

SDL_Texture* SDL_CreateTextureFromSurface(SDL_Renderer* renderer,
                                      SDL_Surface*  surface)
于 2015-11-24T16:02:35.313 に答える
0

以下をお試しいただけますでしょうか。ピンク (r=255,g=0,b=255) のピクセルを透明に置き換える必要があります。必要に応じて、pixel32 操作を変更するだけです。

SDL_Surface* image = IMG_Load(filename);

SDL_Surface* imageFomatted = SDL_ConvertSurfaceFormat(image, 
                                                      SDL_PIXELFORMAT_RGBA8888, 
                                                      NULL);

texture = SDL_CreateTexture(renderer,
                            SDL_PIXELFORMAT_RGBA8888,
                            SDL_TEXTUREACCESS_STREAMING,
                            imageFomatted->w, imageFomatted->h);

void* pixels = NULL;
int pitch = 0;

SDL_LockTexture(texture, &imageFomatted->clip_rect, &pixels, &pitch);

memcpy(pixels, imageFomatted->pixels, (imageFomatted->pitch * imageFomatted->h));

int width   = imageFomatted->w;
int height  = imageFomatted->h;

Uint32* pixels32    = (Uint32*)pixels;
int     pixelCount  = (pitch / 4) * height;

Uint32 colorKey     = SDL_MapRGB(imageFomatted->format, 0xFF, 0x00, 0xFF);
Uint32 transparent  = SDL_MapRGBA(imageFomatted->format, 0xFF, 0x00, 0xFF, 0x00);

for (int i = 0; i < pixelCount; i++) {
  if (pixels32[i] == colorKey) {
    pixels32[i] = transparent;
  }
}

SDL_UnlockTexture(texture);

SDL_FreeSurface(imageFormatted);
SDL_FreeSurface(image);

pixels = NULL;
pitch = 0;
width = 0;
height = 0;
于 2013-09-16T11:50:08.227 に答える