Rip out main glut and replace with simple loop, more to come

This commit is contained in:
Joshua Coles 2023-02-19 16:56:19 +00:00
parent 496c1022db
commit ef89b285d1
3 changed files with 3 additions and 198 deletions

View File

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

View File

@ -1,34 +0,0 @@
#pragma once
#include <GLUT/glut.h>
#include <iostream>
#include <string>
#include <sstream>
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[]);
};

View File

@ -1,4 +1,3 @@
#include <GLUT/glut.h>
#include <iostream> #include <iostream>
#include <stdio.h> #include <stdio.h>
#include <vector> #include <vector>
@ -6,7 +5,6 @@
#include <string> #include <string>
#include "DLASystem.h" #include "DLASystem.h"
#include "Window.h"
using namespace std; using namespace std;
@ -25,148 +23,16 @@ namespace drawFuncs {
DLASystem *sys; DLASystem *sys;
int main(int argc, char **argv) { 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 // create the system
sys = new DLASystem(win); sys = new DLASystem();
// this is the seed for the random numbers // this is the seed for the random numbers
int seed = 6; int seed = 6;
cout << "setting seed " << seed << endl; cout << "setting seed " << seed << endl;
sys->setSeed(seed); sys->setSeed(seed);
// print the "help" message to the console while (sys->Update()) {
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();
return 0; 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();
}