Oolite
Loading...
Searching...
No Matches
OOTextureSprite.m
Go to the documentation of this file.
1/*
2
3OOTextureSprite.m
4
5Oolite
6Copyright (C) 2004-2013 Giles C Williams and contributors
7
8This program is free software; you can redistribute it and/or
9modify it under the terms of the GNU General Public License
10as published by the Free Software Foundation; either version 2
11of the License, or (at your option) any later version.
12
13This program is distributed in the hope that it will be useful,
14but WITHOUT ANY WARRANTY; without even the implied warranty of
15MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16GNU General Public License for more details.
17
18You should have received a copy of the GNU General Public License
19along with this program; if not, write to the Free Software
20Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
21MA 02110-1301, USA.
22
23*/
24
25#import "OOTextureSprite.h"
26#import "OOMacroOpenGL.h"
27#import "OOMaths.h"
28#import "OOTexture.h"
29
30@implementation OOTextureSprite
31
32- (id)initWithTexture:(OOTexture*)inTexture
33{
34 return [self initWithTexture:inTexture size:[inTexture originalDimensions]];
35}
36
37- (id)initWithTexture:(OOTexture*)inTexture size:(NSSize)spriteSize
38{
39 if (inTexture == nil) {
40 [self release];
41 return nil;
42 }
43
44 self = [super init];
45 if (self != nil) {
46 texture = [inTexture retain];
47 size = spriteSize;
48 }
49 return self;
50}
51
52- (void)dealloc
53{
54 [texture release];
55
56 [super dealloc];
57}
58
59- (NSSize)size
60{
61 return size;
62}
63
64- (void)blitToX:(float)x Y:(float)y Z:(float)z alpha:(float)a
65{
68
69 a = OOClamp_0_1_f(a);
70 OOGL(glEnable(GL_TEXTURE_2D));
71 OOGL(glColor4f(1.0, 1.0, 1.0, a));
72
73 // Note that the textured Quad is drawn ACW from the top left.
74
75 [texture apply];
76 OOGLBEGIN(GL_QUADS);
77 glTexCoord2f(0.0, 0.0);
78 glVertex3f(x, y + size.height, z);
79
80 glTexCoord2f(0.0, 1.0);
81 glVertex3f(x, y, z);
82
83 glTexCoord2f(1.0, 1.0);
84 glVertex3f(x + size.width, y, z);
85
86 glTexCoord2f(1.0, 0.0);
87 glVertex3f(x + size.width, y + size.height, z);
88 OOGLEND();
89
90 OOGL(glDisable(GL_TEXTURE_2D));
91
93}
94
95- (void)blitCentredToX:(float)x Y:(float)y Z:(float)z alpha:(float)a
96{
97 float xs = x - size.width / 2.0;
98 float ys = y - size.height / 2.0;
99 [self blitToX:xs Y:ys Z:z alpha:a];
100}
101
102- (void)blitBackgroundCentredToX:(float)x Y:(float)y Z:(float)z alpha:(float)a
103{
104 // Without distance, coriolis stations would be rendered behind the background image.
105 // Set an arbitrary value for distance, might not be sufficient for really huge ships.
106 float distance = 512.0f;
107
108 size.width *= distance;
109 size.height *= distance;
110 [self blitCentredToX:x Y:y Z:z * distance alpha:a];
111 size.width /= distance;
112 size.height /= distance;
113}
114
115@end
OOINLINE float OOClamp_0_1_f(float value) INLINE_CONST_FUNC
#define OO_ENTER_OPENGL()
#define OOGLBEGIN
Definition OOOpenGL.h:269
@ OPENGL_STATE_OVERLAY
Definition OOOpenGL.h:124
#define OOVerifyOpenGLState()
Definition OOOpenGL.h:133
#define OOSetOpenGLState(STATE)
Definition OOOpenGL.h:132
#define OOGL(statement)
Definition OOOpenGL.h:262
#define OOGLEND
Definition OOOpenGL.h:270
return nil
float y
float x
OOINLINE OOScalar distance(Vector v1, Vector v2) INLINE_CONST_FUNC
Definition OOVector.h:280
OOTexture * texture
void blitCentredToX:Y:Z:alpha:(float x,[Y] float y,[Z] float z,[alpha] float a)
void blitToX:Y:Z:alpha:(float x,[Y] float y,[Z] float z,[alpha] float a)
id initWithTexture:size:(OOTexture *texture,[size] NSSize spriteSize)
void apply()
Definition OOTexture.m:258
NSSize originalDimensions()
Definition OOTexture.m:298