classes: a pain in the ass.
Albin Engström

you know this thing they call classes? i hate it.. if i could, i would stick to structures and having all the code in one file, but after a while you get kinda tired of never finding what you're looking for.. so this morning i started to learn about classes (for real..), seams like it can't be mastered in one hour..

i think classes is very confusing.. a long time ago i thought classes was used to link files of code making it easier to manage your project.. i don't have to use classes right? isn't there a tutorial on this shit??

The question is: How do i alter use_triple_buffering in configuration.h from initialization.cpp function dynamic_initialization?(example) i've tried many things and it to complex to google..

1/*main.cpp -.-*/
2#include <allegro.h>
3#include "initialization.h"
4 
5initialization initialization;
6 
7int main()
8{
9 initialization.static_initialization();
10 initialization.dynamic_initialization();
11
12 while(!key[KEY_ESC])
13 {//main loop
14
15
16
17 }//main loop
18
19 initialization.de_initialization();
20}END_OF_MAIN()

1/*initialization.cpp :)*/
2#include <allegro.h>
3#include "initialization.h"
4#include "configuration.h"
5 
6//con
7initialization::initialization(){}
8//des
9initialization::~initialization(){}
10 
11//Static initialization
12void initialization::static_initialization()
13{
14 allegro_init();
15
16 install_timer();
17 install_keyboard();
18 install_mouse();
19
20 set_config_file("config.ini");
21}
22 
23//Dynamic initialization
24void initialization::dynamic_initialization()
25{
26 install_sound(DIGI_AUTODETECT, MIDI_NONE, NULL);
27
28 set_color_depth(16);
29}
30 
31void initialization::de_initialization()
32{
33 clear_keybuf();
34}

/*initialization.h :S*/
#ifndef INITIALIZATION_H
#define INITIALIZATION_H

class initialization
{
  public:
    initialization();
    ~initialization();
    void static_initialization();
    void dynamic_initialization();
    void de_initialization();
};
#endif

/*configuration.cpp :|*/
#include "configuration.h"
//con
configuration::configuration(){}
//des
configuration::~configuration(){}

1 
2/*configuration.h :(*/
3#ifndef CONFIGURATION_H
4#define CONFIGURATION_H
5 
6class configuration
7{
8 public:
9 configuration();
10 ~configuration();
11
12 bool use_triple_buffering;
13};
14#endif

Marco Radaelli
Quote:

The question is: How do i alter use_triple_buffering in configuration.h from initialization.cpp function dynamic_initialization?

You need a reference in initialization to an object of type configuration. And you need to add a setter to the configuration class (like void set_use_tb() { use_triple_buffer = true; } ), but I think you're not using classes the way they're meant to be used.

For instance, a class should contain all the properties that make up a state, plus the operations to manipulate that state.

So, in your case I would do a game class, that will initialize things in its constructor (there's no need to add that static_initialization(), if you put that code in the constructor it will be called when the variable in created).

[edit]

The main difference between structs and classes is that the latters let you specify properties and operations to be executed on them in a single, close, object.

[edit2]

Another point: it's not always the best to separate things in the code just because they are in real life (above all if you then need a reference to keep the objects together, like initialization and configuration).

X-G

I would just like to add that this is not the right way to use OO. This is just procedural programming pretending to be something else, so no wonder it's coming off as strange...

Albin Engström
Marco Radaelli said:

but I think you're not using classes the way they're meant to be used

neither do i.. thank you,

X-G said:

I would just like to add that this is not the right way to use OO. This is just procedural programming pretending to be something else, so no wonder it's coming off as strange...


what is OO?
pretending!? - -

X-G
Paul whoknows

You don't need a class for your allegro game initialization, that can be simple done with some ifs.
Nice tutorial

Matt Weir

If you really want to learn this stuff a book is probably the best way to go. Probably not the 'dive right in' answer you seem to be looking for but Object Orientated programming is a completely different mindset to structured programming and you need to learn some new concepts.

I've read a lot in the past and most were good enough to get you through all the basics. While a I can't remember what the best book I've read is, any of the following were good enough should be at your library, school etc.

Teach yourself C++ in 24 hours (don't be suckered by the title...)
Dummies guide to C++ (don't let the 'Dummies guide' bit blind your judgement in thinking the book is crap)

Once you start learning a bit you'll wonder why you didn't start earlier! It's really useful stuff. :)

Matt Weir.

23yrold3yrold
Quote:

you know this thing they call classes? i hate it.. if i could, i would stick to structures

Go for it. :) Structs are just classes whose members default to public instead of private. ;)

.... yes, I know what you're saying, but I had to throw that out there. ;D

Tobias Dammers
Quote:

Teach yourself C++ in 24 hours (don't be suckered by the title...)

Bad choice.
I used it, and it doesn't even cover all the basic language features. It also advises you to create your own string class, and it doesn't really cover the STL (nor does it encourage you to use it).

Quote:

Dummies guide to C++ (don't let the 'Dummies guide' bit blind your judgement in thinking the book is crap)

Haven't really tried that.

My choice would have to be Bruce Eckel's Thinking in C++. Available on-line for free, but in my experience, a printed book is always nice to have next to your computer.

OT:
You don't need to use classes just to divide your project into several files. Here's a quick-start guide for multi-file project management:
1. Group your existing functions and definitions by functional category. For example, "Allegro setup", "Screen buffering", "Sprite management", "Game entities", "Main loop", "GUI".
2. For each of these categories, make a .c or .cpp file that contains everything that belongs into the category.
3. Create a "header" for each .cpp file, with the same name but ending in .h or .hpp. Put guards around it:

#ifndef ALLEGRO_SETUP_H
#define ALLEGRO_SETUP_H

// everything else goes here.

#endif

4. For each function that is needed in another module, put the function declaration into the header of its own module, and include the header into the .cpp file of the module that needs it. For example, the main loop will probably use a function called flip() from the screen buffer module. So you have:

1// ------ screenbuf.h ----
2#ifndef SCREENBUF_H
3#define SCREENBUF_H
4 
5void flip(); // defined in screenbuf.cpp
6// ...
7 
8#endif
9// ------ mainloop.cpp ----
10#include "mainloop.h"
11#include "screenbuf.h" // this is so we can use flip()
12 
13//...
14 
15void graphics_update() {
16 // Render the scene to backbuf
17 flip();
18}

5. Put all type definitions (including struct types) into the header file for their module.
6. For each variable that you want to be accessible from the outside, put an "extern" reference in the module's header:

// ---- module.h ----
#ifndef MODULE_H
#define MODULE_H

extern int this_is_globally_available;

#endif
// ---- module.cpp ----
int this_is_globally_available = 23;

7. NEVER include a .c or .cpp file.
8. NEVER include a .c or .cpp file.

When you come to realize that certain groups of variables and functions "belong together", then you have a candidate for a class. But I suggest you read up on OO design before doing actual coding; and it's probably a very bad idea to apply OO design to an already existing project. Design in OO paradigm from the start, or keep the project procedural.

James Stanley
Quote:

if i could, i would stick to structures and having all the code in one file, but after a while you get kinda tired of never finding what you're looking for

Um... You're allowed to use more than one file when you use structs... ;)

Dustin Dettmer
Quote:

think classes is very confusing.. a long time ago i thought classes was used to link files of code making it easier to manage your project.. i don't have to use classes right?

Classes are just structs with a few extra features.

Hard Rock

Personally for books I'd recommend "C++ the complete reference" which is significantly better then the Java book by the same author. It's really easy to read and does cover MOST of the important concepts you'll need to learn.

However for indepth knowledge you'll need "The C++ Programming Language", which is THE C++ book.

Albin Engström

;D thanks!!

i'm more of a writing person than a reading one.. in all aspects of life.
but i guess you need to read to know how to write.. :).

Thread #590665. Printed from Allegro.cc