Environment: SDL2 + vc++2015
The following code will open Background.bmp and image.bmp, tile the background background, and render the image as a foreground
1#include <iostream>2#include"SDL.h"3 4 //Screen Width5 Const intScreen_width =640;6 Const intScreen_height =480;7 8 //Global window and renderer9Sdl_window *window =nullptr;TenSdl_renderer *renderer =nullptr; One A //Loading Pictures -sdl_texture* LoadImage (std::stringfile) - { theSdl_surface *loadedimage =nullptr; -Sdl_texture *texture =nullptr; - -Loadedimage =sdl_loadbmp (File.c_str ()); + if(Loadedimage! =nullptr) - { +Texture =sdl_createtexturefromsurface (renderer, loadedimage); A Sdl_freesurface (loadedimage); at } - Else -Std::cout << sdl_geterror () <<Std::endl; - returnTexture; - } - in //apply a surface to the renderer - voidApplysurface (intXintY, sdl_texture *tex, Sdl_renderer *rend) to { + Sdl_rect Pos; -Pos.x =x; thePos.y =y; *Sdl_querytexture (tex, NULL, NULL, &POS.W, &pos.h); $Sdl_rendercopy (Rend, Tex, NULL, &POS);Panax Notoginseng } - the intMainintargcChar**argv) + { A //Initialize SDL the if(Sdl_init (sdl_init_everything) = =-1) + { -Std::cout << sdl_geterror () <<Std::endl; $ return 1; $ } - - //Create Window thewindow = Sdl_createwindow ("Lesson 2", - sdl_windowpos_centered, sdl_windowpos_centered,Wuyi screen_width, Screen_height, sdl_window_shown); the if(Window = =nullptr) - { WuStd::cout << sdl_geterror () <<Std::endl; - return 2; About } $ - //Create renderer -Renderer = sdl_createrenderer (window,-1, -sdl_renderer_accelerated |sdl_renderer_presentvsync); A if(Renderer = =nullptr) + { theStd::cout << sdl_geterror () <<Std::endl; - return 3; $ } the the //create background and foreground textures theSdl_texture *background = nullptr, *image =nullptr; theBackground = LoadImage ("background.bmp"); -Image = LoadImage ("image.bmp"); in if(Background = = Nullptr | | image = =nullptr) the return 4; the About //Empty renderer the sdl_renderclear (renderer); the the //internal flush the background in the renderer + intBW, BH; -Sdl_querytexture (background, NULL, NULL, &BW, &BH); theApplysurface (0,0, background, renderer);BayiApplysurface (BW,0, background, renderer); theApplysurface (0, BH, background, renderer); the applysurface (BW, BH, background, renderer); - - //placing the foreground in the center of the renderer the intIW, IH; theSdl_querytexture (image, NULL, NULL, &IW, &IH); the intx = screen_width/2-IW/2; the inty = screen_height/2-IH/2; - applysurface (x, y, image, renderer); the the //Render Renderer the sdl_renderpresent (renderer);94Sdl_delay ( -); the the //Freeing Resources the sdl_destroytexture (background);98 sdl_destroytexture (image); About sdl_destroyrenderer (renderer); - Sdl_destroywindow (window);101 102 sdl_quit ();103 104 return 0; the}
Eggs:
What if the window becomes 1920*1080? The tiling method above is not flexible enough. Try the following:
1 int BW, BH; 2 Sdl_querytexture (background, NULL, NULL, &BW, &BH); 3 for (int0; y <= screen_height; y + = BH) 4 for (int0; x <= screen_width; x + = BW) 5 Applysurface (x, y, background, renderer);
Tile the background with SDL2 and display the foreground