Pas de dépendances
#include <sdl/sdl.h> #ifdef WIN32 #pragma comment(lib,"sdl.lib") #pragma comment(lib,"sdlmain.lib") #endif void UpdateEvents(Sint32* mousex,Sint32* mousey,char boutons[8],char key[SDLK_LAST]) { SDL_Event event; while(SDL_PollEvent(&event)) { switch (event.type) { case SDL_KEYDOWN: key[event.key.keysym.sym]=1; break; case SDL_KEYUP: key[event.key.keysym.sym]=0; break; case SDL_MOUSEMOTION: *mousex=event.motion.x; *mousey=event.motion.y; break; case SDL_MOUSEBUTTONDOWN: boutons[event.button.button]=1; break; case SDL_MOUSEBUTTONUP: boutons[event.button.button]=0; break; } } } void SDL_PutPixel32(SDL_Surface *surface, int x, int y, Uint32 pixel) { Uint8 *p = (Uint8*)surface->pixels + y * surface->pitch + x * 4; *(Uint32*)p = pixel; } Uint32 SDL_GetPixel32(SDL_Surface *surface, int x, int y) { Uint8 *p = (Uint8*)surface->pixels + y * surface->pitch + x * 4; return *(Uint32*)p; } void Line(SDL_Surface* surf,int x1,int y1, int x2,int y2,Uint32 couleur) // Bresenham { int x,y; int Dx,Dy; int xincr,yincr; int erreur; int i; Dx = abs(x2-x1); Dy = abs(y2-y1); if(x1<x2) xincr = 1; else xincr = -1; if(y1<y2) yincr = 1; else yincr = -1; x = x1; y = y1; if(Dx>Dy) { erreur = Dx/2; for(i=0;i<Dx;i++) { x += xincr; erreur += Dy; if(erreur>Dx) { erreur -= Dx; y += yincr; } SDL_PutPixel32(surf,x, y,couleur); } } else { erreur = Dy/2; for(i=0;i<Dy;i++) { y += yincr; erreur += Dx; if(erreur>Dy) { erreur -= Dy; x += xincr; } SDL_PutPixel32(surf,x, y,couleur); } } SDL_PutPixel32(surf,x1,y1,couleur); SDL_PutPixel32(surf,x2,y2,couleur); } int main(int argc,char** argv) { Sint32 mousex = 0; Sint32 mousey = 0; char boutons[8] = {0}; char key[SDLK_LAST] = {0}; SDL_Surface* screen; int xs,ys; int en_train_d_ecrire = 0; SDL_Init(SDL_INIT_VIDEO); screen=SDL_SetVideoMode(800,600,32,SDL_SWSURFACE|SDL_DOUBLEBUF); SDL_ShowCursor(1); xs=ys=0; while(!key[SDLK_ESCAPE]) { UpdateEvents(&mousex,&mousey,boutons,key); if (SDL_MUSTLOCK(screen)) SDL_LockSurface(screen); if (boutons[SDL_BUTTON_LEFT]) { en_train_d_ecrire=1; } if (!boutons[SDL_BUTTON_LEFT]) { if (en_train_d_ecrire && (xs!=mousex || ys!=mousey)) Line(screen,xs,ys,mousex,mousey,0xFFFFFF); xs = mousex; ys = mousey; en_train_d_ecrire = 0; } if (boutons[SDL_BUTTON_RIGHT]) { SDL_SaveBMP(screen,"out.bmp"); } if (SDL_MUSTLOCK(screen)) SDL_UnlockSurface(screen); SDL_Flip(screen); } return 0; }
No explanations yet.