بسم الله الرحمن الرحيم ساقوم بإذن الله باستخدام SDL لعرض مثال بسيط لعمل animation ثنائي الأبعاد باستخدام لغة D. لن اتطرق الى تفاصيل اعداد و تحميل مكتبة SDL او طريقة استخدامها مع D, بل ساطرح الفكرة الرئيسية و اللتي اظن انها يمكن ان تطبق في C++ و حتى المكتبات الأخرى غير SDL. (يعني اذا اردت التطبيق فأنت مجبر ان تقوم بأكثر من مجرد copy-paste ) اولا, لا بد من عمل initialization للـ SDL و النافذة, و اعداد الـ program loop او الـ game loop.
import derelict.sdl.sdl;import derelict.sdl.image;void init(){ //derelict initialization/loading //(You don't need this if you're using C++) DerelictSDL.load(); DerelictSDLImage.load(); //sdl initialization SDL_Init( SDL_INIT_EVERYTHING ); //set some attributes //I think it will work even without these SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1); SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 16); SDL_GL_SetAttribute(SDL_GL_RED_SIZE, 8); SDL_GL_SetAttribute(SDL_GL_GREEN_SIZE, 8); SDL_GL_SetAttribute(SDL_GL_BLUE_SIZE, 8); SDL_GL_SetAttribute(SDL_GL_ALPHA_SIZE, 8); //create the window screen = SDL_SetVideoMode( 640, 480, 32, SDL_SWSURFACE ); //set the window title SDL_WM_SetCaption( "SDL Experiments!", null );}SDL_Surface * screen;void main(){ init(); SDL_Event event; mainLoop: while( true ) { if( SDL_PollEvent( &event ) ) { if( event.type == SDL_QUIT ) { break mainLoop; } } } }
void main(){ init(); loadImages(); SDL_Event event; mainLoop: while( true ) { drawImages(); if( SDL_PollEvent( &event ) ) { if( event.type == SDL_QUIT ) { break mainLoop; } } } }
SDL_Surface * image;void loadImages(){ SDL_Surface * temp = SDL_LoadBMP("images/kaitokid.bmp"); image = SDL_DisplayFormat( temp ); SDL_FreeSurface( temp );}
void drawImages(){ SDL_Rect offset; offset.x = 100; offset.y = 100; SDL_BlitSurface( image, null, screen, &offset ); SDL_Flip( screen );}
import derelict.sdl.sdl;import derelict.sdl.image;void init(){ //derelict initialization/loading //(You don't need this if you're using C++) DerelictSDL.load(); DerelictSDLImage.load(); //sdl initialization SDL_Init( SDL_INIT_EVERYTHING ); //set some attributes //I think it will work even without these SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1); SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 16); SDL_GL_SetAttribute(SDL_GL_RED_SIZE, 8); SDL_GL_SetAttribute(SDL_GL_GREEN_SIZE, 8); SDL_GL_SetAttribute(SDL_GL_BLUE_SIZE, 8); SDL_GL_SetAttribute(SDL_GL_ALPHA_SIZE, 8); //create the window screen = SDL_SetVideoMode( 640, 480, 32, SDL_SWSURFACE ); //set the window title SDL_WM_SetCaption( "SDL Experiments!", null );}SDL_Surface * screen;SDL_Surface * image;void loadImages(){ SDL_Surface * temp = SDL_LoadBMP("images/kaitokid.bmp"); image = SDL_DisplayFormat( temp ); SDL_FreeSurface( temp );}void drawImages(){ SDL_Rect offset; offset.x = 100; offset.y = 100; SDL_BlitSurface( image, null, screen, &offset ); SDL_Flip( screen );}void main(){ init(); loadImages(); SDL_Event event; mainLoop: while( true ) { drawImages(); if( SDL_PollEvent( &event ) ) { if( event.type == SDL_QUIT ) { break mainLoop; } } } }
SDL_Surface *[20] images;void loadImages(){ for(int i = 0; i < 20; i++ ) { char[] nextFrame; char[] num = toString(i); if( i < 10 ) { nextFrame = "000" ~ num ~ ".bmp"; } else { nextFrame = "00" ~ num ~ ".bmp"; } SDL_Surface * temp = SDL_LoadBMP("images/" ~ nextFrame); images[i] = SDL_DisplayFormat( temp ); SDL_FreeSurface( temp ); }}
int currentFrame = 0;int prevTime = 0;void drawImages(){ int ticks = SDL_GetTicks(); if( ticks - prevTime > 60 ) { currentFrame = (currentFrame + 1) % 20; prevTime = ticks; } SDL_Rect offset; offset.x = 100; offset.y = 100; SDL_BlitSurface( images[currentFrame], null, screen, &offset ); SDL_Flip( screen );}
import derelict.sdl.sdl;import derelict.sdl.image;import std.string;void init(){ //derelict initialization/loading //(You don't need this if you're using C++) DerelictSDL.load(); DerelictSDLImage.load(); //sdl initialization SDL_Init( SDL_INIT_EVERYTHING ); //set some attributes //I think it will work even without these SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1); SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 16); SDL_GL_SetAttribute(SDL_GL_RED_SIZE, 8); SDL_GL_SetAttribute(SDL_GL_GREEN_SIZE, 8); SDL_GL_SetAttribute(SDL_GL_BLUE_SIZE, 8); SDL_GL_SetAttribute(SDL_GL_ALPHA_SIZE, 8); //create the window screen = SDL_SetVideoMode( 640, 480, 32, SDL_SWSURFACE ); //set the window title SDL_WM_SetCaption( "SDL Experiments!", null );}SDL_Surface * screen;SDL_Surface *[20] images;void loadImages(){ for(int i = 0; i < 20; i++ ) { char[] nextFrame; char[] num = toString(i); if( i < 10 ) { nextFrame = "000" ~ num ~ ".bmp"; } else { nextFrame = "00" ~ num ~ ".bmp"; } SDL_Surface * temp = SDL_LoadBMP("images/" ~ nextFrame); images[i] = SDL_DisplayFormat( temp ); SDL_FreeSurface( temp ); }}int currentFrame = 0;int prevTime = 0;void drawImages(){ int ticks = SDL_GetTicks(); if( ticks - prevTime > 60 ) { currentFrame = (currentFrame + 1) % 20; prevTime = ticks; } SDL_Rect offset; offset.x = 100; offset.y = 100; SDL_BlitSurface( images[currentFrame], null, screen, &offset ); SDL_Flip( screen );}void main(){ init(); loadImages(); SDL_Event event; mainLoop: while( true ) { drawImages(); if( SDL_PollEvent( &event ) ) { if( event.type == SDL_QUIT ) { break mainLoop; } } } }