Update the code for the iOS demos to handle modern devices. Fixes bug #3337

This commit is contained in:
Alex Szpakowski 2016-09-25 00:23:35 -03:00
parent 40ecac8e60
commit a8b033d7c4
12 changed files with 233 additions and 143 deletions

View file

@ -24,6 +24,13 @@
FA30DECD1BBF5C14009C397F /* UIKit.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 1DF5F4DF0D08C38300B7A737 /* UIKit.framework */; };
FA30DECE1BBF5C14009C397F /* Foundation.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 1D30AB110D05D00D00671497 /* Foundation.framework */; };
FA30DECF1BBF5C14009C397F /* CoreAudio.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = FDF0D71D0E12D2AB00247964 /* CoreAudio.framework */; };
FA86C0371D9765BB009CB637 /* iOS Launch Screen.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = FA86C0361D9765BA009CB637 /* iOS Launch Screen.storyboard */; };
FA86C0381D9765BB009CB637 /* iOS Launch Screen.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = FA86C0361D9765BA009CB637 /* iOS Launch Screen.storyboard */; };
FA86C0391D9765BB009CB637 /* iOS Launch Screen.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = FA86C0361D9765BA009CB637 /* iOS Launch Screen.storyboard */; };
FA86C03A1D9765BB009CB637 /* iOS Launch Screen.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = FA86C0361D9765BA009CB637 /* iOS Launch Screen.storyboard */; };
FA86C03B1D9765BB009CB637 /* iOS Launch Screen.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = FA86C0361D9765BA009CB637 /* iOS Launch Screen.storyboard */; };
FA86C03C1D9765BB009CB637 /* iOS Launch Screen.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = FA86C0361D9765BA009CB637 /* iOS Launch Screen.storyboard */; };
FA86C03D1D9765BB009CB637 /* iOS Launch Screen.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = FA86C0361D9765BA009CB637 /* iOS Launch Screen.storyboard */; };
FA8B4BA31967070A00F8EB7C /* CoreMotion.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = FA8B4BA21967070A00F8EB7C /* CoreMotion.framework */; };
FA8B4BA41967071300F8EB7C /* CoreMotion.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = FA8B4BA21967070A00F8EB7C /* CoreMotion.framework */; };
FA8B4BA51967071A00F8EB7C /* CoreMotion.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = FA8B4BA21967070A00F8EB7C /* CoreMotion.framework */; };
@ -220,6 +227,7 @@
1DF5F4DF0D08C38300B7A737 /* UIKit.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = UIKit.framework; path = System/Library/Frameworks/UIKit.framework; sourceTree = SDKROOT; };
8D1107310486CEB800E47090 /* Info.plist */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = "<group>"; };
FA30DE961BBF59D9009C397F /* Happy-TV.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = "Happy-TV.app"; sourceTree = BUILT_PRODUCTS_DIR; };
FA86C0361D9765BA009CB637 /* iOS Launch Screen.storyboard */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = file.storyboard; path = "iOS Launch Screen.storyboard"; sourceTree = "<group>"; };
FA8B4BA21967070A00F8EB7C /* CoreMotion.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = CoreMotion.framework; path = System/Library/Frameworks/CoreMotion.framework; sourceTree = SDKROOT; };
FABA34D31D8B5E5600915323 /* AVFoundation.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = AVFoundation.framework; path = System/Library/Frameworks/AVFoundation.framework; sourceTree = SDKROOT; };
FABA34D71D8B5E7700915323 /* AVFoundation.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = AVFoundation.framework; path = Platforms/AppleTVOS.platform/Developer/SDKs/AppleTVOS10.0.sdk/System/Library/Frameworks/AVFoundation.framework; sourceTree = DEVELOPER_DIR; };
@ -423,6 +431,7 @@
29B97314FDCFA39411CA2CEA /* CustomTemplate */ = {
isa = PBXGroup;
children = (
FA86C0361D9765BA009CB637 /* iOS Launch Screen.storyboard */,
FD1B48920E313154007AB34E /* SDL.xcodeproj */,
FD77A0040E26BC0500F39101 /* src */,
29B97317FDCFA39411CA2CEA /* Resources */,
@ -676,7 +685,9 @@
TargetAttributes = {
FA30DE951BBF59D9009C397F = {
CreatedOnToolsVersion = 7.1;
DevelopmentTeam = DJN9C5VR5G;
};
FDC52EC60E2843D6008D768C = {
ProvisioningStyle = Automatic;
};
};
};
@ -735,6 +746,7 @@
isa = PBXResourcesBuildPhase;
buildActionMask = 2147483647;
files = (
FA86C0371D9765BB009CB637 /* iOS Launch Screen.storyboard in Resources */,
FD925B1B0E0F276600E92347 /* Icon.png in Resources */,
FD787AA20E22A5CC003E8E36 /* Default.png in Resources */,
);
@ -755,6 +767,7 @@
buildActionMask = 2147483647;
files = (
FDB651D00E43D1AD00F688B5 /* icon.bmp in Resources */,
FA86C0381D9765BB009CB637 /* iOS Launch Screen.storyboard in Resources */,
FD925B1A0E0F276600E92347 /* Icon.png in Resources */,
FD787AA10E22A5CC003E8E36 /* Default.png in Resources */,
);
@ -768,6 +781,7 @@
FDB651D10E43D1B300F688B5 /* ship.bmp in Resources */,
FD925B190E0F276600E92347 /* Icon.png in Resources */,
FD787AA30E22A5CC003E8E36 /* Default.png in Resources */,
FA86C0391D9765BB009CB637 /* iOS Launch Screen.storyboard in Resources */,
);
runOnlyForDeploymentPostprocessing = 0;
};
@ -779,6 +793,7 @@
FDB651FA0E43D1F300F688B5 /* Icon.png in Resources */,
FDB651FB0E43D1F300F688B5 /* Default.png in Resources */,
FDB652C70E43E25900F688B5 /* kromasky_16x16.bmp in Resources */,
FA86C03D1D9765BB009CB637 /* iOS Launch Screen.storyboard in Resources */,
);
runOnlyForDeploymentPostprocessing = 0;
};
@ -787,6 +802,7 @@
buildActionMask = 2147483647;
files = (
FDB651D30E43D1BA00F688B5 /* stroke.bmp in Resources */,
FA86C03A1D9765BB009CB637 /* iOS Launch Screen.storyboard in Resources */,
FDC202E10E107B1200ABAC90 /* Icon.png in Resources */,
FD787AA40E22A5CC003E8E36 /* Default.png in Resources */,
);
@ -797,6 +813,7 @@
buildActionMask = 2147483647;
files = (
FDB651D80E43D1D800F688B5 /* stroke.bmp in Resources */,
FA86C03C1D9765BB009CB637 /* iOS Launch Screen.storyboard in Resources */,
FDC52EC80E2843D6008D768C /* Icon.png in Resources */,
FDC52EC90E2843D6008D768C /* Default.png in Resources */,
);
@ -809,6 +826,7 @@
FDB651D40E43D1C500F688B5 /* ds_brush_snare.wav in Resources */,
FDB651D50E43D1C500F688B5 /* ds_china.wav in Resources */,
FDB651D60E43D1C500F688B5 /* ds_kick_big_amb.wav in Resources */,
FA86C03B1D9765BB009CB637 /* iOS Launch Screen.storyboard in Resources */,
FDB651D70E43D1C500F688B5 /* ds_loose_skin_mute.wav in Resources */,
FDF0D6960E12D05400247964 /* Icon.png in Resources */,
FD787AA50E22A5CC003E8E36 /* Default.png in Resources */,
@ -940,6 +958,7 @@
isa = XCBuildConfiguration;
buildSettings = {
INFOPLIST_FILE = Info.plist;
PRODUCT_BUNDLE_IDENTIFIER = com.yourcompany.Rectangles;
PRODUCT_NAME = Rectangles;
};
name = Debug;
@ -948,6 +967,7 @@
isa = XCBuildConfiguration;
buildSettings = {
INFOPLIST_FILE = Info.plist;
PRODUCT_BUNDLE_IDENTIFIER = com.yourcompany.Rectangles;
PRODUCT_NAME = Rectangles;
};
name = Release;
@ -995,6 +1015,7 @@
CLANG_WARN__DUPLICATE_METHOD_MATCH = YES;
COPY_PHASE_STRIP = NO;
DEBUG_INFORMATION_FORMAT = dwarf;
DEVELOPMENT_TEAM = "";
ENABLE_STRICT_OBJC_MSGSEND = YES;
ENABLE_TESTABILITY = YES;
GCC_C_LANGUAGE_STANDARD = gnu99;
@ -1013,7 +1034,7 @@
INFOPLIST_FILE = Info.plist;
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks";
MTL_ENABLE_DEBUG_INFO = YES;
PRODUCT_BUNDLE_IDENTIFIER = "com.Happy-TV";
PRODUCT_BUNDLE_IDENTIFIER = "com.yourcompany.Happy-TV";
PRODUCT_NAME = "$(TARGET_NAME)";
SDKROOT = appletvos;
TARGETED_DEVICE_FAMILY = 3;
@ -1040,6 +1061,7 @@
CLANG_WARN__DUPLICATE_METHOD_MATCH = YES;
COPY_PHASE_STRIP = NO;
DEBUG_INFORMATION_FORMAT = "dwarf-with-dsym";
DEVELOPMENT_TEAM = "";
ENABLE_NS_ASSERTIONS = NO;
ENABLE_STRICT_OBJC_MSGSEND = YES;
GCC_C_LANGUAGE_STANDARD = gnu99;
@ -1053,7 +1075,7 @@
INFOPLIST_FILE = Info.plist;
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks";
MTL_ENABLE_DEBUG_INFO = NO;
PRODUCT_BUNDLE_IDENTIFIER = "com.Happy-TV";
PRODUCT_BUNDLE_IDENTIFIER = "com.yourcompany.Happy-TV";
PRODUCT_NAME = "$(TARGET_NAME)";
SDKROOT = appletvos;
TARGETED_DEVICE_FAMILY = 3;
@ -1067,6 +1089,7 @@
buildSettings = {
GCC_DYNAMIC_NO_PIC = NO;
INFOPLIST_FILE = Info.plist;
PRODUCT_BUNDLE_IDENTIFIER = com.yourcompany.Happy;
PRODUCT_NAME = Happy;
SDKROOT = iphoneos;
};
@ -1076,6 +1099,7 @@
isa = XCBuildConfiguration;
buildSettings = {
INFOPLIST_FILE = Info.plist;
PRODUCT_BUNDLE_IDENTIFIER = com.yourcompany.Happy;
PRODUCT_NAME = Happy;
SDKROOT = iphoneos;
};
@ -1085,6 +1109,7 @@
isa = XCBuildConfiguration;
buildSettings = {
INFOPLIST_FILE = Info.plist;
PRODUCT_BUNDLE_IDENTIFIER = com.yourcompany.Accel;
PRODUCT_NAME = Accel;
SDKROOT = iphoneos;
};
@ -1094,6 +1119,7 @@
isa = XCBuildConfiguration;
buildSettings = {
INFOPLIST_FILE = Info.plist;
PRODUCT_BUNDLE_IDENTIFIER = com.yourcompany.Accel;
PRODUCT_NAME = Accel;
SDKROOT = iphoneos;
};
@ -1103,6 +1129,7 @@
isa = XCBuildConfiguration;
buildSettings = {
INFOPLIST_FILE = Info.plist;
PRODUCT_BUNDLE_IDENTIFIER = com.yourcompany.Keyboard;
PRODUCT_NAME = Keyboard;
SDKROOT = iphoneos;
};
@ -1112,6 +1139,7 @@
isa = XCBuildConfiguration;
buildSettings = {
INFOPLIST_FILE = Info.plist;
PRODUCT_BUNDLE_IDENTIFIER = com.yourcompany.Keyboard;
PRODUCT_NAME = Keyboard;
SDKROOT = iphoneos;
};
@ -1121,6 +1149,7 @@
isa = XCBuildConfiguration;
buildSettings = {
INFOPLIST_FILE = Info.plist;
PRODUCT_BUNDLE_IDENTIFIER = com.yourcompany.Touch;
PRODUCT_NAME = Touch;
SDKROOT = iphoneos;
};
@ -1130,6 +1159,7 @@
isa = XCBuildConfiguration;
buildSettings = {
INFOPLIST_FILE = Info.plist;
PRODUCT_BUNDLE_IDENTIFIER = com.yourcompany.Touch;
PRODUCT_NAME = Touch;
SDKROOT = iphoneos;
};
@ -1138,7 +1168,10 @@
FDC52EDC0E2843D6008D768C /* Debug */ = {
isa = XCBuildConfiguration;
buildSettings = {
"CODE_SIGN_IDENTITY[sdk=iphoneos*]" = "iPhone Developer";
DEVELOPMENT_TEAM = "";
INFOPLIST_FILE = Info.plist;
PRODUCT_BUNDLE_IDENTIFIER = com.yourcompany.Fireworks;
PRODUCT_NAME = Fireworks;
SDKROOT = iphoneos;
};
@ -1147,7 +1180,10 @@
FDC52EDD0E2843D6008D768C /* Release */ = {
isa = XCBuildConfiguration;
buildSettings = {
"CODE_SIGN_IDENTITY[sdk=iphoneos*]" = "iPhone Developer";
DEVELOPMENT_TEAM = "";
INFOPLIST_FILE = Info.plist;
PRODUCT_BUNDLE_IDENTIFIER = com.yourcompany.Fireworks;
PRODUCT_NAME = Fireworks;
SDKROOT = iphoneos;
};
@ -1157,6 +1193,7 @@
isa = XCBuildConfiguration;
buildSettings = {
INFOPLIST_FILE = Info.plist;
PRODUCT_BUNDLE_IDENTIFIER = com.yourcompany.Mixer;
PRODUCT_NAME = Mixer;
SDKROOT = iphoneos;
};
@ -1166,6 +1203,7 @@
isa = XCBuildConfiguration;
buildSettings = {
INFOPLIST_FILE = Info.plist;
PRODUCT_BUNDLE_IDENTIFIER = com.yourcompany.Mixer;
PRODUCT_NAME = Mixer;
SDKROOT = iphoneos;
};

View file

@ -11,7 +11,7 @@
<key>CFBundleIconFile</key>
<string></string>
<key>CFBundleIdentifier</key>
<string>com.yourcompany.${PRODUCT_NAME:identifier}</string>
<string>$(PRODUCT_BUNDLE_IDENTIFIER)</string>
<key>CFBundleInfoDictionaryVersion</key>
<string>6.0</string>
<key>CFBundleName</key>
@ -24,6 +24,8 @@
<string>1.0</string>
<key>NSMainNibFile</key>
<string></string>
<key>UILaunchStoryboardName</key>
<string>iOS Launch Screen</string>
<key>UISupportedInterfaceOrientations</key>
<array/>
</dict>

View file

@ -0,0 +1,40 @@
<?xml version="1.0" encoding="UTF-8"?>
<document type="com.apple.InterfaceBuilder3.CocoaTouch.Storyboard.XIB" version="3.0" toolsVersion="11201" systemVersion="16A323" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" launchScreen="YES" useTraitCollections="YES" colorMatched="YES" initialViewController="01J-lp-oVM">
<dependencies>
<deployment identifier="iOS"/>
<plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="11161"/>
<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">
<layoutGuides>
<viewControllerLayoutGuide type="top" id="Llm-lL-Icb"/>
<viewControllerLayoutGuide type="bottom" id="xb3-aO-Qok"/>
</layoutGuides>
<view key="view" contentMode="scaleToFill" id="Ze5-6b-2t3">
<rect key="frame" x="0.0" y="0.0" width="375" height="667"/>
<autoresizingMask key="autoresizingMask" widthSizable="YES" heightSizable="YES"/>
<subviews>
<imageView userInteractionEnabled="NO" contentMode="scaleAspectFit" horizontalHuggingPriority="251" verticalHuggingPriority="251" image="Default.png" translatesAutoresizingMaskIntoConstraints="NO" id="VeL-6u-rS3"/>
</subviews>
<color key="backgroundColor" red="1" green="1" blue="1" alpha="1" colorSpace="custom" customColorSpace="sRGB"/>
<constraints>
<constraint firstItem="VeL-6u-rS3" firstAttribute="top" secondItem="Ze5-6b-2t3" secondAttribute="top" id="C5X-Vg-tvO"/>
<constraint firstAttribute="trailing" secondItem="VeL-6u-rS3" secondAttribute="trailing" id="X4i-1U-3JE"/>
<constraint firstItem="VeL-6u-rS3" firstAttribute="bottom" secondItem="xb3-aO-Qok" secondAttribute="top" id="dSu-2l-DcF"/>
<constraint firstItem="VeL-6u-rS3" firstAttribute="leading" secondItem="Ze5-6b-2t3" secondAttribute="leading" id="xKC-uj-bxE"/>
</constraints>
</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="Default.png" width="320" height="480"/>
</resources>
</document>

View file

@ -8,7 +8,6 @@
#include "math.h"
#include "common.h"
#define MILLESECONDS_PER_FRAME 16 /* about 60 frames per second */
#define DAMPING 0.5f; /* after bouncing off a wall, damping coefficient determines final speed */
#define FRICTION 0.0008f /* coefficient of acceleration that opposes direction of motion */
#define GRAVITY_CONSTANT 0.004f /* how sensitive the ship is to the accelerometer */
@ -31,9 +30,9 @@ static SDL_Texture *ship = 0; /* texture for spaceship */
static SDL_Texture *space = 0; /* texture for space (background */
void
render(SDL_Renderer *renderer, int w, int h)
render(SDL_Renderer *renderer, int w, int h, double deltaTime)
{
double deltaMilliseconds = deltaTime * 1000;
float speed;
/* get joystick (accelerometer) axis values and normalize them */
@ -54,10 +53,10 @@ render(SDL_Renderer *renderer, int w, int h)
*/
shipData.vx +=
ax * SDL_IPHONE_MAX_GFORCE / SINT16_MAX * GRAVITY_CONSTANT *
MILLESECONDS_PER_FRAME;
deltaMilliseconds;
shipData.vy +=
ay * SDL_IPHONE_MAX_GFORCE / SINT16_MAX * GRAVITY_CONSTANT *
MILLESECONDS_PER_FRAME;
deltaMilliseconds;
speed = sqrt(shipData.vx * shipData.vx + shipData.vy * shipData.vy);
@ -67,10 +66,10 @@ render(SDL_Renderer *renderer, int w, int h)
float diry = shipData.vy / speed; /* normalized y velocity */
/* update velocity due to friction */
if (speed - FRICTION * MILLESECONDS_PER_FRAME > 0) {
if (speed - FRICTION * deltaMilliseconds > 0) {
/* apply friction */
shipData.vx -= dirx * FRICTION * MILLESECONDS_PER_FRAME;
shipData.vy -= diry * FRICTION * MILLESECONDS_PER_FRAME;
shipData.vx -= dirx * FRICTION * deltaMilliseconds;
shipData.vy -= diry * FRICTION * deltaMilliseconds;
} else {
/* applying friction would MORE than stop the ship, so just stop the ship */
shipData.vx = 0.0f;
@ -79,8 +78,8 @@ render(SDL_Renderer *renderer, int w, int h)
}
/* update ship location */
shipData.x += shipData.vx * MILLESECONDS_PER_FRAME;
shipData.y += shipData.vy * MILLESECONDS_PER_FRAME;
shipData.x += shipData.vx * deltaMilliseconds;
shipData.y += shipData.vy * deltaMilliseconds;
if (shipData.x > maxx) {
shipData.x = maxx;
@ -161,9 +160,6 @@ main(int argc, char *argv[])
SDL_Window *window; /* main window */
SDL_Renderer *renderer;
Uint32 startFrame; /* time frame began to process */
Uint32 endFrame; /* time frame ended processing */
Sint32 delay; /* time to pause waiting to draw next frame */
int done; /* should we clean up and exit? */
int w, h;
@ -173,12 +169,11 @@ main(int argc, char *argv[])
}
/* create main window and renderer */
window = SDL_CreateWindow(NULL, 0, 0, SCREEN_WIDTH, SCREEN_HEIGHT,
SDL_WINDOW_OPENGL |
SDL_WINDOW_FULLSCREEN);
window = SDL_CreateWindow(NULL, 0, 0, 320, 480, SDL_WINDOW_FULLSCREEN | SDL_WINDOW_ALLOW_HIGHDPI);
renderer = SDL_CreateRenderer(window, 0, 0);
SDL_GetWindowSize(window, &w, &h);
SDL_RenderSetLogicalSize(renderer, w, h);
/* print out some info about joysticks and try to open accelerometer for use */
printf("There are %d joysticks available\n", SDL_NumJoysticks());
@ -208,24 +203,15 @@ main(int argc, char *argv[])
done = 0;
/* enter main loop */
while (!done) {
double deltaTime = updateDeltaTime();
SDL_Event event;
startFrame = SDL_GetTicks();
while (SDL_PollEvent(&event)) {
if (event.type == SDL_QUIT) {
done = 1;
}
}
render(renderer, w, h);
endFrame = SDL_GetTicks();
/* figure out how much time we have left, and then sleep */
delay = MILLESECONDS_PER_FRAME - (endFrame - startFrame);
if (delay < 0) {
delay = 0;
} else if (delay > MILLESECONDS_PER_FRAME) {
delay = MILLESECONDS_PER_FRAME;
}
SDL_Delay(delay);
render(renderer, w, h, deltaTime);
SDL_Delay(1);
}
/* delete textures */

View file

@ -32,5 +32,25 @@ void
fatalError(const char *string)
{
printf("%s: %s\n", string, SDL_GetError());
SDL_ShowSimpleMessageBox(SDL_MESSAGEBOX_ERROR, string, SDL_GetError(), NULL);
exit(1);
}
static Uint64 prevTime = 0;
double
updateDeltaTime()
{
Uint64 curTime;
double deltaTime;
if (prevTime == 0) {
prevTime = SDL_GetPerformanceCounter();
}
curTime = SDL_GetPerformanceCounter();
deltaTime = (double) (curTime - prevTime) / (double) SDL_GetPerformanceFrequency();
prevTime = curTime;
return deltaTime;
}

View file

@ -4,14 +4,7 @@
* use however you want
*/
#if __TVOS__
#define SCREEN_WIDTH 1920
#define SCREEN_HEIGHT 1080
#else
#define SCREEN_WIDTH 320
#define SCREEN_HEIGHT 480
#endif
extern int randomInt(int min, int max);
extern float randomFloat(float min, float max);
extern void fatalError(const char *string);
extern double updateDeltaTime();

View file

@ -10,13 +10,13 @@
#include <math.h>
#include <time.h>
#define MILLESECONDS_PER_FRAME 16 /* about 60 frames per second */
#define ACCEL 0.0001f /* acceleration due to gravity, units in pixels per millesecond squared */
#define WIND_RESISTANCE 0.00005f /* acceleration per unit velocity due to wind resistance */
#define MAX_PARTICLES 2000 /* maximum number of particles displayed at once */
static GLuint particleTextureID; /* OpenGL particle texture id */
static SDL_bool pointSizeExtensionSupported; /* is GL_OES_point_size_array supported ? */
static float pointSizeScale;
/*
used to describe what type of particle a given struct particle is.
emitter - this particle flies up, shooting off trail particles, then finally explodes into dust particles.
@ -55,7 +55,7 @@ void initializeParticles(void);
void initializeTexture();
int nextPowerOfTwo(int x);
void drawParticles();
void stepParticles(void);
void stepParticles(double deltaTime);
/* helper function (used in texture loading)
returns next power of two greater than or equal to x
@ -74,8 +74,9 @@ nextPowerOfTwo(int x)
steps each active particle by timestep MILLESECONDS_PER_FRAME
*/
void
stepParticles(void)
stepParticles(double deltaTime)
{
float deltaMilliseconds = deltaTime * 1000;
int i;
struct particle *slot = particles;
struct particle *curr = particles;
@ -93,10 +94,10 @@ stepParticles(void)
curr->isActive = 0;
/* step velocity, then step position */
curr->yvel += ACCEL * MILLESECONDS_PER_FRAME;
curr->yvel += ACCEL * deltaMilliseconds;
curr->xvel += 0.0f;
curr->y += curr->yvel * MILLESECONDS_PER_FRAME;
curr->x += curr->xvel * MILLESECONDS_PER_FRAME;
curr->y += curr->yvel * deltaMilliseconds;
curr->x += curr->xvel * deltaMilliseconds;
/* particle behavior */
if (curr->type == emitter) {
@ -111,29 +112,29 @@ stepParticles(void)
sqrt(curr->xvel * curr->xvel + curr->yvel * curr->yvel);
/* if wind resistance is not powerful enough to stop us completely,
then apply winde resistance, otherwise just stop us completely */
if (WIND_RESISTANCE * MILLESECONDS_PER_FRAME < speed) {
if (WIND_RESISTANCE * deltaMilliseconds < speed) {
float normx = curr->xvel / speed;
float normy = curr->yvel / speed;
curr->xvel -=
normx * WIND_RESISTANCE * MILLESECONDS_PER_FRAME;
normx * WIND_RESISTANCE * deltaMilliseconds;
curr->yvel -=
normy * WIND_RESISTANCE * MILLESECONDS_PER_FRAME;
normy * WIND_RESISTANCE * deltaMilliseconds;
} else {
curr->xvel = curr->yvel = 0; /* stop particle */
}
if (curr->color[3] <= MILLESECONDS_PER_FRAME * 0.1275f) {
if (curr->color[3] <= deltaMilliseconds * 0.1275f) {
/* if this next step will cause us to fade out completely
then just mark for deletion */
curr->isActive = 0;
} else {
/* otherwise, let's fade a bit more */
curr->color[3] -= MILLESECONDS_PER_FRAME * 0.1275f;
curr->color[3] -= deltaMilliseconds * 0.1275f;
}
/* if we're a dust particle, shrink our size */
if (curr->type == dust)
curr->size -= MILLESECONDS_PER_FRAME * 0.010f;
curr->size -= deltaMilliseconds * 0.010f;
}
@ -147,7 +148,7 @@ stepParticles(void)
/* the number of active particles is computed as the difference between
old number of active particles, where slot points, and the
new size of the array, where particles points */
num_active_particles = slot - particles;
num_active_particles = (int) (slot - particles);
}
/*
@ -206,7 +207,7 @@ explodeEmitter(struct particle *emitter)
p->y = emitter->y + emitter->yvel;
p->isActive = 1;
p->type = dust;
p->size = 15;
p->size = 15 * pointSizeScale;
/* inherit emitter's color */
p->color[0] = emitter->color[0];
p->color[1] = emitter->color[1];
@ -244,7 +245,7 @@ spawnTrailFromEmitter(struct particle *emitter)
p->color[3] = (0.7f) * 255;
/* set other attributes */
p->size = 10;
p->size = 10 * pointSizeScale;
p->type = trail;
p->isActive = 1;
@ -298,7 +299,7 @@ spawnEmitterParticle(GLfloat x, GLfloat y)
p->xvel = 0;
p->yvel = -sqrt(2 * ACCEL * (screen_h - y));
/* set other attributes */
p->size = 10;
p->size = 10 * pointSizeScale;
p->type = emitter;
p->isActive = 1;
/* our array has expanded at the end */
@ -363,7 +364,7 @@ main(int argc, char *argv[])
{
SDL_Window *window; /* main window */
SDL_GLContext context;
int w, h;
int drawableW, drawableH;
Uint32 startFrame; /* time frame began to process */
Uint32 endFrame; /* time frame ended processing */
Uint32 delay; /* time to pause waiting to draw next frame */
@ -391,11 +392,19 @@ main(int argc, char *argv[])
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 1);
/* create main window and renderer */
window = SDL_CreateWindow(NULL, 0, 0, SCREEN_WIDTH, SCREEN_HEIGHT,
SDL_WINDOW_OPENGL |
SDL_WINDOW_BORDERLESS);
window = SDL_CreateWindow(NULL, 0, 0, 320, 480,
SDL_WINDOW_OPENGL | SDL_WINDOW_BORDERLESS | SDL_WINDOW_ALLOW_HIGHDPI);
context = SDL_GL_CreateContext(window);
/* The window size and drawable size may be different when highdpi is enabled,
* due to the increased pixel density of the drawable. */
SDL_GetWindowSize(window, &screen_w, &screen_h);
SDL_GL_GetDrawableSize(window, &drawableW, &drawableH);
/* In OpenGL, point sizes are always in pixels. We don't want them looking
* tiny on a retina screen. */
pointSizeScale = (float) drawableH / (float) screen_h;
/* load the particle texture */
initializeTexture();
@ -412,8 +421,7 @@ main(int argc, char *argv[])
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
SDL_GetWindowSize(window, &screen_w, &screen_h);
glViewport(0, 0, screen_w, screen_h);
glViewport(0, 0, drawableW, drawableH);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
@ -436,14 +444,14 @@ main(int argc, char *argv[])
glEnableClientState(GL_POINT_SIZE_ARRAY_OES);
} else {
/* if extension not available then all particles have size 10 */
glPointSize(10);
glPointSize(10 * pointSizeScale);
}
done = 0;
/* enter main loop */
while (!done) {
startFrame = SDL_GetTicks();
SDL_Event event;
double deltaTime = updateDeltaTime();
while (SDL_PollEvent(&event)) {
if (event.type == SDL_QUIT) {
done = 1;
@ -454,19 +462,10 @@ main(int argc, char *argv[])
spawnEmitterParticle(x, y);
}
}
stepParticles();
stepParticles(deltaTime);
drawParticles();
SDL_GL_SwapWindow(window);
endFrame = SDL_GetTicks();
/* figure out how much time we have left, and then sleep */
delay = MILLESECONDS_PER_FRAME - (endFrame - startFrame);
if (delay > MILLESECONDS_PER_FRAME) {
delay = MILLESECONDS_PER_FRAME;
}
if (delay > 0) {
SDL_Delay(delay);
}
SDL_Delay(1);
}
/* delete textures */

View file

@ -8,8 +8,7 @@
#include "common.h"
#define NUM_HAPPY_FACES 100 /* number of faces to draw */
#define MILLESECONDS_PER_FRAME 16 /* about 60 frames per second */
#define HAPPY_FACE_SIZE 32 /* width and height of happyface (pixels) */
#define HAPPY_FACE_SIZE 32 /* width and height of happyface */
static SDL_Texture *texture = 0; /* reference to texture holding happyface */
@ -24,30 +23,37 @@ static struct
units of velocity are pixels per millesecond
*/
void
initializeHappyFaces()
initializeHappyFaces(SDL_Renderer *renderer)
{
int i;
int w;
int h;
SDL_RenderGetLogicalSize(renderer, &w, &h);
for (i = 0; i < NUM_HAPPY_FACES; i++) {
faces[i].x = randomFloat(0.0f, SCREEN_WIDTH - HAPPY_FACE_SIZE);
faces[i].y = randomFloat(0.0f, SCREEN_HEIGHT - HAPPY_FACE_SIZE);
faces[i].xvel = randomFloat(-0.1f, 0.1f);
faces[i].yvel = randomFloat(-0.1f, 0.1f);
faces[i].x = randomFloat(0.0f, w - HAPPY_FACE_SIZE);
faces[i].y = randomFloat(0.0f, h - HAPPY_FACE_SIZE);
faces[i].xvel = randomFloat(-60.0f, 60.0f);
faces[i].yvel = randomFloat(-60.0f, 60.0f);
}
}
void
render(SDL_Renderer *renderer)
render(SDL_Renderer *renderer, double deltaTime)
{
int i;
SDL_Rect srcRect;
SDL_Rect dstRect;
int w;
int h;
SDL_RenderGetLogicalSize(renderer, &w, &h);
/* setup boundaries for happyface bouncing */
Uint16 maxx = SCREEN_WIDTH - HAPPY_FACE_SIZE;
Uint16 maxy = SCREEN_HEIGHT - HAPPY_FACE_SIZE;
Uint16 minx = 0;
Uint16 miny = 0;
int maxx = w - HAPPY_FACE_SIZE;
int maxy = h - HAPPY_FACE_SIZE;
int minx = 0;
int miny = 0;
/* setup rects for drawing */
srcRect.x = 0;
@ -68,8 +74,8 @@ render(SDL_Renderer *renderer)
- draw
*/
for (i = 0; i < NUM_HAPPY_FACES; i++) {
faces[i].x += faces[i].xvel * MILLESECONDS_PER_FRAME;
faces[i].y += faces[i].yvel * MILLESECONDS_PER_FRAME;
faces[i].x += faces[i].xvel * deltaTime;
faces[i].y += faces[i].yvel * deltaTime;
if (faces[i].x > maxx) {
faces[i].x = maxx;
faces[i].xvel = -faces[i].xvel;
@ -123,48 +129,45 @@ initializeTexture(SDL_Renderer *renderer)
int
main(int argc, char *argv[])
{
SDL_Window *window;
SDL_Renderer *renderer;
Uint32 startFrame;
Uint32 endFrame;
Uint32 delay;
int done;
int width;
int height;
/* initialize SDL */
if (SDL_Init(SDL_INIT_VIDEO) < 0) {
fatalError("Could not initialize SDL");
}
window = SDL_CreateWindow(NULL, 0, 0, SCREEN_WIDTH, SCREEN_HEIGHT,
SDL_WINDOW_OPENGL |
SDL_WINDOW_BORDERLESS);
/* The specified window size doesn't matter - except for its aspect ratio,
* which determines whether the window is in portrait or landscape on iOS
* (if SDL_WINDOW_RESIZABLE isn't specified). */
window = SDL_CreateWindow(NULL, 0, 0, 320, 480, SDL_WINDOW_FULLSCREEN | SDL_WINDOW_ALLOW_HIGHDPI);
renderer = SDL_CreateRenderer(window, -1, 0);
SDL_GetWindowSize(window, &width, &height);
SDL_RenderSetLogicalSize(renderer, width, height);
initializeTexture(renderer);
initializeHappyFaces();
initializeHappyFaces(renderer);
/* main loop */
done = 0;
while (!done) {
SDL_Event event;
startFrame = SDL_GetTicks();
double deltaTime = updateDeltaTime();
while (SDL_PollEvent(&event)) {
if (event.type == SDL_QUIT) {
done = 1;
}
}
render(renderer);
endFrame = SDL_GetTicks();
/* figure out how much time we have left, and then sleep */
delay = MILLESECONDS_PER_FRAME - (endFrame - startFrame);
if (delay < 0) {
delay = 0;
} else if (delay > MILLESECONDS_PER_FRAME) {
delay = MILLESECONDS_PER_FRAME;
}
SDL_Delay(delay);
render(renderer, deltaTime);
SDL_Delay(1);
}
/* cleanup */

View file

@ -132,10 +132,13 @@ keyToIndex(SDL_Keysym key)
void
getPositionForCharNumber(int n, int *x, int *y)
{
int renderW, renderH;
SDL_RenderGetLogicalSize(renderer, &renderW, &renderH);
int x_padding = 16; /* padding space on left and right side of screen */
int y_padding = 32; /* padding space at top of screen */
/* figure out the number of characters that can fit horizontally across the screen */
int max_x_chars = (SCREEN_WIDTH - 2 * x_padding) / GLYPH_SIZE_SCREEN;
int max_x_chars = (renderW - 2 * x_padding) / GLYPH_SIZE_SCREEN;
int line_separation = 5; /* pixels between each line */
*x = (n % max_x_chars) * GLYPH_SIZE_SCREEN + x_padding;
*y = (n / max_x_chars) * (GLYPH_SIZE_SCREEN + line_separation) +
@ -228,21 +231,25 @@ loadFont(void)
int
main(int argc, char *argv[])
{
int index; /* index of last key we pushed in the bitmap font */
SDL_Window *window;
SDL_Event event; /* last event received */
SDL_Keymod mod; /* key modifiers of last key we pushed */
SDL_Scancode scancode; /* scancode of last key we pushed */
int width;
int height;
if (SDL_Init(SDL_INIT_VIDEO) < 0) {
printf("Error initializing SDL: %s", SDL_GetError());
}
/* create window */
window = SDL_CreateWindow("iPhone keyboard test", 0, 0, SCREEN_WIDTH, SCREEN_HEIGHT, 0);
window = SDL_CreateWindow("iPhone keyboard test", 0, 0, 320, 480, SDL_WINDOW_ALLOW_HIGHDPI);
/* create renderer */
renderer = SDL_CreateRenderer(window, -1, 0);
SDL_GetWindowSize(window, &width, &height);
SDL_RenderSetLogicalSize(renderer, width, height);
/* load up our font */
loadFont();

View file

@ -33,7 +33,7 @@ static struct sound drums[NUM_DRUMS];
void handleMouseButtonDown(SDL_Event * event);
void handleMouseButtonUp(SDL_Event * event);
int playSound(struct sound *);
void initializeButtons();
void initializeButtons(SDL_Renderer *);
void audioCallback(void *userdata, Uint8 * stream, int len);
void loadSound(const char *file, struct sound *s);
@ -52,19 +52,21 @@ struct
/* sets up the buttons (color, position, state) */
void
initializeButtons()
initializeButtons(SDL_Renderer *renderer)
{
int i;
int spacing = 10; /* gap between drum buttons */
SDL_Rect buttonRect; /* keeps track of where to position drum */
SDL_Color upColor = { 86, 86, 140, 255 }; /* color of drum when not pressed */
SDL_Color downColor = { 191, 191, 221, 255 }; /* color of drum when pressed */
int renderW, renderH;
SDL_RenderGetLogicalSize(renderer, &renderW, &renderH);
buttonRect.x = spacing;
buttonRect.y = spacing;
buttonRect.w = SCREEN_WIDTH - 2 * spacing;
buttonRect.h = (SCREEN_HEIGHT - (NUM_DRUMS + 1) * spacing) / NUM_DRUMS;
buttonRect.w = renderW - 2 * spacing;
buttonRect.h = (renderH - (NUM_DRUMS + 1) * spacing) / NUM_DRUMS;
/* setup each button */
for (i = 0; i < NUM_DRUMS; i++) {
@ -270,24 +272,23 @@ audioCallback(void *userdata, Uint8 * stream, int len)
int
main(int argc, char *argv[])
{
int done; /* has user tried to quit ? */
SDL_Window *window; /* main window */
SDL_Renderer *renderer;
SDL_Event event;
Uint32 startFrame; /* holds when frame started processing */
Uint32 endFrame; /* holds when frame ended processing */
Uint32 delay; /* calculated delay, how long should we wait before next frame? */
int i;
int width;
int height;
if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_AUDIO) < 0) {
fatalError("could not initialize SDL");
}
window =
SDL_CreateWindow(NULL, 0, 0, SCREEN_WIDTH, SCREEN_HEIGHT,
SDL_WINDOW_OPENGL | SDL_WINDOW_BORDERLESS);
window = SDL_CreateWindow(NULL, 0, 0, 320, 480, SDL_WINDOW_BORDERLESS | SDL_WINDOW_ALLOW_HIGHDPI);
renderer = SDL_CreateRenderer(window, 0, 0);
SDL_GetWindowSize(window, &width, &height);
SDL_RenderSetLogicalSize(renderer, width, height);
/* initialize the mixer */
SDL_memset(&mixer, 0, sizeof(mixer));
/* setup output format */
@ -310,12 +311,11 @@ main(int argc, char *argv[])
loadSound("ds_china.wav", &drums[0]);
/* setup positions, colors, and state of buttons */
initializeButtons();
initializeButtons(renderer);
/* enter main loop */
done = 0;
while (!done) {
startFrame = SDL_GetTicks();
while (SDL_PollEvent(&event)) {
switch (event.type) {
case SDL_MOUSEBUTTONDOWN:
@ -330,16 +330,8 @@ main(int argc, char *argv[])
}
}
render(renderer); /* draw buttons */
endFrame = SDL_GetTicks();
/* figure out how much time we have left, and then sleep */
delay = MILLESECONDS_PER_FRAME - (endFrame - startFrame);
if (delay < 0) {
delay = 0;
} else if (delay > MILLESECONDS_PER_FRAME) {
delay = MILLESECONDS_PER_FRAME;
}
SDL_Delay(delay);
SDL_Delay(1);
}
/* cleanup code, let's free up those sound buffers */

View file

@ -11,14 +11,18 @@
void
render(SDL_Renderer *renderer)
{
Uint8 r, g, b;
int renderW;
int renderH;
SDL_RenderGetLogicalSize(renderer, &renderW, &renderH);
/* Come up with a random rectangle */
SDL_Rect rect;
rect.w = randomInt(64, 128);
rect.h = randomInt(64, 128);
rect.x = randomInt(0, SCREEN_WIDTH);
rect.y = randomInt(0, SCREEN_HEIGHT);
rect.x = randomInt(0, renderW);
rect.y = randomInt(0, renderH);
/* Come up with a random color */
r = randomInt(50, 255);
@ -31,7 +35,6 @@ render(SDL_Renderer *renderer)
/* update screen */
SDL_RenderPresent(renderer);
}
int
@ -42,6 +45,8 @@ main(int argc, char *argv[])
SDL_Renderer *renderer;
int done;
SDL_Event event;
int windowW;
int windowH;
/* initialize SDL */
if (SDL_Init(SDL_INIT_VIDEO) < 0) {
@ -52,7 +57,7 @@ main(int argc, char *argv[])
srand(time(NULL));
/* create window and renderer */
window = SDL_CreateWindow(NULL, 0, 0, SCREEN_WIDTH, SCREEN_HEIGHT, 0);
window = SDL_CreateWindow(NULL, 0, 0, 320, 480, SDL_WINDOW_ALLOW_HIGHDPI);
if (window == 0) {
fatalError("Could not initialize Window");
}
@ -61,6 +66,9 @@ main(int argc, char *argv[])
fatalError("Could not create renderer");
}
SDL_GetWindowSize(window, &windowW, &windowH);
SDL_RenderSetLogicalSize(renderer, windowW, windowH);
/* Fill screen with black */
SDL_SetRenderDrawColor(renderer, 0, 0, 0, 255);
SDL_RenderClear(renderer);

View file

@ -82,6 +82,7 @@ main(int argc, char *argv[])
SDL_Window *window; /* main window */
SDL_Renderer *renderer;
int done; /* does user want to quit? */
int w, h;
/* initialize SDL */
if (SDL_Init(SDL_INIT_VIDEO) < 0) {
@ -89,11 +90,12 @@ main(int argc, char *argv[])
}
/* create main window and renderer */
window = SDL_CreateWindow(NULL, 0, 0, SCREEN_WIDTH, SCREEN_HEIGHT,
SDL_WINDOW_OPENGL |
SDL_WINDOW_BORDERLESS);
window = SDL_CreateWindow(NULL, 0, 0, 320, 480, SDL_WINDOW_BORDERLESS | SDL_WINDOW_ALLOW_HIGHDPI);
renderer = SDL_CreateRenderer(window, 0, 0);
SDL_GetWindowSize(window, &w, &h);
SDL_RenderSetLogicalSize(renderer, w, h);
/* load brush texture */
initializeTexture(renderer);