-(void)addSprite{ // This code works btw, image is drawn on screen and positioned accordingly
self.player = [[GLSprite alloc] initWithFile:@"logo2" fileFormat:@"png"];
self.player.positionX = 100;
[self.player render];
}
[NSTimer scheduledTimerWithTimeInterval:(NSTimeInterval) 0.05 target:self selector:@selector(update) userInfo:nil repeats:YES];
-(void)update{
myInt += 1;
if (myInt < 2) {
// Calls alloc once
self.player = [[GLSprite alloc] initWithFile:@"logo2" fileFormat:@"png"];
NSLog(@"ALLOC");
}
// Render, render, render that image
self.player.positionX += 10; // Increments x coordinate every 0.05s (will eventually revert xPos to be dependant on mouse)
[self.player render];
NSLog(@"RENDER");
}
-(id)initWithFile:(NSString *)imageName fileFormat: (NSString *) format{
if ((self = [super init])) {
[GLSprite getWindowSize];
NSDictionary *options = [NSDictionary dictionaryWithObjectsAndKeys:
[NSNumber numberWithBool:YES],
GLKTextureLoaderOriginBottomLeft,
nil];
NSString *imageURL, *dirImage;
imageURL = [NSString stringWithFormat:@"images/%@", imageName];
dirImage = [[NSBundle mainBundle] pathForResource:imageURL ofType:format];
NSError *error;
self.texture = [GLKTextureLoader textureWithContentsOfFile:dirImage options:options error:&error];
if (error) {
NSLog(@"Error loading texture from image: %@",error);
}
}
return self;
}
-(void)render{
x = (self.texture.width / window.x);
y = (self.texture.height / window.y);
self.initPositionX = (self.positionX / window.x) * 2;
self.initPositionY = (self.positionY / window.y) * 2;
xCoord[0] = 0 - x; yCoord[0] = 0 - y;
xCoord[1] = 0 + x; yCoord[1] = 0 - y;
xCoord[2] = 0 - x; yCoord[2] = 0 + y;
xCoord[3] = 0 + x; yCoord[3] = 0 + y;
NSLog(@"Render: %f", self.initPositionX);
glEnable(GL_TEXTURE_2D);
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glBegin(GL_TRIANGLE_STRIP);
glTexCoord2d(0.0, 0.0); glVertex2d(xCoord[0] + self.initPositionX, yCoord[0] + self.initPositionY);
glTexCoord2d(1.0, 0.0); glVertex2d(xCoord[1] + self.initPositionX, yCoord[1] + self.initPositionY);
glTexCoord2d(0.0, 1.0); glVertex2d(xCoord[2] + self.initPositionX, yCoord[2] + self.initPositionY);
glTexCoord2d(1.0, 1.0); glVertex2d(xCoord[3] + self.initPositionX, yCoord[3] + self.initPositionY);
glEnd();
}