Abstractions and interfaces around units. (Figured out some of the nuance around...
authordsc <david.schoonover@gmail.com>
Mon, 16 May 2011 16:39:13 +0000 (09:39 -0700)
committerdsc <david.schoonover@gmail.com>
Mon, 16 May 2011 16:39:13 +0000 (09:39 -0700)
34 files changed:
src/game/Active.h [moved from src/tanks/Active.h with 100% similarity]
src/game/Displayable.h [new file with mode: 0644]
src/game/Game.h [new file with mode: 0644]
src/game/Game.mm [new file with mode: 0644]
src/game/actor/Actor.h [moved from src/tanks/unit/Actor.h with 53% similarity]
src/game/actor/Actor.mm [new file with mode: 0644]
src/game/actor/Unit.h [new file with mode: 0644]
src/game/actor/Unit.mm [new file with mode: 0644]
src/main.mm
src/physics/World.h
src/physics/debug/GLESDebugDraw.h [moved from src/ui/GLES-Render.h with 97% similarity]
src/physics/debug/GLESDebugDraw.mm [moved from src/ui/GLES-Render.mm with 99% similarity]
src/physics/debug/PhysicsDebugView.h [moved from src/physics/PhysicsDebugView.h with 100% similarity]
src/physics/debug/PhysicsDebugView.mm [moved from src/physics/PhysicsDebugView.mm with 100% similarity]
src/render/QQSparrowExtensions.h [new file with mode: 0644]
src/render/QQSparrowExtensions.mm [new file with mode: 0644]
src/render/animation/AnimationContainer.h
src/tanks/Displayable.h [deleted file]
src/tanks/Game.h [deleted file]
src/tanks/Game.mm [deleted file]
src/tanks/unit/Actor.mm [deleted file]
src/tanks/unit/Unit.h [deleted file]
src/tanks/unit/Unit.mm [deleted file]
src/ui/AppDelegate.h [moved from src/RootAppDelegate.h with 69% similarity]
src/ui/AppDelegate.mm [moved from src/RootAppDelegate.mm with 72% similarity]
src/ui/Viewport.h
src/ui/Viewport.mm
src/ui/iPad/AppDelegate_iPad.h [moved from src/ui/iPad/RootAppDelegate_iPad.h with 56% similarity]
src/ui/iPad/AppDelegate_iPad.mm [moved from src/ui/iPad/RootAppDelegate_iPad.mm with 60% similarity]
src/ui/iPad/en.lproj/MainWindow_iPad.xib
src/ui/iPhone/AppDelegate_iPhone.h [moved from src/ui/iPhone/RootAppDelegate_iPhone.h with 55% similarity]
src/ui/iPhone/AppDelegate_iPhone.mm [moved from src/ui/iPhone/RootAppDelegate_iPhone.mm with 59% similarity]
src/ui/iPhone/en.lproj/MainWindow_iPhone.xib
tanks.xcodeproj/project.pbxproj

similarity index 100%
rename from src/tanks/Active.h
rename to src/game/Active.h
diff --git a/src/game/Displayable.h b/src/game/Displayable.h
new file mode 100644 (file)
index 0000000..852bf95
--- /dev/null
@@ -0,0 +1,8 @@
+#import "Sparrow.h"
+
+
+@protocol Displayable
+
+@property (nonatomic, retain, readonly) SPDisplayObject* shape;
+
+@end
\ No newline at end of file
diff --git a/src/game/Game.h b/src/game/Game.h
new file mode 100644 (file)
index 0000000..7628110
--- /dev/null
@@ -0,0 +1,19 @@
+#import "Sparrow.h"
+
+#import "game/actor/Unit.h"
+#import "physics/World.h"
+
+
+@interface Game : SPStage {
+@private
+    Unit*  _unit;
+    World* _world;
+}
+
+@property (nonatomic, retain) World* world;
+
+- (void) onEnterFrame:(SPEnterFrameEvent*)event;
+
++ (Game*) current;
+
+@end
diff --git a/src/game/Game.mm b/src/game/Game.mm
new file mode 100644 (file)
index 0000000..d010137
--- /dev/null
@@ -0,0 +1,61 @@
+#import "Game.h"
+#import "game/actor/Unit.h"
+
+
+
+static Game* _currentGame = NULL;
+
+
+
+@interface Game ()
+@property (nonatomic, retain) Unit* unit;
+@end
+
+
+@implementation Game
+@synthesize world = _world;
+@synthesize unit  = _unit;
+
+
+- (id) init {
+    if (_currentGame) {
+        [self release];
+        [NSException raise:@"TooManyGames" format:@"cannot instantiate more than one Game at a time!"];
+    }
+    
+    if ( (self = [super init]) ){
+        _currentGame = self;
+        
+        _world = [[World alloc] init];
+        [self addEventListener:@selector(onEnterFrame:) atObject:self forType:SP_EVENT_TYPE_ENTER_FRAME];
+        
+        _unit = [[Unit alloc] init];
+        // [self addEventListener:@selector(onTouch:) atObject:_unit forType:SP_EVENT_TYPE_TOUCH];
+        // if (_unit.shape) [self addChild:_unit.shape];
+    }
+    return self;
+}
+
+- (void) dealloc {
+    // [self setUnit:nil];
+    // [self setWorld:nil];
+    [_unit release];
+    [_world release];
+    _currentGame = NULL;
+    [super dealloc];
+}
+
+- (void) onEnterFrame:(SPEnterFrameEvent*)event {
+    NSLog(@"Time passed since last frame: %f", event.passedTime);
+    // [world step];
+}
+
++ (Game*) current {
+    if (!_currentGame)
+        [[[Game alloc] init]    // init assigns to singleton, but singleton is a weakref,
+            autorelease];       // XXX: so game will still be released if not retained...
+    return _currentGame;
+}
+
+
+@end
\ No newline at end of file
similarity index 53%
rename from src/tanks/unit/Actor.h
rename to src/game/actor/Actor.h
index 335e918..ab2f513 100644 (file)
@@ -1,19 +1,19 @@
-#import "tanks/Active.h"
-#import "tanks/Displayable.h"
-
+#import "game/Active.h"
+#import "game/Displayable.h"
 #import "physics/World.h"
 
+@class Game;
+
 
 @interface Actor : NSObject <Active, Displayable> {
     
 @private
-    BOOL _active;
-    World* _world;
+    BOOL   _active;
 }
 
+@property (nonatomic, readonly) Game*  game;
 @property (nonatomic, readonly) World* world;
 
-- (id) init:(World*)theWorld;
 
 
 @end
diff --git a/src/game/actor/Actor.mm b/src/game/actor/Actor.mm
new file mode 100644 (file)
index 0000000..ba63e62
--- /dev/null
@@ -0,0 +1,18 @@
+#import "Actor.h"
+#import "game/Game.h"
+
+
+@implementation Actor
+
+@synthesize active;
+
+- (Game*)  game  { return Game.current; }
+- (World*) world { return self.game.world; }
+
+- (SPDisplayObject*) shape { return nil; }
+
+- (void) act {
+    // stub
+}
+
+@end
diff --git a/src/game/actor/Unit.h b/src/game/actor/Unit.h
new file mode 100644 (file)
index 0000000..c6650c2
--- /dev/null
@@ -0,0 +1,21 @@
+#import "Sparrow.h"
+#import "render/QQSparrowExtensions.h"
+
+#import "game/actor/Actor.h"
+#import "physics/World.h"
+
+
+@interface Unit : Actor {
+
+@private
+    SPDisplayObject* _shape;
+}
+
+@property (nonatomic, retain, readwrite) SPDisplayObject* shape;
+
+- (id) initWithFile:(NSString*)fileName atX:(float)x y:(float)y;
+- (id) initWithShape:(SPDisplayObject*)aShape;
+
+- (void) onTouch:(SPTouchEvent*)event;
+
+@end
diff --git a/src/game/actor/Unit.mm b/src/game/actor/Unit.mm
new file mode 100644 (file)
index 0000000..f37dd31
--- /dev/null
@@ -0,0 +1,52 @@
+#import "Sparrow.h"
+#import "Unit.h"
+
+
+@implementation Unit
+
+@synthesize shape = _shape;
+
+- (id) init {
+    return [self initWithShape:[[SPQuad quadWithWidth:32 height:32 color:0xff0000] setPositionX:50 y:50]];
+}
+
+- (id) initWithFile:(NSString*)fileName atX:(float)x y:(float)y {
+    return [self initWithShape:[[[[SPImage alloc] initWithContentsOfFile:fileName] autorelease] setPositionX:x y:y]];
+}
+
+- (id) initWithShape:(SPDisplayObject*)aShape {
+    if ((self = [super init])) {
+        self.shape = aShape;
+        [self.game addEventListener:@selector(onTouch:) atObject:self forType:SP_EVENT_TYPE_TOUCH];
+    }
+    return self;
+}
+
+- (void) dealloc {
+    [self.game removeEventListener:@selector(onTouch:) atObject:self forType:SP_EVENT_TYPE_TOUCH];
+    [self.game removeChild:_shape];
+    [_shape release];
+    [super dealloc];
+}
+
+- (void) setShape:(SPDisplayObject*)newShape {
+    if (_shape != newShape) {
+        [self.game removeChild:_shape];
+        [_shape release];
+        _shape = [newShape retain];
+        [self.game addChild:_shape];
+    }
+}
+
+- (void) onTouch:(SPTouchEvent*)event {
+    NSLog(@"%@ onTouch! shape=%@ parent=%@", self, self.shape, self.shape.parent);
+    SPTouch* touch = [[event touchesWithTarget:self.shape.parent] anyObject];
+    if (touch) {
+        SPPoint* touchPosition = [touch locationInSpace:self.shape.parent];
+        self.shape.x = touchPosition.x - self.shape.width / 2.0f;
+        self.shape.y = touchPosition.y - self.shape.height / 2.0f;
+    }
+}
+
+
+@end
index 6b5e5b5..af502d5 100644 (file)
@@ -1,13 +1,6 @@
-//
-//  main.m
-//  tanks
-//
-//  Created by dsc on 4/27/11.
-//  Copyright 2011 lttlst.com. All rights reserved.
-//
-
 #import <UIKit/UIKit.h>
 
+
 int main(int argc, char *argv[])
 {
     NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
index d9d2b56..c0663fa 100644 (file)
@@ -1,5 +1,5 @@
 #include <Box2D/Box2D.h>
-#import "ui/GLES-Render.h"
+#import "physics/debug/GLESDebugDraw.h"
 
 
 @interface World : NSObject {
similarity index 97%
rename from src/ui/GLES-Render.h
rename to src/physics/debug/GLESDebugDraw.h
index 2551666..6131196 100644 (file)
@@ -18,8 +18,8 @@
 * 3. This notice may not be removed or altered from any source distribution.
 */
 
-#ifndef RENDER_H
-#define RENDER_H
+#ifndef GLES_DEBUG_DRAW_H
+#define GLES_DEBUG_DRAW_H
 
 #import <UIKit/UIKit.h>
 #import <OpenGLES/EAGL.h>
similarity index 99%
rename from src/ui/GLES-Render.mm
rename to src/physics/debug/GLESDebugDraw.mm
index abf5c57..ab02a83 100644 (file)
@@ -18,7 +18,7 @@
 * 3. This notice may not be removed or altered from any source distribution.
 */
 
-#include "GLES-Render.h"
+#include "GLESDebugDraw.h"
 
 
 #include <cstdio>
diff --git a/src/render/QQSparrowExtensions.h b/src/render/QQSparrowExtensions.h
new file mode 100644 (file)
index 0000000..076ee38
--- /dev/null
@@ -0,0 +1,9 @@
+#import "SPDisplayObject.h"
+
+
+@interface SPDisplayObject (QQSparrowExtensions)
+
+- (id) setPositionX:(float)x y:(float)y;
+
+@end
+
diff --git a/src/render/QQSparrowExtensions.mm b/src/render/QQSparrowExtensions.mm
new file mode 100644 (file)
index 0000000..8226e98
--- /dev/null
@@ -0,0 +1,12 @@
+#import "QQSparrowExtensions.h"
+
+
+@implementation SPDisplayObject (QQSparrowExtensions)
+
+- (id) setPositionX:(float)x y:(float)y {
+    self.x = x;
+    self.y = y;
+    return self;
+}
+
+@end
\ No newline at end of file
index 8e7c8c3..4b4d16c 100644 (file)
@@ -1,12 +1,3 @@
-//
-//  AnimationContainer.h
-//  tanks
-//
-//  Created by Doris Chen on 5/12/11.
-//  Copyright 2011 __MyCompanyName__. All rights reserved.
-//
-
-#import <Foundation/Foundation.h>
 #import "SPTextureAtlas.h"
 
 ////////////////////////////////////////////////////////////////////////////////////
diff --git a/src/tanks/Displayable.h b/src/tanks/Displayable.h
deleted file mode 100644 (file)
index c99f41c..0000000
+++ /dev/null
@@ -1,8 +0,0 @@
-#import "Sparrow.h"
-
-
-@protocol Displayable
-
-@property (nonatomic, readonly) SPDisplayObject* shape;
-
-@end
\ No newline at end of file
diff --git a/src/tanks/Game.h b/src/tanks/Game.h
deleted file mode 100644 (file)
index 0fd6712..0000000
+++ /dev/null
@@ -1,12 +0,0 @@
-#import "Sparrow.h"
-
-#import "physics/World.h"
-
-
-@interface Game : SPStage
-
-@property (nonatomic, retain) World* world;
-
-- (void) onEnterFrame:(SPEnterFrameEvent*)event;
-
-@end
diff --git a/src/tanks/Game.mm b/src/tanks/Game.mm
deleted file mode 100644 (file)
index aa9a8db..0000000
+++ /dev/null
@@ -1,44 +0,0 @@
-#import "Game.h"
-#import "tanks/unit/Unit.h"
-
-
-@interface Game ()
-
-@property (nonatomic, retain) Unit* unit;
-
-@end
-
-
-@implementation Game
-
-@synthesize unit;
-@synthesize world;
-
-
-- (id) init {
-    if ( (self = [super init]) ){
-        world = [[[World alloc] init] autorelease];
-        [self addEventListener:@selector(onEnterFrame:) atObject:self forType:SP_EVENT_TYPE_ENTER_FRAME];
-        
-        unit = [[[Unit alloc] init:world] autorelease];
-        [self addEventListener:@selector(onTouch:) atObject:unit forType:SP_EVENT_TYPE_TOUCH];
-        [self addChild:unit.quad];
-    }
-    return self;
-}
-
-- (void) dealloc {
-    [self setUnit:nil];
-    [self setWorld:nil];
-    [super dealloc];
-}
-
-- (void) onEnterFrame:(SPEnterFrameEvent*)event {
-    NSLog(@"Time passed since last frame: %f", event.passedTime);
-    // [world step];
-}
-
-
-
-
-@end
\ No newline at end of file
diff --git a/src/tanks/unit/Actor.mm b/src/tanks/unit/Actor.mm
deleted file mode 100644 (file)
index ddb6457..0000000
+++ /dev/null
@@ -1,24 +0,0 @@
-#import "Actor.h"
-
-
-@implementation Actor
-
-@synthesize active;
-@synthesize world = _world;
-
-
-- (id) init:(World*)theWorld {
-    if ((self = [super init])) {
-        _world = theWorld; // weakref: does not inc refcount
-    }
-    return self;
-}
-
-- (SPDisplayObject*) shape {
-    return nil;
-}
-
-
-- (void) act {}
-
-@end
diff --git a/src/tanks/unit/Unit.h b/src/tanks/unit/Unit.h
deleted file mode 100644 (file)
index e5ff05e..0000000
+++ /dev/null
@@ -1,16 +0,0 @@
-#import "Sparrow.h"
-#import "tanks/unit/Actor.h"
-#import "physics/World.h"
-
-
-@interface Unit : Actor {
-}
-
-@property (nonatomic, retain) SPQuad* quad;
-
-- (Unit*) initWithWidth:(float)width height:(float)height X:(float)x Y:(float)y color:(int)color;
-- (Unit*) initWithFile:(NSString*)fileName atX:(float)x andY:(float)y;
-
-- (void) onTouch:(SPTouchEvent*)event;
-
-@end
diff --git a/src/tanks/unit/Unit.mm b/src/tanks/unit/Unit.mm
deleted file mode 100644 (file)
index 469e6b7..0000000
+++ /dev/null
@@ -1,33 +0,0 @@
-#import "Unit.h"
-#import "Sparrow.h"
-
-
-@implementation Unit
-
-@synthesize quad;
-
-- (SPDisplayObject*) shape { return quad; }
-
-
-
-- (Unit*) init:(World*)theWorld {
-    if ((self = [super init:theWorld])) {
-        quad = [SPQuad quadWithWidth:32 height:32];
-        quad.color = 0xff0000;
-        quad.x = 0;
-        quad.y = 0;
-    }
-    return self;
-}
-
-- (void) onTouch:(SPTouchEvent*)event {
-    SPTouch* touch = [[event touchesWithTarget:quad.parent] anyObject];
-    if (touch) {
-        SPPoint* touchPosition = [touch locationInSpace:quad.parent];
-        quad.x = touchPosition.x - quad.width / 2.0f;
-        quad.y = touchPosition.y - quad.height / 2.0f;
-    }
-}
-
-
-@end
similarity index 69%
rename from src/RootAppDelegate.h
rename to src/ui/AppDelegate.h
index 13411b4..7e85122 100644 (file)
@@ -1,12 +1,12 @@
 #import "Sparrow.h"
-#import "Game.h"
+#import "game/Game.h"
 
 
-@interface RootAppDelegate : NSObject <UIApplicationDelegate>
+@interface AppDelegate : NSObject <UIApplicationDelegate>
 {
     UIWindow* window;
     SPView* sparrowView;
-    Game* game;
+    Game* _game;
 }
 
 @property (nonatomic, retain) IBOutlet UIWindow* window;
similarity index 72%
rename from src/RootAppDelegate.mm
rename to src/ui/AppDelegate.mm
index 34b7cd9..78f81dc 100644 (file)
@@ -1,19 +1,19 @@
 //
-//  RootAppDelegate.m
+//  AppDelegate.m
 //  tanks
 //
 //  Created by dsc on 4/27/11.
 //  Copyright 2011 lttlst.com. All rights reserved.
 //
 
-#import "RootAppDelegate.h"
-#import "Game.h"
+#import "AppDelegate.h"
+#import "game/Game.h"
 
-@implementation RootAppDelegate
+@implementation AppDelegate
 
 @synthesize window;
 @synthesize sparrowView;
-@synthesize game;
+@synthesize game = _game;
 
 
 - (void) applicationDidFinishLaunching:(UIApplication*)application {
     [SPAudioEngine start];
     if ( sparrowView.frameRate != 60.0f )
         sparrowView.frameRate = 60.0f;
+    // sparrowView.stage = [[[SPStage alloc] init] autorelease];
     
-    game = [[[Game alloc] init] autorelease];
-    sparrowView.stage = game;
+    _game = [[Game alloc] init];
+    sparrowView.stage = _game;
     
     [window makeKeyAndVisible];
     [sparrowView start];
@@ -42,7 +43,8 @@
 }
 
 - (void) dealloc {
-    [self setGame:nil];
+    // [self setGame:nil];
+    [_game release];
     [sparrowView release];
     [window release];
     [super dealloc];
index 1cd86b8..456b5e2 100644 (file)
@@ -1,4 +1,3 @@
-#include <Box2D/Box2D.h>
 #import "Sparrow.h"
 
 #import "physics/World.h"
index 6bca710..f0c4eac 100644 (file)
@@ -1,10 +1,12 @@
 #import <OpenGLES/EAGL.h>
 #import <OpenGLES/ES1/gl.h>
 #import <OpenGLES/ES1/glext.h>
+
 #import "Sparrow.h"
 
 #import "Viewport.h"
-#import "World.h"
+#import "physics/World.h"
+
 
 @implementation Viewport
 
similarity index 56%
rename from src/ui/iPad/RootAppDelegate_iPad.h
rename to src/ui/iPad/AppDelegate_iPad.h
index 36cc1ab..0fa5aee 100644 (file)
@@ -1,5 +1,5 @@
 //
-//  RootAppDelegate_iPad.h
+//  AppDelegate_iPad.h
 //  tanks
 //
 //  Created by dsc on 4/27/11.
@@ -7,9 +7,9 @@
 //
 
 #import <UIKit/UIKit.h>
-#import "RootAppDelegate.h"
+#import "ui/AppDelegate.h"
 
-@interface RootAppDelegate_iPad : RootAppDelegate {
+@interface AppDelegate_iPad : AppDelegate {
     
 }
 
similarity index 60%
rename from src/ui/iPad/RootAppDelegate_iPad.mm
rename to src/ui/iPad/AppDelegate_iPad.mm
index 75c6d5d..ec4518d 100644 (file)
@@ -1,14 +1,14 @@
 //
-//  RootAppDelegate_iPad.m
+//  AppDelegate_iPad.m
 //  tanks
 //
 //  Created by dsc on 4/27/11.
 //  Copyright 2011 lttlst.com. All rights reserved.
 //
 
-#import "RootAppDelegate_iPad.h"
+#import "AppDelegate_iPad.h"
 
-@implementation RootAppDelegate_iPad
+@implementation AppDelegate_iPad
 
 - (void)dealloc
 {
index 68e46d8..eafdb9b 100644 (file)
                                                <reference key="source" ref="250404236"/>
                                                <reference key="destination" ref="607297697"/>
                                        </object>
-                                       <int key="connectionID">14</int>
+                                       <int key="connectionID">15</int>
                                </object>