diff --git a/LibGui/pixel.c b/LibGui/pixel.c index 1b3d09e..f8cf988 100644 --- a/LibGui/pixel.c +++ b/LibGui/pixel.c @@ -2,7 +2,7 @@ * @Author: klmp200 * @Date: 2016-12-27 19:59:21 * @Last Modified by: klmp200 -* @Last Modified time: 2016-12-28 23:26:39 +* @Last Modified time: 2016-12-28 23:57:29 */ #include @@ -36,3 +36,52 @@ void DisplayMatrixSDL(SCREEN *screen, Matrix m){ } SDL_RenderPresent(screen->renderer); } + +void WaitUntilEnter(SCREEN * screen){ + int keypress = 0; + + while (!keypress){ + while(SDL_PollEvent(&screen->event)){ + switch (screen->event.type){ + case SDL_QUIT: + keypress = 1; + break; + case SDL_KEYDOWN: + switch(screen->event.key.keysym.sym){ + case SDLK_RETURN: + keypress = 1; + break; + } + } + } + } +} + +int NewWindowFromMatrix(Matrix m){ + SCREEN screen; + + screen.WIDTH = m.colCount; + screen.HEIGHT = m.rowCount; + + if (SDL_Init(SDL_INIT_VIDEO) < 0){ + return 1; + } + + SDL_CreateWindowAndRenderer(screen.WIDTH, screen.HEIGHT, + SDL_WINDOW_RESIZABLE | SDL_WINDOW_INPUT_FOCUS | SDL_WINDOW_SHOWN, + &(screen.window), &(screen.renderer)); + + if (screen.window == NULL || screen.renderer == NULL){ + SDL_Quit(); + return 1; + } + + DisplayMatrixSDL(&screen, m); + + WaitUntilEnter(&screen); + + SDL_DestroyRenderer(screen.renderer); + SDL_DestroyWindow(screen.window); + + return 0; +} diff --git a/LibGui/pixel.h b/LibGui/pixel.h index dbf255f..c2f8803 100644 --- a/LibGui/pixel.h +++ b/LibGui/pixel.h @@ -31,4 +31,18 @@ void SetPixel(SDL_Renderer *renderer, int x, int y, bool value); */ void DisplayMatrixSDL(SCREEN *screen, Matrix m); +/** +* Wait until the user press the enter key +* @param screen the screen where the renderer is +* @return +*/ +void WaitUntilEnter(SCREEN * screen); + +/** +* Print a matrix in a new SDL window +* @param Matrix the matrix to display +* @return int error code +*/ +int NewWindowFromMatrix(Matrix m); + #endif diff --git a/matrixmain.c b/matrixmain.c index bfc169a..8eb5a87 100644 --- a/matrixmain.c +++ b/matrixmain.c @@ -17,42 +17,12 @@ int main(){ int rules[] = {2, 32, 8, 128}; Matrix m2; BooleanMatrix bmatrix = CreateBooleanMatrix(300, 300); - int keypress = 0; - - SCREEN screen; - screen.WIDTH = 300; - screen.HEIGHT = 300; - - - if (SDL_Init(SDL_INIT_VIDEO) < 0){ - return 1; - } - - SDL_CreateWindowAndRenderer(screen.WIDTH, screen.HEIGHT, - SDL_WINDOW_RESIZABLE | SDL_WINDOW_MAXIMIZED | SDL_WINDOW_SHOWN, - &(screen.window), &(screen.renderer)); - if (screen.window == NULL || screen.renderer == NULL){ - SDL_Quit(); - return 1; - } bmatrix = RandomizeBooleanMatrix(bmatrix); m1 = newMatrix(bmatrix); FreeBooleanMatrix(bmatrix); - while (!keypress){ - DisplayMatrixSDL(&screen, m1); - while(SDL_PollEvent(&screen.event)){ - switch (screen.event.type){ - case SDL_QUIT: - keypress = 1; - break; - case SDL_KEYDOWN: - keypress = 1; - break; - } - } - } + NewWindowFromMatrix(m1); m2 = matrixFromRules(m1, 4, rules); freeMatrix(m1);