SDL之鼠标移动事件

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/m0_38126105/article/details/85227584

这节没有什么特殊的东西,需要注意的是,当鼠标移动时,会产生影子,这是需要更新之前,将背景进行刷新

#include "SDL.h"
#include "SDL_image.h"
int main(int argc, char* argv[])
{
	SDL_Init(SDL_INIT_VIDEO);
	SDL_Window* window = SDL_CreateWindow("hello SDL", 100, 100, 600, 800, SDL_WINDOW_SHOWN);
	SDL_Surface* surface = SDL_GetWindowSurface(window);
	SDL_Surface* picture = SDL_LoadBMP("C:\\Users\\xxxxx\\Desktop\\1.bmp");
	SDL_BlitSurface(picture, NULL, surface, NULL);
	SDL_UpdateWindowSurface(window);
	SDL_Rect rect;
	rect.x = 0;
	rect.y = 0;

	SDL_Event event;
	bool quit = false;
	while (false == quit)
	{
		while (SDL_PollEvent(&event))
		{
			if (SDL_QUIT == event.type)
			{
				quit = true;
			}
			else if (SDL_MOUSEMOTION == event.type)
			{
				int x = event.motion.x - picture->w / 2;
				int y = event.motion.y - picture->h / 2;
				printf("x = %d,y=%d",x,y);
				rect.x = x;
				rect.y = y;
				SDL_FillRect(surface, NULL, 0); //去除尾际
				SDL_BlitSurface(picture, NULL, surface, &rect);
				SDL_UpdateWindowSurface(window);
			}
		}
	}

	SDL_FreeSurface(surface);
	SDL_FreeSurface(picture);
	SDL_DestroyWindow(window);
	SDL_Quit();
}

猜你喜欢

转载自blog.csdn.net/m0_38126105/article/details/85227584