1
0
mirror of https://gitlab.com/klmp200/LO27.git synced 2024-11-26 15:04:14 +00:00
LO27/LibGui/gui.c

154 lines
3.2 KiB
C
Raw Permalink Normal View History

/*
2016-12-29 15:09:39 +00:00
* This is a gui for the cellular automaton library
*
* Copyright (C) 2016-2017 Antoine BARTUCCIO, Jean POREE DE RIDDER
*
* Licensed under the MIT License,(the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* hhttps://opensource.org/licenses/MIT
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*
*/
#include <stdio.h>
#include <stdlib.h>
#include <SDL2/SDL.h>
#include <gui.h>
#include <matrix.h>
2016-12-28 22:26:58 +00:00
#include <CellElement.h>
#include <termios.h>
#include <unistd.h>
2017-01-01 15:43:40 +00:00
void setPixel(SDL_Renderer *renderer, int x, int y, bool value){
2016-12-28 20:39:42 +00:00
if (value){
SDL_SetRenderDrawColor(renderer, 255, 255, 255, 255);
} else {
SDL_SetRenderDrawColor(renderer, 0, 0, 0, 255);
}
SDL_RenderDrawPoint(renderer, x, y);
}
2017-01-01 15:43:40 +00:00
void displayMatrixSDL(SCREEN *screen, Matrix m){
2016-12-28 20:39:42 +00:00
int i;
int j;
SDL_SetRenderDrawColor(screen->renderer, 0, 0, 0, 0);
SDL_RenderClear(screen->renderer);
for (i=0;i<m.rowCount;i++){
for (j=0;j<m.colCount;j++){
2017-01-01 15:43:40 +00:00
setPixel(screen->renderer, i, j, getCellValue(m, j, i));
2016-12-28 20:39:42 +00:00
}
}
SDL_RenderPresent(screen->renderer);
}
2016-12-28 22:59:43 +00:00
2017-01-01 15:43:40 +00:00
void waitUntilEnter(SCREEN * screen){
2016-12-28 22:59:43 +00:00
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;
}
}
}
}
}
2017-01-01 15:43:40 +00:00
int newWindowFromMatrix(Matrix m){
2016-12-28 22:59:43 +00:00
SCREEN screen;
screen.WIDTH = m.colCount;
screen.HEIGHT = m.rowCount;
if (SDL_Init(SDL_INIT_VIDEO) < 0){
2016-12-29 00:37:58 +00:00
SDL_Quit();
2016-12-28 22:59:43 +00:00
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;
}
2017-01-01 15:43:40 +00:00
displayMatrixSDL(&screen, m);
2016-12-28 22:59:43 +00:00
2017-01-01 15:43:40 +00:00
waitUntilEnter(&screen);
2016-12-28 22:59:43 +00:00
SDL_DestroyRenderer(screen.renderer);
SDL_DestroyWindow(screen.window);
2016-12-29 00:37:58 +00:00
SDL_Quit();
2016-12-28 22:59:43 +00:00
return 0;
}
2017-01-01 15:43:40 +00:00
bool inputYesOrNo(char message[]){
char response;
printf("%s (Y/n)\n", message);
response = getchar();
2017-01-01 15:43:40 +00:00
clearBuffer();
if (response == 'n'){
return false;
} else {
return true;
}
}
2017-01-01 15:43:40 +00:00
void clearBuffer(){
char c;
while ((c = getchar()) != '\n' && c != EOF) { }
}
2017-01-01 15:43:40 +00:00
void displayMatrixGUI(Matrix m, bool useSDL){
if (useSDL){
printf("Press ENTER or the red cross to exit the window and continue.\n");
2017-01-01 15:43:40 +00:00
newWindowFromMatrix(m);
} else {
printMatrix(m);
}
}
2017-01-01 15:43:40 +00:00
int safeNumberInput(int min, int max){
char str[30];
int input;
fgets(str, 29, stdin);
2017-01-01 15:43:40 +00:00
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);
2017-01-01 15:43:40 +00:00
clearBuffer();
input = atoi(str);
}
return input;
}