How should i use functions and header files? Programming logic question.

This is a discussion on How should i use functions and header files? Programming logic question. within the C++ Programming forums, part of the General Programming Boards category; Hi everyone... Since i cannot delete the old thread, and it's a little bit of a different question related, i ...

  1. #1
    Novice programmer newn's Avatar
    Join Date
    Aug 2010
    Posts
    59

    How should i use functions and header files? Programming logic question.

    Hi everyone... Since i cannot delete the old thread, and it's a little bit of a different question related, i wanted to create a new one.
    Anyway, someone's said, that i shouldn't use functions as I'm using them. I wanted to put every bit of code into different. Like game loop with all the logic calculations into one header file and function, menu and it's logic into another, etc.

    But now I'm a little bit confused, as i was told, that i shouldn't put game logic into header files, i should put it into the main function. So how should i use functions at programming games at all? Header files?

  2. #2
    Sweet
    Join Date
    Aug 2002
    Location
    Tucson, Arizona
    Posts
    1,807
    You were on the right track.

    Normally you put a header and a cpp file together.

    So if you have Gamelogic.h you have Gamelogic.cpp

    Gamelogic.h has all the declarations
    Gamelogic.cpp has all the implementations
    Woop?

  3. #3

  4. #4
    Novice programmer newn's Avatar
    Join Date
    Aug 2010
    Posts
    59
    Aha... So basically... Logic goes into separate .cpp files, and declarations goes into separate .h files.

    You need to include header files, but not .cpp files.

    But then I've a few questions from this. I've possible answers, but not for every question, and I'm not sure, if I am correct:

    If my declarations changes are defined in the menu, i should use functions like
    Code:
    resolution(int res){return res;}
    in the header file, right?
    What if I've got a big, long game logic, like calculating collisions, taking movements, changing songs, sounds, different effects, stuff like that... Should i put that like this?:
    Declarations of images, etc into .h file,
    Declarations of sprites, movement, etc into separate/single .cpp file.
    Then how should i use it with the main function? I mean, there's all the logic, so what about the main function? Ain't it's neccesary to put something in it? I mean, will the game work normally like that? And what should i put into the main function then (lol.)?

  5. #5
    Sweet
    Join Date
    Aug 2002
    Location
    Tucson, Arizona
    Posts
    1,807
    Typically you break .h files down by class.

    Bitmap.h
    Code:
    class CBitmap
    {
    public:
        int Width() const;
    private:
        int mWidth;
    };
    Bitmap.cpp
    Code:
    #include "Bitmap.h"
    
    int CBitmap::Width() const
    {
        return this->mWidth;
    }
    And for main ussually when I am doing this I just have a gamemanager class I call. That has the main game loop in there.
    Woop?

  6. #6
    Novice programmer newn's Avatar
    Join Date
    Aug 2010
    Posts
    59
    Hmmm, is there any website explaining all this stuff in-depth? I mean not one by one, a little of each, but one big thing, about how to use these things big. Maybe even some examples, that would be very nice. Would explain me a lot more, i think. Now I'm a little bit confused, still.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Header files question
    By Programmer_P in forum C++ Programming
    Replies: 8
    Last Post: 05-14-2009, 01:16 PM
  2. header and source files
    By gtriarhos in forum C Programming
    Replies: 3
    Last Post: 10-02-2005, 03:16 AM
  3. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 09:33 AM
  4. What /what not to put in header files
    By Just in forum C Programming
    Replies: 1
    Last Post: 12-14-2002, 09:45 AM
  5. Using c++ standards
    By subdene in forum C++ Programming
    Replies: 4
    Last Post: 06-06-2002, 09:15 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21