Improved style of Cocoa example

This commit is contained in:
Marco Antognini 2014-05-17 13:48:44 +02:00
parent 6a5391c0b6
commit 6e01d1fde7
4 changed files with 61 additions and 77 deletions

View File

@ -28,7 +28,7 @@
/* /*
* NB : We need pointers for C++ objects fields in Obj-C interface ! * NB : We need pointers for C++ objects fields in Obj-C interface !
* The recomanded way is to use PIMP idiom. * The recommended way is to use PIMP idiom.
* *
* It's elegant. Moreover, we do no constrain * It's elegant. Moreover, we do no constrain
* other file including this one to be Obj-C++. * other file including this one to be Obj-C++.
@ -36,26 +36,27 @@
struct SFMLmainWindow; struct SFMLmainWindow;
@interface CocoaAppDelegate : NSObject <NSApplicationDelegate> { @interface CocoaAppDelegate : NSObject <NSApplicationDelegate>
{
@private @private
NSWindow *m_window; NSWindow* m_window;
NSView *m_sfmlView; NSView* m_sfmlView;
NSTextField *m_textField; NSTextField* m_textField;
SFMLmainWindow *m_mainWindow; SFMLmainWindow* m_mainWindow;
NSTimer *m_renderTimer; NSTimer* m_renderTimer;
BOOL m_visible; BOOL m_visible;
BOOL m_initialized; BOOL m_initialized;
} }
@property (retain) IBOutlet NSWindow *window; @property (retain) IBOutlet NSWindow* window;
@property (assign) IBOutlet NSView *sfmlView; @property (assign) IBOutlet NSView* sfmlView;
@property (assign) IBOutlet NSTextField *textField; @property (assign) IBOutlet NSTextField* textField;
-(IBAction)colorChanged:(NSPopUpButton *)sender; -(IBAction)colorChanged:(NSPopUpButton*)sender;
-(IBAction)rotationChanged:(NSSlider *)sender; -(IBAction)rotationChanged:(NSSlider*)sender;
-(IBAction)visibleChanged:(NSButton *)sender; -(IBAction)visibleChanged:(NSButton*)sender;
-(IBAction)textChanged:(NSTextField *)sender; -(IBAction)textChanged:(NSTextField*)sender;
-(IBAction)updateText:(NSButton *)sender; -(IBAction)updateText:(NSButton*)sender;
@end @end
@ -65,6 +66,6 @@ struct SFMLmainWindow;
*/ */
@interface SilentWindow : NSWindow @interface SilentWindow : NSWindow
-(void)keyDown:(NSEvent *)theEvent; -(void)keyDown:(NSEvent*)theEvent;
@end @end

View File

@ -34,14 +34,13 @@
// Our PIMPL // Our PIMPL
struct SFMLmainWindow struct SFMLmainWindow
{ {
SFMLmainWindow(sf::WindowHandle win) SFMLmainWindow(sf::WindowHandle win) :
: renderWindow(win) renderWindow(win),
, background(sf::Color::Blue) background(sf::Color::Blue)
{ {
std::string resPath = [[[NSBundle mainBundle] resourcePath] tostdstring]; std::string resPath = [[[NSBundle mainBundle] resourcePath] tostdstring];
if (!logo.loadFromFile(resPath + "/logo.png")) { if (!logo.loadFromFile(resPath + "/logo.png"))
NSLog(@"Couldn't load the logo image"); NSLog(@"Couldn't load the logo image");
}
logo.setSmooth(true); logo.setSmooth(true);
@ -55,9 +54,8 @@ struct SFMLmainWindow
unsigned int wh = renderWindow.getSize().y; unsigned int wh = renderWindow.getSize().y;
sprite.setPosition(sf::Vector2f(ww, wh) / 2.f); sprite.setPosition(sf::Vector2f(ww, wh) / 2.f);
if (!font.loadFromFile(resPath + "/sansation.ttf")) { if (!font.loadFromFile(resPath + "/sansation.ttf"))
NSLog(@"Couldn't load the font"); NSLog(@"Couldn't load the font");
}
text.setColor(sf::Color::White); text.setColor(sf::Color::White);
text.setFont(font); text.setFont(font);
@ -74,13 +72,13 @@ struct SFMLmainWindow
// Private stuff // Private stuff
@interface CocoaAppDelegate () @interface CocoaAppDelegate ()
@property (assign) SFMLmainWindow *mainWindow; @property (assign) SFMLmainWindow* mainWindow;
@property (retain) NSTimer *renderTimer; @property (retain) NSTimer* renderTimer;
@property (assign) BOOL visible; @property (assign) BOOL visible;
@property (assign) BOOL initialized; @property (assign) BOOL initialized;
-(void)renderMainWindow:(NSTimer *)aTimer; -(void)renderMainWindow:(NSTimer*)aTimer;
@end @end
@ -98,15 +96,16 @@ struct SFMLmainWindow
@synthesize initialized = m_initialized; @synthesize initialized = m_initialized;
- (id)init { - (id)init
{
self = [super init]; self = [super init];
if (self) { if (self)
self.initialized = NO; self.initialized = NO;
}
return self; return self;
} }
-(void)applicationDidFinishLaunching:(NSNotification *)aNotification -(void)applicationDidFinishLaunching:(NSNotification*)aNotification
{ {
(void)aNotification; (void)aNotification;
@ -149,64 +148,53 @@ struct SFMLmainWindow
self.sfmlView = nil; self.sfmlView = nil;
self.textField = nil; self.textField = nil;
delete (SFMLmainWindow *) self.mainWindow; delete (SFMLmainWindow*) self.mainWindow;
self.mainWindow = 0; self.mainWindow = 0;
self.renderTimer = nil; self.renderTimer = nil;
[super dealloc]; [super dealloc];
} }
-(void)renderMainWindow:(NSTimer *)aTimer -(void)renderMainWindow:(NSTimer*)aTimer
{ {
(void)aTimer; (void)aTimer;
// Scaling // Scaling
/* /!\ we do this at 60fps so choose low scaling factor! /!\ */ /* /!\ we do this at 60fps so choose low scaling factor! /!\ */
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Up)) if (sf::Keyboard::isKeyPressed(sf::Keyboard::Up))
{
self.mainWindow->sprite.scale(1.01f, 1.01f); self.mainWindow->sprite.scale(1.01f, 1.01f);
}
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Down)) if (sf::Keyboard::isKeyPressed(sf::Keyboard::Down))
{
self.mainWindow->sprite.scale(0.99f, 0.99f); self.mainWindow->sprite.scale(0.99f, 0.99f);
}
// Clear the window, display some stuff and display it into our view. // Clear the window, display some stuff and display it into our view.
self.mainWindow->renderWindow.clear(self.mainWindow->background); self.mainWindow->renderWindow.clear(self.mainWindow->background);
if (self.visible) if (self.visible)
{
self.mainWindow->renderWindow.draw(self.mainWindow->sprite); self.mainWindow->renderWindow.draw(self.mainWindow->sprite);
}
self.mainWindow->renderWindow.draw(self.mainWindow->text); self.mainWindow->renderWindow.draw(self.mainWindow->text);
self.mainWindow->renderWindow.display(); self.mainWindow->renderWindow.display();
} }
-(IBAction)colorChanged:(NSPopUpButton *)sender -(IBAction)colorChanged:(NSPopUpButton*)sender
{ {
if (self.initialized) if (self.initialized)
{ {
// Convert title to color // Convert title to color
NSString *color = [[sender selectedItem] title]; NSString* color = [[sender selectedItem] title];
if ([color isEqualToString:BLUE]) if ([color isEqualToString:BLUE])
{
self.mainWindow->background = sf::Color::Blue; self.mainWindow->background = sf::Color::Blue;
}
else if ([color isEqualToString:GREEN]) else if ([color isEqualToString:GREEN])
{
self.mainWindow->background = sf::Color::Green; self.mainWindow->background = sf::Color::Green;
}
else else
{
self.mainWindow->background = sf::Color::Red; self.mainWindow->background = sf::Color::Red;
}
} }
} }
-(IBAction)rotationChanged:(NSSlider *)sender -(IBAction)rotationChanged:(NSSlider*)sender
{ {
if (self.initialized) if (self.initialized)
{ {
@ -215,23 +203,23 @@ struct SFMLmainWindow
} }
} }
-(IBAction)visibleChanged:(NSButton *)sender -(IBAction)visibleChanged:(NSButton*)sender
{ {
if (self.initialized) if (self.initialized)
self.visible = [sender state] == NSOnState; self.visible = [sender state] == NSOnState;
} }
-(IBAction)textChanged:(NSTextField *)sender -(IBAction)textChanged:(NSTextField*)sender
{ {
if (self.initialized) if (self.initialized)
self.mainWindow->text.setString([[sender stringValue] tostdwstring]); self.mainWindow->text.setString([[sender stringValue] tostdwstring]);
} }
- (IBAction)updateText:(NSButton *)sender - (IBAction)updateText:(NSButton*)sender
{ {
(void)sender; (void)sender;
// Simply simulate textChanged : // Simply simulate textChanged:
[self textChanged:self.textField]; [self textChanged:self.textField];
} }
@ -239,7 +227,7 @@ struct SFMLmainWindow
@implementation SilentWindow @implementation SilentWindow
-(void)keyDown:(NSEvent *)theEvent -(void)keyDown:(NSEvent*)theEvent
{ {
(void)theEvent; (void)theEvent;
// Do nothing except preventing this alert. // Do nothing except preventing this alert.

View File

@ -28,9 +28,9 @@
@interface NSString (NSString_stdstring) @interface NSString (NSString_stdstring)
+(id)stringWithstdstring:(std::string const &)string; +(id)stringWithstdstring:(const std::string&)string;
+(id)stringWithstdwstring:(std::wstring const &)string; +(id)stringWithstdwstring:(const std::wstring&)string;
-(std::string)tostdstring; -(std::string)tostdstring;

View File

@ -28,18 +28,28 @@
@implementation NSString (NSString_stdstring) @implementation NSString (NSString_stdstring)
+(id)stringWithstdstring:(std::string const &)string +(id)stringWithstdstring:(const std::string&)string
{ {
std::string utf8; std::string utf8;
utf8.reserve(string.size() + 1); utf8.reserve(string.size() + 1);
sf::Utf8::fromAnsi(string.begin(), string.end(), std::back_inserter(utf8)); sf::Utf8::fromAnsi(string.begin(), string.end(), std::back_inserter(utf8));
NSString *str = [NSString stringWithCString:utf8.c_str() NSString* str = [NSString stringWithCString:utf8.c_str()
encoding:NSUTF8StringEncoding]; encoding:NSUTF8StringEncoding];
return str; return str;
} }
+(id)stringWithstdwstring:(const std::wstring&)string
{
char* data = (char*)string.data();
unsigned size = string.size() * sizeof(wchar_t);
NSString* str = [[[NSString alloc] initWithBytes:data length:size
encoding:NSUTF32LittleEndianStringEncoding] autorelease];
return str;
}
-(std::string)tostdstring -(std::string)tostdstring
{ {
// Not sure about the encoding to use. Using [self UTF8String] doesn't // Not sure about the encoding to use. Using [self UTF8String] doesn't
@ -47,24 +57,9 @@
const char *cstr = [self cStringUsingEncoding:NSISOLatin1StringEncoding]; const char *cstr = [self cStringUsingEncoding:NSISOLatin1StringEncoding];
if (cstr != NULL) if (cstr != NULL)
{ return std::string(cstr);
std::string str(cstr);
return str;
}
else else
{
return ""; return "";
}
}
+(id)stringWithstdwstring:(std::wstring const &)string
{
char* data = (char *)string.data();
unsigned size = string.size() * sizeof(wchar_t);
NSString *str = [[[NSString alloc] initWithBytes:data length:size
encoding:NSUTF32LittleEndianStringEncoding] autorelease];
return str;
} }
-(std::wstring)tostdwstring -(std::wstring)tostdwstring
@ -72,7 +67,7 @@
// According to wikipedia, Mac OS X is Little Endian on x86 and x86-64 // According to wikipedia, Mac OS X is Little Endian on x86 and x86-64
// http://en.wikipedia.org/wiki/Endianness // http://en.wikipedia.org/wiki/Endianness
NSData* asData = [self dataUsingEncoding:NSUTF32LittleEndianStringEncoding]; NSData* asData = [self dataUsingEncoding:NSUTF32LittleEndianStringEncoding];
return std::wstring((wchar_t *)[asData bytes], [asData length] / sizeof(wchar_t)); return std::wstring((wchar_t*)[asData bytes], [asData length] / sizeof(wchar_t));
} }
@end @end