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 - bglaze

Pages: [1]
1
General / Time as float?
« on: November 03, 2011, 11:29:32 am »
I was taught that when programming, Time is is typically stored using an integer that represents milliseconds. However, I see that SFML uses floats to represent Time.

Should all of my sub-times that get assigned by and then compared to ElapsedTime() be floats? If I have them as as integers, will SFML have to convert them, thus using more (though I'm sure only slightly) processor power?

Or was I simply taught wrong, and using floats is actually the norm?

Thanks!!

2
Audio / Reusing Soundbuffer issues...
« on: November 03, 2011, 12:51:32 am »
Okay, so in order to play multiple instances of the same sound over top of each other (such as laser shots or explosions), I have created a vector to contain the Sound objects.

A SoundBuffer object matching each of my wav files is stored as a member in my file manager object and is always alive.

First I call my PlaySound function:

Code: [Select]
PlaySound(loads.MyBuffer);

This is my complete PlaySound function:

Code: [Select]
void Game::PlaySound(sf::SoundBuffer& buffer){
    soundbox.push_back(sf::Sound(buffer));
    soundbox[soundbox.size()-1].Play();
}


Then, I have a function that sits in my main loop that checks if each sound has finished playing, and if it has, it gets destroyed. Here is that function:

Code: [Select]
void Game::DestroySounds(){
    for(std::vector<sf::Sound>::iterator iter = soundbox.begin(); iter != soundbox.end(); ){
        if(!iter->GetStatus()){
            iter = soundbox.erase(iter);
        }else{
            ++iter;
        }
    }
}


All this works. However, as soon as one of the Sound objects is erased from the vector, the most recent Sounds that share the same SoundBuffer as the erased Sound are stopped short if they were still playing.

Any advice?

3
Audio / Playing the same sound over top of itself...
« on: November 02, 2011, 11:04:47 pm »
Okay, i could be missing something huge here, but I would like to be able to play the same sound over top of itself. For instance, when I shoot my lasers very fast in my game, I get buzzing sound if the sound hasn't finished playing. Then I tried using:

Code: [Select]
if(!lasersound.GetStatus())
    lasersound.Play();


However, this doesn't sound natural at all, because i have, say, 5 lasers in mid air, but I only heard one of them fire.

I understand (i think) that a new buffer must be created for each sound. But I am having a hard time conceptualizing how to do this. This must be a VERY common problem, because the need has arisen MANY times already in just my simple game.

Any ready solutions out there?
[/u]

4
SFML projects / Freighter - w/Source (Updated 11/04)
« on: November 02, 2011, 05:05:25 am »
Many of you have already seen and commented on my first SFML/C++ project located here: http://sfml-dev.org/forum/viewtopic.php?t=6110

This game -- "Freighter" -- is my second project.

The game is completely functional, though I will continue to add features. I apologize for using a .wav file for the background music. I am having trouble using .ogg files, none of them seem to load for me, so I need to work out that kink.

Freighter is a Space Skill game. You must collect the cargo crates that are floating outside your space station. The catch is that they are in an asteroid field, and you must navigate or blast your way through the asteroids to reach them without being killed. Your freighter ship (a millenium falcon =P ) has a shield, but it can only sustain so much damage. The weight of the asteroid determines how much damage it will do to your ship. So even though the big rocks move slowly, they will do lots of damage, so watch out!

Also, you can come back to your Space Station's "Safe Zone" at any time during the level to re-assess your strategy. A Force Field keeps the rocks from penetrating this safe zone, so you should be relatively safe there.

There are 10 levels.

The controls are as follows:
W-A-S-D or Arrow keys to move.
Left Mouse Button or Space Bar to shoot your laser.
F5 Restarts the game if you lose or advances you to the next "Zone" if you win.

Here is the compiled version for Windows:
http://bglz.net/files/cpp/projects/Freighter.zip

And here is the full source code in the form of a Code::Blocks project:
http://bglz.net/files/cpp/projects/Freighter-src-11-04.zip

I would REALLY appreciate suggestions and critiques on this game and especially on the code! As I stated in my first project--Yellow Snake--I am new to C++, so I am REALLY soaking up any advice you can give me on my coding techniques and logic.

Here are some screenshots:




5
Audio / Can OpenAL32.dll & libsndfile-1.dll be linked statically
« on: November 02, 2011, 03:13:26 am »
And if so how? I have my whole application statically linked, and these would be the only two dll's I would have in my exe's folder. So, I was hoping to be able to link them statically as well, for uniformity.

In other linker options, I use the following for my other dlls:

Code: [Select]
-static-libgcc
-static-libstdc++


I have tried adding -static-openal32, but this didn't work...

Any recommendations?

6
General / Advice on bullets? [SOLVED]
« on: October 31, 2011, 03:58:22 pm »
Ok folks, I am having a hard time coming up with an efficient bullet system.

My problem isn't with the creation, display, or movement of the bullets--those are easy. However, I am having a hard time with destroying them when their life is over (i.e. they have gone out of screen or they have hit an object and must disappear).

I have been using a vector to contain my bullet sprites, but removing a specific bullet sprite from wherever it happens to reside in that vector has become a problem.

Does anyone have a recommendation for a good container and/or system to use for managing bullets?

If another container is suggested, I would also appreciate any simple code examples you are willing to give. Though, I am also willing to research it myself with whatever info is provided!

Thank you!
Brock

7
Graphics / Help with this bouncing code??
« on: October 30, 2011, 09:07:08 pm »
My "rocks" are not bouncing off of each other in the most natural way, and I was wondering if anyone would be willing to look at my code and give me some advice on a better way to accomplish better rect-wall-detection of my sprite and the sprite it is bouncing off of.

Here is the code I am using to cause my rocks to bounce off of each other:

Code: [Select]
for(unsigned sprite1 = 0; sprite1 < rockbox.size(); sprite1++){
    rockbox[sprite1].Update();
    Window.Draw(rockbox[sprite1]);

    for(unsigned sprite2 = 0; sprite2 < rockbox.size(); sprite2++){
        if(sprite1 != sprite2){
            bounce_sprites(rockbox[sprite1], rockbox[sprite2]);
        }
    }
}


Here is the function bounce_sprites:

Code: [Select]
void bounce_sprites(SubSprite& sprite1, SubSprite& sprite2)
{
    if(sprite1.Rect.Intersects(sprite2.Rect)){

        if(sprite1.Rect.Top <= sprite2.Rect.Top + sprite2.Rect.Height
           and sprite1.Rect.Top + sprite1.Rect.Height >= sprite2.Rect.Top + sprite2.Rect.Height){

            switch(sprite1.Direction){
                case UPRIGHT:
                    sprite1.Direction = DOWNRIGHT;
                    break;
                case UPLEFT:
                    sprite1.Direction = DOWNLEFT;
                    break;
                default:
                    break;
            }
        }

        if(sprite1.Rect.Top + sprite1.Rect.Height >= sprite2.Rect.Top
           and sprite1.Rect.Top <= sprite2.Rect.Top){

            switch(sprite1.Direction){
                case DOWNRIGHT:
                    sprite1.Direction = UPRIGHT;
                    break;
                case DOWNLEFT:
                    sprite1.Direction = UPLEFT;
                    break;
                default:
                    break;
            }
        }

        if(sprite1.Rect.Left + sprite1.Rect.Width >= sprite2.Rect.Left
           and sprite1.Rect.Left <= sprite2.Rect.Left){

            switch(sprite1.Direction){
                case DOWNRIGHT:
                    sprite1.Direction = DOWNLEFT;
                    break;
                case UPRIGHT:
                    sprite1.Direction = UPLEFT;
                    break;
                default:
                    break;
            }
        }

        if(sprite1.Rect.Left <= sprite2.Rect.Left + sprite2.Rect.Width
           and sprite1.Rect.Left + sprite1.Rect.Width >= sprite2.Rect.Left + sprite2.Rect.Width){

            switch(sprite1.Direction){
                case DOWNLEFT:
                    sprite1.Direction = DOWNRIGHT;
                    break;
                case UPLEFT:
                    sprite1.Direction = UPRIGHT;
                    break;
                default:
                    break;
            }
        }
    }
}


If anyone is willing and able to give me some advice in the general direction of what I could be doing better I would be very appreciative!

I prefer to not import a huge library like Box2D, since this is about the extent of the physics that I will be doing in this game.

Thanks so much!

8
Graphics / sf::Rect::Intersects() for two moving rectangles?
« on: October 30, 2011, 05:32:23 am »
Is it possible to use sf::Rect::Intersects() for two moving rectangles?

I notice that Intersects() takes a const Rect, so I am assuming that one of the Rects has to be permanently stationary like a platform or something.

I am trying to make collision detection for a laser and a moving asteroid, and I was hoping to use this function, but if this isn't possible I may have to make my own. In that case I would love some guidance (doesn't have to be direct code, just basic advice, though code is fine too... =) ) on creating my own Bounding Box Collision.

Anyway, thank you!!!!
Brock G.

9
SFML projects / Yellow Snake - w/Source (Updated 10/21)
« on: October 17, 2011, 02:08:45 am »
I've been programming in C++ for about 3 weeks. About a week into it, I stumbled upon SFML! I wanted my first project to be a game that my kids could play. I also wanted to make something I was familiar with, so I chose to make a snake game.

When my wife and I were dating in high-school, we use to compete on her cell-phone playing the snake game, so this is also something we can have fun with together.

This game has 2 modes: NORMAL and DRAW mode. Normal mode is classic snake game. Draw mode is what I made for my kids (they are 3 and 4 years old)--your snake never dies, and you grow at a very fast pace.

There are also 3 speeds. The game saves your last speed to a file, so it will load that same speed the next time you launch the game.

I am definitely open to criticism, seeing that I am new to C++ in general. I am out to better my skills and logic techniques, so keep the criticisms flowing! Thanks!

So far I only have a Windows version.

[Edit (10/21): I have implemented some changes suggested by the people on this post. Also, the game now tracks the person who holds the High-Score; and the game now starts in a paused state]

You can download it here: http://bglz.net/files/cpp/projects/Yellow_Snake.zip

And the source code and CB project here: http://bglz.net/files/cpp/projects/Yellow_Snake_src_10-21.zip

Here are some screenshots.



Draw Mode:

10
General / Simple Collision Detection - BoundingBoxTest
« on: October 15, 2011, 12:56:52 pm »
I am using the simple collision detection modules found here:
http://www.sfml-dev.org/wiki/en/sources/simple_collision_detection

I am using this with a snake game to test whether or not my snake eats his food.

The BoundingBoxTest seems to create a box that is nearly twice the size of my sprite image. My snake will eat his food if I am merely passing beside the food on the grid. Is there any way to fix this, such as shrinking the size of the bounding box in this code?

I don't need pixel perfect detection, but I don't want to eat my food by merely passing next to my food either.

Thanks so much!!

11
General / Keep seeing: error: expected ')' before '&' token ?
« on: October 11, 2011, 07:48:12 pm »
I keep seeing the following error when trying to assign my game object to my snake object. Since I have seen it so many times now, and have spent 2 days finding workarounds, but without knowing what is causing it, I am hoping someone can see why it's happening, so I can fix it directly.

Here is the full error:
Code: [Select]

s Files\CPP\Dev\The_Yellow_Snake\Yellow_Snake_testing_window_passing\Snake.h|12|error: expected ')' before '&' token|
s Files\CPP\Dev\The_Yellow_Snake\Yellow_Snake_testing_window_passing\Snake.h|15|error: 'Game' does not name a type|
s Files\CPP\Dev\The_Yellow_Snake\Yellow_Snake_testing_window_passing\Snake.cpp|6|error: prototype for 'Snake::Snake(Game&)' does not match any in class 'Snake'|
s Files\CPP\Dev\The_Yellow_Snake\Yellow_Snake_testing_window_passing\Snake.h|9|error: candidates are: Snake::Snake(const Snake&)|
s Files\CPP\Dev\The_Yellow_Snake\Yellow_Snake_testing_window_passing\Snake.h|9|error:                 Snake::Snake()|
||=== Build finished: 5 errors, 0 warnings ===|



And here are my files, starting with main.cpp
Code: [Select]

#include "Game.h"
#include "Snake.h"
#include <SFML/Graphics.hpp>
#include <iostream>

int main()
{
    sf::VideoMode VMode(600, 390, 32);
    sf::RenderWindow window;
    window.Create(VMode, "Yellow Snake", sf::Style::Close);

    Game game(window);
    game.Run();

    return 0;
}


Game.h
Code: [Select]

#ifndef GAME_H
#define GAME_H

#include "Game.h"
#include "Snake.h"
#include <SFML/Graphics.hpp>
#include <iostream>

class Game
{
    public:
        Game(sf::RenderWindow& window);

        //funcs
        void Run();

        //vars
        double FPS;
        sf::RenderWindow& Window;


    protected:

    private:
};

#endif // GAME_H


Game.cpp
Code: [Select]

#include "Game.h"
#include "Snake.h"
#include <SFML/Graphics.hpp>
#include <iostream>

Game::Game(sf::RenderWindow& window):
FPS(15.0),
Window(window)
{
}

void Game::Run()
{
    Snake snake(this);

    while(Window.IsOpened())
    {
        sf::Event event;
        while(Window.GetEvent(event))
        {
            switch(event.Type)
            {
                case sf::Event::Closed:
                    Window.Close();
                    break;
                default:
                    break;
            }
        }

        Window.Clear(sf::Color(0, 0, 0));
        Window.Display();

        /* Free up the CPU */
        sf::Sleep(1.0/FPS);
    }
}


Snake.h
Code: [Select]

#ifndef SNAKE_H
#define SNAKE_H

#include "Game.h"
#include "Snake.h"
#include <SFML/Graphics.hpp>
#include <iostream>

class Snake
{
    public:
        Snake(Game& g);
    protected:
    private:
    Game game;
};

#endif // SNAKE_H


Snake.cpp
Code: [Select]

#include "Game.h"
#include "Snake.h"
#include <SFML/Graphics.hpp>
#include <iostream>

Snake::Snake(Game& g):
game(g)
{
}



My full program is larger, but this is as small as I could get it and reproduce the error.

Your consideration is much appreciated!

Thanks
- Brock

12
Graphics / Pointer to RenderWindow object?
« on: October 11, 2011, 05:24:09 am »
I would like to create my window object in my Main function, and then set my Game class to have a member variable called "window" so that I can access the window's properties via game.window or game->window.

Is there a way to do this?

Thanks!

13
General / New to C++ and SFML - Hoping to get advice on my code...
« on: October 11, 2011, 05:21:35 am »
Hey folks!

New to the forum, and so far I am loving SFML (though I am not sure if I am utilizing it well, yet).

In order to try to teach myself c++ and SFML, I began a small snake game, and I am hoping some people might be willing to check out the code and let me know what I can do to clean it up so far.

So far it is just the snake's head moving around the screen and dying when it hits the walls.

But I am sure that some of the code could be cleaned up. Especially in my Move_Me() SnakeHead member function such as this line:
this->SetPosition( (game.SW - this->GetImage()->GetWidth()), this->GetPosition().y );

I am wondering if there is an easier way to access my sprite's width, screen width, etc...

Anyway, here is the zip of my Code::Blocks project.

http://bglz.net/files/cpp/projects/Yellow_Snake.zip

My code, though crappy as it probably is, is always free to use, reuse, and distribute at your pleasure!

Thanks!
Brock

Pages: [1]
anything