目的
裁剪一个图片中的多个子图片作为图片使用
初始化
创建窗口
载入图片
200*200
//Start up SDL and make sure it went ok if (SDL_Init(SDL_INIT_VIDEO) != 0) { logSDLError(std::cout, "SDL_Init"); return 1; } //Setup our window and renderer SDL_Window* window = SDL_CreateWindow("Lesson 5", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, SCREEN_WIDTH, SCREEN_HEIGHT, SDL_WINDOW_SHOWN); if (window == nullptr) { logSDLError(std::cout, "CreateWindow"); SDL_Quit(); return 1; } SDL_Renderer* renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC); if (renderer == nullptr) { logSDLError(std::cout, "CreateRenderer"); cleanup(window); SDL_Quit(); return 1; } const std::string resPath = getResourcePath("Lesson5"); SDL_Texture* image = loadTexture("..\\..\\res\\05sdl_learn\\image.png", renderer); if (image == nullptr) { cleanup(image, renderer, window); IMG_Quit(); SDL_Quit(); return 1; }
确认裁剪坐标
确认裁剪显示坐标
int iW = 100, iH = 100; int x = SCREEN_WIDTH / 2 - iW / 2; int y = SCREEN_HEIGHT / 2 - iH / 2;
确认裁剪大小的圆为100*100
绿(0,0)
蓝(0,100)
红(100,0)
黄(100,100)
//Setup the clips for our image SDL_Rect clips[4]; //Since our clips our uniform in size we can generate a list of their //positions using some math (the specifics of this are covered in the lesson) for (int i = 0; i < 4; ++i) { clips[i].x = i / 2 * iW; clips[i].y = i % 2 * iH; clips[i].w = iW; clips[i].h = iH; SDL_Log("(%d,%d)", clips[i].x, clips[i].y); }
按下1,2,3,4显示不同裁剪图片
//Specify a default clip to start with int useClip = 0; SDL_Event e; bool quit = false; while (!quit) { //Event Polling while (SDL_PollEvent(&e)) { if (e.type == SDL_QUIT) { quit = true; } //Use number input to select which clip should be drawn if (e.type == SDL_KEYDOWN) { switch (e.key.keysym.sym) { case SDLK_1: case SDLK_KP_1: useClip = 0; break; case SDLK_2: case SDLK_KP_2: useClip = 1; break; case SDLK_3: case SDLK_KP_3: useClip = 2; break; case SDLK_4: case SDLK_KP_4: useClip = 3; break; case SDLK_ESCAPE: quit = true; break; default: break; } } } //Rendering SDL_RenderClear(renderer); //Draw the image renderTexture(image, renderer, x, y, &clips[useClip]); //Update the screen SDL_RenderPresent(renderer); } //Clean up cleanup(image, renderer, window); IMG_Quit(); SDL_Quit();
总结
ApplySurface
SDL_RenderCopy
SDL_QueryTexture含义
查询Texture属性