@OrionPaxxx
2017-04-29T13:48:21.000000Z
字数 5827
阅读 1252
SDL
1.ubuntu下,对于 lesson1:hello_SDL,如果使用命令行安装SDL编译程序时可能报错:no available video device,需去官网下载tar文件手动安装
2.ubuntu下,对于lesson6:Extension Libraries and Loading Other Image Formats,如果下载tar文件手动安装后,运行程序报错:没有找到共享库,试试用命令行下载安装
#include <SDL.h>#include <stdio.h>const int SCREEN_WIDTH = 640;//宽度const int SCREEN_HEIGHT = 480;//高度int main( int argc, char* args[] ){SDL_Window* window = NULL;//将要render的 windowSDL_Surface* screenSurface = NULL;//window中包含的surfaceSDL_Init( SDL_INIT_VIDEO );//初始化SDLwindow = SDL_CreateWindow( "fuck you", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, SCREEN_WIDTH, SCREEN_HEIGHT, SDL_WINDOW_SHOWN );//创建windows,参数列表:窗口名称,窗口位置,窗口大小,flag(具体初看SDL wiki)screenSurface = SDL_GetWindowSurface( window );//获取window surfaceSDL_FillRect( screenSurface, NULL, SDL_MapRGB( screenSurface->format, 0xFF, 0xFF, 0xFF ) );//把surface填充为白色SDL_UpdateWindowSurface( window );//update the surfaceSDL_Delay( 2000 );SDL_DestroyWindow( window );SDL_Quit();//quit SDL systemreturn 0;}
#include<SDL.h>#include<cstdio>const int HEIGHT=460;const int WIDTH=640;void init();void loadimage();void close();SDL_Window* window=NULL;SDL_Surface* surface=NULL;SDL_Surface* image=NULL;void init()//初始化,创建窗口,获取surface{SDL_Init(SDL_INIT_VIDEO);window=SDL_CreateWindow("fuck you",SDL_WINDOWPOS_CENTERED,SDL_WINDOWPOS_CENTERED,WIDTH,HEIGHT,SDL_WINDOW_SHOWN);surface=SDL_GetWindowSurface(window);}void loadimage()//load .bmp image{image=SDL_LoadBMP("hello_world.bmp");}void close(){SDL_FreeSurface(image);//释放image的内存image=NULL;SDL_DestroyWindow(window);//同时也释放了surfacewindow=NULL;SDL_Quit();}int main(int argc,char* argv[]){init();loadimage();SDL_BlitSurface(image,NULL,surface,NULL);//NULL:覆盖整个表面SDL_UpdateWindowSurface(window);//This is the function you use to reflect any changes to the surface on the screen.SDL_Delay(2000);close();return 0;}
#include<SDL.h>#include<cstdio>const int HEIGHT=460;const int WIDTH=640;void init();void loadimage();void close();SDL_Window* window=NULL;SDL_Surface* surface=NULL;SDL_Surface* image=NULL;void init(){SDL_Init(SDL_INIT_VIDEO);window=SDL_CreateWindow("fuck you",SDL_WINDOWPOS_CENTERED,SDL_WINDOWPOS_CENTERED,WIDTH,HEIGHT,SDL_WINDOW_SHOWN);surface=SDL_GetWindowSurface(window);}void loadimage(){image=SDL_LoadBMP("hello_world.bmp");}void close(){SDL_FreeSurface(image);image=NULL;SDL_DestroyWindow(window);window=NULL;SDL_Quit();}int main(int argc,char* argv[]){init();loadimage();SDL_BlitSurface(image,NULL,surface,NULL);SDL_UpdateWindowSurface(window);SDL_Delay(2000);//===================================bool quit=false;SDL_Event e;while(!quit){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. )*/{if(e.type == SDL_QUIT){quit=true;}}}//========================================================close();return 0;}
#include<iostream>#include<SDL.h>#include<string>const int WIDTH=640;const int HEIGHT=480;enum key_presses{KEY_PRESS_DEFUALT,KEY_PRESS_UP,KEY_PRESS_DOWN,KEY_PRESS_LEFT,KEY_PRESS_RIGHT,KEY_PRESS_TOTAL,};bool init();bool load_image();bool close();SDL_Window* window=NULL;SDL_Surface* window_surface=NULL;SDL_Surface* new_surface=NULL;SDL_Surface* all_surface[KEY_PRESS_TOTAL];bool init(){bool success=true;SDL_Init(SDL_INIT_VIDEO);window=SDL_CreateWindow("fuck you",SDL_WINDOWPOS_CENTERED,SDL_WINDOWPOS_CENTERED,WIDTH,HEIGHT,SDL_WINDOW_SHOWN);window_surface=SDL_GetWindowSurface(window);if(window==NULL ||window_surface==NULL){std::cout << "failed to initialize";success=false;}return success;}bool loadimage(){bool success=true;all_surface[KEY_PRESS_DEFUALT]=SDL_LoadBMP("press.bmp");all_surface[KEY_PRESS_UP]=SDL_LoadBMP("up.bmp");all_surface[KEY_PRESS_DOWN]=SDL_LoadBMP("down.bmp");all_surface[KEY_PRESS_LEFT]=SDL_LoadBMP("left.bmp");all_surface[KEY_PRESS_RIGHT]=SDL_LoadBMP("right.bmp");for(int i=0;i<=4;i++){if(all_surface[i]==NULL)success=false;}return success;}bool close(){bool success=true;for(int i=0;i<+4;i++){SDL_FreeSurface(all_surface[i]);all_surface[i]=NULL;}SDL_DestroyWindow(window);window=NULL;if(window != NULL)success=false;return true;}int main(int argc,char* argv[]){init();loadimage();SDL_Event e;bool quit=false;new_surface=all_surface[KEY_PRESS_DEFUALT];while(!quit){while(SDL_PollEvent(&e) != 0){if(e.type==SDL_QUIT){quit=true;}else if(e.type==SDL_KEYDOWN){switch(e.key.keysym.sym){case(SDLK_UP):new_surface=all_surface[KEY_PRESS_UP];break;case(SDLK_DOWN):new_surface=all_surface[KEY_PRESS_DOWN];break;case(SDLK_LEFT):new_surface=all_surface[KEY_PRESS_LEFT];break;case(SDLK_RIGHT):new_surface=all_surface[KEY_PRESS_RIGHT];break;default:new_surface=all_surface[KEY_PRESS_DEFUALT];break;}}}SDL_BlitSurface(new_surface,NULL,window_surface,NULL);SDL_UpdateWindowSurface(window);}close();return 0;}
optimizedSurface = SDL_ConvertSurface( loadedSurface, gScreenSurface->format, NULL );//参看SDL wiki和 lazy foo's productions
//Apply the image stretchedSDL_Rect stretchRect;stretchRect.x = 0;stretchRect.y = 0;stretchRect.w = SCREEN_WIDTH;stretchRect.h = SCREEN_HEIGHT;SDL_BlitScaled( gStretchedSurface, NULL, gScreenSurface, &stretchRect );
后来测试了一下直接SDL_BlitScaled(...,NULL,...,NULL)就够了,其他什么都不用
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 ) :
//Clear screenSDL_RenderClear( gRenderer );//Render texture to screenSDL_RenderCopy( gRenderer, gTexture, NULL, NULL );//Update screenSDL_RenderPresent( gRenderer );