XNA Creators Club Online
Page 1 of 1 (4 items)
Sort Posts: Previous Next

DirectX on Windows mobile. Problem with Alphablending

Last post 8/14/2009 6:38 PM by PhilTaylor. 3 replies.
  • 8/10/2009 5:12 PM

    DirectX on Windows mobile. Problem with Alphablending

    Hi all, this is my first post so I hope I am in the right place.

    Just wanted to play with DirectX on windows mobile using C#. The problem I'm having is drawing sprites with alphablend.
    Here is my code

    1 namespace DxUi 
    2
    3     public partial class Form1 : Form 
    4     { 
    5         // The d3d device 
    6         private Device device = null
    7  
    8         private Sprite Wall; 
    9         private Texture Tx; 
    10  
    11         string path; 
    12         List<imgButtons> UiButtons = new List<imgButtons>(); 
    13  
    14         public Form1() 
    15         { 
    16             InitializeComponent(); 
    17             path = System.IO.Path.GetDirectoryName(Assembly.GetExecutingAssembly().GetName().CodeBase); 
    18  
    19             // Now let's setup our D3D parameters 
    20             PresentParameters presentParams = new PresentParameters(); 
    21             presentParams.Windowed = true
    22             presentParams.SwapEffect = SwapEffect.Discard;           
    23  
    24             // create the device 
    25             device = new Device(0, DeviceType.Default, this
    26                 CreateFlags.None, presentParams);             
    27  
    28             device.DeviceReset += new EventHandler(device_DeviceReset); 
    29  
    30             Tx = TextureLoader.FromFile(device, path + @"\img\Wall.bmp"); 
    31             Wall = new Sprite(device);             
    32  
    33             device_DeviceReset(null, EventArgs.Empty); 
    34  
    35             LoadSprites(); 
    36         } 
    37  
    38         void device_DeviceReset(object sender, EventArgs e) 
    39         { 
    40             //Tx = TextureLoader.FromFile(device, path + @"\img\Wall.bmp"); 
    41         } 
    42  
    43         public void Render() 
    44         { 
    45                 try 
    46                 { 
    47                     // start drawing commands 
    48                     device.BeginScene(); 
    49                     // clears the frame 
    50                     device.Clear(ClearFlags.Target, 0, 1.0f, 0); 
    51  
    52                     device.RenderState.AlphaBlendEnable = true
    53  
    54                     device.RenderState.SourceBlend = Blend.SourceAlpha; 
    55                     device.RenderState.DestinationBlend = Blend.InvDestinationAlpha; 
    56                     device.RenderState.AlphaTestEnable = true;                     
    57  
    58                     Wall.Begin(SpriteFlags.AlphaBlend); 
    59  
    60                     Rectangle rect = new Rectangle(0, 0, this.Width, this.Height); 
    61  
    62                     Wall.Draw(Tx, rect, Vector3.Empty, new Vector3(0, 10, 0), Color.White);                     
    63  
    64                     foreach (imgButtons ib in UiButtons) 
    65                     { 
    66                         Wall.Draw(ib.ButtonNorm, ib.rectangle, Vector3.Empty, new Vector3(ib.X, ib.Y - 50, 0), Color.White);                         
    67                     }                     
    68  
    69                     Wall.End(); 
    70  
    71                     device.RenderState.AlphaBlendEnable = false
    72                 } 
    73                 finally 
    74                 { 
    75                     // end the drawing commands and copy to screen 
    76                     device.EndScene(); 
    77                     device.Present(); 
    78                }             
    79         } 
    80  
    81         protected override void OnPaintBackground(PaintEventArgs e) 
    82         { 
    83             // do nothing 
    84         } 
    85  
    86         /// <summary> 
    87         /// Called when the window needs to be repainted 
    88         /// </summary> 
    89         /// <param name="e">Unused</param> 
    90         protected override void OnPaint(System.Windows.Forms.PaintEventArgs e) 
    91         { 
    92             // Render on painting 
    93             this.Render(); 
    94             Thread.Sleep(50); 
    95             Application.DoEvents(); 
    96             // Render again 
    97             this.Invalidate(); 
    98         } 
    99  
    100         void LoadSprites() 
    101         { 
    102             XDocument btns = XDocument.Load(path + @"\UISettings.xml"); 
    103  
    104             var query = from button in btns.Descendants("imgButton"
    105                         select new imgButtons 
    106                         { 
    107                             X = int.Parse((string)button.Element("XPos")), 
    108                             Y = int.Parse((string)button.Element("YPos")),                             
    109                             ButtonPath = path + (string)button.Element("imgPath"), 
    110                             ButtonPushPath = path + (string)button.Element("pushPath"), 
    111                             ProgramToRun = (string)button.Element("Process"), 
    112                             ButtonName = (string)button.Element("ButtonName"), 
    113                              
    114                         }; 
    115  
    116             foreach (imgButtons ibtn in query) 
    117             {                 
    118                 ibtn.CreateImages(device); 
    119                 ibtn.CreateClient(path); 
    120                 UiButtons.Add(ibtn); 
    121             } 
    122         } 
    123     } 
    124

    The function which loads the texture for sprites
    1 public void CreateImages(Device d) 
    2         {             
    3             PaletteEntry[] p; 
    4             ButtonNorm = TextureLoader.FromFile(d, ButtonPath, 0, 0, D3DX.Default, Usage.None, Format.P8, Pool.Managed, 
    5                 Filter.None, Filter.None, 0, out p); 
    6  
    7             ButtonPush = TextureLoader.FromFile(d, ButtonPushPath, D3DX.Default, D3DX.Default, D3DX.Default, Usage.None, Format.A8R8G8B8, Pool.Managed, 
    8                  Filter.None, Filter.None, 0, out p); 
    9             sprite = new Sprite(d); 
    10         } 

    I tried loading the textures a little different as various tutorials I have read seem to say different things.
    The textures are loaded from png files.

    The problem is that the part of the image that starts to go semi-transparent is just drawn transparent.

    Any ideas?
  • 8/12/2009 4:18 PM In reply to

    Re: DirectX on Windows mobile. Problem with Alphablending

    If you only want to blend you should not activate the alpha test. The alpha test will cull pixel completely.
  • 8/13/2009 7:38 AM In reply to

    Re: DirectX on Windows mobile. Problem with Alphablending

    Thanks for the reply. I have tried it with True and False but still no difference.
  • 8/14/2009 6:38 PM In reply to

    Re: DirectX on Windows mobile. Problem with Alphablending

    does a 1:1 blend work?

    what do the device caps show for the hw?

    is there any debug output?

    what does the ref device show?
    http://www.futuregpu.net ex-Aces Lead PM/ex DX SDK PM/ex D3D Evangelist now LRB Launch Native Title Wrangler
Page 1 of 1 (4 items) Previous Next
var gDomain='m.webtrends.com'; var gDcsId='dcschd84w10000w4lw9hcqmsz_8n3x'; var gTrackEvents=1; var gFpc='WT_FPC'; /*<\/scr"+"ipt>");} /*]]>*/
DCSIMG