Thread: cutting down on code lines

  1. #1
    Bored Programmer
    Join Date
    Jul 2009
    Location
    Tomball, TX
    Posts
    428

    cutting down on code lines

    Is there a way I can use multiple files for one program? Like write one lvlone.cpp for my first level functions then a new lvltwo.cpp for my second level and so on. Then when I write the playgame function just have a playlvlone(); function. I am trying to cut down on the lines of code I have to navigate through. I know there has to be a way to do it. No way modern games are made wading through a million lines of code in one file.

  2. #2
    Webhead Spidey's Avatar
    Join Date
    Jul 2009
    Posts
    285
    Sure, you can split your declarations/function prototypes into separate header files and then include them in your main program.
    Here is a short example

    Code:
    //Level1.h
    void LoadLevel1();
    Code:
    //Level1.cpp
    #include "Level1.h"
    #include <iostream>
    
    using std::cout;
    using std::endl;
    
    void LoadLevel1()
    {
    	cout << "Level 1 Loaded" << endl;
    }
    Code:
    //main.cpp
    #include "Level1.h";
    
    int main()
    {
    	LoadLevel1();
    }
    This way, you can split your code into several source files and have the main.cpp file nice and short.

  3. #3
    C++11 User Tux0r's Avatar
    Join Date
    Nov 2008
    Location
    Sweden
    Posts
    135
    Be sure to use header guards when writing headers, Spidey.

  4. #4
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    If you've got one file per level, you're doing something wrong.

    Your typical game is a few MB of program code (probably a few hundred thousand lines of code), and the rest of the DVD is program data.

    Pick any program with a level editor. You can add as many levels as you like, and customise almost ad infinitum, but the actual source code doesn't change at all.

    Eg.
    Code:
    <game>
      <level id="1">
        <map>
        </map>
      </level>
      <level id="2">
        <map>
        </map>
      </level>
      <level id="3">
        <map>
        </map>
      </level>
    </game>
    Adding level 4 is simple and obvious at this point


    Writing a game engine is harder than writing any single level in hard code, but once you're there, you're going to have a lot more fun adding levels quite quickly rather than cranking out a wedge of new code each time.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  6. #6
    Bored Programmer
    Join Date
    Jul 2009
    Location
    Tomball, TX
    Posts
    428
    I understand there is a way to cut down on code using other programs, but I started programming C++ and SDL less than a week ago. These little games I am making even though they are lengthy is more for my own practice and problem solving. After I feel comfortable enough with the codes and don't having to keep looking back at the last program I made to remember what my SDL load_images() function is written like and handling events, ect. Then I will look into downloading an engine or map editor for a better game.... right now I am just trying to work on a side scroller (mario 1 remnisenct). I haven't gotten anywhere near GL or directX or any 3d components. Thank you spidey and Elisa for the answer I was looking for.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 11
    Last Post: 10-07-2008, 06:19 PM
  2. Replies: 7
    Last Post: 07-30-2005, 11:28 PM
  3. count only lines of code...
    By flightsimdude in forum C Programming
    Replies: 13
    Last Post: 09-23-2003, 07:08 PM
  4. What do these tow lines of code mean?
    By Gamma in forum C++ Programming
    Replies: 1
    Last Post: 04-25-2002, 04:30 PM
  5. Replies: 4
    Last Post: 01-16-2002, 12:04 AM