Click here to Skip to main content
15,886,799 members
Home / Discussions / Managed C++/CLI
   

Managed C++/CLI

 
Questionissue while loading MFC extension dll dynamically Pin
ptr_Electron24-Mar-11 23:37
ptr_Electron24-Mar-11 23:37 
AnswerRe: issue while loading MFC extension dll dynamically Pin
Richard MacCutchan25-Mar-11 0:02
mveRichard MacCutchan25-Mar-11 0:02 
GeneralRe: issue while loading MFC extension dll dynamically Pin
ptr_Electron25-Mar-11 0:21
ptr_Electron25-Mar-11 0:21 
GeneralRe: issue while loading MFC extension dll dynamically Pin
Richard MacCutchan25-Mar-11 1:03
mveRichard MacCutchan25-Mar-11 1:03 
GeneralRe: issue while loading MFC extension dll dynamically Pin
ptr_Electron25-Mar-11 2:50
ptr_Electron25-Mar-11 2:50 
GeneralRe: issue while loading MFC extension dll dynamically Pin
Richard MacCutchan25-Mar-11 4:24
mveRichard MacCutchan25-Mar-11 4:24 
QuestionInstances of an object Pin
Cyclone_S19-Mar-11 15:47
Cyclone_S19-Mar-11 15:47 
AnswerRe: Instances of an object Pin
John Schroedl19-Mar-11 17:51
professionalJohn Schroedl19-Mar-11 17:51 
I think your real slowdown comes from having a timer in each individual missiles. They keep ticking even after they've left the screen. And you never stop the timer and unsubscribe the tick event in each missile. This keeps the timers and missiles all alive all the time.

I've tweaked your code some and removed those separate timers and just update all the missiles from the Form's timer. Also, I remove the missiles which have gone off-screen so those won't be updated after they're gone . This should allow for more missiles and better perf/memory usage.

There are clearly other improvements you could do such as having the logic to determine whether a missile is off-screen determined by the missile class.

I hope this helps you get going...

John

using namespace System;

using namespace System::ComponentModel;
using namespace System::Collections;
using namespace System::Windows::Forms;
using namespace System::Data;
using namespace System::Drawing;

using namespace System::Collections::Generic;

public ref class object{	
	public:			
		PictureBox^ Box3;						
		
		object( Form ^ form ) // Class Constructor.			
		{				
			Box3 = gcnew PictureBox();				
			Box3->Left = 150;				
			Box3->Top = 50;				
			Box3->Width = 100;				
			Box3->Height = 100;				
			Box3->BackColor = System::Drawing::Color::Red;				
			form->Controls->Add(Box3);			
		}
};

public ref class missile{	
	public:			
		PictureBox^ Box1;			
		
		missile( Form ^ form ) // Class Constructor.			
		{				
			Box1 = gcnew PictureBox();				
			Box1->Left = 150;				
			Box1->Top = 240;				
			Box1->Width = 10;				
			Box1->Height = 10;				
			Box1->BackColor = System::Drawing::Color::Blue;				
			form->Controls->Add(Box1);				
		}		
		
		void update()
		{
			Box1->Top--;
		}
};

public ref class Form1 : public System::Windows::Forms::Form
{
public:		
	PictureBox^ Box2;			
	object^ o1;			
	Timer^ Timer3;			
	bool x;			
	List<missile^>^ Missiles;
		
	Form1() // Class constructor.			
	{		
		Missiles = gcnew List<missile^>();

		x=false;				
		Timer3 = gcnew Timer();				
		Timer3->Interval = 1;				
		Timer3->Start();				
		Box2 = gcnew PictureBox();				
		Box2->BackColor = Color::Blue;				
		Box2->Top = 240;				
		Box2->Left = (this->Width / 2) - 40;				
		Box2->Width = 40;				
		Box2->Height = 10;				
		this->Controls->Add(Box2);				
		this->KeyDown += gcnew System::Windows::Forms::KeyEventHandler(this, &Form1::Form_KeyDown);				
		Timer3->Tick += gcnew System::EventHandler(this, &Form1::timer3_Tick);			
	}			
		
	System::Void Form_KeyDown(System::Object^ sender, System::Windows::Forms::KeyEventArgs^ e)			
	{					
		if(e->KeyCode == Keys::Up)
		{
			x=true;
			missile^ m = gcnew missile(this);
			m->Box1->Left = Box2->Left + (Box2->Width / 2) - 10;
			Missiles->Add(m);
		}				
			
		if(e->KeyCode == Keys::Left) {Box2->Left -= 4; }							
		if(e->KeyCode == Keys::Right){Box2->Left += 4; }				
		if(e->KeyCode == Keys::Down) {o1 = gcnew object(this); }			
	}			
		
	bool IsMissileOffScreen(missile^ m)
	{
		return m->Box1->Bottom < 0;
	};

	System::Void timer3_Tick(System::Object^  sender, System::EventArgs^  e)			
	{		
		for each (missile^ m in Missiles)
		{
			m->update();
				if(x==true && o1 != nullptr)				
			{					
				if (m->Box1->Top == o1->Box3->Bottom)
				{
					m->Box1->BackColor = Color::Green;
				}				
			}	
		}

		Missiles->RemoveAll(gcnew Predicate<missile^>(this, &Form1::IsMissileOffScreen));
	}			
		
	bool isColliding(PictureBox^ box1, PictureBox^ box3)			
	{	
		Rectangle r2 = o1->Box3->Bounds;				
		for each (missile^ m in Missiles)
		{
			Rectangle r1 = m->Box1->Bounds;				
			if (r1.IntersectsWith(r2)) //return true if they collide		
				return true;
		}
		return false;
	}			
};

GeneralRe: Instances of an object Pin
John Schroedl19-Mar-11 18:01
professionalJohn Schroedl19-Mar-11 18:01 
GeneralRe: Instances of an object Pin
Cyclone_S20-Mar-11 9:56
Cyclone_S20-Mar-11 9:56 
AnswerRe: Instances of an object Pin
Luc Pattyn20-Mar-11 10:47
sitebuilderLuc Pattyn20-Mar-11 10:47 
GeneralRe: Instances of an object Pin
Cyclone_S20-Mar-11 14:47
Cyclone_S20-Mar-11 14:47 
AnswerRe: Instances of an object Pin
Luc Pattyn20-Mar-11 14:50
sitebuilderLuc Pattyn20-Mar-11 14:50 
GeneralRe: Instances of an object Pin
Cyclone_S20-Mar-11 15:30
Cyclone_S20-Mar-11 15:30 
GeneralRe: Instances of an object Pin
John Schroedl21-Mar-11 4:02
professionalJohn Schroedl21-Mar-11 4:02 
GeneralRe: Instances of an object Pin
Cyclone_S21-Mar-11 15:44
Cyclone_S21-Mar-11 15:44 
GeneralRe: Instances of an object Pin
Cyclone_S23-Mar-11 13:21
Cyclone_S23-Mar-11 13:21 
AnswerRe: Instances of an object Pin
Luc Pattyn23-Mar-11 16:01
sitebuilderLuc Pattyn23-Mar-11 16:01 
GeneralRe: Instances of an object Pin
John Schroedl23-Mar-11 18:22
professionalJohn Schroedl23-Mar-11 18:22 
GeneralRe: Instances of an object Pin
Cyclone_S25-Mar-11 14:31
Cyclone_S25-Mar-11 14:31 
GeneralRe: Instances of an object Pin
Cyclone_S26-Mar-11 9:38
Cyclone_S26-Mar-11 9:38 
Questionreturn 2 HWND Pin
Masternoob00718-Mar-11 1:19
Masternoob00718-Mar-11 1:19 
AnswerRe: return 2 HWND Pin
Richard MacCutchan18-Mar-11 2:50
mveRichard MacCutchan18-Mar-11 2:50 
Question[C++/CLI] Progressbar doesn't work [modified] Pin
Masternoob00716-Mar-11 2:08
Masternoob00716-Mar-11 2:08 
AnswerRe: [C++/CLI] Progressbar doesn't work Pin
Luc Pattyn16-Mar-11 2:27
sitebuilderLuc Pattyn16-Mar-11 2:27 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.