This repository has been archived by the owner on Nov 11, 2017. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
MainWindow.cpp
133 lines (117 loc) · 4.29 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
126
127
128
129
130
131
132
133
#include "MainWindow.h"
#include <QMenuBar>
#include <QApplication>
#include <QMessageBox>
#include <QFrame>
#include <QHBoxLayout>
#include <QPushButton>
MainWindow::MainWindow()
: QMainWindow(NULL), m_LuaState(NULL), m_Cells(NULL)
{
m_pNewSimDialog = new NewSimDialog(this);
connect(m_pNewSimDialog, SIGNAL(newSim(int, int, QString)),
this, SLOT(newSim(int, int, QString)));
m_pNewSimDialog->hide();
QMenu *fileMenu = menuBar()->addMenu(trUtf8("&File"));
{
QAction *newSim = new QAction(trUtf8("&New simulation..."), this);
connect(newSim, SIGNAL(triggered()), m_pNewSimDialog, SLOT(show()));
fileMenu->addAction(newSim);
}
fileMenu->addSeparator();
{
QAction *quitAct = new QAction(trUtf8("&Quit"), this);
connect(quitAct, SIGNAL(triggered()), qApp, SLOT(quit()));
fileMenu->addAction(quitAct);
}
setCentralWidget(new QFrame);
QHBoxLayout *layout = new QHBoxLayout;
m_pGrid = new GridWidget;
layout->addWidget(m_pGrid);
{
QVBoxLayout *right = new QVBoxLayout;
QPushButton *blank = new QPushButton(trUtf8("Empty the grid"));
blank->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
connect(blank, SIGNAL(clicked()), m_pGrid, SLOT(resetBlank()));
right->addWidget(blank);
QPushButton *random = new QPushButton(trUtf8("Randomize"));
random->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
connect(random, SIGNAL(clicked()), m_pGrid, SLOT(resetRandom()));
right->addWidget(random);
QPushButton *t = new QPushButton(trUtf8("Tick"));
t->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
connect(t, SIGNAL(clicked()), this, SLOT(tick()));
right->addWidget(t);
layout->addLayout(right);
}
// TODO : slider to set a simulation speed (and disable the Tick button)
centralWidget()->setLayout(layout);
}
void MainWindow::newSim(int width, int height, QString script)
{
if(m_Cells)
delete[] m_Cells;
if(m_LuaState)
lua_close(m_LuaState);
m_LuaState = lua_open();
luaL_openlibs(m_LuaState);
if(luaL_loadstring(m_LuaState, script.toAscii()) != 0)
{
QMessageBox::critical(this, trUtf8("Error from Lua"),
trUtf8("Error loading script:\n%1").arg(lua_tostring(m_LuaState, -1)));
}
if(lua_pcall(m_LuaState, 0, 0, 0) != 0)
{
QMessageBox::critical(this, trUtf8("Error from Lua"),
trUtf8("Error starting script:\n%1").arg(lua_tostring(m_LuaState, -1)));
}
m_Cells = new Cell[width*height];
m_iWidth = width;
m_iHeight = height;
m_pGrid->setData(m_Cells, width, height);
}
void MainWindow::tick()
{
if(!m_Cells)
return ;
int y, x;
for(y = 0; y < m_iHeight; y++)
{
for(x = 0; x < m_iWidth; x++)
{
Cell &cell = m_Cells[y*m_iWidth + x];
lua_getglobal(m_LuaState, "tick");
lua_pushinteger(m_LuaState, cell.getState());
lua_createtable(m_LuaState, 0, 8);
int i, j;
for(j = -1; j <= 1; j++)
{
if(y+j < 0 || y+j >= m_iHeight)
continue;
for(i = -1; i <= 1; i++)
{
if(i == 0 && j == 0)
continue;
if(x+i < 0 || x+i >= m_iWidth)
continue;
QString key = QString("%1;%2").arg(i).arg(j);
lua_pushstring(m_LuaState, key.toAscii());
lua_pushinteger(m_LuaState, m_Cells[(y+j)*m_iWidth + x+i].getState());
lua_settable(m_LuaState, -3);
}
}
if(lua_pcall(m_LuaState, 2, 1, 0) != 0)
{
QMessageBox::critical(this, trUtf8("Error from Lua"),
trUtf8("An error occurred:\n%1").arg(lua_tostring(m_LuaState, -1)));
}
cell.setFutureState(lua_tointeger(m_LuaState, -1));
}
}
for(y = 0; y < m_iHeight; y++)
{
for(x = 0; x < m_iWidth; x++)
m_Cells[y*m_iWidth + x].tick();
}
m_pGrid->update();
}