178 lines
3.3 KiB
C
178 lines
3.3 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 destroyWindow(int);
|
|
long getFileSize(FILE*);
|
|
void parseFile(char*, long);
|
|
bool charger(char*);
|
|
void printCourbeTerminal(void);
|
|
|
|
// Variables donneés
|
|
#define MAXC 20
|
|
#define MAXV 100
|
|
|
|
float courbe[MAXC][MAXV];
|
|
float valmin[MAXC];
|
|
float valmax[MAXC];
|
|
int nc = 0;
|
|
|
|
|
|
void afficher(void)
|
|
{
|
|
glMatrixMode(GL_PROJECTION);
|
|
glLoadIdentity();
|
|
glOrtho(-1.0,1.0,-1.0,1.0,-1.0,1.0);
|
|
glClearColor(0.0,0.0,0.0,0.0);
|
|
glClear(GL_COLOR_BUFFER_BIT);
|
|
glColor3f(1.0,1.0,1.0);
|
|
glBegin(GL_POLYGON);
|
|
glVertex2f(-0.7,-0.7);
|
|
glVertex2f(-0.7, 0.7);
|
|
glVertex2f( 0.7, 0.7);
|
|
glVertex2f( 0.7,-0.7);
|
|
glEnd();
|
|
glFlush();
|
|
}
|
|
|
|
int initWindow(int argc, char* argv[])
|
|
{
|
|
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(afficher);
|
|
|
|
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);
|
|
// printf(" ->\"%d\" \n", taille_tableau);
|
|
|
|
// On suppose que les nombres sont séparés par un espace
|
|
for(unsigned int i = 0; i < taille_tableau; i++)
|
|
{
|
|
pointeur = strchr(pointeur, ' ') + 1;
|
|
float num = strtof(pointeur, 0);
|
|
courbe[line][i] = num;
|
|
|
|
if(i == 0)
|
|
{
|
|
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[])
|
|
{
|
|
//int window = initWindow(argc, argv);
|
|
|
|
//glutMainLoop();
|
|
|
|
if(!charger("valeurs.txt"))
|
|
return EXIT_FAILURE;
|
|
|
|
//printCourbeTerminal();
|
|
|
|
//destroyWindow(window);
|
|
return EXIT_SUCCESS;
|
|
}
|