1998-12-31 05:54:41 -05:00
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
// Name: fractal.cpp
|
|
|
|
// Purpose: demo of wxConfig and related classes
|
|
|
|
// Author: Andrew Davison
|
|
|
|
// Modified by:
|
|
|
|
// Created: 05.04.94
|
|
|
|
// RCS-ID: $Id$
|
|
|
|
// Copyright: (c) 1994 Andrew Davison
|
|
|
|
// Licence: wxWindows licence
|
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
Date: Tue, 5 Apr 1994 12:01:18 +1000
|
|
|
|
From: Andrew Davison <andrewd@au.com.sfe>
|
|
|
|
To: wxwin-users@ed.aiai
|
|
|
|
Subject: Fractal mountains
|
|
|
|
|
|
|
|
Hi,
|
|
|
|
|
|
|
|
This is a quick port of a fractal mountain generator originally
|
|
|
|
done for MS-Windows. On a Sun the colours look a little washed
|
|
|
|
out and there is not as much snow or high mountains (maybe the
|
|
|
|
random number generators fault). The viewing plane is not
|
|
|
|
quite right as the original code used SetViewportOrg() which there
|
2004-05-25 07:20:37 -04:00
|
|
|
doesn't seem to be an equivalent of under wxWidgets, and my quick
|
2004-12-14 15:52:13 -05:00
|
|
|
hack doesn't fix.
|
1998-12-31 05:54:41 -05:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include "wx/wxprec.h"
|
|
|
|
|
2004-01-08 06:33:32 -05:00
|
|
|
#ifdef __BORLANDC__
|
|
|
|
#pragma hdrstop
|
|
|
|
#endif
|
|
|
|
|
1998-12-31 05:54:41 -05:00
|
|
|
#ifndef WX_PRECOMP
|
|
|
|
#include "wx/wx.h"
|
|
|
|
#endif //precompiled headers
|
|
|
|
|
2004-12-14 15:52:13 -05:00
|
|
|
#include "wx/math.h"
|
2005-01-31 13:18:18 -05:00
|
|
|
#include "wx/stockitem.h"
|
2004-12-14 15:52:13 -05:00
|
|
|
|
1998-12-31 05:54:41 -05:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <time.h>
|
|
|
|
|
|
|
|
#define Random(x) (rand() % x)
|
|
|
|
#define Randomize() (srand((unsigned int)time(NULL)))
|
|
|
|
|
2004-05-27 13:29:32 -04:00
|
|
|
static int detail = 9; // CHANGE THIS... 7,8,9 etc
|
1998-12-31 05:54:41 -05:00
|
|
|
|
2004-12-14 15:52:13 -05:00
|
|
|
static bool running = false;
|
1998-12-31 05:54:41 -05:00
|
|
|
static wxMenuBar *menuBar = NULL;
|
|
|
|
|
|
|
|
// Define a new application type
|
|
|
|
class MyApp: public wxApp
|
2004-05-27 13:29:32 -04:00
|
|
|
{
|
|
|
|
public:
|
|
|
|
bool OnInit();
|
1998-12-31 05:54:41 -05:00
|
|
|
};
|
|
|
|
|
|
|
|
IMPLEMENT_APP(MyApp)
|
|
|
|
|
|
|
|
// Define a new frame type
|
|
|
|
class MyFrame: public wxFrame
|
|
|
|
{
|
|
|
|
public:
|
2004-05-27 13:29:32 -04:00
|
|
|
MyFrame(wxFrame *frame, const wxString& title, const wxPoint& pos, const wxSize& size);
|
1998-12-31 05:54:41 -05:00
|
|
|
|
2004-05-27 13:29:32 -04:00
|
|
|
void OnCloseWindow(wxCloseEvent& event);
|
1998-12-31 05:54:41 -05:00
|
|
|
void OnExit(wxCommandEvent& event);
|
2004-05-27 13:29:32 -04:00
|
|
|
|
|
|
|
DECLARE_EVENT_TABLE()
|
1998-12-31 05:54:41 -05:00
|
|
|
};
|
|
|
|
|
|
|
|
// Define a new canvas which can receive some events
|
|
|
|
class MyCanvas: public wxWindow
|
|
|
|
{
|
|
|
|
public:
|
2004-05-27 13:29:32 -04:00
|
|
|
MyCanvas(wxFrame *frame);
|
|
|
|
void Draw(wxDC& dc);
|
1998-12-31 05:54:41 -05:00
|
|
|
|
|
|
|
private:
|
2004-05-27 13:29:32 -04:00
|
|
|
void OnPaint(wxPaintEvent& event);
|
|
|
|
void Fractal(wxDC& dc, int X1, int Y1, int X2, int Y2, int Z1, int Z2, int Z3, int Z4, int Iteration, double Std, double Ratio);
|
|
|
|
wxPen SnowPen, MtnPen, GreenPen;
|
|
|
|
wxBrush WaterBrush;
|
|
|
|
int Sealevel;
|
1998-12-31 05:54:41 -05:00
|
|
|
|
|
|
|
DECLARE_EVENT_TABLE()
|
|
|
|
};
|
|
|
|
|
|
|
|
// `Main program' equivalent, creating windows and returning main app frame
|
|
|
|
bool MyApp::OnInit()
|
|
|
|
{
|
|
|
|
// Create the main frame window
|
2004-05-27 13:29:32 -04:00
|
|
|
MyFrame *frame = new MyFrame(NULL, _T("Fractal Mountains for wxWidgets"), wxDefaultPosition, wxSize(640, 480));
|
1998-12-31 05:54:41 -05:00
|
|
|
|
|
|
|
// Make a menubar
|
|
|
|
wxMenu *file_menu = new wxMenu;
|
2005-01-31 13:18:18 -05:00
|
|
|
file_menu->Append(wxID_EXIT, wxGetStockLabel(wxID_EXIT));
|
1998-12-31 05:54:41 -05:00
|
|
|
menuBar = new wxMenuBar;
|
2002-12-22 14:57:17 -05:00
|
|
|
menuBar->Append(file_menu, _T("&File"));
|
1998-12-31 05:54:41 -05:00
|
|
|
frame->SetMenuBar(menuBar);
|
|
|
|
|
|
|
|
int width, height;
|
|
|
|
frame->GetClientSize(&width, &height);
|
|
|
|
|
|
|
|
(void) new MyCanvas(frame);
|
|
|
|
|
|
|
|
// Show the frame
|
2004-05-27 13:29:32 -04:00
|
|
|
frame->Show(true);
|
1998-12-31 05:54:41 -05:00
|
|
|
|
2004-05-27 13:29:32 -04:00
|
|
|
return true;
|
1998-12-31 05:54:41 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
BEGIN_EVENT_TABLE(MyFrame, wxFrame)
|
|
|
|
EVT_CLOSE(MyFrame::OnCloseWindow)
|
|
|
|
EVT_MENU(wxID_EXIT, MyFrame::OnExit)
|
|
|
|
END_EVENT_TABLE()
|
|
|
|
|
|
|
|
// My frame constructor
|
|
|
|
MyFrame::MyFrame(wxFrame *frame, const wxString& title, const wxPoint& pos, const wxSize& size):
|
2004-05-27 13:29:32 -04:00
|
|
|
wxFrame(frame, wxID_ANY, title, pos, size, wxDEFAULT_FRAME_STYLE | wxFULL_REPAINT_ON_RESIZE )
|
1998-12-31 05:54:41 -05:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
// Intercept menu commands
|
2003-09-18 09:33:34 -04:00
|
|
|
void MyFrame::OnExit(wxCommandEvent& WXUNUSED(event))
|
1998-12-31 05:54:41 -05:00
|
|
|
{
|
|
|
|
this->Destroy();
|
|
|
|
}
|
|
|
|
|
2003-09-18 09:33:34 -04:00
|
|
|
void MyFrame::OnCloseWindow(wxCloseEvent& WXUNUSED(event))
|
1998-12-31 05:54:41 -05:00
|
|
|
{
|
2004-05-27 13:29:32 -04:00
|
|
|
static bool destroyed = false;
|
1998-12-31 05:54:41 -05:00
|
|
|
if (destroyed)
|
|
|
|
return;
|
|
|
|
|
|
|
|
this->Destroy();
|
|
|
|
|
2004-05-27 13:29:32 -04:00
|
|
|
destroyed = true;
|
1998-12-31 05:54:41 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
BEGIN_EVENT_TABLE(MyCanvas, wxWindow)
|
|
|
|
EVT_PAINT(MyCanvas::OnPaint)
|
|
|
|
END_EVENT_TABLE()
|
|
|
|
|
|
|
|
// Define a constructor for my canvas
|
|
|
|
MyCanvas::MyCanvas(wxFrame *frame):
|
2004-05-27 13:29:32 -04:00
|
|
|
wxWindow(frame, wxID_ANY)
|
1998-12-31 05:54:41 -05:00
|
|
|
{
|
|
|
|
wxColour wxCol1(255,255,255);
|
|
|
|
SnowPen = wxPen(wxCol1, 2, wxSOLID);
|
|
|
|
|
|
|
|
wxColour wxCol2(128,0,0);
|
|
|
|
MtnPen = wxPen(wxCol2, 1, wxSOLID);
|
|
|
|
|
|
|
|
wxColour wxCol3(0,128,0);
|
|
|
|
GreenPen = wxPen(wxCol3, 1, wxSOLID);
|
|
|
|
|
|
|
|
wxColour wxCol4(0,0,128);
|
|
|
|
WaterBrush = wxBrush(wxCol4, wxSOLID);
|
|
|
|
}
|
|
|
|
|
2003-09-18 09:33:34 -04:00
|
|
|
void MyCanvas::OnPaint(wxPaintEvent& WXUNUSED(event))
|
1998-12-31 05:54:41 -05:00
|
|
|
{
|
|
|
|
wxPaintDC dc(this);
|
2004-05-27 13:29:32 -04:00
|
|
|
PrepareDC(dc);
|
|
|
|
Draw(dc);
|
1998-12-31 05:54:41 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
void MyCanvas::Draw(wxDC& dc)
|
|
|
|
{
|
|
|
|
if (running) return;
|
2004-12-14 15:52:13 -05:00
|
|
|
|
2004-05-27 13:29:32 -04:00
|
|
|
running = true;
|
|
|
|
menuBar->EnableTop(0, false);
|
|
|
|
|
|
|
|
Randomize();
|
1998-12-31 05:54:41 -05:00
|
|
|
|
2004-05-27 13:29:32 -04:00
|
|
|
dc.SetBackground(*wxLIGHT_GREY_BRUSH);
|
|
|
|
dc.Clear();
|
1998-12-31 05:54:41 -05:00
|
|
|
|
2004-05-27 13:29:32 -04:00
|
|
|
int Left, Top, Right, Bottom;
|
|
|
|
GetClientSize(&Right, &Bottom);
|
1998-12-31 05:54:41 -05:00
|
|
|
|
2004-05-27 13:29:32 -04:00
|
|
|
Right *= 3; Right /= 4;
|
|
|
|
Bottom *= 3; Bottom /= 4;
|
|
|
|
Left = 0;
|
|
|
|
Top = Bottom/8;
|
1998-12-31 05:54:41 -05:00
|
|
|
|
2004-05-27 13:29:32 -04:00
|
|
|
wxPoint Water[4];
|
|
|
|
Water[0].x = Left; Water[0].y = Top;
|
|
|
|
Water[1].x = Right; Water[1].y = Top;
|
|
|
|
Water[2].x = Right+Bottom/2; Water[2].y = Bottom;
|
|
|
|
Water[3].x = Bottom/2; Water[3].y = Bottom;
|
1998-12-31 05:54:41 -05:00
|
|
|
|
2004-05-27 13:29:32 -04:00
|
|
|
dc.SetBrush(WaterBrush);
|
|
|
|
dc.DrawPolygon(4, Water);
|
1998-12-31 05:54:41 -05:00
|
|
|
|
2004-05-27 13:29:32 -04:00
|
|
|
double H = 0.75;
|
|
|
|
double Scale = Bottom;
|
|
|
|
double Ratio = 1.0 / pow(2.0, H);
|
|
|
|
double Std = Scale * Ratio;
|
|
|
|
Sealevel = Random(18) - 8;
|
1998-12-31 05:54:41 -05:00
|
|
|
|
2004-05-27 13:29:32 -04:00
|
|
|
Fractal(dc, Left, Top, Right, Bottom, 0, 0, 0, 0, detail, Std, Ratio);
|
1998-12-31 05:54:41 -05:00
|
|
|
|
2004-05-27 13:29:32 -04:00
|
|
|
menuBar->EnableTop(0, true);
|
|
|
|
running = false;
|
1998-12-31 05:54:41 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
void MyCanvas::Fractal(wxDC& dc, int X1, int Y1, int X2, int Y2, int Z1, int Z2, int Z3, int Z4, int Iteration, double Std, double Ratio)
|
|
|
|
{
|
2004-05-27 13:29:32 -04:00
|
|
|
int Xmid = (X1 + X2) / 2;
|
|
|
|
int Ymid = (Y1 + Y2) / 2;
|
|
|
|
int Z23 = (Z2 + Z3) / 2;
|
|
|
|
int Z41 = (Z4 + Z1) / 2;
|
|
|
|
int Newz = (int)((Z1 + Z2 + Z3 + Z4) / 4 + (double)(Random(17) - 8) / 8.0 * Std);
|
|
|
|
|
|
|
|
if (--Iteration)
|
|
|
|
{
|
|
|
|
int Z12 = (Z1 + Z2) / 2;
|
|
|
|
int Z34 = (Z3 + Z4) / 2;
|
|
|
|
double Stdmid = Std * Ratio;
|
|
|
|
|
|
|
|
Fractal(dc, Xmid, Y1, X2, Ymid, Z12, Z2, Z23, Newz, Iteration, Stdmid, Ratio);
|
|
|
|
Fractal(dc, X1, Y1, Xmid, Ymid, Z1, Z12, Newz, Z41, Iteration, Stdmid, Ratio);
|
|
|
|
Fractal(dc, Xmid, Ymid, X2, Y2, Newz, Z23, Z3, Z34, Iteration, Stdmid, Ratio);
|
|
|
|
Fractal(dc, X1, Ymid, Xmid, Y2, Z41, Newz, Z34, Z4, Iteration, Stdmid, Ratio);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (Newz <= Sealevel)
|
|
|
|
{
|
|
|
|
wxPoint P[4];
|
|
|
|
P[0].x = Y1 / 2 + X1; P[0].y = Y1 + Z1;
|
|
|
|
P[1].x = Y1 / 2 + X2; P[1].y = Y1 + Z2;
|
|
|
|
P[2].x = Y2 / 2 + X2; P[2].y = Y2 + Z3;
|
|
|
|
P[3].x = Y2 / 2 + X1; P[3].y = Y2 + Z4;
|
|
|
|
|
|
|
|
dc.SetPen(* wxBLACK_PEN);
|
|
|
|
dc.SetBrush(* wxBLACK_BRUSH);
|
|
|
|
|
|
|
|
dc.DrawPolygon(4, P);
|
|
|
|
|
|
|
|
if (Z1 >= -(60+Random(25)))
|
|
|
|
dc.SetPen(GreenPen);
|
|
|
|
else if (Z1 >= -(100+Random(25)))
|
|
|
|
dc.SetPen(MtnPen);
|
|
|
|
else
|
|
|
|
dc.SetPen(SnowPen);
|
|
|
|
|
|
|
|
dc.DrawLine(Ymid/2+X2, Ymid+Z23, Ymid/2+X1, Ymid+Z41);
|
|
|
|
}
|
|
|
|
}
|
1998-12-31 05:54:41 -05:00
|
|
|
}
|
|
|
|
|