• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.
Sign in to follow this  
Followers 0
superman3275

Another SFML pong error.... :(

12 posts in this topic

Well, I have my work cut out for me. Pong seems impossible!
It won't display my sprite to the screen for some reason?
Pong.cpp: (The extra Sprite is just for reference, ignore it)
[CODE]
// Pong.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <SFML/Graphics.hpp>
#include "Paddle.h"

int _tmain(int argc, _TCHAR* argv[])
{
sf::RenderWindow App(sf::VideoMode(800,600,32), "Pong");
App.SetFramerateLimit(30);
sf::Image Image1;
sf::Sprite Sprite1;
if (!Image1.LoadFromFile("images/RedPaddle.png"))
return EXIT_FAILURE;
Sprite1.SetImage(Image1);
Sprite1.Move(350, 750);
Paddle Paddle1;
Paddle1.Initialize(200, false, Image1);

while (App.IsOpened())
{
sf::Event Event;
while (App.GetEvent(Event))
{
if (Event.Type == sf::Event::Closed)
App.Close();
}
Paddle1.Update(App, Image1);
Paddle1.Draw(App);
App.Clear();
App.Display();
}
return 0;
}
[/CODE]
Paddle.hpp:
[CODE]
#pragma once
#include <SFML/Graphics.hpp>
class Paddle
{
public:
void Draw(sf::RenderWindow &Window);
void Update(sf::RenderWindow &Window, sf::Image PaddleImage);
void Initialize(float speedx, bool isplayercontrolled, sf::Image Image);
private:
sf::FloatRect Collision;
sf::Sprite PaddleSprite;
bool HasAI;
float speedx;
};
[/CODE]
Paddle.cpp:
[CODE]
#include "stdafx.h"
#include <SFML/Graphics.hpp>
#include "Paddle.h"

void Paddle::Initialize(float speedx, bool isplayercontrolled, sf::Image Image)
{
this->speedx = speedx;
HasAI = isplayercontrolled;
PaddleSprite.SetImage(Image);
}
void Paddle::Draw(sf::RenderWindow &Window)
{
Window.Draw(PaddleSprite);
}
void Paddle::Update(sf::RenderWindow &Window, sf::Image PaddleImage)
{
if (sf::Key::Left) PaddleSprite.Move(-speedx * Window.GetFrameTime(), 0);
else if (sf::Key::Right) PaddleSprite.Move(speedx * Window.GetFrameTime(), 0);
Collision.Top = PaddleSprite.GetPosition().y;
Collision.Left = PaddleSprite.GetPosition().x;
Collision.Bottom = Collision.Top + PaddleImage.GetHeight();
Collision.Right = Collision.Left + PaddleImage.GetWidth();
}
[/CODE] Edited by superman3275
0

Share this post


Link to post
Share on other sites
Backstory: This is my third attempt at pong! (Never Give Up -- I read 99% of people give up on programming their first game -- thats not going to be me!)
I haven't implemented collision yet, and this code *Should* be working, however I have a sneaking suspicion I declared something wrong.
0

Share this post


Link to post
Share on other sites
ok, i dont know SFML at all, but... heres some suggestions:
it looks like you draw the sprite using Sprite.Draw(app); then shortly after that you clear the screen
using App.Clear()

when programming, you have to follow lines in a logical step-by-step way:
if you draw something and THEN clear the screen, the result is a cleared screen!

so.. i think your solution is (i'm assuming App is a View, which is created from a window handle):
App.Clear();
App.Draw(sprite);
App.Display(); Edited by Kaptein
2

Share this post


Link to post
Share on other sites
I did what you said, and later found out it was a combination of me messing up with input and not doing what you said!!!! (Fixed) The only problem now is the image, which for some reason is appearing white, although I have a sneaking suspicion I didn't put a line of code in my Initialize() class that should be there. Thank You!
EDIT:
I fixed one problem which involved me not knowing the resolution (thought the width was the height and vice versa). But now I have another problem:
the Paddle shows up as white. Everything else works except it's white. I can promise you that it is, indeed, red. Any Ideas? Edited by superman3275
0

Share this post


Link to post
Share on other sites
[s]Screenshot?[/s]

[s]Is the paddle image showing up as an entirely white square, or is it still in the correct shape of the paddle but white? Or is it a single white pixel?[/s]

You are passing by value. An sf::Image holds the image, and sf::Sprite is used for drawing the image. The sf::Image must continue to exist, for as long as you intend to use the sf::Sprite (This is so multiple sf::Sprites can share the same sf::Image without wasting memory).

So what's the problem then? I'll comment your code to illustrate.
[code]sf::Sprite Sprite1; //Creating a sprite. Good.
if (!Image1.LoadFromFile("images/RedPaddle.png")) //Loading the image. Good.
return EXIT_FAILURE;
Sprite1.SetImage(Image1); //Setting the sprite to reference the image. Good, because the image continues to exist.

Paddle1.Initialize(200, false, Image1); //Passing the image into the Paddle... by value, not by reference. ***This copies the image***

void Paddle::Initialize(float speedx, bool isplayercontrolled, sf::Image Image) //'Image' is now a new copy.
{
this->speedx = speedx;
HasAI = isplayercontrolled;
PaddleSprite.SetImage(Image); //Setting the paddle's sprite to the image _copy_. This is fine, because the copy still exists.
} //Oh no! Our image copy went out of scope and got deleted! Our sprite now is referencing a deleted image![/code]

Solution: Pass the image into the Initialize() function by reference (which is actually referencing the original image), not by value (which creates a copy that then goes out of scope).

[code]void func(sf::Image Image) //Pass by value.
void func(sf::Image &Image) //Pass by reference.[/code]

If you haven't already, now would be a good time to look up [url="https://www.google.com/search?q=C%2B%2B+references&sugexp=chrome,mod=15&sourceid=chrome&ie=UTF-8"]C++ references[/url].

[hr]

One other thing that isn't related to your problem: Instead of a Initialize() function, use class constructors. They do almost the same thing, but have a common meaning and bring extra benefits as well, like enabling RAII. It's just better, safer, programming. [img]http://public.gamedev.net//public/style_emoticons/default/wink.png[/img]

Pong seems impossible now, but it's not Pong that's giving you difficulty, it's learning the language that's the hard part.
You are currently doing five things at once:[list=1]
[*]You are learning generalized programming concepts that apply to almost every language. (Computer science)
[*]You are learning multiple abstract programming paradigms. (Procedural programming, OOP programming, and RAII, as well as a number of others that are less visible but none-the-less present)
[*]You are learning the ins and outs of a specific language and its syntax. (C++)
[*]You are learning the ins and outs of a specific API, and its pros and cons. (SFML)
[*]You are making an interactive game. (Pong)
[/list]

Let me tell you something else: You're also doing a fantastic job at it, even if it doesn't seem like it. I can tell just from looking at your code, even though it may not be as good as an experienced programmer's code, I can see where you are learning good practices ahead of some other beginners at your level. You are doing excellently, so stick with it - I'm not just blowing false encouragement either.

Also, there's no shame in making Pong. Some things that seem foolish to people (because it's 'weak' or 'childish'), is actually the wiser thing to do. Edited by Servant of the Lord
2

Share this post


Link to post
Share on other sites
I'm getting an all new .pdf / book on memory management now, just so I'll understand this more. Thanks, the fix worked.
EDIT:
Just touched up and experimented, and you Can set an image = to another image! That makes using my classes functions way easier because there's less arguments! Edited by superman3275
0

Share this post


Link to post
Share on other sites
Yes, you can set an image to another image, but you have to make sure whatever copy you give to the sprite exists as long or longer than the sprite is being used for (or until you set the sprite to a different image).
0

Share this post


Link to post
Share on other sites
Oh Crap, another error message. Which makes no sense:
Main.cpp:
[CODE]
// Pong.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <SFML/Graphics.hpp>
#include "Paddle.h"
#include "Ball.h"

int _tmain(int argc, _TCHAR* argv[])
{
sf::RenderWindow App(sf::VideoMode(800,600,32), "Pong");
App.SetFramerateLimit(30);
sf::Image iBluePaddle;
sf::Image iRedPaddle;
if (!iRedPaddle.LoadFromFile("images/RedPaddle.png"))
return EXIT_FAILURE;
if (!iBluePaddle.LoadFromFile("images/BluePaddle.png"))
return EXIT_FAILURE;
Paddle PlayerPaddle(200, false, iRedPaddle);
Paddle AI_Paddle(200, true, iBluePaddle);

while (App.IsOpened())
{
sf::Event Event;
while (App.GetEvent(Event))
{
if (Event.Type == sf::Event::Closed)
App.Close();
}
AI_Paddle.Update(App);
PlayerPaddle.Update(App);
App.Clear();
PlayerPaddle.Draw(App);
AI_Paddle.Draw(App);
App.Display();
}
return 0;
}
[/CODE]
Paddle.h:
[CODE]
#pragma once
#include <SFML/Graphics.hpp>
#include "Ball.h"
class Paddle
{
public:
Paddle(float speedx, bool isplayercontrolled, sf::Image &Image);
void Draw(sf::RenderWindow &Window);
void Update(sf::RenderWindow &Window, Ball bally);
sf::FloatRect GetCollision();
private:
sf::Image PaddleImage;
sf::FloatRect Collision;
sf::Sprite PaddleSprite;
bool HasAI;
float speedx;
};
[/CODE]
Paddle.cpp:
[CODE]
#include "stdafx.h"
#include <SFML/Graphics.hpp>
#include "Paddle.h"

Paddle::Paddle(float speedx, bool isplayercontrolled, sf::Image &Image)
{
this->speedx = speedx;
HasAI = isplayercontrolled;
PaddleImage = Image;
PaddleSprite.SetImage(PaddleImage);
PaddleSprite.Move(350, 550);
}
void Paddle::Draw(sf::RenderWindow &Window)
{
Window.Draw(PaddleSprite);
}
void Paddle::Update(sf::RenderWindow &Window, Ball bally)
{
if (HasAI)
{
return;
}
if (Window.GetInput().IsKeyDown(sf::Key::Left)) PaddleSprite.Move(-speedx * Window.GetFrameTime(), 0);
else if (Window.GetInput().IsKeyDown(sf::Key::Right)) PaddleSprite.Move(speedx * Window.GetFrameTime(), 0);
Collision.Top = PaddleSprite.GetPosition().y;
Collision.Left = PaddleSprite.GetPosition().x;
Collision.Bottom = Collision.Top + PaddleImage.GetHeight();
Collision.Right = Collision.Left + PaddleImage.GetWidth();
}
sf::FloatRect Paddle::GetCollision()
{
return Collision;
}
[/CODE]
Ball.h:
[CODE]
#pragma once
#include <SFML/Graphics.hpp>
#include "Paddle.h"
class Ball
{
public:
Ball(float speedx, float speedy, sf::Image &BallImage);
void Update(sf::RenderWindow &Window);
void Draw(sf::RenderWindow &Window);
sf::FloatRect GetCollision();
void Test_For_Collision(sf::FloatRect LeftWall, sf::FloatRect RightWall, Paddle &TopPaddle, Paddle &BottomPaddle);
private:
sf::FloatRect Collision;
sf::Sprite BallSprite;
sf::Image BallImage;
float speedx, speedy;
};
[/CODE]
Ball.cpp:
[CODE]
#include "stdafx.h"
#include <SFML/Graphics.hpp>
#include "Ball.h"
Ball::Ball(float speedx, float speedy, sf::Image &BallImage)
{
this->speedx = speedx;
this->speedy = speedy;
this->BallImage = BallImage;
BallSprite.SetImage(this->BallImage);
}
void Ball::Update(sf::RenderWindow &Window)
{
BallSprite.Move(speedx * Window.GetFrameTime(), speedy * Window.GetFrameTime());
Collision.Top = BallSprite.GetPosition().y;
Collision.Left = BallSprite.GetPosition().x;
Collision.Bottom = Collision.Top + BallImage.GetHeight();
Collision.Right = Collision.Left + BallImage.GetWidth();
}
void Ball::Draw(sf::RenderWindow &Window)
{
Window.Draw(BallSprite);
}
sf::FloatRect Ball::GetCollision()
{
return Collision;
}
void Ball::Test_For_Collision(sf::FloatRect LeftWall, sf::FloatRect RightWall, Paddle &TopPaddle, Paddle &BottomPaddle)
{
if (GetCollision().Intersects(TopPaddle.GetCollision()))
{
speedy = -speedy;
}
if(GetCollision().Intersects(BottomPaddle.GetCollision()))
{
speedy = -speedy;
}
if (GetCollision().Intersects(LeftWall))
{
speedx = -speedx;
}
if (GetCollision().Intersects(RightWall))
{
speedx = -speedx;
}
}
[/CODE]
Error Message:
[CODE]
1>------ Build started: Project: Pong, Configuration: Debug Win32 ------
1> stdafx.cpp
1>c:\users om\documents\visual studio 2010\projects\pong\ball.h(12): error C2061: syntax error : identifier 'Paddle'
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
[/CODE]
When I use a Paddle in one of my member functions in the Ball class, I get an error message, while it works fine to use a ball in the paddle class. I have been searching Google and have no idea why my class isn't working. I'm pretty much thinking of stuff that might be causing it and experimenting (As I've been for about 45 minutes now).
Sorry about asking you guys for SO MUCH help.
0

Share this post


Link to post
Share on other sites
@servant of the lord:
wow perfect and in depth explanation. I had this problem once too and I was pulling my hair out until I figured this out. [s]OP why don't you give pre-compiled headers a try they not only make compilation faster but also makes managing header files easier.[/s]
EDIT: it seems you are already using precompiled headers. But you are not using them correctly. Include the headers in the stdafx.h and just include this file in your other files. This way you won't have to include Paddle.h in Ball.h and vice versa. Just include "stdafx.h" and you're set. Edited by frazchaudhry
0

Share this post


Link to post
Share on other sites
forward declares makes your life so much easier :)
if you google the subject you can see all sorts of ways to use them, and it will give you many more options when you are designing your application
there was indeed a time when i didn't know about them :) trying to sew together an application without them was a nightmare..
it compiled fast! but.. it was a real nightmare, and thank god someone told me about forward declares.. i use it on an as-needed basis!
0

Share this post


Link to post
Share on other sites
OP, not directly related to your question, but I highly suggest you start from the sample code in SFML package.
It's guaranteed working code (other wise, poke SFML author for a bug report :) ), and what you need to do is to understand the code, then re-structure it for your need, add more game plays, etc.
There is a Pong sample code and I tested it works very well.
0

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0