[.net] SDL crashes when I click on the window.

Started by
7 comments, last by GameDev.net 18 years, 6 months ago
I'm using sdl, again, and it's working to some extent. I see my gl code being drawn perfectly as it should, some 2d stuff. But as soon as I click on the sdl window it turns white, and goes into a 'not responding' state. I have to exit the application. Anything with sdl setup that can be causing this? This is the sdl set up code I use, using tao's framework.

  int flags;
            if (fullscreen == true)
            {
                flags = (Sdl.SDL_HWSURFACE | Sdl.SDL_DOUBLEBUF | Sdl.SDL_ANYFORMAT | Sdl.SDL_FULLSCREEN | Sdl.SDL_OPENGL );
            }
            else
            {
                flags = (Sdl.SDL_HWSURFACE | Sdl.SDL_DOUBLEBUF | Sdl.SDL_ANYFORMAT | Sdl.SDL_OPENGL );
            }
            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, 0);
            Sdl.SDL_GL_SetAttribute(Sdl.SDL_GL_DOUBLEBUFFER, 1);
            vidPtr = Sdl.SDL_SetVideoMode(
                      width,
                      height,
                      depth,
                      flags );
            dwidth = width;
            dheight = height;
            ddepth = depth;
            SetupGL();
            SetColor(255, 255, 255);
            Il.ilInit();
            Ilut2.ilutRenderer(Ilut2.ILUT_OPENGL);
Advertisement
Hi ant,

How are you handling the event system? I know I've had issues in the past where I've forgotten to keep processing sdl events, and that causes the application to just hang.

Might be worth looking into :)
-----------------------------www.devcrunch.com - independant developer forums
make sure your deriving your main class from form.

can you post full source?
| Member of UBAAG (Unban aftermath Association of Gamedev)
Havn't done any events stuff so maybe poed.

From form? I'm just using a console project template, as i'm using sdl in a class library. Can you explain how to use a form without opening up another redundent window or post an example? I guess the best option would be to derive the class' engine class from form so that each app can just be console or empty to start off with.

Here's the class source.

using System;using System.Collections.Generic;using System.Text;using Tao.Sdl;using Tao.OpenGl;using Tao.DevIl;using Tao.Glfw;namespace Vivid2D{    public class Engine    {        public IntPtr vidPtr;        public static Engine main;        int dwidth, dheight, ddepth;        public Engine()        {            Engine.main = this;        }        public Engine(int width, int height, int depth)        {            OpenScreen(width, height, depth, false);            Engine.main = this;        }        public Engine(int width, int height, int depth,bool fullscreen)        {            OpenScreen(width, height, depth, fullscreen);            Engine.main = this;        }        ~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 mode;            if (fullscreen == true)            {                mode = Glfw.GLFW_FULLSCREEN;            }            else            {                mode = Glfw.GLFW_WINDOW;            }            Glfw.glfwInit();                      if (Glfw.glfwOpenWindow(width, height,0,0,0,0 ,0, 0,mode) == Gl.GL_FALSE)            {                Console.WriteLine("Failed to open window.");                return false;            }           else            {                Console.WriteLine("Window Opened.");            };            SetupGL();            SetColor(255, 255, 255);            Il.ilInit();            Ilut2.ilutRenderer(Ilut2.ILUT_OPENGL);            dwidth = width;            dheight = height;            ddepth = depth;            Glfw.glfwSwapInterval(0);            return true;            */            int flags;            if (fullscreen == true)            {                flags = (Sdl.SDL_HWSURFACE | Sdl.SDL_DOUBLEBUF | Sdl.SDL_ANYFORMAT | Sdl.SDL_FULLSCREEN | Sdl.SDL_OPENGL );            }            else            {                flags = (Sdl.SDL_HWSURFACE | Sdl.SDL_DOUBLEBUF | Sdl.SDL_ANYFORMAT | Sdl.SDL_OPENGL );            }            int init = Sdl.SDL_Init(Sdl.SDL_INIT_VIDEO);            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, 0);            Sdl.SDL_GL_SetAttribute(Sdl.SDL_GL_DOUBLEBUFFER, 1);            vidPtr = Sdl.SDL_SetVideoMode(                      width,                      height,                      depth,                      flags );            dwidth = width;            dheight = height;            ddepth = depth;            SetupGL();            SetColor(255, 255, 255);            Il.ilInit();            Ilut2.ilutRenderer(Ilut2.ILUT_OPENGL);            Sdl.SDL_WM_GrabInput(Sdl.SDL_GRAB_ON);            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();         Gl.glDisable(Gl.GL_LIGHTING);        }        public void ClearScreen()        {            Gl.glClear(Gl.GL_COLOR_BUFFER_BIT);        }        public void FlipScreen()        {            Sdl.SDL_GL_SwapBuffers();                    }        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)        {            y = dheight - y;            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();        }        public void DrawRectUV(int x, int y, int width, int height)        {            y = dheight - y;            Gl.glBegin(Gl.GL_QUADS);            Gl.glTexCoord2f(u0, v0);            Gl.glVertex2i(x, y);            Gl.glTexCoord2f(u1, v0);            Gl.glVertex2i(x + width, y);            Gl.glTexCoord2f(u1, v1);            Gl.glVertex2i(x + width, y - height);            Gl.glTexCoord2f(u0, v1);            Gl.glVertex2i(x, y - height);            Gl.glEnd();        }        public void DrawRectUVFast(int x, int y, int width, int height)        {            y = dheight - y;            Gl.glTexCoord2f(u0, v0);            Gl.glVertex2i(x, y);            Gl.glTexCoord2f(u1, v0);            Gl.glVertex2i(x + width, y);            Gl.glTexCoord2f(u1, v1);            Gl.glVertex2i(x + width, y - height);            Gl.glTexCoord2f(u0, v1);            Gl.glVertex2i(x, y - height);        }        public void DrawRectRotatedUVFast(float x, float y, float width, float height, float angle)        {            y = (float)dheight - y;            float mx = x + width / (float)2.0;            float my = y - height / (float)2.0;            float x1 = x - mx;            float y1 = y - my;            float x2 = (x + width) - mx;            float y2 = y1;            float x3 = x2;            float y3 = (y - height) - my;            float x4 = x1;            float y4 = y3;            TFormCoord(x1, y1, angle);            x1 = rotx;            y1 = roty;            TFormCoord(x2, y2, angle);            x2 = rotx;            y2 = roty;            TFormCoord(x3, y3, angle);            x3 = rotx;            y3 = roty;            TFormCoord(x4, y4, angle);            x4 = rotx;            y4 = roty;                     Gl.glTexCoord2f(u0, v0);            Gl.glVertex2f(mx + x1, my + y1);            Gl.glTexCoord2f(u1, v0);            Gl.glVertex2f(mx + x2, my + y2);            Gl.glTexCoord2f(u0, v1);            Gl.glVertex2f(mx + x4, my + y4);            Gl.glTexCoord2f(u1, v0);            Gl.glVertex2f(mx + x2, my + y2);            Gl.glTexCoord2f(u1, v1);            Gl.glVertex2f(mx + x3, my + y3);            Gl.glTexCoord2f(u0, v1);            Gl.glVertex2f(mx + x4, my + y4);                }        public void DrawRectRotatedUV(float x, float y, float width, float height, float angle)        {            y = (float)dheight - y;            float mx = x + width / (float)2.0;            float my = y - height / (float)2.0;            float x1 = x - mx;            float y1 = y - my;            float x2 = (x + width) - mx;            float y2 = y1;            float x3 = x2;            float y3 = (y - height) - my;            float x4 = x1;            float y4 = y3;            TFormCoord(x1, y1, angle);            x1 = rotx;            y1 = roty;            TFormCoord(x2, y2, angle);            x2 = rotx;            y2 = roty;            TFormCoord(x3, y3, angle);            x3 = rotx;            y3 = roty;            TFormCoord(x4, y4, angle);            x4 = rotx;            y4 = roty;            Gl.glBegin(Gl.GL_TRIANGLES);            Gl.glTexCoord2f(u0, v0);            Gl.glVertex2f(mx + x1, my + y1);            Gl.glTexCoord2f(u1, v0);            Gl.glVertex2f(mx + x2, my + y2);            Gl.glTexCoord2f(u0, v1);            Gl.glVertex2f(mx + x4, my + y4);            Gl.glTexCoord2f(u1, v0);            Gl.glVertex2f(mx + x2, my + y2);            Gl.glTexCoord2f(u1, v1);            Gl.glVertex2f(mx + x3, my + y3);            Gl.glTexCoord2f(u0, v1);            Gl.glVertex2f(mx + x4, my + y4);            Gl.glEnd();        }        public void DrawRectRotated(float x, float y, float width, float height,float angle)        {            y = (float)dheight - y;            float mx = x + width / (float)2.0;            float my = y - height / (float)2.0;            float x1 = x - mx;            float y1 = y - my;            float x2 = (x + width) - mx;            float y2 = y1;            float x3 = x2;            float y3 = (y - height) - my;            float x4 = x1;            float y4 = y3;                        TFormCoord(x1, y1, angle);            x1 = rotx;            y1 = roty;            TFormCoord(x2, y2, angle);            x2 = rotx;            y2 = roty;            TFormCoord(x3, y3, angle);            x3 = rotx;            y3 = roty;            TFormCoord(x4, y4, angle);            x4 = rotx;            y4 = roty;                        Gl.glBegin(Gl.GL_TRIANGLES);             Gl.glVertex2f(mx + x1, my + y1);             Gl.glVertex2f(mx + x2, my + y2);             Gl.glVertex2f(mx + x4, my + y4);             Gl.glVertex2f(mx + x2, my + y2);             Gl.glVertex2f(mx + x3, my + y3);             Gl.glVertex2f(mx + x4, my + y4);            Gl.glEnd();        }        float rotx, roty;        public void TFormCoord(float x, float y, float angle)        {            //nx=(Cos (ang)*x - Sin (ang)*y)            //ny=(Sin (ang)*x + Cos (ang)*y)            double ang = (Double)angle * Math.PI/180;            rotx = (float)((float)Math.Cos(ang) * x - (float)Math.Sin(ang) * y);            roty = (float)((float)Math.Sin(ang) * x + (float)Math.Cos(ang) * y);        }        public float u0, v0, u1, v1;        public void SetUV(float nu0, float nv0, float nu1, float nv1)        {            u0 = nu0;            v0 = nv0;            u1 = nu1;            v1 = nv1;        }    }    public class Image    {        int width, height, depth;        int raw, gltex;        public Image()        {        }        ~Image()        {        }        public Image(string file)        {            Load(file);        }        /// <summary>        /// Loads the given image file from disk        /// into memory.        /// </summary>        /// <param name="file">Path of the file to load.</param>        /// <returns>True on success, false otherwise.</returns>        public bool Load(string file)        {            Il.ilGenImages(1, out raw);            Il.ilBindImage(raw);            if (!Il.ilLoadImage(file))            {                Console.WriteLine("Image failed to load.");                return false;            }            Console.WriteLine("Image loaded ok.");            width = Il.ilGetInteger(Il.IL_IMAGE_WIDTH);            height = Il.ilGetInteger(Il.IL_IMAGE_HEIGHT);            depth = Il.ilGetInteger(Il.IL_IMAGE_DEPTH);            Gl.glGenTextures(1, out gltex);            Gl.glEnable(Gl.GL_TEXTURE_2D);            Gl.glBindTexture(Gl.GL_TEXTURE_2D, gltex);            Gl.glTexParameteri(Gl.GL_TEXTURE_2D, Gl.GL_TEXTURE_MIN_FILTER, Gl.GL_LINEAR);            Gl.glTexParameteri(Gl.GL_TEXTURE_2D, Gl.GL_TEXTURE_MAG_FILTER, Gl.GL_LINEAR);            Tao.DevIl.Ilut2.ilutGLTexImage(0);            Console.Write("Width:");            Console.WriteLine(width);            Console.Write("Height:");            Console.WriteLine(height);            Console.Write("TextureID:");            Console.WriteLine(gltex);            Gl.glBindTexture(Gl.GL_TEXTURE_2D, 0);            Gl.glDisable(Gl.GL_TEXTURE_2D);            return true;        }        public void Draw(int x, int y)        {            Gl.glEnable(Gl.GL_TEXTURE_2D);            Gl.glBindTexture(Gl.GL_TEXTURE_2D, gltex);            Engine.main.SetUV(0, 0, 1, 1);            Engine.main.DrawRectUV(x, y, width, height);            Gl.glBindTexture(Gl.GL_TEXTURE_2D, 0);            Gl.glDisable(Gl.GL_TEXTURE_2D);        }        public void Draw(int x, int y, float angle)        {            Gl.glEnable(Gl.GL_TEXTURE_2D);            Gl.glBindTexture(Gl.GL_TEXTURE_2D, gltex);            Engine.main.SetUV(0, 0, 1, 1);            Engine.main.DrawRectRotatedUV((float)x, (float)y, (float)width, (float)height,angle);            Gl.glBindTexture(Gl.GL_TEXTURE_2D, 0);            Gl.glDisable(Gl.GL_TEXTURE_2D);        }    }    public enum Keys    {        A = Sdl.SDLK_a,        B = Sdl.SDLK_b,        C = Sdl.SDLK_c,        D = Sdl.SDLK_d,        E = Sdl.SDLK_e,        F = Sdl.SDLK_f    }        public class Input    {        public Input()        {                    }        ~Input()        {        }                public void Update()        {            int numkeys;            key = Sdl.SDL_GetKeyState(out numkeys);        }        public bool KeyDown(Keys check)        {            if (key[(int)check] == 1)            {                return true;            }            return false;        }                public byte[] key;    }}


And here's the example source code,

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);                       float ang = 0;            Image test = new Image("tst.jpg");            Input t = new Input();            while (true)            {                meng.ClearScreen();                meng.DrawRect(20, 20, 200, 200);                //ang += (float)0.5;                t.Update();                if (t.KeyDown(Keys.D) == true)                {                    ang += 1;                }                if (t.KeyDown(Keys.F) == true)                {                    ang -= 1;                }                meng.DrawRectRotated(300, 300, 80, 80, ang);                test.Draw(20, 200);                test.Draw(300, 200, ang);                meng.FlipScreen();                            }        }    }}
this has nothing to do with windows forms-- he's using sdl...

you must be handling sdl events someplace? AFAIK, the sdl event system requires you to pump the event queue each frame. It may be possible to turn it off, but I haven't seen how thats done.

You have to call SDL_PumpEvents, PeekEvents, or PollEvents depending on what you are doing, and how you want to go about it.

Look in the wiki under Events http://www.libsdl.org/cgi/docwiki.cgi/
-----------------------------www.devcrunch.com - independant developer forums
Just a quick note to say that I'm glad there's an active dev tester of the Tao libraries [wink]. Thanks a lot, Scope!
Rob Loach [Website] [Projects] [Contact]
You were right poed, it works fine now :)

Rob,(Bond voice) glad I can be of service. Speaking of tao, any likelyhood of a GLSL addition or is there already some way to access it?

My first commercial lib using tao is nearly done, I'll send you a free copy for your help if you like.
Quote:Original post by ScopeDynamo
Rob,(Bond voice) glad I can be of service. Speaking of tao, any likelyhood of a GLSL addition or is there already some way to access it?
I really don't know about the GLSL, would be interesting. You could bring it up on the mailing list. thoHere's something that might interest you though. Vlad just announced in the Tao Mailing List that he will be temporarily holding the official tao binaries on his site for a while before putting them on the official Tao site: Temporary/testing Tao download. I've checked it out and am pretty impressed!
Rob Loach [Website] [Projects] [Contact]
Scope here,
Will start using them , thanks.

As for glsl, any ideas where to begin? Does the tao dist you mention above solve the opengl extension problem? Surely if that's working we could just load the glsl extension?
I'll post a glsl source if I getting working, but I'd appreachiate(spell it right, or not at all - Some dude with a hat.) any advice you can give before I start.

This topic is closed to new replies.

Advertisement