Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

barakus

c# directdraw System.NullReferenceException

This topic is 5201 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hey im trying to add animation to some code from a C3 directdraw tutorial. However, i get an exception from the BitmapObject class when i try to run it. Below i have posted my BitmapObject class and my Game class, which is where my GameLoop() is located. Any help would be greatly appreciated! public enum BitmapType { SOLID = 0, TRANSPARENT = 1 } public class BitmapObject { protected Bitmap sourceBitmap; protected BitmapType objectType; protected int frameHeight; protected int frameWidth; protected int nFrames; protected int currentframe; protected Rectangle[] frames; protected Device targetDevice; protected Surface bitmapSurface; protected bool bVisible = true; protected double dXPos = 0.0f; protected double dYPos = 0.0f; protected double dLastXPos = 0.0f; protected double dLastYPos = 0.0f; protected Rectangle rectPosition = Rectangle.Empty; protected SurfaceDescription desc; public double XPosition { get { return dXPos; } set { // save last position dLastXPos = dXPos; // save value dXPos = value; // set integer position rectPosition = new Rectangle(new Point(Convert.ToInt32(dXPos), rectPosition.Y), new Size(this.frameWidth, this.frameHeight)); } } /// /// The current vertical position /// public double YPosition { get { return dYPos; } set { // save last position dLastYPos = dYPos; // save value dYPos = value; // set integer position rectPosition = new Rectangle(new Point(rectPosition.X, Convert.ToInt32(dYPos)), new Size(this.frameWidth, this.frameHeight)); } } public bool Visible { get { return bVisible; } set { bVisible = value; } } public double LastXPosition { get { return dLastXPos; } } /// /// Last vertical position. Read only. /// public double LastYPosition { get { return dLastYPos; } } public BitmapObject(Bitmap SourceBitmap, BitmapType ObjectType, int FrameWidth, int FrameHeight, int NFrames, Device TargetDevice) { sourceBitmap = SourceBitmap; objectType = ObjectType; frameHeight = FrameHeight; frameWidth = FrameWidth; nFrames = NFrames; targetDevice = TargetDevice; InitSurfaceDescription(); InitSurface(); } public BitmapObject(string Resource, BitmapType ObjectType, int FrameWidth, int FrameHeight, int NFrames, Device TargetDevice) { sourceBitmap = new Bitmap(GetType().Module.Assembly.GetManifestResourceStream(Resource)); objectType = ObjectType; frameHeight = FrameHeight; frameWidth = FrameWidth; nFrames = NFrames; targetDevice = TargetDevice; InitSurfaceDescription(); InitSurface(); } protected void InitSurfaceDescription() { SurfaceDescription desc = new SurfaceDescription(); desc.SurfaceCaps.OffScreenPlain = true; desc.Width = frameWidth; desc.Height = frameHeight; } protected void InitSurface() { this.bitmapSurface = new Surface(sourceBitmap, desc, targetDevice); switch(this.objectType) { case BitmapType.TRANSPARENT: //Create a new colour key and choose colour black ColorKey tempKey = new ColorKey(); tempKey.ColorSpaceHighValue = 0; tempKey.ColorSpaceLowValue = 0; // Assign the color key to the surface bitmapSurface.SetColorKey(ColorKeyFlags.SourceDraw, tempKey); break; } for(int i=0; i< nFrames; i++) { Point pt = new Point(frameWidth * i, 0); frames = new Rectangle(pt, new Size(frameWidth, frameHeight)); } if(currentframe >= nFrames - 1) { currentframe = 0; } else { currentframe++; } } public void DrawBitmap(Surface TargetSurface, float TargetXPos, float TargetYPos) { this.XPosition = TargetXPos; this.YPosition = TargetYPos; DrawBitmap(TargetSurface); } public void DrawBitmap(Surface TargetSurface) { if(!this.bVisible) { return; } if( this.objectType == BitmapType.TRANSPARENT) { TargetSurface.DrawFast(rectPosition.X, rectPosition.Y, bitmapSurface, frames[currentframe], DrawFastFlags.SourceColorKey | DrawFastFlags.Wait); } else { TargetSurface.DrawFast(rectPosition.X, rectPosition.Y, bitmapSurface, frames[currentframe], DrawFastFlags.NoColorKey | DrawFastFlags.Wait); } } public void RestoreSurface() { // create surface description and surface this.InitSurfaceDescription(); this.InitSurface(); } } } public enum GameStates { Initialized = 0, ExitRequested = 1, Exit = 2, Ready = 3 } public class Game { protected GameStates gameState; protected Control target; protected Graphics graphics; protected Input input; protected double dLoopDuration; //title screem bitmap protected BitmapObject boTitleScreen; //test area bitmap protected BitmapObject boTestArea; public Game(Control RenderTarget) { this.target = RenderTarget; this.target.GotFocus += new System.EventHandler(this.Restore); // Create a new Graphics handler this.graphics = new Graphics(this.target); // Create a new Input handler this.input = new Input(this.target); //title bitmap this.boTitleScreen = new BitmapObject("CastlevaniaAlpha.NewFolder1.title.jpg", BitmapType.SOLID, 640, 480, 1, graphics.getDisplay); try { DXTimer.Init(); } catch(Exception ex) { MessageBox.Show("Error While Initializing: "+ ex.Message); return; } //Set the gamestate to initialized this.gameState = GameStates.Initialized; //Begin game loop this.GameLoop(); } protected void Restore(object sender, System.EventArgs e) { this.graphics = new Graphics(this.target); this.boTitleScreen.RestoreSurface(); } protected void GameLoop() { //to check the elapsed time dLoopDuration = 0.0; //Start timer DXTimer.Start(); while(target.Created) { if(!target.Focused) { System.Threading.Thread.Sleep(100); Application.DoEvents(); //Get elapsed time(to reset the timer-> "Freeze" gameplay. DXTimer.GetElapsedMilliseconds(); continue; } dLoopDuration += DXTimer.GetElapsedMilliseconds(); if(dLoopDuration > 10) { this.ProcessInput(); // if gameState == GameState.Exit, quit. if(gameState == GameStates.Exit) { return; } if (gameState == GameStates.Initialized) { this.boTitleScreen.DrawBitmap(this.graphics.getSecondarySurface); } if (gameState == GameStates.Ready) { this.boTestArea.DrawBitmap(this.graphics.getSecondarySurface); } graphics.Flip(); dLoopDuration = 0.0; } Application.DoEvents(); } } protected void ProcessInput() { KeyboardState state; state = this.input.GetKeyboardState(); if(state != null) { if(state[Key.Escape]) { gameState = GameStates.Exit; } if(state[Key.Space]) { if( gameState == GameStates.Initialized) { gameState = GameStates.Ready; } } } } } }

Share this post


Link to post
Share on other sites
Advertisement

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!