Jump to content

  • Log In with Google      Sign In   
  • Create Account

Help with splitting new files.


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
9 replies to this topic

#1 rishan1996   Members   -  Reputation: 137

Like
0Likes
Like

Posted 09 September 2012 - 05:24 AM

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 eventsEvent event;//Global variables, functions, classes//C++ program entry pointint main(){ //Variables //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 ; //Mouse 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) ; //Background Texture background; if (background.loadFromFile("Pictures/Background.png") == 0){ return 1; } //Shapes Colour Texture Shape; if (Shape.loadFromFile("Pictures/ShapeColour.png") == 0){ return 1; } //Character Texture Character; if (Character.loadFromFile("Pictures/Character1.png") == 0){ return 1; } Texture texture; texture.create(40, 45); Sprite sprite1; sprite1.setColor(Color::White); sprite1.setTexture(Character); sprite1.setPosition(70, 635); //Levels //Level 1 part1 Texture Level1a; if (Level1a.loadFromFile("Pictures/level1 part1.png") == 0){ return 1; } Texture leveltex; leveltex.create(1280, 40); Sprite level1a; level1a.setTexture(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.setTexture(Level1b); level1b.setPosition(0, 40); //level 1 part3 Texture Level1c; if (Level1c.loadFromFile("Pictures/Level1 part3.png") == 0) return 1; Sprite level1c; level1c.setTexture(Level1c); level1c.setPosition(0,0); //level 1 part4 Texture Level1d; if (Level1d.loadFromFile("Pictures/Level1 part2.png") == 0) return 1; Sprite level1d; level1d.setTexture(Level1d); level1d.setPosition(1240,40); //Font Font font; if (font.loadFromFile("Fonts/ariali.ttf") == 0){ return 1; } //Sounds SoundBuffer JumpBuffer; if (JumpBuffer.loadFromFile("Sounds/jump.wav") == 0){ return 1; } Sound Jump; Jump.setBuffer(JumpBuffer); Jump.setVolume(20); //Music Music sidewalk; if (sidewalk.openFromFile("Songs/Sidewalk.ogg") == 0){ return 1; } sidewalk.setVolume(50); sidewalk.setLoop(true); sidewalk.play(); //Render Shapes RectangleShape background1; background1.setSize(Vector2f(1280, 720)); background1.setPosition(0, 0); background1.setTexture(&background); //Text Text title; title.setFont(font); title.setCharacterSize(100); title.setString("Bearumper\n"); title.setPosition(0, 0); title.setColor(Color::Blue); Text credits; credits.setFont(font); credits.setCharacterSize(50); credits.setString("by Rishan"); credits.setPosition(0, 150); credits.setColor(Color::Blue); //Game Loop while (play == true) { //Events while(window.pollEvent(event)){ //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) ; Jump.play(); 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){ sidewalk.pause(); } //Music Play if (event.type == Event::KeyPressed && event.key.code == Keyboard::F3){ sidewalk.play(); } } //Logic //Movement 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; } //Move sprite1.move(xVelocity, 0); sprite1.move(0, yVelocity); //Collision 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 ; } //Character //Rendering window.clear(); window.draw(background1); window.draw(level1a); window.draw(level1b); window.draw(level1c); window.draw(level1d); window.draw(title); window.draw(credits); window.draw(sprite1); window.display(); } //Clean Up window.close();}[/source]

Sponsor:

#2 kd7tck   Members   -  Reputation: 715

Like
1Likes
Like

Posted 09 September 2012 - 04:00 PM

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.

#3 Servant of the Lord   Crossbones+   -  Reputation: 19486

Like
0Likes
Like

Posted 09 September 2012 - 04:26 PM

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 SFML has a class called 'string' also? 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: "using std::string" instead of "using (the entire) namespace std". Posted Image

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?
It's perfectly fine to abbreviate my username to 'Servant' rather than copy+pasting it all the time.
All glory be to the Man at the right hand... On David's throne the King will reign, and the Government will rest upon His shoulders. All the earth will see the salvation of God.
Of Stranger Flames - [indie turn-based rpg set in a para-historical French colony] | Indie RPG development journal

[Fly with me on Twitter] [Google+] [My broken website]

[Need web hosting? I personally like A Small Orange]


#4 rishan1996   Members   -  Reputation: 137

Like
0Likes
Like

Posted 11 September 2012 - 02:11 PM

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.

#5 Servant of the Lord   Crossbones+   -  Reputation: 19486

Like
0Likes
Like

Posted 11 September 2012 - 03:09 PM

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 sf::Vector2u.

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

The documentation tells you all the different variables and functions a struct or class has.
It's perfectly fine to abbreviate my username to 'Servant' rather than copy+pasting it all the time.
All glory be to the Man at the right hand... On David's throne the King will reign, and the Government will rest upon His shoulders. All the earth will see the salvation of God.
Of Stranger Flames - [indie turn-based rpg set in a para-historical French colony] | Indie RPG development journal

[Fly with me on Twitter] [Google+] [My broken website]

[Need web hosting? I personally like A Small Orange]


#6 rishan1996   Members   -  Reputation: 137

Like
0Likes
Like

Posted 11 September 2012 - 03:37 PM

thank you i just didnt understand it much, also thank you soo much for the amazingly quick reply!

#7 rishan1996   Members   -  Reputation: 137

Like
0Likes
Like

Posted 11 September 2012 - 03:39 PM

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?

#8 rishan1996   Members   -  Reputation: 137

Like
0Likes
Like

Posted 11 September 2012 - 03:51 PM

i am using this 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.

#9 Servant of the Lord   Crossbones+   -  Reputation: 19486

Like
0Likes
Like

Posted 11 September 2012 - 03:51 PM

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?

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 sf::Rect which has width and height, and is sometimes returned by SFML functions. Posted Image

Edited by Servant of the Lord, 11 September 2012 - 03:52 PM.

It's perfectly fine to abbreviate my username to 'Servant' rather than copy+pasting it all the time.
All glory be to the Man at the right hand... On David's throne the King will reign, and the Government will rest upon His shoulders. All the earth will see the salvation of God.
Of Stranger Flames - [indie turn-based rpg set in a para-historical French colony] | Indie RPG development journal

[Fly with me on Twitter] [Google+] [My broken website]

[Need web hosting? I personally like A Small Orange]


#10 rishan1996   Members   -  Reputation: 137

Like
0Likes
Like

Posted 11 September 2012 - 04:03 PM

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




Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS