This should be easy but it is driving me nuts. I am trying to pass a value from a ViewController to a View but it seems to be getting forgetting the value. More specifically, I am trying to get the accelerometer value in my ViewController and then pass this to the View. It appears I am successfully getting the input data correctly and sending it to the View but when I initiate a method within the view the data appears to be uninitialized. Any help would be appreciated.
Code details following. I have tried to remove code that is not relevant for ease of reading but please let me know if you need more info.
GameViewController.h
GameViewController.mm
GameView.h
GameView.mm
!!! OK This is where the problem is. When I enter drawView, the values
previously set are no longer set !!!!
Code details following. I have tried to remove code that is not relevant for ease of reading but please let me know if you need more info.
GameViewController.h
Code:
#import <UIKit/UIKit.h>
@class GameView;
@interface GameViewController : UIViewController <UIAccelerometerDelegate> {
GameView *game_view;
}
@property (nonatomic, retain) IBOutlet GameView *game;
GameViewController.mm
Code:
#import "GameView.h"
#import "GameViewController.h"
#include "Globals.h"
@implementation GameViewController
@synthesize game_view;
- (IBAction)closeGameView:(id)sender
{
[self.view removeFromSuperview];
}
- (void)accelerometer:(UIAccelerometer*)accelerometer didAccelerate:(UIAcceleration*)acceleration
{
game_view.acceleration = malloc(sizeof(acceleration));
// OK, this seems to work so far. I know that I am getting accelerometer
// readings and it does seem to be passing it to the GameView
[game_view setAcceleration:acceleration];
}
GameView.h
Code:
#import <UIKit/UIKit.h>
#import <OpenGLES/EAGL.h>
#import <OpenGLES/ES1/gl.h>
#import <OpenGLES/ES1/glext.h>
@interface GameView : UIView
{
@private
UIAcceleration *acceleration;
// The pixel dimensions of the backbuffer
GLint backingWidth;
GLint backingHeight;
EAGLContext *context;
// OpenGL names for the renderbuffer and framebuffers used to render to this view
GLuint viewRenderbuffer, viewFramebuffer;
// OpenGL name for the depth buffer that is attached to viewFramebuffer, if it exists (0 if it does not exist)
GLuint depthRenderbuffer;
BOOL animating;
BOOL displayLinkSupported;
NSInteger animationFrameInterval;
id displayLink;
NSTimer *animationTimer;
}
@property (readonly, nonatomic, getter=isAnimating) BOOL animating;
@property (nonatomic) NSInteger animationFrameInterval;
@property (nonatomic, retain) UIAcceleration *acceleration;
-(void)startAnimation;
-(void)stopAnimation;
-(void)drawView;
- (BOOL)createFramebuffer;
- (void)destroyFramebuffer;
- (void)setupView;
@end
GameView.mm
!!! OK This is where the problem is. When I enter drawView, the values
previously set are no longer set !!!!
Code:
#import <QuartzCore/QuartzCore.h>
#import <OpenGLES/EAGLDrawable.h>
#import "AsteroidsView.h"
#include "Asteroid.h"
#include "Camera.h"
#include "Game.h"
#include "Globals.h"
#include "MovableObject.h"
#include "World.h"
// MACROS
#define DEGREES_TO_RADIANS(__ANGLE__) ((__ANGLE__) / 180.0 * M_PI)
@implementation GameView
@synthesize animating;
@synthesize acceleration;
@dynamic animationFrameInterval;
// Updates the OpenGL view
- (void)drawView
{
static Game current_game;
static Camera main_view;
static World test_world;
// Make sure that you are drawing to the current context
[EAGLContext setCurrentContext:context];
glBindFramebufferOES(GL_FRAMEBUFFER_OES, viewFramebuffer);
glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
// Setup model view matrix
glLoadIdentity();
// Move back a bit
glTranslatef(0.0, 0.0, -3.0);
// Main game "loop" calls this method multiple times.
// Check if the game is active, if so, go through major game steps.
// Game not fully implemented yet
if(current_game.IsActive()){
// acceleration.x y, and z should have values but they do not!
current_game.Update(acceleration.x, acceleration.y, acceleration.z);
current_game.PlaySounds();
current_game.Draw();
}
glBindRenderbufferOES(GL_RENDERBUFFER_OES, viewRenderbuffer);
[context presentRenderbuffer:GL_RENDERBUFFER_OES];
}