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

Pages: [1] 2
1
General discussions / Re: SFML API ugliness
« on: July 15, 2012, 05:35:01 pm »
As I said, it is just my opinion. Any ugliness I think there is I just remedy by wrapping them in a function of my own. I don't stress over it, lot easier just to wrap them and keep going than to worry about the developers possibly fixing them. Since the developers and other users don't think the API is ugly then I will just continue to wrap it and use it that way. No need to rock the boat over interpretations of clean and ugly API as the API works fine even though it isn't to my tastes. After 16 years I have just become accustom to doing wrappers anyways :).

Why so defensive all of a sudden? You made a claim, now back it up or shut up. With your (claimed) 16 years of experience I believe you should be pretty good at designing a good API if you like to write your own "better" wrappers around the API in question. Thus far, you haven't mentioned a single flaw in SFML API, instead you've emphasized it's your own opinion and doesn't really matter, while boasting your experience as a developer.

2
General discussions / Re: How to make music/sound, easily?
« on: July 12, 2012, 06:42:03 pm »
Code: [Select]
#include <math.h> /*** ---- viznut --- http://www.hytti.uku.fi/~vheikkil/ */
main(v,i,z,n,u,t){for(v=-1;;)for(n=pow(/* gcc -lm sig.c; a.out > /dev/dsp */
1.06,"`cW`g[`cgcg[eYcb^bV^eW^be^bVecb^"[++v&31]+(v&64)/21),i=999;i;putchar(
128+((8191&u)>i?0:i/8)-((8191&(z+=n))*i-->>16)))u+=v&1?t/2:(t=v&6?t:n/4);}

It works. Plays a cool tune on any Unix machine with /dev/dsp. Honest.

3
SFML projects / Yellow Snake - w/Source (Updated 10/21)
« on: October 17, 2011, 04:30:39 pm »
Quote from: "Nexus"
Quote from: "Calmatory"
You're also initializing a sf::Event local variable inside the main loop of game::Run() for some reason. You could make it a member variable of the class and avoid always generating a new variable. This isn't likely to hurt your performance though, but it's horribly bad code when you think about it. Local variables get "freed" at the end of the scope, so you keep initializing and freeing the variable for nothing.
In fact, the opposite is horrible, namely having variables with a much wider scope than actually needed. The event is not part of the object's state, so don't make it a member.

Local variables are mostly the better choice, because you directly see their scope and avoid unwanted side effects and accesses to the variable. sf::Event even is a POD, so no initialization is performed at definition. Always declare variables as local as possible, except if you have measured a relevant performance gain.


This is actually true, making it a member of a class is even worse than having it as a local. Defining it outside the scope of the loop(before the loop) is the best approach indeed.

4
SFML projects / Yellow Snake - w/Source (Updated 10/21)
« on: October 17, 2011, 07:40:46 am »
At a very quick glance I stumbled upon this:

Code: [Select]

 sf::Sleep(1.0f/FPS);


You could replace this with something like

Code: [Select]

if(1.0/window.GetFrameRate() < 1.0/FPS) {
    sf::Sleep(1.0/FPS-(1.0/window.GetFrameRate()));
}

It calculates the actual delay between frames for desired FPS. Say your target FPS is 60, the desired delay between frames is 1.0/60 = ~0.01667 seconds. Now, if your game runs at 80 FPS, the actual delay between each frame is 1.0/80 = 0.0125 seconds. Because 0.01667 - 0.0125 is 0.004 seconds, the program sleeps for approximately 4 milliseconds to increase the frame time to ~0.01667, and making FPS to a approximate of the targeted 60. However, note that this isn't an ideal approach either, because if FPS fluctuates a lot between frames, this can't provide consistency needed to cope with it, but for this case it should help things a bit.

Do you have compiler optimizations turned on? On what kind of a machine do you run the code?

There's lots of room for improvement in the code, however, of what I can tell it's hardly anything complex which a compiler shouldn't be able to optimize away anyway(e.g. there are multiple if()'s inside a loop, which all could be avoided by doing comparisons and branches before entering the loop).

You're also initializing a sf::Event local variable inside the main loop of game::Run() for some reason. You could make it a member variable of the class and avoid always generating a new variable. This isn't likely to hurt your performance though, but it's horribly bad code when you think about it. Local variables get "freed" at the end of the scope, so you keep initializing and freeing the variable for nothing.

After you handle the events, you test if the snake eats food with Terrarium[0], this is fine and good. However, after that, you loop through the snake and do the following per every node of the snake:
CollideBody() to check if the head has it part of the body
EatFood() to check if any of the nodes have eaten food
Update() to move the nodes of the body
Draw

The bolded one makes no sense. The head has already traversed the exact positions of the nodes already and they are certainly free of any food, so there's no way for the rest of the body to stumble upon food if you change your food::NewFood function to make sure the new food does not spawn on locations of the snake's body. I also suspect that the EatFood() function is the root of all evil here as it calls the function which does pixel perfect collision, which is definitely slow.

Also the CollideBody() function could be changed to loop through the snake body inside the function rather than calling the function per every node of the snake's body. This wouldn't only be good design but also would avoid the function call+return overhead(which isn't much, but still something you should avoid in this case).

Also initializing the Terrarium vector with a big enough value to hold the snake from the beginning without having to resize every now and then can save you from a few hiccups which happen because when the vector gets full, it has to allocate new memory(usually double size of the old) and copy the contents to that new memory and free the old memory. This usually happens when the vector reaches size of 2, 4, 8, 16, 32, ... etc. It only gets "visible" in forms of performance loss when the vector contains lots of data to copy around.

As always though, these are mere guesses and hints for you to take a closer look at, I am not able to run the code and as such it's nearly impossible to get an actual understanding of what truly holds you back in terms of performance. The only real way to measure that would be to run the code and use a performance profiler to get an idea what functions are being called the most and what functions take longest to finish, and as such focus on those slow/frequent functions in terms of optimizing. Usually in these cases the popular 80-20 rule applies; 80 % of time is spent by 20 % of code. Locating and focusing on that 20 % is the first and most important thing to do when considering code performance and optimizing. As always, measure the performance before and after you make any changes.

..and remember, after all the most important thing to do is to measure twice and cut once(..and curse three or four times).

5
General / rbSFML
« on: December 08, 2010, 10:33:01 pm »
This is just awesome work, Swede. :)

A big big thank you.

6
Feature requests / SetCursor in sf::Window
« on: September 29, 2010, 12:16:21 am »
I recently had to tackle with this issue with SDL(Yes, there are reasons to use it, still. ;)) and I would like to see a similar feature in SFML. Sure, it's not much work to implement one myself, but having it as a feature is a definite plus in my opinion.

7
Graphics / A slight problem with drawing sprites
« on: July 22, 2010, 12:06:31 am »
Using std::list solved the problem. Thanks for the info. :) ...and now my journey shall continue.

8
Graphics / A slight problem with drawing sprites
« on: July 21, 2010, 10:11:19 pm »
Well, I came across this kind of a problem while writing a tetris clone, and I'm kind of stuck with this problem for now.

The first two sprites just draw a white box, I don't know why. As if not all the images in vector seem to be valid for some reason?

This code should well reproduce the problem. block.png should be a png file with 16x16 dimensions.

Code: [Select]

#include <SFML/System.hpp>
#include <SFML/Graphics.hpp>
#include <string>
#include <vector>

/* Define global vector to hold all loaded images. */
std::vector<sf::Image> tempVector;

sf::Sprite LoadImage(std::string filename) {
    sf::Image tempImage;
    if(tempImage.LoadFromFile(filename)) {
        sf::Sprite tempSprite;
        tempVector.push_back(tempImage);
        tempSprite.SetImage(tempVector.back());
        return tempSprite;
    }
}

int main() {

    sf::RenderWindow App(sf::VideoMode(320,240,32), "Bugtest");
   
    sf::Sprite sprite[4];

    sprite[0] = LoadImage("block.png");
    sprite[1] = LoadImage("block.png");
    sprite[2] = LoadImage("block.png");
    sprite[3] = LoadImage("block.png");
    int i=0;

    for(int y=0;y<240;y+=16) {
        for(int x=0;x<320;x+=16) {
            sprite[i%4].SetPosition(x,y);
            App.Draw(sprite[i%4]);
            i++;
        }
    }
    App.Display();
    sf::Sleep(10.0f);
    App.Close();

}


Using linux x86 with SFML 1.6 and gcc version 4.5.0 20100610 (prerelease) (GCC).

9
Python / A little more abstraction
« on: July 21, 2010, 01:31:00 pm »
Have you already benchmarked anything? If so, with which results?

I'd suggest posting the source code along with the results.

10
General / Rect class
« on: July 21, 2010, 01:16:32 pm »
I agree, X,Y & W,H sounds much more practical to my ear. Considering that I haven't really dealt with rects since I switched to SFML from SDL, my opinion could be a bit biased.

11
General / Pixel Perfect Collision
« on: July 19, 2010, 10:20:51 pm »
Do you understand the fundamental idea behind the pixel perfect collision detection though?

I mean, take two rectangles, then see if they intersect. If they do, make third rectangle from the part which intersects. Then scan trough that rectangle. Per every pixel, see if both of the two inital rectangles have pixel on that coord. If yes, return true. Else keep scanning.

The idea is simple. If you get errors, then the code is wrong. If you get no errors but the code doesn't work, then the logic is wrong.

12
SFML projects / Gravity Particles Simulator (sandbox)
« on: July 16, 2010, 03:53:45 pm »
Ha, looks great. :) I wrote something similar in Qbasic many years ago. Actually I guess I'll make my own version, with a slight twist.

Oh, and nice website. ;)

13
General / Basic game loop question
« on: November 04, 2009, 09:02:53 am »
Let's just say that the method used there isn't the most optimal or best available. (For example the frame delaying should be with Sleep(int ms) function, to preserve system resources and update the game world only once per frame).

With SFML you can use SetFramerateLimit() and GetFrameTime() methods(sf::Window) for example.

Of couse it's quite trivial to come up with your own functions for these, if you want to.

14
Graphics / Small problem with Sprites
« on: October 18, 2009, 04:19:30 am »
So every sf::Sprite object needs it's own sf::Image object?

That makes sense though, and the symptoms indicate to it.

Thank you. :)

15
Graphics / Small problem with Sprites
« on: October 18, 2009, 03:15:16 am »
Consider the following two chunks of code:
sprite.hpp:
Code: [Select]

/* sprite.h */
#ifndef SPRITE_H_
#define SPRITE_H_
#include <string>
#include <SFML/Graphics.hpp>
class CSprite {
    friend class CEngine;
private:
    sf::Image m_TempImage;
    sf::Sprite s_Hero[4];
    sf::Sprite s_Grass;
    sf::Sprite s_Gravel;
    sf::Sprite s_Sky;
public:
    CSprite();
    ~CSprite();
    bool LoadSprite(std::string filename, sf::Sprite &sprite);

};
#endif

and sprite.cpp:
Code: [Select]

/* sprite.cpp */
#include "include/sprite.hpp"

CSprite::CSprite() {
/* Constructor */
}

CSprite::~CSprite() {
/* Destructor */
}

    bool CSprite::LoadSprite(std::string filename, sf::Sprite &sprite) {
if(!m_TempImage.LoadFromFile(filename)) {
   return true;
} else {
sprite.SetImage(m_TempImage);
return false;
}
}


The files obviously try to handle sprite loading. The problem however is that when I call LoadSprite for example like this:
Code: [Select]

    Sprites.LoadSprite("gravel.png", Sprites.s_Gravel);
    Sprites.LoadSprite("grass.png", Sprites.s_Grass);
    Sprites.LoadSprite("sky.png", Sprites.s_Sky);


ONLY the last sprite seems to have the correct binded to it. All the previous sprites will have the LAST sprites image in them. Consider the following example:
Code: [Select]

    Sprites.s_Grass.SetPosition(0,0);
    m_App.Draw(Sprites.s_Grass);

    Sprites.s_Gravel.SetPosition(8,8);
    m_App.Draw(Sprites.s_Gravel);

    Sprites.s_Sky.SetPosition(16,16);
    m_App.Draw(Sprites.s_Sky);


Yields 3 Sprites.s_Sky sprites, instead of 3 different sprites.

If I change the order of LoadSprite functions, to load say s_Gravel as the last sprite, then ALL the loaded sprites will have the image of s_Gravel.

I suspect that I am doing something wrong with the m_TempImage in the LoadSprite function declaration. Though, I am not sure.

Any thoughts?

Pages: [1] 2