Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: iPhone Port (in progress)  (Read 44234 times)

0 Members and 1 Guest are viewing this topic.

seoushi

  • Newbie
  • *
  • Posts: 15
    • View Profile
iPhone Port (in progress)
« on: January 21, 2009, 11:43:51 pm »
As I've hinted in another thread I've been working on getting SFML on the iPhone but I need some feedback on design decisions due to the nature of the device.

So far the following has been ported over complete but largely untested
SFML-System
SFML-Audio (minus libsnd)
SFML-Network

I'm currently working on SFML-Window and it is partially working. You can create a window, draw with OpenGLES and swap the buffers, here is the sample code that works.

Code: [Select]
/*
 *  test.cpp
 *  SFML-iPhone
 *
 *  Created by Sean Chapel on 1/20/09.
 *
 */



#include <SFML/Window.hpp>
#include <SFML/Window/UIKit/UserMain.h>



int main(int argc, char** argv)
{

    sf::Window App(sf::VideoMode(320,480,32), "title");
    sf::Clock Clock;



const GLfloat squareVertices[] =
{
-0.5f, -0.5f,
0.5f,  -0.5f,
-0.5f,  0.5f,
0.5f,   0.5f,
};

const GLubyte squareColors[] =
{
255, 255,   0, 255,
0,   255, 255, 255,
0,     0,   0,   0,
255,   0, 255, 255,
};


    while (App.IsOpened())
    {
App.SetActive(true);

        sf::Event Event;
while (App.GetEvent(Event))
{
}

glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrthof(-1.0f, 1.0f, -1.5f, 1.5f, -1.0f, 1.0f);
glMatrixMode(GL_MODELVIEW);

glRotatef(3.0f, 0.0f, 0.0f, 1.0f);

glClearColor(0.5f, 0.5f, 0.5f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT);

glVertexPointer(2, GL_FLOAT, 0, squareVertices);
glEnableClientState(GL_VERTEX_ARRAY);
glColorPointer(4, GL_UNSIGNED_BYTE, 0, squareColors);
glEnableClientState(GL_COLOR_ARRAY);

glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);

App.Display();
}

    return 0;
}


The only real difference is that I needed to include "SFML/Window/UIKit/UserMain.h". The reason for this is that inorder to run a UIWindow application it requires that the UIWindow handles the main loop, thus taking over control. I really hate that it does this so I used a hack like what SDL does and it redefines main to UserMain and when the UIWindow starts it calls UserMain, it works but it's not ideal.


So the next thing I want to get done is input. Touching the screen acts like a mouse, you have a press/release/drag and it has x,y coordinates however you can have multiple touches at once and SFML doesn't look like it support multiple mice. A solution would to use a joystick however it seems a bit odd.

The other thing the iPhone has is the accelerometer, which can act like a joysticks analog stick movement but it has no buttons.

Given the touch input and the  accelerometer, where do you think these two things fit the best? I would like to avoid creating new devices for portability reasons (not everyone has a mac to develop with and developing iPhone games on linux and window seems appealing to my company).

Right now my idea is to make joystick 1 the accelerometer and joysticks 2-? the multi-touch inputs. I'm not sure how many touches you can do at once tho, I initially thought it was only two but I've heard some people talk about handling more.

The last thing is that the iPhone doesn't have a keyboard in the normal sense. It has the onscreen one and it does work properly over OpenGLES however it doesn't mesh well with games. I don't plan to support the built-in keyboard, it's easy enough to make your own and there is a lot of other work to be done however if enough people want it I will consider it.


I will be releasing this code and hopefully it can be included in SFML someday but until I get the SFML-Window lib ported over properly I don't see a reason to release the code.


Any thoughts and suggestions would be great.

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
iPhone Port (in progress)
« Reply #1 on: January 22, 2009, 07:59:50 am »
Hi

That's really amazing, I'm excited to see an iPhone port :)

I think the support for touchscreen and accelerometer should be added, those kind of interfaces will be widely used in the future. I'll have to think more about it.

If you need some help from me, you can send me an e-mail so that we can discuss more about the technical details.
Laurent Gomila - SFML developer

seoushi

  • Newbie
  • *
  • Posts: 15
    • View Profile
iPhone Port (in progress)
« Reply #2 on: January 22, 2009, 09:50:52 pm »
I went ahead and added some event types (Accelerometer and Touched) and wrote the implementation behind them. Here is some example code of it working.

Code: [Select]
/*
 *  test.cpp
 *  SFML-iPhone
 *
 *  Created by Sean Chapel on 1/20/09.
 *
 */



#include <SFML/Window.hpp>
#include <SFML/Window/UIKit/UserMain.h>



int main(int argc, char** argv)
{

    sf::Window App(sf::VideoMode(320,480,32), "title");
    sf::Clock Clock;



const GLfloat squareVertices[] =
{
-0.5f, -0.5f,
0.5f,  -0.5f,
-0.5f,  0.5f,
0.5f,   0.5f,
};

const GLubyte squareColors[] =
{
255, 255,   0, 255,
0,   255, 255, 255,
0,     0,   0,   0,
255,   0, 255, 255,
};


    while (App.IsOpened())
    {
App.SetActive(true);

        sf::Event Event;
while (App.GetEvent(Event))
{
if(Event.Type == sf::Event::Accelerometer)
{
printf("%f,%f,%f \n", Event.Acceleration.X, Event.Acceleration.Y, Event.Acceleration.Z);
}
else if(Event.Type == sf::Event::Touched)
{
switch (Event.Touch.Type)
{
case sf::Event::TouchEvent::Began:
printf("Started touch %d at %f,%f \n", Event.Touch.TouchId, Event.Touch.X, Event.Touch.Y);
break;
case sf::Event::TouchEvent::Ended:
printf("Ended touch %d at %f,%f \n", Event.Touch.TouchId, Event.Touch.X, Event.Touch.Y);
break;
case sf::Event::TouchEvent::Canceled:
printf("Canceled touch %d at %f,%f \n", Event.Touch.TouchId, Event.Touch.X, Event.Touch.Y);
break;
case sf::Event::TouchEvent::Moved:
printf("Moved touch %d at %f,%f \n", Event.Touch.TouchId, Event.Touch.X, Event.Touch.Y);
break;
default:
break;
}
}
}


glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrthof(-1.0f, 1.0f, -1.5f, 1.5f, -1.0f, 1.0f);
glMatrixMode(GL_MODELVIEW);

glRotatef(3.0f, 0.0f, 0.0f, 1.0f);

glClearColor(0.5f, 0.5f, 0.5f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT);

glVertexPointer(2, GL_FLOAT, 0, squareVertices);
glEnableClientState(GL_VERTEX_ARRAY);
glColorPointer(4, GL_UNSIGNED_BYTE, 0, squareColors);
glEnableClientState(GL_COLOR_ARRAY);

glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);

App.Display();
}

    return 0;
}



Files I've had to change are as follows :
Window/Input.h
Window/Input.cpp
Window/Event.hpp


I've added the following files under Window/UIKit :
EAGLView.h
EAGLView.mm
UIKitAppDelegate.h
UIKitAppDelegate.mm
VideoModeSupport.cpp
VideoModeSupport.hpp
WindowImplUIKit.mm
WindowImplUIKit.hpp
UserMain.h

As promised I am releasing the source code/project since the SFML-Window is mostly working. http://seoushi.com/dev/SFML-iPhone.tar.bz2

The archive contains a project to build a static lib and a simple test program.

The only bug I know of at the moment is touch move events sometimes don't seem to spawn until a touch is released, I'm unsure what is causing this perhaps it doesn't like the way I'm polling for events. I might have to make the poll events threaded.


Anyways I'm going to see if I can't fix that bug then start on the graphics package port.

@Laurent - if you could comment on my code that would be great. Basically I just want to know if I did something odd in my port that doesn't fit in well with SFML.

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
iPhone Port (in progress)
« Reply #3 on: January 22, 2009, 10:36:55 pm »
Everything looks fine so far :)

Just a few questions:
- What is a "touch cancelled" event?
- What is the accelerometer?
- Why are you using a framebuffer object?

Regarding the public API changes, you shouldn't modify too much stuff (except for your own tests), as I'll change it anyway ;)

Porting the graphics package may be a hard job (or not). If iPhone uses OpenGL ES 1.x it should be ok, but if it's using OpenGL ES 2.0 you'll basically need to abstract the whole rendering pipeline (2.0 is purely based on shaders, there's no FFP).
Laurent Gomila - SFML developer

seoushi

  • Newbie
  • *
  • Posts: 15
    • View Profile
iPhone Port (in progress)
« Reply #4 on: January 22, 2009, 11:10:36 pm »
The touch canceled event is when the iPhone for whatever reason decides to stop the touch event, I could combine touch ended and touch canceled as they are almost the same thing.

The accelerometer is sorta like a gyro, it tells how much the phone has accelerated in the certain direction when moved.

As for the framebuffer, that is the only way to get an opengl context on the iPhone. Basically you create a framebuffer and then do all the opengl es commands you want then render the opengl to a framebuffer and tell the UIView that a new framebuffer is up to render.


Also, I've looked more into the touch events and it turns out even SDL 1.3 has this issue because if you interrupt the main application loop bad things happen. I have a solution where basically in the int Main you register an Update callback function and the main loop from the main application calls it however it is a bit different from how a normal SFML application runs.

The iPhone uses OpenGL ES-CM 1.1, so the graphics package shouldn't be too bad to port over.

If you have a few moments, I'll like to talk over IM or IRC about some of these changes (Im PM'ed you my contact information).



edit:
I've uploaded a newer version which fixes the Touch Inputs however users are no longer in control of the main loop, unfortunately this is the only fix.

I've been thinking and since the iPhone device is so different from the computer I think that I shouldn't even use SFML-Window but rather make a new package called SFML-iPhone which would represent the iPhone better. Over half of the functions that are currently in SFML-Window don't even apply to the iPhone.

While I'm thinking about SFML-iPhone and if it really should be seperate I started working on SFML-Graphics. PostFX will not be able to be used (no shaders as far as I know) and font rendering will be a bit different since freetype is not accessible for the iPhone unless I was to include it statically (don't think the license allows this however). GLEW isn't usable on the iPhone (i tried). The rest of it should port over fine however. I already have it compiling however a lot of it is commented out so it won't work.

Anyways the link is in the same place. Hopefully I can make some design decisions by the end of the weekend and have a beta release.

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
iPhone Port (in progress)
« Reply #5 on: January 23, 2009, 07:53:55 am »
Great :)

Just one thing: you can statically link FreeType, it has a BSD-like license in addition to the GPL one.
Laurent Gomila - SFML developer

IrmatDen

  • Newbie
  • *
  • Posts: 2
    • View Profile
iPhone Port (in progress)
« Reply #6 on: February 07, 2009, 06:09:16 pm »
Hi,

We were discussing about language used for iphone development on another forum (this thread precisely), and while I though Obj-C was used (with official SDK), Laurent pointed out your project using C++.
So, I was wondering if the official SDK supports C++, or are you using an unofficial SDK, thus restricting the use of your port to Cydia and alike distribution models ?

Ceylo

  • Hero Member
  • *****
  • Posts: 2325
    • View Profile
    • http://sfemovie.yalir.org/
    • Email
iPhone Port (in progress)
« Reply #7 on: February 07, 2009, 06:12:06 pm »
You can use Objective-C++.
Want to play movies in your SFML application? Check out sfeMovie!

bslima

  • Newbie
  • *
  • Posts: 1
    • View Profile
iPhone Port (in progress)
« Reply #8 on: March 12, 2009, 05:21:32 am »
How is the porting going ?
I would like to help since im interesting in using in my projects :)

Gammenon

  • Newbie
  • *
  • Posts: 27
    • View Profile
iPhone Port (in progress)
« Reply #9 on: March 20, 2009, 01:23:50 pm »
Hi!

I'm really excited with that port, what it's its current state?

Currently I'm using cocos2d for iphone to develop my games, but it uses objective-C and also draws sprites one by one, I mean, it doesn't batch sprites by texture, for example. SFML does this, or batches sprites by texture, state and so on?

I'll keep an eye in this port, because I am very interested in using SFML for commercial projects.

Thanks :)

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
iPhone Port (in progress)
« Reply #10 on: March 20, 2009, 07:09:26 pm »
Quote
SFML does this, or batches sprites by texture, state and so on?

SFML is just a low-level library, not a 2D engine. So no, no automatic batching.
Laurent Gomila - SFML developer

Gammenon

  • Newbie
  • *
  • Posts: 27
    • View Profile
iPhone Port (in progress)
« Reply #11 on: March 21, 2009, 11:02:36 am »
Thanks for your answer, Laurent.

Anyway, with a fast look over how SFML draws sprites I think it would be possible to render things batched with just a small layer over SFML. For example grouping draw calls in function of returned pointer of function Sprite::GetImage(). What do you think?

Thanks :)

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
iPhone Port (in progress)
« Reply #12 on: March 21, 2009, 01:18:53 pm »
Yes, of course you can do that :)
Laurent Gomila - SFML developer

nfries88

  • Newbie
  • *
  • Posts: 42
    • View Profile
iPhone Port (in progress)
« Reply #13 on: April 11, 2009, 06:36:43 pm »
Quote from: "seoushi"
The accelerometer is sorta like a gyro, it tells how much the phone has accelerated in the certain direction when moved.


G1, Blackberry storm, and some other smartphones have accelerometers too. Wiimotes also have one.
So, adding accelerometer support to SFML is good for porting to other systems in the future as well.

Quote from: "seoushi"
I've been thinking and since the iPhone device is so different from the computer I think that I shouldn't even use SFML-Window but rather make a new package called SFML-iPhone which would represent the iPhone better. Over half of the functions that are currently in SFML-Window don't even apply to the iPhone.

I agree completely with the reason for separating it from traditional computers, but the name should be more generic... SFML-handheld maybe?

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
iPhone Port (in progress)
« Reply #14 on: April 12, 2009, 11:06:43 am »
Quote
I agree completely with the reason for separating it from traditional computers, but the name should be more generic... SFML-handheld maybe?

That sounds a good idea, but what about people who want to make games for both PC and handheld devices? It would be better if they could use the same package with some tweaks, rather than having to use two different packages.
Laurent Gomila - SFML developer

 

anything