Sunday, July 10, 2011

Color fills in SDL

Color fills are typically only used to clear the screen or a large rectangular portion of the screen. They are still important, nonetheless. If you want to go deep, you can set individual pixels. If you can set a single pixel, you can do anything graphics-wise, such as drawing lines, circles, ellipses, polygons, and whatever else you can imagine. SDL has no functions for drawing these primitives, so if you want them you have to implement them yourself. The ability to retrieve the value of individual pixels goes hand-in-hand with setting them.

 
SDL_FillRect

int SDL_FillRect(SDL_Surface *dst, SDL_Rect *dstrect, Uint32 color);

This function returns an int. If successful, the value returned will be 0. If it fails, the return value will be -1.
There are three parameters. The first (dst) is a pointer to an SDL_Surface on which you are drawing the filled rectangle. The second (dstrect) is a pointer to an SDL_Rect that describes the rectangular area that you want to fill. The third (color) is a Uint32 that represents the color with which you want to fill the rectangle.


SDL_MapRGB

Uint32 SDL_MapRGB(SDL_PixelFormat *fmt, Uint8 r, Uint8 g, Uint8 b);

This function takes four parameters. fmt is a pointer to an SDL_PixelFormat for the surface to which you are mapping a color. The r, g, and b parameters are (naturally) the red, green, and blue components of that color. The value returned by this function is the closest approximation of the color specified in that particular pixel format.

SDL_UpdateRect

SDL_UpdateRect is one of the way to update the SDL screen.

void SDL_UpdateRect(SDL_Surface *screen, Sint32 x, Sint32 y, Sint32 w, Sint32 h);

In SDL_UpdateRect, there are five parameters. First is a pointer to the surface you want to update (screen), followed by x, y, w, and h, which describe the single rectangle you want to update. If these four values are 0, the entire surface is updated.

uint8
An unsigned 8-bit integer quantity. Can represent values between  0 and 255, inclusive.
uint32
An unsigned 32-bit integer quantity. Can represent values between  0 and 4294967295, inclusive.

Here we are going to write a  program that will randomly draw filled rectangles of random colors to a window.


#include <sdl/sdl.h>

const int SCREEN_WIDTH=640;
const int SCREEN_HEIGHT=480;
SDL_Surface* screen = NULL;
SDL_Event Event;
SDL_Rect box;
Uint8 g_Red, g_Green, g_Blue;
Uint32 g_Color;
int main(int argc, char* argv[])
{
SDL_Init(SDL_INIT_VIDEO);
screen = SDL_SetVideoMode(SCREEN_WIDTH,SCREEN_HEIGHT,0,SDL_SWSURFACE);
for(;;)
{
if(SDL_PollEvent(&Event)==0)
{
g_Red=rand()%256;
g_Green=rand()%256;
g_Blue=rand()%256;
g_Color=SDL_MapRGB(screen->format,g_Red,g_Green,g_Blue);
box.x=rand()%SCREEN_WIDTH;
box.y=rand()%SCREEN_HEIGHT;
box.w=rand()%(SCREEN_WIDTH-box.x);
box.h=rand()%(SCREEN_HEIGHT-box.y);
SDL_FillRect(screen,&box,g_Color);
SDL_UpdateRect(screen ,0,0,0,0);
}
else
{
if(Event.type==SDL_QUIT) break;
}
}
SDL_Quit();
return(0);
}


The source code is available at:

No comments:

Post a Comment