clean koko_carnaval
[exterlulz-kokogems.git] / src / ScoreBubble.m
blobc1ea34e2c3e14fde2f6822ad920f10167bdf86e3
1 //
2 //  ScoreBubble.m
3 //  jeweltoy
4 //
5 //  Created by Mike Wessler on Sat Jun 15 2002.
6 //
7 /*
8  This program is free software; you can redistribute it and/or
9  modify it under the terms of the GNU General Public License
10  as published by the Free Software Foundation; either version 2
11  of the License, or (at your option) any later version.
13  This program is distributed in the hope that it will be useful,
14  but WITHOUT ANY WARRANTY; without even the implied warranty of
15  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  GNU General Public License for more details.
18  You should have received a copy of the GNU General Public License
19  along with this program; if not, write to the Free Software
20  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
21  ----====----====----====----====----====----====----====----====----====---- */
23 #import "ScoreBubble.h"
25 #import "OpenGLSprite.h"
27 NSMutableDictionary *stringAttributes;
29 // Open GL Z value for scorebubbles
30 #define SCOREBUBBLE_SPRITE_Z    -0.30
32 @implementation ScoreBubble
34 @synthesize _value;
35 @synthesize _screenLocation;
36 @synthesize _animationCount;
38 + (ScoreBubble *)scoreWithValue:(int)value at:(NSPoint)location duration:(int)count
40   // FIXME: possible bug, is it [self alloc] or [[self class] alloc]???
41   ScoreBubble *scoreBubble = [[self alloc] initWithValue:value
42                                                       at:location
43                                                 duration:count];
44   return [scoreBubble autorelease];
47 -(id)initWithValue:(int)value at:(NSPoint)location duration:(int)count
49     NSString *str= [NSString stringWithFormat:@"%d", value];
50     NSSize strsize;
51   self = [super init];
52     if (self != nil) {
53         if (!stringAttributes) {
54             stringAttributes= [NSMutableDictionary dictionaryWithObjectsAndKeys:[NSFont fontWithName:@"ArialNarrow-Bold" size:18],
55                 NSFontAttributeName, [NSColor blackColor], NSForegroundColorAttributeName, NULL];
56             [stringAttributes retain];
57         }
58         strsize= [str sizeWithAttributes:stringAttributes];
59         strsize.width+= 3;
60         strsize.height+=1;
61         _value= value;
62         _screenLocation = location;
63         _screenLocation.x -= strsize.width / 2;
64         _screenLocation.y -= strsize.height / 2;
65         _animationCount= count;
66         image= [[NSImage alloc] initWithSize:strsize];
67         [image lockFocus];
68         [stringAttributes setObject:[NSColor blackColor] forKey:NSForegroundColorAttributeName];        
69         [str drawAtPoint:NSMakePoint(2,0) withAttributes:stringAttributes];
70         [stringAttributes setObject:[NSColor yellowColor] forKey:NSForegroundColorAttributeName];       
71         [str drawAtPoint:NSMakePoint(1,1) withAttributes:stringAttributes];
72         [image unlockFocus];
74         // Open GL
75         //
76         sprite = [[OpenGLSprite alloc] initWithImage:image
77                                        cropRectangle:NSMakeRect(0, 0, [image size].width, [image size].height)
78                                                 size:[image size]];
79         //
80     }
81     return self;
84 - (void)dealloc
86   [image release];
87   [sprite release];
88   
89   [super dealloc];
92 -(void)drawImage
94     float alpha = (float)_animationCount/20;
95     if (alpha>1) {
96         alpha= 1;
97     }
98   
99     [image compositeToPoint:_screenLocation
100                   operation:NSCompositeSourceOver
101                    fraction:alpha];
104 -(void)drawSprite
106     float alpha= (float)_animationCount/20;
107     if (alpha>1) {
108         alpha= 1;
109     }
110     [sprite blitToX:_screenLocation.x
111                   Y:_screenLocation.y
112                   Z:SCOREBUBBLE_SPRITE_Z
113               Alpha:alpha];
116 -(int)animate
118   if (_animationCount > 0) {
119     _screenLocation.y++;
120     _animationCount--;
121   }
122   
123   return _animationCount;
126 -(NSImage *)image
128     return image;
131 @end