<commit_before>#include "display.h"

#include <stdio.h>
#include <string.h>
#include <math.h>
#include <assert.h>

#if defined TARGET_GLUT
   #include <GL/freeglut.h>
#elif defined TARGET_RASPI
   #include <bcm_host.h>
   #include <GLES/gl.h>
   #include <EGL/egl.h>
   #include <EGL/eglext.h>
   #include <sys/time.h>
   #include <time.h>
#endif

#include "config.h"
#include "color.h"
#include "simulation.h"

#if defined TARGET_GLUT
   #ifndef GL_MULTISAMPLE
   #define GL_MULTISAMPLE  0x809D
   #endif
   #ifndef GL_CLAMP_TO_EDGE
   #define GL_CLAMP_TO_EDGE  0x812F
   #endif
#elif defined TARGET_RASPI
   #define glOrtho glOrthof
#endif

static void initSystem(int* argc, char** argv);
static void swapBuffers(void);
static unsigned long getTime(void);

typedef struct
{
   rgb color;
   float fadeout;
} UiPlayer;

static UiPlayer* uiPlayer;
static GLuint tex;
static float vertCircle[32][2];
static float left, right, bottom, top, zoom;
static int screenW, screenH;
static int fps;

static void drawString(char* str, float x, float y, float r, float g, float b)
{
   const char *p;

   glEnable(GL_TEXTURE_2D);
   glEnableClientState(GL_TEXTURE_COORD_ARRAY);
   glBindTexture(GL_TEXTURE_2D, tex);

   glColor4f(r, g, b, 1.0);

   /* Loop through all characters */
   for (p = str; *p; ++p)
   {
      /* Calculate the vertex and texture coordinates */
      float vert[] =
      {
         x,        y - 24.0,
         x + 16.0, y - 24.0,
         x,        y,
         x + 16.0, y,
      };
      float texc[] =
      {
         (*p - 32) / 96.0, 0.0,
         (*p - 31) / 96.0, 0.0,
         (*p - 32) / 96.0, 1.0,
         (*p - 31) / 96.0, 1.0,
      };
      
      /* Draw the character on the screen */
      glVertexPointer(2, GL_FLOAT, 0, vert);
      glTexCoordPointer(2, GL_FLOAT, 0, texc);
      glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
      
      /* Advance the cursor to the start of the next character */
      x += 14.0;
   }
   glDisableClientState(GL_TEXTURE_COORD_ARRAY);
   glDisable(GL_TEXTURE_2D);
}

static void drawFps(void)
{
   static unsigned long timeOld;
   static int frameCounter;
   static float dfps;

   unsigned long time;
   char buffer[16];

   frameCounter++;
   time = getTime();

   if(time > timeOld + 500)
   {
      dfps = frameCounter * 1000.0 / (time - timeOld);

      frameCounter = 0;
      timeOld = time;
   }

   sprintf(buffer, "%.1lf fps", dfps);

   drawString(buffer, 3.0, 48.0, 1.0, 1.0, 1.0);
}

static void drawPlayers(void)
{
   static char buffer[128];
   float x, y;
   int p;

   for(p = 0; p < conf.maxPlayers; ++p)
   {
      SimPlayer* pl = getPlayer(p);
      if(!pl->active) continue;

      sprintf(buffer, "%s%s (%d:%d)%s", p == getCurrentPlayer() ? "> " : "  ", pl->name, pl->kills, pl->deaths, p == getCurrentPlayer() ? " <" : "");
      if(conf.oneline)
      {
         x = p * (float)screenW / conf.maxPlayers + 3.0;
         y = 24.0;
      }
      else
      {
         x = (p / 2) * ((float)screenW / ((conf.maxPlayers + 1) / 2)) + 3.0;
         y = (p % 2) ? screenH - 3.0 : 24.0;
      }
      drawString(buffer, x, y, uiPlayer[p].color.r, uiPlayer[p].color.g, uiPlayer[p].color.b);
   }
}

static void draw(void)
{
   int i, p, s;
   float assumedW;

   glViewport(0, 0, screenW, screenH);

   glMatrixMode(GL_PROJECTION);
   glLoadIdentity();
   glOrtho(left, right, bottom, top, -100, 100); /* left, right, bottom, top, near, far */

   glMatrixMode(GL_MODELVIEW);
   glLoadIdentity();

   glClear(GL_COLOR_BUFFER_BIT);

   glTranslatef((right - left) / 2, (bottom - top) / 2, 0);
   glScalef(zoom, zoom, zoom);
   glTranslatef((left - right) / 2, (top - bottom) / 2, 0);

   for(p = 0; p < conf.maxPlayers; ++p)
   {
      SimPlayer* pl = getPlayer(p);
      if(!pl->active) continue;
      uiPlayer[p].fadeout = LIMIT(uiPlayer[p].fadeout - 0.02, 0.0, 1.0);
      for(s = 0; s < conf.numShots; ++s)
      {
         double bright;
         SimShot* shot;
         shot = getShot(p, s);
         if(shot->missile.live && shot->length < 4)
         {
            uiPlayer[p].fadeout = 1.0;
         }
         bright = (double)(conf.numShots - s) / conf.numShots;
         if(s > 0)
         {
            bright += uiPlayer[p].fadeout / conf.numShots;
         }
         bright *= bright;
         glColor4f(uiPlayer[p].color.r, uiPlayer[p].color.g, uiPlayer[p].color.b, bright);
         glVertexPointer(2, GL_FLOAT, 0, shot->dot);
         glDrawArrays(GL_LINE_STRIP, 0, shot->length);
      }
      glColor4f(uiPlayer[p].color.r, uiPlayer[p].color.g, uiPlayer[p].color.b, 1.0f);
      glPushMatrix();
      glTranslatef(pl->position.x, pl->position.y, 0);
      glScalef(4.0, 4.0, 1.0);
      glVertexPointer(2, GL_FLOAT, 0, vertCircle);
      glDrawArrays(GL_LINE_LOOP, 0, 16);
      glDrawArrays(GL_LINES, 16, 16);
      glPopMatrix();
   }

   glColor4f(0.3, 0.3, 0.3, 1.0);
   for(i = 0; i < conf.numPlanets; ++i)
   {
      SimPlanet* p = getPlanet(i);
      glPushMatrix();
      glTranslatef(p->position.x, p->position.y, 0);
      glScalef(p->radius, p->radius, 1.0);
      glVertexPointer(2, GL_FLOAT, 0, vertCircle);
      glDrawArrays(GL_LINE_LOOP, 0, 16);
      glDrawArrays(GL_LINES, 16, 16);
      glPopMatrix();
   }

   glMatrixMode(GL_PROJECTION);
   glLoadIdentity();

   assumedW = screenW > 1600 ? screenW : screenW < 800 ? screenW * 2 : 1600;
   glOrtho(0, assumedW, assumedW * screenH / screenW, 0, -100, 100); /* left, right, bottom, top, near, far */

   glMatrixMode(GL_MODELVIEW);
   glLoadIdentity();

   if(fps) drawFps();
   drawPlayers();

   swapBuffers();
}

static void reshape(int w, int h)
{
   double ratioScreen = (double)w / h;
   double ratioSim = conf.battlefieldW / conf.battlefieldH;
   screenW = w;
   screenH = h;
   if(ratioScreen > ratioSim)
   {
      top = 0.0;
      bottom = conf.battlefieldH;
      left = -(conf.battlefieldH * ratioScreen - conf.battlefieldW) / 2.0;
      right = conf.battlefieldW + (conf.battlefieldH * ratioScreen - conf.battlefieldW) / 2.0;
   }
   else
   {
      top = -(conf.battlefieldW / ratioScreen - conf.battlefieldH) / 2.0;
      bottom = conf.battlefieldH + (conf.battlefieldW / ratioScreen - conf.battlefieldH) / 2.0;
      left = 0.0;
      right = conf.battlefieldW;
   }
}

void initDisplay(int* argc, char** argv)
{
   int p, i;
   GLuint font[96*24*16*4];
   FILE* fp;

   initSystem(argc, argv);

   glEnable(GL_BLEND);
   glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
   glLineWidth(1.5);
   glHint(GL_LINE_SMOOTH_HINT, GL_NICEST);
   glEnable(GL_LINE_SMOOTH);
   glClearColor(0.0, 0.0, 0.0, 1.0);
   glEnableClientState(GL_VERTEX_ARRAY);

   uiPlayer = malloc(conf.maxPlayers * sizeof(UiPlayer));

   for(p = 0; p < conf.maxPlayers; ++p)
   {
      hsv color;
      color.h = 360.0 / conf.maxPlayers * p;
      color.s = 0.8;
      color.v = 1.0;
      uiPlayer[p].color = hsv2rgb(color);
   }
   zoom = 0.8;

   for(i = 0; i < 8; ++i)
   {
      double a = 2 * M_PI / 32 + 2 * M_PI / 16 * i;
      float x = sin(a);
      float y = cos(a);
      vertCircle[i][0] = x;
      vertCircle[i][1] = y;
      vertCircle[8 + i][0] = -x;
      vertCircle[8 + i][1] = -y;
      vertCircle[16 + 2 * i][0] = x;<commit_msg>fix ui size, tune for raspi<commit_after>#include "display.h"

#include <stdio.h>
#include <string.h>
#include <math.h>
#include <assert.h>

#if defined TARGET_GLUT
   #include <GL/freeglut.h>
#elif defined TARGET_RASPI
   #include <bcm_host.h>
   #include <GLES/gl.h>
   #include <EGL/egl.h>
   #include <EGL/eglext.h>
   #include <sys/time.h>
   #include <time.h>
#endif

#include "config.h"
#include "color.h"
#include "simulation.h"

#if defined TARGET_GLUT
   #ifndef GL_MULTISAMPLE
   #define GL_MULTISAMPLE  0x809D
   #endif
   #ifndef GL_CLAMP_TO_EDGE
   #define GL_CLAMP_TO_EDGE  0x812F
   #endif
#elif defined TARGET_RASPI
   #define glOrtho glOrthof
#endif

static void initSystem(int* argc, char** argv);
static void swapBuffers(void);
static unsigned long getTime(void);

typedef struct
{
   rgb color;
   float fadeout;
} UiPlayer;

static UiPlayer* uiPlayer;
static GLuint tex;
static float vertCircle[32][2];
static float left, right, bottom, top, zoom;
static int screenW, screenH;
static float uiW, uiH;
static int fps;

static void drawString(char* str, float x, float y, float r, float g, float b)
{
   const char *p;

   glEnable(GL_TEXTURE_2D);
   glEnableClientState(GL_TEXTURE_COORD_ARRAY);
   glBindTexture(GL_TEXTURE_2D, tex);

   glColor4f(r, g, b, 1.0);

   /* Loop through all characters */
   for (p = str; *p; ++p)
   {
      /* Calculate the vertex and texture coordinates */
      float vert[] =
      {
         x,        y - 24.0,
         x + 16.0, y - 24.0,
         x,        y,
         x + 16.0, y,
      };
      float texc[] =
      {
         (*p - 32) / 96.0, 0.0,
         (*p - 31) / 96.0, 0.0,
         (*p - 32) / 96.0, 1.0,
         (*p - 31) / 96.0, 1.0,
      };
      
      /* Draw the character on the screen */
      glVertexPointer(2, GL_FLOAT, 0, vert);
      glTexCoordPointer(2, GL_FLOAT, 0, texc);
      glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
      
      /* Advance the cursor to the start of the next character */
      x += 14.0;
   }
   glDisableClientState(GL_TEXTURE_COORD_ARRAY);
   glDisable(GL_TEXTURE_2D);
}

static void drawFps(void)
{
   static unsigned long timeOld;
   static int frameCounter;
   static float dfps;

   unsigned long time;
   char buffer[16];

   frameCounter++;
   time = getTime();

   if(time > timeOld + 500)
   {
      dfps = frameCounter * 1000.0 / (time - timeOld);

      frameCounter = 0;
      timeOld = time;
   }

   sprintf(buffer, "%.1lf fps", dfps);

   drawString(buffer, 3.0, 48.0, 1.0, 1.0, 1.0);
}

static void drawPlayers(void)
{
   static char buffer[128];
   float x, y;
   int p;

   for(p = 0; p < conf.maxPlayers; ++p)
   {
      SimPlayer* pl = getPlayer(p);
      if(!pl->active) continue;

      sprintf(buffer, "%s%s (%d:%d)%s", p == getCurrentPlayer() ? "> " : "  ", pl->name, pl->kills, pl->deaths, p == getCurrentPlayer() ? " <" : "");
      if(conf.oneline)
      {
         x = p * uiW / conf.maxPlayers + 3.0;
         y = 24.0;
      }
      else
      {
         x = (p / 2) * (uiW / ((conf.maxPlayers + 1) / 2)) + 3.0;
         y = (p % 2) ? uiH - 3.0 : 24.0;
      }
      drawString(buffer, x, y, uiPlayer[p].color.r, uiPlayer[p].color.g, uiPlayer[p].color.b);
   }
}

static void draw(void)
{
   int i, p, s;

   glViewport(0, 0, screenW, screenH);

   glMatrixMode(GL_PROJECTION);
   glLoadIdentity();
   glOrtho(left, right, bottom, top, -100, 100); /* left, right, bottom, top, near, far */

   glMatrixMode(GL_MODELVIEW);
   glLoadIdentity();

   glClear(GL_COLOR_BUFFER_BIT);

   glTranslatef((right - left) / 2, (bottom - top) / 2, 0);
   glScalef(zoom, zoom, zoom);
   glTranslatef((left - right) / 2, (top - bottom) / 2, 0);

   for(p = 0; p < conf.maxPlayers; ++p)
   {
      SimPlayer* pl = getPlayer(p);
      if(!pl->active) continue;
      uiPlayer[p].fadeout = LIMIT(uiPlayer[p].fadeout - 0.02, 0.0, 1.0);
      for(s = 0; s < conf.numShots; ++s)
      {
         double bright;
         SimShot* shot;
         shot = getShot(p, s);
         if(shot->missile.live && shot->length < 4)
         {
            uiPlayer[p].fadeout = 1.0;
         }
         bright = (double)(conf.numShots - s) / conf.numShots;
         if(s > 0)
         {
            bright += uiPlayer[p].fadeout / conf.numShots;
         }
         bright *= bright;
         glColor4f(uiPlayer[p].color.r, uiPlayer[p].color.g, uiPlayer[p].color.b, bright);
         glVertexPointer(2, GL_FLOAT, 0, shot->dot);
         glDrawArrays(GL_LINE_STRIP, 0, shot->length);
      }
      glColor4f(uiPlayer[p].color.r, uiPlayer[p].color.g, uiPlayer[p].color.b, 1.0f);
      glPushMatrix();
      glTranslatef(pl->position.x, pl->position.y, 0);
      glScalef(4.0, 4.0, 1.0);
      glVertexPointer(2, GL_FLOAT, 0, vertCircle);
      glDrawArrays(GL_LINE_LOOP, 0, 16);
      glDrawArrays(GL_LINES, 16, 16);
      glPopMatrix();
   }

   glColor4f(0.3, 0.3, 0.3, 1.0);
   for(i = 0; i < conf.numPlanets; ++i)
   {
      SimPlanet* p = getPlanet(i);
      glPushMatrix();
      glTranslatef(p->position.x, p->position.y, 0);
      glScalef(p->radius, p->radius, 1.0);
      glVertexPointer(2, GL_FLOAT, 0, vertCircle);
      glDrawArrays(GL_LINE_LOOP, 0, 16);
      glDrawArrays(GL_LINES, 16, 16);
      glPopMatrix();
   }

   glMatrixMode(GL_PROJECTION);
   glLoadIdentity();

   glOrtho(0, uiW, uiH, 0, -100, 100); /* left, right, bottom, top, near, far */

   glMatrixMode(GL_MODELVIEW);
   glLoadIdentity();

   if(fps) drawFps();
   drawPlayers();

   swapBuffers();
}

static void reshape(int w, int h)
{
   double ratioScreen = (double)w / h;
   double ratioSim = conf.battlefieldW / conf.battlefieldH;
   screenW = w;
   screenH = h;
   if(ratioScreen > ratioSim)
   {
      top = 0.0;
      bottom = conf.battlefieldH;
      left = -(conf.battlefieldH * ratioScreen - conf.battlefieldW) / 2.0;
      right = conf.battlefieldW + (conf.battlefieldH * ratioScreen - conf.battlefieldW) / 2.0;
   }
   else
   {
      top = -(conf.battlefieldW / ratioScreen - conf.battlefieldH) / 2.0;
      bottom = conf.battlefieldH + (conf.battlefieldW / ratioScreen - conf.battlefieldH) / 2.0;
      left = 0.0;
      right = conf.battlefieldW;
   }
   uiW = screenW > 1600 ? screenW : screenW < 800 ? screenW * 2 : 1600;
   uiH = uiW * screenH / screenW;
}

void initDisplay(int* argc, char** argv)
{
   int p, i;
   GLuint font[96*24*16*4];
   FILE* fp;

   initSystem(argc, argv);

   glEnable(GL_BLEND);
   glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
   glLineWidth(2.0);
   glHint(GL_LINE_SMOOTH_HINT, GL_NICEST);
   glEnable(GL_LINE_SMOOTH);
   glClearColor(0.0, 0.0, 0.0, 1.0);
   glEnableClientState(GL_VERTEX_ARRAY);

   uiPlayer = malloc(conf.maxPlayers * sizeof(UiPlayer));

   for(p = 0; p < conf.maxPlayers; ++p)
   {
      hsv color;
      color.h = 360.0 / conf.maxPlayers * p;
      color.s = 0.8;
      color.v = 1.0;
      uiPlayer[p].color = hsv2rgb(color);
   }
   zoom = 0.8;

   for(i = 0; i < 8; ++i)
   {
      double a = 2 * M_PI / 32 + 2 * M_PI / 16 * i;
      float x = sin(a);
      float y = cos(a);
      vertCircle[i][0] = x;
      vertCircle[i][1] = y;
      vertCircle[8 + i][0] = -x;
      vertCircle[8 + i][1] = -y;
      vertCircle[16 + 2 * i][0] = x;
{'fraction_non_alphanumeric': 0.15312999374696032, 'fraction_numerical': 0.03612867366080734, 'mean_word_length': 2.4279590378661586, 'pattern_counts': {'":': 0, '<': 43, '<?xml version=': 0, '>': 65, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4453753', 'n_tokens_mistral': 6038, 'n_tokens_neox': 5585, 'n_words': 1258}