Welcome Guest! To enable all features please Login or Register.

Notification

Icon
Error

Options
Go to last post Go to first unread
Offline Robert Walter  
#1 Posted : Thursday, November 24, 2011 3:47:39 PM(UTC)
Robert Walter

Joined: 8/24/2011(UTC)
Posts: 68

Thanks: 4 times
For my drawing program I have written a static class Pixel, which can draw Pixels in different sizes. Basically it is using the Rect.DrawFilled at the given position used as center point with the given size (in pixels) and color. But I think the usage is easier this way, because of the size, which represents real pixels.
Maybe this comes in handy for anyone else.

Code:

using System;
using Delta.Utilities.Datatypes;
using Delta.Rendering.Basics.Drawing;
using Delta.Engine;
using Delta.Utilities;

namespace DeltaFindIt
{
    public static class Pixel
    {
        /// <summary> 
        /// Calculate the width of one pixel in pixel space for the actual resolution 
        /// </summary> 
        static readonly Size onePixel = 1.0f / ScreenSpace.ToPixelSpace(Size.One); 
        
        /// <summary> 
        /// draws a single pixel with the given middlePoint and size
        /// </summary> 
        /// <param name="middlePoint">The center of the Pixel.</param>
        /// <param name="size">the size of the Pixel</param>
        /// <param name="color">the color of the Pixel</param>
        public static void DrawPixel(Point middlePoint, int size, Color color)
        {
            Point leftTop = middlePoint - (((float)(size/2))*onePixel);
            Size rectSize = size * onePixel;
            Rectangle rect = new Rectangle(leftTop, rectSize);
            Rect.DrawFilled(rect, color);
        }
    }
}

Edited by user Thursday, November 24, 2011 4:21:17 PM(UTC)  | Reason: moved size calculation

Wanna join the discussion?! Login to your forum accountregister a new account. Or Connect via Facebook Twitter Google

Offline Benjamin  
#2 Posted : Thursday, November 24, 2011 3:54:38 PM(UTC)
Benjamin

Medals: Admin

Joined: 8/20/2011(UTC)
Posts: 1,421
Location: Hannover

Thanks: 18 times
Was thanked: 97 time(s) in 92 post(s)
You could move the line to the static declaration for some additional speedup (ToPixelSpace is actually the slowest method in your code besides the actual drawing). Maybe update it if your application can change the resolution (normally not needed).
Code:

             /// <summary>
             /// Calculate the width of one pixel in pixel space for the actual resolution
             /// </summary>
             static readonly Size onePixel = 1.0f / ScreenSpace.ToPixelSpace(Size.One); 
Offline Robert Walter  
#3 Posted : Thursday, November 24, 2011 4:20:00 PM(UTC)
Robert Walter

Joined: 8/24/2011(UTC)
Posts: 68

Thanks: 4 times
Yeah actually it doesn't have the best performance, but by moving the line into the static declaration helps to speed it up quite a bit (have edited that in my first post). Thanks for the tip! :)

Is there a better way to speed up the drawing? I already tried to do it with drawing lines, but that doesn't seem to speed things up.
Offline Benjamin  
#4 Posted : Thursday, November 24, 2011 4:49:25 PM(UTC)
Benjamin

Medals: Admin

Joined: 8/20/2011(UTC)
Posts: 1,421
Location: Hannover

Thanks: 18 times
Was thanked: 97 time(s) in 92 post(s)
Well, the actual drawing is probably really fast. What is slow is that you call Rect.DrawFilled so often. You could implement what DrawFilled does yourself, create a geometry and add your own vertices here, then render it out once per frame. This will be much faster, but also take a lot more work to implement.

Another thing you might not be doing now is to check if a "Custom Pixel of your" has already been drawn this frame with the same color and position, then you can skip it completely.

I suggest using build-in functionality of the graphic APIs to avoid that (use lines, polygons, etc. instead of building your own pixel painting system). What you could also do is reduce the resolution and thus limit the amount of pixels you have to draw and calculate.

As long as your performance is acceptable (more than 60fps), you probably don't need to worry about it.
Rss Feed  Atom Feed
Users browsing this topic
OceanSpiders 2.0
Forum Jump  
You cannot post new topics in this forum.
You cannot reply to topics in this forum.
You cannot delete your posts in this forum.
You cannot edit your posts in this forum.
You cannot create polls in this forum.
You cannot vote in polls in this forum.

Powered by YAF.NET | YAF.NET © 2003-2022, Yet Another Forum.NET
This page was generated in 0.046 seconds.