[SOLVED]Drawing problems with C# and SdlDotNet

Started by
2 comments, last by Rob Loach 18 years, 8 months ago
After about an hour of reading through the numerous examples that come with the SdlDotNet API, SimpleExample and ImageExample to be exact, and I've run into a problem of trying to construct my own little demo of just blitting a background and a little space ship that you can move with the arrow keys. I've got the following source:

using System;
using System.Drawing;

using SdlDotNet;

namespace SDL_Test
{
	public class App
	{
		private bool done;
		private const int height = 480;
		private const int width = 640;
		private int xPos, yPos;
		private Surface screen;

		public App()
		{
			Events.KeyboardDown += new KeyboardEventHandler(this.KeyDown);
			Events.Quit += new QuitEventHandler(this.Quit);
			screen = Video.SetVideoModeWindow(width, height);
			Video.WindowCaption = "C# with SDL.NET";
			done = false;
			xPos = 100;
			yPos = 200;
		}

		private void KeyDown(object sender, KeyboardEventArgs e)
		{
			switch (e.Key)
			{
				case (Key.Escape):
				{
					done = true;
					break;
				}
				case (Key.Q):
				{
					done = true;
					break;
				}
			}
		}

		private void Quit(object sender, QuitEventArgs e)
		{
			done = true;
		}
		
		public void Run()
		{
			Surface background = new Surface("images/bg.bmp");
			Surface spaceship = new Surface("images/ship.jpg");
			spaceship.TransparentColor = System.Drawing.Color.Magenta;  // R = 255 G = 0 B = 255
			spaceship.Transparent = true;

			while (done == false)
			{
				while(Events.Poll())
				{
				}
				screen.Blit(background, new Rectangle(new Point(0,0), background.Size));
				screen.Blit(spaceship, new Rectangle(new Point(xPos,yPos), spaceship.Size));
				screen.Flip();
			}
		}

		public static void Main()
		{
			App mainApp = new App();
			mainApp.Run();
		}
	}
}


And here are the images I'm using: bg.bmp ship.jpg The background blits and so does the ship but the magenta colored background is still visible save for some parts that are somehow missing. So basically its a space ship with magenta colored blots around it. I'm still pretty new to coding with SdlDotNet, it's a little hard to find a tutorial. And a full OOP approach is a lot different than procedural or using a custom wrapper class with C++. New language, new API, same old problems. [Edited by - Ekim_Gram on July 29, 2005 11:50:06 AM]
Advertisement
You probably shouldn't be using a jpg for your ship. Jpegs are lossy compression and there is no guarentee that the color before converting to jpeg is the same color as after. Probably what is happening is that some spots of your picture are still the correct color and disappear, but the rest have been shifter slightly to another color and stay. Try using a bmp (and make sure all of the background is the right color), and then move to a lossless format like gif.
Turring Machines are better than C++ any day ^_~
Looks like you were right. Thanks a bunch!
You can safely use a PNG which will dramatically decrease the size of the image. It looks pretty good, keep it up.

Oh, and the current contest's elements are Pirates, Zombies, Ninjas and Robots [smile].
Rob Loach [Website] [Projects] [Contact]

This topic is closed to new replies.

Advertisement