public Color(string color) { r = 255; g = 255; b = 255; a = 255; if ("" == color) { return; } try { string[] colors = color.Split(' '); if (colors.Length >= 3) { r = Convert.ToByte(colors[0]); g = Convert.ToByte(colors[1]); b = Convert.ToByte(colors[2]); if (colors.Length == 4) { this.a = Convert.ToByte(colors[3]); } } } catch { EngineLog.Get().Error("Error converting " + color + " to Color", "Color"); } }
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); }
public void BindTexture() { if (texId == 0) { //Gl.glGenTextures(1, new int[] { texId }); int[] textureAr = new int[1]; Gl.glGenTextures(1, textureAr); texId = textureAr[0]; } Gl.glBindTexture(Gl.GL_TEXTURE_2D, texId); Gl.glTexParameteri(Gl.GL_TEXTURE_2D, Gl.GL_TEXTURE_MIN_FILTER, Gl.GL_LINEAR_MIPMAP_LINEAR); Gl.glTexParameteri(Gl.GL_TEXTURE_2D, Gl.GL_TEXTURE_MAG_FILTER, Gl.GL_LINEAR); int err = Glu.gluBuild2DMipmaps(Gl.GL_TEXTURE_2D, Gl.GL_RGB, 128, 128, Gl.GL_RGB, Gl.GL_UNSIGNED_BYTE, map); if (err != 0) { EngineLog.Get().Error("Error binding texture (" + texId + "): " + Glu.gluErrorString(err), ToString()); } }
public int LoadModel(string file) { if (modelList.ContainsKey(file)) { return(0); } BinaryReader binReader; binReader = new BinaryReader(File.Open(file, FileMode.Open)); // ToDo: Error handling Model model = new Model(); model.file = file; ModelMaterial modelMaterial = new ModelMaterial(); ModelObject modelObject = new ModelObject(); long fileLenght = binReader.BaseStream.Length; while (binReader.BaseStream.Position < fileLenght) { ushort chunkId = binReader.ReadUInt16(); uint chunkLen = binReader.ReadUInt32(); switch (chunkId) { case 0x4d4d: // Main break; case 0x3d3d: // Editor break; case 0x4000: // Object model.objectList.Add(modelObject); modelObject = new ModelObject(); modelObject.vertexList.Clear(); modelObject.faceList.Clear(); modelObject.normalList.Clear(); modelObject.texcoordList.Clear(); modelObject.name = ReadNames(ref binReader, 20); break; case 0x4100: break; case 0x4110: // Vertices ushort lenv = binReader.ReadUInt16(); for (int i = 0; i < lenv; i++) { Vector3 vec = new Vector3(); vec.x = binReader.ReadSingle(); vec.y = binReader.ReadSingle(); vec.z = binReader.ReadSingle(); modelObject.vertexList.Add(vec); } break; case 0x4120: // Faces ushort lenf = binReader.ReadUInt16(); for (int i = 0; i < lenf; i++) { ModelFace modelFace = new ModelFace(); modelFace.vertexIndex[0] = binReader.ReadUInt16(); modelFace.vertexIndex[1] = binReader.ReadUInt16(); modelFace.vertexIndex[2] = binReader.ReadUInt16(); binReader.ReadUInt16(); modelObject.faceList.Add(modelFace); } break; case 0x4130: // Material information modelObject.materialName = ReadNames(ref binReader, 20); binReader.BaseStream.Seek(chunkLen - 6 - modelObject.materialName.Length, SeekOrigin.Current); break; case 0x4140: // Texture coordinates ushort lent = binReader.ReadUInt16(); for (int i = 0; i < lent; i++) { ModelTextureCoord modelTc = new ModelTextureCoord(); modelTc.u = binReader.ReadSingle(); modelTc.v = binReader.ReadSingle(); modelObject.texcoordList.Add(modelTc); } break; case 0xAFFF: // Material model.materialList.Add(modelMaterial); modelMaterial = new ModelMaterial(); break; case 0xA000: modelMaterial.name = ReadNames(ref binReader, 20); break; case 0xA200: break; case 0xA300: modelMaterial.textureName = ReadNames(ref binReader, 20); break; default: binReader.BaseStream.Seek(chunkLen - 6, SeekOrigin.Current); break; } } binReader.Close(); model.objectList.Add(modelObject); model.materialList.Add(modelMaterial); model.objectList.RemoveAt(0); model.materialList.RemoveAt(0); foreach (ModelMaterial modelMat in model.materialList) { int pos = file.LastIndexOf("/", file.Length); string tmpStr = file.Substring(0, pos + 1) + modelMat.textureName; modelMat.textureName = tmpStr; LightFireCS.Graphics.TextureManager.Get().LoadTextureFromFile(modelMat.textureName); } Console.WriteLine("Objects: " + model.objectList.Count); Console.WriteLine("Materials: " + model.materialList.Count); int faces = 0; foreach (ModelObject mdlObj in model.objectList) { faces += mdlObj.faceList.Count; } Console.WriteLine("Faces: " + faces); modelList.Add(file, model); EngineLog.Get().Info(file + " loaded", "Model manager"); return(0); }
private int LoadTextureFromFile(string file, int oldId) { if (textureList.ContainsKey(file) && oldId == -1) { return(0); } int imageId = 0; Il.ilGenImages(1, out imageId); Il.ilBindImage(imageId); if (!Il.ilLoadImage(file)) { Il.ilDeleteImages(1, ref imageId); EngineLog.Get().Error("Error loading " + file + " (DevIL: " + Il.ilGetError() + ")", "Texture manager"); return(-1); } if (file.ToUpper().LastIndexOf(".TGA") == 0) { Ilu.iluFlipImage(); } Texture tex = new Texture(); tex.width = Il.ilGetInteger(Il.IL_IMAGE_WIDTH); tex.height = Il.ilGetInteger(Il.IL_IMAGE_HEIGHT); tex.bpp = Il.ilGetInteger(Il.IL_IMAGE_BITS_PER_PIXEL); IntPtr Ptr = Il.ilGetData(); Gl.glPushAttrib(Gl.GL_TEXTURE_BIT); if (oldId == -1) { //Gl.glGenTextures(1, new int[] { tex.id }); int[] texture = new int[1]; Gl.glGenTextures(1, texture); tex.id = texture[0]; } else { tex.id = oldId; } Gl.glBindTexture(Gl.GL_TEXTURE_2D, tex.id); Gl.glTexParameteri(Gl.GL_TEXTURE_2D, Gl.GL_TEXTURE_MIN_FILTER, Gl.GL_LINEAR_MIPMAP_NEAREST); Gl.glTexParameteri(Gl.GL_TEXTURE_2D, Gl.GL_TEXTURE_MAG_FILTER, Gl.GL_LINEAR); int texType = Gl.GL_RGB; if (file.ToUpper().LastIndexOf(".TGA") != -1) { texType = Gl.GL_BGR; } if (tex.bpp == 32) { texType = Gl.GL_RGBA; } if (file.ToUpper().LastIndexOf(".TGA") != -1 && tex.bpp == 32) { texType = Gl.GL_BGRA; } int texDestType = Gl.GL_RGB; if (tex.bpp == 32) { texDestType = Gl.GL_RGBA; } Glu.gluBuild2DMipmaps(Gl.GL_TEXTURE_2D, texDestType, tex.width, tex.height, texType, Gl.GL_UNSIGNED_BYTE, Ptr); Gl.glPopAttrib(); if (oldId == -1) { textureList.Add(file, tex); } EngineLog.Get().Info(file + " loaded", "Texture manager"); return(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); }