1
\$\begingroup\$

I'm trying to write all of my game in C++ except for drawing and game loop timing. Those parts are going to be in Objective-C for iOS.

Right now, I have ViewController handling the update cycle, but I want to create a GameModel class that ViewController could update. I want GameModel to be in C++. I know how to integrate these two classes. My problem is how to have these two parts interact with the drawing and image loading.

GameModel will keep track of a list of children of type GameObject. These GameObjects update every frame, and then need to pass position and visibility data to whatever class or method will handle drawing.

I feel like I'm answering my own question now (talking it out helps) but would it be a good idea to put all of the visible game objects into an array at the end of the update method, return it, and use that to update graphics inside ViewController?

\$\endgroup\$

1 Answer 1

2
\$\begingroup\$

You want to try and avoid inter-mingling objective-c and c++ as much as possible, it will create code that is very hard to debug and even harder to understand by others potentially joining your project.

It is essential that you ground a sense of framework and work in a modular fashion in respect to how the platform wants to you to interact with objective-c modules and how your c++ modules fit with these.

If you're using c++, that indicates to me that you want to abstract away from objective-c and use c++ as your main language, then do so. Objective-c will only handle the low-tier ios system-specific features.

What purpose do you have for crossing the two languages over in such a complicated way, this speaks to me of bad software design and poor consideration of design methodologies.

I can only advise to keep it straight forward:

//Init In your ViewController.mm just invoke a main engine class(c++) such as:

-(void)viewDidLoad
{
/*unrelated ios-specific init code*/
[super viewDidLoad];

self.context = [[EAGLContext alloc] initWithAPI:kEAGLRenderingAPIOpenGLES2];

engine = new engine();//your engine class, that contains init, update and draw methods

if(!self.context ||  ![EAGLContext setCurrentContext:self.context])
{
    NSLog(@"Failed to create ES context");
}

GLKView * view = (GLKView *)self.view;
view.context = self.context;

GRect screenRect = [[UIScreen mainScreen] bounds];
CGFloat screenWidth = screenRect.size.width;
CGFloat screenHeight = screenRect.size.height;


view.drawableDepthFormat = GLKViewDrawableDepthFormat16;


if(engine != NULL)
    engine->init(screenWidth, screenHeight);
}

//Update

-(void)update {
engine->update();  
}

//Draw

- (void)glkView:(GLKView *)view drawInRect:(CGRect)rect
 {
   engine->draw((GLfloat)view.drawableWidth,(GLfloat)view.drawableHeight);
   [self.context presentRenderbuffer:GL_RENDERBUFFER];
 }

You will need to assert a solid architecture for communicating between your lower-level objective-c and higher-level c++ classes if you wish to use all the features the platform has to offer.

\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .