From ef89b285d1044203d9d1319b0f6f5e8ad8dcc75c Mon Sep 17 00:00:00 2001 From: Joshua Coles Date: Sun, 19 Feb 2023 16:56:19 +0000 Subject: [PATCH] Rip out main glut and replace with simple loop, more to come --- Window.cpp | 27 ---------- Window.h | 34 ------------- mainDLA.cpp | 140 ++-------------------------------------------------- 3 files changed, 3 insertions(+), 198 deletions(-) delete mode 100644 Window.cpp delete mode 100644 Window.h diff --git a/Window.cpp b/Window.cpp deleted file mode 100644 index f9765b0..0000000 --- a/Window.cpp +++ /dev/null @@ -1,27 +0,0 @@ -#include "Window.h" - -// constructor -Window::Window(int set_size[], string &set_title) { - size[0] = set_size[0]; - size[1] = set_size[1]; - title = set_title; - - locateOnScreen(); - glutInitWindowSize(size[0], size[1]); - glutInitWindowPosition(pos[0], pos[1]); - glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE); - glutCreateWindow(title.c_str()); - - // sets the background to black - glClearColor(0.0, 0.0, 0.0, 0.0); -} - -// print a string at a given position, don't worry about how this works... -void Window::displayString(ostringstream &str, double x, double y, GLfloat col[]) { - string localString = str.str(); - glColor4fv(col); - glRasterPos2d(x, y); - for (int i = 0; i < localString.length(); i++) { - glutBitmapCharacter(GLUT_BITMAP_8_BY_13, localString[i]); - } -} diff --git a/Window.h b/Window.h deleted file mode 100644 index 5734619..0000000 --- a/Window.h +++ /dev/null @@ -1,34 +0,0 @@ -#pragma once - -#include -#include -#include -#include - -using namespace std; - -class Window { -public: - string title; - int size[2]; - int pos[2]; - - void locateOnScreen() { - // the fx sets where on the screen the window will appear - // (values should be between 0 and 1) - double fx[] = {0.7, 0.5}; - pos[0] = (glutGet(GLUT_SCREEN_WIDTH) - size[0]) * fx[0]; - pos[1] = (glutGet(GLUT_SCREEN_HEIGHT) - size[1]) * fx[1]; - } - - // constructor, size is in pixels - Window(int set_size[], string &set_title); - - // function which prints a string to the screen, at a given position, with a given color - // note position is "absolute", not easy to get two strings spaced one above each other like this - void displayString(ostringstream &str, double x, double y, GLfloat col[]); - - -}; - - diff --git a/mainDLA.cpp b/mainDLA.cpp index dbb313f..7c66563 100644 --- a/mainDLA.cpp +++ b/mainDLA.cpp @@ -1,4 +1,3 @@ -#include #include #include #include @@ -6,7 +5,6 @@ #include #include "DLASystem.h" -#include "Window.h" using namespace std; @@ -25,148 +23,16 @@ namespace drawFuncs { DLASystem *sys; int main(int argc, char **argv) { - // turn on glut - glutInit(&argc, argv); - - int window_size[] = {480, 480}; - string window_title("simple DLA simulation"); - - // create a window - Window *win = new Window(window_size, window_title); - // create the system - sys = new DLASystem(win); + sys = new DLASystem(); // this is the seed for the random numbers int seed = 6; cout << "setting seed " << seed << endl; sys->setSeed(seed); - // print the "help" message to the console - drawFuncs::introMessage(); - - // tell openGL how to redraw the screen and respond to the keyboard - glutDisplayFunc(drawFuncs::display); - glutKeyboardFunc(drawFuncs::handleKeypress); - - // tell openGL to do its first update after waiting 10ms - int wait = 10; - int val = 0; - glutTimerFunc(wait, drawFuncs::update, val); - - // start the openGL stuff - glutMainLoop(); + while (sys->Update()) { + } return 0; } - -// this is just a help message -void drawFuncs::introMessage() { - cout << "Keys (while in graphics window):" << endl << " q or e to quit (or exit)" << endl; - cout << " h to print this message (help)" << endl; - cout << " u for a single update" << endl; - cout << " g to start running (go)" << endl; - cout << " p to pause running" << endl; - cout << " s to run in slow-mode" << endl; - cout << " f to run in fast-mode" << endl; - cout << " r to clear everything (reset)" << endl; - cout << " z to pause and zoom in" << endl; - cout << " w or b to change background colour to white or black" << endl; -} - -// openGL function deals with the keyboard -void drawFuncs::handleKeypress(unsigned char key, int x, int y) { - switch (key) { - case 'h': - drawFuncs::introMessage(); - break; - case 'q': - case 'e': - cout << "Exiting..." << endl; - // delete the system - delete sys; - exit(0); - break; - case 'p': - cout << "pause" << endl; - sys->pauseRunning(); - break; - case 'g': - cout << "go" << endl; - sys->setRunning(); - glutTimerFunc(0, drawFuncs::update, 0); - break; - case 's': - cout << "slow" << endl; - sys->setSlow(); - break; - case 'w': - cout << "white" << endl; - sys->setWinBackgroundWhite(); - break; - case 'b': - cout << "black" << endl; - sys->setWinBackgroundBlack(); - break; - case 'f': - cout << "fast" << endl; - sys->setFast(); - break; - case 'r': - cout << "reset" << endl; - sys->Reset(); - break; - case 'z': - cout << "zoom" << endl; - sys->pauseRunning(); - sys->viewAddCircle(); - break; - case 'u': - cout << "upd" << endl; - sys->Update(); - break; - } - // tell openGL to redraw the window - glutPostRedisplay(); -} - -// this function gets called whenever the algorithm should do its update -void drawFuncs::update(int val) { - int wait; // time to wait between updates (milliseconds) - - if (sys->running) { - if (sys->slowNotFast == 1) - wait = 10; - else - wait = 0; - - sys->Update(); - - // tell openGL to call this funtion again after "wait" milliseconds - glutTimerFunc(wait, drawFuncs::update, 0); - } - -} - -// this function redraws the window when necessary -void drawFuncs::display() { - // Clear the window or more specifically the frame buffer... - // This happens by replacing all the contents of the frame - // buffer by the clear color (black in our case) - glClear(GL_COLOR_BUFFER_BIT); - - // this puts the camera at the origin (not sure why) with (I think) z axis out of page and y axis up - // there is also the question of the GL perspective which is not set up in any clear way at the moment - glMatrixMode(GL_MODELVIEW); - glLoadIdentity(); - gluLookAt(0.0, 0.0, 1.0, /* camera position */ - 0.0, 0.0, -1.0, /* point to look at */ - 0.0, 1.0, 0.0); /* up direction */ - - //sys->DrawSpheres(); - sys->DrawSquares(); - - // Swap contents of backward and forward frame buffers - glutSwapBuffers(); -} -