File: animation.c

package info (click to toggle)
wmweather+ 2.15-1.1
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 1,100 kB
  • ctags: 579
  • sloc: ansic: 5,822; sh: 1,294; makefile: 42
file content (138 lines) | stat: -rw-r--r-- 3,968 bytes parent folder | download | duplicates (6)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
#include "config.h"

/*  Copyright (C) 2002  Brad Jorsch <anomie@users.sourceforge.net>

    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program; if not, write to the Free Software
    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
*/

#include <stdio.h>

#include <X11/Xlib.h>
#include <X11/xpm.h>

#include "wmgeneral/wmgeneral-x11.h"
#include "wmgeneral/xpm_trans.h"

#include "wmweather+.h"
#include "animation.h"
#include "moon.h"
#include "font.h"

#define P 4   /* Percent to increment by */
#define X 4   /* Frames to wait before incrementing */
#define M (((100/P)+1)*X)

static int heights[5]={ 11, 12, 14, 14, 19 };

void SetAnimation(struct animation *a, int x, int y, int sky, int obs, int vis,
                  int frz, int snow, int rain, int tstorm, int svtstorm,
                  double moon){
    int i;

    a->changed=1;
    a->active=1;
    a->x=x;
    a->y=y;
    if(sky>=0 && sky<6) a->sky=sky*27;
    else a->sky=-1;
    if(vis<7 && obs>0 && vis>0 && obs<4){
        a->obs=obs*27-27;
        a->vis=256-236*(vis-1)/6;
    } else a->vis=0;

    a->items[0]=frz;
    a->items[1]=snow;
    a->items[2]=rain;
    a->items[3]=tstorm;
    a->items[4]=svtstorm;
    for(i=0; i<5; i++){
        if(a->items[i]==0) continue;
        if(a->items[i]%P) a->items[i]=a->items[i]/P+1;
        else a->items[i]=a->items[i]/P;
        a->items[i]*=X;
        if(a->items[i]!=0) a->changed=1;
    }
    a->ac=0;
    a->moon=moon;
}

void DoAnimation(struct animation *a){
    int i;
    int top, h;
    
    /* Turned off? */
    if(!a->active) return;

    /* Any parameters changed? If yes, draw */
    if(a->changed) goto doit;

    /* Nothing changed, quit if not animating */
    if(!a->do_animate) return;

    /* We are animating. If it's the first frame of a cycle, draw it */
    a->ac++; if(a->ac>=M){ a->ac=0; goto doit; }

    /* Not the first frame, see if anything changed. If so, draw it */
    for(i=0; i<5; i++){
        if(a->ac>=a->items[i] && a->ac<=a->items[i]+X) goto doit;
    }

    /* Just draw the counter... */
    if(a->show_counter) goto do_counter;

    /* Nothing to draw, quit */
    return;

doit:
    if(a->min_pct!=a->old_pct){
        if(a->min_pct%P) a->pct=(a->min_pct/P+1)*X;
        else a->pct=(a->min_pct/P)*X;
        a->old_pct=a->min_pct;
    }
    a->changed=0;

    if(!a->do_animate) a->ac=a->pct;
    copyPixmapArea(124, 18, 26, 31, a->x, a->y);
    if(a->sky!=-1){
        copySunMoon(a->x, a->y, a->moon);
        combineWithTrans(a->sky, 64, 26, 25, a->x, a->y);
    }
    if(a->vis>0) combineWithOpacity(a->obs, 89, 26, 25, a->x, a->y, a->vis);
    for(i=0; i<5; i++){
        if(a->items[i]==0 || a->ac>=a->items[i]+X) continue;
        h=heights[i];
        top=h;
        if(a->ac<X){
            h=h*a->ac/X;
        } else if(a->ac>=a->items[i] && a->ac<a->items[i]+X){
            h-=h*(a->ac%X)/X;
            top=h;
        }
        combineWithTrans(i*27, 129-top, 26, h, a->x, a->y+31-top);
    }
    if(a->show_counter){{
        char foo[5];
do_counter:
        if(!a->do_animate){
            snprintf(foo, 5, "%d%%", a->min_pct);
        } else {
            for(i=0; i<100 && a->ac>((i+P-1)/P)*X; i++);
            snprintf(foo, 5, "%d%%", i);
        }
        i=GetStringWidth(foo);
        copyPixmapArea(124, 18, i+2, 7, a->x+(26-i-2)/2, a->y+12);
        DrawString(a->x+(26-i)/2, a->y+13, foo, 2);
    }}
}