From http://www.jwz.org/xscreensaver/xscreensaver-5.37.tar.gz
[xscreensaver] / hacks / glx / glschool.c
index ecaa6aadbdac9ffe42f8b688ee063c2efd325818..c8f6e2c1490ae1160bfc5cbb3c9549457f9f3d54 100644 (file)
 #undef countof
 #define countof(x) (sizeof((x))/sizeof((*x)))
 
+#define DEF_NFISH      "100"
+#define DEF_FOG                "False"
+#define DEF_DRAWBBOX   "True"
+#define DEF_DRAWGOAL   "False"
+#define DEF_GOALCHGF   "50"
+#define DEF_MAXVEL     "7.0"
+#define DEF_MINVEL     "1.0"
+#define DEF_ACCLIMIT   "8.0"
+#define DEF_DISTEXP    "2.2"
+#define DEF_AVOIDFACT  "1.5"
+#define DEF_MATCHFACT  "0.15"
+#define DEF_CENTERFACT "0.1"
+#define DEF_TARGETFACT "80"
+#define DEF_MINRADIUS  "30.0"
+#define DEF_MOMENTUM   "0.9"
+#define DEF_DISTCOMP   "10.0"
+
 static int                     NFish;
 static Bool                    DoFog;
 static Bool                    DoDrawBBox;
@@ -63,22 +80,22 @@ static XrmOptionDescRec opts[] = {
 };
 
 static argtype vars[] = {
-       {&NFish,                "nfish",                "NFish",                "100", t_Int},
-       {&DoFog,                "fog",                  "DoFog",                "False", t_Bool},
-       {&DoDrawBBox,   "drawbbox",             "DoDrawBBox",   "True", t_Bool},
-       {&DoDrawGoal,   "drawgoal",             "DoDrawGoal",   "False", t_Bool},
-       {&GoalChgFreq,  "goalchgf",             "GoalChgFreq",  "50",  t_Int},
-       {&MaxVel,               "maxvel",               "MaxVel",               "7.0",  t_Float},
-       {&MinVel,               "minvel",               "MinVel",               "1.0",  t_Float},
-       {&AccLimit,             "acclimit",             "AccLimit",             "8.0",  t_Float},
-       {&DistExp,              "distexp",              "DistExp",              "2.2",  t_Float},
-       {&AvoidFact,    "avoidfact",    "AvoidFact",    "1.5",  t_Float},
-       {&MatchFact,    "matchfact",    "MatchFact",    "0.15",  t_Float},
-       {&CenterFact,   "centerfact",   "CenterFact",   "0.1",  t_Float},
-       {&TargetFact,   "targetfact",   "TargetFact",   "80",  t_Float},
-       {&MinRadius,    "minradius",    "MinRadius",    "30.0",  t_Float},
-       {&Momentum,             "momentum",             "Momentum",             "0.9",  t_Float},
-       {&DistComp,             "distcomp",             "DistComp",             "10.0",  t_Float},
+       {&NFish,                "nfish",                "NFish",                DEF_NFISH, t_Int},
+       {&DoFog,                "fog",                  "DoFog",                DEF_FOG, t_Bool},
+       {&DoDrawBBox,   "drawbbox",             "DoDrawBBox",   DEF_DRAWBBOX, t_Bool},
+       {&DoDrawGoal,   "drawgoal",             "DoDrawGoal",   DEF_DRAWGOAL, t_Bool},
+       {&GoalChgFreq,  "goalchgf",             "GoalChgFreq",  DEF_GOALCHGF,  t_Int},
+       {&MaxVel,               "maxvel",               "MaxVel",               DEF_MAXVEL,  t_Float},
+       {&MinVel,               "minvel",               "MinVel",               DEF_MINVEL,     t_Float},
+       {&AccLimit,             "acclimit",             "AccLimit",             DEF_ACCLIMIT,  t_Float},
+       {&DistExp,              "distexp",              "DistExp",              DEF_DISTEXP,  t_Float},
+       {&AvoidFact,    "avoidfact",    "AvoidFact",    DEF_AVOIDFACT,  t_Float},
+       {&MatchFact,    "matchfact",    "MatchFact",    DEF_MATCHFACT,  t_Float},
+       {&CenterFact,   "centerfact",   "CenterFact",   DEF_CENTERFACT,  t_Float},
+       {&TargetFact,   "targetfact",   "TargetFact",   DEF_TARGETFACT,  t_Float},
+       {&MinRadius,    "minradius",    "MinRadius",    DEF_MINRADIUS,  t_Float},
+       {&Momentum,             "momentum",             "Momentum",             DEF_MOMENTUM,  t_Float},
+       {&DistComp,             "distcomp",             "DistComp",             DEF_DISTCOMP,  t_Float},
 };
 
 ENTRYPOINT ModeSpecOpt glschool_opts = {countof(opts), opts, countof(vars), vars, NULL};
@@ -92,6 +109,7 @@ typedef struct {
        GLuint          bboxList;
        GLuint          goalList;
        GLuint          fishList;
+        int            fish_polys, box_polys;
        XColor          *colors;
        School          *school;
        GLXContext      *context;
@@ -108,11 +126,12 @@ reshape_glschool(ModeInfo *mi, int width, int height)
 
        glXMakeCurrent(MI_DISPLAY(mi), MI_WINDOW(mi), *(sc->context));
        if (sc->school != (School *)0) {
-               setBBox(sc->school, -aspect*160, aspect*160, -130, 130, -450, -50.0);
+               glschool_setBBox(sc->school, -aspect*160, aspect*160, -130, 130, -450, -50.0);
                glDeleteLists(sc->bboxList, 1);
-               createBBoxList(&SCHOOL_BBOX(sc->school), &sc->bboxList, wire);
+                glschool_createBBoxList(&SCHOOL_BBOX(sc->school),
+                                        &sc->bboxList, wire);
        }
-       reshape(width, height);
+       glschool_reshape(width, height);
 }
 
 ENTRYPOINT void
@@ -123,13 +142,7 @@ init_glschool(ModeInfo *mi)
        Bool                                    wire = MI_IS_WIREFRAME(mi);
        glschool_configuration  *sc;
 
-       if (!scs) {
-               scs = (glschool_configuration *)calloc(MI_NUM_SCREENS(mi), sizeof(glschool_configuration));
-               if (!scs) {
-                       perror("init_glschool: ");
-                       exit(1);
-               }
-       }
+       MI_INIT (mi, scs, NULL);
        sc = &scs[MI_SCREEN(mi)];
 
        sc->drawGoal = DoDrawGoal;
@@ -138,13 +151,13 @@ init_glschool(ModeInfo *mi)
        sc->nColors = 360;
        sc->context = init_GL(mi);
        sc->colors = (XColor *)calloc(sc->nColors, sizeof(XColor));
-       make_color_ramp(0, 0,
+       make_color_ramp(0, 0, 0,
                                        0.0, 1.0, 1.0,
                                        359.0, 1.0, 1.0,
                                        sc->colors, &sc->nColors,
                                        False, 0, False);
 
-       sc->school = initSchool(NFish, AccLimit, MaxVel, MinVel, DistExp, Momentum,
+       sc->school = glschool_initSchool(NFish, AccLimit, MaxVel, MinVel, DistExp, Momentum,
                                                        MinRadius, AvoidFact, MatchFact, CenterFact, TargetFact,
                                                        DistComp);
        if (sc->school == (School *)0) {
@@ -154,10 +167,12 @@ init_glschool(ModeInfo *mi)
 
        reshape_glschool(mi, width, height);
 
-       initGLEnv(DoFog);
-       initFishes(sc->school);
-       createDrawLists(&SCHOOL_BBOX(sc->school), &sc->bboxList, &sc->goalList, &sc->fishList, wire);
-       computeAccelerations(sc->school);
+       glschool_initGLEnv(DoFog);
+       glschool_initFishes(sc->school);
+       glschool_createDrawLists(&SCHOOL_BBOX(sc->school), 
+                                 &sc->bboxList, &sc->goalList, &sc->fishList,
+                                 &sc->fish_polys, &sc->box_polys, wire);
+       glschool_computeAccelerations(sc->school);
 }
 
 ENTRYPOINT void
@@ -174,16 +189,22 @@ draw_glschool(ModeInfo *mi)
 
        glXMakeCurrent(MI_DISPLAY(mi), MI_WINDOW(mi), *(sc->context));
 
+        mi->polygon_count = 0;
+
        if ((sc->goalCounter % GoalChgFreq) == 0)
-               newGoal(sc->school);
+               glschool_newGoal(sc->school);
        sc->goalCounter++;
 
        sc->rotCounter++;
        sc->rotCounter = (sc->rotCounter%360);
 
-       applyMovements(sc->school);
-       drawSchool(sc->colors, sc->school, sc->bboxList, sc->goalList, sc->fishList, sc->rotCounter, sc->drawGoal, sc->drawBBox);
-       computeAccelerations(sc->school);
+       glschool_applyMovements(sc->school);
+       glschool_drawSchool(sc->colors, sc->school, sc->bboxList, 
+                            sc->goalList, sc->fishList, sc->rotCounter, 
+                              sc->drawGoal, sc->drawBBox, 
+                            sc->fish_polys, sc->box_polys,
+                            &mi->polygon_count);
+       glschool_computeAccelerations(sc->school);
 
        if (mi->fps_p)
                do_fps(mi);