Page 1 of 1

EDITT: MarauderIIC I need help, YOURS because...

Posted: Tue Jun 16, 2009 11:02 pm
by MadPumpkin
MarauderIIC I need help, YOURS because I've already seen how you do it xD

well to make a long story short... i need help in C#!!!
im making a level editor called orange and i don't know how to make a portion for rendering all of the tiles
for example...
i have the program, their is 2 "docked navigator" with buttons and such, 1 docked on top and 1 on right
the rest of it is... well thats where i need your help... i want to make the rest of it render my tiles and that will be the editing section as well...obviously...

so would you like to let me know how?? like... do i need a panel in that area? or some other form?? or just raw programming mad skillz? im not very advanced w/ C# ima C++ typa person

EDIT: By the way i don't ONLY want Marauder's opinion! any opinion or way of doing so would be pretty sweet

Re: EDITT: MarauderIIC I need help, YOURS because...

Posted: Wed Jun 17, 2009 11:05 am
by thejahooli
I'm needing help with this too.
Preferably C# forms but even if anyone know how to do it in C++ forms I will be grateful.

Re: EDITT: MarauderIIC I need help, YOURS because...

Posted: Wed Jun 17, 2009 12:11 pm
by RyanPridgeon
I believe a good easy way to do it would to be using a picture box.

This is basically a bitmap on the form that you can draw onto :P

I haven't used it in C#, but I used it on VB.net, and I've used other parts of forms on C# and it all seems similar.

Re: EDITT: MarauderIIC I need help, YOURS because...

Posted: Wed Jun 17, 2009 12:48 pm
by MarauderIIC
RyanPridgeon wrote:I believe a good easy way to do it would to be using a picture box.
I used this approach but ran into problems; possibly because I was making the bitmap the size of the map in order to use the autoscroll property of the panel that the picture box was contained in. Now I use a hardware-accelerated approach for the viewport, but I still use this bitmap approach for the tile selectors on the right hand side.

For the viewport, I use OpenTK ( http://www.opentk.com ) which is a free toolkit. It provides a user control called GLControl that you can place on a form, and that area is rendered by OpenGL. I have a user control that inherits from GLControl, called Viewport, which is placed on the main form (actually, it's placed on my tabcontrol so that I can eventually edit multiple maps at once, but that's a different story). The map is rendered in the Viewport control. To render the map, I use the standard OpenGL approach that is used whenever you render an image from a tile sheet (loop through map data which tells us which tile should be drawn, get this tile from the correct area on the tile sheet using a set of stored rectangles, draw this tile at the appropriate area on the viewport). The scrollbars actually have maximums defined by the size of the map minus the size of the viewport, and they scroll one tile at a time. They cause the Viewport to do a glTranslate, I think. So the viewport is no bigger than what you see, but the scrollbars scroll the image that the viewport displays. This approach could probably be used with PictureBoxes to decent effect, too, but redrawing would be slower.

Does that answer your question, or do you need more?

Re: EDITT: MarauderIIC I need help, YOURS because...

Posted: Wed Jun 17, 2009 1:07 pm
by thejahooli
Thanks.
I'll probably not use it for now because I haven't done any OpenGL so I'll probably make my own simple GUI using SDL in C++ for my level editor.

Re: EDITT: MarauderIIC I need help, YOURS because...

Posted: Wed Jun 17, 2009 1:28 pm
by RyanPridgeon
MarauderIIC wrote:I used this approach but ran into problems;
Just out of interest, what problems did you run into?

Re: EDITT: MarauderIIC I need help, YOURS because...

Posted: Wed Jun 17, 2009 2:12 pm
by MadPumpkin
thanks to you all for your very quit replies! and thanks marauder for your link to OpenTK im going to start learning it once i come back home from my chickky friends haus xD
but... i don't know much C# anyways... so a bit of code and an explaination of where it goes would be VERY useful on this

Re: EDITT: MarauderIIC I need help, YOURS because...

Posted: Wed Jun 17, 2009 2:14 pm
by M_D_K
mainly mem usage and CPU power needed for it to run smooth. Since the picture box isn't hardware accelerated.

Re: EDITT: MarauderIIC I need help, YOURS because...

Posted: Wed Jun 17, 2009 3:14 pm
by thejahooli
Do you know any good tutorials for it.
I can't get the tutorials on the website to work for me, probably because I forgot to include a file or something stupid but the tutorials fo not explain well

Re: EDITT: MarauderIIC I need help, YOURS because...

Posted: Wed Jun 17, 2009 3:18 pm
by dandymcgee
If you guys have never made any sort of editor before I would definitely recommend you start off with SDL or OpenGL, just drawing the tiles in a window. It's much less complicated and doing it on some sort of form. Once you can do that, you can essentially use the same code along-side a GUI. If you've done this and were just wondering what Mar used (I asked the exact same question recently, so you didn't search the forums very well in the first place ;) ), then disregard this post.

Re: EDITT: MarauderIIC I need help, YOURS because...

Posted: Wed Jun 17, 2009 3:51 pm
by thejahooli
dandymcgee wrote:If you guys have never made any sort of editor before I would definitely recommend you start off with SDL or OpenGL, just drawing the tiles in a window. It's much less complicated and doing it on some sort of form. Once you can do that, you can essentially use the same code along-side a GUI. If you've done this and were just wondering what Mar used (I asked the exact same question recently, so you didn't search the forums very well in the first place ;) ), then disregard this post.
I'm doing my level editor in SDL now but I would still like to know about using this with C# forms in case I want to use them in something else.

Re: EDITT: MarauderIIC I need help, YOURS because...

Posted: Wed Jun 17, 2009 5:40 pm
by MarauderIIC
RyanPridgeon: M_D_K got the problems I ran into 100% accurate. PictureBoxes are slow when they get big, and redrawing them isn't fast, either.
dandymcgee wrote:If you guys have never made any sort of editor before I would definitely recommend you start off with SDL or OpenGL, just drawing the tiles in a window. It's much less complicated and doing it on some sort of form.
Actually, with the GLControl, drawing the map is exactly the same as drawing the map outside of a form, in a normal OpenGL window. With that out of the way,
MadPumpkin wrote:a bit of code and an explaination of where it goes would be VERY useful on this
I don't want to teach you OpenGL basics here, so here's some summary followed by the only code that you won't find on NeHe:

I got OpenTK, and made a "public partial class Viewport : GLControl", which I then placed on my form.

For initialization, look @ NeHe's tutorial on Orthographic projection, or read the documentation on glOrtho (OpenTK: GL.Ortho)
My initialization uses GL.MatrixMode, GL.LoadIdentity, GL.Ortho and GL.Viewport (I'm pretty sure I just copied this bunch out of the tutorial on opentk.com).

Drawing the map is just iterating through my list of tiles and drawing the correct one from the tile sheet. The tile that the map has can be used as a direct index into the tilesheet's list of rectangles, when the tilesheet is loaded left to right top to bottom.

To draw, it's just the calls you can find in the tutorials on texture mapping with a tile sheet. Mine enables Texture2D, AlphaTest, uses Greater for the alpha function, and sets color to (1.0, 1.0, 1.0, 1.0) using GL.Color4(). My GL.Translate takes into account the values of my scroll bars.

The only thing that really differs from standard OpenGL techniques is the tile sheet loading, so here's that, with some parts edited out.

Code: Select all

        public TileSheet(string imageLocation)
        {
/* do some parameter validation... */
/* set some defaults... */
/* save the image location... */

            Bitmap sheet = new Bitmap(imageLocation); //TCR: Load the sheet's image so we can load it into opengl

/* save sheet width and height... */

            TilesHigh = SheetHeight / TileHeight;   //FIXME: Default value only; needs to load from file
            TilesWide = SheetWidth / TileWidth; //FIXME: Default value only; needs to load from file

/* Class "Tile" is pretty much just Rectangle with some extra class members that I found useful during map editing.
So this is just a glorified list of Rectangles. */
            Tiles = new List<Tile>(TilesHigh * TilesWide); 

            this.ID = GL.GenTexture();
            GL.BindTexture(TextureTarget.Texture2D, this.ID);

/* Copied this next bit from OpenTK forums. It probably does what you think it does. */
            BitmapData data = sheet.LockBits(new Rectangle(0, 0, sheet.Width, sheet.Height),
                ImageLockMode.ReadOnly, System.Drawing.Imaging.PixelFormat.Format32bppArgb);

            GL.TexImage2D(TextureTarget.Texture2D, 0, PixelInternalFormat.Rgba, data.Width, data.Height, 0, OpenTK.Graphics.PixelFormat.Bgra,
                PixelType.UnsignedByte, data.Scan0);
//            GL.TexEnv(TextureEnvTarget.TextureEnv, TextureEnvParameter.TextureEnvMode, (float)TextureEnvMode.Replace);
            GL.Finish(); /* Finish guarantees that the file is not being held by OpenGL, otherwise we get occasional exceptions during the next call */
            sheet.UnlockBits(data); /* Copied this from OpenTK forums ... */

            sheet.Dispose(); /* Get rid of our Bitmap, it's a memory hog */
            sheet = null; /* Ensure no references so the garbage collector doesn't get confused */
            data = null; /* Same for its data */

/* Do some standard NeHe stuff */
            GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMinFilter,
                (int)TextureMinFilter.Linear);

            GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMagFilter,
                (int)TextureMagFilter.Linear);

/* Increment tile cut-out coordinates by the size of a tile, where "1.0" is the size of the sheet. */
            float uInc = (float)TileWidth / (float)SheetWidth;
            float vInc = (float)TileHeight / (float)SheetHeight;
            float u1, v1, u2, v2;

            for (int y = 0; y < TilesHigh; ++y)
            {
                for (int x = 0; x < TilesWide; ++x)
                {
                    u1 = uInc * x;
                    v1 = vInc * y;

                    u2 = uInc * x + uInc;
                    v2 = vInc * y + vInc;
                    Tile t = new Tile(u1, v1, u2, v2);
                    Tiles.Add(t);
                }
            }
MadPumpkin: Let me know if you need more. I don't really want to teach you C# basics, either. :) You'll find it's a lot like C++ combined with Visual Basic.

Re: EDITT: MarauderIIC I need help, YOURS because...

Posted: Thu Jun 18, 2009 1:50 pm
by MadPumpkin
thank you very much again everyone! ima start adding that to my code xD