void clearsheet(void) /* Clears the current spreadsheet */ { int col, row; for (row = 0; row <= lastrow; row++) { for (col = 0; col <= lastcol; col++) deletecell(col, row, NOUPDATE); } initvars(); setrightcol(); setbottomrow(); displayscreen(NOUPDATE); printfreemem(); changed = FALSE; } /* clearsheet */
void insertcol(int col) /* Inserts a column */ { int counter, row; if (lastcol == MAXCOLS - 1) { for (counter = 0; counter <= lastrow; counter++) deletecell(lastcol, counter, NOUPDATE); printfreemem(); } if (col != MAXCOLS - 1) { movmem(&cell[col][0], &cell[col + 1][0], MAXROWS * sizeof(CELLPTR) * (MAXCOLS - col - 1)); movmem(&format[col][0], &format[col + 1][0], MAXROWS * (MAXCOLS - col - 1)); movmem(&colwidth[col], &colwidth[col + 1], MAXCOLS - col - 1); } setmem(&cell[col][0], MAXROWS * sizeof(CELLPTR), 0); setmem(&format[col][0], MAXROWS, DEFAULTFORMAT); colwidth[col] = DEFAULTWIDTH; lastcol = MAXCOLS - 1; setlastcol(); setrightcol(); if (curcol > rightcol) { rightcol++; setleftcol(); } for (counter = 0; counter <= lastcol; counter++) { for (row = 0; row <= lastrow; row++) { if ((cell[counter][row] != NULL) && (cell[counter][row]->attrib == FORMULA)) fixformula(counter, row, COLADD, col); updateoflags(col, row, NOUPDATE); } } while (col <= rightcol) displaycol(col++, NOUPDATE); changed = TRUE; recalc(); } /* insertcol */
void mainmenu(void) /* Executes the commands in the main menu */ { switch(getcommand(MENU, COMMAND)) { case 0 : smenu(); break; case 1 : formatcells(); break; case 2 : deletecell(curcol, currow, UPDATE); printfreemem(); if (autocalc) recalc(); break; case 3 : gotocell(); break; case 4 : cmenu(); break; case 5 : rmenu(); break; case 6 : editcell(curcell); break; case 7 : umenu(); break; case 8 : changeautocalc(!autocalc); break; case 9 : checkforsave(); stop = TRUE; break; } /* switch */ } /* mainmenu */
void insertrow(int row) /* Inserts a row */ { int counter, rowc; if (lastrow == MAXROWS - 1) { for (counter = 0; counter <= lastcol; counter++) deletecell(counter, lastrow, NOUPDATE); printfreemem(); } if (row != MAXROWS - 1) { for (counter = 0; counter < MAXCOLS; counter++) { movmem(&cell[counter][row], &cell[counter][row + 1], sizeof(CELLPTR) * (MAXROWS - row - 1)); movmem(&format[counter][row], &format[counter][row + 1], MAXROWS - row - 1); } } for (counter = 0; counter < MAXCOLS; counter++) { cell[counter][row] = NULL; format[counter][row] = DEFAULTFORMAT; } lastrow = MAXROWS - 1; setlastrow(); for (counter = 0; counter <= lastcol; counter++) { for (rowc = 0; rowc <= lastrow; rowc++) { if ((cell[counter][rowc] != NULL) && (cell[counter][rowc]->attrib == FORMULA)) fixformula(counter, rowc, ROWADD, row); } } while (row <= bottomrow) displayrow(row++, NOUPDATE); changed = TRUE; recalc(); } /* insertrow */
void deleterow(int row) /* Deletes a row */ { int counter, rowc; for (counter = 0; counter <= lastcol; counter++) deletecell(counter, row, NOUPDATE); printfreemem(); if (row != MAXROWS - 1) { for (counter = 0; counter < MAXCOLS; counter++) { movmem(&cell[counter][row + 1], &cell[counter][row], sizeof(CELLPTR) * (MAXROWS - row - 1)); movmem(&format[counter][row + 1], &format[counter][row], MAXROWS - row - 1); } } else { for (counter = 0; counter <= lastcol; counter++) { cell[counter][MAXROWS - 1] = NULL; format[counter][MAXROWS - 1] = DEFAULTFORMAT; } } if ((lastrow >= row) && (lastrow > 0)) lastrow--; for (counter = 0; counter <= lastcol; counter++) { for (rowc = 0; rowc <= lastrow; rowc++) { if ((cell[counter][rowc] != NULL) && (cell[counter][rowc]->attrib == FORMULA)) fixformula(counter, rowc, ROWDEL, row); } } while (row <= bottomrow) displayrow(row++, NOUPDATE); changed = TRUE; recalc(); } /* deleterow */
void act(char *s) /* Acts on a particular input */ { int attrib, allocated; double value; deletecell(curcol, currow, UPDATE); value = parse(s, &attrib); switch(attrib) { case TEXT : allocated = alloctext(curcol, currow, s); if (allocated) displaycell(curcol, currow, NOHIGHLIGHT, NOUPDATE); break; case VALUE : allocated = allocvalue(curcol, currow, value); break; case FORMULA : allocated = allocformula(curcol, currow, s, value); break; } /* switch */ if (allocated) { format[curcol][currow] &= ~OVERWRITE; clearoflags(curcol + 1, currow, UPDATE); if (attrib == TEXT) setoflags(curcol, currow, UPDATE); if (curcol > lastcol) lastcol = curcol; if (currow > lastrow) lastrow = currow; if (autocalc) recalc(); } else errormsg(MSGLOMEM); printfreemem(); } /* act */
int main( int argc, char* args[] ) { //get a random seed. srand(time(NULL)); //mouse variables and cell types int x, y, sleepTime = 0, countVar = 0; //mouse is held variables int mouseStatusLeft = 0, mouseStatusRight = 0; //make sure the program waits for a quit int quit = false; //Initialize if( init() == false ) return 1; //Load the files if( load_files() == false ) return 2; //Update the screen //if( SDL_Flip( screen ) == -1 ) return 3; //if(SDL_RenderPresent( screen ) == -1 ) return 3; //initialize the cell stuff. This gets the cell system up and running. This also sets all cells to m_air and all the saturation to m_no_saturaion init_cell_stuff(); /* CELL_SIZE = 4; int i;// //putting test materials into grid for(i=0; i<GRID_WIDTH; i++){ if(get_rand(1,4)==1) grid[i+camera_x][GRID_HEIGHT-1-get_rand(7,15)+camera_y].mat = m_plant_root; if(get_rand(1,10)==10) grid[i+camera_x][camera_y+get_rand(20,35)].mat = m_spring; grid[i+camera_x][camera_y+get_rand(30,34)+30].mat = m_earth; grid[i+camera_x][camera_y+get_rand(30,34)+30].mat = m_earth; grid[i+camera_x][camera_y+get_rand(30,34)+30].mat = m_earth; } */ CELL_SIZE = 8; sleepTime = 0; //int i;// //putting test materials into grid /* for(i=7 ; i<GRID_WIDTH ; i+=15){ for(j=26 ; j<GRID_HEIGHT ; j+=20){ grid[i][j].mat = m_anti_scurge; } } */ //-------------------------------------- //for(i=0 ; i<GRID_WIDTH*GRID_HEIGHT / 2 ; i++) // grid[get_rand(0,GRID_WIDTH-1)][get_rand(0,GRID_HEIGHT-1)].mat = m_plant; //int keyw=0, keya=0, keys=0, keyd=0; //unsigned lastPanTime = 0; bool alt = 0; // this keeps track of the state of the alt key //these variables store the position of the user's cursor at the moment that the user decided to pan the screen int mouse_x_when_pan=0; int mouse_y_when_pan=0; // these are for keeping track of the motion of the mouse when the user is panning. these keep the panning from being really jumpy. // without them, there is a truncation error when adjusting the camera_x and camera_y materials and re-setting the mouse coordinates to its original state. // these keep track of the truncation error and add it to the next operation so that the mouse motion feels fluid. int remainder_panning_motion_x=0; int remainder_panning_motion_y=0; //last minute verification that the initial start up values for the grid size and the camera positions are valid. verify_grid_and_cell_size(); verify_camera(); //While the user hasn't quit while(1){ //While there's an event to handle while( SDL_PollEvent( &event ) ){ //If the user has Xed out the window if( event.type == SDL_QUIT || quit == true ){ //Quit the program clean_up(); return 0; } if( event.type == SDL_MOUSEBUTTONDOWN ){ /// mouse down x = event.motion.x; y = event.motion.y; if( event.button.button == SDL_BUTTON_LEFT ){ mouseStatusLeft = 1; } else if( event.button.button == SDL_BUTTON_RIGHT ){ mouseStatusRight = 1; } } else if(event.type == SDL_MOUSEWHEEL){ if(event.wheel.y > 0) zoom_in(x,y); if(event.wheel.y < 0) zoom_out(x,y); } else if(event.type == SDL_MOUSEBUTTONUP){ /// mouse up x = event.motion.x; y = event.motion.y; if( event.button.button == SDL_BUTTON_LEFT ){ mouseStatusLeft = 0; } else if( event.button.button == SDL_BUTTON_RIGHT ){ mouseStatusRight = 0; } } else if( event.type == SDL_MOUSEMOTION ){ /// mouse motion x = event.motion.x; y = event.motion.y; // if the alt key (camera panning key) is down and the coordinates have changed, then let the screen be panned! if(alt && x != mouse_x_when_pan && y != mouse_y_when_pan){ // this adjusts the x-axis camera (this scales with the CELL_SIZE) camera_x += (x-mouse_x_when_pan+remainder_panning_motion_x)/CELL_SIZE; camera_y += (y-mouse_y_when_pan+remainder_panning_motion_y)/CELL_SIZE; //calculate the remainders of the mouse motion. // these values represent the motion of the mouse that is not utilized by the discrete nature of the operation on the camera_x and camera_y values. remainder_panning_motion_x = (x-mouse_x_when_pan+remainder_panning_motion_x) - (int)((x-mouse_x_when_pan+remainder_panning_motion_x)/CELL_SIZE)*CELL_SIZE; remainder_panning_motion_y = (y-mouse_y_when_pan+remainder_panning_motion_y) - (int)((y-mouse_y_when_pan+remainder_panning_motion_y)/CELL_SIZE)*CELL_SIZE; // make sure the camera is not out of bounds. verify_camera(); //reset the user's curcor position to the original position the curcor was in when the user started panning the camera SDL_WarpMouseInWindow(window, mouse_x_when_pan, mouse_y_when_pan); } } else if(event.type == SDL_WINDOWEVENT_RESIZED ){ /// window resize //float new_cell_size = CELL_SIZE * event.resize.h/((float)SCREEN_HEIGHT); // adjust the pixel size. float new_cell_size = CELL_SIZE * event.window.data1/((float)SCREEN_HEIGHT); // adjust the pixel size. if(new_cell_size - ((int)new_cell_size) >= 0.5f) CELL_SIZE = new_cell_size + 1; else CELL_SIZE = new_cell_size; //SCREEN_WIDTH = event.resize.w; //SCREEN_HEIGHT = event.resize.h; SCREEN_WIDTH = event.window.data1; SCREEN_HEIGHT = event.window.data2; verify_grid_and_cell_size(); // make sure the window isn't too big for the cell size //set_window_size(event.resize.w, event.resize.h); set_window_size(event.window.data1, event.window.data2); verify_camera(); } if( event.type == SDL_KEYDOWN ){ ///keyboard event switch( event.key.keysym.sym ){ case SDLK_UP: break; //change block type up case SDLK_DOWN: break; // change block type down case SDLK_c: reset_cells(); break;//clear the screen case SDLK_p: print_saturation_data(); break; // prints the cellSat[][] array to stdout. this is for debuggin purposes. case SDLK_r: randomize_grid(); break; // randomize grid case SDLK_LEFT: if(paused != 1) {sleepTime /= 2;} break; //speeds up the game case SDLK_RIGHT: if(paused != 1) {if(sleepTime == 0){sleepTime = 1;} {sleepTime *= 2;} if(sleepTime > 2000) {sleepTime = 2000;}} break; //slows down the game case SDLK_SPACE: if(paused == 0) {paused = 1;} else if(paused == 1) {paused = 0;} break; //pause the game case SDLK_ESCAPE: quit = true; // quit with escape case SDLK_w: gen_world(w_normal,0); break; // generate a world /* case SDLK_w: keyw=1; break; // store key state case SDLK_a: keya=1; break; case SDLK_s: keys=1; break; case SDLK_d: keyd=1; break; */ case SDLK_LALT: case SDLK_RALT: // store the state of the alt key. alt = 1; // turn off the cursor SDL_ShowCursor(SDL_DISABLE); // store the position of the mouse. this will allow the program to make the cursor stay stationary mouse_x_when_pan = x; mouse_y_when_pan = y; //reset the remainder mouse motion variables for x and y. remainder_panning_motion_x = 0; remainder_panning_motion_y = 0; break; default: break; } } if( event.type == SDL_KEYUP ){ ///keyboard event switch( event.key.keysym.sym ){ /* case SDLK_w: keyw=0; break;//lastPanTime=0; break; // store key state case SDLK_a: keya=0; break;//lastPanTime=0; break; case SDLK_s: keys=0; break;//lastPanTime=0; break; case SDLK_d: keyd=0; break;//lastPanTime=0; break; */ case SDLK_LALT: case SDLK_RALT: // show the cursor again. SDL_ShowCursor(SDL_ENABLE); alt = 0; break; default: break; } } } // end while(event) //no more events to handle at the moment. /* //this handles time-controlled panning if(SDL_GetTicks() - MIN_PAN_INTERVAL > lastPanTime){ if(keyw) pan(D_UP); if(keya) pan(D_LEFT); if(keys) pan(D_DOWN); if(keyd) pan(D_RIGHT); lastPanTime = SDL_GetTicks(); //#if (DEBUG_GRIDSIM) // printf("\nlastPanTime = %d\n", lastPanTime); //#endif } */ //checks if the mouse is held or not if(mouseStatusLeft == 1 && mouseModifier == 0){ //make sure the mouse isn't inside either of the two GUIs. if(y >= 50 && x < SCREEN_WIDTH - 200) setcell(x, y, currentMat); } else if(mouseStatusRight == 1 && mouseModifier == 0){ deletecell(x, y, currentMat); } //speed of gameplay countVar++; if(sleepTime <= 0) { sleepTime = 0; } //evealuate cells if(countVar >= sleepTime && paused != 1){ evaluate_grid(); countVar = 0; } //updates screen with cells print_cells(); //displays selection gui selectionGUI(x, y, mouseStatusLeft); //displays brushes and misc gui brushesGUI(x, y, mouseStatusLeft); // If the user is not panning, then it is fine to show the cursor. if(!alt){ //displays cursor cursorDisplay(x, y); } //draw_line(screen, 300, 300, x, y, 1, mats[currentMat].color); // test the line drawing function //updates the screen //SDL_Flip( screen ); //SDL_RenderPresent( screen ); SDL_UpdateWindowSurface(window); }// end while(quit == false) //Free the surface and quit SDL clean_up(); return 0; }
void fixformula(int col, int row, int action, int place) /* Modifies a formula when its column or row designations need to change. */ { char *colstart, *rowstart, s[6], newformula[MAXINPUT + 1], *curpos = newformula; int fcol, frow; CELLPTR cellptr = cell[col][row]; double value; strcpy(newformula, cellptr->v.f.formula); while (*curpos != 0) { if (formulastart(&curpos, &fcol, &frow)) { rowstart = curpos - rowwidth(frow); colstart = rowstart - ((fcol > 25) ? 2 : 1); switch (action) { case COLADD : if (fcol < place) break; if (fcol == 25) { if (strlen(newformula) == MAXINPUT) { deletecell(col, row, NOUPDATE); alloctext(col, row, newformula); return; } movmem(colstart, colstart + 1, strlen(colstart) + 1); } colstring(fcol + 1, s); movmem(s, colstart, strlen(s)); break; case ROWADD : if (frow < place) break; if (rowwidth(frow + 1) != rowwidth(frow)) { if (strlen(newformula) == MAXINPUT) { deletecell(col, row, NOUPDATE); alloctext(col, row, newformula); return; } movmem(rowstart, rowstart + 1, strlen(rowstart) + 1); } sprintf(s, "%d", frow + 2); movmem(s, rowstart, strlen(s)); break; case COLDEL : if (fcol <= place) break; if (fcol == 26) movmem(colstart + 1, colstart, strlen(colstart) + 1); colstring(fcol - 1, s); movmem(s, colstart, strlen(s)); break; case ROWDEL : if (frow <= place) break; if (rowwidth(frow) != rowwidth(frow - 1)) movmem(rowstart + 1, rowstart, strlen(rowstart) + 1); sprintf(s, "%d", frow); movmem(s, rowstart, strlen(s)); break; } /* switch */ } else curpos++; } if (strlen(newformula) != strlen(cellptr->v.f.formula)) { value = cellptr->v.f.fvalue; deletecell(col, row, NOUPDATE); allocformula(col, row, newformula, value); } else strcpy(cellptr->v.f.formula, newformula); } /* fixformula */