Move the platform independent stuff from main() into Qcommon_*().

There's no need to duplicate machine independent parts of the client
initialization and the main loop for every platform.

While at it remove the nearly empty unix.h header and move Windows
main() into an own file. Not both platform have the same basic layout.
This commit is contained in:
Yamagi Burmeister 2018-02-03 18:09:19 +01:00
parent bed6439d19
commit acb50c6907
8 changed files with 154 additions and 188 deletions

View file

@ -184,13 +184,10 @@ set(Backends-Unix-Source
${BACKENDS_SRC_DIR}/unix/shared/hunk.c
)
set(Backends-Unix-Header
${BACKENDS_SRC_DIR}/unix/header/unix.h
)
set(Backends-Windows-Source
${BACKENDS_SRC_DIR}/generic/frame.c
${BACKENDS_SRC_DIR}/windows/icon.rc
${BACKENDS_SRC_DIR}/windows/main.c
${BACKENDS_SRC_DIR}/windows/network.c
${BACKENDS_SRC_DIR}/windows/system.c
${BACKENDS_SRC_DIR}/windows/shared/mem.c
@ -213,7 +210,7 @@ if(${CMAKE_SYSTEM_NAME} MATCHES "Windows")
set(Platform-Specific-Source ${Backends-Windows-Source} ${Backends-Windows-Header})
set(REF-Platform-Specific-Source ${REF-Windows-Source})
else()
set(Platform-Specific-Source ${Backends-Unix-Source} ${Backends-Unix-Header})
set(Platform-Specific-Source ${Backends-Unix-Source})
set(REF-Platform-Specific-Source ${REF-Unix-Source})
endif()

View file

@ -863,6 +863,7 @@ CLIENT_OBJS_ := \
ifeq ($(YQ2_OSTYPE), Windows)
CLIENT_OBJS_ += \
src/backends/windows/main.o \
src/backends/windows/network.o \
src/backends/windows/system.o \
src/backends/windows/shared/mem.o
@ -1010,6 +1011,7 @@ SERVER_OBJS_ := \
ifeq ($(YQ2_OSTYPE), Windows)
SERVER_OBJS_ += \
src/backends/windows/main.o \
src/backends/windows/network.o \
src/backends/windows/system.o \
src/backends/windows/shared/mem.o

View file

@ -19,44 +19,32 @@
*
* =======================================================================
*
* This file is the starting point of the program and implements
* the main loop
* This file is the starting point of the program. It does some platform
* specific initialization stuff and calls the common initialization code.
*
* =======================================================================
*/
#include <fcntl.h>
#include <sys/types.h>
#include <time.h>
#include <unistd.h>
#include "../../common/header/common.h"
#include "header/unix.h"
void registerHandler(void);
qboolean is_portable;
int
main(int argc, char **argv)
{
int verLen, i;
long long oldtime, newtime;
const char* versionString;
// Time slept each frame.
#ifndef DEDICATED_ONLY
struct timespec t = {0, 5000};
#else
struct timespec t = {0, 850000};
#endif
/* register signal handler */
// register signal handler
registerHandler();
/* Setup FPU if necessary */
// Setup FPU if necessary
Sys_SetupFPU();
/* Are we portable? */
for (i = 0; i < argc; i++) {
// Are we portable?
for (int i = 0; i < argc; i++) {
if (strcmp(argv[i], "-portable") == 0) {
is_portable = true;
}
@ -73,8 +61,7 @@ main(int argc, char **argv)
return 1;
}
/* Enforce the real UID to
prevent setuid crap */
// Enforce the real UID to prevent setuid crap
if (getuid() != geteuid())
{
printf("The effective UID is not the real UID! Your binary is probably marked\n");
@ -85,73 +72,15 @@ main(int argc, char **argv)
return 1;
}
/* enforce C locale */
// enforce C locale
setenv("LC_ALL", "C", 1);
versionString = va("Yamagi Quake II v%s", YQ2VERSION);
verLen = strlen(versionString);
printf("\n%s\n", versionString);
for(i=0; i<verLen; ++i)
{
putc('=', stdout);
}
puts("\n");
#ifndef DEDICATED_ONLY
printf("Client build options:\n");
#ifdef SDL2
printf(" + SDL2\n");
#else
printf(" - SDL2 (using 1.2)\n");
#endif
#ifdef CDA
printf(" + CD audio\n");
#else
printf(" - CD audio\n");
#endif
#ifdef OGG
printf(" + OGG/Vorbis\n");
#else
printf(" - OGG/Vorbis\n");
#endif
#ifdef USE_OPENAL
printf(" + OpenAL audio\n");
#else
printf(" - OpenAL audio\n");
#endif
#ifdef ZIP
printf(" + Zip file support\n");
#else
printf(" - Zip file support\n");
#endif
#endif
printf("Platform: %s\n", YQ2OSTYPE);
printf("Architecture: %s\n", YQ2ARCH);
/* Seed PRNG */
randk_seed();
/* Initialze the game */
Qcommon_Init(argc, argv);
/* Do not delay reads on stdin*/
/// Do not delay reads on stdin
fcntl(fileno(stdin), F_SETFL, fcntl(fileno(stdin), F_GETFL, NULL) | FNDELAY);
oldtime = Sys_Microseconds();
/* The mainloop. The legend. */
while (1)
{
// Throttle the game a little bit.
nanosleep(&t, NULL);
newtime = Sys_Microseconds();
Qcommon_Frame(newtime - oldtime);
oldtime = newtime;
}
// Initialize the game.
// Never returns.
Qcommon_Init(argc, argv);
return 0;
}

View file

@ -54,7 +54,6 @@
#include "../../common/header/common.h"
#include "../../common/header/glob.h"
#include "../generic/header/input.h"
#include "header/unix.h"
unsigned sys_frame_time;
static void *game_library;
@ -601,3 +600,10 @@ Sys_RedirectStdout(void)
{
return;
}
void
Sys_Nanosleep(int nanosec)
{
struct timespec t = {0, nanosec};
nanosleep(&t, NULL);
}

View file

@ -19,14 +19,48 @@
*
* =======================================================================
*
* A header file for some of the plattform dependend stuff.
* This file is the starting point of the program. It does some platform
* specific initialization stuff and calls the common initialization code.
*
* =======================================================================
*/
#ifndef UNIX_UNIX_H
#define UNIX_UNIX_H
#include <windows.h>
#include <SDL2/SDL.h>
#include <SDL2/SDL_Main.h>
void registerHandler(void);
#include "../../common/header/common.h"
qboolean is_portable;
/*
* Windows main function. Containts the
* initialization code and the main loop
*/
int
main(int argc, char **argv)
{
// Setup FPU if necessary.
Sys_SetupFPU();
// Force DPI awareness.
Sys_SetHighDPIMode();
// Are we portable?
for (int i = 0; i < argc; i++) {
if (strcmp(argv[i], "-portable") == 0) {
is_portable = true;
}
}
// Need to redirect stdout before anything happens.
#ifndef DEDICATED_ONLY
Sys_RedirectStdout();
#endif
// Call the initialization code.
// Never returns.
Qcommon_Init(argc, argv);
return 0;
}

View file

@ -35,9 +35,6 @@
#include <shlobj.h>
#include <windows.h>
#include "SDL2/SDL.h"
#include "SDL2/SDL_Main.h"
#include "../../common/header/common.h"
#include "../generic/header/input.h"
#include "header/resource.h"
@ -702,93 +699,6 @@ Sys_SetHighDPIMode(void)
/* ======================================================================= */
/*
* Windows main function. Containts the
* initialization code and the main loop
*/
int
main(int argc, char *argv[])
{
long long oldtime, newtime;
/* Setup FPU if necessary */
Sys_SetupFPU();
/* Force DPI awareness */
Sys_SetHighDPIMode();
/* Are we portable? */
for (int i = 0; i < argc; i++) {
if (strcmp(argv[i], "-portable") == 0) {
is_portable = true;
}
}
/* Need to redirect stdout before anything happens. */
#ifndef DEDICATED_ONLY
Sys_RedirectStdout();
#endif
printf("Yamagi Quake II v%s\n", YQ2VERSION);
printf("=====================\n\n");
#ifndef DEDICATED_ONLY
printf("Client build options:\n");
#ifdef SDL2
printf(" + SDL2\n");
#else
printf(" - SDL2 (using 1.2)\n");
#endif
#ifdef CDA
printf(" + CD audio\n");
#else
printf(" - CD audio\n");
#endif
#ifdef OGG
printf(" + OGG/Vorbis\n");
#else
printf(" - OGG/Vorbis\n");
#endif
#ifdef USE_OPENAL
printf(" + OpenAL audio\n");
#else
printf(" - OpenAL audio\n");
#endif
#ifdef ZIP
printf(" + Zip file support\n");
#else
printf(" - Zip file support\n");
#endif
#endif
printf("Platform: %s\n", YQ2OSTYPE);
printf("Architecture: %s\n", YQ2ARCH);
/* Seed PRNG */
randk_seed();
/* Call the initialization code */
Qcommon_Init(argc, argv);
/* Save our time */
oldtime = Sys_Microseconds();
/* The legendary main loop */
while (1)
{
// Throttle the game a little bit
Sys_Nanosleep(5000);
newtime = Sys_Microseconds();
Qcommon_Frame(newtime - oldtime);
oldtime = newtime;
}
/* never gets here */
return TRUE;
}
void
Sys_FreeLibrary(void *handle)
{

View file

@ -19,7 +19,7 @@
*
* =======================================================================
*
* Generic frame handling.
* Platform independent initialization, main loop and frame handling.
*
* =======================================================================
*/
@ -71,6 +71,83 @@ void SCR_EndLoadingPlaque(void);
// ----
static void
Qcommon_Buildstring(void)
{
int i;
int verLen;
const char* versionString;
versionString = va("Yamagi Quake II v%s", YQ2VERSION);
verLen = strlen(versionString);
printf("\n%s\n", versionString);
for( i = 0; i < verLen; ++i)
{
printf("=");
}
printf("\n");
#ifndef DEDICATED_ONLY
printf("Client build options:\n");
#ifdef SDL2
printf(" + SDL2\n");
#else
printf(" - SDL2 (using 1.2)\n");
#endif
#ifdef CDA
printf(" + CD audio\n");
#else
printf(" - CD audio\n");
#endif
#ifdef OGG
printf(" + OGG/Vorbis\n");
#else
printf(" - OGG/Vorbis\n");
#endif
#ifdef USE_OPENAL
printf(" + OpenAL audio\n");
#else
printf(" - OpenAL audio\n");
#endif
#ifdef ZIP
printf(" + Zip file support\n");
#else
printf(" - Zip file support\n");
#endif
#endif
printf("Platform: %s\n", YQ2OSTYPE);
printf("Architecture: %s\n", YQ2ARCH);
}
void
Qcommon_Mainloop(void)
{
long long newtime;
long long oldtime = Sys_Microseconds();
/* The mainloop. The legend. */
while (1)
{
// Throttle the game a little bit.
#ifdef DEDICATED_ONLY
Sys_Nanosleep(850000);
#else
Sys_Nanosleep(5000);
#endif
newtime = Sys_Microseconds();
Qcommon_Frame(newtime - oldtime);
oldtime = newtime;
}
}
void
Qcommon_Init(int argc, char **argv)
{
@ -80,6 +157,12 @@ Qcommon_Init(int argc, char **argv)
Sys_Error("Error during initialization");
}
// Print the build and version string
Qcommon_Buildstring();
// Seed PRNG
randk_seed();
// Initialize zone malloc().
z_chain.next = z_chain.prev = &z_chain;
@ -186,6 +269,9 @@ Qcommon_Init(int argc, char **argv)
Com_Printf("==== Yamagi Quake II Initialized ====\n\n");
Com_Printf("*************************************\n\n");
// Call the main loop
Qcommon_Mainloop();
}
#ifndef DEDICATED_ONLY

View file

@ -755,12 +755,13 @@ extern vec3_t bytedirs[NUMVERTEXNORMALS];
/* this is in the client code, but can be used for debugging from server */
void SCR_DebugGraph(float value, int color);
void *Sys_GetGameAPI(void *parms);
/* NON-PORTABLE OSTYPE SERVICES */
void Sys_Init(void);
void Sys_UnloadGame(void);
void *Sys_GetGameAPI(void *parms);
char *Sys_ConsoleInput(void);
void Sys_ConsoleOutput(char *string);
void Sys_SendKeyEvents(void);
@ -774,6 +775,7 @@ void *Sys_LoadLibrary(const char *path, const char *sym, void **handle);
void *Sys_GetProcAddress(void *handle, const char *sym);
void Sys_RedirectStdout(void);
void Sys_SetupFPU(void);
void Sys_Nanosleep(int);
/* CLIENT / SERVER SYSTEMS */