Exemplo n.º 1
0
        /**
         * Calculates the size that a string would occupy if it were renderered.
         *
         * @param text The text to size
         * @param size The point size of the font
         * @return A vector containing the width and height of the given text at the given point size
         */
        public static Vector2 calcTextSize(Handle handle, string text, int size)
        {
            int w, h;

            // Calculate the size (C# uses UNICODE)
            if (SdlTtf.TTF_SizeUNICODE(handle.getResource <Font>().getFontHandle(size), text, out w, out h) == -1)
            {
                throw new Exception("Text sizing failed: " + SdlTtf.TTF_GetError());
            }

            // Return the size
            return(new Vector2(w, h));
        }
Exemplo n.º 2
0
        public int LoadFont(string ttfFile, int size)
        {
            font = SdlTtf.TTF_OpenFont(ttfFile, size);

            if (IntPtr.Zero == font)
            {
                EngineLog.Get().Error("Error loading " + ttfFile + ": " + SdlTtf.TTF_GetError(), "Font");
                return(1);
            }

            EngineLog.Get().Info(ttfFile + " loaded", "Font");
            return(0);
        }
Exemplo n.º 3
0
        //////////////////////////////////////////////////////////////
        // RUDIMENTARY DRAW FUNCTIONS
        //////////////////////////////////////////////////////////////

        //Draws text to the screen
        public void drawText(string text, int x, int y, Handle font, Color color, int ptSize = 12)
        {
            // Get the font handle at the desired point size
            IntPtr handle;

            try
            {
                handle = font.getResource <Font>().getFontHandle(ptSize);
            }
            catch (Exception e)
            {
                Trace.WriteLine("GraphicsComponent.drawText(string,Font,Color,int): Could not draw font: " + e.Message);
                return;
            }

            // Render the text to a SDL surface
            IntPtr surf = SdlTtf.TTF_RenderUNICODE_Blended(handle, text, color.sdlColor);

            if (surf == IntPtr.Zero)
            {
                Trace.WriteLine("GraphicsComponent.drawText(string,Font,Color,int): Could not render font: " + SdlTtf.TTF_GetError());
            }

            // Draw the Texture2D
            Texture2D tempTex = new Texture2D(surf);

            drawTex(tempTex, x, y, Color.WHITE);
            Sdl.SDL_FreeSurface(surf);
        }
Exemplo n.º 4
0
        public int CreateDevice(int width, int height, int colorDepth, bool fullscreen)
        {
            this.width      = width;
            this.height     = height;
            this.colorDepth = colorDepth;
            this.fullscreen = fullscreen;
            videoFlags      = Sdl.SDL_OPENGL | Sdl.SDL_GL_DOUBLEBUFFER | Sdl.SDL_RESIZABLE | Sdl.SDL_HWPALETTE;

            if (Sdl.SDL_Init(Sdl.SDL_INIT_VIDEO) < 0)
            {
                EngineLog.Get().Error("Error init SDL: " + Sdl.SDL_GetError(), "Graphics");
                Sdl.SDL_Quit();
                return(-1);
            }

            if (-1 == SdlTtf.TTF_Init())
            {
                EngineLog.Get().Error("Error init SDL TTF: " + SdlTtf.TTF_GetError(), "Graphics");
                return(-1);
            }
            else
            {
                EngineLog.Get().Info("SDL TTF initialized", "Graphics");
            }

            if (this.fullscreen)
            {
                videoFlags |= Sdl.SDL_FULLSCREEN | Sdl.SDL_ANYFORMAT;
                EngineLog.Get().Info("Switching to Fullscreen", "Graphics");
            }
            else
            {
                EngineLog.Get().Info("Switching to Window", "Graphics");
            }

            IntPtr videoInfoPtr = Sdl.SDL_GetVideoInfo();

            if (IntPtr.Zero == videoInfoPtr)
            {
                EngineLog.Get().Error("GetVideoInfo failed", "Graphics");
                return(1);
            }

            Sdl.SDL_VideoInfo videoInfo = (Sdl.SDL_VideoInfo)Marshal.PtrToStructure(
                videoInfoPtr, typeof(Sdl.SDL_VideoInfo));

            // This doesn't work

            /*if(1 == videoInfo.hw_available)
             * {
             *      EngineLog.Get().Info("Hardware Surface", "Graphics");
             *      videoFlags |= Sdl.SDL_HWSURFACE;
             * } else   {
             *      EngineLog.Get().Warn("Software Surface", "Graphics");
             *      videoFlags |= Sdl.SDL_SWSURFACE;
             * }*/
            videoFlags |= Sdl.SDL_HWSURFACE;

            if (1 == videoInfo.blit_hw)
            {
                EngineLog.Get().Info("Hardware Blitting", "Graphics");
                videoFlags |= Sdl.SDL_HWACCEL;
            }
            else
            {
                EngineLog.Get().Warn("No Hardware Blitting", "Graphics");
            }

            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, 16);
            Sdl.SDL_GL_SetAttribute(Sdl.SDL_GL_DOUBLEBUFFER, 1);

            sdlSurface = Sdl.SDL_SetVideoMode(width, height, colorDepth, videoFlags);
            if (0 == sdlSurface.ToInt32())
            {
                EngineLog.Get().Error("Set video mode: " + Sdl.SDL_GetError(), "Graphics");
                return(1);
            }
            else
            {
                EngineLog.Get().Info("Set video mode to " + width + "x" + height + "x" + colorDepth, "Graphics");
            }

            Gl.glShadeModel(Gl.GL_SMOOTH);
            Gl.glClearColor(0.0f, 0.0f, 0.0f, 0.0f);

            Gl.glClearDepth(1.0f);
            Gl.glEnable(Gl.GL_DEPTH_TEST);
            Gl.glDepthFunc(Gl.GL_LEQUAL);

            Gl.glHint(Gl.GL_PERSPECTIVE_CORRECTION_HINT, Gl.GL_NICEST);
            Gl.glEnable(Gl.GL_TEXTURE_2D);

            Gl.glEnable(Gl.GL_CULL_FACE);
            Gl.glCullFace(Gl.GL_BACK);

            string glVendor    = Gl.glGetString(Gl.GL_VENDOR);
            string glVersion   = Gl.glGetString(Gl.GL_VERSION);
            string glVideoCard = Gl.glGetString(Gl.GL_RENDERER);
            string glExt       = Gl.glGetString(Gl.GL_EXTENSIONS);

            EngineLog.Get().Info("OpenGL Vendor: " + glVendor, "Graphics");
            EngineLog.Get().Info("OpenGL Version " + glVersion, "Graphics");
            EngineLog.Get().Info("OpenGL Renderer: " + glVideoCard, "Graphics");
            EngineLog.Get().Info("OpenGL Extensions: " + glExt, "Graphics");

            if (!Gl.IsExtensionSupported("GL_ARB_multitexture"))
            {
                EngineLog.Get().Error("Extension GL_ARB_multitexture not supported!", "Graphics");
                return(1);
            }

            // New TAO Framwork loads all extensions on startup

            /*if(false == GlExtensionLoader.LoadExtension("GL_ARB_multitexture"))
             * {
             *      EngineLog.Get().Error("Load OpenGL Extension: GL_ARB_multitexture", "Graphics");
             *      return 1;
             * }*/

            isRunning = true;
            isOrtho   = false;
            fpsTime   = Sdl.SDL_GetTicks();
            frameTime = Sdl.SDL_GetTicks();

            ResizeWindow(width, height);

            return(0);
        }