1 /* glschool.c, Copyright (c) 2005-2006 David C. Lambert <dcl@panix.com>
3 * Permission to use, copy, modify, distribute, and sell this software and its
4 * documentation for any purpose is hereby granted without fee, provided that
5 * the above copyright notice appear in all copies and that both that
6 * copyright notice and this permission notice appear in supporting
7 * documentation. No representations are made about the suitability of this
8 * software for any purpose. It is provided "as is" without express or
11 #include "xlockmore.h"
14 #define sws_opts xlockmore_opts
15 #define DEFAULTS "*delay: 20000 \n" \
16 "*showFPS: False \n" \
17 "*wireframe: False \n" \
19 #define refresh_glschool (0)
20 #define release_glschool (0)
21 #define glschool_handle_event (0)
24 #define countof(x) (sizeof((x))/sizeof((*x)))
28 static Bool DoDrawBBox;
29 static Bool DoDrawGoal;
30 static int GoalChgFreq;
34 static float AccLimit;
35 static float AvoidFact;
36 static float MatchFact;
37 static float TargetFact;
38 static float CenterFact;
39 static float MinRadius;
40 static float Momentum;
41 static float DistComp;
43 static XrmOptionDescRec opts[] = {
44 { "-nfish", ".nfish", XrmoptionSepArg, 0 },
45 { "-fog", ".fog", XrmoptionNoArg, "True" },
46 { "+fog", ".fog", XrmoptionNoArg, "False" },
47 { "-drawgoal", ".drawgoal", XrmoptionNoArg, "True" },
48 { "+drawgoal", ".drawgoal", XrmoptionNoArg, "False" },
49 { "-drawbbox", ".drawbbox", XrmoptionNoArg, "True" },
50 { "+drawbbox", ".drawbbox", XrmoptionNoArg, "False" },
51 { "-goalchgf", ".goalchgf", XrmoptionSepArg, 0 },
52 { "-maxvel", ".maxvel", XrmoptionSepArg, 0 },
53 { "-minvel", ".minvel", XrmoptionSepArg, 0 },
54 { "-acclimit", ".acclimit", XrmoptionSepArg, 0 },
55 { "-distexp", ".distexp", XrmoptionSepArg, 0 },
56 { "-avoidfact", ".avoidfact", XrmoptionSepArg, 0 },
57 { "-matchfact", ".matchfact", XrmoptionSepArg, 0 },
58 { "-centerfact",".centerfact", XrmoptionSepArg, 0 },
59 { "-targetfact",".targetfact", XrmoptionSepArg, 0 },
60 { "-minradius", ".minradius", XrmoptionSepArg, 0 },
61 { "-distcomp", ".distcomp", XrmoptionSepArg, 0 },
62 { "-momentum", ".momentum", XrmoptionSepArg, 0 },
65 static argtype vars[] = {
66 {&NFish, "nfish", "NFish", "100", t_Int},
67 {&DoFog, "fog", "DoFog", "False", t_Bool},
68 {&DoDrawBBox, "drawbbox", "DoDrawBBox", "True", t_Bool},
69 {&DoDrawGoal, "drawgoal", "DoDrawGoal", "False", t_Bool},
70 {&GoalChgFreq, "goalchgf", "GoalChgFreq", "50", t_Int},
71 {&MaxVel, "maxvel", "MaxVel", "7.0", t_Float},
72 {&MinVel, "minvel", "MinVel", "1.0", t_Float},
73 {&AccLimit, "acclimit", "AccLimit", "8.0", t_Float},
74 {&DistExp, "distexp", "DistExp", "2.2", t_Float},
75 {&AvoidFact, "avoidfact", "AvoidFact", "1.5", t_Float},
76 {&MatchFact, "matchfact", "MatchFact", "0.15", t_Float},
77 {&CenterFact, "centerfact", "CenterFact", "0.1", t_Float},
78 {&TargetFact, "targetfact", "TargetFact", "80", t_Float},
79 {&MinRadius, "minradius", "MinRadius", "30.0", t_Float},
80 {&Momentum, "momentum", "Momentum", "0.9", t_Float},
81 {&DistComp, "distcomp", "DistComp", "10.0", t_Float},
84 ENTRYPOINT ModeSpecOpt glschool_opts = {countof(opts), opts, countof(vars), vars, NULL};
98 } glschool_configuration;
100 static glschool_configuration *scs = NULL;
103 reshape_glschool(ModeInfo *mi, int width, int height)
105 Bool wire = MI_IS_WIREFRAME(mi);
106 double aspect = (double)width/(double)height;
107 glschool_configuration *sc = &scs[MI_SCREEN(mi)];
109 glXMakeCurrent(MI_DISPLAY(mi), MI_WINDOW(mi), *(sc->context));
110 if (sc->school != (School *)0) {
111 setBBox(sc->school, -aspect*160, aspect*160, -130, 130, -450, -50.0);
112 glDeleteLists(sc->bboxList, 1);
113 createBBoxList(&SCHOOL_BBOX(sc->school), &sc->bboxList, wire);
115 reshape(width, height);
119 init_glschool(ModeInfo *mi)
121 int width = MI_WIDTH(mi);
122 int height = MI_HEIGHT(mi);
123 Bool wire = MI_IS_WIREFRAME(mi);
124 glschool_configuration *sc;
127 scs = (glschool_configuration *)calloc(MI_NUM_SCREENS(mi), sizeof(glschool_configuration));
129 perror("init_glschool: ");
133 sc = &scs[MI_SCREEN(mi)];
135 sc->drawGoal = DoDrawGoal;
136 sc->drawBBox = DoDrawBBox;
139 sc->context = init_GL(mi);
140 sc->colors = (XColor *)calloc(sc->nColors, sizeof(XColor));
141 make_color_ramp(0, 0,
144 sc->colors, &sc->nColors,
147 sc->school = initSchool(NFish, AccLimit, MaxVel, MinVel, DistExp, Momentum,
148 MinRadius, AvoidFact, MatchFact, CenterFact, TargetFact,
150 if (sc->school == (School *)0) {
151 fprintf(stderr, "couldn't initialize TheSchool, exiting\n");
155 reshape_glschool(mi, width, height);
158 initFishes(sc->school);
159 createDrawLists(&SCHOOL_BBOX(sc->school), &sc->bboxList, &sc->goalList, &sc->fishList, wire);
160 computeAccelerations(sc->school);
164 draw_glschool(ModeInfo *mi)
166 Window window = MI_WINDOW(mi);
167 Display *dpy = MI_DISPLAY(mi);
168 glschool_configuration *sc = &scs[MI_SCREEN(mi)];
171 fprintf(stderr, "no context\n");
175 glXMakeCurrent(MI_DISPLAY(mi), MI_WINDOW(mi), *(sc->context));
177 if ((sc->goalCounter % GoalChgFreq) == 0)
182 sc->rotCounter = (sc->rotCounter%360);
184 applyMovements(sc->school);
185 drawSchool(sc->colors, sc->school, sc->bboxList, sc->goalList, sc->fishList, sc->rotCounter, sc->drawGoal, sc->drawBBox);
186 computeAccelerations(sc->school);
192 glXSwapBuffers(dpy, window);
195 XSCREENSAVER_MODULE("GLSchool", glschool)