After only about an hour of playing with
SDL (
Simple DirectMedia Layer), I came up with the following interesting looking effect (note: it's slow as
molasses, probably from it being mostly example code... but,
ianacm (I am not a
Code Monkey), infact I don't even have any formal coding training. So, just deal with it)
#include <stdlib.h>
#include "SDL.h"
#include "SDL_endian.h" /* Used for the endian-dependent 24 bpp mode */
/* The following function is from the SDL introduction available on
libsdl.org, so blame them for the lack of comments */
void DrawPixel(SDL_Surface *screen, int x, int y, Uint8 R, Uint8 G, Uint8 B){
Uint32 color=SDL_MapRGB(screen->format,R,G,B);
if(SDL_MUSTLOCK(screen)){
if(SDL_LockSurface(screen) < 0){
return;
}
}
switch(screen->format->BytesPerPixel){
case 1:{ /* Assuming 8bpp */
Uint8 *bufp;
bufp=(Uint8 *)screen->pixels + y * screen->pitch + x;
*bufp=color;
}
break;
case 2:{ /* Probably 15 or 16 bpp */
Uint16 *bufp;
bufp=(Uint16 *)screen->pixels + y * screen->pitch / 2 + x;
*bufp=color;
}
break;
case 3:{ /* Slow 24 bpp mode, not usually used */
Uint8 *bufp;
bufp=(Uint8 *)screen->pixels + y * screen->pitch + x * 3;
if(SDL_BYTEORDER == SDL_LIL_ENDIAN){
bufp[0]=color;
bufp[1]=color>>8;
bufp[2]=color>>16;
} else {
bufp[2]=color;
bufp[1]=color>>8;
bufp[0]=color>>16;
}
}
break;
case 4:{ /* probably 32bpp */
Uint32 *bufp;
bufp=(Uint32 *)screen->pixels + y * screen->pitch / 4 + x;
*bufp=color;
}
break;
}
if(SDL_MUSTLOCK(screen)){
SDL_UnlockSurface(screen);
}
SDL_UpdateRect(screen,x,y,1,1);
}
int main(int argc, char *argv[]){
SDL_Surface *screen;
Uint8 R,G,B;
int x,y;
/* initialise SDL */
if(SDL_Init(SDL_INIT_VIDEO) < 0){
fprintf(stderr, "Unable to init SDL: %s\n", SDL_GetError());
exit(1);
}
atexit(SDL_Quit);
/* initialise screen */
screen=SDL_SetVideoMode(640,480,16,SDL_SWSURFACE);
if(screen==NULL){
fprintf(stderr,"Unable to set 640x480 video: %s\n",SDL_GetError());
exit(1);
}
/* main loop: draw one pixel to the screen at a time (which would be the
cause of the really slow performance, but gives a chance to see the effect
before it closes as soon as it finishes */
for(y=0;y<480;y++){
for(x=0;x<640;x++){
if((x)&&(y)){
R=(Uint8)(x/y)*x;
B=(Uint8)(x/y)*y;
G=(Uint8)(((y/x)*x)/2)+(((y/x)*y)/2);
} else {
R=0;B=0;
}
DrawPixel(screen,x,y,R,G,B);
}
}
return(0);
}
And, for those of who just can't live without a makefile (meaning you'll probably want to save the above as sdleffect.c):
all: sdleffect
sdleffect: sdleffect.o
gcc -o sdleffect `sdl-config --libs` -Wall sdleffect.o
sdleffect.o: sdleffect.c
gcc -o sdleffect.o -c `sdl-config --cflags` -Wall sdleffect.c
Or for those who just want a one line compile without calling make (pointed out by
Profoundly_Slack):
$ gcc `sdl-config --cflags --libs` -Wall sdleffect.c
There, now
have fun.
Also, if anyone wants to post any more interesting effects, go ahead