Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: How to use header and .cpp files efficiantly with SFML?  (Read 8950 times)

0 Members and 1 Guest are viewing this topic.

newn

  • Guest
How to use header and .cpp files efficiantly with SFML?
« on: September 14, 2010, 05:05:27 pm »
Hi everyone. Since i was trying to rewrite my Ping Pong game many, many, many times... I don't even know how many times. I always discover something new, better, more advanced and rewriting it whole again. So i came to header file questions. My program wasn't working alright, then I asked in some other forums about why it isn't working alright. It was more a C++ related question, not SFML, i think. So, i was explained, that in header files, we should include only definitions, prototypes... Then i was confused... Why, how, when, where... All these questions. You already are using such things in SFML... Even declaring images. So i was thinking: how to make the code look good? How to create few nice header files, few .cpp files for my Ping Pong game... I've tried a few things, but there was something not working, or not working as intended things. So i went to the tutorials section and found the multiple screens tutorial again. It had a few header files. However, there was a lot of... Logic(?) code, not only definitions. So I'm even more confused, on how to use those header files after all... So this is the first question, how am I supposed to use those header files in SFML? When should i create additional .cpp files, or .h files? I could provide the code, if it's neccesary.

Another quesiton is, why I'm getting some errors, when trying to compile this program: http://www.sfml-dev.org/wiki/en/tutoriels/multiecransjeu_en?
I've made all the header files and .cpp file, copied (to test it, before applying to my own project) and tried to compile. I'm receifving these errors (guessing, that something is old in that code):
Code: [Select]
1>f:\development\tests\multiple windows\multiple windows\screen_0.h(5): error C2504: 'Screen' : base class undefined
1>f:\development\tests\multiple windows\multiple windows\screen_0.h(64): error C2039: 'SetBackgroundColor' : is not a member of 'sf::RenderWindow'
1>          d:\software\microsoft visual studio\vc\include\sfml\graphics\renderwindow.hpp(45) : see declaration of 'sf::RenderWindow'
1>f:\development\tests\multiple windows\multiple windows\screen_1.h(5): error C2504: 'Screen' : base class undefined
1>f:\development\tests\multiple windows\multiple windows\screen_1.h(33): error C2039: 'SetBackgroundColor' : is not a member of 'sf::RenderWindow'
1>          d:\software\microsoft visual studio\vc\include\sfml\graphics\renderwindow.hpp(45) : see declaration of 'sf::RenderWindow'
1>f:\development\tests\multiple windows\multiple windows\screen_1.h(80): warning C4244: 'argument' : conversion from 'int' to 'float', possible loss of data
1>f:\development\tests\multiple windows\multiple windows\screen_1.h(80): warning C4244: 'argument' : conversion from 'int' to 'float', possible loss of data
1>f:\development\tests\multiple windows\multiple windows\main.cpp(9): error C2065: 'cScreen' : undeclared identifier
1>f:\development\tests\multiple windows\multiple windows\main.cpp(9): error C2059: syntax error : '>'
1>f:\development\tests\multiple windows\multiple windows\main.cpp(13): error C2143: syntax error : missing ',' before ')'
1>f:\development\tests\multiple windows\multiple windows\main.cpp(26): error C2143: syntax error : missing ';' before '{'
1>f:\development\tests\multiple windows\multiple windows\main.cpp(28): error C2143: syntax error : missing ';' before '}'
1>f:\development\tests\multiple windows\multiple windows\main.cpp(31): error C2143: syntax error : missing ';' before '}'
1>f:\development\tests\multiple windows\multiple windows\main.cpp(32): error C2143: syntax error : missing ';' before '}'
1>f:\development\tests\multiple windows\multiple windows\main.cpp(32): fatal error C1004: unexpected end-of-file found

Hiura

  • SFML Team
  • Hero Member
  • *****
  • Posts: 4321
    • View Profile
    • Email
How to use header and .cpp files efficiantly with SFML?
« Reply #1 on: September 15, 2010, 12:09:20 pm »
Well, the code of this page could have be better (as always in fact). There are two things that pop up to me. The first one, as you said, is about header/source file (i.e., .hpp/.cpp files). For example I would have split «screen_0.hpp» up this way :
Header (put in these kind of file the «definition»)
Code: [Select]

#ifndef __SCREEN_0__ // NEW
#define __SCREEN_0__ // NEW


#include <iostream>
#include "screen.hpp"
 
class screen_0 : public Screen
{
private:
    int alpha_max;
    int alpha_div;
    bool playing;
public:
    screen_0 (void);
    virtual int Run (sf::RenderWindow &App);
};

#endif // NEW

Source (put here the «implementation»)
Code: [Select]

#include "screen_0.hpp" // NEW

screen_0::screen_0 (void)
{
    alpha_max = 3*255;
    alpha_div = 3;
<snip>
:
</snip>
    //Never reaching this point normally, but just in case, exit the application
    return (-1);
}


The second thing is about readability and C-style VS C++-style. For example the author creates all his variables at the beginning of functions which is «bad». With C++ we create variables as late as possible. (The rule is : I need a variable NOW (not before) to store some data so I create it NOW (and not before)).


Quote
Another quesiton is, why I'm getting some errors, when trying to compile this program: http://www.sfml-dev.org/wiki/en/tutoriels/multiecransjeu_en?
I've made all the header files and .cpp file, copied (to test it, before applying to my own project) and tried to compile. I'm receifving these errors (guessing, that something is old in that code):

The code was designed to use an earlier version of SFML. Now SetBackgroundColor is Clear for example. There may be non-SFML related errors too. Nothing is perfect.
SFML / OS X developer

newn

  • Guest
How to use header and .cpp files efficiantly with SFML?
« Reply #2 on: September 15, 2010, 01:59:59 pm »
Okay, I've renamed the name of the thread, since this question was answered, there's only one question left. Also, I've described the question in bold too. And thanks for your answer.

Hiura

  • SFML Team
  • Hero Member
  • *****
  • Posts: 4321
    • View Profile
    • Email
How to use header and .cpp files efficiantly with SFML?
« Reply #3 on: September 15, 2010, 02:27:32 pm »
Quote
When should i create additional .cpp files, or .h files?
Think «modules». Each module comes in one header (.h/.hpp) and one source file (.cpp). You may suit this kind of rule to the situation if needed.

For example if you have some function about drawing (clear_screen(), display_score(), display_pads(), ... ) put them into drawing.hpp/drawing.cpp.

If you create a class you typically follow the above rule and create MYCLASSNAME.hpp and MYCLASSNAME.cpp files.

I don't know if I answered your answer… You may also be interested in this reading : http://geosoft.no/development/cppstyle.html/ .
SFML / OS X developer

newn

  • Guest
How to use header and .cpp files efficiantly with SFML?
« Reply #4 on: September 15, 2010, 08:40:01 pm »
I've made a little code example, but I'm a little confused on how to deal with the sf::RenderWindow thing, and if I'm doing the thing alright. It isn't working, as i said - confused about those two things.

http://pastebin.com/sLEe5mZb

main.cpp wasn't done yet, because I'm already confused about that window rendering thing...

Hiura

  • SFML Team
  • Hero Member
  • *****
  • Posts: 4321
    • View Profile
    • Email
How to use header and .cpp files efficiantly with SFML?
« Reply #5 on: September 16, 2010, 12:42:46 am »
Your problem is in fact not about how to use SFML but about C++ variables and their scope. I strongly recommend you to read a good book about C++, get some experiences without libraries (do some console-project) and then come back here to use the SFML.

I don't want to be arrogant or anything like that but if you don't have the basis of C++ programming you're about 99% sure to fail again and again.
SFML / OS X developer

newn

  • Guest
How to use header and .cpp files efficiantly with SFML?
« Reply #6 on: September 16, 2010, 01:25:04 am »
Well, i know the basics, and average stuff of C++,  but not advanced. I've read some tutorials about C++. I cannot afford a book though. Probably i should read advanced tutorials, before taking a library.
Hmmm, but is there any nice text-based games? I mean... That sounds strange. And doing simple example programs from some tutorial isn't going to help much with learning C++.

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6287
  • Thor Developer
    • View Profile
    • Bromeon
How to use header and .cpp files efficiantly with SFML?
« Reply #7 on: September 16, 2010, 01:36:01 am »
You could download a free e-book like "Thinking in C++" or borrow a C++ book at a local library, if you don't want to buy one.

There are many possibilities to create a text-based console game. Role play games are quite suitable, for example. It's important to play around with language features in own project, like this you get a lot of experience. Try to build up an object-oriented design, use the STL, and you're on a good way to learn C++. :)
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

newn

  • Guest
How to use header and .cpp files efficiantly with SFML?
« Reply #8 on: September 16, 2010, 09:48:15 am »
Thanks for the advice. To me RPG text-based sounds a little bit strange, lol. I'll try to think of something though. My head is not working for the past three weeks, but I'll try to think of some text-based game idea. But it sounds very strange to me, haven't played such a game, and I've played a lot of games. :)

Anyway, around here libraries doesn't have programming books, so I guess, that I'll download the free ebook then. Just on break from school for 15 minutes now.
Thanks for advices.