Jump to content
  • Advertisement
Sign in to follow this  
ScopeDynamo

[.net] glfw produces a always black screen?

This topic is 4695 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I've switched from sdl to glfw because sdl frooze up whenever I touched the screen(I.e pressed the mouse over it) but glfw although working and opening a screen, results in a always black window. as if I'm not drawing to it at all. Below is the class lib code and some example code that although crashed eventually with sdl, at least displayed perfectly. The gl code etc was not touched when I switched to glfw, just the openscreen method, which returns before the sdl code. (I.e you can pad the glfw code out and switch back to sdl. you just have to change the flipscreen method to use sdl's swap buffers function instead of the glfw one called here.) Any ideas why this is happening? Here's the code to the 2d rendering lib.
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_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,
                      flags );
            dwidth = width;
            dheight = height;
            ddepth = depth;
            SetupGL();
            SetColor(255, 255, 255);
            Il.ilInit();
            Ilut2.ilutRenderer(Ilut2.ILUT_OPENGL);
            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()
        {
            Glfw.glfwSwapBuffers();
        }
        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 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);
        }
    }
}

And here's an example that uses it
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");

            while (true)
            {
                meng.ClearScreen();
                meng.DrawRect(20, 20, 200, 200);
                ang += (float)0.5;
                meng.DrawRectRotated(300, 300, 80, 80, ang);
               // test.Draw(20, 300);
                meng.FlipScreen();
             
            }
        }
    }
}

Share this post


Link to post
Share on other sites
Advertisement
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!