]> git.treefish.org Git - seamulator.git/blob - seamulator.cpp
Moved view code to separate class
[seamulator.git] / seamulator.cpp
1 #include <ctime>
2 #include <cmath>
3 #include <memory>
4
5 #include <GL/glut.h>
6
7 #include "sea.h"
8 #include "seaview.h"
9 #include "watersurface.h"
10
11 const int LATTICE_SIZE = 10;
12 const double LATTICE_UNIT = 1;
13
14 SeaPtr sea;
15 WaterSurfacePtr surface;
16 std::unique_ptr<SeaView> seaView;
17
18 void glDisplayFunc();
19 void glReshapeFunc(int width, int height);
20 void glMouseFunc(int button, int state, int x, int y);
21 void glMotionFunc(int x, int y);
22
23 int main(int argc, char** argv)
24 {
25   std::srand(std::time(0));
26
27   surface = std::make_shared<WaterSurface>(LATTICE_SIZE, LATTICE_UNIT);
28   sea = std::make_shared<Sea>(surface);
29   seaView = std::make_unique<SeaView>(LATTICE_SIZE * LATTICE_UNIT * 1.5,
30                                       0, M_PI/4);
31
32   glutInit(&argc, argv);
33   glutInitDisplayMode(GLUT_DOUBLE);
34   glutInitWindowSize(300, 300);
35   glutInitWindowPosition(100, 100);
36   glutCreateWindow("seamulator");
37   glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
38
39   glutDisplayFunc(glDisplayFunc);
40   glutReshapeFunc(glReshapeFunc);
41   glutMouseFunc(glMouseFunc);
42   glutMotionFunc(glMotionFunc);
43
44   glutMainLoop();
45
46   return 0;
47 }
48
49 void glDisplayFunc()
50 {
51   glClear(GL_COLOR_BUFFER_BIT);
52
53   seaView->setupView();
54   sea->update();
55   surface->draw();
56
57   glutSwapBuffers();
58   glutPostRedisplay();
59 }
60
61 void glReshapeFunc(int width, int height)
62 {
63   glMatrixMode(GL_PROJECTION);
64   glLoadIdentity();
65   gluPerspective(50.0, ((float)width/(float)height), 0, 1000.0);
66   glViewport(0, 0, width, height);
67 }
68
69 void glMouseFunc(int button, int state, int x, int y)
70 {
71   seaView->onMouseEvent(button, state, x, y);
72 }
73
74 void glMotionFunc(int x, int y)
75 {
76   seaView->onMouseMove(x, y);
77 }