Sign in to follow this  
ScopeDynamo

[.net] Sdl.Net FlipScreen fails?

Recommended Posts

Any idea why whenever I call Sdl.SDL_Flip( scrnPointer ) I get an exception thrown that says attempting to access protected memory? The pointer I pass is the one returned by this
  vidPtr = Sdl.SDL_SetVideoMode(
                      width,
                      height,
                      depth,
                      Sdl.SDL_OPENGL | Sdl.SDL_DOUBLEBUF );
the window is created fine so I'm not sure why it doesn't work. Is there another way of flipping the gl display? Here's the full class library source,
using System;
using System.Collections.Generic;
using System.Text;
using Tao.Sdl;
using Tao.OpenGl;
using Tao.DevIl;


namespace Vivid2D
{
    public class Engine
    {
        public IntPtr vidPtr;
        int dwidth, dheight, ddepth;
        public Engine()
        {
        }
        public Engine(int width, int height, int depth)
        {
            OpenScreen(width, height, depth, false);
        }
        public Engine(int width, int height, int depth,bool fullscreen)
        {
            OpenScreen(width, height, depth, fullscreen);
        }

        ~Engine()
        {

        }
        /// <summary>
        /// Opens a new window or full screen display
        /// and returns true on success.
        /// </summary>
        /// <param name="width">Width of the display</param>
        /// <param name="height">Height of the display</param>
        /// <param name="depth">Depth in bits of the display</param>
        /// <param name="fullscreen">True to open a full screen, false for a windowed display.</param>
        /// <returns>True on success, false if failed.</returns>
        public bool OpenScreen(int width, int height, int depth,bool fullscreen)
        {
            int flags;
            if (fullscreen == true)
            {
                flags = (Sdl.SDL_HWSURFACE | Sdl.SDL_DOUBLEBUF | Sdl.SDL_ANYFORMAT | Sdl.SDL_FULLSCREEN);
            }
            else
            {
                flags = (Sdl.SDL_HWSURFACE | Sdl.SDL_DOUBLEBUF | Sdl.SDL_ANYFORMAT);
            }
            int init = Sdl.SDL_Init(Sdl.SDL_INIT_EVERYTHING);
            Sdl.SDL_GL_SetAttribute(Sdl.SDL_GL_RED_SIZE, 5);
            Sdl.SDL_GL_SetAttribute(Sdl.SDL_GL_GREEN_SIZE, 5);
            Sdl.SDL_GL_SetAttribute(Sdl.SDL_GL_BLUE_SIZE, 5);
            Sdl.SDL_GL_SetAttribute(Sdl.SDL_GL_DEPTH_SIZE, depth);
            Sdl.SDL_GL_SetAttribute(Sdl.SDL_GL_DOUBLEBUFFER, 1);
            
           
            vidPtr = Sdl.SDL_SetVideoMode(
                      width,
                      height,
                      depth,
                      Sdl.SDL_OPENGL | Sdl.SDL_DOUBLEBUF );
            dwidth = width;
            dheight = height;
            ddepth = depth;
            SetupGL();
            SetColor(255, 255, 255);
            return true;
        }
        public void SetupGL()
        {
         Gl.glViewport(0, 0, dwidth,dheight);
         Gl.glMatrixMode(Gl.GL_PROJECTION);
         Gl.glLoadIdentity();
         Tao.OpenGl.Glu.gluOrtho2D(0, dwidth, 0, dheight);
         Gl.glMatrixMode(Gl.GL_MODELVIEW);
         Gl.glLoadIdentity();
        }
        public void ClearScreen()
        {
            Gl.glClear(Gl.GL_COLOR_BUFFER_BIT);
        }
        public void FlipScreen()
        {
            Sdl.SDL_Flip(vidPtr);
        }
        public void SetColor(float red, float green, float blue)
        {
            Gl.glColor3f(red / (float)255.0, green / (float)255.0, blue / (float)255.0);
        }
        public void DrawRect(int x, int y, int width, int height)
        {
            Gl.glBegin(Gl.GL_QUADS);
            Gl.glVertex2i(x, y);
            Gl.glVertex2i(x + width, y);
            Gl.glVertex2i(x + width, y + height);
            Gl.glVertex2i(x, y + height);
            Gl.glEnd();
        }
    }
}

And the example code that uses the above class lib.
using System;
using System.Collections.Generic;
using System.Text;
using Vivid2D;
using Tao.Sdl;

namespace Vivid2D_Example1
{
    class Program
    {
        
        static void Main(string[] args)
        {

            Engine meng = new Engine();

            meng.OpenScreen(640, 480, 16, false);
            while (true)
            {
                meng.ClearScreen();
                meng.DrawRect(20, 20, 200, 200);
                meng.FlipScreen();
            }
        }
    }
}

Share this post


Link to post
Share on other sites
Quote:
Original post by Drew_Benton
When you use SDL with OpenGL, you do not call SDL_FlipScreen, instead you call SDL_GL_SwapBuffers(); function. Of course you will need to find the equivlant for the TAO framework.
Sdl.SDL_GL_SwapBuffers();

Not too complicated [wink]. You can browse the source of Sdl.cs here.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this