Tille - I can see you, read those man pages!   Tille's Site

Full screen exercise

This is one way to do the exercise from Going fullscreen.

/* Program name: fullscreen2.c */
/* Author: tille */
/* SDL fullscreen test */

# include <SDL.h>
# include <stdio.h>
# include <stdlib.h>
# include <unistd.h>
# include <math.h>
# include <time.h>

/* This is a function that draws a pixel in the appropriate colour depth. */
/* See libsdl.org docs. */
void DrawPixel(SDL_Surface *screen, int x, int y, Uint8 R, Uint8 G, Uint8 B)
{
  /* Don't fail on attempts to draw outside the screen. */
  if (( x >= 640) || (x < 0)) return;
  if (( y >= 480) || (y < 0)) return;

    Uint32 color = SDL_MapRGB(screen->format, R, G, B);
 
    switch (screen->format->BytesPerPixel) {
        case 1: { /* Assuming 8-bpp */
            Uint8 *bufp;
 
            bufp = (Uint8 *)screen->pixels + y*screen->pitch + x;
            *bufp = color;
        }
        break;
 
        case 2: { /* Probably 15-bpp or 16-bpp */
            Uint16 *bufp;
 
            bufp = (Uint16 *)screen->pixels + y*screen->pitch/2 + x;
            *bufp = color;
        }
        break;
 
        case 3: { /* Slow 24-bpp mode, usually not 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 32-bpp */
            Uint32 *bufp;
 
            bufp = (Uint32 *)screen->pixels + y*screen->pitch/4 + x;
            *bufp = color;
        }
        break;
    }
}

/* Draw a circle with specified color, size and center.  Uses DrawPixel.*/
void DrawCircle(SDL_Surface *screen, int red, int green, int blue, int radius, int offsetx, int offsety) {
    if ( SDL_MUSTLOCK(screen) ) {
        if ( SDL_LockSurface(screen) < 0 ) {
            return;
        }
    }

  float i;
  for (i=0; i<2*M_PI; i+=0.01) {
    int x = offsetx + radius*cos(i);
    int y = offsety + radius*sin(i);
    DrawPixel(screen, x, y, red, green, blue );
    }
  if ( SDL_MUSTLOCK(screen) ) {
        SDL_UnlockSurface(screen);
    }
    int min_xcord, min_ycord, width, height;

/* Check that these coordinates are all within the region of the specified 
screen. */
    if ( offsetx - radius < 0 )
	min_xcord = 0;
	else 
	min_xcord = offsetx - radius;
    if ( offsety - radius < 0 )
	min_ycord = 0;
	else
	min_ycord = offsety - radius;
    if ( offsetx + radius > 639 )
	width = 639 - min_xcord;
	else
	width = offsetx + radius - min_xcord;
    if ( offsety + radius > 479 )
	height = 479 - min_ycord;
	else
	height = offsety + radius - min_ycord;

    SDL_UpdateRect(screen, min_xcord, min_ycord, width, height);
}

/* Begin random generator between 0 and specified value. */
int randomnumber(int randmax) {
  return rand()/(RAND_MAX/randmax);
}

/* Define a couple of variations on drawing circles.  Uses randomnumber. */
void RandomCircle(SDL_Surface *screen) {
  int offsetx = randomnumber(640);
  int offsety = randomnumber(480);
  int red = randomnumber(255);
  int green = randomnumber(255);
  int blue = randomnumber(255);
  int radius = 1;
  int switchcase = randomnumber(3);
  switch (switchcase) {
	int i;
	case 0: 
	      for (i=0; i < red; i++) {
        	DrawCircle(screen, i, green, blue, radius, offsetx, offsety);
        	radius++;
      		}
		break;
	case 1:
		for ( i = 0; i < green; i++) {
		DrawCircle(screen, red, i, blue, radius, offsetx, offsety);
	        radius++;
		}
		break;
	case 2: 
		for (i=0; i < blue; i++) {
                DrawCircle(screen, red, green, i, radius, offsetx, offsety);
                radius++;
                }
		break;
	}
}

/* What we really want to do. */
void MyEffect (SDL_Surface *screen) {
  RandomCircle(screen);
  sleep (3);
}

int main()
{
  SDL_Surface *screen;

  if ( SDL_Init(SDL_INIT_VIDEO) < 0 ) {
	fprintf(stderr, "Unable to init SDL: %s\n", SDL_GetError());
	exit(1);
  }
  atexit(SDL_Quit);

  screen = SDL_SetVideoMode(640, 480, 16, SDL_FULLSCREEN|SDL_HWSURFACE);
    if ( screen == NULL ) {
        fprintf(stderr, "Unable to set 640x480 video: %s\n", SDL_GetError());
        exit(1);
    }

  printf("Set window of 640x480 at %d bits per pixel mode\n", 
	screen->format->BitsPerPixel);

/* Don't show mouse pointer. */
  SDL_ShowCursor(SDL_DISABLE);

/* Set random seed. */
  srandom(time (0));

  SDL_Event event;
  
  int quit = 0;
  while ( !quit ) {

  MyEffect(screen);

/* Keep an eye on the keyboard and mouse. */
    while (SDL_PollEvent ( &event )) {
                
                switch( event.type ){
		case SDL_QUIT: 
			printf("Received Close Window request.\n");
                        quit = 1;
                        break;
		case SDL_KEYDOWN:
		if ( (event.key.keysym.sym == SDLK_ESCAPE) || (event.key.keysym.sym == SDLK_q)) {
			printf("Received keyboard quit request.\n");
			quit = 1;
			}
			break;
                    default:
                        break;
                }

            }

    }

  printf("Quitting SDL.\n");

  return(0);
}
Home
© 1995-2010 Machtelt Garrels - tille - Powered by vIm - Best viewed with your eyes - Validated by W3C - Last update 20100511