Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: sf::Vertex* quad = &m_vertices[(i + j * width) * 4]; from C++ to C#.  (Read 19778 times)

0 Members and 1 Guest are viewing this topic.

Anteara

  • Newbie
  • *
  • Posts: 40
    • View Profile
    • Email
Re: sf::Vertex* quad = &m_vertices[(i + j * width) * 4]; from C++ to C#.
« Reply #30 on: July 18, 2013, 11:26:14 am »
Alright, thanks.

So what I've done is whenever the window is activated, the timer is set to true, otherwise, it's set to false.

Then, I do my drawing in the timer tick function. I set the interval of Timer Tick to 1; but it still does not call anywhere near as often as my while loop did. However, this works for me as because it doesn't tick often, it gives me an effective framerate of 64 FPS.

However, Because my game doesn't draw at 500 fps anymore, It's quite jumpy to move the view so i can see all parts of the map.

Previously this would result in a clean movement of the map:

if (Keyboard.IsKeyPressed(Keyboard.Key.Left))  
    view.Move(new Vector2f(1.25f, 0));

However, the only way I can produce a scrolling speed that I'm happy with is to do this:

if (Keyboard.IsKeyPressed(Keyboard.Key.Left))
    view.Move(new Vector2f(10.0f, 0));

But it is quite 'jumpy', so I need a way to smooth it out. Since my timer is a millisecond timer, and I already have it set to 1, that's as fast as I can get the timer to tick.

Do you have any idea how I could do this?

P.S. by doing this, I've got my engine down from 12-17% CPU usage to less than 0.1% CPU usage.

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
Re: sf::Vertex* quad = &m_vertices[(i + j * width) * 4]; from C++ to C#.
« Reply #31 on: July 18, 2013, 11:48:53 am »
Hum... 60 FPS should be enough to achieve smooth movements, I don't know why it's jumpy.

But are you really moving your view of 600 units per second? It's seem a lot -- unless the view is zoomed out.

By the way, you should probably not use Keyboard.IsKeyPressed, because that will make your view move even if your window doesn't have the focus.
Laurent Gomila - SFML developer

Anteara

  • Newbie
  • *
  • Posts: 40
    • View Profile
    • Email
Re: sf::Vertex* quad = &m_vertices[(i + j * width) * 4]; from C++ to C#.
« Reply #32 on: July 18, 2013, 12:41:02 pm »
Yeah I'm zoomed in when I'm trying to move it like that, I think i'll use a scrollbar or something from windows forms and implement a variable to position the middle of the view relative to the scrollbar, or something...

haha.

Anteara

  • Newbie
  • *
  • Posts: 40
    • View Profile
    • Email
Re: sf::Vertex* quad = &m_vertices[(i + j * width) * 4]; from C++ to C#.
« Reply #33 on: July 19, 2013, 10:52:08 am »
Hey, I have one more question if that's okay:

Since editing my code greatly to try and improve the speed, etc (I didn't save a backup :/) I've now  been getting this error:

$exception{System.AccessViolationException: Attempted to read or write protected memory. This is often an indication that other memory is corrupt.
at SFML.Graphics.Context.sfContext_create()
at SFML.Graphics.Context..ctor()
at SFML.Graphics.Context.get_Global()
at SFML.Graphics.Texture.Destroy(Boolean disposing)
at SFML.ObjectBase.Dispose(Boolean disposing)
at SFML.ObjectBase.Finalize()}

I only get this error on exit. It doesn't affect my program at all, I just can't exit without getting the error.

It seems to reference Graphics and ObjectBase a lot, however I have no idea what is causing the error, so I can't make a complete and minimal example. Do you have any idea what might be causing this?

Otherwise, I guess i'll have to backtrack and remove code until it stops getting the error, and re-add that part.

Thanks.

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
Re: sf::Vertex* quad = &m_vertices[(i + j * width) * 4]; from C++ to C#.
« Reply #34 on: July 19, 2013, 10:59:30 am »
I have no idea, sorry.
Laurent Gomila - SFML developer

Anteara

  • Newbie
  • *
  • Posts: 40
    • View Profile
    • Email
Re: sf::Vertex* quad = &m_vertices[(i + j * width) * 4]; from C++ to C#.
« Reply #35 on: July 19, 2013, 11:10:44 am »
Alright, well i guess i can always just do this until I decide I want to fix it, lol.

        private void OnProcessExit(object sender, EventArgs e)
        {
            //crashes on exit if i dont do this
            Environment.Exit(1);
        }

zsbzsb

  • Hero Member
  • *****
  • Posts: 1409
  • Active Maintainer of CSFML/SFML.NET
    • View Profile
    • My little corner...
    • Email
Re: sf::Vertex* quad = &m_vertices[(i + j * width) * 4]; from C++ to C#.
« Reply #36 on: July 19, 2013, 01:18:35 pm »
Quote
at SFML.Graphics.Context.sfContext_create()
at SFML.Graphics.Context..ctor()
at SFML.Graphics.Context.get_Global()
at SFML.Graphics.Texture.Destroy(Boolean disposing)
at SFML.ObjectBase.Dispose(Boolean disposing)
at SFML.ObjectBase.Finalize()}

Your error is happening when a texture is being disposed by the garbage collector. When the GC disposes SFML objects Laurent attempts to create a SFML context to ensure there is still an active context to dispose of the OpenGL resources. See this topic for more information there. However you can ensure SFML will never attempt to create contexts as long as you call sfmlObject.Dispose() on all of your SFML objects.

When you do your cleanup be sure you call Dispose() on all of your textures/font/rendertextures before letting your program exit.

On a side note you should seriously consider creating a git repository in your source code directory. And then it will be super easy to go see what you changed :D You could even open a free account on BitBucket and push your source code there for additional safety and backups. I recommend BitBucket for unlimited private repos and Github for public repos.
« Last Edit: July 19, 2013, 01:22:24 pm by zsbzsb »
Motion / MotionNET - Complete video / audio playback for SFML / SFML.NET

NetEXT - An SFML.NET Extension Library based on Thor

Anteara

  • Newbie
  • *
  • Posts: 40
    • View Profile
    • Email
Re: sf::Vertex* quad = &m_vertices[(i + j * width) * 4]; from C++ to C#.
« Reply #37 on: July 21, 2013, 09:07:41 am »
Alright thanks, when i get a working version I think i'll upload that on GitHub and make revisions so I don't end up in the same situation and I can debug when something bad happens.

I started Implementing IDisposable to try and fix the problem. Dispose calls ReleaseManagedResources().

And I indeed found exactly where my error comes from:

void ReleaseManagedResources()
{
    Console.WriteLine("Releasing Managed Resources");
    if (MVertices != null)
    {
        MVertices.Dispose();
    }
    if (MTileset != null)
    {
        MTileset.Dispose();
        //Exception - Attempted to read or write on protected memory.
    }
    if (TransformInstance != null)
    {
        TransformInstance.Dispose();
    }
}

MTileset is a Texture. I don't change MTileset in any way.

Any ideas? If not; i'll try and make a small example with the error happening.

Edit: This also happens when I try to dispose of a VertexArray, but my program doesn't crash; it only crashes with the texture.

http://i.imgur.com/dkssvv9.png

Edit 2: I seem to have fixed it. Instead of disposing it when I don't need it anymore, I dispose it inside the WinForms dispose method, and it fixes it, my only hypothesis of why this fixes it is because even though it was disposed, the checks done by SFML somehow referenced it and tried to read it in some way, so by disposing it after everything else fixes it because there's now nothing to check

...or something.
« Last Edit: July 21, 2013, 02:45:00 pm by Anteara »

 

anything