也是在前一课的基础上稍作修改的。
#include <iostream> #include <windows.h> #include <string> using namespace std; #include <SDL.h> #include <SDL_image.h> const int SCREEN_WIDTH = 640; const int SCREEN_HEIGHT = 480; SDL_Window *win = nullptr; SDL_Renderer *ren = nullptr; SDL_Texture * LoadImage(string file) { SDL_Texture *tex = nullptr; tex = IMG_LoadTexture(ren, file.c_str()); if (tex == nullptr) { cout << "Fail to load image in function LoadImage()!" << endl; } return tex; } void renderTexture(SDL_Texture *tex, SDL_Renderer *ren, int x, int y, int w, int h) { SDL_Rect dst; dst.x = x; dst.y = y; dst.w = w; dst.h = h; SDL_RenderCopy(ren, tex, NULL, &dst); } void renderTexture(SDL_Texture *tex, SDL_Renderer *ren, int x, int y) { int w, h; SDL_QueryTexture(tex, NULL, NULL, &w, &h); renderTexture(tex, ren, x, y, w, h); } int main(int argc, char** argv) { if (SDL_Init(SDL_INIT_EVERYTHING) == -1) { cout << SDL_GetError() << endl; return 0; } win = SDL_CreateWindow("04:", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, SCREEN_WIDTH, SCREEN_HEIGHT, SDL_WINDOW_RESIZABLE); if (win == nullptr) { cout << SDL_GetError() << endl; return 0; } ren = SDL_CreateRenderer(win, -1, SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC); if (ren == nullptr) { cout << SDL_GetError() << endl; return 0; } SDL_Texture *bg = LoadImage("f:/material/giantShadowFlash.jpg"); if (bg == nullptr) { cout << "Could not load one of the images!" << endl; return 0; } SDL_RenderClear(ren); int bw, bh; SDL_QueryTexture(bg, NULL, NULL, &bw, &bh); SDL_Event e; bool quit = false; while (!quit) { while (SDL_PollEvent(&e)) { if (e.type == SDL_QUIT) { cout << "quit type..." << endl; quit = true; } if (e.type == SDL_KEYDOWN) { cout << "keydown type..." << endl; quit = true; } if (e.type == SDL_MOUSEBUTTONDOWN) { cout << "mousebuttondown type..." << endl; quit = true; } } SDL_RenderClear(ren); renderTexture(bg, ren, SCREEN_WIDTH/2 - bw/2, SCREEN_HEIGHT/2 - bh/2); SDL_RenderPresent(ren); } SDL_DestroyTexture(bg); SDL_DestroyRenderer(ren); SDL_DestroyWindow(win); SDL_Quit(); IMG_Quit(); cout << "Success!" << endl; return 0; }