SFML community forums

General => General discussions => Topic started by: Sigvatr on September 03, 2009, 11:41:27 am

Title: Sigvatr's Newbie Thread
Post by: Sigvatr on September 03, 2009, 11:41:27 am
I figured I'd make a thread for this in general because I have a lot of questions to ask all the time that sometimes go into separate sub-forums, so I figured I'd just save space by keeping all of my questions in one thread.

Today's question: Does SetFramerateLimit apply only to graphics rendering or to code that runs in a Window in general? Is there an easy way to distinguish the rendering code from the logic code, say if I want some code to run at unlimited FPS and have the screen update at a rate of 6fps?
Title: Sigvatr's Newbie Thread
Post by: Laurent on September 03, 2009, 01:44:06 pm
Quote
I figured I'd make a thread for this in general because I have a lot of questions to ask all the time that sometimes go into separate sub-forums, so I figured I'd just save space by keeping all of my questions in one thread.

Well, it's not a matter of saving space (it's all virtual space ;)), it's more important to get topics properly categorized / entitled so that other people can easily know what you're talking about, or find relevant informations.

But anyway, it's ok if you prefer doing things like this.

Quote
Does SetFramerateLimit apply only to graphics rendering or to code that runs in a Window in general?

The framerate limit applies to the thread in which window.Display() is called. No matter what was done before, when call window.Display() your current thread will sleep for a while in order to adapt to the framerate limit.

Quote
Is there an easy way to distinguish the rendering code from the logic code, say if I want some code to run at unlimited FPS and have the screen update at a rate of 6fps?

You have to handle it yourself. Usually, threads are used to create separate flows that can run at their own speed. But they introduce a complexity in your program that you probably don't want.
Title: Sigvatr's Newbie Thread
Post by: Sigvatr on September 04, 2009, 05:34:03 pm
Alright, the way I've been thinking of making this work is as follows.

This is how an ordinary application with both a 60fps logic and rendering code would go:

Code: [Select]
SYS.SetFramerateLimit(60);
while(SYS.IsOpened())
{
    // All of the game logic here

    // Render code here
}


What I want to do is have some parts of my logic to run at 60fps and some of it to run as fast as the machine can go.

So would I be able to do something like this?

Code: [Select]
SYS.SetFramerateLimit(60);
bool gameRunning = true;
while( gameRunning = true )
{
    // Machine speed game logic here

    if(SYS.IsOpened())
    {
        // 60fps game logic here

        // Render code here
    }
}
Title: Sigvatr's Newbie Thread
Post by: Laurent on September 04, 2009, 05:40:47 pm
No, the execution flow is sequential so anything which is in the same thread will be affected by the FPS limit.

And "SYS.IsOpened()" will always evaluate to true until you close your application so there's absolutely no difference between your two loops.

You must use threads to have separate flows of execution.
Title: Sigvatr's Newbie Thread
Post by: Sigvatr on September 04, 2009, 05:42:16 pm
So basically all of the code that runs in my int main() is going to run at the frame limit unless I make threads?
Title: Sigvatr's Newbie Thread
Post by: Laurent on September 04, 2009, 05:50:16 pm
Your loop looks like this:
Code: [Select]
while (running)
{
   ...

   sleep(x); // stops for a while

   ...
}


So yes, everything inside will be affected by the pause.
Title: Sigvatr's Newbie Thread
Post by: Sigvatr on September 04, 2009, 06:03:43 pm
Basically what I am trying to do is provide my own built in music tracking format (which supports randomly generated music in game). All instruments are loopable, but they don't necessarily loop back to the beginning of the sample but to a predefined position in the sound file that I have to specify manually so that the looping is smooth and has no clicks or fuzz in them. Because of the very precise time scales that this looping requires (in some cases 0.001 of a second or less) I have deduced that running the music code in the frame limited game code won't allow me those very precise time specifications for the looping to sound right.

Do you have any other ideas that might over come this, or do you think that threading is necessary for this to work properly? I don't have a problem with figuring out threads if I need to do so, since this is a multiplayer game I am going to have to get around to threading eventually anyway.
Title: Sigvatr's Newbie Thread
Post by: Laurent on September 04, 2009, 06:34:04 pm
Using a separate thread for audio-specific stuff sounds good, and I think there's no other solution anyway.
Title: Sigvatr's Newbie Thread
Post by: Meltra Bour on September 04, 2009, 08:11:32 pm
hmm, just thinking out loud here. Way not something like ...

Code: [Select]
sf::Window win;
sf::Clock drawTimer;

while( win.IsOpened() )
{
    // Machine speed game logic here

    if(drawTimer.GetElapsedTime() > 0.016666)
    {
        drawTimer.Reset();

        // 60fps game logic here
        // Render code here
    }
}


If you need 0.001 timers you need to make sure your render/logic loop never takes longer then that. That said, separate threads sound like a better solution but this could do the trick if you don't wane mess with threads yet.
Title: Sigvatr's Newbie Thread
Post by: Sigvatr on September 06, 2009, 12:54:46 am
I have a few new questions.

No matter what I am setting my Framerate Limit to, I am always getting incorrect readings, and they vary depending on the system I am using.

When I run the app on my laptop (Vista, dual core), my FPS counter states a much higher frame rate per second than should actually be happening, up to 1.6 times higher than I have set it.

When I run the app on my PC (XP, quad core), my FPS counter states a much lower frame rate, around half of what it should be.

They both have a refresh rate of 60hz.

I am using the same code on both computers, and the FPS code I am using is exactly the same as the one in the timing tutorial.

Anyone know what could be screwing this up?

My second question is about the render window not displaying when my app goes out of sync. If my computer changes screen resolutions or alt tabs another application over my app and then back, the window will not update, it just appears either black or an empty window, period.

Anyone know what is wrong?
Title: Sigvatr's Newbie Thread
Post by: phear- on September 06, 2009, 02:35:15 am
If your trying to limit your FPS to 60, then turn v-sync on. Although its wierd because at times on my windows 7 desktop i get bursts of frames up to 13k even though it mostly stays at 60fps, while on my vista laptop it always stays at 60
Title: Sigvatr's Newbie Thread
Post by: Sigvatr on September 06, 2009, 10:05:25 am
I'm trying to set FPS to 320 for some logic code.

Is it possible to set threads to run at a specified speed?
Title: Sigvatr's Newbie Thread
Post by: Hiura on September 06, 2009, 11:46:43 am
You can manage the speed of your loops yourself : use sleep function to not go too fast.

PS : is the above sentence grammatically correct ?
Title: Sigvatr's Newbie Thread
Post by: Sigvatr on September 06, 2009, 01:37:49 pm
Can anyone point me towards anything that will explain to me how to use bitmap fonts?

I want to import my font from a graphical image and not a TTF.
Title: Sigvatr's Newbie Thread
Post by: Hiura on September 06, 2009, 03:25:40 pm
google, but why don't you want to use ttf files ?
Title: Sigvatr's Newbie Thread
Post by: Sigvatr on September 14, 2009, 05:07:59 pm
Hey guys,

I have been doing some great work so far. I have written an algorithm that converts height maps into normal maps very quickly and actually extends upon the height map concept by using more than a single color channel in the height map file to allow me to use additional parameters.

Anyway, on to my questions.

1) I am trying to implement normal mapping into my game (obviously, since I made a height map to normal map converter), but I have a few misunderstandings about how SFML implements GLSL. If I am correct, the PostFX class is the SFML link to GLSL functionality. It says that the PostFX class only applies shaders to the entire screen. I'm not interested in doing this, however, I only need to apply the shader to specific parts of the screen, including sprites and such which will have an alpha channel (which makes this a little bit messy). Is there a way to apply shaders without the PostFX class, or without applying it to the whole screen?

2) Is it possible to compile shader files into your program and not have them stored outside of the .exe?

3) Does anyone have a decent understanding of how 2d normal mapping with point lighting works in general? I've been surfing the web trying to figure out how to implement normal mapping but most tutorials cater towards a specific language or don't use GLSL (I'm using C++).

Thanks guys,
- Sig
Title: Sigvatr's Newbie Thread
Post by: Laurent on September 14, 2009, 07:02:30 pm
Hi

1) This will be possible in SFML 2. However you should be able to use fragment shaders in 1.5, as SFML doesn't reset this kind of states when rendering drawables.

2) Yes, just like any other resource. Have a look at the wiki for some useful code (search "DAT files") ;)
Title: Sigvatr's Newbie Thread
Post by: Sigvatr on September 14, 2009, 08:02:17 pm
I'm having a problem using the PostFX class, the output from all of my effects are flipped upside down. I can even input a raw image and it will spit it out flipped vertically.

I'm not sure if this is a bug, a problem with my graphics card and/or drivers or an intentional feature.

Have you ever heard of this happening before?
Title: Sigvatr's Newbie Thread
Post by: Laurent on September 14, 2009, 08:05:20 pm
Can you show an example?
Title: Sigvatr's Newbie Thread
Post by: Sigvatr on September 14, 2009, 08:50:13 pm
My drivers are out of date, I will get back to you once I have updated them and restarted.

Basically, the shader output was flipped vertically for some reason. It's that simple. Not an 180 degree rotation but an actual flip.
Title: Sigvatr's Newbie Thread
Post by: Sigvatr on September 14, 2009, 08:59:06 pm
Alright, turns out it wasn't my drivers, the PostFX output is still flipped upside down.

Aside from the fact that it is upside down it renders perfectly.
Title: Sigvatr's Newbie Thread
Post by: Laurent on September 14, 2009, 09:03:38 pm
Ok, so now you can show me your source code / effect ;)
Title: Sigvatr's Newbie Thread
Post by: Sigvatr on September 14, 2009, 09:15:33 pm
Haha ok.

What I am concerned about is whether or not this is simply a problem on my computer or some other peoples, so that when I release my game some people will have flipped images and some won't.

MAIN.CPP
Code: [Select]
#include <SFML/Graphics.hpp>
#include "type_conversion.h"

using namespace sf;
using namespace std;

// Normal Mapping Test
int main()
{
    /* Create window */
RenderWindow SYS(VideoMode(512, 512, 32), "Annihilator", 1, WindowSettings(24, 8, 0));
SYS.SetFramerateLimit(0);
SYS.UseVerticalSync(0);

    /* Declare clocks */
    Clock SystemClock;
Clock ClockSeconds;

/* Load System Font */
Font System;
if(!System.LoadFromFile("systemfont.ttf"))
{
return EXIT_FAILURE;
}
String SystemFont("Frame Processing Time: 0.0000ms", System, 15.882f);

/* Load normal map */
Image *NormalMapImage = new Image;
NormalMapImage->LoadFromFile("normalmap.png");

/* Load shader */
PostFX *NormalMap = new PostFX;
NormalMap->LoadFromFile("normalmapping.sfx");
NormalMap->SetTexture("NormalMap", NormalMapImage);
int mouseX;
int mouseY;

// Test area


    // Main loop
    while(SYS.IsOpened())
    {
        /* Receive input */
        Event Event;
        while(SYS.GetEvent(Event))
        {
            /* Escape key pressed */
if( ( Event.Type == Event::KeyPressed ) && ( Event.Key.Code == Key::Escape ) ) { SYS.Close(); }

/* Mouse moved */
if( Event.Type == Event::MouseMoved ) { mouseX = Event.MouseMove.X; mouseY = Event.MouseMove.Y;  }
}
/* Determine light positiong */
NormalMap->SetParameter( "lightPosition", float(mouseX), float(mouseY) );

/* Clear screen */
        SYS.Clear( Color( 128, 128, 128 ) );

/* Render shader */
SYS.Draw(*NormalMap);

/* Render text */
SystemFont.SetColor(Color(0, 0, 0));
SystemFont.SetPosition(3,-3);
SYS.Draw(SystemFont);
SystemFont.SetColor(Color(255, 255, 255));
SystemFont.SetPosition(2,-4);
SYS.Draw(SystemFont);

        /* Render buffer to screen */
        SYS.Display();

/* Reset clock */
if ( ClockSeconds.GetElapsedTime() >= 0.5 )
{
SystemFont.SetText("Frame Processing Time: " + FloatToString( SystemClock.GetElapsedTime() * 1000 ) + "ms");
ClockSeconds.Reset();
}
Sleep( 1.000f / 60.000f - SystemClock.GetElapsedTime() );
SystemClock.Reset();
    }
    return EXIT_SUCCESS;
}


NORMALMAPPING.SFX (this doesn't actually normal map yet, it just renders the original texture)
Code: [Select]
texture NormalMap
vec2 lightPosition

effect
{
    // Get the value of the current screen pixel
    vec4 pixel = NormalMap(_in);

    // ...
    float red = pixel.r;

    // ...
    _out = vec4(pixel.r,pixel.g,pixel.b,255);
}
Title: Sigvatr's Newbie Thread
Post by: Laurent on September 14, 2009, 10:49:21 pm
Your code looks ok. Which version of SFML are you using? Did you try the post-fx sample from the SDK? As far as I know the images are not flipped in it.
Title: Sigvatr's Newbie Thread
Post by: Sigvatr on September 14, 2009, 10:50:44 pm
1.5. Yes, I tried that sample. Everything gets flipped for some reason and as far as I am aware there is no way to flip a PostFX object.
Title: Sigvatr's Newbie Thread
Post by: Laurent on September 14, 2009, 10:51:31 pm
What is your configuration?
Title: Sigvatr's Newbie Thread
Post by: Sigvatr on September 14, 2009, 11:02:44 pm
Er, I don't know what you mean :/
Title: Sigvatr's Newbie Thread
Post by: Laurent on September 15, 2009, 12:27:15 am
Graphics card, driver, OS, CPU, ...
Title: Sigvatr's Newbie Thread
Post by: Sigvatr on September 15, 2009, 03:51:03 pm
EN9800 GT with latest drivers, XP Service Pack 3, Intel Core 2 Quad CPU Q6600 @ 2.40GHz, 3.00 GB of RAM.
Title: Sigvatr's Newbie Thread
Post by: Laurent on September 15, 2009, 03:57:48 pm
As far as I know, you're the only one to report this kind of issue about post-effects :?
Title: Sigvatr's Newbie Thread
Post by: Sigvatr on September 15, 2009, 05:34:06 pm
Until the next SFML version comes out (when is that going to be anyway?), how can I side step this issue of PostFX rendering to the entire screen? Is there any easy edits I could make to the source code, or do you think that I should just manually use OpenGL without the SFML plugs?

EDIT: By the way, everyone who I have sent my program to for testing gets the vertical flipping too.

And here's what my normal mapping looks like in case you're interested:

(http://www.sigvatr.com/junk/normalmapping.jpg)
Title: Sigvatr's Newbie Thread
Post by: Laurent on September 15, 2009, 05:44:54 pm
I think you'd better use OpenGL directly. You can clone the PostFx class and adapt its Render function, it should be easy to do ;)
Title: Sigvatr's Newbie Thread
Post by: Sigvatr on September 15, 2009, 11:31:10 pm
Alright, OpenGL translation has been going good so far, but I would prefer not to have to use PreserveOpenGLStates if I can avoid it.

How can I manage my OpenGL states manually in the code so that my .Draw and .Display don't screw it up?

Cheers.
Title: Sigvatr's Newbie Thread
Post by: Sigvatr on September 16, 2009, 02:40:05 am
Also another question, I noticed that loading compressed DDS textures didn't have any effect on my VRAM usage compared to any other file format. Is there a way to store and make use of compressed DDS textures in SFML programs? Right now, I think SFML can load DDS files but stores them just the same as any other image.
Title: Sigvatr's Newbie Thread
Post by: Laurent on September 16, 2009, 08:52:27 am
Quote
Alright, OpenGL translation has been going good so far, but I would prefer not to have to use PreserveOpenGLStates if I can avoid it.

How can I manage my OpenGL states manually in the code so that my .Draw and .Display don't screw it up?

There's nothing more to do than what PostFx does (reset current fragment program and textures on units > 0).

Quote
Also another question, I noticed that loading compressed DDS textures didn't have any effect on my VRAM usage compared to any other file format. Is there a way to store and make use of compressed DDS textures in SFML programs? Right now, I think SFML can load DDS files but stores them just the same as any other image.

You're right, and there's currently no way to use compressed textures in SFML. Do you really need it?
Title: Sigvatr's Newbie Thread
Post by: Sigvatr on September 16, 2009, 09:56:57 am
I'm using a streaming method to load into memory only the graphics that I need at any particular time and I want to take advantage of that and squeeze as much detail as possible in there.

Although I'm sure that I can probably just tinker with OpenGL to get the DDS to work?

I'm considering using SFML for all non-graphical things and then trying my luck with doing straight forward OpenGL code for all of the graphics. The graphics capabilities are good for people breaking into programming and making hobby games, but I don't think they are flexible enough at the moment for my needs.

Also, btw, how do I make an sf::Window fullscreen?
Title: Sigvatr's Newbie Thread
Post by: K-Bal on September 16, 2009, 10:16:39 am
Quote from: "Sigvatr"

Also, btw, how do I make an sf::Window fullscreen?


Give the constructor of sf::RenderWindow for the third parameter a sf::Style::Fullscreen.
Title: Sigvatr's Newbie Thread
Post by: Laurent on September 16, 2009, 10:46:07 am
Quote
Although I'm sure that I can probably just tinker with OpenGL to get the DDS to work?

Like in the OpenGL sample of the SDK, you can use sf::Image to load the image file and then pass its pixels to your own OpenGL texture.

Quote
The graphics capabilities are good for people breaking into programming and making hobby games, but I don't think they are flexible enough at the moment for my needs.

Can you describe the kind of project you're working on? I'm curious :)
Title: Sigvatr's Newbie Thread
Post by: Sigvatr on September 16, 2009, 07:47:01 pm
Well basically I worked with as a graphic artist with a programmer for over two years to make a game. We split up a couple of weeks ago because we couldn't agree on a lot of stuff, even though the game was getting pretty far into development, and also because he wasn't implementing enough of the features I wanted because he was too lazy. So, not knowing very much about programming at all, I decided to keep all of my artwork and learn how to code the game by myself.

I've only been programming this game for around two weeks now, but essentially I'm creating a game that has already been thoroughly designed and documented over the past few years, so I know pretty much exactly what I need to do to get it done.

That being said, it's a team-based 2d platformer with cross-platform online multiplayer support and a unified campaign that players all around the world can participate and have an influence over. It's actually distinctly different from the game I was working on before, but that's because I have complete control over it this time.

Some of the features the game is going to have:
- Normal, specular and luminescent mapping
- 6 layered parallax scrolling
- A versatile random map generation system, including the ability to randomly generate map graphics. All maps are based on seeds and generated client side, so servers never need to transfer files to players. Maps are stored on the hard drive in image format and streamed into memory when they are needed, allowing for very large, high quality maps.
- Destructable terrain
- World-wide, unified campaign that all players can participate in and have an influence over
- Dynamic, procedurally generated music

Obviously this is an extremely ambitious project, especially for a single person, and especially for someone who has only been using C++ for about two weeks now. However, I've been working on the game for > 80 hours a week since I started working on it two weeks ago, so I've been making fast progress. Also, the fact that I've already for the most part documented the design of this game over the past few years helps me to move ahead quicker too because I already know what I'm supposed to be doing.

We were using Irrlicht before, but I didn't really like it because it was slow and full of 3d stuff I didn't want. I'm using SFML primarily because I want cross-platform capability.

I just want to clear up any misconceptions about me being clueless or strictly a hobbyist. To be honest, I think coming up with your own functional normal mapping shader algorithm within two weeks of learning C++ is a pretty good effort.
Title: Sigvatr's Newbie Thread
Post by: Laurent on September 16, 2009, 08:45:20 pm
Quote
Obviously this is an extremely ambitious project

This is exactly was I was going to say after reading the list of features ;)

Quote
especially for someone who has only been using C++ for about two weeks now

:shock:
If you really succeed to make this game then... WOW.
Title: Sigvatr's Newbie Thread
Post by: Spidyy on September 16, 2009, 08:50:50 pm
It will take several mounth.. Maybe years to complete, but the result will be awesome. Cheers. =p

Quote
- Dynamic, procedurally generated music


I'm not fan of the generated music. :( One only and single music is, I think, greatly better to listen than generated. Even if it don't apply to the action, having one great musical theme is better than stop randomly played instrumentation (For exemple : Dark Age of Camelot) or channel-separated played music (Unreal Tournament 3) =p

It is my opinion.
Title: Sigvatr's Newbie Thread
Post by: Sigvatr on September 16, 2009, 09:03:53 pm
Well, I have estimated that I could take up to 4 years to complete this project, but I have the time and resources to dedicate towards that. Accomplishing difficult tasks is not the reason that I have set such a long time frame for this game, but because I want to pay a lot of attention to detail.

I agree that dynamic and/or random music in the past has mostly been very bad. I've done a lot of research in music theory and I'm taking a completely different approach to procedurally generated music than has ever been tried before. My method is primarily based on Hindemith's theory of dissonant intervals, but I'm still working on the design documents at the moment so it will be a while before I try and implement it.

I already have a rudimentary dynamic music system put in place in the code but it's pretty useless so far because I haven't applied a musical composition model to it yet.
Title: Sigvatr's Newbie Thread
Post by: Sigvatr on September 17, 2009, 11:59:19 am
When I load a .dds using sf::Image, does the LoadFromFile function convert the .dds into a bitmap, or does it load the .dds exactly how it is?

If the latter, what method can I use to load .dds files into memory without converting it into a bitmap?
Title: Sigvatr's Newbie Thread
Post by: Laurent on September 17, 2009, 03:04:32 pm
I think I already answered this question, didn't I?

Quote
Quote
Also another question, I noticed that loading compressed DDS textures didn't have any effect on my VRAM usage compared to any other file format. Is there a way to store and make use of compressed DDS textures in SFML programs? Right now, I think SFML can load DDS files but stores them just the same as any other image.

You're right, and there's currently no way to use compressed textures in SFML
Title: Sigvatr's Newbie Thread
Post by: Sigvatr on September 17, 2009, 03:07:41 pm
Alright, I just got confused when you said:

Quote
Like in the OpenGL sample of the SDK, you can use sf::Image to load the image file and then pass its pixels to your own OpenGL texture.
Title: Sigvatr's Newbie Thread
Post by: Laurent on September 17, 2009, 03:10:26 pm
Ok I see. So yes, sf::Image always returns you an array of 32 bits RGBA pixels (like the documentation (should) say) ;)
Title: Sigvatr's Newbie Thread
Post by: Sigvatr on September 20, 2009, 12:07:45 am
I'm having issues getting Fullscreen to work in Windows Vista. When I run my SFML app in XP, there are no problems changing to the new screen resolution. On my Vista computer, the application doesn't technically become fullscreen. Instead, it stretches the RenderWindow to the size of the current screen resolution and also includes a title and border.

Here's my code:

Code: [Select]
/* Create window */
RenderWindow *Annihilator = new RenderWindow;
Annihilator->Create(
VideoMode( windowWidth, windowHeight, 32 ), /* Window width, height and color bit depth */
"Annihilator Game Engine Version 0.1.0.0", /* Window title */
Style::Fullscreen, /* Window mode */
WindowSettings( 24, 8, 0 ) /* Depth buffer, stencil buffer and antialiasing level */
);
/* Set window parameters */
Annihilator->SetFramerateLimit( 60 );
Annihilator->UseVerticalSync( 1 );
Annihilator->ShowMouseCursor( 0 );
Annihilator->SetCursorPosition( windowWidth / 2, windowHeight / 2 );
Annihilator->PreserveOpenGLStates( 1 );