原教程
通过键盘控制窗口的图片
#include <SDL2/SDL.h>
#include <iostream>
#include <string>
const int SCR_WIDTH = 800;
const int SCR_HEIGHT = 600;
enum KeyPressSurfaces {
KEY_PRESS_SURFACE_DEFAULT,
KEY_PRESS_SURFACE_UP,
KEY_PRESS_SURFACE_DOWN,
KEY_PRESS_SURFACE_LEFT,
KEY_PRESS_SURFACE_RIGHT,
KEY_PRESS_SURFACE_TOTAL
};
bool Init();
bool LoadMedia();
void Close();
SDL_Surface* LoadSurface(std::string path);
SDL_Window *gWindow = NULL;
SDL_Surface *gScreenSurface = NULL;
SDL_Surface *gKeyPressSurfaces[KEY_PRESS_SURFACE_TOTAL];
SDL_Surface *gCurrentSurface = NULL;
int main(int argc, char* args[])
{
if (!Init()) {
std::cout << "Failed to initialize!\n";
}
else {
if (!LoadMedia()) {
std::cout << "Failed to load media!\n";
}
else {
//Main_loop_flag
bool quit = false;
//Event handler
SDL_Event e;
//While application is running
while (!quit) {
//Handle events on queue
while (SDL_PollEvent(&e) !=0) {
//User requests quit
if (e.type == SDL_QUIT) {
quit = true;
}
//User presses a key
else if (e.type == SDL_KEYDOWN) {
//Select surfaces based on key press
switch (e.key.keysym.sym) {
case SDLK_UP:
gCurrentSurface = gKeyPressSurfaces[KEY_PRESS_SURFACE_UP];
break;
case SDLK_DOWN:
gCurrentSurface = gKeyPressSurfaces[KEY_PRESS_SURFACE_DOWN];
break;
case SDLK_LEFT:
gCurrentSurface = gKeyPressSurfaces[KEY_PRESS_SURFACE_LEFT];
break;
case SDLK_RIGHT:
gCurrentSurface = gKeyPressSurfaces[KEY_PRESS_SURFACE_RIGHT];
break;
default:
gCurrentSurface = gKeyPressSurfaces[KEY_PRESS_SURFACE_DEFAULT];
break;
}
}
}
SDL_BlitSurface(gCurrentSurface, NULL, gScreenSurface, NULL);
SDL_UpdateWindowSurface(gWindow);
}
}
}
Close();
return 0;
}
bool Init()
{
bool success = true;
if (SDL_Init(SDL_INIT_VIDEO) < 0) {
std::cout << "SLD could not initialize! SDL_Error: " << SDL_GetError() << std::endl;
success = false;
}
else {
gWindow = SDL_CreateWindow("SDL_Window", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED,
SCR_WIDTH, SCR_HEIGHT, SDL_WINDOW_SHOWN);
}
if (gWindow == NULL) {
std::cout << "Window could not be created! SDL_Error: " << SDL_GetError() << std::endl;
success = false;
}
else {
gScreenSurface = SDL_GetWindowSurface(gWindow);
}
return success;
}
SDL_Surface* LoadSurface(std::string path)
{
SDL_Surface* loadedSurface = SDL_LoadBMP(path.c_str());
if (loadedSurface == NULL) {
std::cout << "Unable to load image! SDL Error: " << path.c_str() << ' ' << SDL_GetError() << std::endl;
}
return loadedSurface;
}
bool LoadMedia()
{
bool success = true;
//Load default surface
gKeyPressSurfaces[KEY_PRESS_SURFACE_DEFAULT] = LoadSurface("04_key_presses/press.bmp");
if (gKeyPressSurfaces[KEY_PRESS_SURFACE_DEFAULT] == NULL) {
std::cout << "Failed to lead default image!\n";
success = false;
}
//Load up surface
gKeyPressSurfaces[KEY_PRESS_SURFACE_UP] = LoadSurface("04_key_presses/up.bmp");
if (gKeyPressSurfaces[KEY_PRESS_SURFACE_UP] == NULL) {
std::cout << "Failed to load up image!\n";
success = false;
}
//Load down surface
gKeyPressSurfaces[KEY_PRESS_SURFACE_DOWN] = LoadSurface("04_key_presses/down.bmp");
if (gKeyPressSurfaces[KEY_PRESS_SURFACE_DOWN] == NULL) {
std::cout << "Failed to load down image!\n";
success = false;
}
//Load left surface
gKeyPressSurfaces[KEY_PRESS_SURFACE_LEFT] = LoadSurface("04_key_presses/left.bmp");
if (gKeyPressSurfaces[KEY_PRESS_SURFACE_LEFT] == NULL) {
std::cout << "Failed to load left image!\n";
success = false;
}
//Load right surface
gKeyPressSurfaces[KEY_PRESS_SURFACE_RIGHT] = LoadSurface("04_key_presses/right.bmp");
if (gKeyPressSurfaces[KEY_PRESS_SURFACE_RIGHT] == NULL) {
std::cout << "Failed to load right image!\n";
success = false;
}
return success;
}
void Close()
{
SDL_FreeSurface(gCurrentSurface);
gCurrentSurface = NULL;
SDL_DestroyWindow(gWindow);
gWindow = NULL;
SDL_Quit();
}```