11#include < string>
22#include < iostream>
33#include < SDL2/SDL.h>
4+ #include < SDL2pp/SDL2pp.hh>
45#include " res_path.h"
56#include " cleanup.h"
67
1112const int SCREEN_WIDTH = 640 ;
1213const int SCREEN_HEIGHT = 480 ;
1314
14- /*
15- * Log an SDL error with some error message to the output stream of our choice
16- * @param os The output stream to write the message too
17- * @param msg The error message to write, format will be msg error: SDL_GetError()
18- */
19- void logSDLError (std::ostream &os, const std::string &msg){
20- os << msg << " error: " << SDL_GetError () << std::endl;
21- }
22- /*
23- * Loads a BMP image into a texture on the rendering device
24- * @param file The BMP image file to load
25- * @param ren The renderer to load the texture onto
26- * @return the loaded texture, or nullptr if something went wrong.
27- */
28- SDL_Texture* loadTexture (const std::string &file, SDL_Renderer *ren){
29- SDL_Texture *texture = nullptr ;
30- // Load the image
31- SDL_Surface *loadedImage = SDL_LoadBMP (file.c_str ());
32- // If the loading went ok, convert to texture and return the texture
33- if (loadedImage != nullptr ){
34- texture = SDL_CreateTextureFromSurface (ren, loadedImage);
35- SDL_FreeSurface (loadedImage);
36- // Make sure converting went ok too
37- if (texture == nullptr ){
38- logSDLError (std::cout, " CreateTextureFromSurface" );
39- }
40- }
41- else {
42- logSDLError (std::cout, " LoadBMP" );
43- }
44- return texture;
45- }
4615/*
4716 * Draw an SDL_Texture to an SDL_Renderer at position x, y, preserving
4817 * the texture's width and height
@@ -51,77 +20,55 @@ SDL_Texture* loadTexture(const std::string &file, SDL_Renderer *ren){
5120 * @param x The x coordinate to draw too
5221 * @param y The y coordinate to draw too
5322 */
54- void renderTexture (SDL_Texture * tex, SDL_Renderer * ren, int x, int y){
23+ void renderTexture (SDL2pp::Texture & tex, SDL2pp::Renderer & ren, int x, int y){
5524 // Setup the destination rectangle to be at the position we want
56- SDL_Rect dst;
57- dst.x = x;
58- dst.y = y;
59- // Query the texture to get its width and height to use
60- SDL_QueryTexture (tex, NULL , NULL , &dst.w , &dst.h );
61- SDL_RenderCopy (ren, tex, NULL , &dst);
25+ SDL2pp::Rect dst (x, y, tex.GetWidth (), tex.GetHeight ());
26+ ren.Copy (tex, SDL2pp::NullOpt, dst);
6227}
6328
6429int main (int , char **){
65- // Start up SDL and make sure it went ok
66- if (SDL_Init (SDL_INIT_VIDEO) != 0 ){
67- logSDLError (std::cout, " SDL_Init" );
68- return 1 ;
69- }
30+ try {
31+ // Start up SDL and make sure it went ok
32+ SDL2pp::SDL sdl (SDL_INIT_VIDEO);
7033
71- // Setup our window and renderer
72- SDL_Window *window = SDL_CreateWindow (" Lesson 2" , 100 , 100 , SCREEN_WIDTH, SCREEN_HEIGHT, SDL_WINDOW_SHOWN);
73- if (window == nullptr ){
74- logSDLError (std::cout, " CreateWindow" );
75- SDL_Quit ();
76- return 1 ;
77- }
78- SDL_Renderer *renderer = SDL_CreateRenderer (window, -1 , SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC);
79- if (renderer == nullptr ){
80- logSDLError (std::cout, " CreateRenderer" );
81- cleanup (window);
82- SDL_Quit ();
83- return 1 ;
84- }
34+ // Setup our window and renderer
35+ SDL2pp::Window window (" Lesson 2" , 100 , 100 , SCREEN_WIDTH, SCREEN_HEIGHT, SDL_WINDOW_SHOWN);
36+ SDL2pp::Renderer renderer (window, -1 , SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC);
8537
86- // The textures we'll be using
87- const std::string resPath = getResourcePath (" Lesson2" );
88- SDL_Texture *background = loadTexture (resPath + " background.bmp" , renderer);
89- SDL_Texture *image = loadTexture (resPath + " image.bmp" , renderer);
90- // Make sure they both loaded ok
91- if (background == nullptr || image == nullptr ){
92- cleanup (background, image, renderer, window);
93- SDL_Quit ();
94- return 1 ;
95- }
38+ // The textures we'll be using
39+ const std::string resPath = getResourcePath (" Lesson2" );
40+ SDL2pp::Texture background (renderer, resPath + " background.bmp" );
41+ SDL2pp::Texture image (renderer, resPath + " image.bmp" );
42+
43+ // Clear the window
44+ renderer.Clear ();
9645
97- // Clear the window
98- SDL_RenderClear (renderer);
46+ // Get the width and height from the texture so we know how much to move x,y by
47+ // to tile it correctly
48+ int bW = background.GetWidth (), bH = background.GetHeight ();
9949
100- // Get the width and height from the texture so we know how much to move x,y by
101- // to tile it correctly
102- int bW, bH;
103- SDL_QueryTexture (background, NULL , NULL , &bW, &bH);
104- // We want to tile our background so draw it 4 times
105- renderTexture (background, renderer, 0 , 0 );
106- renderTexture (background, renderer, bW, 0 );
107- renderTexture (background, renderer, 0 , bH);
108- renderTexture (background, renderer, bW, bH);
50+ // We want to tile our background so draw it 4 times
51+ renderTexture (background, renderer, 0 , 0 );
52+ renderTexture (background, renderer, bW, 0 );
53+ renderTexture (background, renderer, 0 , bH);
54+ renderTexture (background, renderer, bW, bH);
10955
110- // Draw our image in the center of the window
111- // We need the foreground image's width to properly compute the position
112- // of it's top left corner so that the image will be centered
113- int iW, iH;
114- SDL_QueryTexture (image, NULL , NULL , &iW, &iH);
115- int x = SCREEN_WIDTH / 2 - iW / 2 ;
116- int y = SCREEN_HEIGHT / 2 - iH / 2 ;
117- renderTexture (image, renderer, x, y);
56+ // Draw our image in the center of the window
57+ // We need the foreground image's width to properly compute the position
58+ // of it's top left corner so that the image will be centered
59+ int iW = image.GetWidth (), iH = image.GetHeight ();
60+ int x = SCREEN_WIDTH / 2 - iW / 2 ;
61+ int y = SCREEN_HEIGHT / 2 - iH / 2 ;
62+ renderTexture (image, renderer, x, y);
11863
119- // Update the screen
120- SDL_RenderPresent ( renderer);
121- SDL_Delay (2000 );
64+ // Update the screen
65+ renderer. Present ( );
66+ SDL_Delay (2000 );
12267
123- cleanup (background, image, renderer, window);
124- SDL_Quit ();
68+ } catch (SDL2pp::Exception& e) {
69+ std::cerr << e.GetSDLFunction () << " Error: " << e.GetSDLError () << std::endl;
70+ return 1 ;
71+ }
12572
12673 return 0 ;
12774}
0 commit comments