-
Notifications
You must be signed in to change notification settings - Fork 32
/
main.cpp
166 lines (142 loc) · 4.7 KB
/
main.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
/***************************************************************
* Name: titanicMain.cpp
* Purpose: Code for Application Frame
* Author: Luke Wren (wren6991@gmail.com)
* Created: 2013-04-30
* Copyright: Luke Wren (http://github.com/Wren6991)
* License:
**************************************************************/
#include <cmath>
#include <map>
#include <GLFW/glfw3.h>
#include <IL/il.h>
#include <IL/ilu.h>
#include "game.h"
#include "util.h"
#include <sstream>
int scroll_delta = 0;
// GGGGG RRRR A PPPP H H IIIIIII CCC SSS
// GG R RR A A P PP H H I CC CC SS SS
// GG R RR A A P PP H H I CC C S
// G R RR A A P PP H H I C SS
// G RRRR AAAAAAA PPPP HHHHHHH I C SSS
// G GGGG R RR A A P H H I C SS
// GG G R R A A P H H I CC C S
// GG GG R R A A P H H I CC CC SS SS
// GGGG R R A A P H H IIIIIII CCC SSS
void initgl(GLFWwindow *window, game *gm)
{
// Set the context, clear the canvas and set up all the matrices.
glfwGetWindowSize(window, &(gm->canvaswidth), &(gm->canvasheight));
glViewport(0, 0, gm->canvaswidth, gm->canvasheight);
glClearColor(0.529, 0.808, 0.980, 1); //(cornflower blue)
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
float halfheight = gm->zoomsize;
float halfwidth = (float)gm->canvaswidth / gm->canvasheight * halfheight;
glFrustum(-halfwidth, halfwidth, -halfheight, halfheight, 1, 1000);
glTranslatef(-gm->camx, -gm->camy, 0);
glEnable(GL_LINE_SMOOTH);
glHint(GL_LINE_SMOOTH, GL_NICEST);
glEnable(GL_POINT_SMOOTH);
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA,GL_ONE_MINUS_SRC_ALPHA);
glPointSize(0.15f * gm->canvasheight / gm->zoomsize);
glLineWidth(0.1f * gm->canvasheight / gm->zoomsize);
glColor3f(0, 0, 0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}
void endgl(GLFWwindow *window, game *gm)
{
// Flush all the draw operations and flip the back buffer onto the screen.
glFlush();
glfwSwapBuffers(window);
}
/*void titanicFrame::OnMenuItemLoadSelected(wxCommandEvent& event)
{
if (dlgOpen->ShowModal() == wxID_OK)
{
std::string filename = dlgOpen->GetPath().ToStdString();
delete gm.wld;
gm.wld = new phys::world;
gm.assertSettings();
gm.loadShip(filename);
}
}*/
// M M OOO U U SSS EEEEEEE
// MM MM O O U U SS SS E
// M M M M O O U U S E
// M M M O O U U SS E
// M M O O U U SSS EEEE
// M M O O U U SS E
// M M O O U U S E
// M M O O U U SS SS E
// M M OOO UUU SSS EEEEEEE
/*
void titanicFrame::OnMenuReloadSelected(wxCommandEvent& event)
{
delete gm.wld;
gm.wld = new phys::world;
gm.assertSettings();
gm.loadShip(gm.lastFilename);
}*/
void doInput(GLFWwindow *window, game &gm)
{
double xd, yd;
glfwGetCursorPos(window, &xd, &yd);
gm.mouse.x = xd;
gm.mouse.y = yd;
gm.mouse.ldown = glfwGetMouseButton(window, GLFW_MOUSE_BUTTON_LEFT);
gm.mouse.rdown = glfwGetMouseButton(window, GLFW_MOUSE_BUTTON_RIGHT);
if (scroll_delta != 0)
{
gm.zoomsize *= pow(0.85, scroll_delta);
scroll_delta = 0;
}
}
void scrollCallback(GLFWwindow *window, double x, double y)
{
scroll_delta += y;
}
template <typename T> std::string tostring(T x)
{
std::stringstream ss;
ss << x;
return ss.str();
}
int main()
{
ilInit();
iluInit();
if (glfwInit() == -1)
return -1;
GLFWwindow *window = glfwCreateWindow(1024, 768, "Sinking Simulator", NULL, NULL);
glfwSwapInterval(1);
glfwSetScrollCallback(window, scrollCallback);
double lasttime = glfwGetTime();
int nframes = 0;
glfwMakeContextCurrent(window);
game gm;
gm.loadShip("ship.png");
bool running = true;
while (running && !glfwWindowShouldClose(window))
{
nframes++;
if (glfwGetTime() - lasttime > 1.0)
{
lasttime = glfwGetTime();
glfwSetWindowTitle(window, ("Sinking Simulator - " + gm.lastFilename + " (" + tostring<int>(nframes) + " FPS)").c_str());
nframes = 0;
}
doInput(window, gm);
gm.update();
initgl(window, &gm);
gm.render();
endgl(window, &gm);
glfwPollEvents();
}
glfwTerminate();
return 0;
}