summaryrefslogtreecommitdiff
path: root/examples
diff options
context:
space:
mode:
authorJames Cowgill <jcowgill@debian.org>2016-08-09 15:20:03 +0000
committerJames Cowgill <jcowgill@debian.org>2016-08-09 15:20:03 +0000
commitdf93e238e30e97850d76ad5585b8ab9ad9c03e67 (patch)
tree2ae5f3305e1ee1882f563d2803f94aa6446dc367 /examples
parent301fd78b3ac87cf1fbce9d2c955db89094a304a5 (diff)
Imported Upstream version 2.4.0+dfsg
Diffstat (limited to 'examples')
-rw-r--r--examples/android/AndroidManifest.xml2
-rw-r--r--examples/android/jni/Application.mk2
-rw-r--r--examples/android/jni/main.cpp102
-rw-r--r--examples/cocoa/CocoaAppDelegate.h2
-rw-r--r--examples/cocoa/CocoaAppDelegate.mm4
-rw-r--r--examples/cocoa/NSString+stdstring.h2
-rw-r--r--examples/cocoa/NSString+stdstring.mm2
-rw-r--r--examples/cocoa/main.m2
-rw-r--r--examples/cocoa/resources/Cocoa-Info.plist2
-rw-r--r--examples/opengl/OpenGL.cpp374
-rw-r--r--examples/pong/Pong.cpp2
-rw-r--r--examples/shader/Shader.cpp106
-rw-r--r--examples/shader/resources/billboard.frag11
-rw-r--r--examples/shader/resources/billboard.geom56
-rw-r--r--examples/shader/resources/billboard.vert5
-rw-r--r--examples/shader/resources/logo.pngbin0 -> 8849 bytes
-rw-r--r--examples/sound/Sound.cpp5
-rw-r--r--examples/sound_capture/SoundCapture.cpp3
-rw-r--r--examples/voip/Client.cpp20
19 files changed, 489 insertions, 213 deletions
diff --git a/examples/android/AndroidManifest.xml b/examples/android/AndroidManifest.xml
index beb3538..edee84f 100644
--- a/examples/android/AndroidManifest.xml
+++ b/examples/android/AndroidManifest.xml
@@ -10,6 +10,8 @@
<uses-sdk android:minSdkVersion="9"
android:targetSdkVersion="19" />
+ <uses-permission android:name="android.permission.VIBRATE" />
+
<application android:label="@string/app_name"
android:icon="@drawable/sfml_logo"
android:hasCode="false"
diff --git a/examples/android/jni/Application.mk b/examples/android/jni/Application.mk
index a18107b..71828c8 100644
--- a/examples/android/jni/Application.mk
+++ b/examples/android/jni/Application.mk
@@ -1,4 +1,4 @@
-NDK_TOOLCHAIN_VERSION := 4.8
+NDK_TOOLCHAIN_VERSION := 4.9
APP_PLATFORM := android-9
APP_STL := c++_shared
APP_ABI := armeabi-v7a
diff --git a/examples/android/jni/main.cpp b/examples/android/jni/main.cpp
index a8e75de..56b6741 100644
--- a/examples/android/jni/main.cpp
+++ b/examples/android/jni/main.cpp
@@ -4,7 +4,76 @@
#include <SFML/Audio.hpp>
#include <SFML/Network.hpp>
+// Do we want to showcase direct JNI/NDK interaction?
+// Undefine this to get real cross-platform code.
+#define USE_JNI
+#if defined(USE_JNI)
+// These headers are only needed for direct NDK/JDK interaction
+#include <jni.h>
+#include <android/native_activity.h>
+
+// Since we want to get the native activity from SFML, we'll have to use an
+// extra header here:
+#include <SFML/System/NativeActivity.hpp>
+
+// NDK/JNI sub example - call Java code from native code
+int vibrate(sf::Time duration)
+{
+ // First we'll need the native activity handle
+ ANativeActivity *activity = sf::getNativeActivity();
+
+ // Retrieve the JVM and JNI environment
+ JavaVM* vm = activity->vm;
+ JNIEnv* env = activity->env;
+
+ // First, attach this thread to the main thread
+ JavaVMAttachArgs attachargs;
+ attachargs.version = JNI_VERSION_1_6;
+ attachargs.name = "NativeThread";
+ attachargs.group = NULL;
+ jint res = vm->AttachCurrentThread(&env, &attachargs);
+
+ if (res == JNI_ERR)
+ return EXIT_FAILURE;
+
+ // Retrieve class information
+ jclass natact = env->FindClass("android/app/NativeActivity");
+ jclass context = env->FindClass("android/content/Context");
+
+ // Get the value of a constant
+ jfieldID fid = env->GetStaticFieldID(context, "VIBRATOR_SERVICE", "Ljava/lang/String;");
+ jobject svcstr = env->GetStaticObjectField(context, fid);
+
+ // Get the method 'getSystemService' and call it
+ jmethodID getss = env->GetMethodID(natact, "getSystemService", "(Ljava/lang/String;)Ljava/lang/Object;");
+ jobject vib_obj = env->CallObjectMethod(activity->clazz, getss, svcstr);
+
+ // Get the object's class and retrieve the member name
+ jclass vib_cls = env->GetObjectClass(vib_obj);
+ jmethodID vibrate = env->GetMethodID(vib_cls, "vibrate", "(J)V");
+
+ // Determine the timeframe
+ jlong length = duration.asMilliseconds();
+
+ // Bzzz!
+ env->CallVoidMethod(vib_obj, vibrate, length);
+
+ // Free references
+ env->DeleteLocalRef(vib_obj);
+ env->DeleteLocalRef(vib_cls);
+ env->DeleteLocalRef(svcstr);
+ env->DeleteLocalRef(context);
+ env->DeleteLocalRef(natact);
+
+ // Detach thread again
+ vm->DetachCurrentThread();
+}
+#endif
+
+// This is the actual Android example. You don't have to write any platform
+// specific code, unless you want to use things not directly exposed.
+// ('vibrate()' in this example; undefine 'USE_JNI' above to disable it)
int main(int argc, char *argv[])
{
sf::RenderWindow window(sf::VideoMode::getDesktopMode(), "");
@@ -31,25 +100,28 @@ int main(int argc, char *argv[])
while (window.pollEvent(event))
{
- if (event.type == sf::Event::Closed)
- {
- window.close();
- }
-
- if (event.type == sf::Event::Resized)
+ switch (event.type)
{
- view.setSize(event.size.width, event.size.height);
- view.setCenter(event.size.width/2, event.size.height/2);
- window.setView(view);
+ case sf::Event::Closed:
+ window.close();
+ break;
+ case sf::Event::Resized:
+ view.setSize(event.size.width, event.size.height);
+ view.setCenter(event.size.width/2, event.size.height/2);
+ window.setView(view);
+ break;
+ case sf::Event::TouchBegan:
+ if (event.touch.finger == 0)
+ {
+ image.setPosition(event.touch.x, event.touch.y);
+#if defined(USE_JNI)
+ vibrate(sf::milliseconds(10));
+#endif
+ }
+ break;
}
}
- if (sf::Touch::isDown(0))
- {
- sf::Vector2i position = sf::Touch::getPosition(0);
- image.setPosition(position.x, position.y);
- }
-
window.clear(sf::Color::White);
window.draw(image);
window.display();
diff --git a/examples/cocoa/CocoaAppDelegate.h b/examples/cocoa/CocoaAppDelegate.h
index b3fe9ca..23f6a4a 100644
--- a/examples/cocoa/CocoaAppDelegate.h
+++ b/examples/cocoa/CocoaAppDelegate.h
@@ -1,7 +1,7 @@
////////////////////////////////////////////////////////////
//
// SFML - Simple and Fast Multimedia Library
-// Copyright (C) 2007-2015 Marco Antognini (antognini.marco@gmail.com),
+// Copyright (C) 2007-2016 Marco Antognini (antognini.marco@gmail.com),
// Laurent Gomila (laurent@sfml-dev.org)
//
// This software is provided 'as-is', without any express or implied warranty.
diff --git a/examples/cocoa/CocoaAppDelegate.mm b/examples/cocoa/CocoaAppDelegate.mm
index 3afadf6..3d6bc60 100644
--- a/examples/cocoa/CocoaAppDelegate.mm
+++ b/examples/cocoa/CocoaAppDelegate.mm
@@ -1,7 +1,7 @@
////////////////////////////////////////////////////////////
//
// SFML - Simple and Fast Multimedia Library
-// Copyright (C) 2007-2015 Marco Antognini (antognini.marco@gmail.com),
+// Copyright (C) 2007-2016 Marco Antognini (antognini.marco@gmail.com),
// Laurent Gomila (laurent@sfml-dev.org)
//
// This software is provided 'as-is', without any express or implied warranty.
@@ -57,7 +57,7 @@ struct SFMLmainWindow
if (!font.loadFromFile(resPath + "/sansation.ttf"))
NSLog(@"Couldn't load the font");
- text.setColor(sf::Color::White);
+ text.setFillColor(sf::Color::White);
text.setFont(font);
}
diff --git a/examples/cocoa/NSString+stdstring.h b/examples/cocoa/NSString+stdstring.h
index 0e4195a..eeee189 100644
--- a/examples/cocoa/NSString+stdstring.h
+++ b/examples/cocoa/NSString+stdstring.h
@@ -1,7 +1,7 @@
////////////////////////////////////////////////////////////
//
// SFML - Simple and Fast Multimedia Library
-// Copyright (C) 2007-2015 Marco Antognini (antognini.marco@gmail.com),
+// Copyright (C) 2007-2016 Marco Antognini (antognini.marco@gmail.com),
// Laurent Gomila (laurent@sfml-dev.org)
//
// This software is provided 'as-is', without any express or implied warranty.
diff --git a/examples/cocoa/NSString+stdstring.mm b/examples/cocoa/NSString+stdstring.mm
index 5581f5e..fd1a743 100644
--- a/examples/cocoa/NSString+stdstring.mm
+++ b/examples/cocoa/NSString+stdstring.mm
@@ -1,7 +1,7 @@
////////////////////////////////////////////////////////////
//
// SFML - Simple and Fast Multimedia Library
-// Copyright (C) 2007-2015 Marco Antognini (antognini.marco@gmail.com),
+// Copyright (C) 2007-2016 Marco Antognini (antognini.marco@gmail.com),
// Laurent Gomila (laurent@sfml-dev.org)
//
// This software is provided 'as-is', without any express or implied warranty.
diff --git a/examples/cocoa/main.m b/examples/cocoa/main.m
index 2609d64..acd83f2 100644
--- a/examples/cocoa/main.m
+++ b/examples/cocoa/main.m
@@ -1,7 +1,7 @@
////////////////////////////////////////////////////////////
//
// SFML - Simple and Fast Multimedia Library
-// Copyright (C) 2007-2015 Marco Antognini (antognini.marco@gmail.com),
+// Copyright (C) 2007-2016 Marco Antognini (antognini.marco@gmail.com),
// Laurent Gomila (laurent@sfml-dev.org)
//
// This software is provided 'as-is', without any express or implied warranty.
diff --git a/examples/cocoa/resources/Cocoa-Info.plist b/examples/cocoa/resources/Cocoa-Info.plist
index 0e730f2..5ec9e54 100644
--- a/examples/cocoa/resources/Cocoa-Info.plist
+++ b/examples/cocoa/resources/Cocoa-Info.plist
@@ -25,7 +25,7 @@
<key>LSMinimumSystemVersion</key>
<string>10.6</string>
<key>NSHumanReadableCopyright</key>
- <string>Copyright © 2007-2015 Marco Antognini and Laurent Gomila. Shared under zlib/libpng License.</string>
+ <string>Copyright © 2007-2016 Marco Antognini and Laurent Gomila. Shared under zlib/libpng License.</string>
<key>NSMainNibFile</key>
<string>MainMenu</string>
<key>NSPrincipalClass</key>
diff --git a/examples/opengl/OpenGL.cpp b/examples/opengl/OpenGL.cpp
index c2d1697..20af113 100644
--- a/examples/opengl/OpenGL.cpp
+++ b/examples/opengl/OpenGL.cpp
@@ -5,6 +5,10 @@
#include <SFML/Graphics.hpp>
#include <SFML/OpenGL.hpp>
+#ifndef GL_SRGB8_ALPHA8
+#define GL_SRGB8_ALPHA8 0x8C43
+#endif
+
////////////////////////////////////////////////////////////
/// Entry point of application
@@ -14,185 +18,221 @@
////////////////////////////////////////////////////////////
int main()
{
- // Request a 24-bits depth buffer when creating the window
- sf::ContextSettings contextSettings;
- contextSettings.depthBits = 24;
-
- // Create the main window
- sf::RenderWindow window(sf::VideoMode(800, 600), "SFML graphics with OpenGL", sf::Style::Default, contextSettings);
- window.setVerticalSyncEnabled(true);
-
- // Create a sprite for the background
- sf::Texture backgroundTexture;
- if (!backgroundTexture.loadFromFile("resources/background.jpg"))
- return EXIT_FAILURE;
- sf::Sprite background(backgroundTexture);
-
- // Create some text to draw on top of our OpenGL object
- sf::Font font;
- if (!font.loadFromFile("resources/sansation.ttf"))
- return EXIT_FAILURE;
- sf::Text text("SFML / OpenGL demo", font);
- text.setColor(sf::Color(255, 255, 255, 170));
- text.setPosition(250.f, 450.f);
-
- // Make the window the active target for OpenGL calls
- // Note: If using sf::Texture or sf::Shader with OpenGL,
- // be sure to call sf::Texture::getMaximumSize() and/or
- // sf::Shader::isAvailable() at least once before calling
- // setActive(), as those functions will cause a context switch
- window.setActive();
-
- // Load an OpenGL texture.
- // We could directly use a sf::Texture 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 from an image
- GLuint texture = 0;
- {
- sf::Image image;
- if (!image.loadFromFile("resources/texture.jpg"))
- return EXIT_FAILURE;
- glGenTextures(1, &texture);
- glBindTexture(GL_TEXTURE_2D, texture);
- glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, image.getSize().x, image.getSize().y, 0, GL_RGBA, GL_UNSIGNED_BYTE, image.getPixelsPtr());
- glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
- glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
- }
-
- // Enable Z-buffer read and write
- glEnable(GL_DEPTH_TEST);
- glDepthMask(GL_TRUE);
- glClearDepth(1.f);
+ bool exit = false;
+ bool sRgb = false;
- // Disable lighting
- glDisable(GL_LIGHTING);
-
- // Configure the viewport (the same size as the window)
- glViewport(0, 0, window.getSize().x, window.getSize().y);
-
- // Setup a perspective projection
- glMatrixMode(GL_PROJECTION);
- glLoadIdentity();
- GLfloat ratio = static_cast<float>(window.getSize().x) / window.getSize().y;
- glFrustum(-ratio, ratio, -1.f, 1.f, 1.f, 500.f);
-
- // Bind the texture
- glEnable(GL_TEXTURE_2D);
- glBindTexture(GL_TEXTURE_2D, texture);
-
- // Define a 3D cube (6 faces made of 2 triangles composed by 3 vertices)
- static const GLfloat cube[] =
- {
- // positions // texture coordinates
- -20, -20, -20, 0, 0,
- -20, 20, -20, 1, 0,
- -20, -20, 20, 0, 1,
- -20, -20, 20, 0, 1,
- -20, 20, -20, 1, 0,
- -20, 20, 20, 1, 1,
-
- 20, -20, -20, 0, 0,
- 20, 20, -20, 1, 0,
- 20, -20, 20, 0, 1,
- 20, -20, 20, 0, 1,
- 20, 20, -20, 1, 0,
- 20, 20, 20, 1, 1,
-
- -20, -20, -20, 0, 0,
- 20, -20, -20, 1, 0,
- -20, -20, 20, 0, 1,
- -20, -20, 20, 0, 1,
- 20, -20, -20, 1, 0,
- 20, -20, 20, 1, 1,
-
- -20, 20, -20, 0, 0,
- 20, 20, -20, 1, 0,
- -20, 20, 20, 0, 1,
- -20, 20, 20, 0, 1,
- 20, 20, -20, 1, 0,
- 20, 20, 20, 1, 1,
-
- -20, -20, -20, 0, 0,
- 20, -20, -20, 1, 0,
- -20, 20, -20, 0, 1,
- -20, 20, -20, 0, 1,
- 20, -20, -20, 1, 0,
- 20, 20, -20, 1, 1,
-
- -20, -20, 20, 0, 0,
- 20, -20, 20, 1, 0,
- -20, 20, 20, 0, 1,
- -20, 20, 20, 0, 1,
- 20, -20, 20, 1, 0,
- 20, 20, 20, 1, 1
- };
-
- // Enable position and texture coordinates vertex components
- glEnableClientState(GL_VERTEX_ARRAY);
- glEnableClientState(GL_TEXTURE_COORD_ARRAY);
- glVertexPointer(3, GL_FLOAT, 5 * sizeof(GLfloat), cube);
- glTexCoordPointer(2, GL_FLOAT, 5 * sizeof(GLfloat), cube + 3);
-
- // Disable normal and color vertex components
- glDisableClientState(GL_NORMAL_ARRAY);
- glDisableClientState(GL_COLOR_ARRAY);
-
- // Create a clock for measuring the time elapsed
- sf::Clock clock;
-
- // Start game loop
- while (window.isOpen())
+ while (!exit)
{
- // Process events
- sf::Event event;
- while (window.pollEvent(event))
- {
- // Close window: exit
- if (event.type == sf::Event::Closed)
- window.close();
+ // Request a 24-bits depth buffer when creating the window
+ sf::ContextSettings contextSettings;
+ contextSettings.depthBits = 24;
+ contextSettings.sRgbCapable = sRgb;
+
+ // Create the main window
+ sf::RenderWindow window(sf::VideoMode(800, 600), "SFML graphics with OpenGL", sf::Style::Default, contextSettings);
+ window.setVerticalSyncEnabled(true);
+
+ // Create a sprite for the background
+ sf::Texture backgroundTexture;
+ backgroundTexture.setSrgb(sRgb);
+ if (!backgroundTexture.loadFromFile("resources/background.jpg"))
+ return EXIT_FAILURE;
+ sf::Sprite background(backgroundTexture);
- // Escape key: exit
- if ((event.type == sf::Event::KeyPressed) && (event.key.code == sf::Keyboard::Escape))
- window.close();
+ // Create some text to draw on top of our OpenGL object
+ sf::Font font;
+ if (!font.loadFromFile("resources/sansation.ttf"))
+ return EXIT_FAILURE;
+ sf::Text text("SFML / OpenGL demo", font);
+ sf::Text sRgbInstructions("Press space to toggle sRGB conversion", font);
+ sf::Text mipmapInstructions("Press return to toggle mipmapping", font);
+ text.setFillColor(sf::Color(255, 255, 255, 170));
+ sRgbInstructions.setFillColor(sf::Color(255, 255, 255, 170));
+ mipmapInstructions.setFillColor(sf::Color(255, 255, 255, 170));
+ text.setPosition(250.f, 450.f);
+ sRgbInstructions.setPosition(150.f, 500.f);
+ mipmapInstructions.setPosition(180.f, 550.f);
+
+ // Load a texture to apply to our 3D cube
+ sf::Texture texture;
+ if (!texture.loadFromFile("resources/texture.jpg"))
+ return EXIT_FAILURE;
- // Adjust the viewport when the window is resized
- if (event.type == sf::Event::Resized)
- glViewport(0, 0, event.size.width, event.size.height);
- }
+ // Attempt to generate a mipmap for our cube texture
+ // We don't check the return value here since
+ // mipmapping is purely optional in this example
+ texture.generateMipmap();
- // Draw the background
- window.pushGLStates();
- window.draw(background);
- window.popGLStates();
+ // Enable Z-buffer read and write
+ glEnable(GL_DEPTH_TEST);
+ glDepthMask(GL_TRUE);
+ glClearDepth(1.f);
- // Clear the depth buffer
- glClear(GL_DEPTH_BUFFER_BIT);
+ // Disable lighting
+ glDisable(GL_LIGHTING);
- // We get the position of the mouse cursor, so that we can move the box accordingly
- float x = sf::Mouse::getPosition(window).x * 200.f / window.getSize().x - 100.f;
- float y = -sf::Mouse::getPosition(window).y * 200.f / window.getSize().y + 100.f;
+ // Configure the viewport (the same size as the window)
+ glViewport(0, 0, window.getSize().x, window.getSize().y);
- // Apply some transformations
- glMatrixMode(GL_MODELVIEW);
+ // Setup a perspective projection
+ glMatrixMode(GL_PROJECTION);
glLoadIdentity();
- glTranslatef(x, y, -100.f);
- glRotatef(clock.getElapsedTime().asSeconds() * 50.f, 1.f, 0.f, 0.f);
- glRotatef(clock.getElapsedTime().asSeconds() * 30.f, 0.f, 1.f, 0.f);
- glRotatef(clock.getElapsedTime().asSeconds() * 90.f, 0.f, 0.f, 1.f);
+ GLfloat ratio = static_cast<float>(window.getSize().x) / window.getSize().y;
+ glFrustum(-ratio, ratio, -1.f, 1.f, 1.f, 500.f);
- // Draw the cube
- glDrawArrays(GL_TRIANGLES, 0, 36);
+ // Bind the texture
+ glEnable(GL_TEXTURE_2D);
+ sf::Texture::bind(&texture);
- // Draw some text on top of our OpenGL object
- window.pushGLStates();
- window.draw(text);
- window.popGLStates();
-
- // Finally, display the rendered frame on screen
- window.display();
+ // Define a 3D cube (6 faces made of 2 triangles composed by 3 vertices)
+ static const GLfloat cube[] =
+ {
+ // positions // texture coordinates
+ -20, -20, -20, 0, 0,
+ -20, 20, -20, 1, 0,
+ -20, -20, 20, 0, 1,
+ -20, -20, 20, 0, 1,
+ -20, 20, -20, 1, 0,
+ -20, 20, 20, 1, 1,
+
+ 20, -20, -20, 0, 0,
+ 20, 20, -20, 1, 0,
+ 20, -20, 20, 0, 1,
+ 20, -20, 20, 0, 1,
+ 20, 20, -20, 1, 0,
+ 20, 20, 20, 1, 1,
+
+ -20, -20, -20, 0, 0,
+ 20, -20, -20, 1, 0,
+ -20, -20, 20, 0, 1,
+ -20, -20, 20, 0, 1,
+ 20, -20, -20, 1, 0,
+ 20, -20, 20, 1, 1,
+
+ -20, 20, -20, 0, 0,
+ 20, 20, -20, 1, 0,
+ -20, 20, 20, 0, 1,
+ -20, 20, 20, 0, 1,
+ 20, 20, -20, 1, 0,
+ 20, 20, 20, 1, 1,
+
+ -20, -20, -20, 0, 0,
+ 20, -20, -20, 1, 0,
+ -20, 20, -20, 0, 1,
+ -20, 20, -20, 0, 1,
+ 20, -20, -20, 1, 0,
+ 20, 20, -20, 1, 1,
+
+ -20, -20, 20, 0, 0,
+ 20, -20, 20, 1, 0,
+ -20, 20, 20, 0, 1,
+ -20, 20, 20, 0, 1,
+ 20, -20, 20, 1, 0,
+ 20, 20, 20, 1, 1
+ };
+
+ // Enable position and texture coordinates vertex components
+ glEnableClientState(GL_VERTEX_ARRAY);
+ glEnableClientState(GL_TEXTURE_COORD_ARRAY);
+ glVertexPointer(3, GL_FLOAT, 5 * sizeof(GLfloat), cube);
+ glTexCoordPointer(2, GL_FLOAT, 5 * sizeof(GLfloat), cube + 3);
+
+ // Disable normal and color vertex components
+ glDisableClientState(GL_NORMAL_ARRAY);
+ glDisableClientState(GL_COLOR_ARRAY);
+
+ // Create a clock for measuring the time elapsed
+ sf::Clock clock;
+
+ // Flag to track whether mipmapping is currently enabled
+ bool mipmapEnabled = true;
+
+ // Start game loop
+ while (window.isOpen())
+ {
+ // Process events
+ sf::Event event;
+ while (window.pollEvent(event))
+ {
+ // Close window: exit
+ if (event.type == sf::Event::Closed)
+ {
+ exit = true;
+ window.close();
+ }
+
+ // Escape key: exit
+ if ((event.type == sf::Event::KeyPressed) && (event.key.code == sf::Keyboard::Escape))
+ {
+ exit = true;
+ window.close();
+ }
+
+ // Return key: toggle mipmapping
+ if ((event.type == sf::Event::KeyPressed) && (event.key.code == sf::Keyboard::Return))
+ {
+ if (mipmapEnabled)
+ {
+ // We simply reload the texture to disable mipmapping
+ if (!texture.loadFromFile("resources/texture.jpg"))
+ return EXIT_FAILURE;
+
+ mipmapEnabled = false;
+ }
+ else
+ {
+ texture.generateMipmap();
+
+ mipmapEnabled = true;
+ }
+ }
+
+ // Space key: toggle sRGB conversion
+ if ((event.type == sf::Event::KeyPressed) && (event.key.code == sf::Keyboard::Space))
+ {
+ sRgb = !sRgb;
+ window.close();
+ }
+
+ // Adjust the viewport when the window is resized
+ if (event.type == sf::Event::Resized)
+ glViewport(0, 0, event.size.width, event.size.height);
+ }
+
+ // Draw the background
+ window.pushGLStates();
+ window.draw(background);
+ window.popGLStates();
+
+ // Clear the depth buffer
+ glClear(GL_DEPTH_BUFFER_BIT);
+
+ // We get the position of the mouse cursor, so that we can move the box accordingly
+ float x = sf::Mouse::getPosition(window).x * 200.f / window.getSize().x - 100.f;
+ float y = -sf::Mouse::getPosition(window).y * 200.f / window.getSize().y + 100.f;
+
+ // Apply some transformations
+ glMatrixMode(GL_MODELVIEW);
+ glLoadIdentity();
+ glTranslatef(x, y, -100.f);
+ glRotatef(clock.getElapsedTime().asSeconds() * 50.f, 1.f, 0.f, 0.f);
+ glRotatef(clock.getElapsedTime().asSeconds() * 30.f, 0.f, 1.f, 0.f);
+ glRotatef(clock.getElapsedTime().asSeconds() * 90.f, 0.f, 0.f, 1.f);
+
+ // Draw the cube
+ glDrawArrays(GL_TRIANGLES, 0, 36);
+
+ // Draw some text on top of our OpenGL object
+ window.pushGLStates();
+ window.draw(text);
+ window.draw(sRgbInstructions);
+ window.draw(mipmapInstructions);
+ window.popGLStates();
+
+ // Finally, display the rendered frame on screen
+ window.display();
+ }
}
- // Don't forget to destroy our texture
- glDeleteTextures(1, &texture);
-
return EXIT_SUCCESS;
}
diff --git a/examples/pong/Pong.cpp b/examples/pong/Pong.cpp
index e9edf23..58c9fd7 100644
--- a/examples/pong/Pong.cpp
+++ b/examples/pong/Pong.cpp
@@ -71,7 +71,7 @@ int main()
pauseMessage.setFont(font);
pauseMessage.setCharacterSize(40);
pauseMessage.setPosition(170.f, 150.f);
- pauseMessage.setColor(sf::Color::White);
+ pauseMessage.setFillColor(sf::Color::White);
pauseMessage.setString("Welcome to SFML pong!\nPress space to start the game");
// Define the paddles properties
diff --git a/examples/shader/Shader.cpp b/examples/shader/Shader.cpp
index ab147d9..8a81fde 100644
--- a/examples/shader/Shader.cpp
+++ b/examples/shader/Shader.cpp
@@ -31,14 +31,14 @@ public:
// Load the shader
if (!m_shader.loadFromFile("resources/pixelate.frag", sf::Shader::Fragment))
return false;
- m_shader.setParameter("texture", sf::Shader::CurrentTexture);
+ m_shader.setUniform("texture", sf::Shader::CurrentTexture);
return true;
}
void onUpdate(float, float x, float y)
{
- m_shader.setParameter("pixel_threshold", (x + y) / 30);
+ m_shader.setUniform("pixel_threshold", (x + y) / 30);
}
void onDraw(sf::RenderTarget& target, sf::RenderStates states) const
@@ -101,9 +101,9 @@ public:
void onUpdate(float time, float x, float y)
{
- m_shader.setParameter("wave_phase", time);
- m_shader.setParameter("wave_amplitude", x * 40, y * 40);
- m_shader.setParameter("blur_radius", (x + y) * 0.008f);
+ m_shader.setUniform("wave_phase", time);
+ m_shader.setUniform("wave_amplitude", sf::Vector2f(x * 40, y * 40));
+ m_shader.setUniform("blur_radius", (x + y) * 0.008f);
}
void onDraw(sf::RenderTarget& target, sf::RenderStates states) const
@@ -155,10 +155,10 @@ public:
void onUpdate(float time, float x, float y)
{
float radius = 200 + std::cos(time) * 150;
- m_shader.setParameter("storm_position", x * 800, y * 600);
- m_shader.setParameter("storm_inner_radius", radius / 3);
- m_shader.setParameter("storm_total_radius", radius);
- m_shader.setParameter("blink_alpha", 0.5f + std::cos(time * 3) * 0.25f);
+ m_shader.setUniform("storm_position", sf::Vector2f(x * 800, y * 600));
+ m_shader.setUniform("storm_inner_radius", radius / 3);
+ m_shader.setUniform("storm_total_radius", radius);
+ m_shader.setUniform("blink_alpha", 0.5f + std::cos(time * 3) * 0.25f);
}
void onDraw(sf::RenderTarget& target, sf::RenderStates states) const
@@ -215,14 +215,14 @@ public:
// Load the shader
if (!m_shader.loadFromFile("resources/edge.frag", sf::Shader::Fragment))
return false;
- m_shader.setParameter("texture", sf::Shader::CurrentTexture);
+ m_shader.setUniform("texture", sf::Shader::CurrentTexture);
return true;
}
void onUpdate(float time, float x, float y)
{
- m_shader.setParameter("edge_threshold", 1 - (x + y) / 2);
+ m_shader.setUniform("edge_threshold", 1 - (x + y) / 2);
// Update the position of the moving entities
for (std::size_t i = 0; i < m_entities.size(); ++i)
@@ -259,6 +259,85 @@ private:
////////////////////////////////////////////////////////////
+// "Geometry" geometry shader example
+////////////////////////////////////////////////////////////
+class Geometry : public Effect
+{
+public:
+
+ Geometry() :
+ Effect("geometry shader billboards"),
+ m_pointCloud(sf::Points, 10000)
+ {
+ }
+
+ bool onLoad()
+ {
+ // Check if geometry shaders are supported
+ if (!sf::Shader::isGeometryAvailable())
+ return false;
+
+ // Move the points in the point cloud to random positions
+ for (std::size_t i = 0; i < 10000; i++)
+ {
+ // Spread the coordinates from -480 to +480
+ // So they'll always fill the viewport at 800x600
+ m_pointCloud[i].position.x = rand() % 960 - 480.f;
+ m_pointCloud[i].position.y = rand() % 960 - 480.f;
+ }
+
+ // Load the texture
+ if (!m_logoTexture.loadFromFile("resources/logo.png"))
+ return false;
+
+ // Load the shader
+ if (!m_shader.loadFromFile("resources/billboard.vert", "resources/billboard.geom", "resources/billboard.frag"))
+ return false;
+ m_shader.setUniform("texture", sf::Shader::CurrentTexture);
+
+ // Set the render resolution (used for proper scaling)
+ m_shader.setUniform("resolution", sf::Vector2f(800, 600));
+
+ return true;
+ }
+
+ void onUpdate(float time, float x, float y)
+ {
+ // Reset our transformation matrix
+ m_transform = sf::Transform::Identity;
+ // Move to the center of the window
+ m_transform.translate(400, 300);
+ // Rotate everything based on cursor position
+ m_transform.rotate(x * 360.f);
+
+ // Adjust billboard size to scale between 25 and 75
+ float size = 25 + std::abs(y) * 50;
+
+ // Update the shader parameter
+ m_shader.setUniform("size", sf::Vector2f(size, size));
+ }
+
+ void onDraw(sf::RenderTarget& target, sf::RenderStates states) const
+ {
+ // Prepare the render state
+ states.shader = &m_shader;
+ states.texture = &m_logoTexture;
+ states.transform = m_transform;
+
+ // Draw the point cloud
+ target.draw(m_pointCloud, states);
+ }
+
+private:
+
+ sf::Texture m_logoTexture;
+ sf::Transform m_transform;
+ sf::Shader m_shader;
+ sf::VertexArray m_pointCloud;
+};
+
+
+////////////////////////////////////////////////////////////
/// Entry point of application
///
/// \return Application exit code
@@ -283,6 +362,7 @@ int main()
effects.push_back(new WaveBlur);
effects.push_back(new StormBlink);
effects.push_back(new Edge);
+ effects.push_back(new Geometry);
std::size_t current = 0;
// Initialize them
@@ -300,12 +380,12 @@ int main()
// Create the description text
sf::Text description("Current effect: " + effects[current]->getName(), font, 20);
description.setPosition(10, 530);
- description.setColor(sf::Color(80, 80, 80));
+ description.setFillColor(sf::Color(80, 80, 80));
// Create the instructions text
sf::Text instructions("Press left and right arrows to change the current shader", font, 20);
instructions.setPosition(280, 555);
- instructions.setColor(sf::Color(80, 80, 80));
+ instructions.setFillColor(sf::Color(80, 80, 80));
// Start the game loop
sf::Clock clock;
diff --git a/examples/shader/resources/billboard.frag b/examples/shader/resources/billboard.frag
new file mode 100644
index 0000000..3057f64
--- /dev/null
+++ b/examples/shader/resources/billboard.frag
@@ -0,0 +1,11 @@
+#version 150
+
+uniform sampler2D texture;
+
+in vec2 tex_coord;
+
+void main()
+{
+ // Read and apply a color from the texture
+ gl_FragColor = texture2D(texture, tex_coord);
+}
diff --git a/examples/shader/resources/billboard.geom b/examples/shader/resources/billboard.geom
new file mode 100644
index 0000000..2f47a1f
--- /dev/null
+++ b/examples/shader/resources/billboard.geom
@@ -0,0 +1,56 @@
+#version 150
+
+// The render target's resolution (used for scaling)
+uniform vec2 resolution;
+
+// The billboards' size
+uniform vec2 size;
+
+// Input is the passed point cloud
+layout (points) in;
+
+// The output will consist of triangle strips with four vertices each
+layout (triangle_strip, max_vertices = 4) out;
+
+// Output texture coordinates
+out vec2 tex_coord;
+
+// Main entry point
+void main()
+{
+ // Caculate the half width/height of the billboards
+ vec2 half_size = size / 2.f;
+
+ // Scale the size based on resolution (1 would be full width/height)
+ half_size /= resolution;
+
+ // Iterate over all vertices
+ for (int i = 0; i < gl_in.length(); i++)
+ {
+ // Retrieve the passed vertex position
+ vec2 pos = gl_in[i].gl_Position.xy;
+
+ // Bottom left vertex
+ gl_Position = vec4(pos - half_size, 0.f, 1.f);
+ tex_coord = vec2(1.f, 1.f);
+ EmitVertex();
+
+ // Bottom right vertex
+ gl_Position = vec4(pos.x + half_size.x, pos.y - half_size.y, 0.f, 1.f);
+ tex_coord = vec2(0.f, 1.f);
+ EmitVertex();
+
+ // Top left vertex
+ gl_Position = vec4(pos.x - half_size.x, pos.y + half_size.y, 0.f, 1.f);
+ tex_coord = vec2(1.f, 0.f);
+ EmitVertex();
+
+ // Top right vertex
+ gl_Position = vec4(pos + half_size, 0.f, 1.f);
+ tex_coord = vec2(0.f, 0.f);
+ EmitVertex();
+
+ // And finalize the primitive
+ EndPrimitive();
+ }
+}
diff --git a/examples/shader/resources/billboard.vert b/examples/shader/resources/billboard.vert
new file mode 100644
index 0000000..3a89905
--- /dev/null
+++ b/examples/shader/resources/billboard.vert
@@ -0,0 +1,5 @@
+void main()
+{
+ // Transform the vertex position
+ gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex;
+}
diff --git a/examples/shader/resources/logo.png b/examples/shader/resources/logo.png
new file mode 100644
index 0000000..29ba010
--- /dev/null
+++ b/examples/shader/resources/logo.png
Binary files differ
diff --git a/examples/sound/Sound.cpp b/examples/sound/Sound.cpp
index b579fb5..e71aa0d 100644
--- a/examples/sound/Sound.cpp
+++ b/examples/sound/Sound.cpp
@@ -3,7 +3,6 @@
// Headers
////////////////////////////////////////////////////////////
#include <SFML/Audio.hpp>
-#include <iomanip>
#include <iostream>
#include <string>
@@ -36,7 +35,7 @@ void playSound()
sf::sleep(sf::milliseconds(100));
// Display the playing position
- std::cout << "\rPlaying... " << std::fixed << std::setprecision(2) << sound.getPlayingOffset().asSeconds() << " sec ";
+ std::cout << "\rPlaying... " << sound.getPlayingOffset().asSeconds() << " sec ";
std::cout << std::flush;
}
std::cout << std::endl << std::endl;
@@ -70,7 +69,7 @@ void playMusic(const std::string& filename)
sf::sleep(sf::milliseconds(100));
// Display the playing position
- std::cout << "\rPlaying... " << std::fixed << std::setprecision(2) << music.getPlayingOffset().asSeconds() << " sec ";
+ std::cout << "\rPlaying... " << music.getPlayingOffset().asSeconds() << " sec ";
std::cout << std::flush;
}
std::cout << std::endl << std::endl;
diff --git a/examples/sound_capture/SoundCapture.cpp b/examples/sound_capture/SoundCapture.cpp
index 02f2db2..19f114b 100644
--- a/examples/sound_capture/SoundCapture.cpp
+++ b/examples/sound_capture/SoundCapture.cpp
@@ -3,7 +3,6 @@
// Headers
////////////////////////////////////////////////////////////
#include <SFML/Audio.hpp>
-#include <iomanip>
#include <iostream>
@@ -76,7 +75,7 @@ int main()
while (sound.getStatus() == sf::Sound::Playing)
{
// Display the playing position
- std::cout << "\rPlaying... " << std::fixed << std::setprecision(2) << sound.getPlayingOffset().asSeconds() << " sec";
+ std::cout << "\rPlaying... " << sound.getPlayingOffset().asSeconds() << " sec ";
std::cout << std::flush;
// Leave some CPU time for other threads
diff --git a/examples/voip/Client.cpp b/examples/voip/Client.cpp
index 595d624..d66e0e2 100644
--- a/examples/voip/Client.cpp
+++ b/examples/voip/Client.cpp
@@ -32,10 +32,22 @@ public:
{
}
+ ////////////////////////////////////////////////////////////
+ /// Destructor
+ ///
+ /// \see SoundRecorder::~SoundRecorder()
+ ///
+ ////////////////////////////////////////////////////////////
+ ~NetworkRecorder()
+ {
+ // Make sure to stop the recording thread
+ stop();
+ }
+
private:
////////////////////////////////////////////////////////////
- /// /see SoundRecorder::OnStart
+ /// \see SoundRecorder::onStart
///
////////////////////////////////////////////////////////////
virtual bool onStart()
@@ -52,7 +64,7 @@ private:
}
////////////////////////////////////////////////////////////
- /// /see SoundRecorder::ProcessSamples
+ /// \see SoundRecorder::onProcessSamples
///
////////////////////////////////////////////////////////////
virtual bool onProcessSamples(const sf::Int16* samples, std::size_t sampleCount)
@@ -67,7 +79,7 @@ private:
}
////////////////////////////////////////////////////////////
- /// /see SoundRecorder::OnStop
+ /// \see SoundRecorder::onStop
///
////////////////////////////////////////////////////////////
virtual void onStop()
@@ -98,7 +110,7 @@ private:
void doClient(unsigned short port)
{
// Check that the device can capture audio
- if (sf::SoundRecorder::isAvailable() == false)
+ if (!sf::SoundRecorder::isAvailable())
{
std::cout << "Sorry, audio capture is not supported by your system" << std::endl;
return;