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 !
* The recomanded way is to use PIMP idiom.
* The recommended way is to use PIMP idiom.
*
* It's elegant. Moreover, we do no constrain
* other file including this one to be Obj-C++.
@ -36,7 +36,8 @@
struct SFMLmainWindow;
@interface CocoaAppDelegate : NSObject <NSApplicationDelegate> {
@interface CocoaAppDelegate : NSObject <NSApplicationDelegate>
{
@private
NSWindow* m_window;
NSView* m_sfmlView;

View File

@ -34,14 +34,13 @@
// Our PIMPL
struct SFMLmainWindow
{
SFMLmainWindow(sf::WindowHandle win)
: renderWindow(win)
, background(sf::Color::Blue)
SFMLmainWindow(sf::WindowHandle win) :
renderWindow(win),
background(sf::Color::Blue)
{
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");
}
logo.setSmooth(true);
@ -55,9 +54,8 @@ struct SFMLmainWindow
unsigned int wh = renderWindow.getSize().y;
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");
}
text.setColor(sf::Color::White);
text.setFont(font);
@ -98,11 +96,12 @@ struct SFMLmainWindow
@synthesize initialized = m_initialized;
- (id)init {
- (id)init
{
self = [super init];
if (self) {
if (self)
self.initialized = NO;
}
return self;
}
@ -163,22 +162,17 @@ struct SFMLmainWindow
// Scaling
/* /!\ we do this at 60fps so choose low scaling factor! /!\ */
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Up))
{
self.mainWindow->sprite.scale(1.01f, 1.01f);
}
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Down))
{
self.mainWindow->sprite.scale(0.99f, 0.99f);
}
// Clear the window, display some stuff and display it into our view.
self.mainWindow->renderWindow.clear(self.mainWindow->background);
if (self.visible)
{
self.mainWindow->renderWindow.draw(self.mainWindow->sprite);
}
self.mainWindow->renderWindow.draw(self.mainWindow->text);
@ -192,19 +186,13 @@ struct SFMLmainWindow
// Convert title to color
NSString* color = [[sender selectedItem] title];
if ([color isEqualToString:BLUE])
{
self.mainWindow->background = sf::Color::Blue;
}
else if ([color isEqualToString:GREEN])
{
self.mainWindow->background = sf::Color::Green;
}
else
{
self.mainWindow->background = sf::Color::Red;
}
}
}
-(IBAction)rotationChanged:(NSSlider*)sender
{

View File

@ -28,9 +28,9 @@
@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;

View File

@ -28,7 +28,7 @@
@implementation NSString (NSString_stdstring)
+(id)stringWithstdstring:(std::string const &)string
+(id)stringWithstdstring:(const std::string&)string
{
std::string utf8;
utf8.reserve(string.size() + 1);
@ -40,24 +40,7 @@
return str;
}
-(std::string)tostdstring
{
// Not sure about the encoding to use. Using [self UTF8String] doesn't
// work for characters like é or à.
const char *cstr = [self cStringUsingEncoding:NSISOLatin1StringEncoding];
if (cstr != NULL)
{
std::string str(cstr);
return str;
}
else
{
return "";
}
}
+(id)stringWithstdwstring:(std::wstring const &)string
+(id)stringWithstdwstring:(const std::wstring&)string
{
char* data = (char*)string.data();
unsigned size = string.size() * sizeof(wchar_t);
@ -67,6 +50,18 @@
return str;
}
-(std::string)tostdstring
{
// Not sure about the encoding to use. Using [self UTF8String] doesn't
// work for characters like é or à.
const char *cstr = [self cStringUsingEncoding:NSISOLatin1StringEncoding];
if (cstr != NULL)
return std::string(cstr);
else
return "";
}
-(std::wstring)tostdwstring
{
// According to wikipedia, Mac OS X is Little Endian on x86 and x86-64