• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.
Sign in to follow this  
Followers 0
Vero

consistent Gaming Loops and Clocks

8 posts in this topic

So, I've been going through a lot of OpenGL Tutorials using C code, Also side question, what would you guys recommend coding more complex games in C or C++? I'm not really used to C because work and so many languages I've used before use Objects and Classes if not for utilities than just to keep things organized, that I'm not certain how I would keep a C game from massing huge amounts of global variables and just becoming a mish mash of code. but so far the most useful tutorials I've found have been coded in C.

I created my first really simple game using a tutorial and adding improvements here and there and fixing all the major bugs with stuff that well works and works pretty solid but feels more like duct tape than a nice elegant fix.

Now I think most of my issues stemmed from creating consistent/smooth animationwhen traversing cross points on a grid. I did this by incrementing a position stuct for the player/enemie and then every draw cycle I translated to draw on the current position. this either created kind of choppy movement or too slow a movement. My main question is what are some implementations you would use to create movement on a 2d ortho view using C or even C++. Something that would mostly stay consistent not based on CPU power but just by a clock so it would take X amount of time to get from A to B. My next game is going to just be a simple Maze game maybe a maze editor if I get ambitious with it, but I want to make sure I have some of these concepts down first before I continue.
0

Share this post


Link to post
Share on other sites
In general, you solve these issues by giving your objects a speed, using Units/Time. So, for a generic PC screen, you could use pixels/seconds. Basically, you'd do this (pseudo code):
[code]
uint32_t CurrentMilliseconds = GetTimeInMS();

while(IsGameRunning) {
// store last time
uint32_t PreviousTime = currentMilliseconds;

// get new time
CurrentMilliseconds = GetTimeInMS();

// get elapsed time since last frame
double ElapsedSeconds = (double)(CurrentMilliseconds - PreviousTime)/1000.0f;

UpdateObjects(ElapsedSeconds);
DrawAllObjects();
}

// In an objects Update function
void EnemyObject::Update(double ElapsedSeconds)
{
// move the player based on his speed, make sure position is a double
mPosition.x += mVelocity.x * ElapsedSeconds;
mPosition.y += mVelocity.x * ElapsedSeconds;
mPosition.z += mVelocity.z * ElapsedSeconds;
}

void EnemyObject::Draw()
{
RenderEngine.Draw(Polygon, mPosition.x, mPosition.y, mPosition.z);
}
[/code]

There are more elegant methods of doing this (like not using pixels/second, but your world coordinates), but this is the general idea.
0

Share this post


Link to post
Share on other sites
What do you think the best way to go about implementing a GetTimeInMS()? I found this but this code is from the 2005ish maybe earlier. Also C++ or C is usable, Windows only machines. I just want to attempt to establish an animate that would have a time consistancy as said above.
[code]

struct
{
__int64 frequency; //timer frequency
float resolution; //time resolution
unsigned long mm_timer_start; // Multimedia Timer Start Value
unsigned long mm_timer_elapsed; // Multimedia Timer Elapsed Time
bool performance_timer; //Using the performance timer?
__int64 performance_timer_start; //performance timer start
__int64 performance_timer_elapsed;
} timer;


void TimerInit(void)
{
memset(&timer,0,sizeof(timer)); //clear out timer structure

//check to see if a performance Counter is availble
//if one is availble the timer frenquency will be updated
if(!QueryPerformanceFrequency((LARGE_INTEGER *) &timer.frequency))
{
//no performance counter availble
timer.performance_timer = FALSE;
timer.mm_timer_start = timeGetTime();
timer.resolution = 1.0f/1000.0f;
timer.frequency = 1000;
timer.mm_timer_elapsed = timer.mm_timer_start;
}
else{
//performance coutner is available
QueryPerformanceCounter((LARGE_INTEGER *) &timer.performance_timer_start);
timer.performance_timer = TRUE;
//calculate the Timer Resolution Using the Timer frequency
timer.resolution = (float) (((double)1.0f)/((double)timer.frequency));
//Set Elapsed time to the current time
timer.performance_timer_elapsed=timer.performance_timer_start;
}
}

float TimerGetTime()
{
__int64 time; //time will hold a 64 bit integer

if(timer.performance_timer)
{
QueryPerformanceCounter((LARGE_INTEGER *) &time);
//return the current time minus the start time Multiplied By The Resolution And 1000 (To Get MS)

return ( (float) ( time - timer.performance_timer_start * timer.resolution)*1000);
}
else {
return( (float) ( timeGetTime() - timer.mm_timer_start) * timer.resolution)*1000.0f;
}
}
[/code]

To Recap would this be a good method or are their better implementations out there?
0

Share this post


Link to post
Share on other sites
QueryPerformance counter is one way to do it, but, as you see, it's very involved

timeGetTime() in Windows is much simpler, as it returns time in milliseconds. [url="http://msdn.microsoft.com/en-us/library/windows/desktop/dd757629(v=vs.85).aspx"]link[/url]

So, you can just do this:
[code]
double GetTimeInMS()
{
return (double)timeGetTime()/1000.0f;
}
[/code] Edited by BeerNutts
1

Share this post


Link to post
Share on other sites
Thanks Beernutts, I'm definitely going to keep that snippet of code around for later use. I figured I'd add to the thread to see the outcome of what's happened so far with the game loop implementation. I'm keeping a Psuedo FPS counter what it is really keeping track of is how many Cycles a second it will run through the loop. I say pseudo because without throttling the cycles to 200 cycles a second it was getting over 1000 cycles, I have a goal render with some animation and that was definitely getting some tearing. this might have been due to the limitations of the monitor itself, Anywhere here's a few snippets of time so far implemented in my loop:
[code]
//condensed the performance query

long long milliseconds_now() {
static LARGE_INTEGER s_frequency;
static BOOL s_use_qpc = QueryPerformanceFrequency(&s_frequency);
if (s_use_qpc) {
LARGE_INTEGER now;
QueryPerformanceCounter(&now);
return (1000LL * now.QuadPart) / s_frequency.QuadPart;
} else {
return GetTickCount();
}
}


while(!done) //Game LOOOP!
{
if(PeekMessage(&msg,NULL,0,0,PM_REMOVE)) //is there a message?
{ //peek will not halt program while it looks for a message
if(msg.message==WM_QUIT) // have we received a quit message?
{
done=TRUE;
}
else{ //if not deal with the window messages
TranslateMessage(&msg); //Translate the Message
DispatchMessage(&msg); //Dispatch the Message
}
}
else{ // if there are no messages

// Draw the Scene. Watch for ESC Key and Quit Messages From DrawGLScene()
if((active && !DrawGLScene())|| keys[VK_ESCAPE])
{
done=TRUE; // ESC Signalled a Quit
}
else{ //not time to quit, update screen
//drew scene in the if statement
while(FPS(start,milliseconds_now())>200LL) //throttle FPS
{

/*wait*/}
if(fcount < 60)
{
fps_sum=fps_sum+FPS(start,milliseconds_now());
++fcount;
}
else
{
fps=fps_sum/fcount;
fcount=0;
fps_sum=0LL;
}
start=milliseconds_now();

SwapBuffers(hDC); //SwapBuffers (double buffering)
}
}

...
//end of loop
}

//drawing and stepping in the goal object:


GLvoid Goal::Step(long long elapse)
{
if(startTime==0LL)
startTime = elapse; //start new animation cycle

double ElapsedSeconds = (double)(elapse - startTime)/1000.0f;

spin= Velocity.x*ElapsedSeconds;
}
GLvoid Goal::Draw(){
glLoadIdentity(); // Reset The Modelview Matrix
glTranslatef(fPosition.x,fPosition.y,0.0f); //top left corner
glColor3f(1.0f,0.2f,0.2f); // Make Goal Red
glBegin(GL_LINES); // Start Drawing Goal
glVertex2d( 0 , 0+((int)spin%interval)); // Top Point Of Body
glVertex2d( interval , interval-((int)spin)%interval); // Bottom Right
glVertex2d( interval-((int)spin%interval), 0); // Left Point Of Body
glVertex2d( 0+((int)spin%interval) , interval); // Bottom Point Of Body
glEnd(); // Done Drawing Enemy Body
}

[/code]

this is the first game I've been developing and one of the things I've noticed with a lot of help is people put out a lot of assumptions for an implementation to things.

Basically with the current Time implements I can control the frame rate and the animation speed. so I can have choppy fast smooth fast smooth slow etc. I'm very happy with the result. Now that I have that part mostly set up i can start making the actual player :D
0

Share this post


Link to post
Share on other sites
[quote name='Vero' timestamp='1341264435' post='4955057']
What do you think the best way to go about implementing a GetTimeInMS()?
[/quote]

It's already implemented in the (current) C++ standard library -- in particular, in the <chrono> header:
[url="http://en.cppreference.com/w/cpp/chrono"]http://en.cppreferen...om/w/cpp/chrono[/url]

Depending on what you need use either std::chrono::steady_clock or std::chrono::high_resolution_clock:
[url="http://en.cppreference.com/w/cpp/chrono/steady_clock"]http://en.cppreferen...no/steady_clock[/url]
[url="http://en.cppreference.com/w/cpp/chrono/high_resolution_clock"]http://en.cppreferen...esolution_clock[/url] Edited by Matt-D
1

Share this post


Link to post
Share on other sites
[quote name='wolfscaptain' timestamp='1341384984' post='4955543']
This is worth a read [url="http://gafferongames.com/game-physics/fix-your-timestep/"]http://gafferongames...-your-timestep/[/url]
[/quote]

This is the way to go, buckle down and get into it from the beginning, its a bit confusing at first, but your results will be better in the long run.
0

Share this post


Link to post
Share on other sites
Note that the precision of timeGetTime "can be five milliseconds or more" (MSDN), which is almost a third of an ideal frame.
Visual Studio 11 had a known bug last time I checked (a few months ago) where std::chrono::high_resolution_clock had a precision of only about 4 milliseconds.
0

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0