cutting down on code lines

This is a discussion on cutting down on code lines within the Game Programming forums, part of the General Programming Boards category; Is there a way I can use multiple files for one program? Like write one lvlone.cpp for my first level ...

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

    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をこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,538
    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 wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,490
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  6. #6
    Bored Programmer
    Join Date
    Jul 2009
    Location
    Tomball, TX
    Posts
    407
    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-15-2002, 11:04 PM

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