Trees in C

Save this code to your computer with an appropriate name (such as trees.c) and download the tree images to go with it. Compile it with something like:

gcc -o trees trees.c -lbr -lm

and enjoy!


#include <stdio.h>
#include <stdlib.h>
#include <SDL/SDL.h>
#include <SDL/SDL_mixer.h>
#include <SDL/SDL_image.h>
#include <brick.h>


/* Compile with:  gcc -o trees trees.c -lm -lbr */


/*
 * a few configuration options
 */
#define SCREEN_W 384
#define SCREEN_H 240

#define LAYER_CT 5
#define TREE_CT 4


/* a few auxiliary routines */
int my_rand(int min, int max) { return (random() % (max-min)) + min; }
int limit(int min, int val, int max) { return val < min ? min : ( (val > max) ? max : val ); }


int main() {

  int i,j;

  /* layer vars */
  int layers[LAYER_CT];

  /* the sprite variables - trees and mountain, and a temp sprite used in placing the scenery */
  struct sprite *sprites[TREE_CT+1];
  struct sprite *s;

  /* the file names of the trees */
  char *trees[] = {"tree1.png", "tree2.png", "tree3.png", "tree4.png"};
  int tree_amt[] = { 0, 12, 24, 60, 120 };

  /* the camera position */
  int cx, cy;

  /* a struct to contain inputs */
  struct input io;


  /* initialize brick and create the layer */
  init_brick();


  /*
   * let's load each png - mountain first, then trees
   */
  sprites[TREE_CT] = sprite_create();
  sprite_add_frame(sprites[TREE_CT], frame_convert(frame_from_disk("mountain1.png", NULL), FRAME_LT, NULL));

  for( i=0; i < TREE_CT; i++ ) {
    sprites[i] = sprite_create();
    sprite_add_frame(sprites[i], frame_convert(frame_from_disk(trees[i], NULL), FRAME_LT, NULL));
  }


  /*
   * let's make some layers - each layer after the mountain is drawn has
   * some trees
   */
  for( i=0; i < LAYER_CT; i++ ) {

    layers[i] = layer_add();

    /* place a mountain on layer 0 */
    if( !i ) {

      s = sprite_copy(sprites[TREE_CT]);
      sprite_set_position(s, 0, 80);
      list_add( layer_get_sprite_list(layers[i]), s);

    } else {

      /* all other layers get trees */
      for( j=0; j < tree_amt[i]; j++ ) {

        s = sprite_copy(sprites[random() & 3]);
        sprite_set_position(s, my_rand(20, i*600), my_rand(i*10+90, i*20+120));
        list_add( layer_get_sprite_list(layers[i]), s);

      }

    }

  }


  /* open the graphics display */
  graphics_open(GRAPHICS_ACCEL, SCREEN_W, SCREEN_H, 0, 2);


  /* initialize the camera position and enter the main loop */
  cx = 0;
  cy = 0;

  for(;;) {

    /* read the keyboard/joystick input */
    io_fetch(0, &io);

    /* update the camera position */
    cx += io.hat[0].horiz;
    cy += io.hat[0].vert;
    cx = limit(20, cx, 200);


    /*
     * and scroll the layers .. move the mountain at half-speed and
     * the rest at various speeds, to give a nice parallax effect
     */
    layer_set_camera(layers[0], cx/2, 0);
    for( i=1; i < LAYER_CT; i++ )
      layer_set_camera(layers[i], cx*(i+1), 0);


    /* display the frame and run the delay loop */
    render_display();
    delay(50);

    /* quit on escape keypress */
    if( io.esc || io_has_quit() )
      exit(0);

  }

}

 
demos/trees_in_c.txt · Last modified: 2010/05/09 12:16 by steve