@@ -15,61 +15,55 @@ int main(int argc, char** argv){
15
15
return 1 ;
16
16
}
17
17
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);
22
20
// Make sure creating our window went ok
23
21
if (win == nullptr ){
24
22
std::cout << SDL_GetError () << std::endl;
25
23
return 1 ;
26
24
}
27
25
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
31
27
// 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
33
29
// SDL_RENDERER_PRESENTVSYNC: We want the renderer's present function (update screen) to be
34
30
// 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);
36
32
if (ren == nullptr ){
37
33
std::cout << SDL_GetError () << std::endl;
38
34
return 1 ;
39
35
}
40
36
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" );
45
40
if (bmp == nullptr ){
46
41
std::cout << SDL_GetError () << std::endl;
47
42
return 1 ;
48
43
}
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
54
49
SDL_FreeSurface (bmp);
55
50
56
51
// Now lets draw our image
57
52
// First clear the renderer
58
53
SDL_RenderClear (ren);
59
- // Now draw the texture
54
+ // Draw the texture
60
55
SDL_RenderCopy (ren, tex, NULL , NULL );
61
56
// Update the screen
62
57
SDL_RenderPresent (ren);
63
58
64
- // Have the program wait for 2000ms
59
+ // Have the program wait for 2000ms so we get a chance to see the screen
65
60
SDL_Delay (2000 );
66
61
67
- // Destroy our stuff
62
+ // Clean up our objects and quit
68
63
SDL_DestroyTexture (tex);
69
64
SDL_DestroyRenderer (ren);
70
65
SDL_DestroyWindow (win);
71
-
72
66
SDL_Quit ();
73
67
74
68
return 0 ;
75
- }
69
+ }
0 commit comments