Skip to content

Commit 5db39a2

Browse files
committed
Use try as main body to keep same identation as upstream
While here, report error via cout, also as in upstream
1 parent 8d2d934 commit 5db39a2

File tree

7 files changed

+253
-271
lines changed

7 files changed

+253
-271
lines changed

Lesson0/src/main.cpp

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,11 @@
55
/*
66
* Lesson 0: Test to make sure SDL is setup properly
77
*/
8-
int main(int, char**){
9-
try {
10-
SDL2pp::SDL(SDL_INIT_VIDEO);
11-
} catch (SDL2pp::Exception& e) {
12-
std::cout << e.GetSDLFunction() << " Error: " << e.GetSDLError() << std::endl;
13-
return 1;
14-
}
15-
8+
int main(int, char**) try {
9+
SDL2pp::SDL(SDL_INIT_VIDEO);
1610
return 0;
11+
} catch (SDL2pp::Exception& e) {
12+
std::cout << e.GetSDLFunction() << " Error: " << e.GetSDLError() << std::endl;
13+
return 1;
1714
}
1815

Lesson1/src/main.cpp

Lines changed: 36 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -6,45 +6,42 @@
66
/*
77
* Lesson 1: Hello World!
88
*/
9-
int main(int, char**){
10-
try {
11-
//First we need to start up SDL, and make sure it went ok
12-
SDL2pp::SDL(SDL_INIT_VIDEO);
13-
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);
16-
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);
23-
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);
28-
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-
}
9+
int main(int, char**) try {
10+
//First we need to start up SDL, and make sure it went ok
11+
SDL2pp::SDL(SDL_INIT_VIDEO);
12+
13+
//Now create a window with title "Hello World" at 100, 100 on the screen with w:640 h:480 and show it
14+
SDL2pp::Window win("Hello World!", 100, 100, 640, 480, SDL_WINDOW_SHOWN);
15+
16+
//Create a renderer that will draw to the window, -1 specifies that we want to load whichever
17+
//video driver supports the flags we're passing
18+
//Flags: SDL_RENDERER_ACCELERATED: We want to use hardware accelerated rendering
19+
//SDL_RENDERER_PRESENTVSYNC: We want the renderer's present function (update screen) to be
20+
//synchornized with the monitor's refresh rate
21+
SDL2pp::Renderer ren(win, -1, SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC);
22+
23+
//SDL 2.0 now uses textures to draw things but SDL_LoadBMP returns a surface
24+
//this lets us choose when to upload or remove textures from the GPU
25+
std::string imagePath = getResourcePath("Lesson1") + "hello.bmp";
26+
SDL2pp::Surface bmp(imagePath);
27+
28+
//To use a hardware accelerated texture for rendering we can create one from
29+
//the surface we loaded
30+
SDL2pp::Texture tex(ren, bmp);
31+
32+
//Now lets draw our image
33+
//First clear the renderer
34+
ren.Clear();
35+
//Draw the texture
36+
ren.Copy(tex, SDL2pp::NullOpt, SDL2pp::NullOpt);
37+
//Update the screen
38+
ren.Present();
39+
40+
//Have the program wait for 2000ms so we get a chance to see the screen
41+
SDL_Delay(2000);
4742

4843
return 0;
44+
} catch (SDL2pp::Exception& e) {
45+
std::cout << e.GetSDLFunction() << " Error: " << e.GetSDLError() << std::endl;
46+
return 1;
4947
}
50-

Lesson2/src/main.cpp

Lines changed: 33 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -26,50 +26,47 @@ void renderTexture(SDL2pp::Texture &tex, SDL2pp::Renderer &ren, int x, int y){
2626
ren.Copy(tex, SDL2pp::NullOpt, dst);
2727
}
2828

29-
int main(int, char**){
30-
try {
31-
//Start up SDL and make sure it went ok
32-
SDL2pp::SDL sdl(SDL_INIT_VIDEO);
29+
int main(int, char**) try {
30+
//Start up SDL and make sure it went ok
31+
SDL2pp::SDL sdl(SDL_INIT_VIDEO);
3332

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);
33+
//Setup our window and renderer
34+
SDL2pp::Window window("Lesson 2", 100, 100, SCREEN_WIDTH, SCREEN_HEIGHT, SDL_WINDOW_SHOWN);
35+
SDL2pp::Renderer renderer(window, -1, SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC);
3736

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");
37+
//The textures we'll be using
38+
const std::string resPath = getResourcePath("Lesson2");
39+
SDL2pp::Texture background(renderer, resPath + "background.bmp");
40+
SDL2pp::Texture image(renderer, resPath + "image.bmp");
4241

43-
//Clear the window
44-
renderer.Clear();
42+
//Clear the window
43+
renderer.Clear();
4544

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();
45+
//Get the width and height from the texture so we know how much to move x,y by
46+
//to tile it correctly
47+
int bW = background.GetWidth(), bH = background.GetHeight();
4948

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);
49+
//We want to tile our background so draw it 4 times
50+
renderTexture(background, renderer, 0, 0);
51+
renderTexture(background, renderer, bW, 0);
52+
renderTexture(background, renderer, 0, bH);
53+
renderTexture(background, renderer, bW, bH);
5554

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);
55+
//Draw our image in the center of the window
56+
//We need the foreground image's width to properly compute the position
57+
//of it's top left corner so that the image will be centered
58+
int iW = image.GetWidth(), iH = image.GetHeight();
59+
int x = SCREEN_WIDTH / 2 - iW / 2;
60+
int y = SCREEN_HEIGHT / 2 - iH / 2;
61+
renderTexture(image, renderer, x, y);
6362

64-
//Update the screen
65-
renderer.Present();
66-
SDL_Delay(2000);
67-
68-
} catch (SDL2pp::Exception& e) {
69-
std::cerr << e.GetSDLFunction() << " Error: " << e.GetSDLError() << std::endl;
70-
return 1;
71-
}
63+
//Update the screen
64+
renderer.Present();
65+
SDL_Delay(2000);
7266

7367
return 0;
68+
} catch (SDL2pp::Exception& e) {
69+
std::cout << e.GetSDLFunction() << " Error: " << e.GetSDLError() << std::endl;
70+
return 1;
7471
}
7572

Lesson3/src/main.cpp

Lines changed: 35 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -42,50 +42,48 @@ void renderTexture(SDL2pp::Texture &tex, SDL2pp::Renderer &ren, int x, int y){
4242
renderTexture(tex, ren, x, y, w, h);
4343
}
4444

45-
int main(int, char**){
46-
try {
47-
//Start up SDL and make sure it went ok
48-
SDL2pp::SDL sdl(SDL_INIT_VIDEO);
49-
SDL2pp::SDLImage sdlimage;
45+
int main(int, char**) try {
46+
//Start up SDL and make sure it went ok
47+
SDL2pp::SDL sdl(SDL_INIT_VIDEO);
48+
SDL2pp::SDLImage sdlimage;
5049

51-
//Setup our window and renderer
52-
SDL2pp::Window window("Lesson 3", 100, 100, SCREEN_WIDTH, SCREEN_HEIGHT, SDL_WINDOW_SHOWN);
53-
SDL2pp::Renderer renderer(window, -1, SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC);
50+
//Setup our window and renderer
51+
SDL2pp::Window window("Lesson 3", 100, 100, SCREEN_WIDTH, SCREEN_HEIGHT, SDL_WINDOW_SHOWN);
52+
SDL2pp::Renderer renderer(window, -1, SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC);
5453

55-
//The textures we'll be using
56-
const std::string resPath = getResourcePath("Lesson3");
57-
SDL2pp::Texture background(renderer, resPath + "background.png");
58-
SDL2pp::Texture image(renderer, resPath + "image.png");
54+
//The textures we'll be using
55+
const std::string resPath = getResourcePath("Lesson3");
56+
SDL2pp::Texture background(renderer, resPath + "background.png");
57+
SDL2pp::Texture image(renderer, resPath + "image.png");
5958

60-
//Clear the window
61-
renderer.Clear();
59+
//Clear the window
60+
renderer.Clear();
6261

63-
//Determine how many tiles we'll need to fill the screen
64-
int xTiles = SCREEN_WIDTH / TILE_SIZE;
65-
int yTiles = SCREEN_HEIGHT / TILE_SIZE;
62+
//Determine how many tiles we'll need to fill the screen
63+
int xTiles = SCREEN_WIDTH / TILE_SIZE;
64+
int yTiles = SCREEN_HEIGHT / TILE_SIZE;
6665

67-
//Draw the tiles by calculating their positions
68-
for (int i = 0; i < xTiles * yTiles; ++i){
69-
int x = i % xTiles;
70-
int y = i / xTiles;
71-
renderTexture(background, renderer, x * TILE_SIZE, y * TILE_SIZE, TILE_SIZE, TILE_SIZE);
72-
}
66+
//Draw the tiles by calculating their positions
67+
for (int i = 0; i < xTiles * yTiles; ++i){
68+
int x = i % xTiles;
69+
int y = i / xTiles;
70+
renderTexture(background, renderer, x * TILE_SIZE, y * TILE_SIZE, TILE_SIZE, TILE_SIZE);
71+
}
7372

74-
//Draw our image in the center of the window
75-
//We need the foreground image's width to properly compute the position
76-
//of it's top left corner so that the image will be centered
77-
int iW = image.GetWidth(), iH = image.GetHeight();
78-
int x = SCREEN_WIDTH / 2 - iW / 2;
79-
int y = SCREEN_HEIGHT / 2 - iH / 2;
80-
renderTexture(image, renderer, x, y);
73+
//Draw our image in the center of the window
74+
//We need the foreground image's width to properly compute the position
75+
//of it's top left corner so that the image will be centered
76+
int iW = image.GetWidth(), iH = image.GetHeight();
77+
int x = SCREEN_WIDTH / 2 - iW / 2;
78+
int y = SCREEN_HEIGHT / 2 - iH / 2;
79+
renderTexture(image, renderer, x, y);
8180

82-
//Update the screen
83-
renderer.Present();
84-
SDL_Delay(2000);
85-
} catch (SDL2pp::Exception& e) {
86-
std::cerr << e.GetSDLFunction() << " Error: " << e.GetSDLError() << std::endl;
87-
return 1;
88-
}
81+
//Update the screen
82+
renderer.Present();
83+
SDL_Delay(2000);
8984

9085
return 0;
86+
} catch (SDL2pp::Exception& e) {
87+
std::cout << e.GetSDLFunction() << " Error: " << e.GetSDLError() << std::endl;
88+
return 1;
9189
}

Lesson4/src/main.cpp

Lines changed: 44 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -40,59 +40,57 @@ void renderTexture(SDL2pp::Texture &tex, SDL2pp::Renderer &ren, int x, int y){
4040
renderTexture(tex, ren, x, y, w, h);
4141
}
4242

43-
int main(int, char**){
44-
try {
45-
//Start up SDL and make sure it went ok
46-
SDL2pp::SDL sdl(SDL_INIT_VIDEO);
47-
SDL2pp::SDLImage sdlimage;
43+
int main(int, char**) try {
44+
//Start up SDL and make sure it went ok
45+
SDL2pp::SDL sdl(SDL_INIT_VIDEO);
46+
SDL2pp::SDLImage sdlimage;
4847

49-
//Setup our window and renderer, this time let's put our window in the center
50-
//of the screen
51-
SDL2pp::Window window("Lesson 4", SDL_WINDOWPOS_CENTERED,
52-
SDL_WINDOWPOS_CENTERED, SCREEN_WIDTH, SCREEN_HEIGHT, SDL_WINDOW_SHOWN);
53-
SDL2pp::Renderer renderer(window, -1, SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC);
48+
//Setup our window and renderer, this time let's put our window in the center
49+
//of the screen
50+
SDL2pp::Window window("Lesson 4", SDL_WINDOWPOS_CENTERED,
51+
SDL_WINDOWPOS_CENTERED, SCREEN_WIDTH, SCREEN_HEIGHT, SDL_WINDOW_SHOWN);
52+
SDL2pp::Renderer renderer(window, -1, SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC);
5453

55-
//The texture we'll be using
56-
const std::string resPath = getResourcePath("Lesson4");
57-
SDL2pp::Texture image(renderer, resPath + "image.png");
54+
//The texture we'll be using
55+
const std::string resPath = getResourcePath("Lesson4");
56+
SDL2pp::Texture image(renderer, resPath + "image.png");
5857

59-
//Our texture size won't change, so we can get it here
60-
//instead of constantly allocating/deleting ints in the loop
61-
int iW = image.GetWidth(), iH = image.GetHeight();
62-
int x = SCREEN_WIDTH / 2 - iW / 2;
63-
int y = SCREEN_HEIGHT / 2 - iH / 2;
58+
//Our texture size won't change, so we can get it here
59+
//instead of constantly allocating/deleting ints in the loop
60+
int iW = image.GetWidth(), iH = image.GetHeight();
61+
int x = SCREEN_WIDTH / 2 - iW / 2;
62+
int y = SCREEN_HEIGHT / 2 - iH / 2;
6463

65-
//Our event structure
66-
SDL_Event e;
67-
//For tracking if we want to quit
68-
bool quit = false;
69-
while (!quit){
70-
//Read any events that occured, for now we'll just quit if any event occurs
71-
while (SDL_PollEvent(&e)){
72-
//If user closes the window
73-
if (e.type == SDL_QUIT){
74-
quit = true;
75-
}
76-
//If user presses any key
77-
if (e.type == SDL_KEYDOWN){
78-
quit = true;
79-
}
80-
//If user clicks the mouse
81-
if (e.type == SDL_MOUSEBUTTONDOWN){
82-
quit = true;
83-
}
64+
//Our event structure
65+
SDL_Event e;
66+
//For tracking if we want to quit
67+
bool quit = false;
68+
while (!quit){
69+
//Read any events that occured, for now we'll just quit if any event occurs
70+
while (SDL_PollEvent(&e)){
71+
//If user closes the window
72+
if (e.type == SDL_QUIT){
73+
quit = true;
74+
}
75+
//If user presses any key
76+
if (e.type == SDL_KEYDOWN){
77+
quit = true;
78+
}
79+
//If user clicks the mouse
80+
if (e.type == SDL_MOUSEBUTTONDOWN){
81+
quit = true;
8482
}
85-
//Rendering
86-
renderer.Clear();
87-
//Draw the image
88-
renderTexture(image, renderer, x, y);
89-
//Update the screen
90-
renderer.Present();
9183
}
92-
} catch (SDL2pp::Exception& e) {
93-
std::cerr << e.GetSDLFunction() << " Error: " << e.GetSDLError() << std::endl;
94-
return 1;
84+
//Rendering
85+
renderer.Clear();
86+
//Draw the image
87+
renderTexture(image, renderer, x, y);
88+
//Update the screen
89+
renderer.Present();
9590
}
9691

9792
return 0;
93+
} catch (SDL2pp::Exception& e) {
94+
std::cout << e.GetSDLFunction() << " Error: " << e.GetSDLError() << std::endl;
95+
return 1;
9896
}

0 commit comments

Comments
 (0)