Click here to Skip to main content
15,887,175 members
Home / Discussions / C#
   

C#

 
GeneralOn Virtual And Override Pin
GetOn&GetGoing3-Feb-04 17:13
GetOn&GetGoing3-Feb-04 17:13 
GeneralRe: On Virtual And Override Pin
TigerNinja_3-Feb-04 17:38
TigerNinja_3-Feb-04 17:38 
GeneralRe: On Virtual And Override Pin
Heath Stewart3-Feb-04 20:18
protectorHeath Stewart3-Feb-04 20:18 
QuestionScrollBar bug? Pin
Meysam Mahfouzi3-Feb-04 16:58
Meysam Mahfouzi3-Feb-04 16:58 
Generalsome clarification on GDI+ vs. DIrectDraw Pin
SIDDHARTH_JAIN3-Feb-04 16:11
SIDDHARTH_JAIN3-Feb-04 16:11 
GeneralRe: some clarification on GDI+ vs. DIrectDraw Pin
Christian Graus3-Feb-04 17:44
protectorChristian Graus3-Feb-04 17:44 
GeneralRe: some clarification on GDI+ vs. DIrectDraw Pin
Michael Blome3-Feb-04 18:55
Michael Blome3-Feb-04 18:55 
GeneralRe: some clarification on GDI+ vs. DIrectDraw Pin
SIDDHARTH_JAIN4-Feb-04 11:36
SIDDHARTH_JAIN4-Feb-04 11:36 
HI,

I am using DX9 and I think DDraw does not end with DX7, its there in DX9 also. ANyway I created two projects - one in GDI+ another in DDraw9 and I find ddraw is much much slower than GDI+ - obviously it seems I am making a mistake somewhere but where? Please see the DisplayFrame method in my DDraw code.

Regarding SetDisplayMode:
is the fourth parameter the graphics card refresh rate you want to set and its specified in Hz?
I have no idea what the fifth parameter means.

This is my GDI+ code (I would be happy to send you the entire project for GDI+ and Ddraw if you want) - the OnPaint method is of interest:

using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;

namespace enigma1
{
///
/// Summary description for Form1.
///

public class Form1 : System.Windows.Forms.Form
{
///
/// Required designer variable.
///

private System.ComponentModel.Container components = null;

public Form1()
{
//
// Required for Windows Form Designer support
//
InitializeComponent();

//
// TODO: Add any constructor code after InitializeComponent call
//
}

///
/// Clean up any resources being used.
///

protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}

protected override void OnPaint(PaintEventArgs e)
{
Graphics dc = e.Graphics;

Double area = Math.PI*(m_RadiusOuter*m_RadiusOuter
- m_RadiusInner*m_RadiusInner);
Int32 n = (Int32) ((m_DotDensity / (128*128)) * area);

this.WindowState = FormWindowState.Maximized;


this.BackColor = Color.White;
Int32 centery = this.Bottom / 2;
Int32 centerx = this.Right / 2;
dc.DrawEllipse(m_PenInner, centerx-m_RadiusInner,
centery-m_RadiusInner, 2*m_RadiusInner, 2*m_RadiusInner);
dc.DrawEllipse(m_PenOuter, centerx-m_RadiusOuter,
centery-m_RadiusOuter, 2*m_RadiusOuter, 2*m_RadiusOuter);
// draw n random dots
for(Int32 ctr = 0; ctr < n; ctr++)
{
Double theta = (Double) m_Rnd.Next()/Int32.MaxValue * 2*Math.PI;
Double r = m_Rnd.Next(m_RadiusInner+1, m_RadiusOuter-1);
Double cx = centerx + r*Math.Cos(theta);
Double cy = centery - r*Math.Sin(theta);
dc.DrawEllipse(m_DotPen, (float) cx - m_DotSize,
(float) cy - m_DotSize,
(float) 2*m_DotSize, (float) 2*m_DotSize);
dc.FillEllipse(m_DotBrush, (float) cx-m_DotSize,
(float) cy - m_DotSize,
(float) 2*m_DotSize, (float) 2*m_DotSize);
}
// pause for m_t1 ticks
System.Threading.Thread.Sleep(100);
// for(Int32 ctr = 0; ctr < 0xafffff; ctr++);
// blank the screen for m_t2 ticks

this.BackColor = Color.White;
System.Threading.Thread.Sleep(100);
// for(Int32 ctr = 0; ctr < 0xafffff; ctr++);
this.Invalidate(); // redraw again

}
#region Windows Form Designer generated code
///
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
///

private void InitializeComponent()
{
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(692, 466);
this.Name = "Form1";
this.Text = "Form1";
this.KeyUp += new System.Windows.Forms.KeyEventHandler(this.Form1_KeyUp);
}
#endregion

///
/// The main entry point for the application.
///

[STAThread]
static void Main()
{
Form1 f1 = new Form1();
f1.GetInputs();
// following displays the screen resolution
// MessageBox.Show(Screen.PrimaryScreen.Bounds.Right.ToString() + " x " + Screen.PrimaryScreen.Bounds.Bottom.ToString());
//
Application.Run(f1);

}




public void GetInputs()
{
m_DotSize = 2;
m_t1 = 10;
m_t2 = 10;
m_PenInner = new Pen(Color.Black);
m_PenOuter = new Pen(Color.Blue);
m_RadiusInner = 64;
m_RadiusOuter = 128;
m_DotDensity = 20;
m_Rnd = new Random();
m_DotPen = new Pen(Color.Black);
m_DotBrush = new SolidBrush(Color.Black);
}

private Int32 m_DotSize;
private Int32 m_t1;
private Int32 m_t2;
private Pen m_PenInner;
private Pen m_PenOuter;
private Int32 m_RadiusInner;
private Int32 m_RadiusOuter;
private Double m_DotDensity;
private Random m_Rnd;
private Pen m_DotPen;
private Brush m_DotBrush;

///
/// Processes key up events.
///

private void Form1_KeyUp(object sender, System.Windows.Forms.KeyEventArgs e)
{
// If the user presses the Escape key, the app needs to exit.
if (Keys.Escape == e.KeyCode)
Close();
}
}
}

THIS IS MY DDRAW CODE (the DisplayFrame method is of interest):

//-----------------------------------------------------------------------------
// File: AnimatePalette.cs
//
// Desc: AnimatePalette demonstrates DirectDraw palette animation when in full-screen
// on a palettized surface.
//
// Copyright (c) Microsoft Corporation. All rights reserved.
//-----------------------------------------------------------------------------
using System;
using System.Windows.Forms;
using System.Drawing;
using Microsoft.DirectX;
using Microsoft.DirectX.DirectDraw;

namespace AnimatePalette
{

public class AnimatePalette : System.Windows.Forms.Form
{
// Declare the class scope variables here.
public void GetInputs()
{
m_DotSize = 2;
m_t1 = 10;
m_t2 = 10;
m_PenInner = new Pen(Color.Black);
m_PenOuter = new Pen(Color.Blue);
m_RadiusInner = 64;
m_RadiusOuter = 128;
m_DotDensity = 20;
m_Rnd = new Random();
m_DotPen = new Pen(Color.Black);
m_DotBrush = new SolidBrush(Color.Black);
}

private Int32 m_DotSize;
private Int32 m_t1;
private Int32 m_t2;
private Pen m_PenInner;
private Pen m_PenOuter;
private Int32 m_RadiusInner;
private Int32 m_RadiusOuter;
private Double m_DotDensity;
private Random m_Rnd;
private Pen m_DotPen;
private Brush m_DotBrush;

///
/// Required designer variable.
///

private System.ComponentModel.Container components = null;

const int indexBlack = 144; // Index of the palette entry that contains black.
const int indexWhite = 0; // Index of the palette entry that contains white.


const int screenWidth = 640; // Screen width.
const int screenHeight = 480; // Screen height.
Device displayDevice = null; // The Direct Draw Device.
Surface front = null; // The front surface.
Surface back = null; // The back surface.


Palette pal = null; // The palette the front surface will use.
int tickLast = 0; // Holds the value of the last call to GetTick.
bool restoreNeeded = false;// Flag to determine the app needs to restore the surfaces.

PaletteEntry[] pe = new PaletteEntry[256]; // Palette entry array.



///
/// Entry point for the application.
///

public static void Main()
{
AnimatePalette app = new AnimatePalette();
Application.Exit();
}

///
/// Constructor for the main class.
///

public AnimatePalette()
{
//
// Required for Windows Form Designer support.
//
InitializeComponent();

// Make sure to use the arrow cursor.
Cursor = Cursors.Arrow;

// Create a new DrawDevice.
InitializeDirectDraw();

this.GetInputs();

// Keep looping until app quits.
while (Created)
{
ProcessNextFrame(); // Process and draw the next frame.
Application.DoEvents(); // Make sure the app has time to process messages.
}
}
///
/// Restore all the surfaces, and redraw the sprite surfaces.
///

void RestoreSurfaces()
{
/*
SurfaceDescription description = new SurfaceDescription();
*/
displayDevice.RestoreAllSurfaces();


// We need to release and re-load, and set the palette again to
// redraw the bitmap on the surface. Otherwise, GDI will not
// draw the bitmap on the surface with the correct palette.
/*
pal.Dispose();
pal = null;

front.Palette = pal;
*/
// Release and re-load the sprite bitmap.
/*
surfaceLogo.Dispose();
surfaceLogo = new Surface(bitmapFileName, description, displayDevice);
ColorKey ck = new ColorKey();

surfaceLogo.SetColorKey(ColorKeyFlags.SourceDraw, ck);
*/
return;
}


///
/// Move the sprites, blt them to the back buffer, then
/// flips the back buffer to the primary buffer
///

private void ProcessNextFrame()
{
// Figure how much time has passed since the last time.
int CurrTick = Environment.TickCount;
int TickDiff = CurrTick - tickLast;

// Don't update if no time has passed.
if (0 == TickDiff)
return;

tickLast = CurrTick;


//Draw the sprites and text to the screen.
DisplayFrame();
}

private void DisplayFrame()
{
if (null == front)
return;

if (false == displayDevice.TestCooperativeLevel())
{
restoreNeeded = true;
return;
}

if (true == restoreNeeded)
{
restoreNeeded = false;
// The surfaces were lost so restore them .
RestoreSurfaces();
}

// Fill the back buffer with black.
// back.ColorFill(indexBlack);
back.ColorFill(System.Drawing.Color.White);

Double area = Math.PI*(m_RadiusOuter*m_RadiusOuter
- m_RadiusInner*m_RadiusInner);
Int32 n = (Int32) ((m_DotDensity / (128*128)) * area);





Int32 centery = this.Bottom / 2;
Int32 centerx = this.Right / 2;
back.ForeColor = Color.Black;
back.FillStyle = 1;
back.DrawCircle(centerx, centery, m_RadiusInner);
back.DrawCircle(centerx, centery, m_RadiusOuter);

back.FillStyle = 0;
// draw n random dots
for(Int32 ctr = 0; ctr < n; ctr++)
{
Double theta = (Double) m_Rnd.Next()/Int32.MaxValue * 2*Math.PI;
Double r = m_Rnd.Next(m_RadiusInner+1, m_RadiusOuter-1);
Double cx = centerx + r*Math.Cos(theta);
Double cy = centery - r*Math.Sin(theta);
back.DrawCircle((Int32) cx, (Int32) cy, (Int32) m_DotSize);
/*
dc.FillEllipse(m_DotBrush, (float) cx-m_DotSize,
(float) cy - m_DotSize,
(float) 2*m_DotSize, (float) 2*m_DotSize);
*/
}
// We are in fullscreen mode, so perform a flip.
front.Flip(back, FlipFlags.DoNotWait);
// pause for m_t1 ticks
System.Threading.Thread.Sleep(20);
// for(Int32 ctr = 0; ctr < 0xafffff; ctr++);
// blank the screen for m_t2 ticks

back.ColorFill(Color.White);
// We are in fullscreen mode, so perform a flip.
front.Flip(back, FlipFlags.DoNotWait);
System.Threading.Thread.Sleep(20);



}

///
/// Initializes DirectDraw and all the surfaces to be used.
///

private void InitializeDirectDraw()
{
SurfaceDescription description = new SurfaceDescription(); // Describes a surface.

displayDevice = new Device(); // Create a new DirectDrawDevice.
displayDevice.SetCooperativeLevel(this, CooperativeLevelFlags.FullscreenExclusive); // Set the cooperative level.

try
{
displayDevice.SetDisplayMode(screenWidth, screenHeight, 8, 0, false); // Set the display mode width and height, and 8 bit color depth.
}
catch(UnsupportedException)
{
MessageBox.Show("Device doesn't support required mode. Sample will now exit.", "UnsupportedException caught");
Close();
return;
}

description.SurfaceCaps.PrimarySurface = description.SurfaceCaps.Flip = description.SurfaceCaps.Complex = true;
description.BackBufferCount = 1; // Create 1 backbuffer.

front = new Surface(description, displayDevice); // Create the surface using the description above.

SurfaceCaps caps = new SurfaceCaps();
caps.BackBuffer = true; // Caps of the surface.
back = front.GetAttachedSurface(caps); // Get the attached surface that matches the caps, which will be the backbuffer.
front.ColorFill(Color.White);

/*
pal = new Palette(displayDevice, bitmapFileName); // Create a new palette, using the bitmap file.
pe = pal.GetEntries(0, 256); // Store the palette entries.
front.Palette = pal; // The front surface will use this palette.
*/



}

///
/// Clean up any resources being used.
///

protected override void Dispose(bool disposing)
{
if (disposing)
{
displayDevice.RestoreDisplayMode(); // Restore the display mode to what it was.
displayDevice.SetCooperativeLevel(this, CooperativeLevelFlags.Normal); // Set the cooperative level back to windowed.

if (components != null)
{
components.Dispose();
}
}
base.Dispose(disposing);
}

#region Windows Form Designer generated code
///
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
///

private void InitializeComponent()
{
//
// AnimatePalette
//
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(208, 69);
this.ForeColor = System.Drawing.Color.Black;
this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None;
this.KeyPreview = true;
this.Name = "AnimatePalette";
this.Text = "AnimatePalette";
this.KeyUp += new System.Windows.Forms.KeyEventHandler(this.AnimatePalette_KeyUp);

}
#endregion

///
/// Processes key up events.
///

private void AnimatePalette_KeyUp(object sender, System.Windows.Forms.KeyEventArgs e)
{
// If the user presses the Escape key, the app needs to exit.
if (Keys.Escape == e.KeyCode)
Close();
}
}
}
GeneralPrepend to a file in C# Pin
pankajdaga3-Feb-04 13:27
pankajdaga3-Feb-04 13:27 
GeneralRe: Prepend to a file in C# Pin
John Kuhn3-Feb-04 17:29
John Kuhn3-Feb-04 17:29 
Generalsend an image in picturebox Pin
abido3-Feb-04 12:34
abido3-Feb-04 12:34 
GeneralRe: send an image in picturebox Pin
Heath Stewart3-Feb-04 20:13
protectorHeath Stewart3-Feb-04 20:13 
GeneralRe: send an image in picturebox Pin
John Kuhn3-Feb-04 21:02
John Kuhn3-Feb-04 21:02 
QuestionHow to add a new method that has both external and internal name into a ActiveX? Pin
lchuang3-Feb-04 12:28
susslchuang3-Feb-04 12:28 
AnswerRe: How to add a new method that has both external and internal name into a ActiveX? Pin
Heath Stewart3-Feb-04 20:11
protectorHeath Stewart3-Feb-04 20:11 
QuestionFlash swfs in c#? Pin
visiontec3-Feb-04 12:03
visiontec3-Feb-04 12:03 
AnswerRe: Flash swfs in c#? Pin
Meysam Mahfouzi3-Feb-04 17:07
Meysam Mahfouzi3-Feb-04 17:07 
GeneralRe: Flash swfs in c#? Pin
krishna_goluguri8-Jul-10 0:16
krishna_goluguri8-Jul-10 0:16 
Questiondate format converter? Pin
visiontec3-Feb-04 11:58
visiontec3-Feb-04 11:58 
AnswerRe: date format converter? Pin
Dmitriy Kostovetskiy3-Feb-04 15:59
Dmitriy Kostovetskiy3-Feb-04 15:59 
AnswerRe: date format converter? Pin
Heath Stewart3-Feb-04 20:07
protectorHeath Stewart3-Feb-04 20:07 
GeneralHide Start Button Pin
johnstacey3-Feb-04 11:53
johnstacey3-Feb-04 11:53 
GeneralRe: Hide Start Button Pin
Heath Stewart3-Feb-04 19:58
protectorHeath Stewart3-Feb-04 19:58 
GeneralRe: Hide Start Button Pin
johnstacey4-Feb-04 4:59
johnstacey4-Feb-04 4:59 
GeneralRe: Hide Start Button Pin
Heath Stewart4-Feb-04 5:04
protectorHeath Stewart4-Feb-04 5:04 

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.