편집 :
당신이 여기서 뭘 원하는 것은 화면에 텍스처의 일부를 렌더링입니다.
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에서 볼 수 있듯이 두 번째 매개 변수는 렌더러에서 그릴 텍스처를 받거나, 기본 타겟 (스크린)으로 재설정하려는 경우 NULL입니다.
INT의 SDL_SetRenderTarget (SDL_Renderer * 렌더러 SDL_Texture의 * 텍스처)
여기서
렌더링 컨텍스트
// 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);
그렇게하지 : 기본에 대한 617,451,515,
대상 SDL_TEXTUREACCESS_TARGET 플래그를 작성해야합니다 질감, 또는 NULL 렌더 타겟
는 이전과 같은 예를 사용할 수 있습니다 네가하고 싶은 것을 알고 있지만, 나는 첫 번째 방법을 강력히 추천한다.