Dark Bit Factory & Gravity
PROGRAMMING => C / C++ /C# => Topic started by: combatking0 on June 08, 2012
-
I have completed my first C# program - an updated version of Zero Encrypter (http://zero.barcodebattler.co.uk), which encompasses all of my C# skills to date.
Since then, I have been researching the graphical functions of C#, but I cannot even figure out how to make it draw a line, for example:
Yabasic - line(x1,y1, x2,y2)
ActionScript - moveTo(x1,y1); lineTo(x2,y2);
There also seem to be many implementations of OpenGL for C# - if C# does not support graphical functions natively, which implementation of OpenGL would you suggest is best to use in terms of portability? So far, I have found OpenTK and Tao API. Are there any standard ones?
Or should I switch to C++ until C# is officially supported by the OpenGL group?
-
@CK
There's lots of built in functionality for drawing lines and all sorts of other shapes in C#. Have a look at the Graphics (http://msdn.microsoft.com/en-us/library/ac148eb3) class. which provides lots of methods for such things.
Each control in C# has a CreateGraphics() methods that will return a Graphics object (representing the surface of the control) that you can then draw onto. For playing around, I suggest creating a simple form, chucking a Canvas or PictureBox control on it and then calling CreateGraphics() on the Canvas control and try drawing some stuff.
Be aware that the controls will automatically repainted (redrawn) when they're invalidated (by moving the window off-screen and then on-screen for example, which fires the repainting of the control). If you've done some custom drawing on the surface, this will get wiped when the control is redrawn. Have a look at the Paint event (http://msdn.microsoft.com/en-us/library/system.windows.forms.control.paint.aspx) for some info on overriding this behaviour or drawing your own stuff on top of the control each time it's repainted. That link also shows a basic example at the bottom which will probably prove useful to you. Once you've got the hang of this, you can draw text and images too. You can also do pixel manipulation and other stuff, but I'd leave that until you've got your head round this stuff first.
Hope this helps.
-
Here's a tutorial (http://www.dreamincode.net/forums/topic/67275-the-wonders-of-systemdrawinggraphics/) I found that looks quite handy.
-
It's so obvious now - thanks for pointing me in the right direction, Raizor. K++
(edit)
I've got it working - it's only a green circle, but its graphics. Time for something more complex :)
-
It's so obvious now - thanks for pointing me in the right direction, Raizor. K++
(edit)
I've got it working - it's only a green circle, but its graphics. Time for something more complex :)
Good news, you're welcome. Everything seems obvious in hindsight though. There's a ton of different namespaces and assemblies in .Net, so tracking things down can be a little tricky at first. Feel free to hit me up whenever you need and I'll do my best to assist with this stuff.
On the subject of OpenGL in C#, I still thoroughly recommend the OpenTK stuff. If you want to play with DirectX, then have a look at SlimDX (http://slimdx.org/). It's written by lx/Frequency and looks very nice. I've not used it myself though.
-
actually alx/Frequency is now developing sharpDX on his own. I'm using it for my c# directx stuff. It works really good imho.
-
Oops, I meant SharpDX (http://sharpdx.org/), not SlimDX. I understand they're pretty similar though.
-
I'll play with the built-in graphical functions for the time being, and then experiment with OpenTK and SharpDX.
Excellent advice. Thanks again 8)
-
For anyone else who has the same question, here is my example:
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Windows.Forms;
namespace GFX
{
public partial class MainForm : Form
{
// Initialise objects and variables
System.Drawing.Graphics GFX;
int dispX = 0, dispY = 0;
double angle = 0;
public MainForm()
{
InitializeComponent();
// Set up timer for a framerate of 20fps
Timer timer1 = new Timer();
timer1.Interval = 50;
timer1.Tick += new EventHandler(timer1_Tick);
timer1.Start();
// Create a graphic canvas
GFX = this.CreateGraphics();
}
// Draw stuff when the timer interval has elapsed
private void timer1_Tick(object sender, EventArgs e){
double x, y;
// Clear the canvas
GFX.Clear(Color.Black);
// Rotate an angle, reset it if it goes above PI
angle += 0.01;
if(angle > Math.PI){
angle -= Math.PI * 2;
}
// Translate the angle into X/Y coordinates
x = 200 * Math.Sin(angle);
y = 200 * Math.Cos(angle);
// Convert the coordinates into integers
dispX = Convert.ToInt16(x);
dispY = Convert.ToInt16(y);
Draw a red circle at the coordinates
GFX.FillEllipse(Brushes.Red, new Rectangle(dispX + 304, dispY + 240, 32, 32));
}
}
}
Hopefully it's a simple enough example of movement.
-
So easy to do in C# when come making Graphics :clap:
-
My previous sample suffered from single-buffer induced flickering. This example uses the Graphics object to draw onto a hidden Bitmap, which is then displayed in a picturebox (called "img_opt" in this example):
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Windows.Forms;
using System.Drawing.Drawing2D;
namespace GFX
{
/// <summary>
/// Description of MainForm.
/// </summary>
public partial class MainForm : Form
{
int dispX = 0, dispY = 0;
double angle = 0;
//TextureBrush myBrush = new TextureBrush(new Bitmap(@"C:\p\cSharpProjects\GFX\icon.gif"));
HatchBrush myBrush = new HatchBrush(HatchStyle.DiagonalBrick, Color.FromArgb(255,255,127,0), Color.FromArgb(255,200,200,0));
Bitmap buffer = new Bitmap(640, 512);
Graphics bfr;
public MainForm()
{
//
// The InitializeComponent() call is required for Windows Forms designer support.
//
InitializeComponent();
//
// TODO: Add constructor code after the InitializeComponent() call.
//
Timer timer1 = new Timer();
timer1.Interval = 20;
timer1.Tick += new EventHandler(timer1_Tick);
timer1.Start();
bfr = Graphics.FromImage(buffer);
bfr.TranslateTransform(320, 256);
}
private void timer1_Tick(object sender, EventArgs e){
//Random rndm = new Random();
//byte[] bytes = new byte[4];
//rndm.NextBytes(bytes);
double x, y;
angle += 0.01;
if(angle > Math.PI){
angle -= Math.PI * 2;
}
x = 200 * Math.Sin(angle);
y = 200 * Math.Cos(angle);
dispX = Convert.ToInt16(x);
dispY = Convert.ToInt16(y);
bfr.Clear(Color.FromArgb(255,63,127,255));
Pen blackPen = new Pen(Color.Black, 3);
Point point1 = new Point(-320, -256);
Point point2 = new Point(320, 256);
bfr.DrawLine(blackPen, point1, point2);
point1 = new Point(320, -256);
point2 = new Point(-320, 256);
bfr.DrawLine(blackPen, point1, point2);
bfr.FillEllipse(myBrush, new Rectangle(dispX-32, dispY-32, 64, 64));
this.img_opt.Image = buffer;
}
}
}
The flickering is gone, but I imagine this uses more system resources than its C++ equivalent of simply drawing the pre-rendered bitmap into the window.
I found that the built-in double-buffering settings didn't get rid of the flickering, but this works.
Hopefully C# beginners will be able to use this as the basis for their first graphics functions if they are having difficulties.
-
Nice to see that you put picture on the screen.
You will making own Colisions in no time.....doing C++ would be nightmare just putting image on the screen(unless if you were using SDL or Allegro for C++) and then do manual Collisions!
-
I imagine collisions would be tricky in C# - with Flash8, you work with separate graphical objects and check for overlaps.
With C#, I'm working with 1 graphical object. I could create multiple images, and move them round dynamically (in theory, I'll have to check), but there could still be no way of checking if the images have overlapped using the built-in functions.
This leaves us with checking the sizes of the two objects being collided, and how close they are in code. It's one of the things I must learn if I am going to improve as a coder.
-
I imagine collisions would be tricky in C# - with Flash8, you work with separate graphical objects and check for overlaps.
With C#, I'm working with 1 graphical object. I could create multiple images, and move them round dynamically (in theory, I'll have to check), but there could still be no way of checking if the images have overlapped using the built-in functions.
This leaves us with checking the sizes of the two objects being collided, and how close they are in code. It's one of the things I must learn if I am going to improve as a coder.
One way to do this is have some virtual sprites, each with a rectangle representing its bounds. You draw your virtual sprite image onto your canvas at the sprite position and then use the C# Rectangle class to determine if the sprites bounding rectangles intersect (http://msdn.microsoft.com/en-us/library/y10fyck0.aspx) each other. You could move pictureboxes around to represent the sprites, but handling the drawing yourself will be faster.
-
Thanks Raizor - there's always something new to discover with C#.
-
You're welcome CombatKing. A little off-topic maybe, but this (http://gamedev.stackexchange.com/questions/109/what-c-libraries-can-be-used-to-support-game-development) has some handy links that might interest you. I totally forgot to mention XNA before, it's a nice way of getting at DirectX via C# and from what I've seen of it, very easy to get into.
When I first started playing around with synth stuff, I came across this tutorial (http://www.david-gouveia.com/creating-a-basic-synth-in-xna-part-i/) for writing a basic synth in C# using XNA. It's worth a look at some point if you ever get the synth bug :)
-
I imagine collisions would be tricky in C# - with Flash8, you work with separate graphical objects and check for overlaps.
With C#, I'm working with 1 graphical object. I could create multiple images, and move them round dynamically (in theory, I'll have to check), but there could still be no way of checking if the images have overlapped using the built-in functions.
This leaves us with checking the sizes of the two objects being collided, and how close they are in code. It's one of the things I must learn if I am going to improve as a coder.
I rarely use the hittest functions anymore after I discovered bitmaps for the image processing challenge. Checking a pixel color (to discern between background and walls/objects) is more acurate and faster too!
I'm betting C# has some sort of functions for checking a pixel color too?
-
bitmapName.GetPixel(int X, int Y); (http://msdn.microsoft.com/en-us/library/system.drawing.bitmap.getpixel.aspx) should do the job - checking the pixel colour for collision detection sounds like a good idea.
I'm not sure how it works, so I will research it.
-
Usually your interested in checking for a circle or square collision, so I calculate the offset of the points relative the the object origin once at the start of the program. Then you can loop through the collision points with a pixelcheck after moving the object. If the pixel color != background color you know that point is colliding with something.
For a ball/circle you can even pre-calculate the bounce angle for every point as well to save even more cpu power. Or you can use multiple points to calc the normal of the surface at collision; If you calc the angle of the outer most points that collide and then get the angle perpendicular to that one in the direction of the ball, you got the surface normal which you can use as a bounce angle.
In Flash this is a huge optimisation compared to hittest, you can do lots of collisions more acurately and much faster. Great! :)
-
K++ for the explanation, Kirl.
I have attached my first attempt at 3D graphics in C# - it's a port of the same code I used in the Quaternion tutorial, so it's probably more CPU intensive than the equivalent OpenTK / SharpDX equivalent.
Now to switch to one of these API's.
-
That CS_GFX_TEST ran so fast on my system and you should put FPS on top left corner on how many frames per seconds is :)
The XNA Tutorials of putting 2D Sprites is quite good
http://www.xnadevelopment.com/tutorials.shtml
-
K++ for the explanation, Kirl.
I have attached my first attempt at 3D graphics in C# - it's a port of the same code I used in the Quaternion tutorial, so it's probably more CPU intensive than the equivalent OpenTK / SharpDX equivalent.
Now to switch to one of these API's.
Great stuff CK :)
-
I'll look up how to add an FPS counter.
I've been experimenting with OpenTK - it seems that some of the commands are slightly different to their OpenGL equivalents, but there's plenty of documentation for both.
The tricky bit now is finding information regarding 3D functions in OpenTK - perhaps I'd best check the OpenGL documentation too.
-
My Pixel 3D compo entry was written in C# using OpenTK. The source is posted on the board in this post (http://www.dbfinteractive.com/forum/index.php?topic=5300.msg70932#msg70932). Might be some use to you CK.
-
K++, I'll have a look.
-
Between everybody's help, I have created a spinning Isocidodecahedron in C# using OpenTK and some ported Quaternion code, though OpenTK probably has its own built in Quat functions.
I've still not put a FPS counter on, but here it is:
Use Escape to quit, arrow keys to move on the X & Y coordinates, and Numpad8 and Numpad2 to move on the Z coordinate.
Next I'll look into texture mapping.
-
WOW You make look easy ;D
Here Exerciser for ya
1) Make it smaller
2) Chnage the Colours in Red and White
3) move round and if hit on left hand side or right hand side or top or bottom screen then change the value....Hints DX(Left and right) and DY(Top and bottom)
That should keep ya good coding Exerciser :)
-
Challenge accepted :)
(update)
#1 and #2 are done, just working through the maths for #3.
(update 2)
#3 looks like simple trigonometry - time to make it work :)
-
Hopefully this meets your specifications - the ball is now smaller, is red & white, and performs a "wrap" when it goes off the top, bottom, left or right of the drawing area.
I have also improved the controls a little - before, only 1 key could be used at a time. Now you can use as many keys as you want, and the motion is smoother.
The controls are now -
Left & Right - X Position
Up & Down - Y Position
W & S - Z Position
-
I've been experimenting with using 2 separate textures (a red one and a white one), but it seems to always use the last texture which was defined.
Apparently, this is a common issue for beginners, so I'll look into a fix for it tomorrow, followed by how to apply light sourcing. It should then start to look like some of the better 3D demos seen round here.
-
I've been experimenting with using 2 separate textures (a red one and a white one), but it seems to always use the last texture which was defined.
Apparently, this is a common issue for beginners, so I'll look into a fix for it tomorrow, followed by how to apply light sourcing. It should then start to look like some of the better 3D demos seen round here.
Normally, when you create a texture it gives you a texture ID relating to the new texture. You then bind a particular texture ID when you want to use it. I take it this is OpenTK stuff?
-
Yes, it's OpenTK.
I've worked it out - I was trying to use the same bitmap object to store 2 textures (which is very silly, looking back), and trying to use 2 textures during one GL.Begin session (which wasn't as obvious, but I'll get used to it). I have attached the results below.
I'll next learn how to apply light sourcing to a texture.
-
Hopefully this meets your specifications - the ball is now smaller, is red & white, and performs a "wrap" when it goes off the top, bottom, left or right of the drawing area.
I have also improved the controls a little - before, only 1 key could be used at a time. Now you can use as many keys as you want, and the motion is smoother.
The controls are now -
Left & Right - X Position
Up & Down - Y Position
W & S - Z Position
Excellent Man....You could make own Asteroid Clone Game and go on...you can do it :clap:
-
I could make it a bit like Asteroids, but in 3D - cool idea!
I might change the name though, to avoid copyright infringement.
-
I've managed to get the light sourcing working, but the effect seems to work best if the position of the light source is moving.
When I tidy up my code, I'll make a tutorial.
-
Look good......
You have learn how to make
ploygen( I think)
Texture them
Put light on them
You are progressing :)
Tutorials would be nice on how you made it together :)
-
It crashes for me on win7. :-\
-
The code is a bit of a mess, but it runs OK on XP - I'm not sure what could be causing the crashes. Does it run for a short time?
-
It thinks for a bit and then it says it isn't responding, it gives me the option to look for a solution online or to close the program.
I copied the details of the crash but it's in dutch, not sure if it's of any use.
Probleemhandtekening:
Gebeurtenisnaam van probleem: CLR20r3
Probleemhandtekening 01: gametest.exe
Probleemhandtekening 02: 1.0.4559.38359
Probleemhandtekening 03: 4fe8d5af
Probleemhandtekening 04: GameTest
Probleemhandtekening 05: 1.0.4559.38359
Probleemhandtekening 06: 4fe8d5af
Probleemhandtekening 07: 1
Probleemhandtekening 08: 40c
Probleemhandtekening 09: System.IO.FileNotFoundException
Versie van besturingssysteem: 6.1.7601.2.1.0.768.3
Landinstelling-id: 1043
Aanvullende informatie 1: 0a9e
Aanvullende informatie 2: 0a9e372d3b4ad19135b953a78882e789
Aanvullende informatie 3: 0a9e
Aanvullende informatie 4: 0a9e372d3b4ad19135b953a78882e789
-
You forgot add OpenTK.dll and OpenTK.GLControl.dll to make it work as without these files then of course it would crash
Here the files with OpenTK.dll and OpenTK.GLControl.dll and it work ;D
-
Thanks hotshot!
And good stuff CK :clap:
-
Doh! I'll be sure to include them next time. Not sure why the error messages are in Dutch though. ???
I'll start on the prologue to the tutorial tonight.
-
It was a windows error message and I got the language set to dutch. :)