• Announcements

GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.
Followers 0

Needing some quick tips on how to organize my code

20 posts in this topic

I've been learning C++ from a book lately and I've been making something of a console game/thingy, and a map editor for it... Whatever it is I'm just coding and it's being fun and interesting, but I've coded it all in one file, both because I wasn't expecting to get so far with it, and because I don't know any better. At some point I moved all the declarations into a header file to make it easier to tweak things without as much hassle, but it's still being a huge hassle, it's becoming too much to cope with. I'm well over 2000 lines of code (on the map editor alone, including comments and garbage), and I'm finding it too confusing. An example of a side effect I found was that I was assigning values to the same variable from three different functions during the initialization. But I'm finding it hard to detect and fix stuff like this in all of this mess.

So I need to separate my code into more files, but I haven't learned anything about classes or even header files yet. I "know" what they are and what they're for, but I can't make them on my own yet. So what I need is a quick (and maybe dirty) way to just break up the code and make it bearable, because if I stop working on it I'll eventually lose motivation to keep learning. It doesn't matter if it's a cheap thing to do, the project isn't important, it's just a way to get my hands dirty and learn from mistakes. Like my father said once, analogously, your first car is for you to break.

So if anyone could give me some hints or thoughts on how to go about separating the code into more .cpp or .h files I'd really appreciate it. My biggest problem is with avoiding calling the same header file from several files, I think. But I also feel confused on things like, should I just create header files, should I just create cpp files, should I create both as needed, and how do I determine if they're needed, and how exactly do I make the code from one file interact with the code from the other...

Any help is greatly appreciated. Thanks.

Edited by Master thief
2

Share on other sites

Thanks everyone for the responses. It's being quite insightful. I'm going to try some stuff tomorrow.

I've also just found an article that may also be helpful, though not today.

@Vortez, about the classes, I have... some degree of awareness on how to make them and how to use them, since I was starting to use them in actionscript 2 a few years ago before I stopped coding then. But there's two problems: 1-  I always just create a bunch of files and end up staring at them indecisively, due to not quite having an idea where to put what, and 2- I'm not yet familiar with c++'s syntax for classes and their baggage. I skipped a bit in the book I'm reading to quickly get to pointers, but I'm intending to get back to what I skipped (structs, enums, and more on strings). I don't want to rush it.

@dejaime, well... I'm extremely (really) picky when it comes to colors. I can't stand writing code in a white background, and I like to just get my hands dirty when I'm learning something. All the IDEs I tried (well, CB, VC and DC, don't know any others), kind of got in the way of it. There's always something that needs to be set or some intricacy that needs to be understood (i.e., project templates, MS's main() arguments), or otherwise something that doesn't work for very specific reasons. I've been away from C++ for years because of this. Also, they clutter my hard drive with project folders (VS is particularly unorganized, it mixes projects from all apps in one folder by default) when all I want at this point is a source file to experiment with. When learning the basics, I need a basic setup to get right down to it and keep me focused and without obstacles.

To that end I'm using Sublime Text for now, which seems to work well so far, though I see times where an IDE would be beneficial. But I find that those apps aren't much good at replicating the level of customization from ST, sadly. ST is great in it's keyword highlighting and dark background, it makes me feel extremely comfortable. So, at least for now I'm stuck with it. That said, I'm still using CB to, for example, compile tutorials I'm following on SDL. I code them in ST and compile them in CB. I still think it's too early for me to be delving into their distractions. I rather be reading the book instead of losing time with the IDEs.

EDIT: By the way, should, or could, variable declarations go on (non-class) header files too?

Edited by Master thief
1

Share on other sites

@dejaime, well... I'm extremely (really) picky when it comes to colors. I can't stand writing code in a white background, and I like to just get my hands dirty when I'm learning something. All the IDEs I tried (well, CB, VC and DC, don't know any others), kind of got in the way of it. There's always something that needs to be set or some intricacy that needs to be understood (i.e., project templates, MS's main() arguments), or otherwise something that doesn't work for very specific reasons. I've been away from C++ for years because of this. Also, they clutter my hard drive with project folders (VS is particularly unorganized, it mixes projects from all apps in one folder by default) when all I want at this point is a source file to experiment with. When learning the basics, I need a basic setup to get right down to it and keep me focused and without obstacles.

I would recommend you to reconsider - a proper ide (which Sublime Text at least does not seem to be from my cursory examination) is an invaluable assistance, especially if you are relatively new. And even more so when you are not new and your projects grow to anything above trivial.

I would recommend Visual Studio 2013 (for desktops, express edition - ie. free). Its coloring scheme is highly customizable - comes even with a "dark" theme as a preset option (or a starting point for your own customizations).

Syntax coloring options include separation of: global/local/member/static-member variables, namespaces/classes/enums/types, static/non-static member functions, macros etc...

Intellisense can also automatically pick up and mark with red-wiggles most errors without the need to compile and its hover-tooltips, as you will see below, are quite informative.

An example of my, slightly altered from default, coloring (i prefer white - used to prefer dark when i first started out ~20y ago):

http://postimg.org/image/r0ckj03i7/

edit: Uh, what, why the downvote? That makes no sense. Edited by tanzanite7
1

Share on other sites

I like how C::B handles its highlighting color schemes, maybe I am just used to it...
It also has some neat features for C++, such as active/inactive code highlighting (to name one):

Just used to it .

I have never used Code::Blocks myself - does it have comparable highlighting options to VS (look my pic in previous post [VS has quite a lot of type separation too - but i have colored almost all of them with the same color])?

The active/inactive code highlighting is obviously present in VS too - and its presence in CB hints that it too might have some Intellisense-esque capabilities, hence the Question. Edited by tanzanite7
1

Share on other sites

Depending on the project (ie single file or multiple) I use Geany and invert colors so the background is black and the Sons of Oblivion theme in C::B if I'm doing a large project. It isn't so much that I have issues with white backgrounds (and in fact I get nostalgic and change to them once in a while), but rather at night the dark IDE doesn't put off so much light so I don't wake my son while I'm programming.

1

Share on other sites

@dejaime, from reading that article, which I noticed is quite class heavy (which I guess was to be expected), I think I have to admit that separating code and creating classes seem to be two things that don't really go too well without each other. But classes are 6 chapters away in the book.... I guess I'll make a detour and see what I can manage to do.

I kind of noticed CB had themes, but I didn't feel like losing time with it. I may still try some sometime. One thing I noticed about CB is that it allows you to create a list of "user keywords", which probably helps if I want to make it look more similar to ST. But this is something I don't want to get distracted with right now. It's been two days since I last messed up with my code, and it was always distractions that made me end up stopping in the past...

@tanzanite7, sublime text isn't a "proper IDE", it's just a proper generalized text editor with lots of potential to be a decent coding environment. Much like TextMate (I think) and Vim and another one I can't remember. But of course it never reaches the same level of functionalities of an IDE that's made specifically for a language. I will probably want to use the debugger at some point.

Edited by Master thief
1

Share on other sites

I started placing stuff into a new file just to see if I could make sense of it. This is what I could gather so far, but I have conflicting ideas here:

I don't know if I should ask the GraphicsM class to draw the map on the buffer, or if that class should allow other classes to draw on it. Or maybe I'm missing something else that might solve my dilema. Or maybe I'm simply doing it all wrong.

class GraphicsManager
{
private:
char tempBuffer[18][35];

public:
void drawOnBuffer();   // draw something on the buffer?
void blit();
void clearBuffer();
void paintBackground(int symbol);    // paints background with the specified ascii symbol
};

class Map
{
private:
int map[18][35];  // the object is given a map from an output source (file), or from user interface (map editing)
int tileset[2][7];  // class stores active tileset here

public:
drawMap();  // draw the map to the buffer?   // or maybe ask the graphics manager to do it instead?
drawTileset(); // draw the tileset to the buffer?
};


I still haven't read about constructors and such, so don't mind the lack of anything that may be lacking. This is not even the real thing, it's just me trying to understand how to put it together from the code I already have and from what I already learned (which isn't much).

Edited by Master thief
1

Share on other sites

What i would do is create the Map class and let it handle the drawing, then i might add it to the GraphicManager later on, and access it from there.

The truth is, there is no perfect answer to this, but the map class should be responsible to draw the map (and do other map related stuffs) and nothing more.

Blit? what api are you using to draw your stuff? I generally just let a function called RenderScene() to do all the rendering to be honest, which call other classes or functions to draw the different parts of the scene.

Here's a small sample of my main drawing routine of an old game of mine

			// Disable lightning + depth test
glDisable(GL_LIGHTING);
glDisable(GL_DEPTH_TEST);

// Draw the sky
RenderSkyDome();
RenderSkyPlane();

// Enable lightning + depth test
glEnable(GL_LIGHTING);
glEnable(GL_DEPTH_TEST);

// Draw solid objects
RenderTerrain(UseLodModels());
RenderRocks();
RenderColumn();
// Render chess board
RenderChessBoard();
// Render chess pieces
RenderReflectedPcx(UseLodModels());
RenderChessPieces(UseLodModels());
RenderChessPiecesNextMoves(UseLodModels());
// Draw transparent objects
RenderWater(UseLodModels());

// Check the sun visibility status
pCam->SunVisStatus = DoLensFlareTests(pCam);

} else {
}
}



This is just to give you an idea. As you can see, i havent splitted objects into class, i just wrote a function to handle each of them, but it's an old game, today i might do this differently, but this was the simplest solution i though of at the time. I do however usually use a menu class to draw menu related stuff (not shown here).

Maybe you should start writting it in a more functionnal way, then refactor it later into classes, since you look a bit lost on what you are trying to achieve.

OOP is great, but it's a bit hard to get everything right the first time to be honest.

Edited by Vortez
2

Share on other sites

No APIs, just pure c++ and the console. I'm not sure if it matters much but since I'm making it purely on the windows console, the "rendering" may be a little different. Still, I'm doing something comparable to double buffering, where I'm drawing everything to an array, clear the screen, and then draw the whole array on the screen. That's why I named that function blit(), it was just the first thing that came to mind when I needed a name for it. Same with one I have called doThingsWithInput().

Here's the source code, by the way. If it matters.

The way I'm "rendering" it has it's caveats, is kind of slow and forced me to use system calls (for CLS - and this is because I didn't want to have the whole history of the console above, and also because the scrolling seemed worse to watch) for lack of better options. I made a post on TIG about it but no one seemed to have any better solutions that didn't involve external libraries. Still, I'm not really worried about that, I'm more worried about progressing. I'll do something better with SDL one of these days. I'm also enjoying the challenge of getting around the limitations of the console.

So, I don't know, since everything that needs to be drawn is drawn on that array before it gets drawn on the screen, either everything draws in it, or everything tells the class what to draw in it... I suppose those are the options, but I'm not sure which to choose. But if I choose the latter, I'm not sure how to make that class grasp every other class's needs.

So the first option, which is basically what you're saying, seems easier.

Edited by Master thief
1

Share on other sites
I read (vaguely) something about complexity somewhere but I can't recall where... Not sure if it was in the book I'm reading. Which is C++ Primer, by the way.

My only fear at this point - 5 chapters away from classes and not yet much more versed in anything that I wasn't before - would actually be to end up repeating myself, even if I was trying not to, since if I delve into another project the only things I'll do better right now are maybe just to make use of a few more I/O functions and a few tricks I learned about strings.

Or maybe, since the map editor is pretty much finished (except for one slightly important functionality that I couldn't implement due to the messy code (and loading maps)), I might get back to the game itself and start applying whatever else I learn in the next days.

But maybe I'm getting ahead of myself in my thinking. I'll do some reading as you suggested. Thanks. Edited by Master thief
1

Share on other sites
One question, from what I understand from what I've been reading, vectors are great to be used for, say, a player or a room's inventory. Would they be just as good for maps? Considering that the maps as I'm using them, are just an array of ints, I suppose at first they might be, but then the maps are sort of constant (the number of elements never changes in a map, an index's value can change (i.e, closed door (visible)/open door (not visible), but that's about it).

If so then I wonder if I have any use for arrays at all for what I'm doing. Edited by Master thief
1

Share on other sites

I think (and I could be wrong) the general rule is that anything you use an array for, you can use a vector for. Obviously, the first major trade off is you can now make maps any size (so you can make maps that get larger as the player goes on) and resize it per level. This would be a plus in puzzle games as you could start small and get bigger as the player plays and learns more, for example.

Edited by BHXSpecter
1

Share on other sites
I've planned the maps to be of a fixed size, where you can "travel" from one map to the other in an open-world-ish kind of way. But who knows I may change my mind given the opportunity...
0

Share on other sites

If so then I wonder if I have any use for arrays at all for what I'm doing.

A vector is the more robust option and usually what you want to use whenever you want dynamically allocated array-like functionality.

You'll be surprised how little you actually use proper arrays.

Edited by dmatter
0

Create an account

Register a new account