/*
    Raydium - CQFD Corp.
    http://raydium.org/
    Released under both BSD license and Lesser GPL library license.
    See "license.txt" file.
*/

#import <UIKit/UIKit.h>
#import <Foundation/Foundation.h>
#import <QuartzCore/QuartzCore.h>
#import <OpenGLES/EAGL.h>
#import <OpenGLES/ES1/gl.h>
#import <OpenGLES/ES1/glext.h>
#import <OpenGLES/EAGLDrawable.h>

#include "myglut-iphoneos.h"

#define ACCEL_SMOOTH_STEPS 3
#define ACCEL_SAMPLE_FREQ  (1.f/30)

static int raydium_argc;
static char **raydium_argv;
extern int raydium_main(int argc, char **argv);
extern int _glutWindowSize[2];
extern signed char _glutWindowInverseRatio;

jmp_buf raydium_environment;
extern jmp_buf *raydium_jump_environment();

static id MyGLUTContext;
static int MyGLUTRenderbuffer;
static int MyGLUTFramebuffer;
static float MyGLUTAcceleration[3];
static int MyGLUTTouch[3];
static int MyGLUTContentsScale;

/*
    ##########################
    #### OpenGL ES UIView ####
    ##########################
*/

@interface MyGLUTView: UIView
{
    @private
        GLint backingWidth;
        GLint backingHeight;
        
        EAGLContext *context;
        GLuint viewRenderbuffer;
        GLuint viewFramebuffer;
        
        GLuint depthRenderbuffer;
}

- (BOOL) createFramebuffer;
- (void) destroyFramebuffer;

- (void) touchesBegan: (NSSet*) touches withEvent: (UIEvent*) event;
- (void) touchesEnded: (NSSet*) touches withEvent: (UIEvent*) event;
- (void) touchesCancelled: (NSSet*) touches withEvent: (UIEvent*) event;
- (void) touchesMoved: (NSSet*) touches withEvent: (UIEvent*) event;

@end

@implementation MyGLUTView

+ (Class) layerClass
{
    return [CAEAGLLayer class];
}

- (id) initWithFrame: (CGRect) frame
{
#ifndef IPHONEOS_ORIENTATION_PORTRAIT
    _glutWindowInverseRatio=1;
    int w=480;
    int h=320;
#else
    _glutWindowInverseRatio=0;
    int w=320;
    int h=480;
#endif
#ifndef IPHONEOS_NORETINA
    float ver = [[[UIDevice currentDevice] systemVersion] floatValue];
    if(ver>3.2)
        {
        int ww,hh;
        UIScreen* mainscr = [UIScreen mainScreen];
        ww = mainscr.currentMode.size.height;
        hh = mainscr.currentMode.size.width;
#ifndef IPHONEOS_ORIENTATION_PORTRAIT
        if(ww>hh)
	    { w=ww; h=hh; }
	else
	    { w=hh; h=ww; } // iPad 2 seems to reverse a few things ...
#else
        if(ww<hh)
	    { w=ww; h=hh; }
	else
	    { w=hh; h=ww; }
#endif
        }
#endif

    _glutWindowSize[0]=w;
    _glutWindowSize[1]=h;
    MyGLUTContentsScale=1;

    self = [super initWithFrame: frame];
    
    if (self != nil)
    {
        CAEAGLLayer *MyGLUTLayer = (CAEAGLLayer*) [self layer];
        MyGLUTLayer.opaque = YES;
        MyGLUTLayer.drawableProperties = [NSDictionary dictionaryWithObjectsAndKeys: [NSNumber numberWithBool: NO], kEAGLDrawablePropertyRetainedBacking, kEAGLColorFormatRGB565, kEAGLDrawablePropertyColorFormat, nil];
#ifndef IPHONEOS_ORIENTATION_PORTRAIT
        if(_glutWindowSize[1]==640)
#else
        if(_glutWindowSize[0]==640)
#endif
            {
            MyGLUTLayer.contentsScale=2;
            MyGLUTContentsScale=MyGLUTLayer.contentsScale;
            }
        context = [[EAGLContext alloc] initWithAPI: kEAGLRenderingAPIOpenGLES1];
        [EAGLContext setCurrentContext: context];
        MyGLUTContext = context;
        
        [self createFramebuffer];
        [context renderbufferStorage: GL_RENDERBUFFER_OES fromDrawable: MyGLUTLayer];
    }


    self.userInteractionEnabled = YES;
    self.multipleTouchEnabled = NO;
    self.exclusiveTouch = YES;
    
    return self;
}

- (void) layoutSubviews
{
    [EAGLContext setCurrentContext: context];
    [self destroyFramebuffer];
    [self createFramebuffer];
}

- (BOOL) createFramebuffer
{
    glGenFramebuffersOES(1, &viewFramebuffer);
    glGenRenderbuffersOES(1, &viewRenderbuffer);
    
    MyGLUTRenderbuffer = viewRenderbuffer;
    MyGLUTFramebuffer = viewFramebuffer;
    
    glBindFramebufferOES(GL_FRAMEBUFFER_OES, viewFramebuffer);
    glBindRenderbufferOES(GL_RENDERBUFFER_OES, viewRenderbuffer);
    [context renderbufferStorage: GL_RENDERItem_colors(&menus[0],0.9,0.2,0.2, 0.2,0.2,0.9);

// Play
menus[1].state=1;
strcpy(menus[1].text,"^fNetwork Play");
strcpy(menus[1].texture,"aqua.tga");
menus[1].OnClick=menu_ip;
menus[1].px=50;
menus[1].py=60;
menus[1].sizex=30;
menus[1].sizey=10;
menus[1].text_size=20;
MenuItem_colors(&menus[1],0.9,0.2,0.2,0.2,0.2,0.9);

// Quit
menus[2].state=1;
strcpy(menus[2].text,"^fQuit");
strcpy(menus[2].texture,"aqua.tga");
menus[2].OnClick=exit;
menus[2].param=0;
menus[2].px=50;
menus[2].py=20;
menus[2].sizex=30;
menus[2].sizey=10;
menus[2].text_size=20;
MenuItem_colors(&menus[2],0.9,0.9,0.9,0.1,0.1,0.1);

}


void menu_color(int dummy)
{
menu=1;

MenuItem_init_all();

menus[0].state=1;
strcpy(menus[0].text,"^fRed Team");
strcpy(menus[0].texture,"aqua.tga");
menus[0].OnClick=menu_game;
menus[0].param=0;
menus[0].px=50;
menus[0].py=90;
menus[0].sizex=30;
menus[0].sizey=10;
menus[0].text_size=20;
MenuItem_colors(&menus[0], TEAM_COL2(0), TEAM_COL(0));

menus[1].state=1;
strcpy(menus[1].text,"^fGreen Team");
strcpy(menus[1].texture,"aqua.tga");
menus[1].OnClick=menu_game;
menus[1].param=1;
menus[1].px=50;
menus[1].py=70;
menus[1].sizex=30;
menus[1].sizey=10;
menus[1].text_size=20;
MenuItem_colors(&menus[1], TEAM_COL2(1), TEAM_COL(1));

menus[2].state=1;
strcpy(menus[2].text,"^fBlue Team");
strcpy(menus[2].texture,"aqua.tga");
menus[2].OnClick=menu_game;
menus[2].param=2;
menus[2].px=50;
menus[2].py=50;
menus[2].sizex=30;
menus[2].sizey=10;
menus[2].text_size=20;
MenuItem_colors(&menus[2], TEAM_COL2(2), TEAM_COL(2));

menus[3].state=1;
strcpy(menus[3].text,"^fYellow Team");
strcpy(menus[3].texture,"aqua.tga");
menus[3].OnClick=menu_game;
menus[3].param=3;
menus[3].px=50;
menus[3].py=30;
menus[3].sizex=30;
menus[3].sizey=10;
menus[3].text_size=20;
MenuItem_colors(&menus[3], TEAM_COL2(3), TEAM_COL(3));

menus[4].state=1;
strcpy(menus[4].text,"^fBACK");
strcpy(menus[4].texture,"aqua.tga");
menus[4].OnClick=menu_main;
menus[4].px=50;
menus[4].py=10;
menus[4].sizex=30;
menus[4].sizey=10;
menus[4].text_size=25;
MenuItem_colors(&menus[4],0.1,0.1,0.1, 0.9,0.9,0.9);
}

void menu_ip(int dummy)
{
FILE *fp;
menu_networked_game=1;
menu=1;


menu_server_ip[0]=0;
fp=fopen("server.last","rt");
if(fp)
    {
    fgets(menu_server_ip,128,fp);
    if(strlen(menu_server_ip) && menu_server_ip[strlen(menu_server_ip)-1]=='\n')
	menu_server_ip[strlen(menu_server_ip)-1]=0;
    fclose(fp);
    }

menu_team=dummy;

MenuItem_init_all();

menus[0].state=1;
strcpy(menus[0].text,"^fServer IP");
strcpy(menus[0].texture,"aqua.tga");
menus[0].px=50;
menus[0].py=90;
menus[0].sizex=30;
menus[0].sizey=10;
menus[0].text_size=20;
MenuItem_colors(&menus[0],0.6,0.2,0.2, 0.9,0.2,0.2);

menus[1].state=1;
strcpy(menus[1].text,"^fBACK");
strcpy(menus[1].texture,"aqua.tga");
menus[1].OnClick=menu_main;
menus[1].px=50;
menus[1].py=10;
menus[1].sizex=30;
menus[1].sizey=10;
menus[1].text_size=25;
MenuItem_colors(&menus[1],0.1,0.1,0.1, 0.2,0.9,0.2);

menus[2].state=1;
strcpy(menus[2].text,"^f7");
strcpy(menus[2].texture,"aquab.tga");
menus[2].OnClick=menu_number;
menus[2].param='7';
menus[2].px=40;
menus[2].py=75;
menus[2].sizex=10;
menus[2].sizey=10;
menus[2].text_size=20;
MenuItem_colors(&menus[2],0.9,0.2,0.2, 0.2,0.9,0.2);

menus[3].state=1;
strcpy(menus[3].text,"^f8");
strcpy(menus[3].texture,"aquab.tga");
menus[3].OnClick=menu_number;
menus[3].param='8';
menus[3].px=50;
menus[3].py=75;
menus[3].sizex=10;
menus[3].sizey=10;
menus[3].text_size=20;
MenuItem_colors(&menus[3],0.9,0.2,0.2, 0.2,0.9,0.2);

menus[4].state=1;
strcpy(menus[4].text,"^f9");
strcpy(menus[4].texture,"aquab.tga");
menus[4].OnClick=menu_number;
menus[4].param='9';
menus[4].px=60;
menus[4].py=75;
menus[4].sizex=10;
menus[4].sizey=10;
menus[4].text_size=20;
MenuItem_colors(&menus[4],0.9,0.2,0.2, 0.2,0.9,0.2);

menus[5].state=1;
strcpy(menus[5].text,"^f4");
strcpy(menus[5].texture,"aquab.tga");
menus[5].OnClick=menu_number;
menus[5].param='4';
menus[5].px=40;
menus[5].py=65;
menus[5].sizex=10;
menus[5].sizey=10;
menus[5].text_size=20;
MenuItem_colors(&menus[5],0.9,0.2,0.2, 0.2,0.9,0.2);

menus[6].state=1;
strcpy(menus[6].text,"^f5");
strcpy(menus[6].texture,"aquab.tga");
menus[6].OnClick=menu_number;
menus[6].param='5';
menus[6].px=50;
menus[6].py=65;
menus[6].sizex=10;
menus[6].sizey=10;
menus[6].text_size=20;
MenuItem_colors(&menus[6],0.9,0.2,0.2, 0.2,0.9,0.2);

menus[7].state=1;
strcpy(menus[7].text,"^f6");
strcpy(menus[7].texture,"aquab.tga");
menus[7].OnClick=menu_number;
menus[7].param='6';
menus[7].px=60;
menus[7].py=65;
menus[7].sizex=10;
menus[7].sizey=10;
menus[7].text_size=20;
MenuItem_colors(&menus[7],0.9,0.2,0.2, 0.2,0.9,0.2);

menus[8].state=1;
strcpy(menus[8].text,"^f1");
strcpy(menus[8].texture,"aquab.tga");
menus[8].OnClick=menu_number;
menus[8].param='1';
menus[8].px=40;
menus[8].py=55;
menus[8].sizex=10;
menus[8].sizey=10;
menus[8].text_size=20;
MenuItem_colors(&menus[8],0.9,0.2,0.2, 0.2,0.9,0.2);

menus[9].state=1;
strcpy(menus[9].text,"^f2");
strcpy(menus[9].texture,"aquab.tga");
menus[9].OnClick=menu_number;
menus[9].param='2';
menus[9].px=50;
menus[9].py=55;
menus[9].sizex=10;
menus[9].sizey=10;
menus[9].text_size=20;
MenuItem_colors(&menus[9],0.9,0.2,0.2, 0.2,0.9,0.2);

menus[10].state=1;
strcpy(menus[10].text,"^f3");
strcpy(menus[10].texture,"aquab.tga");
menus[10].OnClick=menu_number;
menus[10].param='3';
menus[10].px=60;
menus[10].py=55;
menus[10].sizex=10;
menus[10].sizey=10;
menus[10].text_size=20;
MenuItem_colors(&menus[10],0.9,0.2,0.2, 0.2,0.9,0.2);

menus[11].state=1;
strcpy(menus[11].text,"^f0");
strcpy(menus[11].texture,"aquab.tga");
menus[11].OnClick=menu_number;
menus[11].param='0';
menus[11].px=40;
menus[11].py=45;
menus[11].sizex=10;
menus[11].sizey=10;
menus[11].text_size=20;
MenuItem_colors(&menus[11],0.9,0.2,0.2, 0.2,0.9,0.2);


menus[12].state=1;
strcpy(menus[12].text,"^f.");
strcpy(menus[12].texture,"aquab.tga");
menus[12].OnClick=menu_number;
menus[12].param='.';
menus[12].px=60;
menus[12].py=45;
menus[12].sizex=10;
menus[12].sizey=10;
menus[12].text_size=20;
MenuItem_colors(&menus[12],0.9,0.2,0.2, 0.2,0.9,0.2);

// Displays IP address
menus[13].state=1;
sprintf(menus[13].text,"^f%s",menu_server_ip);
strcpy(menus[13].texture,"aqua.tga");
menus[13].px=30;
menus[13].py=30;
menus[13].sizex=40;
menus[13].sizey=10;
menus[13].text_size=20;
MenuItem_colors(&menus[13],0.9,0.2,0.2, 0.9,0.2,0.2);

menus[14].state=1;
strcpy(menus[14].text,"^fdel");
strcpy(menus[14].texture,"aquab.tga");
menus[14].OnClick=menu_number;
menus[14].param=27;
menus[14].px=50;
menus[14].py=45;
menus[14].sizex=10;
menus[14].sizey=10;
menus[14].text_size=20;
MenuItem_colors(&menus[14],0.9,0.2,0.2, 0.2,0.9,0.2);

menus[15].state=1;
strcpy(menus[15].text,"^fConnect");
strcpy(menus[15].texture,"aqua.tga");
menus[15].OnClick=menu_color;
menus[15].param=dummy;
menus[15].px=70;
menus[15].py=30;
menus[15].sizex=30;
menus[15].sizey=10;
menus[15].text_size=20;
MenuItem_colors(&menus[15],0.9,0.2,0.2, 0.2,0.2,0.9);

}

void menu_game(int team)
{
menu_team=team;
menu=0;
start_game();
}