#include #include #include #include void Lock(SDL_Surface *screen) { if (SDL_MUSTLOCK(screen)) if (SDL_LockSurface(screen) < 0) { printf ("Couldn't lock surface: %s\n", SDL_GetError()); return; } } void Unlock(SDL_Surface *screen) { if (SDL_MUSTLOCK(screen)) SDL_UnlockSurface(screen); } Uint32 GetColor(SDL_Surface *screen, Uint8 R, Uint8 G, Uint8 B) { return SDL_MapRGB(screen->format, R, G, B); } void DrawPixel(SDL_Surface *screen, int x, int y, Uint8 R, Uint8 G, Uint8 B) { Lock(screen); switch (screen->format->BytesPerPixel) { case 1: { Uint8 *bufp; bufp = screen->pixels + y * screen->pitch + x; *bufp = GetColor(screen, R, G, B); } break; case 2: { Uint16 *bufp; bufp = screen->pixels + y * screen->pitch / 2 + x; *bufp = GetColor(screen, R, G, B); } break; case 3: { printf("24 bit mode is not supported\n"); exit(1); } break; case 4: { Uint32 *bufp; bufp = screen->pixels + y * screen->pitch / 4 + x; *bufp = GetColor(screen, R, G, B); } break; default: printf("Unknown Depth\n"); break; } Unlock(screen); SDL_UpdateRect(screen, x, y, 1, 1); } int main() { SDL_Surface *screen; int i, points = 50000; if(SDL_Init (SDL_INIT_VIDEO) != 0) { printf("Couldn't initialize SDL: %s\n", SDL_GetError()); return 1; } atexit(SDL_Quit); if ((screen = SDL_SetVideoMode(320, 200, 8, SDL_SWSURFACE)) == NULL) { printf("Couldn't set Video Mode: %s\n", SDL_GetError()); return 1; } /* for (i = 0; i < points; i++) { DrawPixel(screen, rand () % 320, rand () % 200, 0, 0, 255); DrawPixel(screen, rand () % 320, rand () % 200, 0, 255, 0); DrawPixel(screen, rand () % 320, rand () % 200, 255, 0, 0); DrawPixel(screen, rand () % 320, rand () % 200, 255,255,0); } */ for (i = 0; i < 320; i++) { int j; for (j = 0; j < 200; j++) DrawPixel(screen, i, j, 255, 255, 255); } SDL_Delay (1000); return 0; }