1998-12-31 05:54:41 -05:00
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
// Name: bombs.cpp
|
|
|
|
// Purpose: Bombs game
|
|
|
|
// Author: P. Foggia 1996
|
2005-05-10 15:22:06 -04:00
|
|
|
// Modified by: Wlodzimierz Skiba (ABX) since 2003
|
1998-12-31 05:54:41 -05:00
|
|
|
// Created: 1996
|
|
|
|
// Copyright: (c) 1996 P. Foggia
|
|
|
|
// Licence: wxWindows licence
|
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
#include "wx/wxprec.h"
|
|
|
|
|
2003-12-18 20:34:40 -05:00
|
|
|
|
1998-12-31 05:54:41 -05:00
|
|
|
#ifndef WX_PRECOMP
|
2003-12-18 20:34:40 -05:00
|
|
|
# include "wx/wx.h"
|
1998-12-31 05:54:41 -05:00
|
|
|
#endif //precompiled headers
|
|
|
|
|
2005-01-31 13:02:36 -05:00
|
|
|
#include "wx/stockitem.h"
|
|
|
|
|
1998-12-31 05:54:41 -05:00
|
|
|
#include "bombs.h"
|
|
|
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
|
2015-08-27 11:48:54 -04:00
|
|
|
#include <time.h>
|
2003-12-18 20:34:40 -05:00
|
|
|
|
2012-03-03 19:28:58 -05:00
|
|
|
#ifndef wxHAS_IMAGES_IN_RESOURCES
|
2003-12-18 20:34:40 -05:00
|
|
|
# include "bombs.xpm"
|
1998-12-31 05:54:41 -05:00
|
|
|
#endif
|
|
|
|
|
2015-04-23 07:49:01 -04:00
|
|
|
wxIMPLEMENT_APP(BombsApp);
|
2003-12-18 20:34:40 -05:00
|
|
|
|
1998-12-31 05:54:41 -05:00
|
|
|
// Called to initialize the program
|
2003-12-18 20:34:40 -05:00
|
|
|
bool BombsApp::OnInit()
|
1998-12-31 05:54:41 -05:00
|
|
|
{
|
2003-12-18 20:34:40 -05:00
|
|
|
srand((unsigned) time(NULL));
|
1998-12-31 05:54:41 -05:00
|
|
|
|
2003-12-18 20:34:40 -05:00
|
|
|
m_frame = new BombsFrame(&m_game);
|
1998-12-31 05:54:41 -05:00
|
|
|
|
2003-12-23 05:45:28 -05:00
|
|
|
m_frame->NewGame(bombsID_EASY, false);
|
1998-12-31 05:54:41 -05:00
|
|
|
|
2003-12-18 20:34:40 -05:00
|
|
|
return true;
|
1998-12-31 05:54:41 -05:00
|
|
|
}
|
|
|
|
|
2015-04-23 07:49:01 -04:00
|
|
|
wxBEGIN_EVENT_TABLE(BombsFrame, wxFrame)
|
2005-05-10 15:22:06 -04:00
|
|
|
EVT_MENU(wxID_NEW, BombsFrame::OnNewGame)
|
|
|
|
EVT_MENU(bombsID_EASY, BombsFrame::OnEasyGame)
|
|
|
|
EVT_MENU(bombsID_MEDIUM, BombsFrame::OnMediumGame)
|
|
|
|
EVT_MENU(bombsID_HARD, BombsFrame::OnHardGame)
|
2005-05-06 14:07:14 -04:00
|
|
|
EVT_MENU(bombsID_EASYCORNER, BombsFrame::OnEasyCorner)
|
|
|
|
EVT_MENU(wxID_EXIT, BombsFrame::OnExit)
|
|
|
|
EVT_MENU(wxID_ABOUT, BombsFrame::OnAbout)
|
2015-04-23 07:49:01 -04:00
|
|
|
wxEND_EVENT_TABLE()
|
1998-12-31 05:54:41 -05:00
|
|
|
|
2003-12-18 20:34:40 -05:00
|
|
|
BombsFrame::BombsFrame(BombsGame *game)
|
|
|
|
: wxFrame(NULL, wxID_ANY, wxT("wxBombs"), wxDefaultPosition,
|
|
|
|
wxSize(300, 300), wxDEFAULT_DIALOG_STYLE|wxMINIMIZE_BOX)
|
1998-12-31 05:54:41 -05:00
|
|
|
{
|
2003-12-18 20:34:40 -05:00
|
|
|
m_game = game;
|
2005-05-06 14:07:14 -04:00
|
|
|
m_easyCorner = false;
|
|
|
|
m_lastLevel = bombsID_EASY;
|
1998-12-31 05:54:41 -05:00
|
|
|
|
2003-12-18 20:34:40 -05:00
|
|
|
SetIcon(wxICON(bombs));
|
1998-12-31 05:54:41 -05:00
|
|
|
|
2003-12-18 20:34:40 -05:00
|
|
|
#if wxUSE_STATUSBAR
|
|
|
|
CreateStatusBar();
|
|
|
|
#endif
|
|
|
|
|
|
|
|
// Create a menu bar for the frame
|
|
|
|
wxMenuBar *menuBar = new wxMenuBar;
|
|
|
|
wxMenu *menuFile = new wxMenu;
|
|
|
|
wxMenu *menuLevel = new wxMenu;
|
|
|
|
menuLevel->AppendRadioItem(bombsID_EASY, wxT("&Easy (10x10)\tCtrl-1"));
|
|
|
|
menuLevel->AppendRadioItem(bombsID_MEDIUM, wxT("&Medium (15x15)\tCtrl-2"));
|
|
|
|
menuLevel->AppendRadioItem(bombsID_HARD, wxT("&Hard (25x20)\tCtrl-3"));
|
|
|
|
|
2005-05-10 15:22:06 -04:00
|
|
|
menuFile->Append(wxID_NEW, wxT("&New game\tCtrl-N"));
|
|
|
|
menuFile->Append(bombsID_LEVEL, wxT("&Level"),menuLevel, wxT("Starts a new game"));
|
2005-05-06 14:07:14 -04:00
|
|
|
menuFile->AppendCheckItem(bombsID_EASYCORNER, wxT("&Easy corner"));
|
2003-12-18 20:34:40 -05:00
|
|
|
|
|
|
|
menuFile->AppendSeparator();
|
2005-01-31 13:02:36 -05:00
|
|
|
menuFile->Append(wxID_EXIT, wxGetStockLabel(wxID_EXIT), wxT("Quits the application"));
|
2003-12-18 20:34:40 -05:00
|
|
|
|
|
|
|
menuBar->Append(menuFile, wxT("&File"));
|
|
|
|
|
|
|
|
|
|
|
|
wxMenu *menuHelp = new wxMenu;
|
|
|
|
menuHelp->Append(wxID_ABOUT, wxT("&About"),
|
|
|
|
wxT("Displays the program information") );
|
|
|
|
|
|
|
|
menuBar->Append(menuHelp, wxT("&Help"));
|
|
|
|
|
|
|
|
SetMenuBar(menuBar);
|
|
|
|
|
|
|
|
// Create child subwindows.
|
|
|
|
m_canvas = new BombsCanvas(this, m_game);
|
|
|
|
|
|
|
|
// Ensure the subwindows get resized o.k.
|
|
|
|
// OnSize(width, height);
|
|
|
|
|
|
|
|
// Centre frame on the screen.
|
|
|
|
Centre(wxBOTH);
|
|
|
|
|
|
|
|
// Show the frame.
|
|
|
|
Show();
|
1998-12-31 05:54:41 -05:00
|
|
|
}
|
|
|
|
|
2003-12-18 20:34:40 -05:00
|
|
|
void BombsFrame::OnExit(wxCommandEvent& WXUNUSED(event))
|
1998-12-31 05:54:41 -05:00
|
|
|
{
|
2003-12-18 20:34:40 -05:00
|
|
|
Close();
|
1998-12-31 05:54:41 -05:00
|
|
|
}
|
|
|
|
|
2003-12-23 05:45:28 -05:00
|
|
|
void BombsFrame::NewGame(int level, bool query)
|
1998-12-31 05:54:41 -05:00
|
|
|
{
|
2003-12-23 05:45:28 -05:00
|
|
|
if(query)
|
|
|
|
{
|
|
|
|
int ok = wxMessageBox(
|
|
|
|
wxT("Start new game regardless previous board?"),
|
|
|
|
wxT("Confirm"),
|
|
|
|
wxYES_NO | wxICON_QUESTION,
|
|
|
|
this
|
|
|
|
);
|
|
|
|
if(ok!=wxYES)return;
|
|
|
|
}
|
2003-12-18 20:34:40 -05:00
|
|
|
|
|
|
|
int numHorzCells = 20, numVertCells = 20;
|
2005-05-06 14:07:14 -04:00
|
|
|
m_lastLevel = level;
|
2003-12-18 20:34:40 -05:00
|
|
|
|
|
|
|
switch(level)
|
|
|
|
{
|
|
|
|
case bombsID_EASY:
|
|
|
|
numHorzCells = numVertCells = 10;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case bombsID_MEDIUM:
|
|
|
|
numHorzCells = numVertCells = 15;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case bombsID_HARD:
|
|
|
|
numHorzCells = 25; numVertCells = 20;
|
|
|
|
break;
|
|
|
|
|
|
|
|
default :
|
|
|
|
wxFAIL_MSG(wxT("Invalid level"));
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2005-05-06 14:07:14 -04:00
|
|
|
m_game->Init(numHorzCells, numVertCells, m_easyCorner);
|
2003-12-18 20:34:40 -05:00
|
|
|
|
|
|
|
GetMenuBar()->Check(level, true);
|
|
|
|
|
|
|
|
m_canvas->UpdateGridSize();
|
|
|
|
SetClientSize(m_canvas->GetGridSizeInPixels());
|
1998-12-31 05:54:41 -05:00
|
|
|
}
|
|
|
|
|
2003-12-18 20:34:40 -05:00
|
|
|
void BombsFrame::OnAbout(wxCommandEvent& WXUNUSED(event))
|
1998-12-31 05:54:41 -05:00
|
|
|
{
|
2003-12-18 20:34:40 -05:00
|
|
|
wxMessageBox(
|
|
|
|
wxT("wxBombs (c) 1996 by P. Foggia\n<foggia@amalfi.dis.unina.it>"),
|
|
|
|
wxT("About wxBombs") );
|
1998-12-31 05:54:41 -05:00
|
|
|
}
|
|
|
|
|
2005-05-10 15:22:06 -04:00
|
|
|
void BombsFrame::OnNewGame(wxCommandEvent& WXUNUSED(event))
|
|
|
|
{
|
|
|
|
NewGame(m_lastLevel, true);
|
|
|
|
}
|
|
|
|
|
|
|
|
void BombsFrame::OnEasyGame(wxCommandEvent& WXUNUSED(event))
|
1998-12-31 05:54:41 -05:00
|
|
|
{
|
2003-12-23 05:45:28 -05:00
|
|
|
NewGame(bombsID_EASY, true);
|
1998-12-31 05:54:41 -05:00
|
|
|
}
|
|
|
|
|
2005-05-10 15:22:06 -04:00
|
|
|
void BombsFrame::OnMediumGame(wxCommandEvent& WXUNUSED(event))
|
1998-12-31 05:54:41 -05:00
|
|
|
{
|
2003-12-23 05:45:28 -05:00
|
|
|
NewGame(bombsID_MEDIUM, true);
|
1998-12-31 05:54:41 -05:00
|
|
|
}
|
|
|
|
|
2005-05-10 15:22:06 -04:00
|
|
|
void BombsFrame::OnHardGame(wxCommandEvent& WXUNUSED(event))
|
1998-12-31 05:54:41 -05:00
|
|
|
{
|
2003-12-23 05:45:28 -05:00
|
|
|
NewGame(bombsID_HARD, true);
|
1998-12-31 05:54:41 -05:00
|
|
|
}
|
|
|
|
|
2005-05-06 14:07:14 -04:00
|
|
|
void BombsFrame::OnEasyCorner(wxCommandEvent& WXUNUSED(event))
|
|
|
|
{
|
|
|
|
wxString msg;
|
|
|
|
if(m_easyCorner)
|
|
|
|
msg = wxT("enable");
|
|
|
|
else
|
|
|
|
msg = wxT("disable");
|
|
|
|
|
|
|
|
msg = wxT("Do you really want to ") + msg + wxT(" having\ntop left corner always empty for easier start?");
|
|
|
|
|
|
|
|
int ok = wxMessageBox(
|
|
|
|
msg,
|
|
|
|
wxT("Confirm"),
|
|
|
|
wxYES_NO | wxICON_QUESTION,
|
|
|
|
this
|
|
|
|
);
|
|
|
|
|
|
|
|
if(ok!=wxYES)return;
|
|
|
|
|
|
|
|
m_easyCorner = !m_easyCorner;
|
|
|
|
|
|
|
|
NewGame(m_lastLevel, true);
|
|
|
|
}
|
|
|
|
|
2015-04-23 07:49:01 -04:00
|
|
|
wxBEGIN_EVENT_TABLE(BombsCanvas, wxPanel)
|
2003-12-18 20:34:40 -05:00
|
|
|
EVT_PAINT(BombsCanvas::OnPaint)
|
|
|
|
EVT_MOUSE_EVENTS(BombsCanvas::OnMouseEvent)
|
|
|
|
EVT_CHAR(BombsCanvas::OnChar)
|
2015-04-23 07:49:01 -04:00
|
|
|
wxEND_EVENT_TABLE()
|
1998-12-31 05:54:41 -05:00
|
|
|
|
2003-12-18 20:34:40 -05:00
|
|
|
BombsCanvas::BombsCanvas(wxFrame *parent, BombsGame *game)
|
|
|
|
: wxPanel(parent, wxID_ANY)
|
|
|
|
{
|
|
|
|
m_game = game;
|
|
|
|
int sx, sy;
|
|
|
|
wxClientDC dc(this);
|
2017-11-25 11:39:44 -05:00
|
|
|
dc.SetFont(BOMBS_FONT);
|
1998-12-31 05:54:41 -05:00
|
|
|
|
2007-05-09 21:46:57 -04:00
|
|
|
wxCoord chw, chh;
|
2003-12-18 20:34:40 -05:00
|
|
|
wxString buf = wxT("M");
|
1998-12-31 05:54:41 -05:00
|
|
|
|
2003-12-18 20:34:40 -05:00
|
|
|
dc.GetTextExtent(buf, &chw, &chh);
|
|
|
|
dc.SetFont(wxNullFont);
|
1998-12-31 05:54:41 -05:00
|
|
|
|
2003-12-18 20:34:40 -05:00
|
|
|
dc.SetMapMode(wxMM_METRIC);
|
1998-12-31 05:54:41 -05:00
|
|
|
|
2003-12-18 20:34:40 -05:00
|
|
|
int xcm = dc.LogicalToDeviceX(10);
|
|
|
|
int ycm = dc.LogicalToDeviceY(10);
|
1998-12-31 05:54:41 -05:00
|
|
|
// To have a square cell, there must be :
|
|
|
|
// sx*ycm == sy*xcm
|
2003-12-18 20:34:40 -05:00
|
|
|
if (chw*ycm < chh*xcm)
|
|
|
|
{
|
|
|
|
sy = chh;
|
|
|
|
sx = chh*xcm/ycm;
|
1998-12-31 05:54:41 -05:00
|
|
|
}
|
2003-12-18 20:34:40 -05:00
|
|
|
else
|
|
|
|
{
|
|
|
|
sx = chw;
|
|
|
|
sy = chw*ycm/xcm;
|
1998-12-31 05:54:41 -05:00
|
|
|
}
|
2003-12-18 20:34:40 -05:00
|
|
|
|
|
|
|
m_cellWidth = (sx+3+X_UNIT)/X_UNIT;
|
|
|
|
m_cellHeight = (sy+3+Y_UNIT)/Y_UNIT;
|
|
|
|
dc.SetMapMode(wxMM_TEXT);
|
|
|
|
m_bmp = NULL;
|
1998-12-31 05:54:41 -05:00
|
|
|
}
|
|
|
|
|
2003-12-18 20:34:40 -05:00
|
|
|
BombsCanvas::~BombsCanvas()
|
1998-12-31 05:54:41 -05:00
|
|
|
{
|
2003-12-18 20:34:40 -05:00
|
|
|
if (m_bmp)
|
|
|
|
{
|
|
|
|
delete m_bmp;
|
|
|
|
m_bmp = NULL;
|
|
|
|
}
|
1998-12-31 05:54:41 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// Called when canvas needs to be repainted.
|
2003-12-18 20:34:40 -05:00
|
|
|
void BombsCanvas::OnPaint(wxPaintEvent& WXUNUSED(event))
|
1998-12-31 05:54:41 -05:00
|
|
|
{
|
2003-12-18 20:34:40 -05:00
|
|
|
wxPaintDC dc(this);
|
|
|
|
|
|
|
|
const int numHorzCells = m_game->GetWidth();
|
|
|
|
const int numVertCells = m_game->GetHeight();
|
|
|
|
// Insert your drawing code here.
|
|
|
|
if (!m_bmp)
|
|
|
|
{
|
|
|
|
wxSize size = dc.GetSize();
|
|
|
|
m_bmp = new wxBitmap(size.GetWidth(), size.GetHeight());
|
|
|
|
if (m_bmp)
|
|
|
|
{
|
|
|
|
wxMemoryDC memDC;
|
|
|
|
memDC.SelectObject(*m_bmp);
|
|
|
|
DrawField(&memDC, 0, 0, numHorzCells-1, numVertCells-1);
|
|
|
|
memDC.SelectObject(wxNullBitmap);
|
1998-12-31 05:54:41 -05:00
|
|
|
}
|
|
|
|
}
|
2003-12-18 20:34:40 -05:00
|
|
|
|
|
|
|
if (m_bmp)
|
|
|
|
{
|
|
|
|
wxMemoryDC memDC;
|
|
|
|
memDC.SelectObject(*m_bmp);
|
|
|
|
wxSize size = dc.GetSize();
|
|
|
|
dc.Blit(0, 0, size.GetWidth(), size.GetHeight(),
|
|
|
|
&memDC, 0, 0, wxCOPY);
|
1998-12-31 05:54:41 -05:00
|
|
|
memDC.SelectObject(wxNullBitmap);
|
|
|
|
}
|
2003-12-18 20:34:40 -05:00
|
|
|
else
|
|
|
|
{
|
|
|
|
DrawField(&dc, 0, 0, numHorzCells-1, numVertCells-1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void BombsCanvas::UpdateGridSize()
|
|
|
|
{
|
|
|
|
|
|
|
|
if (m_bmp)
|
|
|
|
{
|
|
|
|
delete m_bmp;
|
|
|
|
m_bmp = NULL;
|
|
|
|
}
|
2005-04-17 09:07:02 -04:00
|
|
|
SetSize(GetGridSizeInPixels());
|
2003-12-18 20:34:40 -05:00
|
|
|
Refresh();
|
|
|
|
}
|
|
|
|
|
|
|
|
wxSize BombsCanvas::GetGridSizeInPixels() const
|
|
|
|
{
|
|
|
|
return wxSize(m_cellWidth*X_UNIT*m_game->GetWidth(),
|
|
|
|
m_cellHeight*Y_UNIT*m_game->GetHeight());
|
1998-12-31 05:54:41 -05:00
|
|
|
}
|
|
|
|
|