1
0
mirror of https://gitlab.com/klmp200/LO27.git synced 2024-12-22 17:31:08 +00:00

Merge branch 'sli' into 'master'

Encapsulation de la SDL

See merge request !18
This commit is contained in:
Antoine Bartuccio 2016-12-28 23:00:17 +00:00
commit 3df77c90ae
3 changed files with 65 additions and 32 deletions

View File

@ -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 <stdio.h>
@ -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;
}

View File

@ -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

View File

@ -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);