Register FAQ / Rules Forum Spy Search Today's Posts Mark Forums Read
Go Back   MacRumors Forums > Apple Systems and Services > Programming > Mac Programming

Reply
 
Thread Tools Search this Thread Display Modes
Old Oct 13, 2005, 02:25 PM   #1
hoschi
macrumors newbie
 
Join Date: Sep 2005
How to build a Start/Stop Button

I am using a normal cocoa opengl program in a double buffer mode, in order to run something... I use a button to start the program running and the program stops only when it has finished processing itself completely. I was wondering if it is possible to have this start button convert itself into pause button during the time when its running... and have another button for stopping the application and starting again..

Ok so there are two things..

1. How to convert this start button into a start/Pause button ... say something like a ball moving on the floor just pauses when re-press the button and moves again as I press it once more...-- this application which I am using the button for is a cocoa opengl program and also uses some c++ libraries..

2. I want to have another button which can stop the application completely. This would not quite is but just stop it. Say a ball moving on the floor is returned to its initial state so that it can restart.
hoschi is offline   0 Reply With Quote
Old Oct 13, 2005, 07:54 PM   #2
HiRez
macrumors 601
 
HiRez's Avatar
 
Join Date: Jan 2004
Location: Western US
Quote:
Originally Posted by hoschi
1. How to convert this start button into a start/Pause button ... say something like a ball moving on the floor just pauses when re-press the button and moves again as I press it once more...-- this application which I am using the button for is a cocoa opengl program and also uses some c++ libraries..
If you're just using a Cocoa NSButton, sure, just set the alternate title and/or alternate image and set the button behavior to "Toggle" (you will have to choose a button type other than the default). Then just check the state in your action method to decide which action to take in your app.

Quote:
2. I want to have another button which can stop the application completely. This would not quite is but just stop it. Say a ball moving on the floor is returned to its initial state so that it can restart.
I don't really understand what you're asking here...
HiRez is offline   0 Reply With Quote
Old Oct 13, 2005, 08:30 PM   #3
hoschi
Thread Starter
macrumors newbie
 
Join Date: Sep 2005
Quote:
Originally Posted by HiRez
If you're just using a Cocoa NSButton, sure, just set the alternate title and/or alternate image and set the button behavior to "Toggle" (you will have to choose a button type other than the default). Then just check the state in your action method to decide which action to take in your app.

I don't really understand what you're asking here...
What I mean is that I want a button that merely stops the whole application and brings it to its initial state..
hoschi is offline   0 Reply With Quote
Old Oct 14, 2005, 03:19 AM   #4
caveman_uk
Guest
 
caveman_uk's Avatar
 
Join Date: Feb 2003
Location: Hitchin, Herts, UK
Quote:
Originally Posted by hoschi
What I mean is that I want a button that merely stops the whole application and brings it to its initial state..
This is just another button. Not difficult. The main thing to worry about is that if you're program is computation intensive is that you do it right so as not to lock up the GUI. Check out threads.

You sound like you don't have much cocoa experience. Maybe you need to get a bit more familiar with it through simpler projects first?
caveman_uk is offline   0 Reply With Quote
Old Oct 14, 2005, 02:46 PM   #5
AlmostThere
macrumors 6502a
 
Join Date: Feb 2005
Location: The royal throne of kings, the sceptred isle, the other Eden, the fortress built
Your pause button should just set an internal variable (e.g. bool) so there is no update when you are calculating new positions for your moving objects. Just cache the time when the update function is called until your app is ready to un-pause, at which point you can start calculating the new positions again.

Your reset button should just call a function that sets your objects to some saved or initial state.
AlmostThere is offline   0 Reply With Quote
Old Oct 14, 2005, 03:28 PM   #6
hoschi
Thread Starter
macrumors newbie
 
Join Date: Sep 2005
Quote:
Originally Posted by AlmostThere
Your pause button should just set an internal variable (e.g. bool) so there is no update when you are calculating new positions for your moving objects. Just cache the time when the update function is called until your app is ready to un-pause, at which point you can start calculating the new positions again.

Your reset button should just call a function that sets your objects to some saved or initial state.
To be moe specific now...I am posting this program I wrote sometime back, which makes a triangle using OpenGl. There are three text boxes, where the user can enter the initial velocities for each of the three triangle nodes. Then I specify the number of time steps this triangle should redraw itself doing what I ask it to do in the program (which normally is to change its angle randomly and move eaach node). I am using a button to start this process after I specify the velocities and the timesteps. This button is a "toggle" type NSButton which has its alternate title set to Pause. however once I start the program and once the program start redrawing the triangle, I cannot stop it in between. I am looking for ways in which I might be able to discontinue this operation/program by pressing the button again when it displays the "pause" state --------

#import "triangle.h"
#include <stdlib.h>
#include <time.h>
#include <OpenGL/gl.h>


@implementation triangle

- (id) initWithCoder: (NSCoder *) coder
{
NSOpenGLPixelFormatAttribute attrs[] =
{
NSOpenGLPFADoubleBuffer, nil
};

NSOpenGLPixelFormat* pixFmt ;
long swapInterval ;

self = [super initWithCoder: coder] ;
ver1.x = 0.556;
ver1.y = 0.2;
ver2.x = -0.343;
ver2.y = 0.443;
ver3.x = 0.02;
ver3.y = 0.66;
vel1 = 0.;
vel2 = 0.;
vel3 = 0.;
steps = 0;
flipped = NO;
a1 = time(NULL);
r = 0.0;
g = 1.0;
b = 0.0;
angle1 = 2*3.14*[self generateNumber];
angle2 = 2*3.14*[self generateNumber];
angle3 = 2*3.14*[self generateNumber];



pixFmt = [[NSOpenGLPixelFormat alloc] initWithAttributes: attrs] ;
[self setPixelFormat: pixFmt] ;
swapInterval = 1 ;
[[self openGLContext] setValues: &swapInterval forParameter: NSOpenGLCPSwapInterval ] ;
return self ;
}

-(void) awakeFromNib
{
[velocity1 setFloatValue:vel1];
[velocity2 setFloatValue:vel2];
[velocity3 setFloatValue:vel3];
[timesteps setFloatValue:steps];
}

-(float)generateNumber
{
srand(a1);
int a2,b2;
float c;
a2 = rand();
a1 = a2;
b2 = RAND_MAX;
float tempa, tempb;
tempa = a2;
tempb = b2;
c = tempa/tempb;
return c;
}


-(NSPoint)boundaryConditionNSPoint)var
{
NSPoint tempPoint;
tempPoint.x = var.x;
tempPoint.y = var.y;

if (tempPoint.x < -1.0)
{tempPoint.x = tempPoint.x +2.0;
flipped = YES;}
if (tempPoint.x > 1.0)
{ tempPoint.x = tempPoint.x -2.0;
flipped = YES;}

if (tempPoint.y < -1.0)
{ tempPoint.y = 2.0+ tempPoint.y;
flipped = YES;}
if (tempPoint.y > 1.0)
{ tempPoint.y = tempPoint.y - 2.0;
flipped = YES;}

return tempPoint;
}

- (IBAction)moveid)sender
{
vel1 = [velocity1 floatValue];
vel2 = [velocity2 floatValue];
vel3 = [velocity3 floatValue];
steps = [timesteps floatValue];
int t;

for (t=0; t<=steps; t++)
{
float angle, interval;
flipped = NO;
interval =0.005;

NSLog(@"Time Step %d", t);

angle1 += [self generateNumber]*0.01;
ver1.x = ver1.x + interval*vel1*cos(angle1);
ver1.y = ver1.y + interval*vel1*sin(angle1);
ver1 = [self boundaryCondition:ver1];

angle2 += [self generateNumber]*0.01;
ver2.x = ver2.x + interval*vel2*cos(angle2);
ver2.y = ver2.y + interval*vel2*sin(angle2);
ver2 = [self boundaryCondition:ver2];

angle3 += [self generateNumber]*0.01;
ver3.x = ver3.x + interval*vel3*cos(angle3);
ver3.y = ver3.y + interval*vel3*sin(angle3);
ver3 = [self boundaryCondition:ver3];

[self lockFocus];
[self drawRect:[self frame]];
[self unlockFocus];
}
}

-(void)drawRectNSRect)rect
{
if (flipped == YES) {
r = [self generateNumber];
b = [self generateNumber];
g = [self generateNumber];
}

glClearColor( 0, 0, 0, 0 ) ;
glClear( GL_COLOR_BUFFER_BIT ) ;
glColor3f( r,g,b ) ;
glBegin( GL_TRIANGLES) ;
{
glVertex3f( ver1.x, ver1.y, 0.0 ) ;
glVertex3f( ver2.x, ver2.y, 0.0 ) ;
glVertex3f( ver3.x , ver3.y, 0.0 ) ;
}
glEnd() ;
glFinish() ;
[[self openGLContext] flushBuffer];
}
@end

Last edited by hoschi; Oct 14, 2005 at 03:59 PM.
hoschi is offline   0 Reply With Quote
Old Oct 14, 2005, 04:42 PM   #7
AlmostThere
macrumors 6502a
 
Join Date: Feb 2005
Location: The royal throne of kings, the sceptred isle, the other Eden, the fortress built
Can I suggest you (as a starting point) have a look at NSTimer.

http://developer.apple.com/documenta...ers/index.html
AlmostThere is offline   0 Reply With Quote
Old Oct 20, 2005, 03:54 PM   #8
hoschi
Thread Starter
macrumors newbie
 
Join Date: Sep 2005
Thanks...

Quote:
Originally Posted by AlmostThere
Can I suggest you (as a starting point) have a look at NSTimer.

http://developer.apple.com/documenta...ers/index.html
Thanks for the incredible help... the program is running nice and good... Not only have I incorporated the toggle button but I was also able to solve some other problems...
hoschi is offline   0 Reply With Quote

Reply
MacRumors Forums > Apple Systems and Services > Programming > Mac Programming

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Similar Threads
thread Thread Starter Forum Replies Last Post
New to Mac: How to stop applications on start-up? StarFleet Mac Basics and Help 5 Jan 10, 2014 02:17 AM
Stop Moaning about the Home Button on the 5s SMIDG3T iPhone 3 Sep 21, 2013 06:04 AM
Start/stop a shell script loop hakonandersson Mac Programming 2 May 29, 2013 05:58 AM
Start/Stop App Automatically - Anyone know how? eyepea OS X 1 Dec 7, 2012 01:30 AM
iPhone: Podcast App - podcasts start/stop whiteshadoww iOS 6 4 Sep 22, 2012 10:37 AM

Forum Jump

All times are GMT -5. The time now is 03:42 AM.

Mac Rumors | Mac | iPhone | iPhone Game Reviews | iPhone Apps

Mobile Version | Fixed | Fluid | Fluid HD
Copyright 2002-2013, MacRumors.com, LLC