Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

wxWidgets for Joyce #8

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,12 @@
UTILS_DIR = utils/
TEST_DIR = tests/
SRC_INP = src/input
SRC_OUT = src/output

all:
cd $(UTILS_DIR); make all
cd $(SRC_INP); make all
cd $(SRC_OUT); make all
cd $(TEST_DIR); make all


Expand All @@ -23,4 +25,5 @@ testall:
cleanall:
cd $(UTILS_DIR); make cleanall
cd $(SRC_INP); make cleanall
cd $(SRC_OUT); make cleanall
cd $(TEST_DIR); make cleanall
Binary file added bin/test_CA_input
Binary file not shown.
12 changes: 11 additions & 1 deletion include/CellularAutomata.h
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,17 @@ class CellularAutomata
void vn_neighborhood(int row, int column);

// Code to evaluate moore neighborhood where r=1.
void moore_neighborhood(int row, int column);
void moore_neighborhood(int row, int column);

// Getter function to retrieve the data of the Cellular Automata.
std::vector<std::vector<int>> get_data();

// Gives information on the stats of the states in the Cellular Automata.
void get_stats();

void step(int rule_num);

void run(int num_steps, int rule_num);

// Function to print out the Cellular Automata.
void print();
Expand Down
35 changes: 35 additions & 0 deletions include/wxgui.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
#pragma once
// wxWidgets "Hello World" Program
// For compilers that support precompilation, includes "wx/wx.h".
#include <wx/wxprec.h>
#ifndef WX_PRECOMP
#include <wx/wx.h>
#include <wx/grid.h>
#include<wx/gdicmn.h>
#include <wx/string.h>
#endif

class cMain : public wxFrame
{

private:
wxGrid *grid;
wxString mystring;
wxColourDatabase *colour;

public:
cMain(int rows, int columns);

};

class cApp : public wxApp
{
private:
cMain* m_frame1 = nullptr;

public:
virtual bool OnInit();
};



10 changes: 5 additions & 5 deletions src/input/CA_input.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,6 @@ void CellularAutomata::Initialize_Density()
_data[_starting_position.first][_starting_position.second] = _legend_density[_reactor].first;
}


// Code to evaluate von neumman neighborhood where r = 1 with periodic bounds.
void CellularAutomata::vn_neighborhood(int row, int column)
{
Expand Down Expand Up @@ -165,9 +164,11 @@ void CellularAutomata::moore_neighborhood(int row, int column)

}

void CellularAutomata::run(int num_steps, int rule_num){
for(int i=0; i < num_steps; i++){
step(int rule_num);
void CellularAutomata::run(int num_steps, int rule_num)
{
for(int i=0; i < num_steps; i++)
{
step(rule_num);
}
}

Expand All @@ -181,7 +182,6 @@ void CellularAutomata::step(int rule_num){
}
}


// Temporary print that is being used to test if the Initialize function is working as intended.
// Print the formatted matrix out to the terminal using std::cout. Each row is printed with the first element of the Cellular Automata following an opening square bracket and all elements being seperated by commas. The last element of the Cellular Automata is also followed by a closing square bracket.
void CellularAutomata::print()
Expand Down
60 changes: 60 additions & 0 deletions src/output/CA_output.cxx
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
#include <iostream>
#include <utility>
#include "CellularAutomata.h"
#include "myutils.h"

// Getter function to retreive the vector
std::vector<std::vector<int>> CellularAutomata::get_data()
{
std::vector<std::vector<int>> return_data;
return_data.resize(_rows);
for (int i = 0; i < _rows; i++)
{
for (int j = 0; j < _columns; j++)
{
return_data[i].push_back(_data[i][j]);
}
}
return return_data;
}

// Gives information on the stats of the states in the Cellular Automata.
void CellularAutomata::get_stats()
{
if (_legend.empty() == false)
{
for(const auto &it : _legend)
{
int counter = 0;
for (int i = 0; i < _rows; i++)
{
for (int j = 0; j < _columns; j++)
{
if (_data[i][j] == it.second)
{
counter++;
}
}
}
std::cout << "The state: " << it.first << " occupied " << counter << " cells" << std::endl;
}
}
else
{
for(const auto &it2 : _legend_density)
{
int counter2 = 0;
for (int i = 0; i < _rows; i++)
{
for (int j = 0; j < _columns; j++)
{
if (_data[i][j] == it2.second.first)
{
counter2++;
}
}
}
std::cout << "The state: " << it2.first << " occupied " << counter2 << " cells" << std::endl;
}
}
}
6 changes: 5 additions & 1 deletion src/output/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,11 @@ INC_DIR = ../../include
LIB_DIR = ../../lib

# targets this makefile builds
OUTPUT_OBJS=
OUTPUT_OBJS = CA_output.o

CA_output.o: $(INC_DIR)/myutils.h $(INC_DIR)/CellularAutomata.h
$(CXX) $(CXXFLAGS) CA_output.cxx -I$(INC_DIR)
mv CA_output.o $(LIB_DIR)

all: $(OUTPUT_OBJS)

Expand Down
Binary file added src/output/wx
Binary file not shown.
66 changes: 66 additions & 0 deletions src/output/wxgui.cxx
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
#include "../../include/wxgui.h"
#include "../../include/CellularAutomata.h"
#include <map>
#include <iostream>
#include <vector>

wxIMPLEMENT_APP(cApp);

cMain::cMain(int rows, int columns) : wxFrame(nullptr, wxID_ANY, "Cellular Automata", wxPoint(30, 30), wxSize(1700, 1000))
{
// This is still very early development, do not merge into the repo
// Sets up the grid
grid = new wxGrid( this, -1, wxPoint( 0, 0 ));

// Sets up Colour database which will be used to convert wxString colours to wxColour
colour = new wxColourDatabase();

// Then we call CreateGrid to set the dimensions of the grid
// (10 rows and 10 columns)
grid->CreateGrid(rows, columns, wxGrid::wxGridSelectNone);

// Prevents any editing from the user input
grid->EnableEditing(false);

// Makes the header labels disappear
grid->SetRowLabelSize(0);
grid->SetColLabelSize(0);

// Sets the grind line between cells to black
grid->SetGridLineColour (*wxBLACK);

// Prevent users to resizing the cells
grid->DisableDragRowSize();
grid->DisableDragColSize();

// Setting size for the row and columns
grid->SetDefaultColSize (50);
grid->SetDefaultRowSize (50);

// Set up grid colours based on Cellular automata states.
std::vector<std::vector<int>> test = {{1, 1, 1}, {1, 0, 1}, {0, 1, 2}};
std::map<int, std::string> test_map{{0, "White"}, {1, "Green"}, {2, "Red"}, {3, "Brown"}};

for (int i = 0; i < rows; i++)
{
for (int j = 0; j < columns; j++)
{
// Get string for color at that cell
std::string color = test_map[test[i][j]];

// Now convert the string to the wxString
mystring = wxString(color);

// Colour each cell in the grid with the colour associated with it
// colour->find(mystring) is used to look up the wxColour associated with the name
grid->SetCellBackgroundColour(i, j, colour->Find(mystring));
}
}
}

bool cApp::OnInit()
{
m_frame1 = new cMain(3, 3);
m_frame1->Show();
return true;
}
2 changes: 1 addition & 1 deletion tests/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ BIN_DIR = ../bin
EXECUTABLES = test_CA_input

test_CA_input: $(INC_DIR)/myutils.h $(INC_DIR)/CellularAutomata.h
$(CXX) $(CXXFLAGS) test_CA_input test_CA_input.cxx -I$(INC_DIR) $(LIB_DIR)/utils.o $(LIB_DIR)/CA_input.o
$(CXX) $(CXXFLAGS) test_CA_input test_CA_input.cxx -I$(INC_DIR) $(LIB_DIR)/utils.o $(LIB_DIR)/CA_input.o $(LIB_DIR)/CA_output.o
mv test_CA_input $(BIN_DIR)

all: $(EXECUTABLES)
Expand Down
10 changes: 9 additions & 1 deletion tests/test_CA_input.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,13 @@ int main(void)
Test_2.print();
// Testing von neumaan neighborhood analysis
std::cout << "Testing Von Neumaan Neighborhood funtionality around 5, 5:" << std::endl;
Test_2.vn_neighborhood(5,5);
Test_2.vn_neighborhood(5,5);
std::cout << std::endl;
std::cout << std::endl;
Test_2.get_stats();
std::cout << std::endl;
std::cout << std::endl;


std::cout << "Creating Cellular Automata using Density: " << std::endl;
std::map<std::string, std::pair<int, float>> test_legend_density{{"empty", {0, 0.5}}, {"tree", {1, 0.5}}, {"fire", {2, 0.0}}, {"char", {3, 0.0}}};
Expand All @@ -46,4 +50,8 @@ int main(void)
Test_3.moore_neighborhood(0,5);
std::cout << std::endl;
std::cout << std::endl;
Test_3.get_stats();
std::cout << std::endl;
std::cout << std::endl;

}
Binary file added wx
Binary file not shown.