CC2
Game Loop
Game-Loop Grundgerüst
noch unvollständig und unmodularisiert,
d.h. nur als Orientierungsgrundlage für Ihr Spiel
#include <ncurses.h>
#include <unistd.h>
#include <string.h>
// game constant: frames per second
static const float fps = 5;
// game state: actual frame number
static int frame = 0;
// game state: elapsed time
static double elapsed = 0;
// definition of game phases
enum GAME_STATE
{
GAME_INTRO = 0,
GAME_LOOP = 1
};
// game state: actual game phase
static GAME_STATE state = GAME_INTRO;
// prototype of getter function
double get_elapsed();
// prototype of helper function
void msleep(float ms);
// render a single frame
void render_frame()
{
// render the actual frame depending on the state
if (state == GAME_INTRO)
{
// clear the actual frame
clear();
// draw intro text
const char text[] = "Intro"; // intro text to be rendered
mvprintw((LINES-1)/2, (COLS-1)/2-strlen(text)/2, text); // render centered text
// your own intro code
// ...
}
else if (state == GAME_LOOP)
{
// just flash the screen as a starting point
flash();
// your own game code
// ...
}
// refresh the actual frame
refresh();
}
// update the game state
// * 'q' quits the game
bool update_state()
{
// state check cascade
if (state == GAME_INTRO)
{
if (get_elapsed() > 3)
{
state = GAME_LOOP;
clear();
}
}
else if (state == GAME_LOOP)
{
int c = getch();
if (c == 'q')
return(true);
else if (c == 'b')
beep();
}
return(false);
}
// the game loop
// * it renders frame after frame
// * updates the state after each frame
// * and sleeps for the remainder of the frame
void game_loop()
{
float dt = 1/fps; // frame duration
float ms = dt*1000; // milli seconds
// your own initializations
// ...
// the main game loop
while (true)
{
// render a single frame
render_frame();
// update frame counter and elapsed time
frame++;
elapsed += dt;
// update the game state
bool finish = update_state();
// sleep for the remainder of the frame
msleep(ms);
// check for game finish
if (finish) break;
}
}
// get elapsed time
double get_elapsed()
{
return(elapsed);
}
// sleep for a period of time given in milli seconds (ms)
void msleep(float ms)
{
int us = ms*1000; // micro secs
usleep(us);
}
// main
int main()
{
initscr();
curs_set(FALSE);
timeout(0);
game_loop();
endwin();
printf("elapsed time: %f\n", get_elapsed());
return(0);
}
#include <unistd.h>
#include <string.h>
// game constant: frames per second
static const float fps = 5;
// game state: actual frame number
static int frame = 0;
// game state: elapsed time
static double elapsed = 0;
// definition of game phases
enum GAME_STATE
{
GAME_INTRO = 0,
GAME_LOOP = 1
};
// game state: actual game phase
static GAME_STATE state = GAME_INTRO;
// prototype of getter function
double get_elapsed();
// prototype of helper function
void msleep(float ms);
// render a single frame
void render_frame()
{
// render the actual frame depending on the state
if (state == GAME_INTRO)
{
// clear the actual frame
clear();
// draw intro text
const char text[] = "Intro"; // intro text to be rendered
mvprintw((LINES-1)/2, (COLS-1)/2-strlen(text)/2, text); // render centered text
// your own intro code
// ...
}
else if (state == GAME_LOOP)
{
// just flash the screen as a starting point
flash();
// your own game code
// ...
}
// refresh the actual frame
refresh();
}
// update the game state
// * 'q' quits the game
bool update_state()
{
// state check cascade
if (state == GAME_INTRO)
{
if (get_elapsed() > 3)
{
state = GAME_LOOP;
clear();
}
}
else if (state == GAME_LOOP)
{
int c = getch();
if (c == 'q')
return(true);
else if (c == 'b')
beep();
}
return(false);
}
// the game loop
// * it renders frame after frame
// * updates the state after each frame
// * and sleeps for the remainder of the frame
void game_loop()
{
float dt = 1/fps; // frame duration
float ms = dt*1000; // milli seconds
// your own initializations
// ...
// the main game loop
while (true)
{
// render a single frame
render_frame();
// update frame counter and elapsed time
frame++;
elapsed += dt;
// update the game state
bool finish = update_state();
// sleep for the remainder of the frame
msleep(ms);
// check for game finish
if (finish) break;
}
}
// get elapsed time
double get_elapsed()
{
return(elapsed);
}
// sleep for a period of time given in milli seconds (ms)
void msleep(float ms)
{
int us = ms*1000; // micro secs
usleep(us);
}
// main
int main()
{
initscr();
curs_set(FALSE);
timeout(0);
game_loop();
endwin();
printf("elapsed time: %f\n", get_elapsed());
return(0);
}
Der obige Code sollte in die Module
- util.cpp/.h für msleep(),
- game.cpp/.h für game_loop(), render_frame(), update_state() und get_elapsed()
- und main.cpp für main()
aufgeteilt werden.