SDL小作业之键盘按压事件(Lazy Foo' SDL2 Lesson4)

wwwen 97 2022-07-17

运行结果

按UP DOWN RIGHT LEFT 分别切换为对应的图片并刷新CurrentSurface

代码

#include <iostream>
#include "SDL.h"
#include <string>

enum KeyPressSurfaces
{
	KEY_PRESS_SURFACE_DEFAULT,
	KEY_PRESS_SURFACE_UP,
	KEY_PRESS_SURFACE_DOWN,
	KEY_PRESS_SURFACE_RIGHT,
	KEY_PRESS_SURFACE_LEFT,
	KEY_PRESS_SURFACE_TOTAL
};
const int SCREEN_WIDTH = 640;
const int SCREEN_HEIGHT = 480;
bool initWindow();//建立窗口函数

bool loadMedia();//加载媒体函数

bool deallocate();//释放窗口函数

SDL_Surface* loadSurface(std::string path);//加载独立界面函数

SDL_Window* window =NULL;//主窗口
SDL_Surface* screenSurface = NULL;//主窗口界面	
SDL_Surface* CurrentSurface = NULL;//目前显示的媒体界面
SDL_Surface* KEYPRESSSURFACE[KEY_PRESS_SURFACE_TOTAL];//绑定按键
//KEYPRESS常数
int main(int argc, char* argv[])
{
	if (!initWindow())
	{
		printf("Failed to initialize!\n");
	}
	else
	{
		if (!loadMedia())
		{
			printf("Failed to load the media!\n");
		}
		else
		{
			bool quit = false;//循环控制参数
			SDL_Event event;//初始化事件声明
			CurrentSurface = KEYPRESSSURFACE[KEY_PRESS_SURFACE_DEFAULT];//设置默认绘图
			while (!quit)
			{
	
				while (SDL_PollEvent(&event) !=0) {
					if (event.type == SDL_QUIT)
					{
						quit = true;
					}
					else if (event.type == SDL_KEYDOWN)
					{

						switch (event.key.keysym.sym) //绑定按键事件切换
						{
						case SDLK_UP:
							CurrentSurface = KEYPRESSSURFACE[KEY_PRESS_SURFACE_UP];
							break;
						case SDLK_DOWN:
							CurrentSurface = KEYPRESSSURFACE[KEY_PRESS_SURFACE_DOWN];
							break;
						case SDLK_RIGHT:
							CurrentSurface = KEYPRESSSURFACE[KEY_PRESS_SURFACE_RIGHT];
							break;
						case SDLK_LEFT:
							CurrentSurface = KEYPRESSSURFACE[KEY_PRESS_SURFACE_LEFT];
							break;
						default:
							CurrentSurface = KEYPRESSSURFACE[KEY_PRESS_SURFACE_DEFAULT];
						}
					}
				}
				SDL_BlitSurface(CurrentSurface, NULL, screenSurface, NULL);//绘图
				SDL_UpdateWindowSurface(window);
			}
			//SDL_BlitSurface(media, NULL, screenSurface, NULL);
			//SDL_UpdateWindowSurface(window);
			//SDL_Delay(5000);
		}
	}
	deallocate();
    return 0;
}

bool initWindow() {
    bool flag = true;
	if (SDL_Init(SDL_INIT_VIDEO) < 0)
	{
		flag = false;
		printf("Init Failed:%s", SDL_GetError());
	}
	else
	{
		window = SDL_CreateWindow("SDLSample", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, SCREEN_WIDTH, SCREEN_HEIGHT, SDL_WINDOW_SHOWN);
		if (window ==NULL)
		{
			flag = false;
			printf("windowcreate Failed:%s", SDL_GetError());
		}
		else
		{
			screenSurface = SDL_GetWindowSurface(window);
		}
	}
	return flag;
}

bool loadMedia() {
	bool flag = true;
	KEYPRESSSURFACE[KEY_PRESS_SURFACE_DEFAULT] = loadSurface("D:/Code/CppCode/SDLgame/SDLgame/bitmap1.bmp");
	if (KEYPRESSSURFACE[KEY_PRESS_SURFACE_DEFAULT] ==NULL)
	{
		printf("Failed to load DEFAULT image!\n");
		flag = false;
	}
	KEYPRESSSURFACE[KEY_PRESS_SURFACE_UP] = loadSurface("D:/Code/CppCode/SDLgame/SDLgame/bitmap2.bmp");
	if (KEYPRESSSURFACE[KEY_PRESS_SURFACE_UP] == NULL)
	{
		printf("Failed to load UP image!\n");
		flag = false;
	}
	KEYPRESSSURFACE[KEY_PRESS_SURFACE_DOWN] = loadSurface("D:/Code/CppCode/SDLgame/SDLgame/bitmap3.bmp");
	if (KEYPRESSSURFACE[KEY_PRESS_SURFACE_DOWN] == NULL)
	{
		printf("Failed to load DOWN image!\n");
		flag = false;
	}
	KEYPRESSSURFACE[KEY_PRESS_SURFACE_RIGHT] = loadSurface("D:/Code/CppCode/SDLgame/SDLgame/bitmap4.bmp");
	if (KEYPRESSSURFACE[KEY_PRESS_SURFACE_RIGHT] == NULL)
	{
		printf("Failed to load RIGHT image!\n");
		flag = false;
	}
	KEYPRESSSURFACE[KEY_PRESS_SURFACE_LEFT] = loadSurface("D:/Code/CppCode/SDLgame/SDLgame/bitmap5.bmp");
	if (KEYPRESSSURFACE[KEY_PRESS_SURFACE_LEFT] == NULL)
	{
		printf("Failed to load LEFT image!\n");
		flag = false;
	}
	return flag;
}

bool deallocate() {
	SDL_DestroyWindow(window);
	window = NULL;
	for (int i = 0; i < KEY_PRESS_SURFACE_TOTAL; i++)
	{
		SDL_FreeSurface(KEYPRESSSURFACE[i]);
		KEYPRESSSURFACE[i] = NULL;
	}

	SDL_Quit();
	return true;
}

SDL_Surface* loadSurface(std::string path) {
	SDL_Surface* loadedSurface = SDL_LoadBMP(path.c_str());
	if (loadedSurface == NULL)
	{
		printf("UNABLE TO LOAD THE FILE SDL ERROR:%s", SDL_GetError());
	}
	return loadedSurface;
}

TIPS

Int SDL_PollEvent(SDL_Event * event);

RETURN:Returns 1 if there is a pending event or 0 if there are none available.