Skip to content

Commit 60beb1e

Browse files
committed
Polish for the Lesson1 source code
1 parent 075676a commit 60beb1e

File tree

1 file changed

+17
-23
lines changed

1 file changed

+17
-23
lines changed

Lesson1/src/main.cpp

Lines changed: 17 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -15,61 +15,55 @@ int main(int argc, char** argv){
1515
return 1;
1616
}
1717

18-
//First we need to create a window to draw things in
19-
SDL_Window *win = nullptr;
20-
//Create a window with title "Hello World" at 100, 100 on the screen with w:640 h:480 and show it
21-
win = SDL_CreateWindow("Hello World!", 100, 100, 640, 480, SDL_WINDOW_SHOWN);
18+
//Now create a window with title "Hello World" at 100, 100 on the screen with w:640 h:480 and show it
19+
SDL_Window *win = SDL_CreateWindow("Hello World!", 100, 100, 640, 480, SDL_WINDOW_SHOWN);
2220
//Make sure creating our window went ok
2321
if (win == nullptr){
2422
std::cout << SDL_GetError() << std::endl;
2523
return 1;
2624
}
2725

28-
//Now we create our renderer
29-
SDL_Renderer *ren = nullptr;
30-
//Create a renderer that will draw to window, -1 specifies that we want to load whichever
26+
//Create a renderer that will draw to the window, -1 specifies that we want to load whichever
3127
//video driver supports the flags we're passing
32-
//Flags: SDL_RENDERER_ACCELERATED: We want to use hardware accelerated rendering, b/c it rocks!
28+
//Flags: SDL_RENDERER_ACCELERATED: We want to use hardware accelerated rendering
3329
//SDL_RENDERER_PRESENTVSYNC: We want the renderer's present function (update screen) to be
3430
//synchornized with the monitor's refresh rate
35-
ren = SDL_CreateRenderer(win, -1, SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC);
31+
SDL_Renderer *ren = SDL_CreateRenderer(win, -1, SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC);
3632
if (ren == nullptr){
3733
std::cout << SDL_GetError() << std::endl;
3834
return 1;
3935
}
4036

41-
//SDL 2.0 now uses textures to draw things, but without the SDL_image extension we still
42-
//must load the image as a bmp SDL_Surface and the convert it
43-
SDL_Surface *bmp = nullptr;
44-
bmp = SDL_LoadBMP("../res/Lesson1/hello.bmp");
37+
//SDL 2.0 now uses textures to draw things but SDL_LoadBMP returns a surface
38+
//this lets us choose when to upload or remove textures from the GPU
39+
SDL_Surface *bmp = SDL_LoadBMP("../res/Lesson1/hello.bmp");
4540
if (bmp == nullptr){
4641
std::cout << SDL_GetError() << std::endl;
4742
return 1;
4843
}
49-
//The texture we'll be drawing
50-
SDL_Texture *tex = nullptr;
51-
//Now convert it to a texture that is optimized for our renderer
52-
tex = SDL_CreateTextureFromSurface(ren, bmp);
53-
//Now we no longer need the image so we can free it
44+
45+
//To use a hardware accelerated texture for rendering we can create one from
46+
//the surface we loaded
47+
SDL_Texture *tex = SDL_CreateTextureFromSurface(ren, bmp);
48+
//We no longer need the surface
5449
SDL_FreeSurface(bmp);
5550

5651
//Now lets draw our image
5752
//First clear the renderer
5853
SDL_RenderClear(ren);
59-
//Now draw the texture
54+
//Draw the texture
6055
SDL_RenderCopy(ren, tex, NULL, NULL);
6156
//Update the screen
6257
SDL_RenderPresent(ren);
6358

64-
//Have the program wait for 2000ms
59+
//Have the program wait for 2000ms so we get a chance to see the screen
6560
SDL_Delay(2000);
6661

67-
//Destroy our stuff
62+
//Clean up our objects and quit
6863
SDL_DestroyTexture(tex);
6964
SDL_DestroyRenderer(ren);
7065
SDL_DestroyWindow(win);
71-
7266
SDL_Quit();
7367

7468
return 0;
75-
}
69+
}

0 commit comments

Comments
 (0)