Browse Source

Fix CR/LF inconsistencies

master
Peter Nelson 7 years ago
parent
commit
a36e723848
  1. 400
      audio.c
  2. 34
      audio.h
  3. 52
      cars.c
  4. 30
      config.c
  5. 4
      config.h
  6. 98
      gauge.c
  7. 22
      gauge.h
  8. 162
      gettime.h
  9. 4474
      insim.h
  10. 234
      lfsdash.c
  11. 50
      network_worker.c
  12. 2
      network_worker.h
  13. 18
      outgauge.h
  14. 72
      socket.c
  15. 20
      socket.h
  16. 48
      text.c
  17. 32
      text.h
  18. 14
      worker.c

400
audio.c

@ -1,200 +1,200 @@
#include <stdio.h>
#include <stdlib.h>
//#include <GL/glut.h>
#include <AL/al.h>
#include <AL/alc.h>
#include <string.h>
#include <stdint.h>
#include "audio.h"
#define NUM_BUFFERS 5
#define NUM_SOURCES 6
#define NUM_ENVIRONMENTS 1
ALCdevice *dev;
ALCcontext *ctx;
//There is only one listener, and these arrays define the initial set up of the listener for its position, direction, and velocity. The listener is not moving at the start, but can move around any other sound source.
ALfloat listenerPos[]={0.0,0.0,4.0};
ALfloat listenerVel[]={0.0,0.0,0.0};
ALfloat listenerOri[]={0.0,0.0,1.0, 0.0,1.0,0.0};
//Each sound source has similar properties that the listener has. The source0Pos and source0Vel arrays show the position and velocity of the sound source.
ALfloat source0Pos[]={ -2.0, 0.0, 0.0};
ALfloat source0Vel[]={ 0.0, 0.0, 0.0};
//Sounds need to be stored in an array, similar to textures. Several buffers are needed to contain the sounds and other necessary information. The size, freq, format, and data variables are used when loading the sound files.
ALuint buffer[NUM_BUFFERS];
ALuint source[NUM_SOURCES];
ALuint environment[NUM_ENVIRONMENTS];
int file_read_int32_le(char *buffer, FILE *file)
{
int r = fread(buffer, sizeof(char), 4, file);
uint32_t ret = (uint8_t)buffer[0];
ret |= (uint8_t)buffer[1] << 8;
ret |= (uint8_t)buffer[2] << 16;
ret |= (uint8_t)buffer[3] << 24;
return ret;
}
int file_read_int16_le(char *buffer, FILE *file)
{
int r = fread(buffer, sizeof(char), 2, file);
uint16_t ret = (uint8_t)buffer[0];
ret |= (uint8_t)buffer[1] << 8;
return ret;
}
void file_ignore_bytes(FILE *file, int bytes)
{
fseek(file, bytes, SEEK_CUR);
}
void *file_allocate_and_read_bytes(FILE *file, size_t bytes)
{
void *buffer = malloc(bytes);
fread(buffer, 1, bytes, file);
return buffer;
}
static inline ALenum GetFormatFromInfo(short channels, short bitsPerSample) {
if (channels == 1)
return AL_FORMAT_MONO16;
return AL_FORMAT_STEREO16;
}
void audio_load(const char *filename, ALuint buffer)
{
FILE* file = fopen(filename, "rb");
char xbuffer[5];
memset(xbuffer, 0, sizeof xbuffer);
if (fread(xbuffer, sizeof(char), 4, file) != 4 || strcmp(xbuffer, "RIFF") != 0)
printf("Not a WAV file: %s\n", xbuffer);
file_read_int32_le(xbuffer, file);
if (fread(xbuffer, sizeof(char), 4, file) != 4 || strcmp(xbuffer, "WAVE") != 0)
printf("Not a WAV file: %s\n", xbuffer);
if (fread(xbuffer, sizeof(char), 4, file) != 4 || strcmp(xbuffer, "fmt ") != 0)
printf("Invalid WAV file: %s\n", xbuffer);
int size = file_read_int32_le(xbuffer, file);
short audioFormat = file_read_int16_le(xbuffer, file);
short channels = file_read_int16_le(xbuffer, file);
int sampleRate = file_read_int32_le(xbuffer, file);
int byteRate = file_read_int32_le(xbuffer, file);
file_read_int16_le(xbuffer, file);
short bitsPerSample = file_read_int16_le(xbuffer, file);
size -= 16;
file_ignore_bytes(file, size);
if (fread(xbuffer, sizeof(char), 4, file) != 4 || strcmp(xbuffer, "data") != 0)
printf("Invalid WAV file: %s\n", xbuffer);
int dataChunkSize = file_read_int32_le(xbuffer, file);
unsigned char* bufferData = file_allocate_and_read_bytes(file, (size_t) dataChunkSize);
float duration = (float)(dataChunkSize) / byteRate;
alBufferData(buffer, GetFormatFromInfo(channels, bitsPerSample), bufferData, dataChunkSize, sampleRate);
free(bufferData);
fclose(file);
}
void audio_init(void)
{
dev = alcOpenDevice(NULL);
if (!dev)
{
fprintf(stderr, "Oops\n");
return;
}
ctx = alcCreateContext(dev, NULL);
alcMakeContextCurrent(ctx);
if (!ctx)
{
fprintf(stderr, "Oops2\n");
return;
}
alListenerfv(AL_POSITION,listenerPos);
alListenerfv(AL_VELOCITY,listenerVel);
alListenerfv(AL_ORIENTATION,listenerOri);
alGetError(); // clear any error messages
// Generate buffers, or else no sound will happen!
alGenBuffers(NUM_BUFFERS, buffer);
if (alGetError() != AL_NO_ERROR) {
printf("- Error creating buffers !!\n");
exit(1);
}
audio_load("off.wav", buffer[FX_OFF]);
audio_load("on.wav", buffer[FX_ON]);
audio_load("bing.wav", buffer[FX_BING]);
audio_load("bip.wav", buffer[FX_BIP]);
audio_load("bop.wav", buffer[FX_BOP]);
alGetError(); /* clear error */
alGenSources(NUM_SOURCES, source);
if (alGetError() != AL_NO_ERROR)
{
printf("- Error creating sources !!\n");
exit(2);
}
int ii;
for (ii = 0; ii < NUM_SOURCES; ii++)
{
alSourcef(source[ii], AL_PITCH, 1.0f);
alSourcef(source[ii], AL_GAIN, 1.0f);
alSourcefv(source[ii], AL_POSITION, source0Pos);
alSourcefv(source[ii], AL_VELOCITY, source0Vel);
//alSourcei(source[0], AL_BUFFER, buffer[0]);
alSourcei(source[ii], AL_LOOPING, AL_FALSE);
}
}
void audio_deinit(void)
{
int ii;
for (ii = 0; ii < NUM_SOURCES; ii++)
{
alSourceStop(source[ii]);
}
alDeleteSources(NUM_SOURCES, source);
alDeleteBuffers(NUM_BUFFERS, buffer);
alcMakeContextCurrent(NULL);
alcDestroyContext(ctx);
alcCloseDevice(dev);
}
void audio_volume(float f)
{
int ii;
for (ii = 0; ii < NUM_SOURCES; ii++)
{
alSourcef(source[ii], AL_GAIN, f);
}
}
float frand(float mult)
{
return ((float)rand() / (float)RAND_MAX) * mult;
}
void audio_play(int s, int b)
{
alSourceStop(source[s]);
alSourcei(source[s], AL_BUFFER, buffer[b]);
alSourcePlay(source[s]);
}
#include <stdio.h>
#include <stdlib.h>
//#include <GL/glut.h>
#include <AL/al.h>
#include <AL/alc.h>
#include <string.h>
#include <stdint.h>
#include "audio.h"
#define NUM_BUFFERS 5
#define NUM_SOURCES 6
#define NUM_ENVIRONMENTS 1
ALCdevice *dev;
ALCcontext *ctx;
//There is only one listener, and these arrays define the initial set up of the listener for its position, direction, and velocity. The listener is not moving at the start, but can move around any other sound source.
ALfloat listenerPos[]={0.0,0.0,4.0};
ALfloat listenerVel[]={0.0,0.0,0.0};
ALfloat listenerOri[]={0.0,0.0,1.0, 0.0,1.0,0.0};
//Each sound source has similar properties that the listener has. The source0Pos and source0Vel arrays show the position and velocity of the sound source.
ALfloat source0Pos[]={ -2.0, 0.0, 0.0};
ALfloat source0Vel[]={ 0.0, 0.0, 0.0};
//Sounds need to be stored in an array, similar to textures. Several buffers are needed to contain the sounds and other necessary information. The size, freq, format, and data variables are used when loading the sound files.
ALuint buffer[NUM_BUFFERS];
ALuint source[NUM_SOURCES];
ALuint environment[NUM_ENVIRONMENTS];
int file_read_int32_le(char *buffer, FILE *file)
{
int r = fread(buffer, sizeof(char), 4, file);
uint32_t ret = (uint8_t)buffer[0];
ret |= (uint8_t)buffer[1] << 8;
ret |= (uint8_t)buffer[2] << 16;
ret |= (uint8_t)buffer[3] << 24;
return ret;
}
int file_read_int16_le(char *buffer, FILE *file)
{
int r = fread(buffer, sizeof(char), 2, file);
uint16_t ret = (uint8_t)buffer[0];
ret |= (uint8_t)buffer[1] << 8;
return ret;
}
void file_ignore_bytes(FILE *file, int bytes)
{
fseek(file, bytes, SEEK_CUR);
}
void *file_allocate_and_read_bytes(FILE *file, size_t bytes)
{
void *buffer = malloc(bytes);
fread(buffer, 1, bytes, file);
return buffer;
}
static inline ALenum GetFormatFromInfo(short channels, short bitsPerSample) {
if (channels == 1)
return AL_FORMAT_MONO16;
return AL_FORMAT_STEREO16;
}
void audio_load(const char *filename, ALuint buffer)
{
FILE* file = fopen(filename, "rb");
char xbuffer[5];
memset(xbuffer, 0, sizeof xbuffer);
if (fread(xbuffer, sizeof(char), 4, file) != 4 || strcmp(xbuffer, "RIFF") != 0)
printf("Not a WAV file: %s\n", xbuffer);
file_read_int32_le(xbuffer, file);
if (fread(xbuffer, sizeof(char), 4, file) != 4 || strcmp(xbuffer, "WAVE") != 0)
printf("Not a WAV file: %s\n", xbuffer);
if (fread(xbuffer, sizeof(char), 4, file) != 4 || strcmp(xbuffer, "fmt ") != 0)
printf("Invalid WAV file: %s\n", xbuffer);
int size = file_read_int32_le(xbuffer, file);
short audioFormat = file_read_int16_le(xbuffer, file);
short channels = file_read_int16_le(xbuffer, file);
int sampleRate = file_read_int32_le(xbuffer, file);
int byteRate = file_read_int32_le(xbuffer, file);
file_read_int16_le(xbuffer, file);
short bitsPerSample = file_read_int16_le(xbuffer, file);
size -= 16;
file_ignore_bytes(file, size);
if (fread(xbuffer, sizeof(char), 4, file) != 4 || strcmp(xbuffer, "data") != 0)
printf("Invalid WAV file: %s\n", xbuffer);
int dataChunkSize = file_read_int32_le(xbuffer, file);
unsigned char* bufferData = file_allocate_and_read_bytes(file, (size_t) dataChunkSize);
float duration = (float)(dataChunkSize) / byteRate;
alBufferData(buffer, GetFormatFromInfo(channels, bitsPerSample), bufferData, dataChunkSize, sampleRate);
free(bufferData);
fclose(file);
}
void audio_init(void)
{
dev = alcOpenDevice(NULL);
if (!dev)
{
fprintf(stderr, "Oops\n");
return;
}
ctx = alcCreateContext(dev, NULL);
alcMakeContextCurrent(ctx);
if (!ctx)
{
fprintf(stderr, "Oops2\n");
return;
}
alListenerfv(AL_POSITION,listenerPos);
alListenerfv(AL_VELOCITY,listenerVel);
alListenerfv(AL_ORIENTATION,listenerOri);
alGetError(); // clear any error messages
// Generate buffers, or else no sound will happen!
alGenBuffers(NUM_BUFFERS, buffer);
if (alGetError() != AL_NO_ERROR) {
printf("- Error creating buffers !!\n");
exit(1);
}
audio_load("off.wav", buffer[FX_OFF]);
audio_load("on.wav", buffer[FX_ON]);
audio_load("bing.wav", buffer[FX_BING]);
audio_load("bip.wav", buffer[FX_BIP]);
audio_load("bop.wav", buffer[FX_BOP]);
alGetError(); /* clear error */
alGenSources(NUM_SOURCES, source);
if (alGetError() != AL_NO_ERROR)
{
printf("- Error creating sources !!\n");
exit(2);
}
int ii;
for (ii = 0; ii < NUM_SOURCES; ii++)
{
alSourcef(source[ii], AL_PITCH, 1.0f);
alSourcef(source[ii], AL_GAIN, 1.0f);
alSourcefv(source[ii], AL_POSITION, source0Pos);
alSourcefv(source[ii], AL_VELOCITY, source0Vel);
//alSourcei(source[0], AL_BUFFER, buffer[0]);
alSourcei(source[ii], AL_LOOPING, AL_FALSE);
}
}
void audio_deinit(void)
{
int ii;
for (ii = 0; ii < NUM_SOURCES; ii++)
{
alSourceStop(source[ii]);
}
alDeleteSources(NUM_SOURCES, source);
alDeleteBuffers(NUM_BUFFERS, buffer);
alcMakeContextCurrent(NULL);
alcDestroyContext(ctx);
alcCloseDevice(dev);
}
void audio_volume(float f)
{
int ii;
for (ii = 0; ii < NUM_SOURCES; ii++)
{
alSourcef(source[ii], AL_GAIN, f);
}
}
float frand(float mult)
{
return ((float)rand() / (float)RAND_MAX) * mult;
}
void audio_play(int s, int b)
{
alSourceStop(source[s]);
alSourcei(source[s], AL_BUFFER, buffer[b]);
alSourcePlay(source[s]);
}

34
audio.h

@ -1,17 +1,17 @@
#ifndef AUDIO_H
#define AUDIO_H
enum {
FX_ON,
FX_OFF,
FX_BING,
FX_BIP,
FX_BOP,
};
void audio_init(void);
void audio_deinit(void);
void audio_volume(float f);
void audio_play(int s, int on);
#endif /* AUDIO_H */
#ifndef AUDIO_H
#define AUDIO_H
enum {
FX_ON,
FX_OFF,
FX_BING,
FX_BIP,
FX_BOP,
};
void audio_init(void);
void audio_deinit(void);
void audio_volume(float f);
void audio_play(int s, int on);
#endif /* AUDIO_H */

52
cars.c

@ -3,32 +3,32 @@
#include <stdio.h>
#include <string.h>
struct car s_cars[MAX_CARS];
#ifdef WIN32
#include <stddef.h>
#include <string.h>
#include <stdio.h>
char* strsep(char** stringp, const char* delim)
{
char* start = *stringp;
char* p;
p = (start != NULL) ? strpbrk(start, delim) : NULL;
if (p == NULL)
{
*stringp = NULL;
}
else
{
*p = '\0';
*stringp = p + 1;
}
return start;
}
struct car s_cars[MAX_CARS];
#ifdef WIN32
#include <stddef.h>
#include <string.h>
#include <stdio.h>
char* strsep(char** stringp, const char* delim)
{
char* start = *stringp;
char* p;
p = (start != NULL) ? strpbrk(start, delim) : NULL;
if (p == NULL)
{
*stringp = NULL;
}
else
{
*p = '\0';
*stringp = p + 1;
}
return start;
}
#endif
void init_cars(void)

30
config.c

@ -128,8 +128,8 @@ void config_set_int(const char *setting, int value)
char c[MAXCONFIGLEN];
snprintf(c, sizeof c, "%d", value);
config_set_string(setting, c);
}
}
void config_set_float(const char *setting, float value)
{
char c[MAXCONFIGLEN];
@ -167,16 +167,16 @@ int config_get_int(const char *setting, int *value)
return 1;
}
int config_get_float(const char *setting, float *value)
{
char *c, *endptr;
float v;
if (!config_get_string(setting, &c)) return 0;
v = strtof(c, &endptr);
if (*endptr != '\0') return 0;
*value = v;
return 1;
}
int config_get_float(const char *setting, float *value)
{
char *c, *endptr;
float v;
if (!config_get_string(setting, &c)) return 0;
v = strtof(c, &endptr);
if (*endptr != '\0') return 0;
*value = v;
return 1;
}

4
config.h

@ -5,11 +5,11 @@ void config_init(const char *filename);
void config_deinit(void);
void config_set_string(const char *setting, const char *value);
void config_set_int(const char *setting, int value);
void config_set_int(const char *setting, int value);
void config_set_float(const char *setting, float value);
int config_get_string(const char *setting, char **value);
int config_get_int(const char *setting, int *value);
int config_get_int(const char *setting, int *value);
int config_get_float(const char *setting, float *value);
#endif /* CONFIG_H */

98
gauge.c

@ -2,8 +2,8 @@
#include <FTGL/ftgl.h>
#include <math.h>
#include <stdio.h>
#include "gauge.h"
#include "text.h"
#include "gauge.h"
#include "text.h"
void init_gauges(void)
{
@ -73,55 +73,55 @@ void DrawArc(float inner, float outer, float start_angle, float arc_angle, int n
y = s * t + c * y;
}
glEnd();
}*/
void drawDial(const struct gauge *gauge, float value, int style)
{
}*/
void drawDial(const struct gauge *gauge, float value, int style)
{
float range = gauge->rangemax - gauge->rangemin;
float angle = gauge->anglemax - gauge->anglemin;
float cura = (value - gauge->rangemin) / range * angle + gauge->anglemin;
switch (style)
{
case GT_NONE:
break;
case GT_BAR:
glColor4f(gauge->dial.r, gauge->dial.g, gauge->dial.b, gauge->dial.a);
DrawArc(gauge->majorstart, gauge->majorend, cura, cura - gauge->anglemin, 10);
break;
case GT_LINE2:
glColor4f(gauge->dial.r, gauge->dial.g, gauge->dial.b, gauge->dial.a);
float angle = gauge->anglemax - gauge->anglemin;
float cura = (value - gauge->rangemin) / range * angle + gauge->anglemin;
switch (style)
{
case GT_NONE:
break;
case GT_BAR:
glColor4f(gauge->dial.r, gauge->dial.g, gauge->dial.b, gauge->dial.a);
DrawArc(gauge->majorstart, gauge->majorend, cura, cura - gauge->anglemin, 10);
break;
case GT_LINE2:
glColor4f(gauge->dial.r, gauge->dial.g, gauge->dial.b, gauge->dial.a);
glBegin(GL_LINES);
glVertex3f(0, 0, 0);
glVertex3f(sin(cura), cos(cura), 0);
glEnd();
glEnd();
break;
case GT_LINE:
glColor4f(1.0, 0.0, 0.0, 1.0);
case GT_LINE:
glColor4f(1.0, 0.0, 0.0, 1.0);
glBegin(GL_LINES);
glVertex3f(sin(cura) * 0.8, cos(cura) * 0.8, 0);
glVertex3f(sin(cura), cos(cura), 0);
glEnd();
break;
default:
case GT_NEEDLE:
glColor4f(gauge->dial.r, gauge->dial.g, gauge->dial.b, gauge->dial.a);
glRotatef(cura * -180.0 / M_PI, 0.0, 0.0, 1.0);
glBegin(GL_TRIANGLE_STRIP);
//glVertex3f(-0.01, -0.04, 0.0);
//glVertex3f(0.01, -0.04, 0.0);
//glVertex3f(-0.02, 0, 0.0);
//glVertex3f(0.02, 0.0, 0.0);
//glVertex3f(-0.005, 1.0, 0.0);
//glVertex3f(0.005, 1.0, 0.0);
glVertex3f(-0.02, 0.2, 0.0);
glVertex3f(0.02, 0.2, 0.0);
glVertex3f(-0.005, 1.0, 0.0);
glVertex3f(0.005, 1.0, 0.0);
//glVertex3f(0.0, 1.0, 0.0);
glEnd();
glEnd();
break;
default:
case GT_NEEDLE:
glColor4f(gauge->dial.r, gauge->dial.g, gauge->dial.b, gauge->dial.a);
glRotatef(cura * -180.0 / M_PI, 0.0, 0.0, 1.0);
glBegin(GL_TRIANGLE_STRIP);
//glVertex3f(-0.01, -0.04, 0.0);
//glVertex3f(0.01, -0.04, 0.0);
//glVertex3f(-0.02, 0, 0.0);
//glVertex3f(0.02, 0.0, 0.0);
//glVertex3f(-0.005, 1.0, 0.0);
//glVertex3f(0.005, 1.0, 0.0);
glVertex3f(-0.02, 0.2, 0.0);
glVertex3f(0.02, 0.2, 0.0);
glVertex3f(-0.005, 1.0, 0.0);
glVertex3f(0.005, 1.0, 0.0);
//glVertex3f(0.0, 1.0, 0.0);
glEnd();
break;
}
}
@ -226,12 +226,12 @@ void draw_gauge(const struct gauge *gauge, float value, float red, FTGLfont *fon
}
float scale = (gauge->majorend - gauge->majorstart) * 1.5;
float offs = gauge->majorstart - scale;
float offs = gauge->majorstart - scale;
drawText(text, font, sin(cura) * offs, cos(cura) * offs, scale, scale, TA_CENTRE, TA_CENTRE);
}
drawDial(gauge, value, gaugetype);
}
drawDial(gauge, value, gaugetype);
glPopMatrix();
glPopMatrix();
}

22
gauge.h

@ -6,8 +6,8 @@
struct colour
{
float r, g, b, a;
};
};
#define OG_SHIFT 1 // key
#define OG_CTRL 2 // key
@ -56,17 +56,17 @@ struct gauge
struct colour minor;
struct colour dial;
};
enum {
GT_NONE,
GT_NEEDLE,
GT_LINE,
GT_LINE2,
GT_BAR,
};
void init_gauges(void);
enum {
GT_NONE,
GT_NEEDLE,
GT_LINE,
GT_LINE2,
GT_BAR,
};
void init_gauges(void);
void drawDial(const struct gauge *gauge, float value, int style);
void draw_gauge(const struct gauge *gauge, float value, float red, FTGLfont *font, int gaugetype);

162
gettime.h

@ -1,81 +1,81 @@
#ifndef GETTIME_H
#define GETTIME_H
#include <time.h>
#ifdef WIN32
#include <windows.h>
#define CLOCK_REALTIME 0
#define CLOCK_MONOTONIC 0
static inline LARGE_INTEGER getFILETIMEoffset()
{
SYSTEMTIME s;
FILETIME f;
LARGE_INTEGER t;
s.wYear = 1970;
s.wMonth = 1;
s.wDay = 1;
s.wHour = 0;
s.wMinute = 0;
s.wSecond = 0;
s.wMilliseconds = 0;
SystemTimeToFileTime(&s, &f);
t.QuadPart = f.dwHighDateTime;
t.QuadPart <<= 32;
t.QuadPart |= f.dwLowDateTime;
return t;
}
static inline int clock_gettime(int X, struct timespec *ts)
{
LARGE_INTEGER t;
FILETIME f;
double microseconds;
static LARGE_INTEGER offset;
static double frequencyToMicroseconds;
static int initialized = 0;
static BOOL usePerformanceCounter = 0;
if (!initialized) {
LARGE_INTEGER performanceFrequency;
initialized = 1;
usePerformanceCounter = QueryPerformanceFrequency(&performanceFrequency);
if (usePerformanceCounter) {
QueryPerformanceCounter(&offset);
frequencyToMicroseconds = (double)performanceFrequency.QuadPart / 1000000.;
} else {
offset = getFILETIMEoffset();
frequencyToMicroseconds = 10.;
}
}
if (usePerformanceCounter) {
QueryPerformanceCounter(&t);
} else {
GetSystemTimeAsFileTime(&f);
t.QuadPart = f.dwHighDateTime;
t.QuadPart <<= 32;
t.QuadPart |= f.dwLowDateTime;
}
t.QuadPart -= offset.QuadPart;
microseconds = (double)t.QuadPart / frequencyToMicroseconds;
t.QuadPart = microseconds;
ts->tv_sec = t.QuadPart / 1000000;
ts->tv_nsec = (t.QuadPart % 1000000) * 1000;
return 0;
}
#endif
static inline unsigned gettime(void)
{
struct timespec ts;
clock_gettime(CLOCK_MONOTONIC, &ts);
return ts.tv_sec * 1000 + ts.tv_nsec / 1000000;
}
#endif
#ifndef GETTIME_H
#define GETTIME_H
#include <time.h>
#ifdef WIN32
#include <windows.h>
#define CLOCK_REALTIME 0
#define CLOCK_MONOTONIC 0
static inline LARGE_INTEGER getFILETIMEoffset()
{
SYSTEMTIME s;
FILETIME f;
LARGE_INTEGER t;
s.wYear = 1970;
s.wMonth = 1;
s.wDay = 1;
s.wHour = 0;
s.wMinute = 0;
s.wSecond = 0;
s.wMilliseconds = 0;
SystemTimeToFileTime(&s, &f);
t.QuadPart = f.dwHighDateTime;
t.QuadPart <<= 32;
t.QuadPart |= f.dwLowDateTime;
return t;
}
static inline int clock_gettime(int X, struct timespec *ts)
{
LARGE_INTEGER t;
FILETIME f;
double microseconds;
static LARGE_INTEGER offset;
static double frequencyToMicroseconds;
static int initialized = 0;
static BOOL usePerformanceCounter = 0;
if (!initialized) {
LARGE_INTEGER performanceFrequency;
initialized = 1;
usePerformanceCounter = QueryPerformanceFrequency(&performanceFrequency);
if (usePerformanceCounter) {
QueryPerformanceCounter(&offset);
frequencyToMicroseconds = (double)performanceFrequency.QuadPart / 1000000.;
} else {
offset = getFILETIMEoffset();
frequencyToMicroseconds = 10.;
}
}
if (usePerformanceCounter) {
QueryPerformanceCounter(&t);
} else {
GetSystemTimeAsFileTime(&f);
t.QuadPart = f.dwHighDateTime;
t.QuadPart <<= 32;
t.QuadPart |= f.dwLowDateTime;
}
t.QuadPart -= offset.QuadPart;
microseconds = (double)t.QuadPart / frequencyToMicroseconds;
t.QuadPart = microseconds;
ts->tv_sec = t.QuadPart / 1000000;
ts->tv_nsec = (t.QuadPart % 1000000) * 1000;
return 0;
}
#endif
static inline unsigned gettime(void)
{
struct timespec ts;
clock_gettime(CLOCK_MONOTONIC, &ts);
return ts.tv_sec * 1000 + ts.tv_nsec / 1000000;
}
#endif

4474
insim.h
File diff suppressed because it is too large
View File

234
lfsdash.c

@ -2,7 +2,7 @@
#include <FTGL/ftgl.h>
#include <pthread.h>
#include <string.h>
#include <math.h>
#include <math.h>
#include <time.h>
#include "outgauge.h"
#include "cars.h"
@ -10,8 +10,8 @@
#include "gauge.h"
#include "socket.h"
#include "audio.h"
#include "text.h"
#include "insim.h"
#include "text.h"
#include "insim.h"
#include "network_worker.h"
static GLuint s_symbols[1];
@ -102,9 +102,9 @@ void drawSymbol2(int symbol, int on, int map, float x, float y, float xs, float
glScalef(xs, ys, 1.0f);
drawSymbol(symbol, on, map);
glPopMatrix();
}
int g_outgauge_port;
}
int g_outgauge_port;
int g_insim_port;
int main(int argc, char **argv)
@ -115,12 +115,12 @@ int main(int argc, char **argv)
int height;
int fullscreen;
int volume;
int rpmleft;
int dofade;
char *insim_host;
float warn_fuel1;
float warn_fuel2;
int rpmleft;
int dofade;
char *insim_host;
float warn_fuel1;
float warn_fuel2;
config_init("lfsdash.txt");
if (!config_get_int("port", &g_outgauge_port)) g_outgauge_port = 4000;
@ -128,17 +128,17 @@ int main(int argc, char **argv)
if (!config_get_int("width", &width)) width = 1024;
if (!config_get_int("height", &height)) height = 600;
if (!config_get_int("volume", &volume)) volume = 100;
if (!config_get_int("rpmleft", &rpmleft)) rpmleft = 0;
if (!config_get_int("fade", &dofade)) dofade = 1;
if (!config_get_string("insim_host", &insim_host)) insim_host = strdup("localhost");
if (!config_get_int("insim_port", &g_insim_port)) g_insim_port = 29999;
if (!config_get_float("warn_fuel1", &warn_fuel1)) warn_fuel1 = 0.05f;
if (!config_get_float("warn_fuel2", &warn_fuel2)) warn_fuel2 = 0.01f;
if (!config_get_int("rpmleft", &rpmleft)) rpmleft = 0;
if (!config_get_int("fade", &dofade)) dofade = 1;
if (!config_get_string("insim_host", &insim_host)) insim_host = strdup("localhost");
if (!config_get_int("insim_port", &g_insim_port)) g_insim_port = 29999;
if (!config_get_float("warn_fuel1", &warn_fuel1)) warn_fuel1 = 0.05f;
if (!config_get_float("warn_fuel2", &warn_fuel2)) warn_fuel2 = 0.01f;
network_worker_init();
socket_init();
outgauge_init(g_outgauge_port);
outgauge_init(g_outgauge_port);
insim_init(insim_host, g_insim_port);
init_cars();
@ -341,8 +341,8 @@ int main(int argc, char **argv)
oil.dial.r = 1.0;
oil.dial.g = 1.0;
oil.dial.b = 1.0;
oil.dial.a = 0.8;
oil.dial.a = 0.8;
/*
struct gauge clock;
clock.radius = 60.0;
@ -405,7 +405,7 @@ int main(int argc, char **argv)
int s1 = FX_OFF;
int s2 = FX_OFF;
int s3 = FX_OFF;
int s3 = FX_OFF;
int s6 = FX_OFF;
float fade = 0.0f;
@ -416,10 +416,10 @@ int main(int argc, char **argv)
int bing = 0;
int mode = 0;
int mode = 0;
uint32_t mode_timer = 0;
float hold = 0;
int hold2 = 0;
int hold2 = 0;
int ack = 0;
get_car("UF1", &car);
@ -459,54 +459,54 @@ int main(int argc, char **argv)
}
}
if (g_pressed == 3) {
if (g_outgauge.time - g_shift_time > 1000 &&
g_outgauge.time - g_ctrl_time > 1000)
{
//if (g_released == 3)
//{
g_pressed = 0;
g_released = 0;
audio_play(4, FX_BIP);
mode++;
mode_timer = g_outgauge.time;
//}
}
}
if (ack == 0) {
if (mode == 1) {
if (g_pressed == 1 && volume > 0) {
if (g_pressed == 3) {
if (g_outgauge.time - g_shift_time > 1000 &&
g_outgauge.time - g_ctrl_time > 1000)
{
//if (g_released == 3)
//{
g_pressed = 0;
g_released = 0;
audio_play(4, FX_BIP);
mode++;
mode_timer = g_outgauge.time;
//}
}
}
if (ack == 0) {
if (mode == 1) {
if (g_pressed == 1 && volume > 0) {
volume -= 10;
if (volume < 0) volume = 0;
audio_volume(volume * 0.01);
audio_play(4, FX_BOP);
ack = 1;
}
if (g_pressed == 2 && volume < 100) {
audio_play(4, FX_BOP);
ack = 1;
}
if (g_pressed == 2 && volume < 100) {
volume += 10;
if (volume > 100) volume = 100;
audio_volume(volume * 0.01);
audio_play(4, FX_BIP);
ack = 1;
}
}
}
// Acknowledge key release
if (g_released)
{
g_pressed &= ~g_released;
g_released = 0;
mode_timer = g_outgauge.time;
ack = 0;
}
if (mode && g_outgauge.time - mode_timer > 2000)
{
mode = 0;
}
/*
audio_play(4, FX_BIP);
ack = 1;
}
}
}
// Acknowledge key release
if (g_released)
{
g_pressed &= ~g_released;
g_released = 0;
mode_timer = g_outgauge.time;
ack = 0;
}
if (mode && g_outgauge.time - mode_timer > 2000)
{
mode = 0;
}
/*
}
if (g_released == 1) {
volume -= 10;
@ -711,34 +711,34 @@ int main(int argc, char **argv)
//drawSymbol2(7, 0, 0, oil.radius * 0.65, 0, lsize, lsize);
//draw_gauge(&oil, g_outgauge.oiltemp, -1, font, GT_BAR);
glPopMatrix();
glPopMatrix();
//glPushMatrix();
//glTranslatef(width * 0.5 + gsize * 1.5, boost.radius * 0.75, 0);
time_t rawtime;
time(&rawtime);
//glTranslatef(width * 0.5 + gsize * 1.5, boost.radius * 0.75, 0);
time_t rawtime;
time(&rawtime);
struct tm *timeinfo = localtime(&rawtime);
//draw_gauge(&clock, (timeinfo->tm_hour % 12) + timeinfo->tm_min / 60.0, -1, font, GT_NEEDLE);
//glPopMatrix();
//printf("%f %f\n", g_outgauge.oilpressure, g_outgauge.oiltemp);
float symspace = symsize * -1.2;
//printf("%f %f\n", g_outgauge.oilpressure, g_outgauge.oiltemp);
float symspace = symsize * -1.2;
/*
glPushMatrix();
glTranslatef(width * 0.5 + symspace, height - gsize * 0.25 + symspace * 6, 0.0f);
input.radius = symsize * 0.5;
input.dial.r = 0; input.dial.g = 0; input.dial.b = 1;
draw_gauge(&input, g_outgauge.clutch, -1, font, GT_BAR);
glTranslatef(-symspace, 0, 0);
input.dial.r = 1; input.dial.g = 0; input.dial.b = 0;
draw_gauge(&input, g_outgauge.brake, -1, font, GT_BAR);
glTranslatef(-symspace, 0, 0);
input.dial.r = 0; input.dial.g = 1; input.dial.b = 0;
draw_gauge(&input, g_outgauge.throttle, -1, font, GT_BAR);
glPopMatrix();
glPushMatrix();
glTranslatef(width * 0.5 + symspace, height - gsize * 0.25 + symspace * 6, 0.0f);
input.radius = symsize * 0.5;
input.dial.r = 0; input.dial.g = 0; input.dial.b = 1;
draw_gauge(&input, g_outgauge.clutch, -1, font, GT_BAR);
glTranslatef(-symspace, 0, 0);
input.dial.r = 1; input.dial.g = 0; input.dial.b = 0;
draw_gauge(&input, g_outgauge.brake, -1, font, GT_BAR);
glTranslatef(-symspace, 0, 0);
input.dial.r = 0; input.dial.g = 1; input.dial.b = 0;
draw_gauge(&input, g_outgauge.throttle, -1, font, GT_BAR);
glPopMatrix();
*/
glPushMatrix();
@ -776,7 +776,7 @@ int main(int argc, char **argv)
if (g_outgauge.dashlights & (1 << DL_BATTERY)) {
drawSymbol2(DL_BATTERY, g_outgauge.showlights & (1 << DL_BATTERY), 1, 0, symspace * 5, symsize, symsize);
}
}
}
glPopMatrix();
glPushMatrix();
@ -822,7 +822,7 @@ int main(int argc, char **argv)
drawText(text, font, 0, sgsize * 0.2, sgsize * 0.15, sgsize * 0.15, TA_CENTRE, TA_BOTTOM);
//snprintf(text, sizeof text, "dist %0.1f time %0.1f fuel %0.1f", s_cars[carnum].dist * distunit, s_cars[carnum].time, s_cars[carnum].cons);
//snprintf(text, sizeof text, "p %d r %d; %d %d, %u", g_pressed, g_released, volume, mode, mode_timer);
//drawText(text, font, 0, sgsize * 1.2, sgsize * 0.2, sgsize * 0.2, TA_CENTRE, TA_CENTRE);
//drawText(text, font, 0, sgsize * 1.2, sgsize * 0.2, sgsize * 0.2, TA_CENTRE, TA_CENTRE);
//snprintf(text, sizeof text, "s %u c %u; %u", g_shift_time, g_ctrl_time, g_outgauge.time);
//drawText(text, font, 0, sgsize * 1.5, sgsize * 0.2, sgsize * 0.2, TA_CENTRE, TA_CENTRE);
snprintf(text, sizeof text, "%02d:%02d", timeinfo->tm_hour, timeinfo->tm_min);
@ -853,8 +853,8 @@ int main(int argc, char **argv)
// if (ci->dist >= width / 2 && ci->dist >= height / 2) continue;
const struct conninfo *pi = s_conninfo + ci->ucid;
int idx = (ci->x - ox) >> 16;
int idy = (ci->y - oy) >> 16;
int idx = (ci->x - ox) >> 16;
int idy = (ci->y - oy) >> 16;
int dist = sqrt(idx * idx + idy * idy);
snprintf(text, sizeof text, "%s - %d (%0.1f %s)", pi->uname, dist, (ci->speed - speed) / 327.68f * speedunit, (g_outgauge.flags & OG_KM) ? "km/h" : "mph");
@ -862,19 +862,19 @@ int main(int argc, char **argv)
glPushMatrix();
glRotatef(-head / 182.0444f, 0.0, 0.0, 1.0);
glTranslatef(dx, dy, 0);
if (ci->hist > 0) {
glBegin(GL_LINE_STRIP);
int j;
for (j = CARPATHSIZE; j > 0; j--)
{
int d = (j + ci->hist - 1) % CARPATHSIZE;
if (ci->hist_x[d] == 0 && ci->hist_y[d] == 0) continue;
glColor4f(ci->hist_s[d] / 32768.0f, 1.0 - ci->hist_s[d] / 32768.0f, 0.0, 0.9);
glVertex3f((ci->hist_x[d] - ci->x) / zoom, (ci->hist_y[d] - ci->y) / zoom, 0.0);
}
glEnd();
}
glTranslatef(dx, dy, 0);
if (ci->hist > 0) {
glBegin(GL_LINE_STRIP);
int j;
for (j = CARPATHSIZE; j > 0; j--)
{
int d = (j + ci->hist - 1) % CARPATHSIZE;
if (ci->hist_x[d] == 0 && ci->hist_y[d] == 0) continue;
glColor4f(ci->hist_s[d] / 32768.0f, 1.0 - ci->hist_s[d] / 32768.0f, 0.0, 0.9);
glVertex3f((ci->hist_x[d] - ci->x) / zoom, (ci->hist_y[d] - ci->y) / zoom, 0.0);
}
glEnd();
}
if (pi->state == 1) {
glColor4f(1.0, 0.0, 0.0, 0.5);
} else if (pi->state == 2) {
@ -889,16 +889,16 @@ int main(int argc, char **argv)
glVertex3f(0, 5, 0);
glVertex3f(2.5, -5, 0);
glVertex3f(-2.5, -5, 0);
glEnd();
glEnd();
glPopMatrix();
}
glPopMatrix();
glPopMatrix();
*/
int os1 = (g_outgauge.showlights & (1 << DL_SIGNAL_L)) ? FX_ON : FX_OFF;
int os2 = (g_outgauge.showlights & (1 << DL_SIGNAL_R)) ? FX_ON : FX_OFF;
int os3 = (g_outgauge.showlights & (1 << DL_SIGNAL_ANY)) ? FX_ON : FX_OFF;
int os3 = (g_outgauge.showlights & (1 << DL_SIGNAL_ANY)) ? FX_ON : FX_OFF;
int os6 = (g_outgauge.showlights & (1 << DL_PITSPEED)) ? FX_ON : FX_OFF;
if (s1 != os1) {
@ -912,7 +912,7 @@ int main(int argc, char **argv)
if (s3 != os3) {
s3 = os3;
if (!g_fade) audio_play(2, s3);
}
}
if (s6 != os6) {
s6 = os6;
if (!g_fade) audio_play(5, s6);
@ -972,8 +972,8 @@ int main(int argc, char **argv)
audio_deinit();
glfwTerminate();
glfwTerminate();
network_worker_deinit();
config_set_int("port", g_outgauge_port);
@ -981,11 +981,11 @@ int main(int argc, char **argv)
config_set_int("width", width);
config_set_int("height", height);
config_set_int("volume", volume);
config_set_int("rpmleft", rpmleft);
config_set_int("fade", dofade);
config_set_string("insim_host", insim_host);
config_set_int("insim_port", g_insim_port);
config_set_float("warn_fuel1", warn_fuel1);
config_set_int("rpmleft", rpmleft);
config_set_int("fade", dofade);
config_set_string("insim_host", insim_host);
config_set_int("insim_port", g_insim_port);
config_set_float("warn_fuel1", warn_fuel1);
config_set_float("warn_fuel2", warn_fuel2);
config_deinit();

50
network_worker.c

@ -142,37 +142,37 @@ void *network_connect(const char *host, int port, network_callback callback, voi
worker_queue(&s_network_worker, job);
return job;
}
int network_listen(int port, int tcp)
{
int fd;
#ifdef USE_IPV6
fd = socket(AF_INET6, tcp ? SOCK_STREAM : SOCK_DGRAM, 0);
#else
fd = socket(AF_INET, tcp ? SOCK_STREAM : SOCK_DGRAM, 0);
#endif
int network_listen(int port, int tcp)
{
int fd;
#ifdef USE_IPV6
fd = socket(AF_INET6, tcp ? SOCK_STREAM : SOCK_DGRAM, 0);
#else
fd = socket(AF_INET, tcp ? SOCK_STREAM : SOCK_DGRAM, 0);
#endif
if (fd < 0)
{
#ifdef WIN32
fprintf(stderr, "socket: %d\n", WSAGetLastError());
{
#ifdef WIN32
fprintf(stderr, "socket: %d\n", WSAGetLastError());
#else
fprintf(stderr, "socket: %s\n", strerror(errno));
fprintf(stderr, "socket: %s\n", strerror(errno));
#endif
return -1;
}
#ifdef USE_IPV6
}
#ifdef USE_IPV6
struct sockaddr_in6 serv_addr;
serv_addr.sin6_family = AF_INET6;
serv_addr.sin6_addr = in6addr_any;
serv_addr.sin6_port = htons(port);
#else
struct sockaddr_in serv_addr;
serv_addr.sin6_port = htons(port);
#else
struct sockaddr_in serv_addr;
serv_addr.sin_family = AF_INET;
serv_addr.sin_addr.s_addr = INADDR_ANY;
serv_addr.sin_port = htons(port);
serv_addr.sin_port = htons(port);
#endif
int on = 1;
@ -187,8 +187,8 @@ int network_listen(int port, int tcp)
return -1;
}
socket_set_nonblock(fd);
if (tcp) socket_set_nodelay(fd);
return fd;
socket_set_nonblock(fd);
if (tcp) socket_set_nodelay(fd);
return fd;
}

2
network_worker.h

@ -6,7 +6,7 @@ typedef void (*network_callback)(int fd, void *data);
void network_worker_init(void);
void network_worker_deinit(void);
void *network_connect(const char *host, int port, network_callback callback, void *data);
void *network_connect(const char *host, int port, network_callback callback, void *data);
int network_listen(int port, int tcp);
#endif /* NETWORK_WORKER_H */

18
outgauge.h

@ -27,17 +27,17 @@ struct outgauge
uint32_t id;
};
extern struct outgauge g_outgauge;
extern float g_consumption;
extern int g_owncar;
extern struct outgauge g_outgauge;
extern float g_consumption;
extern int g_owncar;
extern int g_fade;
extern int g_pressed;
extern int g_released;
extern uint32_t g_shift_time;
extern uint32_t g_ctrl_time;
extern int g_released;
extern uint32_t g_shift_time;
extern uint32_t g_ctrl_time;
void outgauge_init(int port);
void outgauge_init(int port);
#endif /* OUTGAUGE_H */

72
socket.c

@ -1,13 +1,13 @@
#ifndef WIN32
//#define USE_POLL
#ifndef WIN32
//#define USE_POLL
#endif
#include <stdio.h>
#include <string.h>
#include <sys/types.h>
#ifdef WIN32
#include <winsock2.h>
#include <ws2tcpip.h>
#include <sys/types.h>
#ifdef WIN32
#include <winsock2.h>
#include <ws2tcpip.h>
#else
#include <sys/socket.h>
#ifdef USE_POLL
@ -17,18 +17,18 @@
#include <netinet/in.h>
#include <netinet/tcp.h>
#include <unistd.h>
#include <fcntl.h>
#include <fcntl.h>
#endif
#include <errno.h>
#include <pthread.h>
#include "socket.h"
#include "list.h"
#include "list.h"
int s_running;
struct socket_t
{
int fd;
int fd;
int mode;
socket_func socket_func;
void *arg;
@ -46,7 +46,7 @@ static pthread_mutex_t s_sockets_mutex;
#ifdef USE_POLL
static int s_epoll_fd = -1;
#endif /* USE_POLL */
#ifdef USE_POLL
static struct socket_t *socket_get_by_fd(int fd)
{
@ -58,13 +58,13 @@ static struct socket_t *socket_get_by_fd(int fd)
}
return NULL;
}
}
#endif
void register_socket(int fd, int mode, socket_func socket_func, void *arg)
{
struct socket_t *s = malloc(sizeof *s);
s->fd = fd;
s->fd = fd;
s->mode = mode;
s->socket_func = socket_func;
s->arg = arg;
@ -119,16 +119,16 @@ void deregister_socket(int fd)
pthread_mutex_lock(&s_sockets_mutex);
socket_list_del_item(&s_sockets, &s);
pthread_mutex_unlock(&s_sockets_mutex);
}
void socket_init(void)
{
#ifdef WIN32
WSADATA WSA_Data;
WSAStartup (0x101, & WSA_Data);
<