Main Page | Data Structures | File List | Data Fields | Globals

ex9.c

00001 /*=========================================================================
00002 
00003 GFX EXAMPLE CODE - #9
00004         "sprite stars"
00005 
00006 Copyright (C) 2004  Rafael de Oliveira Jannone
00007 
00008 This example's source code is Public Domain.
00009 
00010 WARNING: The author makes no guarantees and holds no responsibility for 
00011 any damage, injury or loss that may result from the use of this source 
00012 code. USE IT AT YOUR OWN RISK.
00013 
00014 Contact the author:
00015         by e-mail : rafael AT jannone DOT org
00016         homepage  : http://jannone.org/gfxlib
00017         ICQ UIN   : 10115284
00018 
00019 =========================================================================*/
00020 
00021 // note: this is exactly the same as ex #1, but with sprites instead of 
00022 // pixels
00023 
00024 #include <stdlib.h>
00025 #include "gfx.h"
00026 
00027 extern u_char star[];
00028 
00029 typedef struct {
00030         int x;
00031         int y;
00032         int z;
00033         int sx, sy;
00034 } star_t;
00035 
00036 void star_randomize(star_t* st) {
00037         st->x = i2f((rand() & 63) - 32);
00038         st->y = i2f((rand() & 63) - 32);
00039         st->z = 64;
00040 }
00041 
00042 void star_move(star_t* st) {
00043         int x, y, z;
00044         z = (st->z -= 2);
00045         x = f2i(divfx(st->x, z)) + 128;
00046         y = f2i(divfx(st->y, z)) + 96;
00047         if (z < 1 || x < 0 || x >= MODE2_WIDTH || y < 0 || y >= MODE2_HEIGHT) {
00048                 star_randomize(st);
00049                 z = st->z;
00050                 x = f2i(divfx(st->x, z)) + 128;
00051                 y = f2i(divfx(st->y, z)) + 96;
00052         }
00053         st->sx = x;
00054         st->sy = y;
00055 }
00056 
00057 #define MAX_STARS 16
00058 
00059 main() {
00060         u_char c;
00061         star_t *st, stars[MAX_STARS];
00062 
00063         set_color(15, 1, 1);
00064 
00065         // set video mode to screen 2
00066         set_mode(mode_2);
00067 
00068         // set whole screen to color white/black
00069         fill(MODE2_ATTR, 0xf1, MODE2_MAX);
00070 
00071         // initialize stars
00072         for (c=0; c < MAX_STARS; c++) {
00073                 st = stars + c;
00074                 star_randomize(st);
00075                 st->z = (rand() & 63) << 2;
00076         }
00077 
00078         // define sprite
00079 
00080         set_sprite_mode(sprite_default);
00081         set_sprite_8(0, star);
00082 
00083         // animation loop
00084         while (!get_trigger(0)) {
00085                 // calculate star movement
00086                 for (st=stars, c=0; c < MAX_STARS; c++, st++)
00087                         star_move(st);
00088 
00089                 // show stars
00090                 for (st=stars, c=0; c < MAX_STARS; c++, st++)
00091                         put_sprite_8(c, st->sx, st->sy, 0, 15);
00092         }
00093 
00094         set_mode(mode_0);
00095 }
00096 
00097 #asm
00098 
00099 psect data
00100 
00101 global _star
00102 _star: 
00103         defb 128, 0, 0, 0, 0, 0, 0, 0
00104         defb 0, 0, 0, 0, 0, 0, 0, 0
00105         defb 0, 0, 0, 0, 0, 0, 0, 0
00106         defb 0, 0, 0, 0, 0, 0, 0, 0
00107 #endasm

Generated on Thu Mar 3 19:55:02 2005 for GFX lib by  doxygen 1.4.1