dynamic arrays, file reading/writing, etc.

Moderator
Posts: 370
Joined: 2006.08
Post: #1
I've been making a class for any 'shape'; the class's name is called, not surprisingly 'shape' Wink I've gotten the below code to work on Windows (it is written in C++), but I'm not quite sure how to duplicate it on the Mac using Objective-C.
Anyway, here's the header file (in C++):

Code:
#ifndef SHAPE_HPP
#define SHAPE_HPP

#include "point.hpp"
#include "texture.hpp"


class shape
{
      private:
              
      point* vertices;
      color reds;
      
      int maximumPoint;
      
      bool usingTexture;
      point tempPoint;
      CTexture theTexture;
      
      public:
            
      shape();
      ~shape();
      void addPoint(point newPoint);
      void draw(GLuint* textureStack);
      void loadObject(const char *sFileName);
};

#endif


Note that point is an object, as is color and CTexture.
Then I have the main code here:

Code:
#include "shape.hpp"

#include <fstream>

using namespace std;

shape::shape()
{
vertices = new point[10];
usingTexture = 0;
maximumPoint = 0;
reds.setR(1.0);
}

shape::~shape()
{
delete [] vertices;
}

void shape::addPoint(point newPoint)
{
vertices[maximumPoint] = newPoint;
maximumPoint = maximumPoint + 1;
}

void shape::draw(GLuint* textureStack)
{
        
         if(usingTexture == 1)
         {
                        
         glBegin(GL_POLYGON);
         for(int x = 0;x<maximumPoint;x++)
         {
         glBindTexture(GL_TEXTURE_2D, textureStack[(theTexture.GetID())]);
         //glColor3f(vertices[x].getColor().getR(),vertices[x].getColor().getG(),vertices[x].getColor().getB());
         glVertex3f(vertices[x].getX(),vertices[x].getY(),vertices[x].getZ());
         }
         glEnd();
        
         }
         else
         {
            
         glBegin(GL_POLYGON);
         for(int x = 0;x<maximumPoint;x++)
         {
         glColor3f(vertices[x].getColor().getR(),vertices[x].getColor().getG(),vertices[x].getColor().getB());
         glVertex3f(vertices[x].getX(),vertices[x].getY(),vertices[x].getZ());
         }
         glEnd();
        
         }
        
}

void shape::loadObject(const char *sFileName)
{
    
    int pointNumber;
    
    FILE * readFile;
    readFile = fopen(sFileName,"r+t");
    if(readFile)
    {
//!readFile.eof()
    fscanf(readFile, "%d", &pointNumber);
    pointNumber = pointNumber * 3;
    //readFile.getline(pointNumber, 16);
    
    double readPoint[pointNumber];
    
    for(int x=0; x<pointNumber; x++)
    {
    fscanf(readFile, "%lf", &readPoint[x]);
    }
    fclose(readFile);
    
    ofstream writeFile;
    writeFile.open("Objects/testWritingfile.txt");
    if(writeFile.is_open())
    {
    
    writeFile << (pointNumber/3) << "\n";
    
    for(int x=0; x<pointNumber; x=x+3)
    {
    tempPoint.setX(readPoint[x]);
    tempPoint.setY(readPoint[x+1]);
    tempPoint.setZ(readPoint[x+2]);
    tempPoint.setColor(reds);
    addPoint(tempPoint);
    }
    
    for(int x=0; x<pointNumber/3; x++)
    {
    writeFile << vertices[x].getX() << "\n";
    writeFile << vertices[x].getY() << "\n";
    writeFile << vertices[x].getZ() << "\n";
    }  
    
    writeFile.close();
    }
    }
    
}


What I'm basically having trouble with is this: I don't know how to make an array(or a dynamically allocated array, which is what I really need), I don't know how to read/write from files using Objective-C and the mac, and I'm not quite sure how Objective-C handles pointers differently from C++ (if, indeed, it handles them differently at all).
I don't really expect someone to come along and explain everything in one post, but any snippets of code, explanations, or links to places that would be helpful are appreciated Smile
Also: I don't want to sound selfish, but please don't post links to libraries that are designed to allow code to be run on Windows and the Mac without and re-writing; I've had bad experiences with that type of thing in the past and I prefer sticking to seperate code for the mac and Windows anyway.
Thanks in advance Smile
-wyrmmage
Quote this message in a reply
Luminary
Posts: 5,125
Joined: 2002.04
Post: #2
This code is already cross-platform; why not just use it?
Quote this message in a reply
Moderator
Posts: 370
Joined: 2006.08
Post: #3
it is? I did not think that the file loading code would work....all of my other code is written in Objective-C, will including a C++ file work well?
Quote this message in a reply
Moderator
Posts: 1,140
Joined: 2005.07
Post: #4
To expand on that: if you already know C++, why not just stick with it? Unless you need Mac UI elements, there's no real benefit with using Cocoa.

But regardless, in Cocoa you use NSArray and NSMutableArray, but since they're a class, they will be slower than real arrays. You could also use malloc. To read and write, you have a choice between the regular C functions and NSArchiver/NSKeyedArchiver. But I still recommend you simply stick with C++. Rasp (and use something like SDL for your windowing and events)
Quote this message in a reply
Moderator
Posts: 370
Joined: 2006.08
Post: #5
well, I don't really know why I'm programming in Objective-C, now that I think about it. I'm drawing using OpenGL, and the only code that I could find to set up a screen on the mac was written in Objective-C, so I guess that I assumed that that was what the common language on the mac was.
I don't suppose you could provide me with some code to make a window on the mac and would let me draw in OpenGL that is in C++?
-wyrmmage
Quote this message in a reply
Luminary
Posts: 5,125
Joined: 2002.04
Post: #6
Quote this message in a reply
Moderator
Posts: 1,140
Joined: 2005.07
Post: #7
I think OSC has some sample code on his website.

Even though you said that you don't want links to cross-platform libraries, I really think you should use SDL. It's very easy to learn and use, and uses the same interface for Mac, Windows, and Linux. Regardless of what you say, it's going to be a lot simpler to manage the same code base for both platforms. I'm working on a cross-platform program at work, and baring a few functions that have platform-dependent code determined by the pre-processor, I have my C++ code base working on Mac, Windows, and Linux, using OpenGL and SDL. The only platform-dependent stuff I have is a gettimeofday function implemented for Windows and some file handling. (since there's no stat function in Windows)

BTW, you might want to store your data in binary files. It's actually easier to use (just fread and fwrite your entire arrays) once you solve any endian issues. Plus, IO is a lot faster and more space efficient.
Quote this message in a reply
Post Reply 

Possibly Related Threads...
Thread: Author Replies: Views: Last Post
  Writing libraries vs writing apps. Najdorf 8 4,464 Nov 13, 2008 02:32 PM
Last Post: backslash
  Carbon - Reading / Writing Text Files dave05 10 3,999 Aug 1, 2006 05:42 PM
Last Post: dave05
  Reading/writing files in Carbon? ia3n_g 2 2,312 Jul 23, 2006 06:54 PM
Last Post: ia3n_g