Load PNG tile background with SDL2 and display foreground

Source: Internet
Author: User

The previous article loaded the BMP, this time can refer to Sdl2_image.lib, loading more formats of the image.

The LoadImage function is changed, except that you don't have to convert your surface into texture.

Environment: SDL2 + vc++2015

The following code will open Background.png and image.png, tile the background background, and render the image as a foreground.

1#include <stdexcept>2#include <string>3#include <iostream>4#include"SDL.h"5#include"sdl_image.h"6 7 //Screen Width8 Const intScreen_width = the;9 Const intScreen_height = the;Ten  One //Global window and renderer ASdl_window *window =nullptr; -Sdl_renderer *renderer =nullptr; -  the //Loading Pictures -sdl_texture* LoadImage (std::stringfile) - { -sdl_texture* Tex =nullptr; +Tex =img_loadtexture (Renderer, File.c_str ()); -     if(Tex = =nullptr) +         ThrowStd::runtime_error ("Failed to load Image:"+ file +img_geterror ()); A     returnTex; at } -  - //apply a surface to the renderer - voidApplysurface (intXintY, sdl_texture *tex, Sdl_renderer *rend) - { - Sdl_rect Pos; inPos.x =x; -Pos.y =y; toSdl_querytexture (tex, NULL, NULL, &AMP;POS.W, &pos.h); +Sdl_rendercopy (Rend, Tex, NULL, &POS); - } the  * intMainintargcChar**argv) $ {Panax Notoginseng     //Initialize SDL -     if(Sdl_init (sdl_init_everything) = =-1) the     { +Std::cout << sdl_geterror () <<Std::endl; A         return 1; the     } +  -     //Create Window $window = Sdl_createwindow ("Lesson 3", $ sdl_windowpos_centered, sdl_windowpos_centered, - screen_width, Screen_height, sdl_window_shown); -     if(Window = =nullptr) the     { -Std::cout << sdl_geterror () <<Std::endl;Wuyi         return 2; the     } -  Wu     //Create renderer -Renderer = sdl_createrenderer (window,-1, Aboutsdl_renderer_accelerated |sdl_renderer_presentvsync); $     if(Renderer = =nullptr) -     { -Std::cout << sdl_geterror () <<Std::endl; -         return 3; A     } +  the     //create background and foreground textures -Sdl_texture *background = nullptr, *image =nullptr; $     Try { theBackground = LoadImage ("Background.png"); theImage = LoadImage ("Image.png"); the     } the     Catch(ConstStd::runtime_error &e) { -Std::cout << e.what () <<Std::endl; in         return 4; the     } the  About     //Empty renderer the sdl_renderclear (renderer); the  the     //internal flush the background in the renderer +     intBW, BH; -Sdl_querytexture (background, NULL, NULL, &AMP;BW, &BH); the      for(inty =0; Y <= screen_height; Y + =BH)Bayi          for(intx =0; x <= screen_width; X + =BW) the applysurface (x, y, background, renderer); the  -  -     //placing the foreground in the center of the renderer the     intIW, IH; theSdl_querytexture (image, NULL, NULL, &AMP;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}

Load PNG tile background with SDL2 and display foreground

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.