[A89] Re: Variables in C


[Prev][Next][Index][Thread]

[A89] Re: Variables in C



Your program would be slower if you re-declared the frog sprite over and over in your ShowFrogSprite() function, because with each execution the variable is re-defined in the local variable stack and then removed upon function completion.  If you were to declare the variable as a global variable, it would be stored in the global variable stack for the remainder of the program.  This is by far faster because no time is lost during execution by moving the content of the variable to a stack repeatedly.  The problem, however, is that a global sprite would make your program bigger.  A way around this is declaring the variable as "static", or declaring all of your sprites locally and then moving them into a global struct (which is what I do whenever I have a lot of sprites to manage), or a struct type definition.  For example, you could do either this:

static unsigned short frog_black[] = {0b0000011111100000, // Use this sprite to
turn all of the black pixils on
                  0b0000100000010000,0b0001000000001000,0b0001000000001000,
                  0b0011000000001100,0b0111000000001110,0b0100100000010010,
                  0b0100010000100010,0b1110000000000111,0b1001000000001001,
                  0b1000011111100001,0b0111100000011110};
  static unsigned short frog_white[] = {0b1111100000011111, // Use this sprite to
turn all of the white pixils INSIDE frogger off
                   0b1111000000001111,0b1111000000001111,0b1111000000001111,
                   0b1100000000000011,0b1111000000001111,0b1100100000010011,

0b1100010000100011,0b1110000000000111,0b1111000000001111};

OR this (which I prefer):

struct game_sprites {
unsigned short *frog_black;
unsigned short *frog_white;
} FROG;

void load_sprites(void) {
unsigned short frog_black[] = {0b0000011111100000,
                  0b0000100000010000,0b0001000000001000,0b0001000000001000,
                  0b0011000000001100,0b0111000000001110,0b0100100000010010,
                  0b0100010000100010,0b1110000000000111,0b1001000000001001,
                  0b1000011111100001,0b0111100000011110};
unsigned short frog_white[] = {0b1111100000011111,
                   0b1111000000001111,0b1111000000001111,0b1111000000001111,
                   0b1100000000000011,0b1111000000001111,0b1100100000010011,
0b1100010000100011,0b1110000000000111,0b1111000000001111};

FROG.frog_black = frog_black;
FROG.frog_white = frog_white;
}

void ShowFrogSprite(int x, int y) {
  //use struct to extract sprite images for sprite() functions
  Sprite16(x,y,13,FROG.frog_black,LCD_MEM,SPRT_OR);
  Sprite16(x,y+1,10,FROG.frog_white,LCD_MEM,SPRT_AND);
}

Jude Nelson