Skip to content

Commit e4dfdb6

Browse files
committed
Convert examples to SDL2pp
1 parent 65e6b61 commit e4dfdb6

File tree

18 files changed

+305
-1144
lines changed

18 files changed

+305
-1144
lines changed

CMakeLists.txt

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,10 @@ elseif (${CMAKE_CXX_COMPILER_ID} STREQUAL "MSVC")
1818
endif()
1919
endif()
2020

21-
find_package(SDL2 REQUIRED)
22-
include_directories(${SDL2_INCLUDE_DIR})
21+
set(SDL2PP_WITH_IMAGE ON)
22+
set(SDL2PP_WITH_TTF ON)
23+
add_subdirectory(extlibs/SDL2pp)
24+
include_directories(${SDL2PP_INCLUDE_DIRS})
2325
include_directories(include)
2426

2527
add_subdirectory(Lesson0)

Lesson0/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
project(Lesson0)
22
add_executable(Lesson0 src/main.cpp)
3-
target_link_libraries(Lesson0 ${SDL2_LIBRARY})
3+
target_link_libraries(Lesson0 ${SDL2PP_LIBRARIES})
44
install(TARGETS Lesson0 RUNTIME DESTINATION ${BIN_DIR})
55

Lesson0/src/main.cpp

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,17 @@
11
#include <iostream>
22
#include <SDL2/SDL.h>
3+
#include <SDL2pp/SDL2pp.hh>
34

45
/*
56
* Lesson 0: Test to make sure SDL is setup properly
67
*/
78
int main(int, char**){
8-
if (SDL_Init(SDL_INIT_VIDEO) != 0){
9-
std::cout << "SDL_Init Error: " << SDL_GetError() << std::endl;
9+
try {
10+
SDL2pp::SDL(SDL_INIT_VIDEO);
11+
} catch (SDL2pp::Exception& e) {
12+
std::cout << e.GetSDLFunction() << " Error: " << e.GetSDLError() << std::endl;
1013
return 1;
1114
}
12-
SDL_Quit();
1315

1416
return 0;
1517
}

Lesson1/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
project(Lesson1)
22
add_executable(Lesson1 src/main.cpp)
3-
target_link_libraries(Lesson1 ${SDL2_LIBRARY})
3+
target_link_libraries(Lesson1 ${SDL2PP_LIBRARIES})
44
install(TARGETS Lesson1 RUNTIME DESTINATION ${BIN_DIR})
55

Lesson1/src/main.cpp

Lines changed: 34 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -1,80 +1,50 @@
11
#include <iostream>
22
#include <SDL2/SDL.h>
3+
#include <SDL2pp/SDL2pp.hh>
34
#include "res_path.h"
45

56
/*
67
* Lesson 1: Hello World!
78
*/
89
int main(int, char**){
9-
//First we need to start up SDL, and make sure it went ok
10-
if (SDL_Init(SDL_INIT_VIDEO) != 0){
11-
std::cout << "SDL_Init Error: " << SDL_GetError() << std::endl;
12-
return 1;
13-
}
14-
15-
//Now create a window with title "Hello World" at 100, 100 on the screen with w:640 h:480 and show it
16-
SDL_Window *win = SDL_CreateWindow("Hello World!", 100, 100, 640, 480, SDL_WINDOW_SHOWN);
17-
//Make sure creating our window went ok
18-
if (win == nullptr){
19-
std::cout << "SDL_CreateWindow Error: " << SDL_GetError() << std::endl;
20-
return 1;
21-
}
10+
try {
11+
//First we need to start up SDL, and make sure it went ok
12+
SDL2pp::SDL(SDL_INIT_VIDEO);
2213

23-
//Create a renderer that will draw to the window, -1 specifies that we want to load whichever
24-
//video driver supports the flags we're passing
25-
//Flags: SDL_RENDERER_ACCELERATED: We want to use hardware accelerated rendering
26-
//SDL_RENDERER_PRESENTVSYNC: We want the renderer's present function (update screen) to be
27-
//synchornized with the monitor's refresh rate
28-
SDL_Renderer *ren = SDL_CreateRenderer(win, -1, SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC);
29-
if (ren == nullptr){
30-
SDL_DestroyWindow(win);
31-
std::cout << "SDL_CreateRenderer Error: " << SDL_GetError() << std::endl;
32-
SDL_Quit();
33-
return 1;
34-
}
14+
//Now create a window with title "Hello World" at 100, 100 on the screen with w:640 h:480 and show it
15+
SDL2pp::Window win("Hello World!", 100, 100, 640, 480, SDL_WINDOW_SHOWN);
3516

36-
//SDL 2.0 now uses textures to draw things but SDL_LoadBMP returns a surface
37-
//this lets us choose when to upload or remove textures from the GPU
38-
std::string imagePath = getResourcePath("Lesson1") + "hello.bmp";
39-
SDL_Surface *bmp = SDL_LoadBMP(imagePath.c_str());
40-
if (bmp == nullptr){
41-
SDL_DestroyRenderer(ren);
42-
SDL_DestroyWindow(win);
43-
std::cout << "SDL_LoadBMP Error: " << SDL_GetError() << std::endl;
44-
SDL_Quit();
45-
return 1;
46-
}
17+
//Create a renderer that will draw to the window, -1 specifies that we want to load whichever
18+
//video driver supports the flags we're passing
19+
//Flags: SDL_RENDERER_ACCELERATED: We want to use hardware accelerated rendering
20+
//SDL_RENDERER_PRESENTVSYNC: We want the renderer's present function (update screen) to be
21+
//synchornized with the monitor's refresh rate
22+
SDL2pp::Renderer ren(win, -1, SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC);
4723

48-
//To use a hardware accelerated texture for rendering we can create one from
49-
//the surface we loaded
50-
SDL_Texture *tex = SDL_CreateTextureFromSurface(ren, bmp);
51-
//We no longer need the surface
52-
SDL_FreeSurface(bmp);
53-
if (tex == nullptr){
54-
SDL_DestroyRenderer(ren);
55-
SDL_DestroyWindow(win);
56-
std::cout << "SDL_CreateTextureFromSurface Error: " << SDL_GetError() << std::endl;
57-
SDL_Quit();
58-
return 1;
59-
}
60-
61-
//Now lets draw our image
62-
//First clear the renderer
63-
SDL_RenderClear(ren);
64-
//Draw the texture
65-
SDL_RenderCopy(ren, tex, NULL, NULL);
66-
//Update the screen
67-
SDL_RenderPresent(ren);
24+
//SDL 2.0 now uses textures to draw things but SDL_LoadBMP returns a surface
25+
//this lets us choose when to upload or remove textures from the GPU
26+
std::string imagePath = getResourcePath("Lesson1") + "hello.bmp";
27+
SDL2pp::Surface bmp(imagePath);
6828

69-
//Have the program wait for 2000ms so we get a chance to see the screen
70-
SDL_Delay(2000);
29+
//To use a hardware accelerated texture for rendering we can create one from
30+
//the surface we loaded
31+
SDL2pp::Texture tex(ren, bmp);
32+
33+
//Now lets draw our image
34+
//First clear the renderer
35+
ren.Clear();
36+
//Draw the texture
37+
ren.Copy(tex, SDL2pp::NullOpt, SDL2pp::NullOpt);
38+
//Update the screen
39+
ren.Present();
40+
41+
//Have the program wait for 2000ms so we get a chance to see the screen
42+
SDL_Delay(2000);
43+
44+
} catch (SDL2pp::Exception& e) {
45+
std::cout << e.GetSDLFunction() << " Error: " << e.GetSDLError() << std::endl;
46+
}
7147

72-
//Clean up our objects and quit
73-
SDL_DestroyTexture(tex);
74-
SDL_DestroyRenderer(ren);
75-
SDL_DestroyWindow(win);
76-
SDL_Quit();
77-
7848
return 0;
7949
}
8050

Lesson2/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
project(Lesson2)
22
add_executable(Lesson2 src/main.cpp)
3-
target_link_libraries(Lesson2 ${SDL2_LIBRARY})
3+
target_link_libraries(Lesson2 ${SDL2PP_LIBRARIES})
44
install(TARGETS Lesson2 RUNTIME DESTINATION ${BIN_DIR})
55

Lesson2/src/main.cpp

Lines changed: 39 additions & 92 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
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

@@ -11,38 +12,6 @@
1112
const int SCREEN_WIDTH = 640;
1213
const 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

6429
int 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
}

Lesson3/CMakeLists.txt

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,6 @@
11
project(Lesson3)
22

3-
find_package(SDL2_image REQUIRED)
4-
include_directories(${SDL2_IMAGE_INCLUDE_DIR})
5-
63
add_executable(Lesson3 src/main.cpp)
7-
target_link_libraries(Lesson3 ${SDL2_LIBRARY} ${SDL2_IMAGE_LIBRARY})
4+
target_link_libraries(Lesson3 ${SDL2PP_LIBRARIES})
85
install(TARGETS Lesson3 RUNTIME DESTINATION ${BIN_DIR})
96

0 commit comments

Comments
 (0)