[关闭]
@OrionPaxxx 2017-04-29T13:48:21.000000Z 字数 5827 阅读 1252

SDL

SDL(original form lazy foo)

一些问题

1.ubuntu下,对于 lesson1:hello_SDL,如果使用命令行安装SDL编译程序时可能报错:no available video device,需去官网下载tar文件手动安装
2.ubuntu下,对于lesson6:Extension Libraries and Loading Other Image Formats,如果下载tar文件手动安装后,运行程序报错:没有找到共享库,试试用命令行下载安装

1.hello_SDL

  1. #include <SDL.h>
  2. #include <stdio.h>
  3. const int SCREEN_WIDTH = 640;//宽度
  4. const int SCREEN_HEIGHT = 480;//高度
  5. int main( int argc, char* args[] )
  6. {
  7. SDL_Window* window = NULL;//将要render的 window
  8. SDL_Surface* screenSurface = NULL;//window中包含的surface
  9. SDL_Init( SDL_INIT_VIDEO );//初始化SDL
  10. window = SDL_CreateWindow( "fuck you", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, SCREEN_WIDTH, SCREEN_HEIGHT, SDL_WINDOW_SHOWN );//创建windows,参数列表:窗口名称,窗口位置,窗口大小,flag(具体初看SDL wiki)
  11. screenSurface = SDL_GetWindowSurface( window );//获取window surface
  12. SDL_FillRect( screenSurface, NULL, SDL_MapRGB( screenSurface->format, 0xFF, 0xFF, 0xFF ) );//把surface填充为白色
  13. SDL_UpdateWindowSurface( window );//update the surface
  14. SDL_Delay( 2000 );
  15. SDL_DestroyWindow( window );
  16. SDL_Quit();//quit SDL system
  17. return 0;
  18. }

2.get an image on the screen

  1. #include<SDL.h>
  2. #include<cstdio>
  3. const int HEIGHT=460;
  4. const int WIDTH=640;
  5. void init();
  6. void loadimage();
  7. void close();
  8. SDL_Window* window=NULL;
  9. SDL_Surface* surface=NULL;
  10. SDL_Surface* image=NULL;
  11. void init()//初始化,创建窗口,获取surface
  12. {
  13. SDL_Init(SDL_INIT_VIDEO);
  14. window=SDL_CreateWindow("fuck you",SDL_WINDOWPOS_CENTERED,SDL_WINDOWPOS_CENTERED,WIDTH,HEIGHT,SDL_WINDOW_SHOWN);
  15. surface=SDL_GetWindowSurface(window);
  16. }
  17. void loadimage()//load .bmp image
  18. {
  19. image=SDL_LoadBMP("hello_world.bmp");
  20. }
  21. void close()
  22. {
  23. SDL_FreeSurface(image);//释放image的内存
  24. image=NULL;
  25. SDL_DestroyWindow(window);//同时也释放了surface
  26. window=NULL;
  27. SDL_Quit();
  28. }
  29. int main(int argc,char* argv[])
  30. {
  31. init();
  32. loadimage();
  33. SDL_BlitSurface(image,NULL,surface,NULL);//NULL:覆盖整个表面
  34. SDL_UpdateWindowSurface(window);//This is the function you use to reflect any changes to the surface on the screen.
  35. SDL_Delay(2000);
  36. close();
  37. return 0;
  38. }

3.event driven programme

  1. #include<SDL.h>
  2. #include<cstdio>
  3. const int HEIGHT=460;
  4. const int WIDTH=640;
  5. void init();
  6. void loadimage();
  7. void close();
  8. SDL_Window* window=NULL;
  9. SDL_Surface* surface=NULL;
  10. SDL_Surface* image=NULL;
  11. void init()
  12. {
  13. SDL_Init(SDL_INIT_VIDEO);
  14. window=SDL_CreateWindow("fuck you",SDL_WINDOWPOS_CENTERED,SDL_WINDOWPOS_CENTERED,WIDTH,HEIGHT,SDL_WINDOW_SHOWN);
  15. surface=SDL_GetWindowSurface(window);
  16. }
  17. void loadimage()
  18. {
  19. image=SDL_LoadBMP("hello_world.bmp");
  20. }
  21. void close()
  22. {
  23. SDL_FreeSurface(image);
  24. image=NULL;
  25. SDL_DestroyWindow(window);
  26. window=NULL;
  27. SDL_Quit();
  28. }
  29. int main(int argc,char* argv[])
  30. {
  31. init();
  32. loadimage();
  33. SDL_BlitSurface(image,NULL,surface,NULL);
  34. SDL_UpdateWindowSurface(window);
  35. SDL_Delay(2000);
  36. //===================================
  37. bool quit=false;
  38. SDL_Event e;
  39. while(!quit)
  40. {
  41. while(SDL_PollEvent(&e) != 0)/* poll for currently pending events,如果事件(Event队列中没有任何事件则返回0,如果有则返回true(Returns 1 if there is a pending event or 0 if there are none available. )*/
  42. {
  43. if(e.type == SDL_QUIT){
  44. quit=true;}
  45. }
  46. }
  47. //========================================================
  48. close();
  49. return 0;
  50. }

4.KEY PRESSES

  1. #include<iostream>
  2. #include<SDL.h>
  3. #include<string>
  4. const int WIDTH=640;
  5. const int HEIGHT=480;
  6. enum key_presses
  7. {
  8. KEY_PRESS_DEFUALT,
  9. KEY_PRESS_UP,
  10. KEY_PRESS_DOWN,
  11. KEY_PRESS_LEFT,
  12. KEY_PRESS_RIGHT,
  13. KEY_PRESS_TOTAL,
  14. };
  15. bool init();
  16. bool load_image();
  17. bool close();
  18. SDL_Window* window=NULL;
  19. SDL_Surface* window_surface=NULL;
  20. SDL_Surface* new_surface=NULL;
  21. SDL_Surface* all_surface[KEY_PRESS_TOTAL];
  22. bool init()
  23. {
  24. bool success=true;
  25. SDL_Init(SDL_INIT_VIDEO);
  26. window=SDL_CreateWindow("fuck you",SDL_WINDOWPOS_CENTERED,SDL_WINDOWPOS_CENTERED,WIDTH,HEIGHT,SDL_WINDOW_SHOWN);
  27. window_surface=SDL_GetWindowSurface(window);
  28. if(
  29. window==NULL ||
  30. window_surface==NULL){
  31. std::cout << "failed to initialize";
  32. success=false;}
  33. return success;
  34. }
  35. bool loadimage()
  36. {
  37. bool success=true;
  38. all_surface[KEY_PRESS_DEFUALT]=SDL_LoadBMP("press.bmp");
  39. all_surface[KEY_PRESS_UP]=SDL_LoadBMP("up.bmp");
  40. all_surface[KEY_PRESS_DOWN]=SDL_LoadBMP("down.bmp");
  41. all_surface[KEY_PRESS_LEFT]=SDL_LoadBMP("left.bmp");
  42. all_surface[KEY_PRESS_RIGHT]=SDL_LoadBMP("right.bmp");
  43. for(int i=0;i<=4;i++)
  44. {
  45. if(all_surface[i]==NULL)
  46. success=false;
  47. }
  48. return success;
  49. }
  50. bool close()
  51. {
  52. bool success=true;
  53. for(int i=0;i<+4;i++)
  54. {
  55. SDL_FreeSurface(all_surface[i]);
  56. all_surface[i]=NULL;
  57. }
  58. SDL_DestroyWindow(window);
  59. window=NULL;
  60. if(window != NULL)
  61. success=false;
  62. return true;
  63. }
  64. int main(int argc,char* argv[])
  65. {
  66. init();
  67. loadimage();
  68. SDL_Event e;
  69. bool quit=false;
  70. new_surface=all_surface[KEY_PRESS_DEFUALT];
  71. while(!quit)
  72. {
  73. while(SDL_PollEvent(&e) != 0)
  74. {
  75. if(e.type==SDL_QUIT){
  76. quit=true;}
  77. else if(e.type==SDL_KEYDOWN)
  78. {
  79. switch(e.key.keysym.sym)
  80. {
  81. case(SDLK_UP):
  82. new_surface=all_surface[KEY_PRESS_UP];
  83. break;
  84. case(SDLK_DOWN):
  85. new_surface=all_surface[KEY_PRESS_DOWN];
  86. break;
  87. case(SDLK_LEFT):
  88. new_surface=all_surface[KEY_PRESS_LEFT];
  89. break;
  90. case(SDLK_RIGHT):
  91. new_surface=all_surface[KEY_PRESS_RIGHT];
  92. break;
  93. default:
  94. new_surface=all_surface[KEY_PRESS_DEFUALT];
  95. break;
  96. }
  97. }
  98. }
  99. SDL_BlitSurface(new_surface,NULL,window_surface,NULL);
  100. SDL_UpdateWindowSurface(window);
  101. }
  102. close();
  103. return 0;
  104. }

5.Optimized Surface Loading and Soft Stretching

  1. optimizedSurface = SDL_ConvertSurface( loadedSurface, gScreenSurface->format, NULL );
  2. //参看SDL wiki和 lazy foo's productions
  1. //Apply the image stretched
  2. SDL_Rect stretchRect;
  3. stretchRect.x = 0;
  4. stretchRect.y = 0;
  5. stretchRect.w = SCREEN_WIDTH;
  6. stretchRect.h = SCREEN_HEIGHT;
  7. SDL_BlitScaled( gStretchedSurface, NULL, gScreenSurface, &stretchRect );
后来测试了一下直接SDL_BlitScaled(...,NULL,...,NULL)就够了,其他什么都不用

6.略

7.Texture Loading and Rendering

SDL_Texture : A structure that contains an efficient, driver-specific representation of pixel data. 
SDL_Renderer : A structure that contains a rendering state. 

 When we deal with SDL textures you need an SDL_Renderer to render it to the screen

 SDL_SetHint( SDL_HINT_RENDER_SCALE_QUALITY, "1" ) : 参看SDL wiki
 SDL_CreateRenderer( gWindow, -1, SDL_RENDERER_ACCELERATED ) : 参看SDL wiki
 SDL_SetRenderDrawColor( gRenderer, 0xFF, 0xFF, 0xFF, 0xFF ) : Use this function to set the color used for drawing operations (Rect, Line and Clear). 



 SDL_Surface* loadedSurface = IMG_Load( path.c_str() );
 newTexture = SDL_CreateTextureFromSurface( gRenderer, loadedSurface ) :
  1. //Clear screen
  2. SDL_RenderClear( gRenderer );
  3. //Render texture to screen
  4. SDL_RenderCopy( gRenderer, gTexture, NULL, NULL );
  5. //Update screen
  6. SDL_RenderPresent( gRenderer );
添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注