Allegro.cc - Online Community

Allegro.cc Forums » Programming Questions » classes: a pain in the ass.

This thread is locked; no one can reply to it. rss feed Print
classes: a pain in the ass.
Albin Engström
Member #8,110
December 2006
avatar

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
Member #3,028
December 2002
avatar

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
Member #856
December 2000
avatar

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...

--
Since 2008-Jun-18, democracy in Sweden is dead. | 悪霊退散!悪霊退散!怨霊、物の怪、困った時は ドーマン!セーマン!ドーマン!セーマン! 直ぐに呼びましょう陰陽師レッツゴー!

Albin Engström
Member #8,110
December 2006
avatar

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
Member #856
December 2000
avatar

--
Since 2008-Jun-18, democracy in Sweden is dead. | 悪霊退散!悪霊退散!怨霊、物の怪、困った時は ドーマン!セーマン!ドーマン!セーマン! 直ぐに呼びましょう陰陽師レッツゴー!

Paul whoknows
Member #5,081
September 2004
avatar

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

____

"The unlimited potential has been replaced by the concrete reality of what I programmed today." - Jordan Mechner.

Matt Weir
Member #7,476
July 2006
avatar

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
Member #1,134
March 2001
avatar

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

--
Software Development == Church Development
Step 1. Build it.
Step 2. Pray.

Tobias Dammers
Member #2,604
August 2002
avatar

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.

---
Me make music: Triofobie
---
"We need Tobias and his awesome trombone, too." - Johan Halmén

James Stanley
Member #7,275
May 2006
avatar

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... ;)

ImLeftFooted
Member #3,935
October 2003
avatar

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
Member #1,547
September 2001
avatar

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.

_________________________________________________
Hard Rock
[ Stars Dev Company ][ Twitter ][Global Warming: ARA My TINS 07 Entry][Pong Ultra Website][GifAllegS Ver 1.07]
"Well there's also coolwebsearch but we'll let that be an IE exclusive feature" - arielb on the New Browser Plugins "What's better, HTML or Variables?"

Albin Engström
Member #8,110
December 2006
avatar

;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.. :).

Go to: