Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed card animation #5

Open
wants to merge 11 commits into
base: master
Choose a base branch
from
23 changes: 20 additions & 3 deletions Freecell.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,8 @@
29B97324FDCFA39411CA2CEA /* AppKit.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = AppKit.framework; path = /System/Library/Frameworks/AppKit.framework; sourceTree = "<absolute>"; };
29B97325FDCFA39411CA2CEA /* Foundation.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = Foundation.framework; path = /System/Library/Frameworks/Foundation.framework; sourceTree = "<absolute>"; };
32CA4F630368D1EE00C91783 /* Freecell_Prefix.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = Freecell_Prefix.h; sourceTree = "<group>"; };
E99D5E802971C39F00A14229 /* FreecellDefault.entitlements */ = {isa = PBXFileReference; lastKnownFileType = text.plist.entitlements; path = FreecellDefault.entitlements; sourceTree = "<group>"; };
E99D5E812971C4BF00A14229 /* FreecellDeployment.entitlements */ = {isa = PBXFileReference; lastKnownFileType = text.plist.entitlements; path = FreecellDeployment.entitlements; sourceTree = "<group>"; };
F2101CAA220FA6350091D29B /* Base */ = {isa = PBXFileReference; lastKnownFileType = wrapper.nib; name = Base; path = Base.lproj/MainMenu.nib; sourceTree = "<group>"; };
F21273FF0508ADD60038B809 /* French */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = folder; name = French; path = French.lproj/Help; sourceTree = "<group>"; };
F241643C07C7A06B002C0965 /* bonded.png */ = {isa = PBXFileReference; lastKnownFileType = image.png; name = bonded.png; path = Cards/bonded.png; sourceTree = "<group>"; };
Expand Down Expand Up @@ -186,6 +188,8 @@
29B97314FDCFA39411CA2CEA /* Freecell */ = {
isa = PBXGroup;
children = (
E99D5E812971C4BF00A14229 /* FreecellDeployment.entitlements */,
E99D5E802971C39F00A14229 /* FreecellDefault.entitlements */,
080E96DDFE201D6D7F000001 /* Classes */,
29B97315FDCFA39411CA2CEA /* Other Sources */,
29B97317FDCFA39411CA2CEA /* Resources */,
Expand Down Expand Up @@ -454,6 +458,7 @@
GCC_WARN_UNINITIALIZED_AUTOS = YES;
GCC_WARN_UNUSED_FUNCTION = YES;
GCC_WARN_UNUSED_VARIABLE = YES;
MACOSX_DEPLOYMENT_TARGET = 10.13;
ONLY_ACTIVE_ARCH = YES;
};
name = Development;
Expand Down Expand Up @@ -489,6 +494,7 @@
GCC_WARN_UNINITIALIZED_AUTOS = YES;
GCC_WARN_UNUSED_FUNCTION = YES;
GCC_WARN_UNUSED_VARIABLE = YES;
MACOSX_DEPLOYMENT_TARGET = 10.13;
};
name = Deployment;
};
Expand Down Expand Up @@ -523,6 +529,7 @@
GCC_WARN_UNINITIALIZED_AUTOS = YES;
GCC_WARN_UNUSED_FUNCTION = YES;
GCC_WARN_UNUSED_VARIABLE = YES;
MACOSX_DEPLOYMENT_TARGET = 10.13;
};
name = Default;
};
Expand All @@ -540,10 +547,12 @@
GCC_PRECOMPILE_PREFIX_HEADER = YES;
GCC_PREFIX_HEADER = Freecell_Prefix.h;
GCC_SYMBOLS_PRIVATE_EXTERN = NO;
GCC_TREAT_WARNINGS_AS_ERRORS = YES;
GCC_TREAT_WARNINGS_AS_ERRORS = NO;
HEADER_SEARCH_PATHS = "";
INFOPLIST_FILE = "Info-Freecell.plist";
INFOPLIST_KEY_LSApplicationCategoryType = "public.app-category.puzzle-games";
LIBRARY_SEARCH_PATHS = "";
MACOSX_DEPLOYMENT_TARGET = 10.13;
OTHER_CFLAGS = "";
OTHER_LDFLAGS = "";
PRODUCT_BUNDLE_IDENTIFIER = org.wasters.Freecell;
Expand All @@ -564,17 +573,21 @@
isa = XCBuildConfiguration;
buildSettings = {
CLANG_ENABLE_OBJC_WEAK = YES;
CODE_SIGN_ENTITLEMENTS = FreecellDeployment.entitlements;
COMBINE_HIDPI_IMAGES = YES;
COPY_PHASE_STRIP = YES;
CURRENT_PROJECT_VERSION = 52;
ENABLE_HARDENED_RUNTIME = YES;
FRAMEWORK_SEARCH_PATHS = "";
GCC_PRECOMPILE_PREFIX_HEADER = YES;
GCC_PREFIX_HEADER = Freecell_Prefix.h;
GCC_SYMBOLS_PRIVATE_EXTERN = NO;
GCC_TREAT_WARNINGS_AS_ERRORS = YES;
GCC_TREAT_WARNINGS_AS_ERRORS = NO;
HEADER_SEARCH_PATHS = "";
INFOPLIST_FILE = "Info-Freecell.plist";
INFOPLIST_KEY_LSApplicationCategoryType = "public.app-category.puzzle-games";
LIBRARY_SEARCH_PATHS = "";
MACOSX_DEPLOYMENT_TARGET = 10.13;
OTHER_CFLAGS = "";
OTHER_LDFLAGS = "";
PRODUCT_BUNDLE_IDENTIFIER = org.wasters.Freecell;
Expand All @@ -595,16 +608,20 @@
isa = XCBuildConfiguration;
buildSettings = {
CLANG_ENABLE_OBJC_WEAK = YES;
CODE_SIGN_ENTITLEMENTS = FreecellDefault.entitlements;
COMBINE_HIDPI_IMAGES = YES;
CURRENT_PROJECT_VERSION = 52;
ENABLE_HARDENED_RUNTIME = YES;
FRAMEWORK_SEARCH_PATHS = "";
GCC_PRECOMPILE_PREFIX_HEADER = YES;
GCC_PREFIX_HEADER = Freecell_Prefix.h;
GCC_SYMBOLS_PRIVATE_EXTERN = NO;
GCC_TREAT_WARNINGS_AS_ERRORS = YES;
GCC_TREAT_WARNINGS_AS_ERRORS = NO;
HEADER_SEARCH_PATHS = "";
INFOPLIST_FILE = "Info-Freecell__Upgraded_.plist";
INFOPLIST_KEY_LSApplicationCategoryType = "public.app-category.puzzle-games";
LIBRARY_SEARCH_PATHS = "";
MACOSX_DEPLOYMENT_TARGET = 10.13;
OTHER_CFLAGS = "";
OTHER_LDFLAGS = "";
PRODUCT_BUNDLE_IDENTIFIER = org.wasters.Freecell;
Expand Down
5 changes: 5 additions & 0 deletions FreecellDefault.entitlements
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<?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/>
</plist>
5 changes: 5 additions & 0 deletions FreecellDeployment.entitlements
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<?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/>
</plist>
2 changes: 2 additions & 0 deletions Game.h
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,8 @@
NSDate *startDate;
NSDate *endDate;
BOOL inProgress;
pthread_mutex_t moveMutex;
BOOL moveInProgress;
Result *result;
}

Expand Down
55 changes: 47 additions & 8 deletions Game.m
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
// SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

#include <stdlib.h>
#include <pthread.h>
#include "vccRand.h"
#import "Game.h"
#import "Card.h"
Expand Down Expand Up @@ -66,6 +67,8 @@ @implementation Game
{
view = newView;
controller = newController;
pthread_mutex_init(&moveMutex, NULL);
moveInProgress = NO;

defaults = [NSUserDefaults standardUserDefaults];

Expand All @@ -81,7 +84,8 @@ @implementation Game
[self setStartDate: [NSDate date]];
inProgress = NO;

[view setNeedsDisplay: YES];
//[view setNeedsDisplay: YES];
[view setNeedsDisplaySafely];
}
return self;
}
Expand All @@ -97,6 +101,7 @@ - (void) dealloc
[hint release];
[self setStartDate: nil];
[self setEndDate: nil];
pthread_mutex_destroy(&moveMutex);
[super dealloc];
}

Expand Down Expand Up @@ -208,7 +213,12 @@ - (void) G_attemptMove
}

[self G_setMove: nil];
[view display];
//[view display];
[view performSelectorOnMainThread:@selector(display) withObject:nil waitUntilDone:YES];
struct timespec ts;
ts.tv_sec = 0;
ts.tv_nsec = 5000000;
nanosleep(&ts, NULL);
[self G_moreMoves];
if ([defaults boolForKey: @"gameAutoStack"] == YES)
[self G_autoStack];
Expand Down Expand Up @@ -363,7 +373,8 @@ - (void) undo
[table move: undo];

[controller moveMade];
[view setNeedsDisplay: YES];
//[view setNeedsDisplay: YES];
[view setNeedsDisplaySafely];
}
}

Expand All @@ -378,12 +389,22 @@ - (void) redo
[table move: redo];

[controller moveMade];
[view setNeedsDisplay: YES];
//[view setNeedsDisplay: YES];
[view setNeedsDisplaySafely];
}
}

- (void) clickedTableLocation: (TableLocation *) location
{
pthread_mutex_lock(&moveMutex);
if (moveInProgress) {
pthread_mutex_unlock(&moveMutex);
return;
} else {
moveInProgress = YES;
pthread_mutex_unlock(&moveMutex);
}

// If a move hasn't been started yet, and the location clicked can be
// moved from (it's a free cell or a column), start the move.
if (move == nil)
Expand All @@ -392,24 +413,38 @@ - (void) clickedTableLocation: (TableLocation *) location
&& [table firstCardAtLocation: location] != nil)
{
[self G_setMove: [TableMove moveFromSource: location]];
[view setNeedsDisplay: YES];
//[view setNeedsDisplay: YES];
[view setNeedsDisplaySafely];
}
return;
goto end;
}

// Otherwise, a move has been started, and this is the desired destination.
[move setDestination: location];

[self G_attemptMove];

end:
pthread_mutex_lock(&moveMutex);
moveInProgress = NO;
pthread_mutex_unlock(&moveMutex);
}

- (void) doubleClickedTableLocation: (TableLocation *) source
{
Card *card;
pthread_mutex_lock(&moveMutex);
if (moveInProgress) {
pthread_mutex_unlock(&moveMutex);
return;
} else {
moveInProgress = YES;
pthread_mutex_unlock(&moveMutex);
}

unsigned i;

[self G_setMove: [TableMove moveFromSource: source]];
card = [table firstCardAtLocation: source];
//Card *card = [table firstCardAtLocation: source];

for (i = 0; i < NUMBER_OF_FREE_CELLS; i++)
{
Expand All @@ -423,6 +458,10 @@ - (void) doubleClickedTableLocation: (TableLocation *) source
}

[self G_attemptMove];

pthread_mutex_lock(&moveMutex);
moveInProgress = NO;
pthread_mutex_unlock(&moveMutex);
}

- (void) setHint
Expand Down
20 changes: 15 additions & 5 deletions GameController.m
Original file line number Diff line number Diff line change
Expand Up @@ -166,12 +166,15 @@ - (IBAction) showHint: (id) sender
if ([game hint])
{
[view display];
sleep(1);
[game setHint: nil];
[view display];
[self performSelector:@selector(hintDone) withObject:nil afterDelay:1];
}
}

- (void) hintDone {
[game setHint: nil];
[view display];
}

- (IBAction) undo: (id) sender
{
[game undo];
Expand Down Expand Up @@ -309,7 +312,10 @@ - (void) updateTime: (NSTimer *) sender
currentDuration, NSLocalizedString(@"bestIs", "best is"), shortestDuration]];
}

- (void) moveMade
- (void) moveMade {
[self performSelectorOnMainThread:@selector(moveMade2) withObject:nil waitUntilDone:NO];
}
- (void) moveMade2
{
NSUInteger currentMoves = [game moves];
NSUInteger shortestMoves = [history shortestMoves];
Expand Down Expand Up @@ -355,7 +361,11 @@ - (void) setGame: (Game *) newGame
game = [newGame retain];
}

- (void) gameOver
- (void) gameOver {
[self performSelectorOnMainThread:@selector(gameOver2) withObject:nil waitUntilDone:YES];
}

- (void) gameOver2
{
NSString *title, *defaultButton, *alternateButton, *message;
SEL selector;
Expand Down
5 changes: 5 additions & 0 deletions GameView.h
Original file line number Diff line number Diff line change
Expand Up @@ -57,4 +57,9 @@
- (void) setCardView: (CardView *) newCardView;
- (void) setBackgroundColour: (NSColor *) colour;

// Thread-safe repainting
//

- (void) setNeedsDisplaySafely;

@end
23 changes: 18 additions & 5 deletions GameView.m
Original file line number Diff line number Diff line change
Expand Up @@ -187,12 +187,22 @@ - (void) mouseDown: (NSEvent *) event
// quad-click to be understood as two double-clicks in quick succession,
// which makes perfect UI sense in this case.
if (([event clickCount] % 2) == 0)
[game doubleClickedTableLocation: location];
[game performSelectorInBackground:@selector(doubleClickedTableLocation:) withObject:location];
//[game doubleClickedTableLocation: location];
else
[game clickedTableLocation: location];
[game performSelectorInBackground:@selector(clickedTableLocation:) withObject:location];
//[game clickedTableLocation: location];
}
}

- (void) setNeedsDisplaySafely {
[self performSelectorOnMainThread:@selector(setNeedsDisplaySafely2) withObject:nil waitUntilDone:NO];
}

- (void) setNeedsDisplaySafely2 {
[self setNeedsDisplay:YES];
}

// Mutators
//

Expand All @@ -201,7 +211,8 @@ - (void) setGame: (Game *) newGame
[game release];
game = [newGame retain];
table = [game table];
[self setNeedsDisplay: YES];
//[self setNeedsDisplay: YES];
[self setNeedsDisplaySafely];
}

- (void) setController: (GameController *) newController;
Expand Down Expand Up @@ -231,15 +242,17 @@ - (void) setCardView: (CardView *) newCardView
edgeMargin + (cardHeight + margin) * 2 + smallOverlap * 18 + edgeMargin);

[controller setWindowSize: NSMakeSize(size.width, size.height + 22)];
[self setNeedsDisplay: YES];
//[self setNeedsDisplay: YES];
[self setNeedsDisplaySafely];
}

- (void) setBackgroundColour: (NSColor *) colour
{
[colour retain];
[backgroundColour release];
backgroundColour = colour;
[self setNeedsDisplay: YES];
//[self setNeedsDisplay: YES];
[self setNeedsDisplaySafely];
}

@end
Binary file added screenshot.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.