Back to the main site.
User Info
Welcome, Guest. Please login or register.
Did you miss your activation email?
Sun 05 September, 2010, 19:22:32

Login with username, password and session length
Search:     Advanced search
News Box
SMF - Just Installed!

Key Stats
106 Posts in 76 Topics by 14 Members
Latest Member: Mikha
Home Help Search Login Register
Propellor and Ratchet Games  |  P&R Related  |  News  |  C++ Headers and Includes « previous next »
Pages: [1] Go Down Reply Print
Author Topic: C++ Headers and Includes  (Read 184 times)
Ratchet
Prevents it from going backwards.
Administrator

Not currently in-game.

Sr. Member
*****

Shmuptitude 1
Offline Offline

Posts: 67


View Profile WWW
« on: Sun 09 May, 2010, 18:19:17 »
Reply with quote

Battery might post about Laika soon, as it's got it compiled for Windows. Unfortunately, he's left the Mac port in my care, so expect it, um, next year or so.

I've been learning about C++ headers, and including classes. When I say "learning about", I in fact mean "swearing at", because for someone who is used to Python it is a horrendously aggravating process.

1) Including a normal header more than once makes it die.
2) Including C++ files works but is looked down upon (and doing it more than once makes it die anyway).
3) Two way includes? TWO WAY INCLUDES? You're having a laugh!

After experimenting ("I could include this variable once across all my classes in it's own file!" "Hahaha no."), for a few days (Battery: "Have you tried the #ifndef trick?" Ratchet: "Yep." Battery: "Did it work?" Ratchet: "Nope.") I think I've finally stumbled across the magic formula.

1) You can't include -definitions- more than once.
2) You can't have global variables, unless they're defined as "extern const static immutable invulnerable impervious" or something. Naturally this means you can't change the value later, which would make it useful.
3) the #ifndef trick DOES work, but only most of the time. Not always. I'm not sure why so I'm leaving it alone, like a house of cards balanced on a single, bottom Ace.
4) Class definitions in the header, a REFERENCE to that class and class functions in the main file. This makes perfect sense when functions have to have their type defined in both, of course.
So here's the examples.
inventory.h:
Code:
#include <OpenGL/OpenGL.h>
#include <string>
using namespace std;

#ifndef INVENTLOL
#define INVENTLOL
class Inventory {
    public:
        bool active;
        GLfloat black[];
        
        Inventory(int glocation);
        void update(string action, int x, int y);
        void draw(int W_WIDTH, int W_HEIGHT);
};
#endif

inventory.cpp
Code:
#include <OpenGL/OpenGL.h>
#include <string>

#include "inventory.h"
using namespace std;

Inventory::Inventory(int glocation){
    active = true;
}

void  Inventory::update(string action, int x, int y){
   //do update
}

void  Inventory::draw(int W_WIDTH, int W_HEIGHT){
    glPushMatrix();
    GLfloat black[]      = {0,0,0};

etc...

It's probably bad practice to define black on every draw(), but I checked and it's using hte same memory address, so it's only CPU time that would be a problem. I'll probably be moving that around later.

Also, note that I have to pass the window's width and height to the draw() function, because I can't store them globally somewhere. I miss Python Sad

So, I hope that beginner coders like me find this, and I hope it helps them. And, I hope I haven't just got it wrong and mislead anyone reading it =D
« Last Edit: Sun 09 May, 2010, 18:21:48 by Ratchet » Logged
Pages: [1] Go Up Reply Print 
« previous next »
Jump to:  

Powered by SMF 1.1.9 | SMF © 2006-2009, Simple Machines LLC
Theme based on Serene Forest, Created by SoulGamerz.net | Buttons by Andrea