Skip to main content

My simple Analog clock with GLUT

here is my simple Analog Clock. create using GLUT (OpenGL). compile correctly in Compiles correctly on Dev-C++ 4.9.9.2.

AnalogClock.dev

[Project]
FileName=AnalogClock.dev
Name=Project1
UnitCount=7
Type=0
Ver=1
ObjFiles=
Includes=
Libs=
PrivateResource=
ResourceIncludes=
MakeIncludes=
Compiler=
CppCompiler=
Linker=-lglut32 -lglu32 -lopengl32 -lwinmm -lgdi32
IsCpp=1
Icon=
ExeOutput=
ObjectOutput=
OverrideOutput=0
OverrideOutputName=
HostApplication=
Folders=
CommandLine=
UseCustomMakefile=0
CustomMakefile=
IncludeVersionInfo=0
SupportXPThemes=0
CompilerSet=0
CompilerSettings=

[Unit1]
FileName=main.cpp
CompileCpp=1
Folder=
Compile=1
Link=1
Priority=1000
OverrideBuildCmd=0
BuildCmd=

[VersionInfo]
Major=0
Minor=1
Release=1
Build=1
LanguageID=1033
CharsetID=1252
CompanyName=
FileVersion=
FileDescription=Developed using the Dev-C++ IDE
InternalName=
LegalCopyright=
LegalTrademarks=
OriginalFilename=
ProductName=
ProductVersion=
AutoIncBuildNr=0

[Unit3]
FileName=drawing.cpp
CompileCpp=1
Folder=
Compile=1
Link=1
Priority=1000
OverrideBuildCmd=0
BuildCmd=

[Unit2]
FileName=drawing.h
CompileCpp=1
Folder=
Compile=1
Link=1
Priority=1000
OverrideBuildCmd=0
BuildCmd=

[Unit4]
FileName=render.h
CompileCpp=1
Folder=
Compile=1
Link=1
Priority=1000
OverrideBuildCmd=0
BuildCmd=

[Unit5]
FileName=render.cpp
CompileCpp=1
Folder=
Compile=1
Link=1
Priority=1000
OverrideBuildCmd=0
BuildCmd=

[Unit6]
FileName=analogclock.h
CompileCpp=1
Folder=
Compile=1
Link=1
Priority=1000
OverrideBuildCmd=0
BuildCmd=

[Unit7]
FileName=analogclock.cpp
CompileCpp=1
Folder=
Compile=1
Link=1
Priority=1000
OverrideBuildCmd=0
BuildCmd=



analogclock.h :


// Class automatically generated by Dev-C++ New Class wizard

#ifndef ANALOGCLOCK_H
#define ANALOGCLOCK_H

#include
#include
#include "drawing.h"
#include "render.h"

/*
* class to get Current local time
*/
class systemTime
{
private:
struct tm ts;
time_t now;
char buf[80];
public :
int hour,min,sec,wday,mday;
// class constructor
systemTime();
// class destructor
~systemTime();
// update current local time
void update();
};

class hand
{
private:
line *tline;

public:
bool visible;
float lnpos[4];// line coordinates info
// class constructor
hand(float x1, float y1, float x2, float y2);
// class destructor
~hand();
// update hand variables
void update(float x1, float y1, float x2, float y2);
// display hand to the screen
void display();

};

class token
{
private:
line *tkline;
float x,y;
float getxRotate(float dx, float dy, float angle);
float getyRotate(float dx, float dy, float angle);
public:
// class constructor
token(float x,float y);
// class destructor
~token();
void display();
};

class clockNumber
{
private:
float getxRotate(float dx, float dy, float angle);
float getyRotate(float dx, float dy, float angle);
public:
bool visible;
float x,y;
// class constructor
clockNumber(float x, float y);
// class destructor
~clockNumber();
void display();
};

class frame
{
public:
bool visible;
// class constructor
frame();
// class destructor
~frame();
void display();
};

class clockDate
{
public:
bool visible;
// class constructor
clockDate();
// class destructor
~clockDate();
// No description
void display(int wday, int mday);
};

class analogClock
{
private:
hand *hour,*minute,*second;
systemTime *localtime;
token *cltoken;
clockNumber *num;
clockDate *clockdate;
frame *clframe;
public:
// class constructor
analogClock();
// class destructor
~analogClock();
// update clock information
void update();
// display analog Clock
void display();
bool getSecondVisible();
void setSecondVisible(bool v);
bool getNumberVisible();
void setNumberVisible(bool v);
bool getClockdateVisible();
void setClockdateVisible(bool v);
bool getFrameVisible();
void setFrameVisible(bool v);

};

#endif // ANALOGCLOCK_H


analogclock.cpp:


// Class automatically generated by Dev-C++ New Class wizard

#include "analogclock.h" // class's header file

float clock_xC=100;
float clock_yC=86;

// class constructor
systemTime::systemTime()
{
time(&now);
ts = *localtime(&now);
wday = ts.tm_wday;
mday = ts.tm_mday;
hour = ts.tm_hour % 12;
min = ts.tm_min;
sec = ts.tm_sec;
}

// class destructor
systemTime::~systemTime()
{

}

// update current local time
void systemTime::update()
{
/* DONE (#1#): Implement systemTime::update() */
time(&now);
ts = *localtime(&now);
wday = ts.tm_wday;
mday = ts.tm_mday;
hour = ts.tm_hour % 12;
min = ts.tm_min;
sec = ts.tm_sec;
}

// class constructor
hand::hand(float x1, float y1, float x2, float y2)
{
tline = new line(x1,y1,x2,y2);
visible = true;
lnpos[0]=x1;
lnpos[1]=y1;
lnpos[2]=x2;
lnpos[3]=y2;
}

// class destructor
hand::~hand()
{
// insert your code here
}

// update hand variables
void hand::update(float x1, float y1, float x2, float y2)
{
/* DONE (#1#): Implement hand::update() */
tline->update(x1,y1,x2,y2);
}

// display hand to the screen
void hand::display()
{
/* DONE (#1#): Implement hand::display() */
if(visible)
{
glPushMatrix();
tline->draw();
glPopMatrix();
}
}

// class constructor
token::token(float x,float y)
{
this->x = x;
this->y = y;
tkline = new line(x,y-2.0,x,y);
}

// class destructor
token::~token()
{
// insert your code here
}

void token::display()
{
/* DONE (#1#): Implement token::display() */
float x1,y1,x2,y2,dx,dy;
glPushMatrix();
glColor3f(0.1,0.1,0.1);
for(float angle=6.0;angle<=360;angle+=6.0) { if(int(angle) % 30==0) glPointSize(4.0); else glPointSize(2.0); dx = x - clock_xC; dy = (y-2.0) - clock_yC; x1 = getxRotate(dx,dy,angle); y1 = getyRotate(dx,dy,angle); dx = x - clock_xC; dy = y - clock_yC; x2 = getxRotate(dx,dy,angle); y2 = getyRotate(dx,dy,angle); tkline->update(x1,y1,x2,y2);
tkline->draw();
}

glPopMatrix();
}

float token::getxRotate(float dx, float dy, float angle)
{
return (clock_xC +
float ((dx)* cos (angle*(M_PI/180))+
(dy)* sin(angle*(M_PI/180))));

}

float token::getyRotate(float dx, float dy, float angle)
{
return (clock_yC +
float ((dy) * cos (angle*(M_PI/180))-
(dx)* sin(angle*(M_PI/180))));
}

// class constructor
analogClock::analogClock()
{
localtime = new systemTime();
hour = new hand(clock_xC,clock_yC,clock_xC,clock_yC+24);
minute = new hand(clock_xC,clock_yC,clock_xC,clock_yC+31);
second = new hand(clock_xC,clock_yC-12,clock_xC,clock_yC+31);
cltoken = new token(clock_xC,clock_yC+25);
clframe = new frame();
num = new clockNumber(clock_xC,clock_yC+30);
clockdate = new clockDate();
}

// class destructor
analogClock::~analogClock()
{
// insert your code here
}


// update clock information
void analogClock::update()
{
//current local time update
localtime->update();
float angle;
float x1,y1,x2,y2,dx,dy;
//hour update
angle = localtime->hour * 30.0 + localtime->min * 0.5;// Ignore change in seconds
dx = hour->lnpos[0]-clock_xC;
dy = hour->lnpos[1]-clock_yC;
x1 = clock_xC + float (dx * cos (angle*(M_PI/180))+ dy * sin(angle*(M_PI/180)));
y1 = clock_yC + float (dy * cos (angle*(M_PI/180))- dx * sin(angle*(M_PI/180)));
dx = hour->lnpos[2]-clock_xC;
dy = hour->lnpos[3]-clock_yC;
x2 = clock_xC + float (dx * cos (angle*(M_PI/180))+ dy * sin(angle*(M_PI/180)));
y2 = clock_yC + float (dy * cos (angle*(M_PI/180))- dx * sin(angle*(M_PI/180)));
hour->update(x1,y1,x2,y2);
//minute update
angle = localtime->min * 6.0 + localtime->sec * (6.0/60.0);// Minute hand's position also changes with secs
dx = minute->lnpos[0]-clock_xC;
dy = minute->lnpos[1]-clock_yC;
x1 = clock_xC + float (dx * cos (angle*(M_PI/180))+ dy * sin(angle*(M_PI/180)));
y1 = clock_yC + float (dy * cos (angle*(M_PI/180))- dx * sin(angle*(M_PI/180)));
dx = minute->lnpos[2]-clock_xC;
dy = minute->lnpos[3]-clock_yC;
x2 = clock_xC + float (dx * cos (angle*(M_PI/180))+ dy * sin(angle*(M_PI/180)));
y2 = clock_yC + float (dy * cos (angle*(M_PI/180))- dx * sin(angle*(M_PI/180)));
minute->update(x1,y1,x2,y2);
//second update
angle = localtime->sec * 6.0 ;
dx = second->lnpos[0]-clock_xC;
dy = second->lnpos[1]-clock_yC;
x1 = clock_xC + float (dx * cos (angle*(M_PI/180))+ (dy) * sin(angle*(M_PI/180)));
y1 = clock_yC + float ((dy) * cos (angle*(M_PI/180))- dx * sin(angle*(M_PI/180)));
dx = second->lnpos[2]-clock_xC;
dy = second->lnpos[3]-clock_yC;
x2 = clock_xC + float (dx * cos (angle*(M_PI/180))+ dy * sin(angle*(M_PI/180)));
y2 = clock_yC + float (dy * cos (angle*(M_PI/180))- dx * sin(angle*(M_PI/180)));
second->update(x1,y1,x2,y2);
}

// display analog Clock
void analogClock::display()
{

clframe->display();
//display title
glPushMatrix();
glColor3d(0.5,0,0.8);
displayString(clock_xC-17,clock_yC+40,GLUT_BITMAP_TIMES_ROMAN_24,"GLUT Analog Clock");
glPopMatrix();
clockdate->display(localtime->wday,localtime->mday);
cltoken->display();
glPointSize(7.0);
glColor3f(0.1,0.1,0.1);
num->display();
hour->display();
minute->display();
glColor3f(0.9,0.2,0.2);
glPointSize(2.2);
second->display();
glPushMatrix();
//circle center;
glPointSize(1);
glColor3f(0.9,0.7,0.5);
circle CenterC(clock_xC,clock_yC,0.1);
CenterC.draw();
for (float i=0.24;i<2.3;i+=0.026){>visible;
}

void analogClock::setSecondVisible(bool v)
{
second->visible = v;
}

bool analogClock::getNumberVisible()
{
return num->visible;
}

void analogClock::setNumberVisible(bool v)
{
num->visible = v;
}

bool analogClock::getClockdateVisible()
{
return clockdate->visible;
}

void analogClock::setClockdateVisible(bool v)
{
clockdate->visible = v;
}

bool analogClock::getFrameVisible()
{
return clframe->visible;
}

void analogClock::setFrameVisible(bool v)
{
clframe->visible = v;
}

// class constructor
clockNumber::clockNumber(float x, float y)
{
this->x = x;
this->y = y;
visible = true;
}

// class destructor
clockNumber::~clockNumber()
{
// insert your code here
}

void clockNumber::display()
{
if(visible){
char* number[12]={"12","1","2","3","4","5","6","7","8","9","10","11"};
float x_,y_,dx, dy;
int val=0;
glPushMatrix();
glColor3d(0.1,0.1,0.1);
for(float angle=0;angle<=355;angle+=30.0) { dx = x - clock_xC; dy = y - clock_yC; x_ = getxRotate(dx,dy,angle); y_ = getyRotate(dx,dy,angle); if(val<=6 && val > 0)
displayString(x_-1.2,y_-1.5,GLUT_BITMAP_TIMES_ROMAN_24,number[val]);
else if(val>6 && val<=9) displayString(x_-0.5,y_-1.5,GLUT_BITMAP_TIMES_ROMAN_24,number[val]); else displayString(x_-2,y_-1.5,GLUT_BITMAP_TIMES_ROMAN_24,number[val]); val++; } glPopMatrix(); } } float clockNumber::getxRotate(float dx, float dy, float angle) { return (clock_xC + float ((dx)* cos (angle*(M_PI/180))+ (dy)* sin(angle*(M_PI/180)))); } float clockNumber::getyRotate(float dx, float dy, float angle) { return (clock_yC + float ((dy) * cos (angle*(M_PI/180))- (dx)* sin(angle*(M_PI/180)))); } // class constructor frame::frame() { visible = true; } // class destructor frame::~frame() { } // No description void frame::display() { if(visible){ glPushMatrix(); glBegin (GL_POLYGON); glColor3f (1,1,0.4); glVertex2f (clock_xC-50, clock_yC+50); glColor3f (0.6, 0.6, 0.4); glVertex2f (clock_xC+50,clock_yC+50); glColor3f (1, 0.5, 1); glVertex2f (clock_xC+50,clock_yC-70); glColor3f (0.5, 0.5, 0.2); glVertex2f (clock_xC-50,clock_yC-70); glEnd (); glPopMatrix(); glPushMatrix(); glColor3f (0.9, 0.9, 0.9); glPointSize(1.2); //float i = 34; float i = 32.5; circle fCircle(clock_xC,clock_yC,i); fCircle.draw(); glPopMatrix(); //for(;i>=2.3;)
for(;i>=28.5;)
{
glPushMatrix();
i-=0.026;
fCircle.update(clock_xC,clock_yC,i);
fCircle.draw();
glPopMatrix();
}

}
}

// class constructor
clockDate::clockDate()
{
visible = true;
}

// class destructor
clockDate::~clockDate()
{
// insert your code here
}

void clockDate::display(int wday, int mday)
{
if(visible){
char* day_name[7]= {"SUN ","MON ","TUE ","WED ","THU ","FRI ","SAT "};
char* str_date[31]= {"1","2","3","4","5","6","7","8","9","10",
"11","12","13","14","15","16","17","18","19","20",
"21","22","23","24","25","26","27","28","29","30","31"};
glPushMatrix();
glColor3f (0.1, 0.1, 0.1);
glPointSize(1.1);
line temp(clock_xC-8.3,clock_yC-15,clock_xC+8.3,clock_yC-15);
temp.draw();
temp.update(clock_xC-8.3,clock_yC-15,clock_xC-8.3,clock_yC-10);
temp.draw();
temp.update(clock_xC-8.3,clock_yC-10,clock_xC+8.3,clock_yC-10);
temp.draw();
temp.update(clock_xC+8.3,clock_yC-15,clock_xC+8.3,clock_yC-10);
temp.draw();
temp.update(clock_xC+1.5,clock_yC-15,clock_xC+1.5,clock_yC-10);
temp.draw();
displayString(clock_xC-7.5,clock_yC-14,GLUT_BITMAP_TIMES_ROMAN_24,day_name[wday]);
glutBitmapCharacter(GLUT_BITMAP_TIMES_ROMAN_24,' ');
displayString(clock_xC+2.7,clock_yC-14,GLUT_BITMAP_TIMES_ROMAN_24,str_date[mday-1]);
glPopMatrix();
}
}


drawing.h:

// Class automatically generated by Dev-C++ New Class wizard

#ifndef DRAWING_H
#define DRAWING_H

#include
#include
#include "render.h"
/*
* drawing.h
* Written by Angga Purwanaputra January 2008
*/
class line
{
private:
float dx;
float dy;
public:
float xBegin;
float yBegin;
float xEnd;
float yEnd;
public:
// class constructor
line(float x1, float y1, float x2, float y2);
// class destructor
~line();
// update line variables
void update(float x1, float y1, float x2, float y2);
// Draw Line with DDA algorithm
void draw();
};



class circle
{
public:
float xC; // Center of circle x
float yC; // Center of circle y
float Radius;
// class constructor
circle(float InxC, float InyC, float radius);
// class destructor
~circle();
// update circle variables
void update(float InxC, float InyC, float radius);
// Draw Circle with Midpoint algorithm
void draw();
private:
// Generate all posible coordinate set to circle
void setCirclePixel(float x,float y);
};


#endif // DRAWING_H


drawing.cpp:


// Class automatically generated by Dev-C++ New Class wizard

#include "drawing.h" // class's header file

// class constructor
line::line(float x1, float y1, float x2, float y2)
{
xBegin = x1;
yBegin = y1;
xEnd = x2;
yEnd = y2;
dx = xEnd - xBegin;
dy = yEnd - yBegin;
}

// class destructor
line::~line()
{

}


// update line variables
void line::update(float x1, float y1, float x2, float y2)
{
xBegin = x1;
yBegin = y1;
xEnd = x2;
yEnd = y2;
dx = xEnd - xBegin;
dy = yEnd - yBegin;
}

// Draw Line with DDA algorithm
void line::draw()
{
int steps;
float xInc,yInc,x=xBegin,y=yBegin;

if(fabs(dx)>fabs(dy)) steps =int (fabs(dx)*7);
else steps =int (fabs(dy)*7);
xInc = float(dx)/float(steps);
yInc = float(dy)/float(steps);
for(int i=0;i
render.h:


#ifndef RENDER_H
#define RENDER_H

#include

void displayString(float x, float y, void *font, char *string);
void setPixel(GLfloat x,GLfloat y);
void init(void);

#endif


render.cpp:


#include "render.h"

void init(void){
glClearColor(1.0,1.0,1.0,0.0);
glMatrixMode(GL_PROJECTION);
gluOrtho2D(0.0,200.0,0.0,150.0);
}

void setPixel(GLfloat x,GLfloat y)
{
glBegin(GL_POINTS);
glVertex2f(x,y);
glEnd();
}

void displayString(float x, float y, void *font, char *string) {
char *element;
glRasterPos3f(x, y,0);
for (element=string; *element != '\0'; element++) {
glutBitmapCharacter(font,*element);
}
}


main.cpp:


/*
* GLUT Analog Clock
* C++ Program to display an analog clock using GLUT Programming Interface
* Written by Angga Purwana January 2008
* This program displays a simple analog clock to show the
* current system time.
* Compiles correctly on Dev-C++ 4.9.9.2
*/

#include
#include

#include "render.h"
#include "analogclock.h"

analogClock *myClock;
//

class menu
{
public:
bool visible;
menu(){
visible = true;
}
void display(){
if(visible){
glPushMatrix();
glColor3d(0.9,0.0,0.0);
displayString(78,45,GLUT_BITMAP_HELVETICA_12,"Key Option Menu");
displayString(78,41,GLUT_BITMAP_HELVETICA_12,"m: Show/Hide menu");
displayString(78,37,GLUT_BITMAP_HELVETICA_12,"s: Show/Hide second hand");
displayString(78,33,GLUT_BITMAP_HELVETICA_12,"n: Show/Hide number");
displayString(78,29,GLUT_BITMAP_HELVETICA_12,"d: Show/Hide date");
displayString(78,25,GLUT_BITMAP_HELVETICA_12,"f: Show/Hide frame");
displayString(78,21,GLUT_BITMAP_HELVETICA_12,"q, Escape: Exit");
glPopMatrix();
}
}
};

menu *myMenu;

static void
display(void)
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glColor3d(1,0,0);

myClock->display();
myMenu->display();

glutSwapBuffers();
}

static void
key(unsigned char key, int x, int y)
{
switch (key)
{
case 27 :
case 'q':
exit(0);
break;
case 's':
myClock->setSecondVisible(!myClock->getSecondVisible());
break;
case 'n':
myClock->setNumberVisible(!myClock->getNumberVisible());
break;
case 'd':
myClock->setClockdateVisible(!myClock->getClockdateVisible());
break;
case 'f':
myClock->setFrameVisible(!myClock->getFrameVisible());
break;
case 'm':
myMenu->visible=!myMenu->visible;
break;
}

glutPostRedisplay();
}

static void
idle(void)
{
//Sleep(20);
glutPostRedisplay();
myClock->update();
}

int
main(int argc, char *argv[])
{
glutInit(&argc, argv);
myClock = new analogClock();
myMenu = new menu();
//glutInitWindowSize(640,480);
//glutInitWindowPosition(10,10);
glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH);
glutCreateWindow("Analog Clock");
glutFullScreen();
//glutSetCursor(GLUT_CURSOR_NONE);
init();
glutDisplayFunc(display);
glutKeyboardFunc(key);
glutIdleFunc(idle);
glutMainLoop();

return EXIT_SUCCESS;
}


Comments

za said…
There's something missing on for loop on drawing.cpp

f(fabs(dx)>fabs(dy)) steps =int (fabs(dx)*7);
else steps =int (fabs(dy)*7);
xInc = float(dx)/float(steps);
yInc = float(dy)/float(steps);
for(int i=0;i
...?

then jump to render.h

Correction please?
admin purwana said…
hi^^

I'm sorry for any inconvenient.
please download my glut analog clock source to get more information.


Regards,

Popular posts from this blog

Step by Step Spread a Feed Feed Burner

Here is my today pratice how to publish a feed to feedburner. first, i read this tutorial from yadi's log . Btw, thank's for all great blogger that guide me to using feedburner / feed subscriptions with their blog :). lets start! - join feedburner.com . if you already have an account, login with your username . - enter your url feed, and click "activate". Optimize your feedBurner: -click optimize tab. you can setting your feed performance here. for the first, just activate your SmartFeed. publicize feedburner: - click publisize tab and get HTML code for FeedCount feature. don't forget to copy this code. - select PindShot menu. check a Ping-O-Matic and Newsgator. in dorpdown menu, add to 5 other ping service. - activate mail subscription. to activate, click email subscribtion form the left menu,select FeedBurner option, click activate,and get html code for email subscription. Just try It and Share It! reference: "http://jnet99.wordpress.com"

I Support "Busby SEO Test" contestant!

As a blogger , SEO or Search Engine Optimization is one important part to increase your blog rank from the Search Engines . To support SEO Master wins the Busby SEO Test Search Engine Optimization World Cup , here is simple things what i doing to increase my favorites Busby SEO Test participants: - Create a relevant article about Busby SEO Test , - Build a relevant links to your favorites SEO Test participant, - Be careful!, search engines like to increase ranking naturally. don't force to much. The most relevant article from the keywords phrase must be number one. - open links for Busby SEO Test that you like every day. See result link for "Busby SEO Test" . Support your SEO MASTER: 1. Enticegth 2. Kang Noval 3. Gladioolers 4. Imfreakz 5. Izandi 6. Ianimaru 7. Abibakar 8. Dewaji 9. Brokencode 10. SEO SEM 11. Kabonfootprint 12. Hakim Tea I support Busby HakimTea SEO Test (SEO Master from Indonesia) and all seo master above. How about YOU?

New header images for Lazyman bloggers..

Well, today i'm looking for free header images to changes this blog look and feel. as a lazy-bloggers , i don't want to create this task by myself. so, i'd try to search an images header by free from the outside. Amazing, when i put "free header images" on search engine, indo-trans show me a lot of places that share images header for blog by free! . Here is the site that provide free images: - smashing magazine , there are a lot of list compilation. some header using .psd files that you can edit with photoshop , macromedia fireworks, etc. - freewebpageheaders.com , a hundred collection of background header images with 800x200 that can use for bloggers. categorical images help you to search easily. - dreamstime , Just click “Free Images” tab on this site. - www.sxc.hu , Looks like nothing inside. but there are a lot of images that you can see free to download. You can also use it for your blogs. - stock vault , This site is know as 'the stock photo sharing ...