/** * Draw the actual board. * Draw the alive cell in black * Draw the dead cell in white * Draw the unfilled area in grey * * @param iniBoard Board * @param sw Window * @param rectangle Rectangle * @param x Number of rows * @param y Number of columns */ void drawBoard(char iniBoard[MAX_SIZE][MAX_SIZE], SDL_Simplewin sw, SDL_Rect rectangle, int x, int y){ int block, i ,j; for(j = 0; j < MAX_SIZE; j++){ for(i = 0; i < MAX_SIZE; i++){ if(iniBoard[j][i] == DEAD){ Neill_SDL_SetDrawColour(&sw, WHITE, WHITE, WHITE); } if(iniBoard[j][i] == ALIVE){ Neill_SDL_SetDrawColour(&sw, BLACK, BLACK, BLACK); } if(iniBoard[j][i] == UNFILLED){ Neill_SDL_SetDrawColour(&sw, GREY, GREY, GREY); } block = WHEIGHT / y; rectangle.w = block; rectangle.h = block; /* Filled Rectangle */ rectangle.x = block * i; rectangle.y = block * j; SDL_RenderFillRect(sw.renderer, &rectangle); /* Unfilled Rectangle */ Neill_SDL_SetDrawColour(&sw, GREY, GREY, GREY); rectangle.x = block * i; rectangle.y = block * j; SDL_RenderDrawRect(sw.renderer, &rectangle); } } }
/* Display the maze in an SDL GUI */ void display_SDL(max_maze array, maze_dimensions dims){ // Fit the maze to the window height by scaling each cell's dimensions int rectsize = WHEIGHT / dims.height; int window_offset = (WWIDTH-WHEIGHT)/2; int cx=0, cy =0; SDL_Simplewin win; SDL_Rect rectangle; rectangle.w = rectsize; rectangle.h = rectsize; Neill_SDL_Init(&win); do { // Draw the array graphically. for (int i = 0; i < dims.height; ++i) { for (int j = 0; j < dims.width; ++j) { if ( array[i][j] == '#' ) { Neill_SDL_SetDrawColour(&win,255,255,255); // Set colour to WHITE // Draw rectangle for each array cell, centred in the window rectangle.x = (j * rectsize) + window_offset ; rectangle.y = i * rectsize; SDL_RenderFillRect(win.renderer, &rectangle); } if ( array[i][j] == '.') { Neill_SDL_SetDrawColour(&win,255,128,0); // Set colour to ORANGE cx = (j*rectsize) + window_offset + (rectsize/2); cy = (i*rectsize) + (rectsize/2); Neill_SDL_RenderFillCircle(win.renderer, cx, cy, RADIUS); } } } // Update window. SDL_RenderPresent(win.renderer); SDL_UpdateWindowSurface(win.win); Neill_SDL_Events(&win); // Check for user kill SDL_Delay(MILLISECONDDELAY); // Wait a short time } while(!win.finished); // Cleans up. atexit(SDL_Quit); }
int main(void) { SDL_Simplewin sw; SDL_Rect rectangle; rectangle.w = RECTSIZE; rectangle.h = RECTSIZE; Neill_SDL_Init(&sw); do{ // Sleep for a short time SDL_Delay(MILLISECONDDELAY); // Choose a random colour, a mixture of red, green and blue. Neill_SDL_SetDrawColour(&sw, rand()%SDL_8BITCOLOUR, rand()%SDL_8BITCOLOUR, rand()%SDL_8BITCOLOUR); // Filled Rectangle, fixed size, random position rectangle.x = rand()%(WWIDTH-RECTSIZE); rectangle.y = rand()%(WHEIGHT-RECTSIZE); SDL_RenderFillRect(sw.renderer, &rectangle); // Unfilled Rectangle, fixed size, random position rectangle.x = rand()%(WWIDTH-RECTSIZE); rectangle.y = rand()%(WHEIGHT-RECTSIZE); SDL_RenderDrawRect(sw.renderer, &rectangle); // Update window - no graphics appear on some devices until this is finished SDL_RenderPresent(sw.renderer); SDL_UpdateWindowSurface(sw.win); // Has anyone pressed ESC or killed the SDL window ? // Must be called frequently - it's the only way of escaping Neill_SDL_Events(&sw); }while(!sw.finished); // Clear up graphics subsystems atexit(SDL_Quit); return 0; }
void print_board_SDL(board_struct *current_board, SDL_Simplewin *sw, on_off intermediate_indicator, int row_coordinate, int col_coordinate, success_failure success_index) { int i, j, temp_x, temp_y; SDL_Rect rectangle; colour_struct colour_range; rectangle.w = RECT_SIZE; rectangle.h = RECT_SIZE; sw->skip_checker = off; for(i = 0; i < BOARD_H; ++i){ for(j = 0; j < BOARD_W; ++j){ if(success_index == symmetric_successful){ temp_x = BOARD_W - BORDER - j; rectangle.x = temp_x * SCALE_FACT + (WWIDTH - SCALE_FACT * BOARD_W) / 2; colour_chooser(current_board, i, j, &colour_range, intermediate_indicator, row_coordinate, BOARD_W - BORDER - col_coordinate); } else{ rectangle.x = j * SCALE_FACT + (WWIDTH - SCALE_FACT * BOARD_W) / 2; colour_chooser(current_board, i, j, &colour_range, intermediate_indicator, row_coordinate, col_coordinate); } temp_y = BOARD_H - BORDER - i; rectangle.y = temp_y * SCALE_FACT + (WHEIGHT - SCALE_FACT * BOARD_H) / 2; //For centering. Neill_SDL_SetDrawColour(sw, colour_range.colour_r, colour_range.colour_g, colour_range.colour_b); SDL_RenderFillRect(sw->renderer, &rectangle); } } SDL_RenderPresent(sw->renderer); SDL_UpdateWindowSurface(sw->win); do{ Neill_SDL_Events(sw); }while(sw->skip_checker != on && sw->finished != 1); }
// Set up a simple (WIDTH, HEIGHT) window. // Attempt to hide the renderer etc. from user. int Neill_SDL_Init(SDL_Simplewin *sw) { if (SDL_Init(SDL_INIT_VIDEO) != 0) { fprintf(stderr, "\nUnable to initialize SDL: %s\n", SDL_GetError()); SDL_Quit(); exit(1); } sw->finished = 0; sw->win= SDL_CreateWindow("SDL Window", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, WWIDTH, WHEIGHT, SDL_WINDOW_SHOWN); if(sw->win == NULL){ fprintf(stderr, "\nUnable to initialize SDL Window: %s\n", SDL_GetError()); SDL_Quit(); exit(1); } sw->renderer = SDL_CreateRenderer(sw->win, -1, 0); if(sw->renderer == NULL){ fprintf(stderr, "\nUnable to initialize SDL Renderer: %s\n", SDL_GetError()); SDL_Quit(); exit(1); } // Set screen to black Neill_SDL_SetDrawColour(sw, 0, 0, 0); SDL_RenderClear(sw->renderer); SDL_RenderPresent(sw->renderer); return 1; }