Original Post
Hi, I am an amateur coder, I have recently finished an engine, its 10 000 lines of code, in one file, I use lots of global variables, theres not one "class", I have used dynamic memory zero times. Detest me. I want to improve my style for future projects, and am hoping for your advice on a couple of questions and in general. How I have survived so far: I use c++-structs with functions, which is as far as I understand it like a class with everything public. Whenever there is a function I want to use in different structures like MoveXY() I make it a global function. MoveXY(&enemy.x,&enemy.y,enemy.speed,...,...etc); 1) Is using classes superior? Or is this something argued about or situationspecific. I understand that classes allow very capsulated programming. But I found that it gave me more coding work and uglier calls. Whats up with that? 2) Global variables. I use global variables(i have maybe 50-100) for everything that needs to be remembered for more than one frame. Is this the correct use? For example I have function RenderButtons() which does in the few times the player is shopping need to know which Button is the active one, so I have a global variable: int menu_buttonactive = 0; 3) Inheritance. Is this something I should cluster my code with to avoid writing it twice? Or something thats not really useful for a game coder unless he has a specific technology in mind. I have two structs, one is enemys[100] and one is particles[1000]. The game is OpenGl 2d and both could use the same basic attributes, like x,y,w,h,velx,vely... (maybe overlapping function, which I currently make global functions, too). Is this worthy of inheritance? 4) Optimization. Should I instead of func(int bla) give a pointer func(int* bla). This is quicker, right? Or is the compiler capable of optimizing the prog itself that far. Also should I write inline infront of everything, or can I rely on the compiler? 5) Dynamic lists. I havent used them, because I was too lazy too code them, and scared to inflict the anger of mighty memory. I dont really *need* them. The question for me is speed. Is int* pointer = new int; pointer = 5; slower than nr = 5; ? My main performance loss (though Im running at 70 fps on a 3Ghz machine) are for-loops. Lets say I have 1000 enemys with different weapons and each has a max of 20 shots at a time available. If I do this static im wasting a lot of loops for inactive shots. I should use an dynamic list here? does managing the list cost significant cpu? Is the memory loss of not using a list significant? Would it be optimal if I create a static array of objects and a dynamic list to keep track of the active objects?