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

ex1.c

00001 /*=========================================================================
00002 
00003 GFX EXAMPLE CODE - #1
00004         "pixel 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 #include <stdlib.h>
00022 #include "gfx.h"
00023 
00024 typedef struct {
00025         int x;
00026         int y;
00027         int z;
00028         u_int addr, last;
00029         u_char sub;
00030 } star_t;
00031 
00032 void star_randomize(star_t* st) {
00033         st->x = i2f((rand() & 63) - 32);
00034         st->y = i2f((rand() & 63) - 32);
00035         st->z = 64;
00036 }
00037 
00038 void star_move(star_t* st) {
00039         int x, y, z;
00040         z = (st->z -= 2);
00041         x = f2i(divfx(st->x, z)) + 128;
00042         y = f2i(divfx(st->y, z)) + 96;
00043         if (z < 1 || x < 0 || x >= MODE2_WIDTH || y < 0 || y >= MODE2_HEIGHT) {
00044                 star_randomize(st);
00045                 z = st->z;
00046                 x = f2i(divfx(st->x, z)) + 128;
00047                 y = f2i(divfx(st->y, z)) + 96;
00048         }
00049         st->last = st->addr;
00050         st->addr = map_pixel(x, y);
00051         st->sub  = map_subpixel(x);
00052 }
00053 
00054 #define MAX_STARS 16
00055 
00056 main() {
00057         u_char c;
00058         star_t *st, stars[MAX_STARS];
00059 
00060         set_color(15, 1, 1);
00061 
00062         // set video mode to screen 2
00063         set_mode(mode_2);
00064 
00065         // set whole screen to color white/black
00066         fill(MODE2_ATTR, 0xf1, MODE2_MAX);
00067 
00068         // initialize stars
00069         for (c=0; c < MAX_STARS; c++) {
00070                 st = stars + c;
00071                 star_randomize(st);
00072                 st->z = (rand() & 63) << 2;
00073                 st->addr = 0;
00074         }
00075 
00076         // animation loop
00077         while (!get_trigger(0)) {
00078                 // calculate star movement
00079                 for (st=stars, c=0; c < MAX_STARS; c++, st++)
00080                         star_move(st);
00081 
00082                 // erase previous stars
00083                 for (st=stars, c=0; c < MAX_STARS; c++, st++)
00084                         vpoke(st->last, 0);
00085 
00086                 // show new stars
00087                 for (st=stars, c=0; c < MAX_STARS; c++, st++)
00088                         vmerge(st->addr, st->sub);
00089         }
00090 
00091         set_mode(mode_0);
00092 }

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