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

Pages: [1]
1
General / Maximizing window causes stack overflow
« on: July 14, 2013, 11:58:59 am »
I was doing some testing, and I noticed this curiosity. When I would maximize my window, I would get a:
"Unhandled exception at 0x101fba76 in GameTestingSFML.exe: 0xC00000FD: Stack overflow."

The example included is essentially minimal, as it's just a slight derivative of the most basic example of SFML.

#include <SFML/Graphics.hpp>

int main(void){
        //Variable Definitions
        sf::RenderWindow window(sf::VideoMode::getFullscreenModes()[7], "SFML works!", 7);
                window.setFramerateLimit(60);

        sf::CircleShape shape(256.f);
                shape.setFillColor(sf::Color(191, 255, 127, 255));
                shape.setPointCount(48);

        //The Game Loop
        while (window.isOpen()){
                sf::Event event;
                //The Event Loop
                while (window.pollEvent(event)){

                        //Event type cases
                        switch(event.type){

                                //Window closed
                                case sf::Event::Closed:
                                        window.close();
                                break;

                                //Key pressed
                                case sf::Event::KeyPressed:

                                        //Keypress cases
                                        switch(event.key.code){

                                                //Escape key pressed
                                                case sf::Keyboard::Escape:
                                                        window.close();
                                                break;
                                        }
                                break;
                        }
                }

                window.clear();
                window.draw(shape);
                window.display();
        }

        return 0;
}

2
Graphics / Reusing sprites
« on: February 09, 2013, 04:22:23 pm »
Let's say I have multiple enemies that look the same, would it be better to give each of them a sprite, or would it be better to draw each one with the same sprite, but changing the necessary properties to fit the properties of one individual enemy? (Like position.) Is there just one better way? Or can there be specific cases where one or the other should be applied?

(Also, I have another question, but I'll give it less priority. How would one reduce the amount of draw calls? What counts as a draw call? Can one ever have a big 2D platformer with parallax scrolling and have everything drawn in 1 call?)

3
General / Ray casting in 2D (Not related to SFML)
« on: January 21, 2013, 03:28:16 am »
I have, for a long time, had a problem. I often found myself looking up how to do ray casting, I might find something, but it would never really help, and everything I had seen looked awfully limited. The environment where rays were being cast had to be a grid because it depended on checking every square on the grid the ray intersected with along the way, and that meant that it would be slower if things were far apart. I also noticed a bizarre pattern of people only wanting to know what ray casting was in the context of rendering a 3D-looking environment. This was weird for me, because people seemed to ONLY refer to it that way, when it had quite a few more applications. Needless to say the internet was useless, and I worked on and developed my own method independently to find if a ray would intersect with a line. No grids, no binary searches. Just rays and lines and math.

Something still bothered me though. I thought it was pretty efficient as compared to some other methods, but I didn't know how efficient it was. I've seen other things ray cast, but I never knew how the did it, and I never knew if my method was better or not.

So, today, I present the algorithm in the form of C++ to programmers I think will be able to judge it based on their experiences with such things. It isn't optimized. (It will still check if the ray is pointing directly away from the line.) Also, the code contains some variation of a class that represents a 2D vector, so there really is no relation to SFML here. I'm asking here because I trust you folks to answer the question:

Is there a more efficient way to do what I've done here? (Not in regards to programming, more in regards to the math.):
#include <iostream>
#include <math.h>
using std::cout;
using std::endl;

//Define the behaviour of a 2D vector (geometry). How this works is mostly irrelevant.
class vec2 {
public:
vec2(float X, float Y){
x = X;
y = Y;
}
vec2 operator = (vec2 arg){
x = arg.x;
y = arg.y;
return *this;
}
vec2 operator * (float arg){
vec2 temp(0,0);
temp.x = x * arg;
temp.y = y * arg;
return temp;
}
vec2 operator + (vec2 arg){
vec2 temp(0,0);
temp.x = x + arg.x;
temp.y = y + arg.y;
return temp;
}
vec2 operator - (vec2 arg){
vec2 temp(0,0);
temp.x = x - arg.x;
temp.y = y - arg.y;
return temp;
}
vec2 operator * (vec2 arg){
vec2 temp(0,0);
temp.x = x * arg.x;
temp.y = y * arg.y;
return temp;
}
vec2 operator / (vec2 arg){
vec2 temp(0,0);
temp.x = x / arg.x;
temp.y = y / arg.y;
return temp;
}
float magnitude(){
return sqrt(x*x + y*y);
}
vec2 normal(){
float mag = this->magnitude();
return vec2(x/mag, y/mag);
}
float x,y;
};
//That's what most of the code is, and I haven't even gotten into the important part.

//This is what makes it happen *IMPORTANT THINGS START HERE*
void raycast(bool * retBool, float * retDis, vec2 * retVec, float Angle, vec2 Origin, vec2 PointA, vec2 PointB){
float aA, bA;
float factor;
vec2 pos(0,0);
aA = atan2(PointA.y, PointA.x);
bA = atan2(PointB.y, PointB.x);
factor = (Angle - aA)/(bA - aA);
*retBool = factor >= 0 && factor <= 1;
*retVec = (PointB-PointA)*factor + PointA;
*retDis = sqrt((retVec->x)*(retVec->x)+(retVec->y)*(retVec->y));
}
//That's it


int main(){
float distance;
bool hit;
vec2 pos(0,0);

raycast(&hit, &distance, &pos, 45.0 / 180.0 * 3.14159, vec2(0,0), vec2(1, 3), vec2(3, 1));

cout<<hit<<endl;
cout<<distance<<endl;
cout<<pos.x<<", "<<pos.y<<endl;

return 0;
}

Of course, I doubt you'll just get it if I post the code, so I'll give a little explanation of how it works in the form of an image. I don't guarantee it'll help because I'm apparently bad at explanations.

That probably didn't help much.

Well, if you think you can help me, feel free to post about it, I'd love to hear it.

4
General / Issues with game speed
« on: January 17, 2013, 05:36:34 am »
After a bit of effort, I think I managed to compress my code into the basics to represent my problem. Unfortunately, my problem depends on a few complicated systems. Too long? The original was over 220 lines long on three files. This is 120 lines in one document. You're welcome.  ;)

Anyway, the problem I present to you is one I don't quite understand. When I usually get problems like these, the solution seems evident enough, but I don't think things quite add up in this case. You see, there is an object in the middle of the screen that can move around and follow your cursor when you press the Up key. When your mouse is still, it goes at a constant pace. However, whenever the mouse moves, the object speeds up. That's not the end of it. If I'm outputting text to the console window in the game loop, the game moves at the same pace as if the mouse were moving in the previous explanation. HOWEVER, the game slows to the calm pace when the object in the middle faces directly right. (Why right? It has to do with some math, but it used to be directly up before I subtracted 90 from an angle.) I think this has to do with me trying to control the speed of the game based on the frame rate of the game. (e.g. Game has half the expected frame rate? Make everything move twice as fast per frame.)

I've been looking at this for a long time, and I've gotten lost in a sea of possibilities. Any help is appreciated. Thanks.
#include <SFML/Graphics.hpp>
#include <SFML/System.hpp>
#include <math.h>
#include <iostream> //For bug fixing

#define PI 3.1415926535

class playerCharacter {
public:
        sf::Vector2f                            Position;
        short int                                       Rotation;
        sf::Vector2f                            Velocity;
        sf::CircleShape                         Visual;
        char                                            Direction;
        friend void                                     Update(playerCharacter *plyr, sf::Int32 passedTime);
};

void Update(playerCharacter *plyr, sf::Int32 passedTime){ //If I use Int64 here, it doesn't build. Probably because this computer doesn't use 64 of them thingamabobs.
        float timeFactor = 60.0/(1.0/((float)passedTime / 1000000.0)); //I suspect all troubles come from this line, where I try to make sure that the speed of the game is independant of the frame rate.

        //std::cout<<timeFactor<<","<<passedTime<<std::endl;

        plyr->Position.x = plyr->Position.x + plyr->Velocity.x * timeFactor;
        plyr->Position.y = plyr->Position.y + plyr->Velocity.y * timeFactor;
}

int main(){

        //System-related
        sf::Clock gameClock;

        sf::Clock rClock;

        sf::VideoMode vm(400,300);

        sf::RenderWindow window(vm, "Placeholder", 7);

        sf::Vector2f mousePosition;

        playerCharacter teabot;
        teabot.Position = sf::Vector2f((float)window.getSize().x/2, (float)window.getSize().y/2);
        //System-related END

        //Graphics-related
        sf::View view(sf::FloatRect(teabot.Position.x-(float)80, teabot.Position.y-((float)vm.height/(float)vm.width) * (float)80, (float)160, ((float)vm.height/(float)vm.width) * (float)160));

        teabot.Visual.setRadius(128);
        teabot.Visual.setPointCount(4);
        teabot.Visual.setFillColor(sf::Color::Blue);
        teabot.Visual.setOrigin(128, 128);
        teabot.Visual.setScale(1.f/32.f, 1.f/32.f);

        sf::CircleShape mReference(0.4, 5.0);
        mReference.setFillColor(sf::Color::White);
        //Graphics-related END

        //Window options
        window.setVerticalSyncEnabled(true);
        window.setFramerateLimit(60);
        window.setView(view);

        //Game loop
        while(window.isOpen()){
                gameClock.restart();
                sf::Event event;
                while(window.pollEvent(event)){
                        switch(event.type){
                                case sf::Event::Closed :
                                        window.close();
                                break;
                                case sf::Event::KeyPressed :
                                        if(sf::Keyboard::isKeyPressed(sf::Keyboard::Escape)){
                                                window.close();
                                        }
                                        //Key presses
                                        if(sf::Keyboard::isKeyPressed(sf::Keyboard::Up))
                                                teabot.Direction = 'F';
                                        //Key presses END
                                break;
                                case sf::Event::MouseMoved :
                                        mousePosition = (sf::Vector2f) sf::Mouse::getPosition(window); //Whenever I move the mouse, the game's speed increases
                                break;
                        }
                }

                view.setCenter(teabot.Position);
                window.setView(view);

                teabot.Visual.setPosition((sf::Vector2f)teabot.Position);
                teabot.Rotation =  90 + atan2( (float)(mousePosition.y + (view.getCenter().y - window.getSize().y/2)) - teabot.Position.y, (float)(mousePosition.x + (view.getCenter().x - window.getSize().x/2)) - teabot.Position.x) / (float)PI * 180;
                teabot.Visual.setRotation(teabot.Rotation);

                //Work on stuff below. Note: Character slows down when facing directly right, investigate.
                //When I get rid of output to cout, the entire game seems to slow to a constant rate, and it speeds up when the mouse moves.
                //Curiouser and curiouser. Because the slow down caused by facing the object to the right is relatively unnoticeable due to the new slowing by lack of output.
                //Theory: The output does cause the game to speed up, but something about all the values outputted being so simple when the object faces the right makes the console window update in a different way. Perhaps, not update at all?
                //Then why does the mouse speed up the game when there's no output? Should the game be as slow as it is when the mouse isn't moving? Does the update of a mouse cause the window to update at the rate at which the mouse position updates?

                if(teabot.Direction == 'F'){
                        teabot.Velocity.x = cos(((float)teabot.Rotation - 90.0) / 180.0 * (float)PI)*50.0;
                        teabot.Velocity.y = sin(((float)teabot.Rotation - 90.0) / 180.0 * (float)PI)*50.0;
                }

                if(rClock.getElapsedTime().asSeconds() >= 1.0){
                        mReference.setPosition(teabot.Position);
                        rClock.restart();
                }

                //std::cout<<teabot.Direction<<teabot.Velocity.x<<", "<<teabot.Velocity.y<<std::endl; //Whenever the console window updates, the game window updates. This seems to make the game go faster somehow.
                Update(&teabot, gameClock.getElapsedTime().asMicroseconds());

                window.clear(sf::Color::Black);
                window.draw(teabot.Visual);
                window.draw(mReference);
                window.display();

        }

        return 0;
}

5
Graphics / RenderTextures and dynamic memory
« on: January 13, 2013, 01:11:06 am »
I have an issue with how things are rendering. When I delete a RenderTexture, the screen is never cleared, and nothing ever renders. I checked if RenderWindow::clear() is even called by outputting text after the method is called, and looks like it is. If I comment out the line that deletes the RenderTexture, everything works fine.


        sf::Vertex * sh = new sf::Vertex[3];
        sh[0] = sf::Vertex(sf::Vector2f(0,64), sf::Color(255,127,64,255));
        sh[1] = sf::Vertex(sf::Vector2f(32,0), sf::Color(255,191,127,255));
        sh[2] = sf::Vertex(sf::Vector2f(64,64), sf::Color(255,127,64,255));

        sf::RenderTexture * shipRTex = new sf::RenderTexture; //A RenderTexture due to be deleted
        if(!shipRTex->create(64,64)){
                return -1;
        }
        shipRTex->draw(sh, 3, sf::Triangles);
        shipRTex->setSmooth(false);
        shipRTex->display();

        sf::Texture shipTex; //This will replace the RenderTexture
        if(!shipTex.create(64,64)){
                return -1;
        }
        shipTex = shipRTex->getTexture(); //Replacing

        sf::Sprite ship(shipTex);
        ship.setColor(sf::Color(255,255,255,255));
        ship.setOrigin(32, 32);
        ship.setScale(1.f/8.f, 1.f/8.f);
        ship.setPosition(window.getSize().x/2, window.getSize().y/2);

        delete[] sh;
        delete shipRTex; //Everything renders fine without this line
 

I checked to see if this was because RenderTexture::getTexture() only points to a texture, (which makes no sense because a copy is made to replace it, but I decided to try it anyway) so I tried deleting shipTex to see what would happen if the sprite no longer had something to render. A white square appeared, but it didn't cause the problems deleting shipRTex did. I wondered if it was because I had not called Texture::create(), but that didn't help. I finally ran out of ideas and I came here. Do you have any idea about what could be causing this?

6
I checked out the examples, and I was disappointed to find out that the Shader example didn't work. I updated my graphics driver, hoping that it would make a difference, but it didn't. I got this computer about 4 years ago, and apparently I'm using a "Intel(R) G33/G31 Express Chipset Family" graphics card. Also, the driver update I got was about 3 years old. :|

So, what are my options?

Pages: [1]
anything