diff --git a/LibGui/Makefile b/LibGui/Makefile index 8aa4751..1039f2c 100644 --- a/LibGui/Makefile +++ b/LibGui/Makefile @@ -7,7 +7,7 @@ INCLUDEDIR=-I/usr/include -I. -I../LibMatrix #Library variables LIBTARGET=libGui.so -LIBSOURCE=pixel +LIBSOURCE=gui LIBSOURCECFILE=$(LIBSOURCE:=.c) LIBSOURCEOFILE=$(LIBSOURCE:=.o) diff --git a/LibGui/pixel.c b/LibGui/gui.c similarity index 66% rename from LibGui/pixel.c rename to LibGui/gui.c index b26926b..2fa73ec 100644 --- a/LibGui/pixel.c +++ b/LibGui/gui.c @@ -2,15 +2,17 @@ * @Author: klmp200 * @Date: 2016-12-27 19:59:21 * @Last Modified by: klmp200 -* @Last Modified time: 2016-12-29 00:03:19 +* @Last Modified time: 2016-12-29 02:51:25 */ #include #include #include -#include +#include #include #include +#include +#include void SetPixel(SDL_Renderer *renderer, int x, int y, bool value){ @@ -88,3 +90,51 @@ int NewWindowFromMatrix(Matrix m){ return 0; } + +bool YesOrNo(char message[]){ + char response; + + printf("%s (Y/n)\n", message); + response = getchar(); + ClearBuffer(); + + if (response == 'n'){ + return false; + } else { + return true; + } + +} + +void ClearBuffer(){ + char c; + while ((c = getchar()) != '\n' && c != EOF) { } +} + +void DisplayMatrixGUI(Matrix m, bool useSDL){ + if (useSDL){ + printf("Press ENTER or the red cross to exit the window and continue.\n"); + NewWindowFromMatrix(m); + } else { + BasicPrintMatrix(m); + } +} + +int SafeNumberInput(int min, int max){ + char str[30]; + int input; + + fgets(str, 29, stdin); + ClearBuffer(); + + input = atoi(str); + + while (input < min || input > max){ + printf("The number should be between %d and %d\n", min, max); + fgets(str, 29, stdin); + ClearBuffer(); + input = atoi(str); + } + + return input; +} diff --git a/LibGui/pixel.h b/LibGui/gui.h similarity index 63% rename from LibGui/pixel.h rename to LibGui/gui.h index c2f8803..dbabf90 100644 --- a/LibGui/pixel.h +++ b/LibGui/gui.h @@ -45,4 +45,33 @@ void WaitUntilEnter(SCREEN * screen); */ int NewWindowFromMatrix(Matrix m); +/** +* Display a message and ask for yes or no to the user +* @param message the message to display +* @return bool the answer +*/ +bool YesOrNo(char message[]); + +/** +* Display matrix choosing the right function +* @param m a Matrix +* @param useSDL a bool +* @return +*/ +void DisplayMatrixGUI(Matrix m, bool useSDL); + +/** +* Get a number from the user safely +* @param min the minimal authorized number +* @param max the maximal authorized number +* @return int the input number +*/ +int SafeNumberInput(int min, int max); + +/** +* Clears the buffer of stdin +* @return +*/ +void ClearBuffer(); + #endif diff --git a/LibMatrix/matrix.c b/LibMatrix/matrix.c index 5aea483..873117f 100644 --- a/LibMatrix/matrix.c +++ b/LibMatrix/matrix.c @@ -9,16 +9,15 @@ Matrix applyRules (Matrix matrix,int Rules, int N){ int power = 2; int sum = 0; int j = 0; - Matrix tempMatrix; + Matrix tempMatrix1; + Matrix tempMatrix2; if (Rules <= 0 || N < 1){ return matrix; - } - - tempMatrix = CreateMatrix(); + } while(power<=512){ - + RulesMatrix[i] = Rules%power - sum; sum = Rules%power; @@ -29,17 +28,20 @@ Matrix applyRules (Matrix matrix,int Rules, int N){ power*=2; } - /* test code : print decomposition */ - /*for (j=0;j #include #include -#include +#include int main(){ - - int Rule34 = 3; + + int rule; + int times; + bool useSDL; + bool cont = true; + int col; + int row; Matrix m1; Matrix m2; - BooleanMatrix bmatrix = CreateBooleanMatrix(5, 5); + BooleanMatrix bmatrix; + + useSDL = YesOrNo("Do you want to use SDL library for matrix display ?"); + printf("A random matrix will be generated\n"); + printf("Enter the number of columns of this matrix\n"); + col = SafeNumberInput(1, 30000); + printf("Enter the number of rows of this matrix\n"); + row = SafeNumberInput(1, 30000); + bmatrix = CreateBooleanMatrix(col, row); bmatrix = RandomizeBooleanMatrix(bmatrix); m1 = newMatrix(bmatrix); - FreeBooleanMatrix(bmatrix); - NewWindowFromMatrix(m1); + DisplayMatrixGUI(m1, useSDL); - m2 = applyRules(m1,Rule34,3); + while (cont){ + printf("What rule do you want to apply to this matrix ?\n"); + rule = SafeNumberInput(1, 481); + printf("How many times do you want the rule to be applied ?\n"); + times = SafeNumberInput(1, 100000); + m2 = applyRules(m1,rule,times); + freeMatrix(m1); - NewWindowFromMatrix(m2); + DisplayMatrixGUI(m2, useSDL); + + cont = YesOrNo("Do you want to apply other rules on this matrix ?"); + m1 = m2; + } - freeMatrix(m1); freeMatrix(m2); - + return 0; }