Pages: [1]
  Print  
Author Topic: extern double returns 0  (Read 1349 times)
Offline (Unknown gender) TheExDeus
Posted on: November 25, 2010, 08:23:13 AM

Developer
Joined: Apr 2008
Posts: 1914

View Profile
Solved: No need to read this unless you're bored.

I am playing with some curves (sounds dirty doesn't it?) and I have a problem almost totally unrelated to that. For some eye candy I usually use cos/sin and current_time to get oscillation. I used current_time and it always returned 0, then I found out that actually current_time is not defined anywhere (and Enigma doesn't report non existing variables, they just assume 0, and that is bad (people don't suggest to use "Threat undefined variables as 0" for a reason)). Anyway, I went to implement it myself thinking it is easy. Found key_game_globals.h, and uncommented "extern double      current_time;". Then went into WINDOWSmain.cpp and modified a small part to this:
Code: [Select]
extern double fps, current_time;
namespace enigma {
  clock_t lc;
  void sleep_for_framerate(int rs)
  {
    clock_t nc = clock();
    int sdur = 1000/rs - 1 - (nc - lc)*1000 / CLOCKS_PER_SEC;
    if (sdur > 0)
    {
        Sleep(sdur);
        fps = room_speed;
    }
    else
    {
        fps = CLOCKS_PER_SEC / (nc - lc);
        if(fps > room_speed){ fps = room_speed; }
    }
    lc = nc;
    current_time = clock() / double(CLOCKS_PER_SEC) * 1000;
  }
}
So now current_time holds ms till the program launched. In GM it actually holds the number of ms the system has started, but whatever. Now whenever I use "extern double current_time;" in .cpp I can get that number and it works great. But the problem is that when I use string(current_time) in Enigma it just still returns 0. Did I miss a place to put it?
I also uncommented the line in GAME_GLOBALS.h to "double      current_time;".

edit: I did everything right. Just didn't occur to me to restart LGM (I know I need to do this when I update header files, but not when I update .cpp files).
« Last Edit: November 25, 2010, 08:42:58 AM by HaRRiKiRi » Logged
Offline (Male) Josh @ Dreamland
Reply #1 Posted on: November 25, 2010, 09:26:50 AM

Prince of all Goldfish
Developer
Location: Ohio, United States
Joined: Feb 2008
Posts: 2946

View Profile Email
Restarting LGM shouldn't really have fixed that. Your current_time actually doesn't behave like GM's at all; GM's (as you said) returns milliseconds since boot, but it is also an accessor. What I mean is, you can use current_time to time operations in a script.

I'll see about implementing that one... it won't be as easy to mimic in C++ as extern double current_time;. :P
Logged
"That is the single most cryptic piece of code I have ever seen." -Master PobbleWobble
"I disapprove of what you say, but I will defend to the death your right to say it." -Evelyn Beatrice Hall, Friends of Voltaire
Offline (Unknown gender) TheExDeus
Reply #2 Posted on: November 25, 2010, 10:02:01 AM

Developer
Joined: Apr 2008
Posts: 1914

View Profile
Yes, I am aware that it is not the same. But it works for my applications, thou I could just use a regular variable I guess.
Anyway, I am working on Bezier curves and slopes. Inspired by this:
http://gmc.yoyogames.com/index.php?showtopic=491339
He also had made a very great example to showcase the beauty.
Here is how it looks now in Enigma:
http://www.host-a.net/u/harrikiri/curvesexample.zip
I haven't created any spline functions, just Bezier. When I get a good grasp on how splines work I will implement thous too. When everything's ready I will release the code.

Also, the number in the corner is the segment number. More segments mean smoother line, but of course, slower drawing. You can increase/decrease them with up and down arrows on your keyboard. Click to move it to the mouse.

edit: And btw, restarting LGM did fix it. I haven't added any new modifications. I changed everything I mentioned.
« Last Edit: November 25, 2010, 10:06:15 AM by HaRRiKiRi » Logged
Offline (Unknown gender) r9k
Reply #3 Posted on: November 27, 2010, 12:01:29 PM
Member
Joined: Aug 2010
Posts: 25

View Profile
Very nice curves. Nice work.
Logged
Offline (Unknown gender) TheExDeus
Reply #4 Posted on: November 27, 2010, 03:00:52 PM

Developer
Joined: Apr 2008
Posts: 1914

View Profile
Thanks. I have create splines, but I think I use some very bad method. But that was the only method I could easily grasp. Thou that method allows making a spline with infinite points, so that's nice. Now I am thinking of some method to allow user to pass thous points. I can't use ds_lists as LSnK does (the creator of curves extension), because they are not implemented yet (at least I think so). I could use _begin, _add_point(x,y), and _end thou. So I am trying to do so.
Logged
Pages: [1]
  Print