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.


Topics - Canvas

Pages: [1] 2 3
1
General / Static class my map of sounds gets de-allocated
« on: March 14, 2015, 03:10:28 pm »
Hello there,

Let me first off show you my static sound manager class

header file

#pragma once

#include "SFML/Audio.hpp"
#include "Enums.h"
#include <map>
#include <vector>

static class SoundLoader
{
public:
        SoundLoader();
        ~SoundLoader();

        void LoadSounds(GameStates state);
        void PlaySound(SoundNames soundName);

        std::map<SoundNames, sf::SoundBuffer> Sounds;
        std::vector<sf::Sound> playingSounds;

        float volume;
};

Class file

#include "SoundLoader.h"

SoundLoader::SoundLoader()
{
        volume = 100;
}

SoundLoader::~SoundLoader()
{

}

void SoundLoader::LoadSounds(GameStates gameState)
{
        Sounds[SoundNames::Explosion1].loadFromFile("Assets/Sounds/Explosion1.wav");
}

void SoundLoader::PlaySound(SoundNames soundName)
{
        std::cout << std::to_string(playingSounds.size()) << std::endl;

        if (playingSounds.size() == 0)
        {
                playingSounds.push_back(sf::Sound());
                playingSounds.at(0).setVolume(volume);
                playingSounds.at(0).setBuffer(Sounds[soundName]);
                playingSounds.at(0).play();
        }
        else
        {
                int location = -1;
                for (int i = 0; i < playingSounds.size(); i++)
                {
                        if (playingSounds.at(i).getStatus() != sf::Sound::Playing && location == -1)
                        {
                                location = i;
                        }
                }

                if (location != -1)
                {
                        playingSounds.at(0).setVolume(volume);
                        playingSounds.at(location).setBuffer(Sounds[soundName]);
                        playingSounds.at(location).play();
                }
                else
                {
                        playingSounds.push_back(sf::Sound());
                        playingSounds.at(playingSounds.size() - 1).setVolume(volume);
                        playingSounds.at(playingSounds.size() - 1).setBuffer(Sounds[soundName]);
                        playingSounds.at(playingSounds.size() - 1).play();
                }

        }
}

I understand that files will of course be destroyed if they go out of scope e.g. the function is finished. but as I have a public map the sound that is loaded shouldn't be de-allocated

When I call LoadSounds() my Sounds have a soundbuffer populated. Then when I go ahead and call PlaySound with the correct enum name, I viewed my Sounds map and the size is 0, can anyone see what I am doing wrong here? Also I am declaring my soundloader like so in my header files

game.h file
SoundLoader soundLoader;

Basically I want to have only one soundLoader which my other classes can just define a static soundLoader which will access the soundLoader which is initialized at the start of the game.

2
General / Game resolution dilemma
« on: January 28, 2015, 12:56:45 pm »
Hello there,

I'm having a bit of a hard time with this one, basically I have myself a game which at the moment is perfect for 1024 by 768, now when I made the game full screen some of my graphics start to float because I am using a view, other graphics just stretch out. However this is a huge issue for me.

First off is it ok to code a game to work with just one resolution I.E. the game is coded for 800:600 ratio 4:3 so if the user makes the window taller or wider the ratio will keep stuff in place and stretch to fit the ratio,

Second is there a better way or is that the best way?

I'm want to try and create a game this year and release it to the public (fingers crosses), however the whole idea of different resolutions is quite scary, can anyone help me out understand how I can get around this or how to deal with such a problem.

Cheers for reading

3
General / Scrolling background loop however produces little gaps
« on: January 27, 2015, 11:38:12 pm »
I have myself a parallaxing background, i'm using SFML to render my background, I have myself 3 sf::Sprites, each sprite has the width of 576, I create myself 3 sprites with x positions 0, 576 and 1152. I then have some if statements that will move the background sprites when they go so far off the screen however... small gaps keep appearing, here is my code for my background manager

void BackgroundManager::Update(int playerXVel)
{
//Update level backgrounds
for (int i = 0; i < 3; i++)
{
    sf::Vector2f postion = levelBackgroundSprites[i].getPosition();
    levelBackgroundSprites[i].setPosition(postion.x += playerXVel, postion.y);

    if (postion.x <= -576)
    {
        levelBackgroundSprites[i].setPosition(1152, postion.y);
    }
    else if (postion.x >= 1152)
    {
        levelBackgroundSprites[i].setPosition(-576, postion.y);
    }
}
}
 

I have attached a screenshot

4
General / Memory Violation odd error with sf::Sprite
« on: January 11, 2015, 08:03:15 pm »
Hello there,

I have myself a ImageLoader which stores a vector of sf::Textures. now here is the odd problem I am having, I have myself a class known as UserInterface.

Here is the header file
#pragma once

#include "SFML\Graphics.hpp"
#include "ImageLoader.h"
#include <vector>

class UserInterface
{
private:
        ImageLoader& _imageLoader;
public:
        int SelectedPower;
        int Powers[9];
        sf::Sprite Bar;
        std::vector<sf::Sprite> PowerUpIcons;
        sf::Sprite PowerSelected;

        UserInterface(ImageLoader& imageLoader);
        ~UserInterface();

        void Setup();
        void SetupPowerUps();
        void Draw(sf::RenderWindow& window);
        void Update();
        void AddPower();
};

Here is the cpp file
#include "UserInterface.h"

UserInterface::UserInterface(ImageLoader& imageLoader) :
_imageLoader(imageLoader)
{
        SelectedPower = 0;
        Powers[1] = 0;
        Powers[2] = 0;
        Powers[3] = 0;
        Powers[4] = 0;
        Powers[5] = 0;
        Powers[6] = 0;
        Powers[7] = 0;
        Powers[8] = 0;
        Powers[9] = 0;
}

UserInterface::~UserInterface()
{

}

void UserInterface::Setup()
{
        Bar.setTexture(_imageLoader.Images[ImageNames::UI_BottomBar]);
        Bar.setPosition(0, 675);

        PowerSelected.setTexture(_imageLoader.Images[ImageNames::UI_Power_Selected]);
        PowerSelected.setPosition(-100, -100);

        SetupPowerUps();
}

void UserInterface::SetupPowerUps()
{
        for (int i = 0; i < 9; i++)
        {
                sf::Sprite newSprite;
                switch (i)
                {
                case 0: newSprite.setTexture(_imageLoader.Images[ImageNames::UI_Power_Up1]); break;
                case 1: newSprite.setTexture(_imageLoader.Images[ImageNames::UI_Power_Up2]); break;
                case 2: newSprite.setTexture(_imageLoader.Images[ImageNames::UI_Power_Up3]); break;
                case 3: newSprite.setTexture(_imageLoader.Images[ImageNames::UI_Power_Up4]); break;
                case 4: newSprite.setTexture(_imageLoader.Images[ImageNames::UI_Power_Up5]); break;
                case 5: newSprite.setTexture(_imageLoader.Images[ImageNames::UI_Power_Up6]); break;
                case 6: newSprite.setTexture(_imageLoader.Images[ImageNames::UI_Power_Up7]); break;
                case 7: newSprite.setTexture(_imageLoader.Images[ImageNames::UI_Power_Up8]); break;
                case 8: newSprite.setTexture(_imageLoader.Images[ImageNames::UI_Power_Up9]); break;
                }
               
                newSprite.setPosition(328 + (i * 41), 700);
                newSprite.setTextureRect(sf::IntRect(0, 0, 38, 38));
                PowerUpIcons.push_back(newSprite);
        }
}

void UserInterface::Update()
{
        PowerSelected.setPosition(328 + ( (SelectedPower - 1) * 41), 700);
}

void UserInterface::Draw(sf::RenderWindow& window)
{
        sf::View tempView(sf::FloatRect(0, 0, window.getSize().x, window.getSize().y));
        window.setView(tempView);

        //Bar.setTexture(_imageLoader.Images[ImageNames::UI_Power_Selected]);
        //Bar.setPosition(0, 640);

        window.draw(Bar);

        for (int i = 0; i < PowerUpIcons.size(); i++)
        {
                window.draw(PowerUpIcons.at(i));
        }

        window.draw(PowerSelected);
}

void UserInterface::AddPower()
{
        SelectedPower++;
}

Now basically if you look at my Setup function it sets up the Bar sf::Sprite and sets up the PowerSelected sf::Sprite, it sets the texture and also sets the position, now in my Draw function, it sets up a view and then draws out the bar, my powers and powerselected sprite. however when it attempts to draw Bar it comes up with this error

Unhandled exception at 0x0F1BE57E (sfml-graphics-d-2.dll) in ColourGalaxy.exe: 0xC0000005: Access violation reading location 0x00000004.
this error is thrown in function
void RenderTarget::draw(const Drawable& drawable, const RenderStates& states)
{
    drawable.draw(*this, states);
}
Look at the image attached to see my error.

Now here is the bit that confuses me. If I comment out my window.draw(Bar); everything works fine. so if though I am setting up my PowerSelected sf::Sprite in the same function as the Bar variable. PowerSelected has no problem at all being drawn. Can anyone help me understand why my Bar variable is causing the memory violation?

5
General / Which side of my bounding box hit
« on: October 20, 2014, 09:45:56 pm »
Hello there

I'm having quite some trouble with my collision detection,
First off here is my collision detection method

CollisionSide MathsService::RectToRectCollisionName(int rect1_x, int rect1_y, int rect1_w, int rect1_h, int rect2_x, int rect2_y, int rect2_w, int rect2_h)
{
        bool collision = false;
        // top-left corner
        if (PointInRect(rect1_x, rect1_y, rect2_x, rect2_y, rect2_w, rect2_h)){ collision = true; }
        // top-right corner
        if (PointInRect(rect1_x + rect1_w - 1, rect1_y, rect2_x, rect2_y, rect2_w, rect2_h)){ collision = true; }
        // bottom-right corner
        if (PointInRect(rect1_x + rect1_w - 1, rect1_y + rect1_h - 1, rect2_x, rect2_y, rect2_w, rect2_h)){ collision = true; }
        // bottom-left corner
        if (PointInRect(rect1_x, rect1_y + rect1_h - 1, rect2_x, rect2_y, rect2_w, rect2_h)){ collision = true; }
        // Check to see if rectangle 2 is hit any part of rectanlge 1
        // top-left corner
        if (PointInRect(rect2_x, rect2_y, rect1_x, rect1_y, rect1_w, rect1_h)){ collision = true; }
        // top-right corner
        if (PointInRect(rect2_x + rect2_w - 1, rect2_y, rect1_x, rect1_y, rect1_w, rect1_h)){ collision = true; }
        // bottom-right corner
        if (PointInRect(rect2_x + rect2_w - 1, rect2_y + rect2_h - 1, rect1_x, rect1_y, rect1_w, rect1_h)){ collision = true; }
        // bottom-left corner
        if (PointInRect(rect2_x, rect2_y + rect2_h - 1, rect1_x, rect1_y, rect1_w, rect1_h)){ collision = true; }
        // If there is no collision
        if (collision == true)
        {
                int rect1xcentre = rect1_x + (rect1_w / 2);
                int rect2xcentre = rect2_x + (rect2_w / 2);

                int rect1ycentre = rect1_y + (rect1_h / 2);
                int rect2ycentre = rect2_y + (rect2_h / 2);

                int     xDifference = rect1xcentre - rect2xcentre;
                int yDifference = rect1ycentre - rect2ycentre;

                int xValue = xDifference < 0 ? xDifference * -1 : xDifference;
                int yValue = yDifference < 0 ? yDifference * -1 : yDifference;

                if (xValue > yValue)
                {
                        CollisionSide result;
                        xDifference < 0 ? result = CollisionSide::Left : result = CollisionSide::Right;
                        return result;
                }

                if (yValue > xValue)
                {
                        CollisionSide result;
                        yDifference < 0 ? result = CollisionSide::Top : result = CollisionSide::Bottom;
                        return result;
                }

                return CollisionSide::None;
        }
        else
        {
                return CollisionSide::None;
        }
}
 

This method checks on two rectangles, if they are colliding a side will be return't, however I am getting some odd results.

I have a player which is a 64 by 64 image which can be moving downwards at a total of 16 pixels per tick. Now usually when it hits a block on the floor it will return my enum "Bottom" and the ball will bounce correctly, however now and then the ball get's stuck inside of the block and will go left, right or down. How can I stop this from happening? Do I need to check for a collision 1 tick in front and pre-empt it?

Basically my code to check if the player hits a block is like so

void LevelState::CheckPlayerCollisions()
{
        bool collision = false;
        for (int i = 0; i < DisplayObjects.size(); i++)
        {
                if (_mathsService.RectToRectCollision(
                        DisplayObjects.at(i).XPos,
                        DisplayObjects.at(i).YPos,
                        DisplayObjects.at(i).Sprite.getTextureRect().width,
                        DisplayObjects.at(i).Sprite.getTextureRect().height,
                        Player.XPos - (Player.Sprite.getTextureRect().width / 2),
                        Player.YPos - (Player.Sprite.getTextureRect().height / 2),
                        Player.Sprite.getTextureRect().width,
                        Player.Sprite.getTextureRect().height
                        ))
                {
                        //Work out the force to bound the ball
                        CollisionSide result = _mathsService.RectToRectCollisionName(
                                DisplayObjects.at(i).XPos,
                                DisplayObjects.at(i).YPos,
                                DisplayObjects.at(i).Sprite.getTextureRect().width,
                                DisplayObjects.at(i).Sprite.getTextureRect().height,
                                Player.XPos - (Player.Sprite.getTextureRect().width / 2),
                                Player.YPos - (Player.Sprite.getTextureRect().height / 2),
                                Player.Sprite.getTextureRect().width,
                                Player.Sprite.getTextureRect().height
                                );


                        int force = ceil(Player.YPos / 42);
                        if (force > 16)
                        {
                                force = 16;
                        }
                        std::cout << "Force feed back : " << force << std::endl;
                        Player.HitObjectUpdate(force, result, DisplayObjects.at(i).YPos);
                        collision = true;
                }

                if (collision)
                {
                        break;
                }
        }
}
 

The method above is being working on of course.

If anyone can give me a hand that would be awesome, I can profile screenshots and pictures if needed.

6
General / Vector of custom class, how to populate?
« on: October 09, 2014, 09:26:47 pm »
Hello there

I have myself a custom TextObject class and I have myself a GameState class which has a vector of TextObjects, so basically this is how I am populating my vector

TextObject textObject;
        TextObjects.push_back(textObject);
        TextObjects.push_back(textObject);
        TextObjects.at(0).Setup(200, 350, "Score : " + std::to_string(Score), 24, fontLoader.Fonts[FontNames::Main_Font]);
        TextObjects.at(0).CentreText();
        TextObjects.at(1).Setup(200, 100, "Game Over", 48, fontLoader.Fonts[FontNames::Main_Font]);
        TextObjects.at(1).SetRotationAnimation(-12, 12, 0.1);
       

To me this doesn't look quite right but it does work, but I have myself in a pickle, sometimes the player may get a new highscore and if they do I want to show some more text, so I have my code like so

TextObject textObject;
        TextObjects.push_back(textObject);
        TextObjects.push_back(textObject);
        TextObjects.at(0).Setup(200, 350, "Score : " + std::to_string(Score), 24, fontLoader.Fonts[FontNames::Main_Font]);
        TextObjects.at(0).CentreText();
        TextObjects.at(1).Setup(200, 100, "Game Over", 48, fontLoader.Fonts[FontNames::Main_Font]);
        TextObjects.at(1).SetRotationAnimation(-12, 12, 0.1);
       
        if (highScore == true)
        {
                TextObject txtObject;
                TextObjects.push_back(txtObject);
                TextObjects.at(2).Setup(200, 3, "New high score!", 24, fontLoader.Fonts[FontNames::Main_Font]);
        }

But when I go to render my third text item in my vector I get this error "Unhandled exception at 0x5361DCE2 (sfml-graphics-d-2.dll) in SpaceDestroyer.exe: 0xC0000005: Access violation reading location 0xCDCDCDD0." in xtree "_Nodeptr _Pnode = _Root();"

My TextObject has a standard constructor so even If I try

if (highScore == true)
        {
                TextObjects.push_back(TextObject());
                TextObjects.at(2).Setup(200, 3, "New high score!", 24, fontLoader.Fonts[FontNames::Main_Font]);
        }

I still get the error, what is the correct way for my to populate my vector? or is there a better way to store objects than using a vector?

7
Audio / Internal OpenAL error
« on: October 08, 2014, 10:29:21 pm »
Hello there,

I have myself a SoundLoader class which loads some wav files into a map of soundbuffers and then I can call a method called PlaySound which takes an enum to play the sound, here is my method

if (playingSounds.size() == 0)
        {
                playingSounds.push_back(sf::Sound());
                playingSounds.at(0).setBuffer(Sounds[soundName]);
                playingSounds.at(0).play();
        }
        else
        {
                int location = -1;
                for (int i = 0; i < playingSounds.size(); i++)
                {
                        if (!playingSounds.at(i).getStatus() == sf::Sound::Playing)
                        {
                                location = i;
                        }
                       
                        if (location != -1)
                        {
                                break;
                        }
                }

                if (location != -1)
                {
                        playingSounds.at(location).setBuffer(Sounds[soundName]);
                        playingSounds.at(location).play();
                }
                else
                {
                        playingSounds.push_back(sf::Sound());
                        playingSounds.at(playingSounds.size()-1).setBuffer(Sounds[soundName]);
                        playingSounds.at(playingSounds.size() - 1).play();
                }

        }

However I was testing my game and for a minute or so it is all going fine, but then all of a sudden I got this error

An internal OpenAL call failed in SoundSource.cpp (181) : AL_INVALID_NAME, an unacceptable name has been specified

What am I doing to cause this? P.S. my soundloader only has 60 lines of code so not sure what the 181 is relating to

UPDATE

I have just coded my enemy to spam its firing which plays one sound, this worked for a few seconds then an error was shown and my game continued for about 3 seconds then I got this error in visual studio

Unhandled exception at 0x56455365 (sfml-system-d-2.dll) in SpaceDestroyer.exe: 0xC00000FD: Stack overflow (parameters: 0x00000001, 0x07B52FFC).

then it shows in Err.cpp

virtual int overflow(int character)
    {
        if ((character != EOF) && (pptr() != epptr()))
        {
            // Valid character
            return sputc(static_cast<char>(character));
        }
        else if (character != EOF)
        {
            // Not enough space in the buffer: synchronize output and try again
            sync();
            return overflow(character);
        }
        else
        {
            // Invalid character: synchronize output
            return sync();
        }
    }
 

sfml-system-d-2.dll!`anonymous namespace'::DefaultErrStreamBuf::overflow(int character) Line 61 C++

UPDATE

It's ok I found it, I just changed my line to check location to be this

if (playingSounds.at(i).getStatus() != sf::Sound::Playing && location == -1)
                        {
                                location = i;
                        }

but just to let everyone else know, make sure when you are playing sounds never reach more than 140 sounds in memory, this will throw the "Internal OpenAL error", this error happened to me when my playingSounds.size() was equal to 140

8
General / mouse button released is spammed?
« on: October 06, 2014, 10:38:28 pm »
I am using SFML and C++ and I am getting an odd problem,

Here is my main game update method

   
while (renderService.Window.isOpen())
        {
                //Poll events
                sf::Event event;
                while (renderService.Window.pollEvent(event))
                {
                        if (event.type == sf::Event::Closed)
                                renderService.Window.close();
                        running = false;
                }

                MouseMovment(event);
                MouseClick(event);
                Update();
                Draw();
        }

and here is my MouseClick method

   
void Game::MouseClick(sf::Event event)
    {
        sf::Vector2i position = sf::Mouse::getPosition(renderService.Window);
       
            if (event.mouseButton.button == sf::Mouse::Left && event.type == sf::Event::MouseButtonReleased)
            {
                    std::cout << "Mouse released" << std::endl;
        }
    }

now here is the weird part, in my console sometimes my cout will be spammed like 10/20 times, but sometimes it will work perfectly, am I calling the event wrongly?

9
Hello there,

Ok bare with me on this one, I have myself a class which is a ImageLoader which basically storing a map of sf::Texture, I also have a class which is LevelState.

Now on the construction of my LevelState it required that two references are initialized on the constructor like so

Header snippet

...
public:
LevelState(FontLoader& fontLoader, ImageLoader& imageLoader);
...
private:

        ImageLoader& _imageLoader;
        FontLoader& _fontLoader;
...

Class snippet
LevelState::LevelState(FontLoader& fontLoader, ImageLoader& imageLoader) : _fontLoader(fontLoader), _imageLoader(imageLoader)
{

}

now this is working fine however it only works for some of my classes, I have a method called Setup which uses my imageLoader like so

UISprite.setTexture(imageLoader.Images[ImageNames::L_UI]);
 

and when asking to draw that it renders perfectly. however it doesn't work for my other class.

Here is a class that works fine

LevelState::Setup
DisplayObject displayObject;
        DisplayObjects.push_back(displayObject);
        DisplayObjects.at(0).Setup(0, 0, imageLoader.Images[ImageNames::Background1]);

//SETUP METHOD FOR DisplayObject
void DisplayObject::Setup(int xSet, int ySet, sf::Texture& spriteSet)
{
        XPos = xSet;
        YPos = ySet;
        Sprite.setTexture(spriteSet);
        Sprite.setPosition(XPos, YPos);
}
//
 

My DisplayObject renders without a problem

My Enemy class however is always a white block

Enemy enemy;
                Row1Enemies.push_back(enemy);
                Row1Enemies[0].Setup(75, 25, _imageLoader.Images[ImageNames::L_Enemy2], EnemyType::Shooter, 0.3, 1);

//ENEMY SETUP METHOD
void Enemy::Setup(int xSet, int ySet, sf::Texture spriteSet, EnemyType type, int fireChance, int lifeSet)
{
        XPos = xSet;
        YPos = ySet;
        Sprite.setTexture(spriteSet);
        Type = type;
        Status = true;
        FireChance = fireChance;
        Life = lifeSet;
}
//
 

Why on earth does my displayobject render fine and my enemy doesn't? Can anyone help me out here?

10
General / xtree error when trying to draw sf::Text
« on: September 26, 2014, 12:58:13 am »
Hello there

I have myself a class that deals with loading fonts like so

#include "FontLoader.h"

FontLoader::FontLoader()
{

}

FontLoader::~FontLoader()
{

}

void FontLoader::LoadFont(GameStates state)
{
        //Load fonts
        Fonts[FontNames::Main_Font].loadFromFile("Assets/Fonts/Font.otf");
}

I then have a MainMenuState which is given a reference of my FontLoader like so

#include "MainMenuState.h"

MainMenuState::MainMenuState()
{

}

MainMenuState::~MainMenuState()
{

}

void MainMenuState::Setup(ImageLoader& imageLoaderSet, FontLoader& fontLoaderSet)
{
        TextObject textObject;
        textObject.Setup(200, 100, "Space Destroyer", 48, fontLoaderSet.Fonts[Main_Font]);
        textObject.SetRotationAnimation(-12, 12, 0.1);
        TextObjects.push_back(textObject);
}

This seems to write fine, but when I hit my draw function

void State::DrawTextObjects(sf::RenderWindow& window)
{
        for (int i = 0; i < TextObjects.size(); i++)
        {
                TextObjects.at(i).Draw(window);
        }
}

I get an error in Unhandled exception at 0x6292DCE2 (sfml-graphics-d-2.dll) in SpaceDestroyer.exe: 0xC0000005: Access violation reading location 0xCCCCCCD0, at

_Nodeptr _Pnode = _Root();

Am I passing the sf::Font the incorrect way?

11
General / Best way to detect Mario style collision?
« on: July 07, 2014, 10:31:07 pm »
Hello there people,

At the moment I'm developing a very simple Mario clone for fun, but I have hit a wall ( you may say :D ). At the moment I'm trying to figure out the best way to detect collision on the ground or a wall, at the moment I'm just checking a rectangle is colliding with another rectangle. Here is the basic code I use to check for collisions

bool CollisionService::PointInRect(int pnt_x, int pnt_y, int rect_x, int rect_y, int rect_w, int rect_h)
{
        if ((pnt_x >= rect_x) && (pnt_x <= rect_x + rect_w - 1))
        {
                if ((pnt_y >= rect_y) && (pnt_y <= rect_y + rect_h - 1))
                {
                        return true;
                }
        }
        return false;
}

bool CollisionService::RectToRectCollision(int rect1_x, int rect1_y, int rect1_w, int rect1_h, int rect2_x, int rect2_y, int rect2_w, int rect2_h)
{
        // top-left cornerif ( pointInRect(rect1_x, rect1_y, rect2_x, rect2_y, rect2_w, rect2_h) ){return true;}
        // top-right corner
        if (PointInRect(rect1_x + rect1_w - 1, rect1_y, rect2_x, rect2_y, rect2_w, rect2_h)){ return true; }
        // bottom-right corner
        if (PointInRect(rect1_x + rect1_w - 1, rect1_y + rect1_h - 1, rect2_x, rect2_y, rect2_w, rect2_h)){ return true; }
        // bottom-left corner
        if (PointInRect(rect1_x, rect1_y + rect1_h - 1, rect2_x, rect2_y, rect2_w, rect2_h)){ return true; }
        // Check to see if rectangle 2 is hit any part of rectanlge 1
        // top-left corner
        if (PointInRect(rect2_x, rect2_y, rect1_x, rect1_y, rect1_w, rect1_h)){ return true; }
        // top-right corner
        if (PointInRect(rect2_x + rect2_w - 1, rect2_y, rect1_x, rect1_y, rect1_w, rect1_h)){ return true; }
        // bottom-right corner
        if (PointInRect(rect2_x + rect2_w - 1, rect2_y + rect2_h - 1, rect1_x, rect1_y, rect1_w, rect1_h)){ return true; }
        // bottom-left corner
        if (PointInRect(rect2_x, rect2_y + rect2_h - 1, rect1_x, rect1_y, rect1_w, rect1_h)){ return true; }
        // If there is no collision
        return false;
}

Now what I am thinking is, I have two rectangles, one which is the characters height but has 2 pixels less width so this can be checked against falling and another rectangle which has 2 less height so it can be checked against walking left and right into a block. Check my first image to see what I mean by the two rectangle solution. If anyone has a better way of checking for collisions or over all a way better solution please let me know as I do feel my solution isn't a great one.

12
Graphics / Odd values for a sprite I'm trying to display
« on: July 01, 2014, 09:12:06 pm »
Hey guys,

I have a vector of a class known has LevelBlock, each LevelBlock has an object known as DisplayObject
DisplayObject holds a sprite and other values here is the header file
#pragma once

#include "Enums.h"
#include "SFML\Graphics.hpp"

class DisplayObject
{
private:
        float xPos, yPos;
        sf::Sprite sprite;
        DisplayName name;
        std::string text;
        bool textObject;
public:
        DisplayObject();
        ~DisplayObject();

        sf::Text displayText;

        void SetSprite(sf::Texture texture){ sprite.setTexture(texture); }
        sf::Sprite GetSprite(){ return sprite; }
        void SetDisplayName(DisplayName setName){ name = setName; }
        DisplayName GetDisplayName(){ return name; }
        bool IsTextObject(){ return textObject; }
        void SetTextObject(bool set){ textObject = set; }
        void SetPosition(float xSet, float ySet);
};

 

Now in my level class I populate my vector of LevelBlocks to hold one block, I set the position for the sprite correctly, then I go ahead and pass a reference of the LevelBlocks DisplayObject for that block to my RenderService, so now the RenderService holds a reference to the block so it can render it, here is the code that converts my vector of LevelBlocks to vector of pointer DisplayObjects

std::vector<DisplayObject*> Level::GetLevelBlockDisplayObjects()
{
        std::vector<DisplayObject*> tempBlocks;

        for (auto block : blocks)
        {
                tempBlocks.push_back(&block.displayObject);
        }

        return tempBlocks;
}

void Level::Setup()
{
        renderService->LevelDisplayObjects = GetLevelBlockDisplayObjects();
}

//Inside my renderService
//Display the level blocks
        for (const auto& object : LevelDisplayObjects)
        {
                Window.draw(object->GetSprite());
        }
 

Check my DisplayObjectSprite.png to see the values of the sprite at ""Window.draw(object->GetSprite());"" line.

Check my DisplayObjectCorrectSpriteValues.png to see the values before it goes thought my ""GetLevelBlockDisplayObjects"" function.

Also the sprite is never displayed, but I don't get any errors at all trying to render it

13
General / How to implement 2D side scroller
« on: June 23, 2014, 09:24:50 pm »
Hey there people

At the moment I'm trying to implement a simple Mario movement, this is what I have at the moment,
Here is my keybaord event checker

if (state == GameStates::GS_Level1)
        {
                if (sf::Keyboard::isKeyPressed(sf::Keyboard::D))
                {
                        //Set move right to true
                        Characters.at(0).SetMoveRight(true);
                }
                else if (sf::Keyboard::isKeyPressed(sf::Keyboard::A))
                {
                        //Set move left to true
                        Characters.at(0).SetMoveLeft(true);
                }

                //Releases
                if (event.type == sf::Event::KeyReleased && event.key.code == sf::Keyboard::D)
                {
                        //Set move right to true
                        Characters.at(0).SetMoveRight(false);
                }
                else if (event.type == sf::Event::KeyReleased && event.key.code == sf::Keyboard::A)
                {
                        //Set move right to true
                        Characters.at(0).SetMoveLeft(false);
                }
        }
 

and here is my character update method

void CharacterObject::UpdateMovement()
{
        if (moveRight == true)
        {
                if (xSpeed < xSpeedMax)
                {
                        xSpeed += xIncrease;
                }
        }

        if (moveLeft == true)
        {
                if (xSpeed > -(xSpeedMax))
                {
                        xSpeed -= xIncrease;
                }
        }

        if (moveRight == false && moveLeft == false)
        {
                if (xSpeed > 0)
                {
                        xSpeed -= xIncrease;
                }

                if (xSpeed < 0)
                {
                        xSpeed += xIncrease;
                }

                if (xSpeed == 0.2 || xSpeed == -0.2)
                {
                        xSpeed = 0;
                }
        }

        xPos += xSpeed;
        yPos += ySpeed;
}
 

But at the moment sometimes my character will continue to move right, moving left seems fine, but some if I move right and release, my xSpeed will always remain at 0.2 not 0, does anyone have any good tutorial sites on how to implement simple 2d side scroller movement? Or any ideas on how to improve my terrible implementation?

14
General / Setting sprite but getting error in RenderWindow::Draw
« on: June 22, 2014, 10:50:37 pm »
Hello there people,

I'm having a little problem with populating a sprite, but when attempting to draw the sprite I get a n error in the RenderWindow Draw function.

Here is the code that I am using to pull back my character object to draw

std::vector<CharacterObject*> State::GetCharacters()
{
        std::vector<CharacterObject*> characters;

        for (auto character : Characters)
        {
                characters.push_back(&character);
        }

        return characters;
}
 

Inspecting &character the variable you can see the spriteSheet is set up, just to make it clear the spriteSheet  is a sheet of sprites, so basically the sprite is is setup at the moment to just draw the spriteSheet, here is the sprite setup code

void CharacterObject::Setup()
{
        sprite.setTexture(spriteSheet);
}
 

But in the image the sprite seems to not be set, is this due to me passing a pointer around or am I setting the sprite wrong?

Here is an image below.

15
General / Trying to display text returns me an error
« on: June 21, 2014, 09:19:11 pm »
Hey guys, I'm trying to display some text in my game but I keep getting the error displayed in the image I have attached coming up, the font is not null, nor the character size, nor the test or colour, what am I doing wrong here?

Here is the render code

//Objects and text
    for (int i = 0; i < DisplayObjects.size(); i++)
    {
        auto object = DisplayObjects.at(i);
        Window.draw(object->GetSprite());
        if (object->IsTextObject())
        {
            Window.draw(object->GetText());
        }
    }

Also here is the code I have which sets up the text
void DisplayObject::SetText(std::string setText)
{
        displayText.setFont(font);
        displayText.setColor(sf::Color(0, 0, 0));
        displayText.setCharacterSize(24);
        displayText.setString(text);
        text = setText;
        textObject = true;
}

Pages: [1] 2 3
anything