Dark Bit Factory & Gravity

PROGRAMMING => C / C++ /C# => Topic started by: mike_g on June 22, 2007

Title: Plasma effect in SDL
Post by: mike_g on June 22, 2007
I'm learning how to use SDL at the moment. So far it has been quite fun. Heres a plasma effect you might recognize:
Code: [Select]
#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.
Title: Re: Plasma effect in SDL
Post by: Jim on June 26, 2007
How do you find SDL?  Does it make giant exe files?  All the things you're doing there can be done with PTC, but I know that SDL can do a lot more than open a screen and draw pixels :)

Jim
Title: Re: Plasma effect in SDL
Post by: Shockwave on June 26, 2007
Lol I remember that tutorial :)
As Jim said, how big is the exe with this Mike as I am also interested?
Title: Re: Plasma effect in SDL
Post by: relsoft on June 27, 2007
Exe ain't big DLL is. So if you're into SW TPTC or any static lib is better.
Title: Re: Plasma effect in SDL
Post by: ninogenio on June 27, 2007
in my messing around i would say sdl is good for games it comes with good timer functions and a key and window handling system also it has sprite blitting which is cool, but for demos you are best as rel said with a static lib.
Title: Re: Plasma effect in SDL
Post by: mike_g on July 01, 2007
Quote from: Jim
How do you find SDL?  Does it make giant exe files?
So far SDL has been quite easy to use and its good for stuff like 2d games. Like relsoft said the exe isent all that big, but the DLL is like 256K, so no good for tiny demos. I downloaded TinyPTC and will be having a play around with it soon.