EDIT:
あなたがここで何をしたいのかが画面にテクスチャの一部をレンダリングしています。
SDL_RenderCopyを使用してこれを行う方法がありますが、これを行うことで、あなたが望む部分をテクスチャから "取って"画面に "スラップ"するだけです。
あなたが望むのは、テクスチャの一部を取り出し、それを後で画面にレンダリングできる別のテクスチャ変数に保存することです。問題へ
最初のソリューションは、このように書き:
// load your image in a SDL_Texture variable (myTexture for example)
// if the image is (256,128) and you want to render only the first half
// you need a rectangle of the same size as that part of the image
SDL_Rect imgPartRect;
imgPartRect.x = 0;
imgPartRect.y = 0;
imgPartRect.w = 32;
imgPartRect.h = 32;
// and the program loop should have a draw block looking like this:
SDL_SetRenderDrawColor(renderer, 0x00, 0x00, 0x00, 0xFF);
SDL_RenderClear(renderer);
SDL_RenderCopy(renderer, myTexture, &imgPartRect, NULL);
SDL_RenderPresent(renderer);
使用しようとしているアプローチは、あなたが画面にそのテクスチャをレンダリング上、その後のレンダリング中間テクスチャを持っています。ここでの問題は、作成したテクスチャをレンダリングするようにレンダラを設定することですが、レンダラをリセットしてデフォルトのターゲット(画面)を使用することはありません。
hereのように、2番目のパラメータは、レンダラーに描画させるテクスチャを受け取ります。デフォルトのターゲット(画面)にリセットする場合は、NULLを受け取ります。
int型SDL_SetRenderTarget(SDL_Renderer *レンダラ、SDL_Texture *テクスチャ)
:
レンダリングコンテキスト
デフォルトのための目標とSDL_TEXTUREACCESS_TARGETフラグを使用して作成する必要がありますテクスチャ、またはNULLレンダーターゲット
は前と同じ例を使用することができます:私はしません
// first off let's build the function that you speak of
SDL_Texture* GetAreaTextrue(SDL_Rect rect, SDL_Renderer* renderer, SDL_Texture* source)
{
SDL_Texture* result = SDL_CreateTexture(renderer, SDL_PIXELFORMAT_RGBA8888, SDL_TEXTUREACCESS_TARGET, rect.w, rect.h);
SDL_SetRenderTarget(renderer, result);
SDL_RenderCopy(renderer, source, &rect, NULL);
// the folowing line should reset the target to default(the screen)
SDL_SetRenderTarget(renderer, NULL);
// I also removed the RenderPresent funcion as it is not needed here
return result;
}
// load your image in a SDL_Texture variable (myTexture for example)
// if the image is (256,128) and you want to render only the first half
// you need a rectangle of the same size as that part of the image
SDL_Rect imgPartRect;
imgPartRect.x = 0;
imgPartRect.y = 0;
imgPartRect.w = 32;
imgPartRect.h = 32;
// now we use the function from above to build another texture
SDL_Texture* myTexturePart = GetAreaTextrue(imgPartRect, renderer, myTexture);
// and the program loop should have a draw block looking like this:
SDL_SetRenderDrawColor(renderer, 0x00, 0x00, 0x00, 0xFF);
SDL_RenderClear(renderer);
// here we render the whole newly created texture as it contains only a part of myTexture
SDL_RenderCopy(renderer, myTexturePart, NULL, NULL);
SDL_RenderPresent(renderer);
あなたがしたいことを知っているが、私は非常に最初の方法をお勧めします。