From bededa78c291135aff2115a5c76003eb81959007 Mon Sep 17 00:00:00 2001 From: klmp200 Date: Wed, 28 Dec 2016 21:39:42 +0100 Subject: [PATCH] Swag matrice en SDL --- LibGui/Makefile | 2 +- LibGui/pixel.c | 31 ++++++++++++---- LibGui/pixel.h | 29 ++++++++++++--- LibMatrix/CellElement.c | 10 ------ LibMatrix/matrix.c | 7 ++-- Makefile | 2 +- matrixmain.c | 79 +++++++++++++++++++++-------------------- 7 files changed, 94 insertions(+), 66 deletions(-) diff --git a/LibGui/Makefile b/LibGui/Makefile index 2c0f011..8aa4751 100644 --- a/LibGui/Makefile +++ b/LibGui/Makefile @@ -1,5 +1,5 @@ CC=gcc -CFLAGS=-Wall -Werror -pedantic -fpic -g -std=c89 +CFLAGS=-Wall -Werror -pedantic -fpic -g -std=c89 -Wextra LIBSDIR=-L/usr/lib -L../Libs diff --git a/LibGui/pixel.c b/LibGui/pixel.c index 9ae1003..09d8a19 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 18:48:54 +* @Last Modified time: 2016-12-28 21:36:39 */ #include @@ -10,10 +10,29 @@ #include #include #include +#include -void SDL_test(){ - PIXEL test; - Matrix gg = CreateMatrix(); - SetCellValue(gg, 1, 1, true); - test.color = 1; +void SetPixel(SDL_Renderer *renderer, int x, int y, bool value){ + + if (value){ + SDL_SetRenderDrawColor(renderer, 255, 255, 255, 255); + } else { + SDL_SetRenderDrawColor(renderer, 0, 0, 0, 255); + } + SDL_RenderDrawPoint(renderer, x, y); +} + +void DisplayMatrixSDL(SCREEN *screen, Matrix m){ + int i; + int j; + + SDL_SetRenderDrawColor(screen->renderer, 0, 0, 0, 0); + SDL_RenderClear(screen->renderer); + + for (i=0;irenderer, i, j, GetCellValue(m, j, i)); + } + } + SDL_RenderPresent(screen->renderer); } diff --git a/LibGui/pixel.h b/LibGui/pixel.h index 84696b0..feccc4a 100644 --- a/LibGui/pixel.h +++ b/LibGui/pixel.h @@ -2,12 +2,33 @@ #define PIXEL_H_INCLUDED #include +#include +#include typedef struct { - SDL_Rect position; - Uint8 color; -} PIXEL; + SDL_Window * window; + SDL_Renderer * renderer; + int WIDTH; + int HEIGHT; + SDL_Event event; +} SCREEN; -void SDL_test(); +/** +* Set a pixel on a given screen +* @param screen the screen to display the pixel +* @param x x position +* @param y y position +* @param value display white if true and black if false +* @return +*/ +void SetPixel(SDL_Renderer *renderer, int x, int y, bool value); + +/** +* Display an entire matrix on a given screen +* @param screen the screen where the matrix should be displayed +* @param m a Matrix +* @return +*/ +void DisplayMatrixSDL(SCREEN *screen, Matrix m); #endif diff --git a/LibMatrix/CellElement.c b/LibMatrix/CellElement.c index d9cb03b..ba6cc7e 100644 --- a/LibMatrix/CellElement.c +++ b/LibMatrix/CellElement.c @@ -14,8 +14,6 @@ cellElement * CreateCellElem(){ return NULL; } - printf("---Created cellElement---\n"); - elem->nextCol = NULL; elem->nextRow = NULL; @@ -62,8 +60,6 @@ int AddNextCol(cellElement* tree){ cellElement * elem = NULL; - printf("---insertNextCol---\n"); - elem = CreateCellElem(); if (elem == NULL){ @@ -78,8 +74,6 @@ int AddNextRow(cellElement* tree){ cellElement * elem = NULL; - printf("---insertNextRow---\n"); - elem = CreateCellElem(); if (elem == NULL){ @@ -90,7 +84,6 @@ int AddNextRow(cellElement* tree){ } void removeNextCol(cellElement* tree){ - printf("---removeNextCol---\n"); if (tree->nextCol != NULL){ free(tree->nextCol); @@ -100,7 +93,6 @@ void removeNextCol(cellElement* tree){ } void removeNextRow(cellElement* tree){ - printf("---removeNextRow---\n"); if (tree->nextRow != NULL){ free(tree->nextRow); @@ -127,8 +119,6 @@ void recursivePrint(cellElement * tree){ void FreeCellElement(cellElement* element) { - printf("---FreeCellElement---\n"); - if (element != NULL){ free(element); }else{ diff --git a/LibMatrix/matrix.c b/LibMatrix/matrix.c index 4e001ae..29840f8 100644 --- a/LibMatrix/matrix.c +++ b/LibMatrix/matrix.c @@ -299,8 +299,6 @@ Matrix freeMatrix(Matrix matrix){ int i = 0; int j = 0; - printf("\n---FREE MATRIX---\n"); - for (i=0;i #include #include #include +#include -int main(int argc, char **argv){ - int Rule = 256; - int N = 1; - int rules[] = {1, 2}; - Matrix matrix = CreateMatrix(); +int main(){ + Matrix m1 = CreateMatrix(); + int rules[] = {2, 32, 8, 128}; Matrix m2; - Matrix m3; - Matrix m4; - BooleanMatrix bmatrix = CreateBooleanMatrix(5, 5); + 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); - m3 = newMatrix(bmatrix); + m1 = newMatrix(bmatrix); FreeBooleanMatrix(bmatrix); - BasicPrintMatrix(m3); - m4 = matrixFromRules(m3, 2, rules); - BasicPrintMatrix(m4); - freeMatrix(m4); - freeMatrix(m3); - applyRules(matrix,Rule,N); - matrix = SetMatrixDim(matrix,3,3); - - BasicPrintMatrix(matrix); - - SetCellValue(matrix,0,0,true); - SetCellValue(matrix,0,1,true); - SetCellValue(matrix,0,2,true); - BasicPrintMatrix(matrix); - - SetCellValue(matrix,0,0,false); - SetCellValue(matrix,0,1,false); - BasicPrintMatrix(matrix); - - m2 = colSequenceOnMatrix(matrix, OR); - BasicPrintMatrix(m2); - freeMatrix(m2); - m2 = rowSequenceOnMatrix(matrix, AND); - BasicPrintMatrix(m2); - - /*sumMatrix(matrix,m2);*/ + 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; + } + } + } + m2 = matrixFromRules(m1, 4, rules); + freeMatrix(m1); freeMatrix(m2); - freeMatrix(matrix); return 0; } -/* todo -*modifier DeleteListContent avec sli -*/