Compare commits

...

2 Commits

Author SHA1 Message Date
Peter Nelson 6dfc0cc5dd
Something 2023-08-05 18:54:11 +01:00
Peter Nelson 86dfa2637d Change: Force spaces for indentation, and auto-reformat.
Some of the formatting is no longer optimal but it is at least consistent.
2023-08-05 16:43:57 +01:00
26 changed files with 3286 additions and 3141 deletions

View File

@ -14,8 +14,8 @@ SRCS += worker.c
OBJS := $(SRCS:.c=.o)
CFLAGS += `pkg-config libglfw ftgl openal --cflags` -g
LDFLAGS += `pkg-config libglfw ftgl openal --libs` -g
CFLAGS += `pkg-config freetype2 glfw3 ftgl openal opengl --cflags` -g
LDFLAGS += -lm -lpthread -lSOIL `pkg-config freetype2 glfw3 ftgl openal opengl --libs`
lfsdash: $(OBJS)
$(CC) $(LDFLAGS) $(OBJS) -o $@

View File

@ -58,7 +58,8 @@ void *file_allocate_and_read_bytes(FILE *file, size_t bytes)
return buffer;
}
static inline ALenum GetFormatFromInfo(short channels, short bitsPerSample) {
static inline ALenum GetFormatFromInfo(short channels, short bitsPerSample)
{
if (channels == 1)
return AL_FORMAT_MONO16;
return AL_FORMAT_STEREO16;
@ -130,7 +131,8 @@ void audio_init(void)
// Generate buffers, or else no sound will happen!
alGenBuffers(NUM_BUFFERS, buffer);
if (alGetError() != AL_NO_ERROR) {
if (alGetError() != AL_NO_ERROR)
{
printf("- Error creating buffers !!\n");
exit(1);
}

View File

@ -1,7 +1,8 @@
#ifndef AUDIO_H
#define AUDIO_H
enum {
enum
{
FX_ON,
FX_OFF,
FX_BING,

15
cars.c
View File

@ -36,15 +36,18 @@ void init_cars(void)
memset(s_cars, 0, sizeof s_cars);
FILE *f = fopen("cars.txt", "r");
if (!f) return;
if (!f)
return;
struct car *car = s_cars;
while (!feof(f))
{
char buf[1024];
if (fgets(buf, sizeof buf, f) <= 0) break;
if (*buf == '#') continue;
if (fgets(buf, sizeof buf, f) <= 0)
break;
if (*buf == '#')
continue;
char *bufp = buf, *p;
p = strsep(&bufp, ",");
@ -70,7 +73,8 @@ void init_cars(void)
car++;
if (car - s_cars == MAX_CARS) break;
if (car - s_cars == MAX_CARS)
break;
}
fclose(f);
@ -83,7 +87,8 @@ int get_car(const char tag[4], struct car *car)
{
if (!strcmp(s_cars[i].tag, tag))
{
if (car != NULL) memcpy(car, &s_cars[i], sizeof *car);
if (car != NULL)
memcpy(car, &s_cars[i], sizeof *car);
return i;
}
}

View File

@ -57,7 +57,8 @@ void config_set_string(const char *key, const char *value)
itemp = &(*itemp)->next;
}
if (value == NULL) return;
if (value == NULL)
return;
*itemp = malloc(sizeof *item);
item = *itemp;
@ -70,24 +71,29 @@ void config_set_string(const char *key, const char *value)
static void config_rehash(void)
{
FILE *f = fopen(s_config.filename, "r");
if (f == NULL) return;
if (f == NULL)
return;
config_free();
while (!feof(f))
{
char buf[256];
if (fgets(buf, sizeof buf, f) <= 0) break;
if (fgets(buf, sizeof buf, f) <= 0)
break;
/* Ignore comments */
if (*buf == '#') continue;
if (*buf == '#')
continue;
char *n = strchr(buf, '=');
if (n == NULL) continue;
if (n == NULL)
continue;
*n++ = '\0';
char *eol = strchr(n, '\n');
if (eol != NULL) *eol = '\0';
if (eol != NULL)
*eol = '\0';
config_set_string(buf, n);
}
@ -98,7 +104,8 @@ static void config_rehash(void)
static void config_write(void)
{
FILE *f = fopen(s_config.filename, "w");
if (f == NULL) return;
if (f == NULL)
return;
struct configitem *curr;
for (curr = s_config.head; curr != NULL; curr = curr->next)
@ -158,10 +165,12 @@ int config_get_int(const char *setting, int *value)
char *c, *endptr;
int v;
if (!config_get_string(setting, &c)) return 0;
if (!config_get_string(setting, &c))
return 0;
v = strtol(c, &endptr, 10);
if (*endptr != '\0') return 0;
if (*endptr != '\0')
return 0;
*value = v;
return 1;
@ -172,10 +181,12 @@ int config_get_float(const char *setting, float *value)
char *c, *endptr;
float v;
if (!config_get_string(setting, &c)) return 0;
if (!config_get_string(setting, &c))
return 0;
v = strtof(c, &endptr);
if (*endptr != '\0') return 0;
if (*endptr != '\0')
return 0;
*value = v;
return 1;

49
gauge.c
View File

@ -1,4 +1,4 @@
#include <GL/glfw.h>
#include <GLFW/glfw3.h>
#include <FTGL/ftgl.h>
#include <math.h>
#include <stdio.h>
@ -23,7 +23,6 @@ void DrawArc(float inner, float outer, float start_angle, float arc_angle, int n
float xo = outer * sinf(start_angle); // we now start at the start angle
float yo = outer * cosf(start_angle);
glBegin(GL_TRIANGLE_STRIP); // since the arc is not a closed curve, this is a strip now
int ii;
for (ii = 0; ii < num_segments; ii++)
@ -81,8 +80,6 @@ void drawDial(const struct gauge *gauge, float value, int style)
float angle = gauge->anglemax - gauge->anglemin;
float cura = (value - gauge->rangemin) / range * angle + gauge->anglemin;
switch (style)
{
case GT_NONE:
@ -126,7 +123,6 @@ 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)
{
float range = gauge->rangemax - gauge->rangemin;
@ -140,24 +136,31 @@ void draw_gauge(const struct gauge *gauge, float value, float red, FTGLfont *fon
float majorstep = gauge->majorstep;
float minorstep = gauge->minorstep;
while (1) {
while (1)
{
major = angle / (range / majorstep + 0.0);
minor = angle / (range / minorstep + 0.0);
if (fabsf(major) < 12 * M_PI / 180.0) {
if (fabsf(major) < 12 * M_PI / 180.0)
{
majorstep *= 2;
minorstep = majorstep / 2;
} else {
}
else
{
break;
}
}
if (value < gauge->rangemin) value = gauge->rangemin;
if (value > gauge->rangemax * 2) value = gauge->rangemax * 2;
if (value < gauge->rangemin)
value = gauge->rangemin;
if (value > gauge->rangemax * 2)
value = gauge->rangemax * 2;
glPushMatrix();
glScalef(gauge->radius, gauge->radius, 1.0);
if (red > 0) {
if (red > 0)
{
glLineWidth(1.0);
glColor4f(0.8, 0.0, 0.0, 1.0);
@ -182,7 +185,8 @@ void draw_gauge(const struct gauge *gauge, float value, float red, FTGLfont *fon
// for (cura = gauge->anglemin; cura < gauge->anglemax; cura += minor)
for (ii = 0; ii <= range / minorstep; ii++)
{
if (red > 0 && ii * minorstep >= red) {
if (red > 0 && ii * minorstep >= red)
{
glColor4f(1.0, 1.0, 1.0, 1.0);
}
float cura = gauge->anglemin + ii * minor;
@ -201,7 +205,8 @@ void draw_gauge(const struct gauge *gauge, float value, float red, FTGLfont *fon
// for (cura = gauge->anglemin; cura < gauge->anglemax; cura += major)
for (ii = 0; ii <= range / majorstep; ii++)
{
if (red > 0 && ii * majorstep > red) {
if (red > 0 && ii * majorstep > red)
{
glColor4f(1.0, 1.0, 1.0, 1.0);
}
float cura = gauge->anglemin + ii * major;
@ -215,12 +220,19 @@ void draw_gauge(const struct gauge *gauge, float value, float red, FTGLfont *fon
glEnd();
char text[10];
if (gauge->majorstep > 100) {
if (gauge->majorstep > 100)
{
snprintf(text, sizeof text, "%d", (int)(ii * majorstep / 1000 + gauge->rangemin));
} else {
if (range == 1) {
snprintf(text, sizeof text, ii == 0 ? "E" : ii == 1 ? "½" : "F");
} else {
}
else
{
if (range == 1)
{
snprintf(text, sizeof text, ii == 0 ? "E" : ii == 1 ? "½"
: "F");
}
else
{
snprintf(text, sizeof text, "%d", (int)(ii * majorstep + gauge->rangemin));
}
}
@ -234,4 +246,3 @@ void draw_gauge(const struct gauge *gauge, float value, float red, FTGLfont *fon
glPopMatrix();
}

View File

@ -58,7 +58,8 @@ struct gauge
struct colour dial;
};
enum {
enum
{
GT_NONE,
GT_NEEDLE,
GT_LINE,

View File

@ -40,21 +40,28 @@ static inline int clock_gettime(int X, struct timespec *ts)
static int initialized = 0;
static BOOL usePerformanceCounter = 0;
if (!initialized) {
if (!initialized)
{
LARGE_INTEGER performanceFrequency;
initialized = 1;
usePerformanceCounter = QueryPerformanceFrequency(&performanceFrequency);
if (usePerformanceCounter) {
if (usePerformanceCounter)
{
QueryPerformanceCounter(&offset);
frequencyToMicroseconds = (double)performanceFrequency.QuadPart / 1000000.;
} else {
}
else
{
offset = getFILETIMEoffset();
frequencyToMicroseconds = 10.;
}
}
if (usePerformanceCounter) {
if (usePerformanceCounter)
{
QueryPerformanceCounter(&t);
} else {
}
else
{
GetSystemTimeAsFileTime(&f);
t.QuadPart = f.dwHighDateTime;
t.QuadPart <<= 32;

45
insim.c
View File

@ -25,7 +25,8 @@
struct conninfo s_conninfo[256];
struct carinfo s_carinfo[256];
struct insim_buffer {
struct insim_buffer
{
int state;
uint8_t buffer[65536];
int pos;
@ -48,12 +49,14 @@ void insim_queue(const void *buffer, size_t size)
int insim_dequeue(int fd)
{
if (s_send_packet == NULL) queue_consume(s_send_queue, (void *)&s_send_packet);
if (s_send_packet == NULL)
queue_consume(s_send_queue, (void *)&s_send_packet);
if (s_send_packet != NULL)
{
ssize_t s = send(fd, (char *)s_send_packet + s_send_pos, s_send_packet[0] - s_send_pos, MSG_NOSIGNAL);
if (s >= 0) s_send_pos += s;
if (s >= 0)
s_send_pos += s;
if (s_send_pos >= s_send_packet[0])
{
free(s_send_packet);
@ -105,8 +108,10 @@ void insim_handle_tiny(int fd, const void *buffer)
static void ltc_duty(const char *uname, int state)
{
int i;
for (i = 0; i < 256; i++) {
if (!strcmp(uname, s_conninfo[i].uname)) {
for (i = 0; i < 256; i++)
{
if (!strcmp(uname, s_conninfo[i].uname))
{
s_conninfo[i].state = state;
printf("Player %s state %d\n", uname, state);
return;
@ -135,16 +140,25 @@ void insim_handle_mso(int fd, const void *buffer)
char *name = NULL;
int i;
for (i = 0; i < 10; i++) {
for (i = 0; i < 10; i++)
{
char *a = strsep(&bufp, " ");
if (a == NULL) return;
if (i == 0 && strcmp(a, "***")) return;
if (i == 1 && (!strcmp(a, "Officer") || !strcmp(a, "Cadet"))) state |= 1;
if (i == 2 && state == 1) name = a;
if (i == 3 && strcmp(a, "has")) return;
if (i == 4 && strcmp(a, "gone")) return;
if (i == 5 && !strcmp(a, "onduty")) ltc_duty(name, 1);
if (i == 5 && !strcmp(a, "offduty")) ltc_duty(name, 0);
if (a == NULL)
return;
if (i == 0 && strcmp(a, "***"))
return;
if (i == 1 && (!strcmp(a, "Officer") || !strcmp(a, "Cadet")))
state |= 1;
if (i == 2 && state == 1)
name = a;
if (i == 3 && strcmp(a, "has"))
return;
if (i == 4 && strcmp(a, "gone"))
return;
if (i == 5 && !strcmp(a, "onduty"))
ltc_duty(name, 1);
if (i == 5 && !strcmp(a, "offduty"))
ltc_duty(name, 0);
}
}
@ -172,7 +186,8 @@ void insim_handle_cpr(int fd, const void *buffer)
{
const struct IS_CPR *p = buffer;
struct conninfo *c = s_conninfo + p->UCID;
if (c->active == 0) fprintf(stderr, "Rename of inactive connection\n");
if (c->active == 0)
fprintf(stderr, "Rename of inactive connection\n");
strncpy(c->pname, p->PName, sizeof c->pname);
}

32
insim.h
View File

@ -14,13 +14,11 @@
// NOTE : This text file was written with a TAB size equal to 4 spaces.
// INSIM VERSION NUMBER (updated for version 0.6B)
// ====================
static const int INSIM_VERSION = 5;
// CHANGES
// =======
@ -51,7 +49,6 @@ static const int INSIM_VERSION = 5;
// FIX : OutGaugePack ID was always zero regardless of ID in cfg.txt
// FIX : InSim camera with vertical pitch would cause LFS to crash
// TYPES : (all multi-byte types are PC style - lowest byte first)
// =====
@ -70,7 +67,6 @@ static const int INSIM_VERSION = 5;
// 100-190 : 100 to 1000 laps... laps = (rl - 100) * 10 + 100
// 191-238 : 1 to 48 hours... hours = rl - 190
// InSim PACKETS
// =============
@ -81,7 +77,6 @@ static const int INSIM_VERSION = 5;
// ReqI : non zero if the packet is a packet request or a reply to a request
// Data : the first data byte
// INITIALISING InSim
// ==================
@ -167,7 +162,6 @@ struct IS_ISI // InSim Init - packet to initialise the InSim system
// Messages typed with this prefix will be sent to your InSim program
// on the host (in IS_MSO) and not displayed on anyone's screen.
// ENUMERATIONS FOR PACKET TYPES
// =============================
@ -270,7 +264,6 @@ enum // the fourth byte of an IS_SMALL packet is one of these
SMALL_NLI, // 7 - instruction : set node lap interval
};
// GENERAL PURPOSE PACKETS - IS_TINY (4 bytes) and IS_SMALL (8 bytes)
// =======================
@ -300,7 +293,6 @@ struct IS_SMALL // General purpose 8 byte packet
uint32_t UVal; // value (e.g. for SMALL_SSP this would be the OutSim packet rate)
};
// VERSION REQUEST
// ===============
@ -327,7 +319,6 @@ struct IS_VER // VERsion
// ReqI : non-zero (returned in the reply)
// SubT : TINY_VER (request an IS_VER)
// CLOSING InSim
// =============
@ -341,7 +332,6 @@ struct IS_VER // VERsion
// You can shut down InSim completely and stop it listening at all by typing /insim=0
// into LFS (or send a MsgTypePack to do the same thing).
// MAINTAINING THE CONNECTION - IMPORTANT
// ==========================
@ -369,7 +359,6 @@ struct IS_VER // VERsion
// ReqI : non-zero (as received in the request packet)
// SubT : TINY_REPLY (reply to ping)
// STATE REPORTING AND REQUESTS
// ============================
@ -453,7 +442,6 @@ struct IS_SFP // State Flags Pack
// Other states must be set by using keypresses or messages (see below)
// SCREEN MODE
// ===========
@ -478,7 +466,6 @@ struct IS_MOD // MODe : send to LFS to change screen mode
// If Width and Height are both zero, LFS will switch to windowed mode.
// TEXT MESSAGES AND KEY PRESSES
// ==============================
@ -625,7 +612,6 @@ struct IS_SCH // Single CHaracter
uint8_t Spare3;
};
// MULTIPLAYER NOTIFICATION
// ========================
@ -658,7 +644,6 @@ struct IS_ISM // InSim Multi
// NOTE : If LFS is not in multiplayer mode, the host name in the ISM will be empty.
// VOTE NOTIFY AND CANCEL
// ======================
@ -704,7 +689,6 @@ struct IS_VTN // VoTe Notify
// ReqI : 0
// SubT : TINY_VTC (VoTe Cancel)
// ALLOWED CARS
// ============
@ -752,7 +736,6 @@ struct IS_PLC // PLayer Cars
// BMW SAUBER F1.06 - 0x40000
// FORMULA BMW FB02 - 0x80000
// RACE TRACKING
// =============
@ -1313,7 +1296,6 @@ static const int NOT_CHANGED = 255;
// bit 6 female
// bit 7 rear right
// TRACKING PACKET REQUESTS
// ========================
@ -1327,7 +1309,6 @@ static const int NOT_CHANGED = 255;
// SubT : TINY_NLP - request a single IS_NLP
// SubT : TINY_MCI - request a set of IS_MCI
// AUTOCROSS
// =========
@ -1372,7 +1353,6 @@ struct IS_AXO // AutoX Object
uint8_t PLID; // player's unique id
};
// CAR TRACKING - car position info sent at constant intervals
// ============
@ -1452,7 +1432,6 @@ struct IS_MCI // Multi Car Info - if more than 8 in race then more than one of t
// SubT : SMALL_NLI (Node Lap Interval)
// UVal : interval (0 means stop, otherwise time interval : 40, 50, 60... 8000 ms)
// CONTACT - reports contacts between two cars if the closing speed is above 0.25 m/s
// =======
@ -1529,7 +1508,6 @@ struct IS_OBH // OBject Hit - car hit an autocross object or an unknown object
//'BBBBHHBBBBhhhhBBBB'
// OBHFlags byte
#define OBH_LAYOUT 1 // an added object
@ -1553,7 +1531,6 @@ struct IS_HLV // Hot Lap Validity - illegal ground / hit wall / speeding in pit
struct CarContOBJ C;
};
// AUTOCROSS OBJECTS - reporting / adding / removing
// =================
@ -1632,7 +1609,6 @@ enum
// the corresponding IS_AXM that will be output when the packet is processed. Then
// you can send the second packet and again wait for the IS_AXM and so on.
// CAR POSITION PACKETS (Initialising OutSim from InSim - See "OutSim" below)
// ====================
@ -1652,7 +1628,6 @@ enum
// NOTE : OutSim packets are not InSim packets and don't have a 4-byte header.
// DASHBOARD PACKETS (Initialising OutGauge from InSim - See "OutGauge" below)
// =================
@ -1672,7 +1647,6 @@ enum
// NOTE : OutGauge packets are not InSim packets and don't have a 4-byte header.
// CAMERA CONTROL
// ==============
@ -1773,7 +1747,6 @@ struct IS_CPP // Cam Pos Pack - Full camera packet (in car OR SHIFT+U mode)
// and later send back exactly the same packet to LFS and it will try to replicate
// that camera position.
// TIME CONTROL
// ============
@ -1806,7 +1779,6 @@ struct IS_CPP // Cam Pos Pack - Full camera packet (in car OR SHIFT+U mode)
// SubT : SMALL_STP (STeP)
// UVal : number (number of hundredths of a second to update)
// REPLAY CONTROL
// ==============
@ -1868,7 +1840,6 @@ enum
// NOTE : RIPOPT_FULL_PHYS makes MPR searching much slower so should not normally be used.
// This flag was added to allow high accuracy MCI packets to be output when fast forwarding.
// SCREENSHOTS
// ===========
@ -1902,7 +1873,6 @@ enum
SSH_NO_SAVE, // 3 - could not save the screenshot
};
// BUTTONS
// =======
@ -2077,7 +2047,6 @@ struct IS_BTT // BuTton Type - sent back when user types into a text entry butto
char Text[96]; // typed text, zero to TypeIn specified in IS_BTN
};
// OutSim - MOTION SIMULATOR SUPPORT
// ======
@ -2116,7 +2085,6 @@ struct OutSimPack
// not support any motion systems in particular and cannot accept responsibility
// for injuries or deaths connected with the use of such machinery.
// OutGauge - EXTERNAL DASHBOARD SUPPORT
// ========

269
lfsdash.c
View File

@ -1,9 +1,11 @@
#include <GL/glfw.h>
#include <GLFW/glfw3.h>
#include <FTGL/ftgl.h>
#include <SOIL/SOIL.h>
#include <pthread.h>
#include <string.h>
#include <math.h>
#include <time.h>
#include <unistd.h>
#include "outgauge.h"
#include "cars.h"
#include "config.h"
@ -16,7 +18,8 @@
static GLuint s_symbols[1];
struct sympos {
struct sympos
{
float a, b, c, d;
int flag;
struct colour off;
@ -39,8 +42,7 @@ struct sympos {
*/
static int dl_to_sympos[] = {
18, 0, 12, 17, 11, 15, 16, 2, 10, 9, 14, 13
};
18, 0, 12, 17, 11, 15, 16, 2, 10, 9, 14, 13};
static struct sympos s_sympos[] = {
{0.0, 0.0, 0.125, 0.125, (1 << DL_FULLBEAM), {0.1, 0.1, 0.1, 1.0}, {0.15, 0.15, 1.0, 1.0}},
@ -72,14 +74,18 @@ static struct sympos s_sympos[] = {
void drawSymbol(int symbol, int on, int map)
{
if (map) symbol = dl_to_sympos[symbol];
if (map)
symbol = dl_to_sympos[symbol];
struct sympos *s = &s_sympos[symbol];
glEnable(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D, s_symbols[0]);
if (on) {
if (on)
{
glColor4f(s->on.r, s->on.g, s->on.b, s->on.a);
} else {
}
else
{
glColor4f(s->off.r, s->off.g, s->off.b, s->off.a);
}
glBegin(GL_QUADS);
@ -123,17 +129,28 @@ int main(int argc, char **argv)
float warn_fuel2;
config_init("lfsdash.txt");
if (!config_get_int("port", &g_outgauge_port)) g_outgauge_port = 4000;
if (!config_get_int("fullscreen", &fullscreen)) fullscreen = 0;
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("port", &g_outgauge_port))
g_outgauge_port = 4000;
if (!config_get_int("fullscreen", &fullscreen))
fullscreen = 0;
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;
network_worker_init();
@ -143,14 +160,16 @@ int main(int argc, char **argv)
init_cars();
glfwInit();
glfwOpenWindowHint(GLFW_FSAA_SAMPLES, 1);
if (!glfwOpenWindow(width, height, 0, 0, 0, 0, 0, 0, fullscreen ? GLFW_FULLSCREEN : GLFW_WINDOW))
glfwWindowHint(GLFW_SAMPLES, 1);
GLFWwindow *window = glfwCreateWindow(width, height, "GL LFS Dashboard", fullscreen ? glfwGetPrimaryMonitor() : NULL, NULL);
if (window == NULL)
{
printf("Could not create window\n");
glfwTerminate();
return 0;
}
glfwSetWindowTitle("GL LFS Dashboard");
glfwSetWindowTitle(window, "GL LFS Dashboard");
audio_init();
@ -387,15 +406,21 @@ int main(int argc, char **argv)
pthread_t thread;
pthread_create(&thread, NULL, &socket_run, NULL);
FTGLfont *font = ftglCreateTextureFont("arialbd.ttf");//"Arial.ttf");//"/usr/share/fonts/truetype/msttcorefonts/Arial.ttf");
FTGLfont *font = ftglCreateOutlineFont("/usr/share/fonts/truetype/dejavu/DejaVuSans-Bold.ttf"); //"Arial.ttf");//"/usr/share/fonts/truetype/msttcorefonts/Arial.ttf");
// DS_DIGII
if (!font) return 0;
if (!font) {
fprintf(stderr, "Unable to open font\n");
return 0;
}
ftglSetFontFaceSize(font, TEXT_SIZE, TEXT_SIZE);
ftglSetFontFaceSize(font, TEXT_SIZE, 0);
glGenTextures(1, s_symbols);
glBindTexture(GL_TEXTURE_2D, s_symbols[0]);
glfwLoadTexture2D("symbols512.tga", GLFW_BUILD_MIPMAPS_BIT);
SOIL_load_OGL_texture("symbols512.png", 0, 1, SOIL_FLAG_MIPMAPS);
// glfwLoadTexture2D("symbols512.tga", GLFW_BUILD_MIPMAPS_BIT);
// glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, )
// Use trilinear interpolation for minification
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
// Use bilinear interpolation for magnification
@ -429,7 +454,8 @@ int main(int argc, char **argv)
float speedtimer = 0;
float zoom = 65536.0;
while (running) {
while (running)
{
double curtime = glfwGetTime();
if (curtime >= next)
@ -438,19 +464,23 @@ int main(int argc, char **argv)
float interval = curtime - last_time;
flash += interval;
if (flash >= 1) flash = 0;
if (flash >= 1)
flash = 0;
if (bing == 2)
{
binger += interval;
if (binger >= 1) {
if (binger >= 1)
{
binger = 0;
if (!g_fade) audio_play(3, FX_BING);
if (!g_fade)
audio_play(3, FX_BING);
}
}
last_time = curtime;
if (speedtimer > 0) {
if (speedtimer > 0)
{
speedtimer -= interval;
if (speedtimer <= 0)
{
@ -459,7 +489,8 @@ int main(int argc, char **argv)
}
}
if (g_pressed == 3) {
if (g_pressed == 3)
{
if (g_outgauge.time - g_shift_time > 1000 &&
g_outgauge.time - g_ctrl_time > 1000)
{
@ -474,18 +505,24 @@ int main(int argc, char **argv)
}
}
if (ack == 0) {
if (mode == 1) {
if (g_pressed == 1 && volume > 0) {
if (ack == 0)
{
if (mode == 1)
{
if (g_pressed == 1 && volume > 0)
{
volume -= 10;
if (volume < 0) volume = 0;
if (volume < 0)
volume = 0;
audio_volume(volume * 0.01);
audio_play(4, FX_BOP);
ack = 1;
}
if (g_pressed == 2 && volume < 100) {
if (g_pressed == 2 && volume < 100)
{
volume += 10;
if (volume > 100) volume = 100;
if (volume > 100)
volume = 100;
audio_volume(volume * 0.01);
audio_play(4, FX_BIP);
ack = 1;
@ -527,7 +564,7 @@ int main(int argc, char **argv)
}*/
}
glfwGetWindowSize(&width, &height);
glfwGetFramebufferSize(window, &width, &height);
height = height > 0 ? height : 1;
glViewport(0, 0, width, height);
glClearColor(0.0, 0.0, 0.0, 0.0);
@ -555,7 +592,8 @@ int main(int argc, char **argv)
float speedunit = (g_outgauge.flags & OG_KM) ? 3.6 : 2.23693629;
float distunit = (g_outgauge.flags & OG_KM) ? 0.001 : 0.0006215;
if (g_outgauge.speed > maxspeed) {
if (g_outgauge.speed > maxspeed)
{
maxspeed = g_outgauge.speed;
speedtimer = 15.0f;
}
@ -576,26 +614,35 @@ int main(int argc, char **argv)
speed.rangemax = car.maxspeed;
speed2.rangemax = speed.rangemax * 1.609f;
}
if (hurr < 0) hurr = 0;
if (hurr > 100) hurr = 100;
if (hurr < 0)
hurr = 0;
if (hurr > 100)
hurr = 100;
if (speed.rangemax < 150) {
if (speed.rangemax < 150)
{
speed.majorstep = 10;
speed.minorstep = 2;
} else {
}
else
{
speed.majorstep = 20;
speed.minorstep = 10;
}
if (speed2.rangemax < 150) {
if (speed2.rangemax < 150)
{
speed2.majorstep = 10;
speed2.minorstep = 2;
} else {
}
else
{
speed2.majorstep = 20;
speed2.minorstep = 10;
}
float gsize = width * 0.20;
if (gsize > height * 0.4) gsize = height * 0.4;
if (gsize > height * 0.4)
gsize = height * 0.4;
speed.radius = gsize * 0.9;
speed2.radius = speed.radius * 0.6;
rpm.radius = speed.radius;
@ -612,16 +659,20 @@ int main(int argc, char **argv)
float lsize = sgsize * 0.16;
glPushMatrix();
if (rpmleft) {
if (rpmleft)
{
glTranslatef(width * 0.5 + gsize * 1.5, height - gsize, 0);
} else {
}
else
{
glTranslatef(width * 0.5 - gsize * 1.5, height - gsize, 0);
}
glColor4f(1.0, 1.0, 1.0, 1.0);
snprintf(text, sizeof text, "%d", (int)(g_outgauge.speed * speedunit));
drawText(text, font, 20, -speed.radius * 0.8, msize, msize, TA_RIGHT, TA_BOTTOM);
drawText((g_outgauge.flags & OG_KM) ? "km/h" : "mph", font, 26, -speed.radius * 0.8, msize * 0.3, msize * 0.4, TA_LEFT, TA_BOTTOM);
if (g_outgauge.dashlights & (1 << DL_PITSPEED)) {
if (g_outgauge.dashlights & (1 << DL_PITSPEED))
{
drawSymbol2(DL_PITSPEED, g_outgauge.showlights & (1 << DL_PITSPEED), 1, 0, gsize * -0.4, symsize * 1.3, symsize * 1.3);
}
if (maxspeed > 0.001f)
@ -641,31 +692,39 @@ int main(int argc, char **argv)
glPopMatrix();
glPushMatrix();
if (rpmleft) {
if (rpmleft)
{
glTranslatef(width * 0.5 - gsize * 1.5, height - gsize, 0);
} else {
}
else
{
glTranslatef(width * 0.5 + gsize * 1.5, height - gsize, 0);
}
glColor4f(1.0, 1.0, 1.0, 1.0);
snprintf(text, sizeof text, "%d", (int)(g_outgauge.rpm));
drawText(text, font, 40, -rpm.radius * 0.8, msize, msize, TA_RIGHT, TA_BOTTOM);
drawText("rpm", font, 46, -rpm.radius * 0.8, msize * 0.3, msize * 0.4, TA_LEFT, TA_BOTTOM);
if (g_outgauge.dashlights & (1 << DL_SHIFT)) {
if (g_outgauge.dashlights & (1 << DL_SHIFT))
{
drawSymbol2(DL_SHIFT, g_outgauge.showlights & (1 << DL_SHIFT), 1, 0, gsize * -0.4, symsize * 1.3, symsize * 1.3);
}
draw_gauge(&rpm, g_outgauge.rpm, car.maxrpm, font, GT_NEEDLE);
glPopMatrix();
if (g_owncar == g_outgauge.playerid) {
if (g_owncar == g_outgauge.playerid)
{
glPushMatrix();
glTranslatef(width * 0.5 - gsize * 1.5, fuel.radius * 0.75, 0);
drawSymbol2(4, 0, 0, fuel.radius * -0.65, 0, lsize, lsize);
if (g_outgauge.fuel <= warn_fuel2) {
if (flash < 0.5) {
if (g_outgauge.fuel <= warn_fuel2)
{
if (flash < 0.5)
{
drawSymbol2(19, 1, 0, -fuel.radius * 0.6, -fuel.radius * 0.3, lsize, lsize);
}
}
else if (g_outgauge.fuel <= warn_fuel1) {
else if (g_outgauge.fuel <= warn_fuel1)
{
drawSymbol2(19, 0, 0, -fuel.radius * 0.6, -fuel.radius * 0.3, lsize * 0.6, lsize * 0.6);
}
draw_gauge(&fuel, g_outgauge.fuel, -1, font, GT_BAR);
@ -695,10 +754,13 @@ int main(int argc, char **argv)
if (g_outgauge.flags & OG_TURBO)
{
if (g_outgauge.flags & OG_BAR) {
if (g_outgauge.flags & OG_BAR)
{
boost.majorstep = 0.4;
boost.minorstep = 0.1;
} else {
}
else
{
boost.majorstep = 2.0;
boost.minorstep = 1.0;
}
@ -747,33 +809,46 @@ int main(int argc, char **argv)
if (g_outgauge.dashlights & (1 << DL_SIGNAL_ANY))
{
drawSymbol2(DL_SIGNAL_ANY, g_outgauge.showlights & (1 << DL_SIGNAL_ANY), 1, 0, 0, symsize, symsize);
} else {
if (g_outgauge.dashlights & (1 << DL_SIGNAL_L)) {
}
else
{
if (g_outgauge.dashlights & (1 << DL_SIGNAL_L))
{
drawSymbol2(DL_SIGNAL_L, g_outgauge.showlights & (1 << DL_SIGNAL_L), 1, -symsize * 0.75, 0, symsize, symsize);
}
if (g_outgauge.dashlights & (1 << DL_SIGNAL_R)) {
if (g_outgauge.dashlights & (1 << DL_SIGNAL_R))
{
drawSymbol2(DL_SIGNAL_R, g_outgauge.showlights & (1 << DL_SIGNAL_R), 1, symsize * 0.75, 0, symsize, symsize);
}
}
if (g_outgauge.dashlights & (1 << DL_FULLBEAM)) {
if (g_outgauge.dashlights & (1 << DL_FULLBEAM))
{
drawSymbol2(DL_FULLBEAM, g_outgauge.showlights & (1 << DL_FULLBEAM), 1, 0, symspace, symsize, symsize);
}
if (g_outgauge.dashlights & (1 << DL_HANDBRAKE)) {
if (g_outgauge.dashlights & (1 << DL_HANDBRAKE))
{
drawSymbol2(DL_HANDBRAKE, g_outgauge.showlights & (1 << DL_HANDBRAKE), 1, 0, symspace * 2, symsize, symsize);
}
if (g_outgauge.dashlights & (1 << DL_ABS)) {
if (g_outgauge.dashlights & (1 << DL_ABS))
{
drawSymbol2(DL_ABS, g_outgauge.showlights & (1 << DL_ABS), 1, 0, symspace * 3, symsize, symsize);
}
if (g_outgauge.dashlights & (1 << DL_TC)) {
if (g_outgauge.dashlights & (1 << DL_TC))
{
drawSymbol2(DL_TC, g_outgauge.showlights & (1 << DL_TC), 1, 0, symspace * 4, symsize, symsize);
}
if (g_outgauge.dashlights & (1 << DL_OILWARN)) {
if (g_outgauge.dashlights & (1 << DL_BATTERY)) {
if (g_outgauge.dashlights & (1 << DL_OILWARN))
{
if (g_outgauge.dashlights & (1 << DL_BATTERY))
{
drawSymbol2(DL_BATTERY, g_outgauge.showlights & (1 << DL_BATTERY), 1, -symsize * 0.75, symspace * 5, symsize, symsize);
}
drawSymbol2(DL_OILWARN, g_outgauge.showlights & (1 << DL_OILWARN), 1, symsize * 0.75, symspace * 5, symsize, symsize);
} else {
if (g_outgauge.dashlights & (1 << DL_BATTERY)) {
}
else
{
if (g_outgauge.dashlights & (1 << DL_BATTERY))
{
drawSymbol2(DL_BATTERY, g_outgauge.showlights & (1 << DL_BATTERY), 1, 0, symspace * 5, symsize, symsize);
}
}
@ -808,11 +883,16 @@ int main(int argc, char **argv)
glPushMatrix();
glColor4f(0.15, 0.15, 1.0, 1.0);
glTranslatef(width * 0.5, 0, 0.0f);
if (g_outgauge.gear == 0) {
if (g_outgauge.gear == 0)
{
snprintf(text, sizeof text, "R");
} else if (g_outgauge.gear == 1) {
}
else if (g_outgauge.gear == 1)
{
snprintf(text, sizeof text, "N");
} else {
}
else
{
snprintf(text, sizeof text, "%d", g_outgauge.gear - 1);
}
drawText(text, font, 0, sgsize * 0.7, sgsize * 0.7, sgsize * 0.7, TA_CENTRE, TA_BOTTOM);
@ -901,21 +981,29 @@ int main(int argc, char **argv)
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) {
if (s1 != os1)
{
s1 = os1;
if (!g_fade) audio_play(0, s1);
if (!g_fade)
audio_play(0, s1);
}
if (s2 != os2) {
if (s2 != os2)
{
s2 = os2;
if (!g_fade) audio_play(1, s2);
if (!g_fade)
audio_play(1, s2);
}
if (s3 != os3) {
if (s3 != os3)
{
s3 = os3;
if (!g_fade) audio_play(2, s3);
if (!g_fade)
audio_play(2, s3);
}
if (s6 != os6) {
if (s6 != os6)
{
s6 = os6;
if (!g_fade) audio_play(5, s6);
if (!g_fade)
audio_play(5, s6);
}
if (g_outgauge.fuel > warn_fuel1 || g_outgauge.rpm == 0)
@ -929,18 +1017,23 @@ int main(int argc, char **argv)
else if (bing != 1 && g_outgauge.fuel <= warn_fuel1)
{
bing = 1;
if (!g_fade) audio_play(3, FX_BING);
if (!g_fade)
audio_play(3, FX_BING);
}
if (g_fade && fade < 0.8) {
if (g_fade && fade < 0.8)
{
fade = fade + 0.01;
} else if (!g_fade && fade > 0.0) {
}
else if (!g_fade && fade > 0.0)
{
fade = fade - 0.01;
}
// glPopMatrix();
if (dofade && fade > 0) {
if (dofade && fade > 0)
{
glColor4f(0.0, 0.0, 0.0, fade);
glBegin(GL_QUADS);
glVertex3f(0.0, 0.0, 0.0);
@ -950,11 +1043,12 @@ int main(int argc, char **argv)
glEnd();
}
glfwSwapBuffers();
glfwSwapBuffers(window);
glfwPollEvents();
// printf("%f\n", g_outgauge.speed * 2.23693629);
running = !glfwGetKey(GLFW_KEY_ESC) && glfwGetWindowParam( GLFW_OPENED) && !glfwGetMouseButton(1);
running = !glfwGetKey(window, GLFW_KEY_ESCAPE) && !glfwWindowShouldClose(window) && !glfwGetMouseButton(window, 1);
/*
int wheel = glfwGetMouseWheel();
if (wheel >= 1) {
@ -965,7 +1059,7 @@ int main(int argc, char **argv)
*/
// printf("%u %u %u\n", g_outgauge.flags, g_outgauge.dashlights, g_outgauge.showlights);
usleep(100);
usleep(1000);
}
s_running = 0;
@ -991,4 +1085,3 @@ int main(int argc, char **argv)
return 0;
}

6
list.h
View File

@ -45,7 +45,8 @@ static inline void X ## _list_del_item(struct X ## _list_t *list, T item) \
size_t i; \
for (i = 0; i < list->used; i++) \
{ \
if (compare_func(&list->items[i], &item)) { \
if (compare_func(&list->items[i], &item)) \
{ \
list->items[i] = list->items[--list->used]; \
return; \
} \
@ -62,7 +63,8 @@ static inline int X ## _list_contains(struct X ## _list_t *list, T item) \
size_t i; \
for (i = 0; i < list->used; i++) \
{ \
if (compare_func(&list->items[i], &item)) { \
if (compare_func(&list->items[i], &item)) \
{ \
return 1; \
} \
} \

View File

@ -188,7 +188,8 @@ int network_listen(int port, int tcp)
}
socket_set_nonblock(fd);
if (tcp) socket_set_nodelay(fd);
if (tcp)
socket_set_nodelay(fd);
return fd;
}

View File

@ -39,14 +39,17 @@ int outgauge_recv(int fd, int can_write, int can_read, void *arg)
/* Ignore out of order packet */
// if (o.time < s_prev_time)
if (o.rpm < 0) o.rpm = 0;
if (o.rpm < 0)
o.rpm = 0;
memcpy(&g_outgauge, &o, len);
g_fade &= ~1;
g_released |= g_pressed & (~(o.flags & 3));
if (!(g_pressed & 1) && (o.flags & 1)) g_shift_time = o.time;
if (!(g_pressed & 2) && (o.flags & 2)) g_ctrl_time = o.time;
if (!(g_pressed & 1) && (o.flags & 1))
g_shift_time = o.time;
if (!(g_pressed & 2) && (o.flags & 2))
g_ctrl_time = o.time;
g_pressed |= (o.flags & 3);
if (g_owncar != o.playerid)
@ -100,6 +103,7 @@ int outgauge_recv(int fd, int can_write, int can_read, void *arg)
void outgauge_init(int port)
{
int fd = network_listen(port, 0);
if (fd < 0) return;
if (fd < 0)
return;
register_socket(fd, SM_READ, &outgauge_recv, NULL);
}

View File

@ -37,7 +37,6 @@ extern int g_released;
extern uint32_t g_shift_time;
extern uint32_t g_ctrl_time;
void outgauge_init(int port);
#endif /* OUTGAUGE_H */

View File

@ -55,7 +55,8 @@ void queue_delete(struct queue_t *queue)
int queue_produce(struct queue_t *queue, void *data)
{
if (queue == NULL) return 0;
if (queue == NULL)
return 0;
struct queue_object_t *qo = malloc(sizeof *qo);
qo->next = NULL;

View File

@ -54,7 +54,8 @@ static struct socket_t *socket_get_by_fd(int fd)
for (i = 0; i < s_sockets.used; i++)
{
struct socket_t *s = s_sockets.items[i];
if (s->fd == fd) return s;
if (s->fd == fd)
return s;
}
return NULL;
@ -76,14 +77,16 @@ void register_socket(int fd, int mode, socket_func socket_func, void *arg)
if (s_epoll_fd == -1)
{
s_epoll_fd = epoll_create(32);
if (s_epoll_fd == -1) fprintf(stderr, "register_socket(): epoll_create: %s\n", strerror(errno));
if (s_epoll_fd == -1)
fprintf(stderr, "register_socket(): epoll_create: %s\n", strerror(errno));
}
struct epoll_event ev;
ev.events = EPOLLIN;
ev.data.ptr = s;
int r = epoll_ctl(s_epoll_fd, EPOLL_CTL_ADD, fd, &ev);
if (r == -1) fprintf(stderr, "register_socket(): epoll_ctl: %s\n", strerror(errno));
if (r == -1)
fprintf(stderr, "register_socket(): epoll_ctl: %s\n", strerror(errno));
#endif /* USE_POLL */
pthread_mutex_unlock(&s_sockets_mutex);
@ -94,9 +97,11 @@ void socket_flag_write(int fd)
#ifdef USE_POLL
struct epoll_event ev;
ev.events = EPOLLIN | EPOLLOUT;
ev.data.ptr = socket_get_by_fd(fd);;
ev.data.ptr = socket_get_by_fd(fd);
;
int r = epoll_ctl(s_epoll_fd, EPOLL_CTL_MOD, fd, &ev);
if (r == -1) fprintf(stderr, "socket_flag_write(): epoll_ctl: %s\n", strerror(errno));
if (r == -1)
fprintf(stderr, "socket_flag_write(): epoll_ctl: %s\n", strerror(errno));
#endif /* USE_POLL */
}
@ -107,7 +112,8 @@ void socket_clear_write(int fd)
ev.events = EPOLLIN;
ev.data.ptr = socket_get_by_fd(fd);
int r = epoll_ctl(s_epoll_fd, EPOLL_CTL_MOD, fd, &ev);
if (r == -1) fprintf(stderr, "socket_clear_write(): epoll_ctl: %s\n", strerror(errno));
if (r == -1)
fprintf(stderr, "socket_clear_write(): epoll_ctl: %s\n", strerror(errno));
#endif /* USE_POLL */
}
@ -182,7 +188,8 @@ void *socket_run(void *arg)
{
struct epoll_event events[EPOLL_EVENTS];
int n = epoll_wait(s_epoll_fd, events, EPOLL_EVENTS, 10000);
if (n == -1) fprintf(stderr, "socket_run(): epoll_wait: %s\n", strerror(errno));
if (n == -1)
fprintf(stderr, "socket_run(): epoll_wait: %s\n", strerror(errno));
int i;
for (i = 0; i < n; i++)
@ -221,8 +228,10 @@ void *socket_run(void *arg)
int fd = s_sockets.items[i]->fd;
if (fd >= 0)
{
if (s_sockets.items[i]->mode & SM_READ ) FD_SET(fd, &read_fd);
if (s_sockets.items[i]->mode & SM_WRITE) FD_SET(fd, &write_fd);
if (s_sockets.items[i]->mode & SM_READ)
FD_SET(fd, &read_fd);
if (s_sockets.items[i]->mode & SM_WRITE)
FD_SET(fd, &write_fd);
}
}
@ -235,7 +244,8 @@ void *socket_run(void *arg)
fprintf(stderr, "select: %s\n", strerror(errno));
return NULL;
}
if (n == 0) continue;
if (n == 0)
continue;
for (i = 0; i < s_sockets.used; i++)
{

View File

@ -3,7 +3,8 @@
extern int s_running;
enum {
enum
{
SM_READ = 1,
SM_WRITE = 2,
};

BIN
symbtols512.png 100644

Binary file not shown.

After

Width:  |  Height:  |  Size: 140 KiB

26
text.c
View File

@ -1,4 +1,4 @@
#include <GL/glfw.h>
#include <GLFW/glfw3.h>
#include <FTGL/ftgl.h>
#include "text.h"
@ -12,15 +12,27 @@ void drawText(const char *text, FTGLfont *font, float x, float y, float xs, floa
float ox = 0, oy = 0;
switch (xa)
{
case TA_LEFT: ox = 0; break;
case TA_RIGHT: ox = -1; break;
case TA_CENTRE: ox = -0.5; break;
case TA_LEFT:
ox = 0;
break;
case TA_RIGHT:
ox = -1;
break;
case TA_CENTRE:
ox = -0.5;
break;
}
switch (ya)
{
case TA_TOP: oy = -1; break;
case TA_BOTTOM: oy = 0; break;
case TA_CENTRE: oy = -0.5; break;
case TA_TOP:
oy = -1;
break;
case TA_BOTTOM:
oy = 0;
break;
case TA_CENTRE:
oy = -0.5;
break;
}
glPushMatrix();

3
text.h
View File

@ -3,7 +3,8 @@
#define TEXT_SIZE 64
enum {
enum
{
TA_LEFT,
TA_RIGHT,
TA_TOP,

View File

@ -38,7 +38,8 @@ void *worker_thread(void *arg)
if (s == -1)
{
worker->thread_timeout = true;
if (errno == ETIMEDOUT) {
if (errno == ETIMEDOUT)
{
timeout = true;
break;
}
@ -50,7 +51,8 @@ void *worker_thread(void *arg)
if (queue_consume(worker->queue, &data))
{
/* Null item added to the queue indicate we should exit. */
if (data == NULL) break;
if (data == NULL)
break;
worker->callback(data);
jobs++;
@ -129,5 +131,3 @@ void worker_queue(struct worker *worker, void *data)
fprintf(stderr, "Queue worker %s unable to queue\n", worker->name);
}
}