5 Replies - 369 Views - Last Post: 24 January 2013 - 11:31 AM Rate Topic: -----

#1 rnty  Icon User is offline

  • D.I.C Head

Reputation: 20
  • View blog
  • Posts: 145
  • Joined: 14-August 12

SDL Code Explanation

Posted 23 January 2013 - 11:53 AM

#include <stdio.h>
#include <SDL.h>

#define WIDTH 640
#define HEIGHT 480
#define BPP 4
#define DEPTH 32

void setpixel(SDL_Surface *screen, int x, int y, Uint8 r, Uint8 g, Uint8 B)/>
{
    Uint32 *pixmem32;
    Uint32 colour;  
 
    colour = SDL_MapRGB( screen->format, r, g, b );
  
    pixmem32 = (Uint32*) screen->pixels  + y + x;
    *pixmem32 = colour;
}


void DrawScreen(SDL_Surface* screen, int h)
{ 
    int x, y, ytimesw;
  
    if(SDL_MUSTLOCK(screen)) 
    {
        if(SDL_LockSurface(screen) < 0) return;
    }

    for(y = 0; y < screen->h; y++ ) 
    {
        ytimesw = y*screen->pitch/BPP;
        for( x = 0; x < screen->w; x++ ) 
        {
            setpixel(screen, x, ytimesw, (x*x)/256+3*y+h, (y*y)/256+x+h, h);
        }
    }

    if(SDL_MUSTLOCK(screen)) SDL_UnlockSurface(screen);
  
    SDL_Flip(screen); 
}


int main(int argc, char* argv[])
{
    SDL_Surface *screen;
    SDL_Event event;
  
    int keypress = 0;
    int h=0; 
  
    if (SDL_Init(SDL_INIT_VIDEO) < 0 ) return 1;
   
    if (!(screen = SDL_SetVideoMode(WIDTH, HEIGHT, DEPTH, SDL_FULLSCREEN|SDL_HWSURFACE)))
    {
        SDL_Quit();
        return 1;
    }
  
    while(!keypress) 
    {
         DrawScreen(screen,h++);
         while(SDL_PollEvent(&event)) 
         {      
              switch (event.type) 
              {
                  case SDL_QUIT:
	              keypress = 1;
	              break;
                  case SDL_KEYDOWN:
                       keypress = 1;
                       break;
              }
         }
    }

    SDL_Quit();
  
    return 0;
}



ytimesw = y*screen->pitch/BPP;
        for( x = 0; x < screen->w; x++ ) 
        {
            setpixel(screen, x, ytimesw, (x*x)/256+3*y+h, (y*y)/256+x+h, h);
        }



What does this portion of the code do?
Please explain it to me.I have just started learning SDL.
Thank you

Is This A Good Question/Topic? 0
  • +

Replies To: SDL Code Explanation

#2 anonymous26  Icon User is offline

  • D.I.C Lover

Reputation: 0
  • View blog
  • Posts: 3,638
  • Joined: 26-November 10

Re: SDL Code Explanation

Posted 23 January 2013 - 08:18 PM

Just a couple of points:

1. If you don't know what this code does why have you copied it?
2. Running the code will demo what it does.
Was This Post Helpful? 0
  • +
  • -

#3 rnty  Icon User is offline

  • D.I.C Head

Reputation: 20
  • View blog
  • Posts: 145
  • Joined: 14-August 12

Re: SDL Code Explanation

Posted 24 January 2013 - 12:32 AM

If you could just explain the small portion where values are being passed to the setpixel method,I would be really grateful.
I was reading a tutorial on drawing a screen and pixels on the screen,when I came across this.I understand the code except that portion.
Reading code makes you a better coder right Mr.Dean?
Thanks and Regards
Was This Post Helpful? 0
  • +
  • -

#4 anonymous26  Icon User is offline

  • D.I.C Lover

Reputation: 0
  • View blog
  • Posts: 3,638
  • Joined: 26-November 10

Re: SDL Code Explanation

Posted 24 January 2013 - 05:47 AM

If you have been around these forums long enough you should know that I only help those who help themselves. I've asked you to run the code and you've ignored me. Likewise will I ignore your request to explain setpixel().

It's so easy to work it out.
Was This Post Helpful? 0
  • +
  • -

#5 rnty  Icon User is offline

  • D.I.C Head

Reputation: 20
  • View blog
  • Posts: 145
  • Joined: 14-August 12

Re: SDL Code Explanation

Posted 24 January 2013 - 11:12 AM

I did infact run the code,download the sdl documentation and read the use each of the in-built functions,structures and symbolic constants,before posting.The setpixel() is used to set the value of a pixel at a particular coordinate.What is confusing is the logic,in simpler terms why
ytimesw = y*screen->pitch/BPP;
	        for( x = 0; x < screen->w; x++ )
	        {
	            setpixel(screen, x, ytimesw, (x*x)/256+3*y+h, (y*y)/256+x+h, h);
	        }


Thanks
Was This Post Helpful? 0
  • +
  • -

#6 anonymous26  Icon User is offline

  • D.I.C Lover

Reputation: 0
  • View blog
  • Posts: 3,638
  • Joined: 26-November 10

Re: SDL Code Explanation

Posted 24 January 2013 - 11:31 AM

Have you tried altering the values to see what happens? Try harder, think about it.

For the record, that is bad code. It uses magic numbers with no indication or explanation of the logic.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1