|
Post by Ajhacksu on Jul 3, 2003 3:19:45 GMT -5
hi trad i have an integer and i want to do sth like a IntToStr() and use the string in the command glutSetWindowTitle but i dunno how to do it, strings in C is too complicated for me cuz i'm corrupted by delphi like this br] int i = 1; char *str; sprintf(&str, "%d", i); glutSetWindowTitle (str); it doesnt works... the program crashes... the definition of that function br]GLUTAPI void APIENTRY glutSetWindowTitle(const char *title); ... so any help?
|
|
Trad
New Member
Posts: 1,122
|
Post by Trad on Jul 3, 2003 5:20:42 GMT -5
Well, dont make any function like IntToStr, you can try this..
wsprintf(str,"%d",i); glutSetWindowTitle(str);
i havent really looked into the print functions, but i use wprintf to convert a DWORD to CHAR, so im presuming it will work in your program too.
|
|
|
Post by Ajhacksu on Jul 4, 2003 22:54:08 GMT -5
warning C4700: local variable 'str' used without having been initialized and then program crash thx anyway! /me goes back to delphi
|
|
Trad
New Member
Posts: 1,122
|
Post by Trad on Jul 5, 2003 4:32:05 GMT -5
you shouldnt get that...
hmm.
if you like send me the source and ill get it to work.
|
|
Trad
New Member
Posts: 1,122
|
Post by Trad on Jul 5, 2003 4:32:57 GMT -5
first of all try making str an array of char's not a pointer.
|
|
|
Post by Ajhacksu on Jul 5, 2003 12:46:34 GMT -5
the code is this... its an opengl program i want to put the milisseconds beteween each frame in the caption
#include <windows.h> #include <gl/glut.h> #include <math.h>
void PutPoint (int i) { switch (i) { case 0: glColor3f(1.0, 1.0, 0.0); break; case 1: glColor3f(0.0, 0.0, 1.0); break; case 2: glColor3f(0.5, 0.5, 0.5); break; } glBegin (GL_POINTS); glVertex3f(0.0, 0.0, 0.0); glEnd (); }
int p = 0; // planeta trans int s = 0; // satelite trans
float rx; // mouse rot x float ry; // mouse rot y float rz; // mouse rot z
void glutDisplay (void) { glClear (GL_COLOR_BUFFER_BIT);
glMatrixMode (GL_MODELVIEW); glLoadIdentity (); glTranslatef (0.0, 0.0, -10.0); glRotatef (rx, 1.0, 0.0, 0.0); glRotatef (ry, 0.0, 1.0, 0.0); glRotatef (rz, 0.0, 0.0, 1.0);
glutWireTorus (2.0, 5.0, 10.0, 18.0);
PutPoint (0);
glRotatef(p, 0.0, 0.0, 1.0); p = p++ % 360; glTranslatef(5.0, 0.0, 0.0);
PutPoint (1);
glRotatef(s, 0.0, 0.0, 1.0); s = (s + 5) % 360; glTranslatef(1.0, 0.0, 0.0);
PutPoint (2);
glutSwapBuffers(); }
SYSTEMTIME st0; int i = 1;
void glutIdle (void) { SYSTEMTIME st1; int dt; char str[5];
if (i % 10 == 0) { GetLocalTime(&st1); dt = ( ( (st1.wMinute * 60000) + (st1.wSecond * 1000) + st1.wMilliseconds ) - ( (st0.wMinute * 60000) + (st0.wSecond * 1000) + st0.wMilliseconds ) ) / 10; if (dt < 0) return; wsprintf(str, "msecs: %d", dt); //sprintf(&str, "msecs: %d", dt); glutSetWindowTitle (str); GetLocalTime(&st0); } i++;
glutPostRedisplay (); }
int m; int xo; int yo; int rxo; int ryo; int rzo;
float clamp (float f) { if (f > 360) f -= 360; if (f < 000) f += 360; return f; }
void glutMotion(int x, int y) { if (m == 1) { rx = clamp(rxo - ((yo - y) / 5.0f)); ry = clamp(ryo - ((xo - x) / 5.0f)); } if (m == 2) { rx = clamp(rxo - ((yo - y) / 5.0f)); rz = clamp(rzo - ((xo - x) / 5.0f)); } }
void glutMouse(int button, int state, int x, int y) { if (state == GLUT_DOWN) { if (button == GLUT_LEFT_BUTTON) { m = 1; } if (button == GLUT_RIGHT_BUTTON) { m = 2; } xo = x; yo = y; rxo = rx; ryo = ry; rzo = rz; } else if (state == GLUT_UP) { m = 0; rxo = rx; ryo = ry; rzo = rz; } }
void glutKeyboard (unsigned char key, int x, int y) { switch (key) { case 0x1b: // esc exit (1); break; } }
void glutResize (int w, int h) { if (!h) return;
glViewport (0, 0, w, h);
glMatrixMode (GL_PROJECTION); glLoadIdentity ();
gluPerspective (90, w / h, 1, 1024);
glutPostRedisplay (); }
void glInit (void) { GetLocalTime(&st0); glPointSize(2.5); }
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow) {
int w = 300; int h = 300; int x = (GetSystemMetrics (SM_CXSCREEN) - w) / 2; int y = (GetSystemMetrics (SM_CYSCREEN) - h) / 2;
glutInitDisplayMode (GLUT_RGB | GLUT_DOUBLE); glutInitWindowPosition (x, y); glutInitWindowSize (w, h); glutCreateWindow ("Orbe. Orbiter Particles.");
glutDisplayFunc (glutDisplay); glutIdleFunc (glutIdle); glutMotionFunc (glutMotion); glutMouseFunc (glutMouse); glutKeyboardFunc (glutKeyboard); glutReshapeFunc (glutResize);
glInit ();
glutMainLoop(); // we never return...
return 0; }
|
|
|
Post by Leonardo on Jul 6, 2003 2:23:12 GMT -5
Hi LZM I would do this br] int i=1; char strCad[5]; sprintf(strCad,"%d",i); THe reason that ur program crashes is because str has no memory allocated so u r writing to a location that its either invalid or maybe a location that is being used by another program or something like that Hope helps Leonardo
|
|
Trad
New Member
Posts: 1,122
|
Post by Trad on Jul 6, 2003 6:12:41 GMT -5
you COULD try........
int ticks GetTickCount();
and inbetween the frames... use
'GetTickCount() - ticks'
and that will return the amount of miliseconds from when you set ticks to when you use GetTickCount() - ticks.
|
|
|
Post by Ajhacksu on Jul 6, 2003 12:30:38 GMT -5
Thanks Trad and Leonardo funfou namoral ticks are much better than systemtime int t0;
void glutIdle (void) { int dt; char str[10];
dt = GetTickCount() - t0; sprintf(str, "msecs: %d", dt); glutSetWindowTitle (str); t0 = GetTickCount();
glutPostRedisplay (); } these strings are still confused for me.. i dont know when/why to use array of chars, pointers, array of pointers, pointers to pointers, etc and how to convert them. pascal strings rlz but thanks for the help. deus abençoe vcs:p
|
|
|
Post by Leonardo on Jul 6, 2003 13:35:58 GMT -5
I would say this br] If you are declaring a string u need to create a space for allocation, so u need an array of characters, remember in C/C++ all strings are an array of characters. If you are passing this string, array, to another function, you will receive it as a pointer. If you want to create an array of stings, then u need an array of pointerrs. Pointers to Pointers... u will use this when u want to change the actual pointer within a function other than where u declared the pointer...... let me use an example void f(){ int *p,i; getElements(&p); for(i=0;i<n;i++){ cout << p ; } delete []p; }
void getElements( int **p){ int i,n; cout << "# ELements"; cin >> n; *p=new int[n]; for(i=0;i<n;i++){ cin >> (*p); } }
In this case in the f function p is actually non determinded yet, there is no space allocated for it, therefore u r going to change the actual value of the pointer, thats why u use a pointer to pointer.
Generally for pointer speaking... before putting any information of the pointer.... just ask urself... does the pointer has memory allocated?? And you can answer this question by asking u another 2 questions -Does it has its own memory allocated ( you used new or malloc or any other function that allocates memory dynamically) -It is using the space of another variable, that has its space already allocated
Hope this is clear enough, if not let me know and I will ellaborate more
Leonardo
|
|
|
Post by Ajhacksu on Jul 11, 2003 1:25:07 GMT -5
heh that will help me next time i code a c program freshed my mind about low level variables and pointers was very clear to me big thanks
|
|