5
\$\begingroup\$

I have been developing a cross-platform game based on open C libraries (mainly glfw) with a direct focus on Windows development. The beginning of my programming career taught me familiarity in Objective-C, however I've been using pure C for the past few years in embedded applications.

Now that I'm trying to return to proper desktop program development, I'm wondering if it's a good idea to transfer my Objective-C skills to Windows in order to tidy up my C code. My current way of programming is creating a struct for data that I need and then creating functions around those structs: struct Point {int x, int y}; and then float Point_calculateDistance(struct Point *obj1, struct Point *obj2). This approach to me seemed very redundant and too object-oriented to write all in C. It would make more sense to me to write an Objective-C class to do this where everything can be more encapsulated.

I prefer not to use any framework for my code to increase the portability. I'm currently using VS Code and MSYS2 (mingw64) with GCC 8.3.0. I threw together a working example that demonstrates how I attempt to build a simple class. Does my code look reasonable? Am I performing any bad habits (within reason considering that I'm not using a framework)? Does this currently leak memory? Let me know what you think.

#include <objc/objc.h>
#include <objc/runtime.h>
#include <objc/Object.h>
#include <stdio.h>
#include <stdlib.h>
#include <math.h>


@interface Point : Object {
    float x;
    float y;
}

@property float x;
@property float y;

-(id)initWithX: (float)xval AndY: (float)yval;
-(float)calculateDistance: (Point *)other;

@end

@implementation Point

@synthesize x;
@synthesize y;

+(id)alloc {
    id obj = malloc(class_getInstanceSize(self));
    object_setClass(obj, self);
    return obj;
}

-(void)dealloc {
    free(self);
}

-(id)init {
    x = 0.0;
    y = 0.0;
    return self;
}

-(id)initWithX: (float)xval AndY: (float)yval {
    x = xval;
    y = yval;
    return self;
}

-(float)calculateDistance: (Point *)other {
    return sqrt((x - other.x) * (x - other.x) + (y - other.y) * (y - other.y));
}

@end

int main(int argc, char **argv) {
    Point *point1 = [[Point alloc] init];
    Point *point2 = [[Point alloc] initWithX: 1.5 AndY: 1.5];

    printf("Distance is %f.\n", [point1 calculateDistance: point2]);

    [point1 dealloc];
    [point2 dealloc];
    return EXIT_SUCCESS;
}

\$\endgroup\$
  • \$\begingroup\$ where is the [super init]? \$\endgroup\$ – E.Coms Apr 1 '19 at 0:19
  • \$\begingroup\$ @E.Coms The base class Object only has the variable isa and the methods class and isEqual: so calling [super init] causes a seg. fault. \$\endgroup\$ – dylanweber Apr 1 '19 at 0:29
  • \$\begingroup\$ I use Xcode and not familiar with Object. I use NSObject and found it runs well at least in grammar. Without super. it just looks missing Root Object. \$\endgroup\$ – E.Coms Apr 1 '19 at 0:31
  • \$\begingroup\$ @E.Coms NSObject is a piece of Foundation.h which is written by Apple. Finding a class that does something similar would require writing my own or using a framework like GNUStep, both of which are overkill for my uses. \$\endgroup\$ – dylanweber Apr 1 '19 at 0:34
  • \$\begingroup\$ how about subclass of Point, will you still miss super? \$\endgroup\$ – E.Coms Apr 1 '19 at 0:40
5
\$\begingroup\$

It's an interesting idea to write Windows code in Objective-C. If you were using NeXTStep frameworks (or GNUStep) it would make a little more sense. I don't understand your desire to avoid frameworks, which tend to be force multipliers saving you time and effort. (And of course, you are using OpenGL and GLFW, so this seems like an arbitrary choice.) Regardless, given those constraints, here are my thoughts.

Language

In my opinion Objective-C is the wrong choice for this particular project. I say that as someone who makes a living programming largely in Objective-C. The problem, as I see it, is that you lose 2 important things that you would get from other languages:

  1. The ability to use objects or structs for doing your work and passing them directly to OpenGL/GLFW.
  2. The ability to use operator overloading.

OpenGL expects to receive geometry as an array of vertex attributes (or several different arrays – one for each attribute). That's going to be impossible using the class as you've written it. Each Objective-C object is its own entity on the heap. If you have an array of them, it's really just an array of pointers to the objects, which may or may not be contiguous in memory. You won't be able to just call glVertexAttribPointer() or other similar functions and pass a pointer to the array since the array will just be an array of other pointers. In C you'd have an array of Point structs either on the stack or the heap and could just pass the address of the first element to the above function. Likewise with something like a C++ std::vector<Point> you'd be able to pass the address of the first element (assuming Point had no v-table). In addition to making it harder to send the data to the GPU, it also makes processing the data on the CPU slower because you lose cache coherency when the data isn't contiguous.

You'll also want to do math on your Point objects. While you can write methods on your Objective-C class to add, subtract, etc., it's not as natural as using a language that allows you to overload operators. In particular, C++ and Swift both allow this and it makes writing graphics code feel much more natural.

Class

This class is rather thin. It doesn't do very much that's useful. What it does do, it appears to do correctly. It might be useful to swap the sqrt() call with a call to hypot(x,y) instead.

At a minimum, if you're doing 2D graphics, you'll probably want to add the following methods:

- (void)add:(Point*)p;
- (void)subtract:(Point*)p;
- (float)dotProduct:(Point*)p;
- (void)normalize;
- (void)multiplyScalar:(float)s;
- (void)multiplyVector:(Point*)p;
- (void)divideScalar:(float)s;
- (void)divideVector:(Point*)p;

And eventually, you'll probably want a Matrix class for things like scaling and rotation operations.

| improve this answer | |
\$\endgroup\$
  • \$\begingroup\$ Thank you for the comments, but perhaps I didn't make my intentions completely clear. I was really just using the Point example to demonstrate a functioning, compiling class more than something I would use to interact directly with OpenGL. If I were storing vertices, I would probably make a Point_3D struct with a float x, y, z and then perhaps implement it in an array as an instance variable of a larger object class (along with things like textures and shaders). \$\endgroup\$ – dylanweber Apr 1 '19 at 1:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.