Skip to content

Instantly share code, notes, and snippets.

@Twinklebear
Created July 27, 2012 01:58
Show Gist options
  • Save Twinklebear/3185765 to your computer and use it in GitHub Desktop.
Save Twinklebear/3185765 to your computer and use it in GitHub Desktop.
SDL_RenderCopy(rend, tex, NULL, &pos);
void ApplySurface(int x, int y, SDL_Texture *tex, SDL_Renderer *rend, SDL_Rect *clip = NULL){
SDL_Rect pos;
pos.x = x;
pos.y = y;
//Detect if we should use clip width settings or texture width
if (clip != NULL){
pos.w = clip->w;
pos.h = clip->h;
}
else {
SDL_QueryTexture(tex, NULL, NULL, &pos.w, &pos.h);
}
SDL_RenderCopy(rend, tex, clip, &pos);
}
//iW and iH are the desired clip width and height
int iW = 100, iH = 100;
SDL_Rect clips[4];
//We use a for loop this time to setup our clips
int column = 0;
for (int i = 0; i < 4; ++i){
if (i != 0 && i % 2 == 0)
++column;
clips[i].x = column * iW;
clips[i].y = i % 2 * iH;
clips[i].w = iW;
clips[i].h = iH;
}
//Specify a default clip to start with
int useClip = 0;
//If user presses any key
if (e.type == SDL_KEYDOWN){
switch (e.key.keysym.sym){
case SDLK_1:
useClip = 0;
break;
case SDLK_2:
useClip = 1;
break;
case SDLK_3:
useClip = 2;
break;
case SDLK_4:
useClip = 3;
break;
//For quitting, escape key
case SDLK_ESCAPE:
quit = true;
break;
default:
break;
}
}
ApplySurface(x, y, image, renderer, &clips[useClip]);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment