-
Notifications
You must be signed in to change notification settings - Fork 0
/
MainWindow.cpp
125 lines (83 loc) · 3.31 KB
/
MainWindow.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
#include "MainWindow.h"
#include "GameLoop.h"
#include "Sprites.h"
#include <QOpenGLWidget>
using namespace std;
MainWindow::MainWindow(QGraphicsView* parent) : QMainWindow(parent) {
sceneRect = QRect(0,0,aspectratio*480,480);
setGeometry(sceneRect.x(), sceneRect.y(), sceneRect.width(), sceneRect.height());
scene = new GraphicsScene();
//scene->addPixmap(QPixmap("C:/Users/Edo/Desktop/Kirby-Tec/design/Map.png"));
scene->setBackgroundBrush(QColor(135,206,235));
view = new QGraphicsView(scene);
view->setGeometry(sceneRect);
view->setCacheMode(QGraphicsView::CacheBackground);
//view->setMouseTracking(true);
QSurfaceFormat format;
format.setSamples(16);
format.setSwapInterval(0);
format.setRenderableType(QSurfaceFormat::OpenGL);
format.setSwapBehavior(QSurfaceFormat::TripleBuffer);
qgl = new QOpenGLWidget();
qgl->setFormat(format);
view->setViewport(qgl);
//std::cout << "Swap interval " << format.swapInterval();
this->setMouseTracking(true);
scene->setSceneRect(sceneRect);
setCentralWidget(view);
show();
setWindowTitle(tr("Kirby's Adventure"));
setWindowIcon(QIcon("sprites/kirbyicon.png"));
}
void MainWindow::resizeEvent(QResizeEvent* event) {
//GameLoop::getInstance().pause();
double expectedwidth = (double)(this->height() * aspectratio);
double expectedheight = (double)(this->width() / aspectratio);
// Resize window to avoi
if (expectedwidth != this->width() && false) {
event->accept();
this->resize(QSize(expectedwidth, this->height()));
}
if (expectedwidth > this->width()) {
expectedwidth = this->width();
}
else {
expectedheight = this->height();
}
QMainWindow::resizeEvent(event);
// Resize drawspace here
sceneRect.setWidth(expectedwidth);
sceneRect.setHeight(expectedheight);
Camera::getInstance().screenwidth = expectedwidth;
Camera::getInstance().screenheight = expectedheight;
scene->setSceneRect(QRect(0,0,sceneRect.width()-5, sceneRect.height()-5));
view->setGeometry(sceneRect);
scale = ((expectedheight) / ((double)(1280.0/aspectratio))) * scalemultiplier;
scalefactor = scale * standardsize;
//GameLoop::getInstance().pause(false);
}
void MainWindow::pleaseRender(bool clearscene) {
for (auto* item : GameLoop::getInstance().renderableObjects)
item->render(*scene, clearscene);
if (GameLoop::getInstance().renderableObjectsToBeDeleted.size() > 0) {
for (auto* item : GameLoop::getInstance().renderableObjectsToBeDeleted)
item->render(*scene, true);
GameLoop::getInstance().renderableObjectsToBeDeleted.clear();
}
for (auto* item : GameLoop::getInstance().GUIItems)
item->render(*scene, clearscene);
//GameLoop::getInstance().getPauseGUI().render(*scene, clearscene);
//GameLoop::getInstance().getPauseSuggestion().render(*scene, clearscene);
//GameLoop::getInstance().getStartGUI().render(*scene, clearscene);
if (scene->items().size() == 0 || clearscene) {
scene->clear();
qDeleteAll(scene->items());
//qgl->update();
//view->update();
}
scene->update();
emit(renderingCompleted());
}
void MainWindow::closeEvent(QCloseEvent* event){
this->hide();
}