IEM2_BOUWMANS_TP1/main.c
Theo d2789fadfc [2.3👍] Coures affichées
Fonctions déjà implémentées pour l'exo 2.4

On parle pas du bug 🤫
2024-09-08 23:50:24 +02:00

210 lines
4.0 KiB
C

#include <stdio.h>
#include <stdbool.h>
#include <stdlib.h>
#include <string.h>
#include <GL/glut.h>
void afficher(void);
int initWindow(int, char**, void(*)(void));
void destroyWindow(int);
long getFileSize(FILE*);
void parseFile(char*, long);
bool charger(char*);
void printCourbeTerminal(void);
void ligne(int);
float map(float, float, float, float, float);
// Variables donneés
#define MAXC 20
#define MAXV 100
float courbe[MAXC][MAXV];
float valmin[MAXC];
float valmax[MAXC];
int nc = 0;
float map(float smin, float smax, float val, float omin, float omax)
{
float percent = (val - smin) / (smax - smin);
//printf("v%f, p%f, min%f, max%f\n", val, percent, omin, omax);
return (omax-omin) * percent + omin;
}
void ligne(int index)
{
if(index >= nc)
{
printf("Courbe non-existante.");
return;
}
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(-1.0,1.0,-1.0,1.0,-1.0,1.0);
glColor3f(1.0,1.0,1.0);
glBegin(GL_LINE_STRIP);
unsigned int size = (unsigned int)courbe[index][0];
for(unsigned int i = 1; i < size; i++)
{
float pos_x = map(0, size, i-1, -1.0, 1.0);
float pos_y = map(valmin[index], valmax[index], courbe[index][i], -1.0, 1.0);
glVertex2f(pos_x, pos_y);
//printf("%d : {%f, %f}\n", i, pos_x, pos_y);
}
fflush(stdout);
glEnd();
}
void afficher(void)
{
glClearColor(0.0,0.0,0.0,0.0);
glClear(GL_COLOR_BUFFER_BIT);
ligne(0);
glFlush();
}
int initWindow(int argc, char* argv[], void(*display)(void))
{
int vx1 = 0, vx2 = 400, vy1 = 0, vy2 = 400;
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE|GLUT_RGBA);
glutInitWindowPosition(vx1, vy1);
glutInitWindowSize(vx2, vy2);
int window = glutCreateWindow("TP1");
glutDisplayFunc(display);
return window;
}
void destroyWindow(int window)
{
glutDestroyWindow(window);
}
long getFileSize(FILE* file)
{
fseek(file, 0, SEEK_END);
long fileSize = ftell(file);
rewind(file);
return fileSize;
}
void parseFile(char* file, long size)
{
nc = atoi(file);
printf("nc=%d\n", nc);
printf("====\n%s\n====\n", file);
// Pointeur mobile dans le fichier
char* pointeur = file;
unsigned int line = 0;
while(line < nc+1)
{
/* Cherche le premier \n à partir du pointeur
* /!\ on suppose un fichier encodé en UNIX (LF) /!\ */
pointeur = strchr(pointeur, '\n') + 1;
// printf(" ->\"%c\"", *pointeur);
int taille_tableau = atoi(pointeur);
courbe[line][0] = taille_tableau;
// printf(" ->\"%d\" \n", taille_tableau);
// On suppose que les nombres sont séparés par un espace
for(unsigned int i = 1; i < taille_tableau; i++)
{
pointeur = strchr(pointeur, ' ') + 1;
float num = strtof(pointeur, 0);
courbe[line][i] = num;
if(i == 1)
{
valmin[line] = num;
valmax[line] = num;
}else{
if(num < valmin[line])
valmin[line] = num;
if(num > valmax[line])
valmax[line] = num;
}
}
line++;
}
}
bool charger(char* nom)
{
FILE* file = fopen(nom, "r");
if(file == NULL)
{
printf("Erreur ouverture fichier.\n");
return false;
}
long fileSize = getFileSize(file);
char* buffer = (char*)malloc(sizeof(char) * fileSize + 1);
if(buffer == NULL)
{
printf("Erreur allocation mémoire.\n");
return false;
}
buffer[fileSize] = 0; // NULL à la fin du fichier
int res = fread(buffer, sizeof(char), fileSize, file);
if(res == 0)
{
printf("Erreur lecture fichier.\n");
return false;
}
fclose(file);
parseFile(buffer, fileSize);
free(buffer);
return true;
}
void printCourbeTerminal(void)
{
for(unsigned int i = 0; i < MAXC; i++)
{
for(unsigned int j = 0; j < MAXV; j++)
{
printf("%f", courbe[i][j]);
if(j < MAXV-1)
printf(", ");
}
printf("\n");
}
}
int main(int argc, char* argv[])
{
if(!charger("valeurs.txt"))
return EXIT_FAILURE;
//printCourbeTerminal();
int window = initWindow(argc, argv, afficher);
glutMainLoop();
destroyWindow(window);
return EXIT_SUCCESS;
}