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

Show Posts

This section allows you to view all posts made by this member. Note that you can only see posts made in areas you currently have access to.


Messages - DylanMorgan

Pages: [1] 2
1
Graphics / Re: Trying to create a graphics engine
« on: November 21, 2017, 06:28:52 pm »
Wow thank you, didn't notice I was doing that :)

2
Graphics / Trying to create a graphics engine
« on: November 20, 2017, 09:49:23 pm »
Okay, so for my new development idea I want to get a solid base before I start working on the game itself. This means I want to set up a graphics engine, sound handler, event handler and logic engine. As I have had a really long break from coding, I am extremely rusty. I was wondering whether using sf::Drawable is going to benefit me in my engine. I can't seem to get it to work right now, these errors are repeated for different lines of code:

Quote
1>d:\program files (x86)\microsoft visual studio\2017\community\vc\tools\msvc\14.10.25017\include\xmemory0(774): error C2528: 'pointer': pointer to reference is illegal
1>d:\program files (x86)\microsoft visual studio\2017\community\vc\tools\msvc\14.10.25017\include\xmemory0(984): note: see reference to class template instantiation 'std::allocator<_Ty>' being compiled
1>        with
1>        [
1>            _Ty=sf::Drawable &
1>        ]

This is my code:

Code: [Select]
#ifndef GRAPHICS_ENGINE
#define GRAPHICS_ENGINE

class GraphicsEngine
{
public:
GraphicsEngine();
~GraphicsEngine();

int addToVector(sf::Drawable& drawableObject);
void deleteFromVector(int objectNumber);

void callObjectsDraw(sf::RenderWindow& window);

private:
std::vector<sf::Drawable&> vectorOfDrawableObjects;
};

#endif

Code: [Select]
#include "stdafx.h"
#include "graphicsEngine.h"

GraphicsEngine::GraphicsEngine()
{
}

GraphicsEngine::~GraphicsEngine()
{
}

int GraphicsEngine::addToVector(sf::Drawable& drawableObject)
{
vectorOfDrawableObjects.push_back(drawableObject);
int objectNumber = vectorOfDrawableObjects.size();

return objectNumber;
}

void GraphicsEngine::deleteFromVector(int objectNumber)
{
vectorOfDrawableObjects.erase(vectorOfDrawableObjects.begin() + objectNumber);
}

void GraphicsEngine::callObjectsDraw(sf::RenderWindow& window)
{
for (int i = 0; i < vectorOfDrawableObjects.size(); i++)
{
window.draw(vectorOfDrawableObjects[i]);
}
}

If you can see I have made some basic error and don't want to explain it, I would appreciate a point in the right direction to where I can read up on the theroy behind it :)

3
Graphics / Re: Creating a tilemap
« on: February 20, 2017, 02:11:35 pm »
I've edited the code to show the bits I don't understand in the comments :)

4
Graphics / Creating a tilemap
« on: February 19, 2017, 09:53:41 pm »
Hey guys, I am trying to create a game which uses a tile map to decide how the level loops. where '1,1' is a white square, and if it is 0,0 then nothing is created. For collision purposes I decided to make an object for each of these squares (don't know if this is stupid or not) then each white square will be displayed on the screen. These white squares are called 'levelSurface.h' and I have a vector holding copies of these surfaces inside 'levelhandler'

I found some code on a website and tried to understand how it works and implement it. However, I still do not understand how this works!

This is my levelHandler.cpp which has the code with the mapping in them. Please can someone explain whats going on here :) I have tried a few things so I am not sure if they are good or not.
#include "Stdafx.h"
#include "levelHandler.h"

LevelHandler::LevelHandler()
{
        if (!texture.loadFromFile("resources/tilemap.png"));

        std::ifstream openfile("resources/level1.txt");

        while (!openfile.eof())
        {
                std::string str;
                openfile >> str;
                char x = str[0], y = str[2];

                if (isdigit(x) || !isdigit(y))
                {
                        // This is meant to push back and object each time there is a digit right.
                        vectorOfSurfaces.push_back(LevelSurface(texture));
                }

                if (openfile.peek() == '\n')
                {
                        loadCounter.x = 0;
                        loadCounter.y++;
                }
                else
                {
                        loadCounter.x++;
                }
        }

        loadCounter = sf::Vector2i(0, 0);
}

LevelHandler::~LevelHandler()
{
}

std::vector<LevelSurface>& LevelHandler::getVector()
{
        return vectorOfSurfaces;
}

void LevelHandler::loadMap()
{
        std::ifstream openfile(location);

        // Load text file into map vector
        while (!openfile.eof())
        {
                std::string str;
                openfile >> str;
                char x = str[0], y = str[2];
                if (!isdigit(x) || !isdigit(y))
                {
                        // No clue what this does
                        map[loadCounter.x][loadCounter.y] = sf::Vector2i(-1, -1);
                }
                else
                {
                        // when i change the numbers in '' then the whole output changes, so what does this do?
                        map[loadCounter.x][loadCounter.y] = sf::Vector2i(x - '2', y - '2');
                }

                if (openfile.peek() == '\n')
                {
                        loadCounter.x = 0;
                        loadCounter.y++;
                }
                else
                {
                        loadCounter.x++;
                }
        }
}

void LevelHandler::addObjectToVector(RenderEngine& renderEngine)
{
        for (int i = 0; i < loadCounter.x; i++)
        {
                for (int j = 0; j < loadCounter.y; j++)
                {
                        if (map[i][j].x != -1 && map[i][j].y != -1)
                        {
                                for (unsigned int number = 1; number < vectorOfSurfaces.size(); number++)
                                {
                                        vectorOfSurfaces[i].getSprite().setPosition(i * 32, j * 32);
                                        vectorOfSurfaces[i].getSprite().setTextureRect(sf::IntRect(map[i][j].x * 32, map[i][j].y * 32, 32, 32));
                                        renderEngine.addObjectToVector(vectorOfSurfaces[i]);
                                }
                        }
                }
        }
}

I have attached the output screen.

5
Window / Re: Making a sf::RenderWindow
« on: February 17, 2017, 06:27:08 pm »
Thank you for the replys guys :) I decided to use the window.create() function

6
Window / Making a sf::RenderWindow
« on: February 16, 2017, 08:28:19 pm »
Hey guys, so I have a game class where I have the private variable
sf::RenderWindow window;
I want to set the windows properties in the constructor of game and then so I can use the window throughout the game class. I will use a getWindow function if I do need to use the window elsewhere. However, I do not understand how to create the window outside of me defining it. I don't know if it is a simple mistake or... Any help is appreciated!

7
General / Re: Cross Reference I think
« on: February 12, 2017, 05:20:50 pm »
Okay thanks guys!

8
General / Re: Cross Reference I think
« on: February 11, 2017, 08:01:20 pm »
Quote
You try to use something called "Block" in containerOfBlocks.h. The compiler has no idea what "Block" is and complains. Now, you include block.h, which defines what "Block" is, in containerOfBlocks.h and the compiler is happy. You really don't understand why that fixed the problem? ;)

No I don't because I thought it was bad practice to include .h file inside a .h file. The block.h was included before containerOfBlocks.h so I thought the compiler would understand?

9
General / Re: Cross Reference I think
« on: February 11, 2017, 05:22:28 pm »
Okay, I included "block.h" in containerOfBlocks.h but i don't see why this fixed my problem. Can someone explain?

10
General / Cross Reference I think
« on: February 11, 2017, 04:01:14 pm »
Quote
error C2065: 'Block' : undeclared identifier   c:\users\dylan\documents\visual studio 2013\projects\pong\containerofblocks.h   10   1   Pong
error C2923: 'std::vector' : 'Block' is not a valid template type argument for parameter '_Ty'   c:\users\dylan\documents\visual studio 2013\projects\pong\containerofblocks.h   10   1   Pong
error C2065: 'Block' : undeclared identifier   c:\users\dylan\documents\visual studio 2013\projects\pong\containerofblocks.h   17   1   Pong
error C2923: 'std::vector' : 'Block' is not a valid template type argument for parameter '_Ty'   c:\users\dylan\documents\visual studio 2013\projects\pong\containerofblocks.h   17   1   Pong

Hey guys, these are my errors. I am not quite sure what code to include. So I will include anything linked the containerOfBlocks. The problem arose when I
#include "containterOfBlocks.h"
inside the collisionHandler class I made.

collisionHandler.h
#include "Stdafx.h"
#ifndef COLISSION_HANDLER
#define COLISSION_HANDLER

class ColisionHandler
{
public:
        ColisionHandler();
        ~ColisionHandler();

        void hasBallHitSides(Ball& ball, int windowWidth);
        void hasBallHitTop(Ball& ball);
        void hasBallHitPaddle(Ball& ball, Paddle& paddle);

        void hasPaddleHitSides(Paddle& paddle, int windowWidth);
        void HasBallHitRow(Ball& ball, ContainerOfBlocks& blockContainer, SoundHandler& soundHandler);
};

#endif

collisionHandler.cpp
#include "Stdafx.h"
#include "containerOfBlocks.h"
#include "ball.h"
#include "paddle.h"
#include "soundHandler.h"
#include "colisionHandler.h"

ColisionHandler::ColisionHandler()
{
}

ColisionHandler::~ColisionHandler()
{
}

block.cpp
#include "Stdafx.h"
#include "block.h"

Block::Block(float startX, float startY)
{
        position.x = startX;
        position.y = startY;

        colour = sf::Color::White;

        block.setSize(sf::Vector2f(width, height));
        block.setFillColor(colour);
}

Block::~Block()
{
}

sf::RectangleShape Block::getBlock()
{
        return block;
}

sf::FloatRect Block::getPosition()
{
        return block.getGlobalBounds();
}

void Block::draw(sf::RenderWindow& window)
{
        window.draw(block);
}

void Block::setPosition(int yPosition)
{
        position.y = yPosition;

        block.setPosition(position);
}

void Block::setColour(sf::Color tempColour)
{
        colour = tempColour;

        block.setFillColor(colour);
}

containerOfBlocks.h
#ifndef CONTAINER_OF_BLOCKS
#define CONTAINER_OF_BLOCKS

class ContainerOfBlocks
{
public:
        ContainerOfBlocks(int yPosition, sf::Color colour);
        ~ContainerOfBlocks();

        std::vector<Block>& getContainer();
        size_t getSize();
       
        void drawContainer(sf::RenderWindow& window);
        void deleteObjectFromVector(int objectNumber);

private:
        std::vector<Block> blockContainer;
};

#endif

containerOfBlocks.cpp
#include "Stdafx.h"
#include "block.h"
#include "containerOfBlocks.h"

ContainerOfBlocks::ContainerOfBlocks(int yPosition, sf::Color colour)
{
        blockContainer.push_back(Block(2, 2));
        blockContainer.push_back(Block(104, 2));
        blockContainer.push_back(Block(206, 2));
        blockContainer.push_back(Block(308, 2));
        blockContainer.push_back(Block(410, 2));
        blockContainer.push_back(Block(512, 2));
        blockContainer.push_back(Block(614, 2));
        blockContainer.push_back(Block(716, 2));
        blockContainer.push_back(Block(818, 2));
        blockContainer.push_back(Block(920, 2));

        for (unsigned int i = 0; i < 10; ++i)
        {
                blockContainer[i].setPosition(yPosition);
        }

        for (unsigned int i = 0; i < 10; ++i)
        {
                blockContainer[i].setColour(colour);
        }
}

ContainerOfBlocks::~ContainerOfBlocks()
{
}

std::vector<Block>& ContainerOfBlocks::getContainer()
{
        return blockContainer;
}

void ContainerOfBlocks::drawContainer(sf::RenderWindow& window)
{
        for (unsigned int i = 0; i < blockContainer.size(); ++i)
        {
                blockContainer[i].draw(window);
        }
}

void ContainerOfBlocks::deleteObjectFromVector(int objectNumber)
{
        blockContainer.erase(blockContainer.begin() + objectNumber);
}

size_t ContainerOfBlocks::getSize()
{
        return blockContainer.size();
}

I know this is a lot of code guys. But I appreciate any sort of help :) sorry I do not have a clue what has gone on!

11
Graphics / Re: Problem with the draw & display methods
« on: February 04, 2017, 10:55:36 pm »
Don't know if this is any help. Or if it even matters. But you haven't declared anything for your sprite? Position, size, etc.

12
Graphics / Re: Moving a ball with FPS
« on: February 01, 2017, 08:58:41 pm »
Quote
The important part to keep it at consistent speed is the time - multiplying any motion by the time passed. Notice in eXpl0it3r's example that the velocity was multiplied by dt (delta time - time taking since the previous frame).

How do I get this dt variable?

13
Graphics / Re: Moving a ball with FPS
« on: February 01, 2017, 06:25:00 pm »
#include "Stdafx.h"
#include "ball.h"

Ball::Ball(int startPositionY, int startPositionX)
{
        position.x = startPositionX;
        position.y = startPositionY;

        startingPosition = position;

        colour = sf::Color::White;

        ball.setRadius(radius);
        ball.setFillColor(colour);
        ball.setPosition(position);
}

Ball::~Ball()
{
}

sf::CircleShape Ball::getBall()
{
        return ball;
}

void Ball::setVelocity(int tempFrameRateX, int tempFrameRateY)
{
        frameRateX = tempFrameRateX;
        frameRateY = tempFrameRateY;
}

float Ball::getVelocityX()
{
        return xVelocity;
}

void Ball::reboundSides()
{
        xVelocity = -xVelocity;
}

void Ball::reboundSurface()
{
        position.y -= (yVelocity * frameRateY);
        yVelocity = -yVelocity;
}

void Ball::update()
{
        // Update the ball position variables
        position.y += yVelocity;
        position.x += xVelocity;

        // Move the ball
        ball.setPosition(position);
}

void Ball::hitBottom()
{
        position.x = startingPosition.x;
        position.y = startingPosition.y;
}

sf::FloatRect Ball::getPosition()
{
        return ball.getGlobalBounds();
}

I edited my ball.cpp to include the velocity and it works, however if I take off vertical sync. When the frame-rate is really high, then the ball moves really fast. It is not that constant speed no matter the fps

14
Graphics / Re: Moving a ball with FPS
« on: February 01, 2017, 06:18:26 pm »
Quote
The simplest solution is to do:
while(window.isOpen()) {
sf::Time dt = clock.restart();
// ...
pos += velocity * dt.asSeconds();
 

So would I put that function inside the ball class or? I am not sure how I would put this into the programme itself

15
Graphics / Moving a ball with FPS
« on: February 01, 2017, 05:32:26 pm »
Hey guys, I really don't have a clue how to do this. I have an fps counter in the main loop using Sf::Clock and Sf::Time. But I do not know how to make it so my ball will move at a constant rate no matter the frame rate. Can someone point me in the right direction and give me a bit of advice on how to go about this?

ball.cpp
#include "Stdafx.h"
#include "ball.h"

Ball::Ball(int startPositionY, int startPositionX)
{
        position.x = startPositionX;
        position.y = startPositionY;

        startingPosition = position;

        colour = sf::Color::White;

        ball.setRadius(radius);
        ball.setFillColor(colour);
        ball.setPosition(position);
}

Ball::~Ball()
{
}

sf::CircleShape Ball::getBall()
{
        return ball;
}

void Ball::setVelocity(int tempVelocityX, int tempVelocityY)
{
        xVelocity = tempVelocityX;
        yVelocity = tempVelocityY;
}

float Ball::getVelocityX()
{
        return xVelocity;
}

void Ball::reboundSides()
{
        xVelocity = -xVelocity;
}

void Ball::reboundSurface()
{
        position.y -= (yVelocity * 50);
        yVelocity = -yVelocity;
}

void Ball::update()
{
        // Update the ball position variables
        position.y += yVelocity;
        position.x += xVelocity;

        // Move the ball
        ball.setPosition(position);
}

void Ball::hitBottom()
{
        position.x = startingPosition.x;
        position.y = startingPosition.y;
}

sf::FloatRect Ball::getPosition()
{
        return ball.getGlobalBounds();
}

the part of main.cpp that involves ball

                sf::Time time = clock.getElapsedTime();
                std::cout << 1.0f / time.asSeconds() << std::endl;

                // Handle ball hitting the bottom
                if (ball.getPosition().top > windowHeight)
                {
                        // reverse the ball direction
                        ball.hitBottom();
                }

                // Handle ball hitting top
                if (ball.getPosition().top < 0)
                {
                        ball.reboundSurface();
                }

                // Handle ball hitting sides
                if (ball.getPosition().left < 0 || ball.getPosition().left + 10 > windowWidth)
                {
                        ball.reboundSides();
                }

                // Has the ball hit the bat?
                if (ball.getPosition().intersects(paddle.getPosition()))
                {
                        // Hit detected so reverse the ball
                        ball.reboundSurface();
                }

                // Has the ball hit a block in the 1st row?
                for (unsigned int i = 0; i < blockContainer.getSize(); i++)
                {
                        if (ball.getPosition().intersects(blockContainer.getContainer()[i].getPosition()))
                        {
                                // testing
                                std::cout << "Hit 1st row, block: " << i << std::endl;

                                // Reverse ball
                                ball.reboundSurface();

                                // Delete block
                                blockContainer.deleteObjectFromVector(i);
                                i--;

                                // Play sound effect
                                sound.play();
                                break;
                        }
                }
               
                // Has the ball hit a block in the 2nd row?
                for (unsigned int i = 0; i < blockContainer2.getSize(); i++)
                {
                        if (ball.getPosition().intersects(blockContainer2.getContainer()[i].getPosition()))
                        {
                                // testing
                                std::cout << "Hit 2nd row, block: " << i << std::endl;

                                // Reverse ball
                                ball.reboundSurface();

                                // Delete Block
                                blockContainer2.deleteObjectFromVector(i);
                                i--;

                                // Play sound effect
                                sound.play();
                                break;
                        }
                }

                ball.setVelocity(time.asSeconds(), time.asSeconds());
                ball.update();

                window.clear();

                // draw objects
                window.draw(paddle.getPaddle());
                window.draw(ball.getBall());

                blockContainer.drawContainer(window);
                blockContainer2.drawContainer(window);

                // Reset clock
                clock.restart().asSeconds();

                window.display();
        }

I don't really understand how to do this. I have made a guess but it doesn't seem to work. The ball shows on screen but is still.

Pages: [1] 2