• Advertisement
Sign in to follow this  

Help with splitting new files.

This topic is 1956 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi, I am trying to organize my coding but i keep getting errors, could someone help me? i am using one file which is the Main file and everything is in there i want to become more organized for example i want a file with logic, one with levels ect. but i keep getting errors need some help. this is the code.
[source lang="cpp"]//Libraries
#include <SFML/Graphics.hpp>
#include <iostream>
#include <string>
#include <SFML/Audio.hpp>
#include <SFML/Graphics.hpp>
using namespace sf ;
using namespace std ;

//Object for the events
Event event;

//Global variables, functions, classes

//C++ program entry point
int main()
//Variable that keeps the Game loop running
bool play = true ;

//Mouse Co-ordinates
bool mousecoordinates = false ;

//Movement Variables
bool dPressed = false ;
bool aPressed = false ;
bool wPressed = false ;
bool sPressed = false ;
int xVelocity = 0;
int yVelocity = 0;

//Action Variable
bool spacebar = false ;

bool leftClick = false ;
int mouseX = 0, mouseY = 0;

//Creating the window
RenderWindow window(VideoMode(1280, 720, 32), "SFML Game") ;

//Setting the framrate limit to 60 FPS
window.setFramerateLimit(60) ;

Texture background;
if (background.loadFromFile("Pictures/Background.png") == 0){
return 1;

//Shapes Colour
Texture Shape;
if (Shape.loadFromFile("Pictures/ShapeColour.png") == 0){
return 1;

Texture Character;
if (Character.loadFromFile("Pictures/Character1.png") == 0){
return 1;
Texture texture;
texture.create(40, 45);
Sprite sprite1;
sprite1.setPosition(70, 635);

//Level 1 part1
Texture Level1a;
if (Level1a.loadFromFile("Pictures/level1 part1.png") == 0){
return 1;
Texture leveltex;
leveltex.create(1280, 40);
Sprite level1a;
level1a.setPosition(0, 680);

//Level 1 part2
Texture Level1b;
if (Level1b.loadFromFile("Pictures/Level1 part2.png") == 0){
return 1;
Texture leveltex1;
leveltex1.create(40, 640);
Sprite level1b;
level1b.setPosition(0, 40);

//level 1 part3
Texture Level1c;
if (Level1c.loadFromFile("Pictures/Level1 part3.png") == 0) return 1;
Sprite level1c;

//level 1 part4
Texture Level1d;
if (Level1d.loadFromFile("Pictures/Level1 part2.png") == 0) return 1;
Sprite level1d;

Font font;
if (font.loadFromFile("Fonts/ariali.ttf") == 0){
return 1;

SoundBuffer JumpBuffer;
if (JumpBuffer.loadFromFile("Sounds/jump.wav") == 0){
return 1;

Sound Jump;

Music sidewalk;
if (sidewalk.openFromFile("Songs/Sidewalk.ogg") == 0){
return 1;


//Render Shapes
RectangleShape background1;
background1.setSize(Vector2f(1280, 720));
background1.setPosition(0, 0);

Text title;
title.setPosition(0, 0);

Text credits;
credits.setString("by Rishan");
credits.setPosition(0, 150);

//Game Loop
while (play == true) {



//When X is pressed the game closes
if (event.type == Event::Closed){
play = false ;

if (event.type == Event::KeyPressed){
if (event.key.code == Keyboard::A) aPressed = true;
if (event.key.code == Keyboard::D) dPressed = true;
if (event.key.code == Keyboard::Space){
window.setKeyRepeatEnabled(false) ;
spacebar = true;}

if (event.type == Event::KeyReleased){
if (event.key.code == Keyboard::A) aPressed = false;
if (event.key.code == Keyboard::D) dPressed = false;
if (event.key.code == Keyboard::Space) spacebar = false;

//Mouse Position
if (event.type == Event::MouseMoved){
mouseX = event.mouseMove.x ;
mouseY = event.mouseMove.y ;
if (event.type == Event::KeyPressed && event.key.code == Keyboard::F1){
mousecoordinates = true ;

//Music Pause
if (event.type == Event::KeyPressed && event.key.code == Keyboard::F2){

//Music Play
if (event.type == Event::KeyPressed && event.key.code == Keyboard::F3){



if (dPressed == true){
xVelocity = 5;

if (aPressed == true){
xVelocity = -5;

if (dPressed == true && aPressed == true){
xVelocity = 0;

if (dPressed == false && aPressed == false){
xVelocity = 0;

if (spacebar == true){
yVelocity = -5;

sprite1.move(xVelocity, 0);
sprite1.move(0, yVelocity);

if (sprite1.getGlobalBounds().intersects(level1a.getGlobalBounds()) == true){

sprite1.move( -xVelocity, 0);
sprite1.move( 0, -yVelocity);


if (sprite1.getGlobalBounds().intersects(level1b.getGlobalBounds()) == true){

sprite1.move( -xVelocity, 0);
sprite1.move( 0, -yVelocity);


if (sprite1.getGlobalBounds().intersects(level1c.getGlobalBounds()) == true){

sprite1.move( -xVelocity, 0);
sprite1.move( 0, -yVelocity);

if (sprite1.getGlobalBounds().intersects(level1d.getGlobalBounds()) == true){

sprite1.move( -xVelocity, 0);
sprite1.move( 0, -yVelocity);


//Print to console
if (leftClick == true){
cout << "Shoot" << endl ;

leftClick = false ;

if (mousecoordinates == true){
cout << "Mouse x: " << mouseX << " Mouse y: " << mouseY << endl ;
mousecoordinates = false ;






//Clean Up


Share this post

Link to post
Share on other sites
Why are you using two namespaces, there could be name collisions going on. Use sf:: for every sf namespace object you want to call, and get rid of line 7.
Why are you hiding the include names from us, if not then why are there empty includes in your code?
Do not name a texture Shape, this could be causing name collisions if you import the Graphics library from sfml.
You need to organize the game into states, state transitions are triggered by global events mapped to other states.

How did you split up the files, show us how you did it. That way we can give you more detailed information.

Share this post

Link to post
Share on other sites
As kd7tck, mentioned, namespaces exist to prevent two classes with the same name from clashing with each other. In std:: there is a class called 'string'. What if [url="http://www.sfml-dev.org/documentation/1.4/classsf_1_1String.php"]SFML has a class called 'string' also[/url]? Luckily, SFML's String has an uppercase 's' so in this instance it won't actually clash, but having 'string str', and 'String str' in the same code base, both serving different purposes and doing different things, makes the code more confusing.

Unless you are going multiple namespaces deep (or those namespaces have long names), you should type out the full namespace. It's really not much worse to type 'std::string' than to type 'string' once you get used to it. If you must bring something into the global namespace, specify that class, instead of dragging the entire 'std' namespace into the global namespace: "[b]using std::string[/b]" instead of "[b]using [/b]([size=2]the entire[/size]) [b]namespace std[/b]". [img]http://public.gamedev.net//public/style_emoticons/default/smile.png[/img]

Remember that the namespace is part of the name of the class. Don't shorthand it just for the sake of typing a few characters less; it helps describe your code better, declaring your intent to people reading your code (including yourself when you revisit the code two months later forgetting what you wrote).

The name of the variable is 'std::string' (The 'string' within the namespace 'std').


Could you repost your code? It seems alot of it is missing.

How did you break it apart into multiple files (show your separate files), and what error messages did you get?

Share this post

Link to post
Share on other sites
Thank you for the reply guys. Sorry but i don't know why it didn't show the includes. I am not hiding them and i thought i posted them something must have gone wrong. I have given up and have started from scratch as I thought it would make life easier as i could modify it. Would you suggest i stop using namespace std and just start putting std::... for practice. I used for the reason you posted. Bit lazy :) . I am a newbie and learn best from experience. I just started SFML right after learning c++. i learnt c++ in 2 months so I still dont understand somethings and hope to understand through practice. I am 16 so i have a lot of time left. Also could you help me with some thing else, its a small thing or should i start a new thread? the problem is this if (velocityX != 0 || velocityY != 0){sourceX += tempTexture.GetWidth() / 4; } , how would i do that in sfml 2.0 as there is only a option for .getSize() and i cant divide it by 4.

Share this post

Link to post
Share on other sites
Yes, I would recommend just typing out the "std::", it's only five more letters, and once you type it out a few hundred times, you can type it out pretty fast and won't even notice.

SFML getSize() functions return another struct/class, usually [url="http://www.sfml-dev.org/documentation/2.0/classsf_1_1Vector2.php"]sf::[/url][url="http://www.sfml-dev.org/documentation/2.0/classsf_1_1Rect.php"]Vector2u[/url].

You can go: [i]myRenderWindow.getSize().width;[/i] and [i]myRenderWindow.getSize().height;[/i]
In your situation: { sourceX += (tempTexture.getSize().height / 4); }

The [url="http://www.sfml-dev.org/documentation/2.0/classsf_1_1Texture.php"]documentation [/url]tells you all the different variables and functions a struct or class has.

Share this post

Link to post
Share on other sites
getSize(). height didn't work it wasn't a member but it allowed me x and y would x be width and y height or are they completely different?

Share this post

Link to post
Share on other sites
i am using this http://www.youtube.com/watch?v=X4cSJ5beVJM&feature=BFa&list=SP0249162D5D6FA5D1 but some of the code i cant do in 2.0 or atleast dont know how to. These are the tutorials i am using to make my first game.

Share this post

Link to post
Share on other sites
That's fine. SFML 2 is still rather new, and not alot of tutorials has come out for it yet, but it's perfectly usable. You just have to learn how to use google.

Is there some specific problem you're having?

[quote name='rishan1996' timestamp='1347399593' post='4979071']
getSize(). height didn't work it wasn't a member but it allowed me x and y would x be width and y height or are they completely different?
Yes, that's correct. Sorry, I was thinking of [url="http://www.sfml-dev.org/documentation/2.0/classsf_1_1Rect.php"]sf::Rect[/url] which has width and height, and is sometimes returned by SFML functions. [img]http://public.gamedev.net//public/style_emoticons/default/happy.png[/img] Edited by Servant of the Lord

Share this post

Link to post
Share on other sites
ok thank you, i have used google alot for sfml 2.0 because i have to but there are some thing which i cant find on google or at least dont have the time to spend looking for or i am just not a good googler :), forums are my last resort and i use google a lot for programming. Also there is one more thing which i have been searching the last half an hour. I will post tomorrow if i cant find anything for it. its 11 where i am so i have to sleep have school tomorrow. thank you fot the help so far really appreciate it. If i could i would give you +1 rep

Share this post

Link to post
Share on other sites
Sign in to follow this  

  • Advertisement