/// <summary> /// The main entry point for the application. /// </summary> static void Main() { // Create main window RenderWindow App = new RenderWindow(new VideoMode(800, 600), "SFML.Net OpenGL"); App.PreserveOpenGLStates(true); // Setup event handlers App.Closed += new EventHandler(OnClosed); App.KeyPressed += new EventHandler<KeyEventArgs>(OnKeyPressed); App.Resized += new EventHandler<SizeEventArgs>(OnResized); // Create a sprite for the background Image BackgroundImage = new Image("datas/opengl/background.jpg"); Sprite Background = new Sprite(BackgroundImage); // Create a text to display String2D Text = new String2D("This is a rotating cube"); Text.Position = new Vector2(250.0F, 300.0F); Text.Color = new Color(128, 0, 128); // Load an OpenGL texture. // We could directly use a sf::Image as an OpenGL texture (with its Bind() member function), // but here we want more control on it (generate mipmaps, ...) so we create a new one int Texture = 0; using (Image TempImage = new Image("datas/opengl/texture.jpg")) { Gl.glGenTextures(1, out Texture); Gl.glBindTexture(Gl.GL_TEXTURE_2D, Texture); Glu.gluBuild2DMipmaps(Gl.GL_TEXTURE_2D, Gl.GL_RGBA, (int)TempImage.Width, (int)TempImage.Height, Gl.GL_RGBA, Gl.GL_UNSIGNED_BYTE, TempImage.Pixels); Gl.glTexParameteri(Gl.GL_TEXTURE_2D, Gl.GL_TEXTURE_MAG_FILTER, Gl.GL_LINEAR); Gl.glTexParameteri(Gl.GL_TEXTURE_2D, Gl.GL_TEXTURE_MIN_FILTER, Gl.GL_LINEAR_MIPMAP_LINEAR); } // Enable Z-buffer read and write Gl.glEnable(Gl.GL_DEPTH_TEST); Gl.glDepthMask(Gl.GL_TRUE); Gl.glClearDepth(1.0F); // Setup a perspective projection Gl.glMatrixMode(Gl.GL_PROJECTION); Gl.glLoadIdentity(); Glu.gluPerspective(90.0F, 1.0F, 1.0F, 500.0F); // Bind our texture Gl.glEnable(Gl.GL_TEXTURE_2D); Gl.glBindTexture(Gl.GL_TEXTURE_2D, Texture); Gl.glColor4f(1.0F, 1.0F, 1.0F, 1.0F); float Time = 0.0F; // Start game loop while (App.IsOpened()) { // Process events App.DispatchEvents(); // Clear the window App.Clear(); // Draw background App.Draw(Background); // Clear depth buffer Gl.glClear(Gl.GL_DEPTH_BUFFER_BIT); // Apply some transformations Time += App.GetFrameTime(); Gl.glMatrixMode(Gl.GL_MODELVIEW); Gl.glLoadIdentity(); Gl.glTranslatef(0.0F, 0.0F, -200.0F); Gl.glRotatef(Time * 50, 1.0F, 0.0F, 0.0F); Gl.glRotatef(Time * 30, 0.0F, 1.0F, 0.0F); Gl.glRotatef(Time * 90, 0.0F, 0.0F, 1.0F); // Draw a cube Gl.glBegin(Gl.GL_QUADS); Gl.glTexCoord2f(0, 0); Gl.glVertex3f(-50.0F, -50.0F, -50.0F); Gl.glTexCoord2f(0, 1); Gl.glVertex3f(-50.0F, 50.0F, -50.0F); Gl.glTexCoord2f(1, 1); Gl.glVertex3f( 50.0F, 50.0F, -50.0F); Gl.glTexCoord2f(1, 0); Gl.glVertex3f( 50.0F, -50.0F, -50.0F); Gl.glTexCoord2f(0, 0); Gl.glVertex3f(-50.0F, -50.0F, 50.0F); Gl.glTexCoord2f(0, 1); Gl.glVertex3f(-50.0F, 50.0F, 50.0F); Gl.glTexCoord2f(1, 1); Gl.glVertex3f( 50.0F, 50.0F, 50.0F); Gl.glTexCoord2f(1, 0); Gl.glVertex3f( 50.0F, -50.0F, 50.0F); Gl.glTexCoord2f(0, 0); Gl.glVertex3f(-50.0F, -50.0F, -50.0F); Gl.glTexCoord2f(0, 1); Gl.glVertex3f(-50.0F, 50.0F, -50.0F); Gl.glTexCoord2f(1, 1); Gl.glVertex3f(-50.0F, 50.0F, 50.0F); Gl.glTexCoord2f(1, 0); Gl.glVertex3f(-50.0F, -50.0F, 50.0F); Gl.glTexCoord2f(0, 0); Gl.glVertex3f(50.0F, -50.0F, -50.0F); Gl.glTexCoord2f(0, 1); Gl.glVertex3f(50.0F, 50.0F, -50.0F); Gl.glTexCoord2f(1, 1); Gl.glVertex3f(50.0F, 50.0F, 50.0F); Gl.glTexCoord2f(1, 0); Gl.glVertex3f(50.0F, -50.0F, 50.0F); Gl.glTexCoord2f(0, 1); Gl.glVertex3f(-50.0F, -50.0F, 50.0F); Gl.glTexCoord2f(0, 0); Gl.glVertex3f(-50.0F, -50.0F, -50.0F); Gl.glTexCoord2f(1, 0); Gl.glVertex3f( 50.0F, -50.0F, -50.0F); Gl.glTexCoord2f(1, 1); Gl.glVertex3f( 50.0F, -50.0F, 50.0F); Gl.glTexCoord2f(0, 1); Gl.glVertex3f(-50.0F, 50.0F, 50.0F); Gl.glTexCoord2f(0, 0); Gl.glVertex3f(-50.0F, 50.0F, -50.0F); Gl.glTexCoord2f(1, 0); Gl.glVertex3f( 50.0F, 50.0F, -50.0F); Gl.glTexCoord2f(1, 1); Gl.glVertex3f( 50.0F, 50.0F, 50.0F); Gl.glEnd(); // Draw some text on top of our OpenGL object App.Draw(Text); // Finally, display the rendered frame on screen App.Display(); } // Don't forget to destroy our texture Gl.glDeleteTextures(1, ref Texture); }
private static void Main(string[] args) { var form = new SettingsDialog(); Application.EnableVisualStyles(); Application.Run(form); videoMode = VideoMode.DesktopMode; RenderWindow window = new RenderWindow(videoMode, "Maze Crap", Styles.Fullscreen); window.EnableVerticalSync(Settings.Default.VerticalSync); window.ShowMouseCursor(false); window.Closed += (sender, e) => Application.Exit(); window.KeyPressed += (sender, e) => ((RenderWindow) sender).Close(); SetUpMaze(); target = new Image(VideoMode.DesktopMode.Width/10, VideoMode.DesktopMode.Height/10) {Smooth = false}; float accumulator = 0; float WaitTime = 0; var fps = (float) Settings.Default.Framerate; window.Show(true); bool hasrun = false; while (window.IsOpened()) { accumulator += window.GetFrameTime(); while (accumulator > fps) { if (FinishedGenerating && !Solving) { if (WaitTime < Settings.Default.WaitTime && hasrun) { WaitTime += window.GetFrameTime(); } else { WaitTime = 0; CurrentCell = StartCell*2; VisitedWalls[CurrentCell.X, CurrentCell.Y] = true; CellStack.Clear(); Solving = true; hasrun = true; } accumulator -= fps; continue; } if (Solving && FinishedSolving) { if (WaitTime < Settings.Default.WaitTime) WaitTime += window.GetFrameTime(); else { Solving = false; FinishedGenerating = false; FinishedSolving = false; SetUpMaze(); continue; } continue; } if (Solving && !FinishedSolving) { if (Settings.Default.ShowSolving) FinishedSolving = !SolveIterate(CellStack, Cells, Walls, StartCell, EndCell); else { while (SolveIterate(CellStack, Cells, Walls, StartCell, EndCell)) ; FinishedSolving = true; } accumulator -= fps; continue; } if (Settings.Default.ShowGeneration) { FinishedGenerating = !GenerateIterate(CellStack, Cells, Walls); hasrun = true; } else { while (GenerateIterate(CellStack, Cells, Walls)) ; FinishedGenerating = true; } accumulator -= fps; NeedsRedraw = true; } Render(window); window.Display(); window.DispatchEvents(); } }
/// <summary> /// The main entry point for the application. /// </summary> static void Main() { // Create the main window RenderWindow window = new RenderWindow(new VideoMode(800, 600), "SFML.Net Shader"); // Setup event handlers window.Closed += new EventHandler(OnClosed); window.KeyPressed += new EventHandler<KeyEventArgs>(OnKeyPressed); // Check that the system can use shaders if (Shader.IsAvailable == false) { DisplayError(window); return; } // Create the render image RenderImage image = new RenderImage(window.Width, window.Height); // Load a background image to display Sprite background = new Sprite(new Image("resources/background.jpg")); background.Image.Smooth = false; // Load a sprite which we'll move into the scene Sprite entity = new Sprite(new Image("resources/sprite.png")); // Load the text font Font font = new Font("resources/arial.ttf"); // Load the image needed for the wave effect Image waveImage = new Image("resources/wave.jpg"); // Load all effects shaders = new Dictionary<string, Shader>(); shaders["nothing"] = new Shader("resources/nothing.sfx"); shaders["blur"] = new Shader("resources/blur.sfx"); shaders["colorize"] = new Shader("resources/colorize.sfx"); shaders["fisheye"] = new Shader("resources/fisheye.sfx"); shaders["wave"] = new Shader("resources/wave.sfx"); shaders["pixelate"] = new Shader("resources/pixelate.sfx"); backgroundShader = new ShaderSelector(shaders); entityShader = new ShaderSelector(shaders); globalShader = new ShaderSelector(shaders); // Do specific initializations shaders["nothing"].SetTexture("texture", Shader.CurrentTexture); shaders["blur"].SetTexture("texture", Shader.CurrentTexture); shaders["blur"].SetParameter("offset", 0.0F); shaders["colorize"].SetTexture("texture", Shader.CurrentTexture); shaders["colorize"].SetParameter("color", 1.0F, 1.0F, 1.0F); shaders["fisheye"].SetTexture("texture", Shader.CurrentTexture); shaders["wave"].SetTexture("texture", Shader.CurrentTexture); shaders["wave"].SetTexture("wave", waveImage); shaders["pixelate"].SetTexture("texture", Shader.CurrentTexture); // Define a string for displaying current effect description shaderText = new Text(); shaderText.Font = font; shaderText.Size = 20; shaderText.Position = new Vector2(5.0F, 0.0F); shaderText.Color = new Color(250, 100, 30); shaderText.DisplayedString = "Background shader: \"" + backgroundShader.Name + "\"\n" + "Flower shader: \"" + entityShader.Name + "\"\n" + "Global shader: \"" + globalShader.Name + "\"\n"; // Define a string for displaying help Text infoText = new Text(); infoText.Font = font; infoText.Size = 20; infoText.Position = new Vector2(5.0F, 500.0F); infoText.Color = new Color(250, 100, 30); infoText.DisplayedString = "Move your mouse to change the shaders' parameters\n" + "Press numpad 1 to change the background shader\n" + "Press numpad 2 to change the flower shader\n" + "Press numpad 3 to change the global shader"; // Start the game loop float time = 0.0F; while (window.IsOpened()) { // Process events window.DispatchEvents(); // TOFIX -- using window.Input together with image.Draw apparently causes a memory corruption // Get the mouse position in the range [0, 1] //float x = window.Input.GetMouseX() / (float)window.Width; //float y = window.Input.GetMouseY() / (float)window.Height; float x = (float)(Math.Cos(time * 1.3) + 1) * 0.5F; float y = (float)(Math.Sin(time * 0.8) + 1) * 0.5F; // Update the shaders backgroundShader.Update(x, y); entityShader.Update(x, y); globalShader.Update(x, y); // Animate the sprite time += window.GetFrameTime(); float entityX = (float)(Math.Cos(time * 1.3) + 1.2) * 300; float entityY = (float)(Math.Cos(time * 0.8) + 1.2) * 200; entity.Position = new Vector2(entityX, entityY); entity.Rotation = time * 100; // Draw the background and the moving entity to the render image image.Draw(background, backgroundShader.Shader); image.Draw(entity, entityShader.Shader); image.Display(); // Draw the contents of the render image to the window window.Draw(new Sprite(image.Image), globalShader.Shader); // Draw interface texts window.Draw(shaderText); window.Draw(infoText); // Finally, display the rendered frame on screen window.Display(); } }
/// <summary> /// The main entry point for the application. /// </summary> static void Main() { // Create main window RenderWindow window = new RenderWindow(new VideoMode(800, 600), "SFML.Net OpenGL", Styles.Default, new ContextSettings(32, 0)); // Setup event handlers window.Closed += new EventHandler(OnClosed); window.KeyPressed += new EventHandler<KeyEventArgs>(OnKeyPressed); window.Resized += new EventHandler<SizeEventArgs>(OnResized); // Create a sprite for the background Image backgroundImage = new Image("resources/background.jpg"); Sprite background = new Sprite(backgroundImage); // Create a text to display Text text = new Text("SFML / OpenGL demo"); text.Position = new Vector2(250.0F, 450.0F); text.Color = new Color(255, 255, 255, 170); // Load an OpenGL texture. // We could directly use a sf::Image as an OpenGL texture (with its Bind() member function), // but here we want more control on it (generate mipmaps, ...) so we create a new one int texture = 0; using (Image image = new Image("resources/texture.jpg")) { Gl.glGenTextures(1, out texture); Gl.glBindTexture(Gl.GL_TEXTURE_2D, texture); Glu.gluBuild2DMipmaps(Gl.GL_TEXTURE_2D, Gl.GL_RGBA, (int)image.Width, (int)image.Height, Gl.GL_RGBA, Gl.GL_UNSIGNED_BYTE, image.Pixels); Gl.glTexParameteri(Gl.GL_TEXTURE_2D, Gl.GL_TEXTURE_MAG_FILTER, Gl.GL_LINEAR); Gl.glTexParameteri(Gl.GL_TEXTURE_2D, Gl.GL_TEXTURE_MIN_FILTER, Gl.GL_LINEAR_MIPMAP_LINEAR); } // Enable Z-buffer read and write Gl.glEnable(Gl.GL_DEPTH_TEST); Gl.glDepthMask(Gl.GL_TRUE); Gl.glClearDepth(1.0F); // Setup a perspective projection Gl.glMatrixMode(Gl.GL_PROJECTION); Gl.glLoadIdentity(); Glu.gluPerspective(90.0F, 1.0F, 1.0F, 500.0F); // Bind our texture Gl.glEnable(Gl.GL_TEXTURE_2D); Gl.glBindTexture(Gl.GL_TEXTURE_2D, texture); Gl.glColor4f(1.0F, 1.0F, 1.0F, 1.0F); float time = 0.0F; // Start game loop while (window.IsOpened()) { // Process events window.DispatchEvents(); // Clear the window window.Clear(); // Draw background window.SaveGLStates(); window.Draw(background); window.RestoreGLStates(); // Activate the window before using OpenGL commands. // This is useless here because we have only one window which is // always the active one, but don't forget it if you use multiple windows window.SetActive(); // Clear depth buffer Gl.glClear(Gl.GL_DEPTH_BUFFER_BIT); // We get the position of the mouse cursor, so that we can move the box accordingly float x = window.Input.GetMouseX() * 200.0F / window.Width - 100.0F; float y = -window.Input.GetMouseY() * 200.0F / window.Height + 100.0F; // Apply some transformations time += window.GetFrameTime(); Gl.glMatrixMode(Gl.GL_MODELVIEW); Gl.glLoadIdentity(); Gl.glTranslatef(x, y, -100.0F); Gl.glRotatef(time * 50, 1.0F, 0.0F, 0.0F); Gl.glRotatef(time * 30, 0.0F, 1.0F, 0.0F); Gl.glRotatef(time * 90, 0.0F, 0.0F, 1.0F); // Draw a cube float size = 20.0F; Gl.glBegin(Gl.GL_QUADS); Gl.glTexCoord2f(0, 0); Gl.glVertex3f(-size, -size, -size); Gl.glTexCoord2f(0, 1); Gl.glVertex3f(-size, size, -size); Gl.glTexCoord2f(1, 1); Gl.glVertex3f( size, size, -size); Gl.glTexCoord2f(1, 0); Gl.glVertex3f( size, -size, -size); Gl.glTexCoord2f(0, 0); Gl.glVertex3f(-size, -size, size); Gl.glTexCoord2f(0, 1); Gl.glVertex3f(-size, size, size); Gl.glTexCoord2f(1, 1); Gl.glVertex3f( size, size, size); Gl.glTexCoord2f(1, 0); Gl.glVertex3f( size, -size, size); Gl.glTexCoord2f(0, 0); Gl.glVertex3f(-size, -size, -size); Gl.glTexCoord2f(0, 1); Gl.glVertex3f(-size, size, -size); Gl.glTexCoord2f(1, 1); Gl.glVertex3f(-size, size, size); Gl.glTexCoord2f(1, 0); Gl.glVertex3f(-size, -size, size); Gl.glTexCoord2f(0, 0); Gl.glVertex3f(size, -size, -size); Gl.glTexCoord2f(0, 1); Gl.glVertex3f(size, size, -size); Gl.glTexCoord2f(1, 1); Gl.glVertex3f(size, size, size); Gl.glTexCoord2f(1, 0); Gl.glVertex3f(size, -size, size); Gl.glTexCoord2f(0, 1); Gl.glVertex3f(-size, -size, size); Gl.glTexCoord2f(0, 0); Gl.glVertex3f(-size, -size, -size); Gl.glTexCoord2f(1, 0); Gl.glVertex3f( size, -size, -size); Gl.glTexCoord2f(1, 1); Gl.glVertex3f( size, -size, size); Gl.glTexCoord2f(0, 1); Gl.glVertex3f(-size, size, size); Gl.glTexCoord2f(0, 0); Gl.glVertex3f(-size, size, -size); Gl.glTexCoord2f(1, 0); Gl.glVertex3f( size, size, -size); Gl.glTexCoord2f(1, 1); Gl.glVertex3f( size, size, size); Gl.glEnd(); // Draw some text on top of our OpenGL object window.SaveGLStates(); window.Draw(text); window.RestoreGLStates(); // Finally, display the rendered frame on screen window.Display(); } // Don't forget to destroy our texture Gl.glDeleteTextures(1, ref texture); }