Reworked timer_delay2() and calc_frame_time() to be more accurate and CPU-friendly; -nicefps replayced by -nonicefps to disable sleeping for calc_frame_time; Implemented VSync via SDL; Fixed compilation issues; Code cleanup

This commit is contained in:
zicodxx 2008-04-21 18:39:49 +00:00
parent a40b362ced
commit 96ed7fa827
14 changed files with 51 additions and 103 deletions

View file

@ -1,5 +1,9 @@
D1X-Rebirth Changelog
20080421
--------
main/console.c, main/custom.c, main/inferno.c, main/menu.c, main/gamesave.c, main/config.c, main/config.h, main/game.c, misc/args.c, d1x.ini, arch/ogl/gr.c, arch/sdl/timer.c, arch/win32/netdrv_ipx.c: Reworked timer_delay2() and calc_frame_time() to be more accurate and CPU-friendly; -nicefps replayced by -nonicefps to disable sleeping for calc_frame_time; Implemented VSync via SDL; Fixed compilation issues; Code cleanup
20080419
--------
include/ogl_init.h, include/gr.h, include/strutil.h, include/internal.h, main/network.c, main/menu.c, main/game.c, d1x-rebirth.xcodeproj/project.pbxproj, SConstruct, arch/ogl/ogl.c, arch/ogl/gr.c, arch/sdl/joy.c, arch/sdl/gr.c, arch/carbon/conf.h: Made joy_flush also resetting button state; Improved glReticle; Implemented Fallback resolution if SDL may fail; Code cleanup

View file

@ -326,6 +326,7 @@ int gr_init(int mode)
SDL_GL_SetAttribute(SDL_GL_ACCUM_BLUE_SIZE,0);
SDL_GL_SetAttribute(SDL_GL_ACCUM_ALPHA_SIZE,0);
SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER,1);
SDL_GL_SetAttribute(SDL_GL_SWAP_CONTROL,GameCfg.VSync);
ogl_init_texture_list_internal();

View file

@ -1,9 +1,7 @@
/* $Id: timer.c,v 1.1.1.1 2006/03/17 19:53:40 zicodxx Exp $ */
/*
*
* SDL library timer functions
*
*
*/
#ifdef HAVE_CONFIG_H
@ -36,19 +34,14 @@ void timer_delay(fix seconds)
// Replacement for timer_delay which considers calc time the program needs between frames (not reentrant)
void timer_delay2(int fps)
{
static u_int32_t last_render_time=0;
static u_int32_t FrameStart=0;
u_int32_t FrameLoop=0;
if (last_render_time > SDL_GetTicks()) // Fallback for SDL_GetTicks() wraparound
last_render_time = 0;
else
while (FrameLoop < 1000/fps)
{
int FrameDelay = (1000/fps) // ms to pause between frames for desired FPS rate
- (SDL_GetTicks()-last_render_time)/(F1_0/1000) // Substract the time the game needs to do it's operations
- 10; // Substract 10ms inaccuracy due to OS scheduling
if (FrameDelay > 0)
SDL_Delay(FrameDelay);
last_render_time = SDL_GetTicks();
SDL_Delay(1);
FrameLoop=SDL_GetTicks()-FrameStart;
}
FrameStart=SDL_GetTicks();
}

View file

@ -5,6 +5,7 @@
#include <winsock.h>
#include <wsipx.h>
#include "netdrv.h"
#include "console.h"
static socket_t IPX_sock;
@ -34,7 +35,7 @@ static int IPXOpenSocket(int port)
sock = socket(AF_IPX, SOCK_DGRAM, NSPROTO_IPX);
if (sock == -1) {
con_printf(CON_URGENT,"IPX: could not open IPX socket.\n"));
con_printf(CON_URGENT,"IPX: could not open IPX socket.\n");
return -1;
}
@ -42,7 +43,7 @@ static int IPXOpenSocket(int port)
/* Permit broadcast output */
if (setsockopt(sock, SOL_SOCKET, SO_BROADCAST,(const char *)&opt, sizeof(opt)) == -1)
{
con_printf(CON_URGENT,"IPX: could not set socket option for broadcast.\n"));
con_printf(CON_URGENT,"IPX: could not set socket option for broadcast.\n");
return -1;
}
@ -55,7 +56,7 @@ static int IPXOpenSocket(int port)
/* now bind to this port */
if (bind(sock, (struct sockaddr *) &ipxs, sizeof(ipxs)) == -1)
{
con_printf(CON_URGENT,"IPX: could not bind socket to address\n"));
con_printf(CON_URGENT,"IPX: could not bind socket to address\n");
closesocket( sock );
return -1;
}
@ -63,7 +64,7 @@ static int IPXOpenSocket(int port)
len = sizeof(ipxs2);
if (getsockname(sock,(struct sockaddr *)&ipxs2,&len) < 0)
{
con_printf(CON_URGENT,"IPX: could not get socket name in IPXOpenSocket\n"));
con_printf(CON_URGENT,"IPX: could not get socket name in IPXOpenSocket\n");
closesocket( sock );
return -1;
}
@ -71,7 +72,7 @@ static int IPXOpenSocket(int port)
if (port == 0)
{
port = htons(ipxs2.sa_socket);
con_printf(CON_URGENT,"IPX: opened dynamic socket %04x\n", port));
con_printf(CON_URGENT,"IPX: opened dynamic socket %04x\n", port);
}
memcpy(MyAddress, ipxs2.sa_netnum, 4);
@ -88,7 +89,7 @@ static int IPXOpenSocket(int port)
static void IPXCloseSocket(void)
{
/* now close the file descriptor for the socket, and free it */
con_printf(CON_URGENT,"IPX: closing file descriptor on socket %x\n", IPX_sock.socket));
con_printf(CON_URGENT,"IPX: closing file descriptor on socket %x\n", IPX_sock.socket);
closesocket(IPX_sock.fd);
WSACleanup();
}

View file

@ -1,7 +1,7 @@
System Options:
;-fps Enable FPS indicator by default
;-nicefps Free CPU-cycles. Less CPU load, but game may become choppy
;-nonicefps Don't free CPU-cycles
;-maxfps <n> Set maximum framerate (1-80)
;-hogdir <s> set shared data directory to <dir>
;-nohogdir Don't try to use shared data directory

View file

@ -52,6 +52,7 @@ static char *AspectXStr="AspectX";
static char *AspectYStr="AspectY";
static char *WindowModeStr="WindowMode";
static char *TexFiltStr="TexFilt";
static char *VSyncStr="VSync";
int ReadConfigFile()
{
@ -71,6 +72,7 @@ int ReadConfigFile()
GameCfg.AspectY = 4;
GameCfg.WindowMode = 0;
GameCfg.TexFilt = 0;
GameCfg.VSync = 0;
infile = PHYSFSX_openReadBuffered("descent.cfg");
@ -124,6 +126,8 @@ int ReadConfigFile()
GameCfg.WindowMode = strtol(value, NULL, 10);
else if (!strcmp(token, TexFiltStr))
GameCfg.TexFilt = strtol(value, NULL, 10);
else if (!strcmp(token, VSyncStr))
GameCfg.VSync = strtol(value, NULL, 10);
}
}
@ -164,6 +168,7 @@ int WriteConfigFile()
PHYSFSX_printf(infile, "%s=%i\n", AspectYStr, GameCfg.AspectY);
PHYSFSX_printf(infile, "%s=%i\n", WindowModeStr, GameCfg.WindowMode);
PHYSFSX_printf(infile, "%s=%i\n", TexFiltStr, GameCfg.TexFilt);
PHYSFSX_printf(infile, "%s=%i\n", VSyncStr, GameCfg.VSync);
PHYSFS_close(infile);

View file

@ -38,6 +38,7 @@ typedef struct Cfg
int AspectY;
int WindowMode;
int TexFilt;
int VSync;
} __attribute__ ((packed)) Cfg;
extern struct Cfg GameCfg;

View file

@ -96,7 +96,7 @@ void con_printf(int priority, char *fmt, ...)
t=time(NULL);
lt=localtime(&t);
PHYSFSX_printf(gamelog_fp,"%02i:%02i:%02i ",lt->tm_hour,lt->tm_min,lt->tm_sec);
PHYSFSX_printf(gamelog_fp,"%s",buffer);
PHYSFSX_printf(gamelog_fp,"%s\n",buffer);
}
}
}

View file

@ -230,7 +230,8 @@ int load_pigpog(char *pogname) {
ubyte *p;
CFILE *f;
struct custom_info *custom_info, *cip;
int i, j, x, rc = -1;
int i, j, rc = -1;
unsigned int x = 0;
if (!(f = cfopen((char *)pogname, "rb")))
return -1; // pog file doesn't exist

View file

@ -154,7 +154,6 @@ grs_canvas VR_editor_canvas; // The canvas that the editor writes to.
grs_canvas *VR_offscreen_menu = NULL; // The offscreen data buffer for menus
//end additions -- adb
int Debug_pause=0; //John's debugging pause system
static int old_cockpit_mode=-1;
int force_cockpit_redraw=0;
int netplayerinfo_on=0;
@ -165,10 +164,6 @@ int *Toggle_var = &Dummy_var;
char faded_in;
#endif
#ifndef NDEBUG //these only exist if debugging
fix fixed_frametime=0; //if non-zero, set frametime to this
#endif
int Game_suspended=0; //if non-zero, nothing moves but player
int create_special_path(void);
void fill_background(int x,int y,int w,int h,int dx,int dy);
@ -756,30 +751,21 @@ void reset_time()
void calc_frame_time()
{
static u_int32_t FrameStart=0;
u_int32_t FrameLoop=0;
fix timer_value,last_frametime = FrameTime;
timer_value = timer_get_fixed_seconds();
FrameTime = timer_value - last_timer_value;
// sleep for one frame to free CPU cycles if GameArg.SysUseNiceFPS is active.
if (GameArg.SysUseNiceFPS)
timer_delay2(GameArg.SysMaxFPS);
while (FrameTime < f1_0 / GameArg.SysMaxFPS)
while (FrameLoop < 1000/GameArg.SysMaxFPS)
{
timer_value = timer_get_fixed_seconds();
FrameTime = timer_value - last_timer_value;
if (GameArg.SysUseNiceFPS)
SDL_Delay(1);
FrameLoop=SDL_GetTicks()-FrameStart;
}
#ifndef NDEBUG
if (!(((FrameTime > 0) && (FrameTime <= F1_0)) || (Function_mode == FMODE_EDITOR) || (Newdemo_state == ND_STATE_PLAYBACK))) {
if (FrameTime == 0)
{
FrameTime = 1;
}
FrameStart=SDL_GetTicks();
}
#endif
timer_value = i2f(FrameStart/1000) | fixdiv(i2f(FrameStart % 1000),i2f(1000));
FrameTime = timer_value - last_timer_value;
if ( Game_turbo_mode )
FrameTime *= 2;
@ -788,26 +774,6 @@ void calc_frame_time()
if (FrameTime < 0) //if bogus frametime...
FrameTime = last_frametime; //...then use time from last frame
#ifndef NDEBUG
if (fixed_frametime) FrameTime = fixed_frametime;
#endif
#ifndef NDEBUG
// Pause here!!!
if ( Debug_pause ) {
int c;
c = 0;
while( c==0 )
c = key_peekkey();
if ( c == KEY_P ) {
Debug_pause = 0;
c = key_inkey();
}
last_timer_value = timer_get_fixed_seconds();
}
#endif
}
void move_player_2_segment(segment *seg,int side)
@ -899,14 +865,6 @@ extern fix Cruise_speed;
void game_draw_hud_stuff()
{
#ifndef NDEBUG
if (Debug_pause) {
gr_set_curfont( GAME_FONT );
gr_set_fontcolor( BM_XRGB(31, 31, 31), -1 );
gr_printf( 0x8000, (SHEIGHT/10), "Debug Pause - Press P to exit" );
}
#endif
#ifdef CROSSHAIR
if ( Viewer->type == OBJ_PLAYER )
laser_do_crosshair(Viewer);
@ -2354,7 +2312,6 @@ void HandleGameKey(int key)
break;
case KEY_DEBUGGED+KEY_COMMA: Render_zoom = fixmul(Render_zoom,62259); break;
case KEY_DEBUGGED+KEY_PERIOD: Render_zoom = fixmul(Render_zoom,68985); break;
case KEY_DEBUGGED+KEY_P+KEY_SHIFTED: Debug_pause = 1; break;
case KEY_DEBUGGED+KEY_F8: speedtest_init(); Speedtest_count = 1; break;
case KEY_DEBUGGED+KEY_F9: speedtest_init(); Speedtest_count = 10; break;
case KEY_DEBUGGED+KEY_D:

View file

@ -1057,23 +1057,7 @@ int load_game_data(CFILE *LoadFile)
}
//================ READ CONTROL CENTER TRIGGER INFO ===============
if (game_fileinfo.control_offset > -1)
{
if (!cfseek( LoadFile, game_fileinfo.control_offset,SEEK_SET )) {
for (i=0;i<game_fileinfo.control_howmany;i++)
if ( sizeof(ControlCenterTriggers) == game_fileinfo.control_sizeof ) {
if (control_center_triggers_read_n(&ControlCenterTriggers, 1, LoadFile)!=1)
Error( "Error reading ControlCenterTriggers %i in gamesave.c", i);
} else {
ControlCenterTriggers.num_links = cfile_read_short( LoadFile );
for (j=0; j<MAX_WALLS_PER_LINK; j++ );
ControlCenterTriggers.seg[j] = cfile_read_short( LoadFile );
for (j=0; j<MAX_WALLS_PER_LINK; j++ );
ControlCenterTriggers.side[j] = cfile_read_short( LoadFile );
}
}
}
control_center_triggers_read_n(&ControlCenterTriggers, 1, LoadFile);
//================ READ MATERIALOGRIFIZATIONATORS INFO ===============

View file

@ -137,7 +137,7 @@ void show_commandline_help()
{
printf( "\n System Options:\n\n");
printf( " -fps %s\n", "Enable FPS indicator by default");
printf( " -nicefps %s\n", "Free CPU-cycles. Less CPU load, but game may become choppy");
printf( " -nonicefps %s\n", "Don't free CPU-cycles");
printf( " -maxfps <n> %s\n", "Set maximum framerate (1-80)");
printf( " -hogdir <s> %s\n", "Set shared data directory to <dir>");
printf( " -nohogdir %s\n", "Don't try to use shared data directory");
@ -227,7 +227,6 @@ void sdl_close()
SDL_Quit();
}
extern fix fixed_frametime;
extern void vfx_set_palette_sub(ubyte *);
int MacHog = 0; // using a Mac hogfile?
#define PROGNAME argv[0]

View file

@ -612,19 +612,20 @@ void input_config()
void do_graphics_menu()
{
newmenu_item m[9];
newmenu_item m[10];
int i = 0, j = 0;
do {
m[0].type = NM_TYPE_TEXT; m[0].text="Texture Filtering:";
m[1].type = NM_TYPE_TEXT; m[1].text=" (Requires Game Restart)";
m[2].type = NM_TYPE_RADIO; m[2].text = "None (Classical)"; m[2].value = 0; m[2].group = 0;
m[3].type = NM_TYPE_RADIO; m[3].text = "Bilinear"; m[3].value = 0; m[3].group = 0;
m[4].type = NM_TYPE_RADIO; m[4].text = "Trilinear"; m[4].value = 0; m[4].group = 0;
m[1].type = NM_TYPE_TEXT; m[1].text=" (requires restart)";
m[2].type = NM_TYPE_RADIO; m[2].text = "None (Classical)"; m[2].value = 0; m[2].group = 0;
m[3].type = NM_TYPE_RADIO; m[3].text = "Bilinear"; m[3].value = 0; m[3].group = 0;
m[4].type = NM_TYPE_RADIO; m[4].text = "Trilinear"; m[4].value = 0; m[4].group = 0;
m[5].type = NM_TYPE_TEXT; m[5].text="";
m[6].type = NM_TYPE_CHECK; m[6].text="Transparency Effects"; m[6].value = PlayerCfg.OglAlphaEffects;
m[7].type = NM_TYPE_CHECK; m[7].text="Vectorial Reticle"; m[7].value = PlayerCfg.OglReticle;
m[8].type = NM_TYPE_CHECK; m[8].text="Screenshots w/o HUD"; m[8].value = PlayerCfg.OglPRShot;
m[6].type = NM_TYPE_CHECK; m[6].text="Transparency Effects"; m[6].value = PlayerCfg.OglAlphaEffects;
m[7].type = NM_TYPE_CHECK; m[7].text="Vectorial Reticle"; m[7].value = PlayerCfg.OglReticle;
m[8].type = NM_TYPE_CHECK; m[8].text="Screenshots w/o HUD"; m[8].value = PlayerCfg.OglPRShot;
m[9].type = NM_TYPE_CHECK; m[9].text="VSync (requires restart)"; m[9].value = GameCfg.VSync;
m[GameCfg.TexFilt+2].value=1;
@ -636,6 +637,7 @@ void do_graphics_menu()
PlayerCfg.OglAlphaEffects = m[6].value;
PlayerCfg.OglReticle = m[7].value;
PlayerCfg.OglPRShot = m[8].value;
GameCfg.VSync = m[9].value;
} while( i>-1 );
}

View file

@ -125,7 +125,7 @@ void ReadCmdArgs(void)
GameArg.SysShowCmdHelp = (FindArg( "-help" ) || FindArg( "-h" ) || FindArg( "-?" ) || FindArg( "?" ));
GameArg.SysFPSIndicator = FindArg("-fps");
GameArg.SysUseNiceFPS = FindArg("-nicefps");
GameArg.SysUseNiceFPS = !FindArg("-nonicefps");
GameArg.SysMaxFPS = get_int_arg("-maxfps", MAXIMUM_FPS);
if (GameArg.SysMaxFPS <= 0 || GameArg.SysMaxFPS > MAXIMUM_FPS)