[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:
Help with splitting new files.
#1 Members - Reputation: 137
Posted 09 September 2012 - 05:24 AM
[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:
#2 Members - Reputation: 603
Posted 09 September 2012 - 04:00 PM
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 Marketplace Seller - Reputation: 8937
Posted 09 September 2012 - 04:26 PM
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".
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?
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
#4 Members - Reputation: 137
Posted 11 September 2012 - 02:11 PM
#5 Marketplace Seller - Reputation: 8937
Posted 11 September 2012 - 03:09 PM
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.
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
#9 Marketplace Seller - Reputation: 8937
Posted 11 September 2012 - 03:51 PM
Is there some specific problem you're having?
Yes, that's correct. Sorry, I was thinking of sf::Rect which has width and height, and is sometimes returned by SFML functions.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?
Edited by Servant of the Lord, 11 September 2012 - 03:52 PM.
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
#10 Members - Reputation: 137
Posted 11 September 2012 - 04:03 PM






