mirror of
https://github.com/SFML/SFML.git
synced 2024-11-25 04:41:05 +08:00
Update examples for iOS including touch support, proper handling of window resize, launch screens and icons
This commit is contained in:
parent
9f4a0c3c4f
commit
353c846c87
@ -223,7 +223,18 @@ macro(sfml_add_example target)
|
||||
add_executable(${target} WIN32 ${target_input})
|
||||
target_link_libraries(${target} PRIVATE sfml-main)
|
||||
elseif(THIS_GUI_APP AND SFML_OS_IOS)
|
||||
add_executable(${target} MACOSX_BUNDLE ${target_input})
|
||||
|
||||
# For iOS apps we need the launch screen storyboard,
|
||||
# and a custom info.plist to use it
|
||||
SET(LAUNCH_SCREEN "${CMAKE_SOURCE_DIR}/examples/assets/LaunchScreen.storyboard")
|
||||
SET(LOGO "${CMAKE_SOURCE_DIR}/examples/assets/logo.png")
|
||||
SET(INFO_PLIST "${CMAKE_SOURCE_DIR}/examples/assets/info.plist")
|
||||
SET(ICONS "${CMAKE_SOURCE_DIR}/examples/assets/icon.icns")
|
||||
add_executable(${target} MACOSX_BUNDLE ${target_input} ${LAUNCH_SCREEN} ${LOGO} ${ICONS})
|
||||
set(RESOURCES ${LAUNCH_SCREEN} ${LOGO} ${ICONS})
|
||||
set_target_properties(${target} PROPERTIES RESOURCE "${RESOURCES}"
|
||||
MACOSX_BUNDLE_INFO_PLIST ${INFO_PLIST}
|
||||
MACOSX_BUNDLE_ICON_FILE icon.icns)
|
||||
target_link_libraries(${target} PRIVATE sfml-main)
|
||||
else()
|
||||
add_executable(${target} ${target_input})
|
||||
@ -264,7 +275,8 @@ macro(sfml_add_example target)
|
||||
# add the install rule
|
||||
install(TARGETS ${target}
|
||||
RUNTIME DESTINATION ${target_install_dir} COMPONENT examples
|
||||
BUNDLE DESTINATION ${target_install_dir} COMPONENT examples)
|
||||
BUNDLE DESTINATION ${target_install_dir} COMPONENT examples
|
||||
RESOURCE DESTINATION ${target_install_dir} COMPONENT examples)
|
||||
|
||||
# install the example's source code
|
||||
install(FILES ${THIS_SOURCES}
|
||||
|
38
examples/assets/LaunchScreen.storyboard
Normal file
38
examples/assets/LaunchScreen.storyboard
Normal file
@ -0,0 +1,38 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<document type="com.apple.InterfaceBuilder3.CocoaTouch.Storyboard.XIB" version="3.0" toolsVersion="14460.31" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" launchScreen="YES" useTraitCollections="YES" useSafeAreas="YES" colorMatched="YES" initialViewController="01J-lp-oVM">
|
||||
<device id="ipad9_7" orientation="landscape">
|
||||
<adaptation id="fullscreen"/>
|
||||
</device>
|
||||
<dependencies>
|
||||
<plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="14460.20"/>
|
||||
<capability name="Safe area layout guides" minToolsVersion="9.0"/>
|
||||
<capability name="documents saved in the Xcode 8 format" minToolsVersion="8.0"/>
|
||||
</dependencies>
|
||||
<scenes>
|
||||
<!--View Controller-->
|
||||
<scene sceneID="EHf-IW-A2E">
|
||||
<objects>
|
||||
<viewController id="01J-lp-oVM" sceneMemberID="viewController">
|
||||
<view key="view" contentMode="scaleToFill" id="Ze5-6b-2t3">
|
||||
<rect key="frame" x="0.0" y="0.0" width="1024" height="768"/>
|
||||
<autoresizingMask key="autoresizingMask" widthSizable="YES" heightSizable="YES"/>
|
||||
<subviews>
|
||||
<imageView userInteractionEnabled="NO" contentMode="scaleAspectFit" horizontalHuggingPriority="251" verticalHuggingPriority="251" fixedFrame="YES" image="logo.png" translatesAutoresizingMaskIntoConstraints="NO" id="HXY-06-8aG">
|
||||
<rect key="frame" x="0.0" y="0.0" width="1024" height="768"/>
|
||||
<autoresizingMask key="autoresizingMask" widthSizable="YES" heightSizable="YES"/>
|
||||
<color key="backgroundColor" white="0.0" alpha="1" colorSpace="custom" customColorSpace="genericGamma22GrayColorSpace"/>
|
||||
</imageView>
|
||||
</subviews>
|
||||
<color key="backgroundColor" red="1" green="1" blue="1" alpha="1" colorSpace="custom" customColorSpace="sRGB"/>
|
||||
<viewLayoutGuide key="safeArea" id="Bcu-3y-fUS"/>
|
||||
</view>
|
||||
</viewController>
|
||||
<placeholder placeholderIdentifier="IBFirstResponder" id="iYj-Kq-Ea1" userLabel="First Responder" sceneMemberID="firstResponder"/>
|
||||
</objects>
|
||||
<point key="canvasLocation" x="52" y="374.66266866566718"/>
|
||||
</scene>
|
||||
</scenes>
|
||||
<resources>
|
||||
<image name="logo.png" width="512" height="512"/>
|
||||
</resources>
|
||||
</document>
|
BIN
examples/assets/icon.icns
Normal file
BIN
examples/assets/icon.icns
Normal file
Binary file not shown.
36
examples/assets/info.plist
Normal file
36
examples/assets/info.plist
Normal file
@ -0,0 +1,36 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
|
||||
<plist version="1.0">
|
||||
<dict>
|
||||
<key>CFBundleIconFile</key>
|
||||
<string>icon.icns</string>
|
||||
<key>UILaunchStoryboardName</key>
|
||||
<string>LaunchScreen</string>
|
||||
<key>CFBundleDevelopmentRegion</key>
|
||||
<string>English</string>
|
||||
<key>CFBundleExecutable</key>
|
||||
<string>$(EXECUTABLE_NAME)</string>
|
||||
<key>CFBundleGetInfoString</key>
|
||||
<string></string>
|
||||
<key>CFBundleIdentifier</key>
|
||||
<string>org.sfml-dev.$(EXECUTABLE_NAME)</string>
|
||||
<key>CFBundleInfoDictionaryVersion</key>
|
||||
<string>6.0</string>
|
||||
<key>CFBundleLongVersionString</key>
|
||||
<string>2.5.1</string>
|
||||
<key>CFBundleName</key>
|
||||
<string>$(EXECUTABLE_NAME)</string>
|
||||
<key>CFBundlePackageType</key>
|
||||
<string>APPL</string>
|
||||
<key>CFBundleShortVersionString</key>
|
||||
<string></string>
|
||||
<key>CFBundleSignature</key>
|
||||
<string>????</string>
|
||||
<key>CFBundleVersion</key>
|
||||
<string></string>
|
||||
<key>CSResourcesFileMapped</key>
|
||||
<true/>
|
||||
<key>NSHumanReadableCopyright</key>
|
||||
<string></string>
|
||||
</dict>
|
||||
</plist>
|
BIN
examples/assets/logo.png
Normal file
BIN
examples/assets/logo.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 30 KiB |
Binary file not shown.
Binary file not shown.
Before Width: | Height: | Size: 8.6 KiB |
Binary file not shown.
Binary file not shown.
@ -220,13 +220,24 @@ int main()
|
||||
// Adjust the viewport when the window is resized
|
||||
if (event.type == sf::Event::Resized)
|
||||
{
|
||||
sf::Vector2u textureSize = backgroundTexture.getSize();
|
||||
|
||||
// Make the window the active window for OpenGL calls
|
||||
window.setActive(true);
|
||||
|
||||
glViewport(0, 0, event.size.width, event.size.height);
|
||||
glMatrixMode(GL_PROJECTION);
|
||||
glLoadIdentity();
|
||||
GLfloat ratio = static_cast<float>(event.size.width) / event.size.height;
|
||||
glFrustum(-ratio, ratio, -1.f, 1.f, 1.f, 500.f);
|
||||
|
||||
// Make the window no longer the active window for OpenGL calls
|
||||
window.setActive(false);
|
||||
|
||||
sf::View view;
|
||||
view.setSize(textureSize.x, textureSize.y);
|
||||
view.setCenter(textureSize.x/2.f, textureSize.y/2.f);
|
||||
window.setView(view);
|
||||
}
|
||||
}
|
||||
|
||||
@ -241,9 +252,17 @@ int main()
|
||||
// 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;
|
||||
// We get the position of the mouse cursor (or touch), so that we can move the box accordingly
|
||||
sf::Vector2i pos;
|
||||
|
||||
#ifdef SFML_SYSTEM_IOS
|
||||
pos = sf::Touch::getPosition(0);
|
||||
#else
|
||||
pos = sf::Mouse::getPosition();
|
||||
#endif
|
||||
|
||||
float x = pos.x * 200.f / window.getSize().x - 100.f;
|
||||
float y = -pos.y * 200.f / window.getSize().y + 100.f;
|
||||
|
||||
// Apply some transformations
|
||||
glMatrixMode(GL_MODELVIEW);
|
||||
|
@ -84,7 +84,12 @@ int main()
|
||||
pauseMessage.setCharacterSize(40);
|
||||
pauseMessage.setPosition(170.f, 150.f);
|
||||
pauseMessage.setFillColor(sf::Color::White);
|
||||
|
||||
#ifdef SFML_SYSTEM_IOS
|
||||
pauseMessage.setString("Welcome to SFML pong!\nTouch the screen to start the game");
|
||||
#else
|
||||
pauseMessage.setString("Welcome to SFML pong!\nPress space to start the game");
|
||||
#endif
|
||||
|
||||
// Define the paddles properties
|
||||
sf::Clock AITimer;
|
||||
@ -134,6 +139,15 @@ int main()
|
||||
while (std::abs(std::cos(ballAngle)) < 0.7f);
|
||||
}
|
||||
}
|
||||
|
||||
// Window size changed, adjust view appropriately
|
||||
if (event.type == sf::Event::Resized)
|
||||
{
|
||||
sf::View view;
|
||||
view.setSize(gameWidth, gameHeight);
|
||||
view.setCenter(gameWidth/2.f, gameHeight/2.f);
|
||||
window.setView(view);
|
||||
}
|
||||
}
|
||||
|
||||
if (isPlaying)
|
||||
@ -154,8 +168,9 @@ int main()
|
||||
|
||||
if (sf::Touch::isDown(0))
|
||||
{
|
||||
sf::Vector2i pos = sf::Touch::getPosition(0, window);
|
||||
leftPaddle.setPosition(0.f, pos.y);
|
||||
sf::Vector2i pos = sf::Touch::getPosition(0);
|
||||
sf::Vector2f mappedPos = window.mapPixelToCoords(pos);
|
||||
leftPaddle.setPosition(leftPaddle.getPosition().x, mappedPos.y);
|
||||
}
|
||||
|
||||
// Move the computer's paddle
|
||||
@ -181,16 +196,22 @@ int main()
|
||||
float factor = ballSpeed * deltaTime;
|
||||
ball.move(std::cos(ballAngle) * factor, std::sin(ballAngle) * factor);
|
||||
|
||||
#ifdef SFML_SYSTEM_IOS
|
||||
const std::string inputString = "Touch the screen to restart";
|
||||
#else
|
||||
const std::string inputString = "Press space to restart or\nescape to exit";
|
||||
#endif
|
||||
|
||||
// Check collisions between the ball and the screen
|
||||
if (ball.getPosition().x - ballRadius < 0.f)
|
||||
{
|
||||
isPlaying = false;
|
||||
pauseMessage.setString("You lost!\nPress space to restart or\nescape to exit");
|
||||
pauseMessage.setString("You Lost!\n" + inputString);
|
||||
}
|
||||
if (ball.getPosition().x + ballRadius > gameWidth)
|
||||
{
|
||||
isPlaying = false;
|
||||
pauseMessage.setString("You won!\nPress space to restart or\nescape to exit");
|
||||
pauseMessage.setString("You Won!\n" + inputString);
|
||||
}
|
||||
if (ball.getPosition().y - ballRadius < 0.f)
|
||||
{
|
||||
|
@ -129,7 +129,13 @@ int main()
|
||||
|
||||
// Resize event: adjust the viewport
|
||||
if (event.type == sf::Event::Resized)
|
||||
{
|
||||
glViewport(0, 0, event.size.width, event.size.height);
|
||||
glMatrixMode(GL_PROJECTION);
|
||||
glLoadIdentity();
|
||||
GLfloat ratio = static_cast<float>(event.size.width) / event.size.height;
|
||||
glFrustum(-ratio, ratio, -1.f, 1.f, 1.f, 500.f);
|
||||
}
|
||||
}
|
||||
|
||||
// Clear the color and depth buffers
|
||||
|
Loading…
Reference in New Issue
Block a user