I'm learning how to use SDL at the moment. So far it has been quite fun. Heres a plasma effect you might recognize:
#include <SDL\SDL.h>
#include <math.h>
#define GRAPHICS_WIDTH 640
#define GRAPHICS_HEIGHT 480
#define DEG 0.017453292519943296
SDL_Surface *screen=NULL;
SDL_Rect src, dest;
SDL_Event event;
int genadd1 = 0, genadd2 = 0;
float sintable[2880];
float costable[2880];
Uint32 colourtable[1024];
void Plasma()
{
int loopx,loopy;// Loop variables.
int cv1,cv2,ca1,ca2;// Used in calculations.
int draw_point=0;
genadd1+=4;// Change these additions
genadd2+=3;// For different speed X+Y.
Uint32 *pix;
if(genadd1>360) genadd1=genadd1-360;
if(genadd2>360) genadd2=genadd2-360;
ca1 = 50+49*costable[genadd1];// You can play with these vars.
ca2 = 50+49*sintable[genadd2];// Strange things may happen though.
for(loopy=0; loopy<GRAPHICS_HEIGHT; loopy++)
{
cv1 = 205+(ca1*sintable[genadd2+loopy]);
for(loopx=0; loopx<GRAPHICS_WIDTH; loopx++)
{
cv2 = 400+(ca2*costable[genadd1+loopx]);
pix=screen->pixels+draw_point;
*pix=colourtable[cv1+cv2];
draw_point+=4;
}
}
}
void ColourGen()
{
int colourload;
int basered=0, basegreen=0, baseblue=255;
int redbounce=0, greenbounce=0, bluebounce=0;
for(colourload=0; colourload<1024; colourload++)
{
if(redbounce==0)
{
basered++;
if(basered>100)
{
basered=255;
redbounce=1;
}
}
if(greenbounce==0 && colourload>512)
{
basegreen+=2;
if(basegreen>255)
{
basegreen= 255;
greenbounce=1;
}
}
if(bluebounce==0)
{
baseblue--;
if(baseblue<0)
{
baseblue = 255;
bluebounce=1;
}
}
if(redbounce==1)
{
basered-=3;
if(basered<0)
{
basered = 0;
redbounce=0;
}
}
if(greenbounce==1)
{
basegreen-=1;
if(basegreen<0)
{
basegreen=0;
greenbounce=0;
}
}
if(bluebounce==1)
{
baseblue++;
if(baseblue>255)
{
baseblue = 0;
bluebounce=0;
}
}
colourtable[colourload]=((basered << 8)+basegreen << 8)+baseblue;
}
}
int main(int argc, char* argv[])
{
if(SDL_Init(SDL_INIT_VIDEO)!=0) return 1;
screen = SDL_SetVideoMode(GRAPHICS_WIDTH, GRAPHICS_HEIGHT, 32, 0);
if(screen == NULL) return 1;
int a;
for(a=0; a<2880; a++){sintable[a]=sin(a*DEG); costable[a]=cos(a*DEG);}
ColourGen();
while(1)
{
Plasma();
while(SDL_PollEvent(&event))
if(event.type==SDL_KEYDOWN)
return 0;
if(SDL_Flip(screen)==-1) return 1;
}
}
Its basically the example from the tutorial off the old DBF board rewritten in C. I havent been able to add a frame rate to this as SDL dosent come with built in text functions, tho I'm working on a crappy little bitmap font loader. Even without a frame timer I the difference in speed is certainly noticeable because it is possible to do the pixel writing within the plasma function, without having to send the coords into a another function then work out the memory address for each pixel. Anyway, maybe I'll have a go at drawing something on top of this.