Developer Resources’s Weblog

June 14, 2008

DirectX draw triangle

Filed under: C#.NET, Triangle Example — Tags: , , , , , — developerresources @ 1:02 am

This is an example of how to draw a simple triangle using DirectX:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Microsoft.DirectX;
using Microsoft.DirectX.Direct3D;

namespace DirectXHelloWorld
{
    public partial class DirectXHelloWorld : Form
    {
        //Define device to write to.
        Device device = null;
        // Define a vertexBuffer to store the 3d objects.
        VertexBuffer vertexBuffer = null;

        //Main method.
        static void Main()
        {
            // Instantiate form, initialize graphics and show the form.
            DirectXHelloWorld form = new DirectXHelloWorld();
            form.InitializeGraphics();
            form.Show();

            // Render loop
            while (form.Created)
            {
                form.render();
                Application.DoEvents();
            }
        }
        // Called to draw screen.
        public void render()
        {
            //Clear the backbuffer to a blue color (ARGB = 000000ff)
            device.Clear(ClearFlags.Target, System.Drawing.Color.Black, 1.0f, 0);

            device.BeginScene();

            device.SetStreamSource(0, vertexBuffer, 0);
            device.VertexFormat = CustomVertex.TransformedColored.Format;
            device.DrawPrimitives(PrimitiveType.TriangleList, 0, 1);

            //End the scene
            device.EndScene();
            device.Present();
        }
        // Initalize the graphics system.
        public void InitializeGraphics()
        {
            try
            {
                PresentParameters presentParams = new PresentParameters();
                presentParams.Windowed = true;
                presentParams.SwapEffect = SwapEffect.Discard;

                // On my chepo laptop I don't have  hardward support so I'm
                // using SoftwareVertexProcessing. If you have a real graphics
                // card you should change this to HardwareVertexProcessing.
                device = new Device(0,
                      DeviceType.Hardware,
                      this,
                      CreateFlags.SoftwareVertexProcessing,
                      presentParams);
                device.DeviceReset+=
                       new System.EventHandler(this.OnResetDevice);
                OnResetDevice(device, null);

            }
            catch (DirectXException e)
            {

                MessageBox.Show(null, "Error intializing graphics: " + e.Message, "Error");
                Close();
            }
        }
            public void OnResetDevice(object sender, EventArgs e)
            {
                Device dev = (Device)sender;
                vertexBuffer = new VertexBuffer(typeof(CustomVertex.TransformedColored),3, dev,
                0,
                CustomVertex.TransformedColored.Format,
                Pool.Default);

                GraphicsStream stm = vertexBuffer.Lock(0, 0, 0);
                CustomVertex.TransformedColored[] verts =  new CustomVertex.TransformedColored[3];

                verts[0].X = 150;
                verts[0].Y = 50;
                verts[0].Z = 0.5f;
                verts[0].Rhw = 1;
                verts[0].Color = System.Drawing.Color.Red.ToArgb();
                verts[1].X = 250;
                verts[1].Y = 250;
                verts[1].Z = 0.5f;
                verts[1].Rhw = 1;
                verts[1].Color = System.Drawing.Color.Green.ToArgb();
                verts[2].X = 50;
                verts[2].Y = 250;
                verts[2].Z = 0.5f;
                verts[2].Rhw = 1;
                verts[2].Color = System.Drawing.Color.Blue.ToArgb();
                stm.Write(verts);
                vertexBuffer.Unlock();
            }

    }

}

For more information on DirectX with c# I recommend the following book: <a href=”http://www.amazon.com/gp/product/1568812361?ie=UTF8&tag=develoresour-20&linkCode=as2&camp=1789&creative=9325&creativeASIN=1568812361″>C# and Game Programming: A Beginner’s Guide, Second Edition (Book & CD-ROM)</a><img src=”http://www.assoc-amazon.com/e/ir?t=develoresour-20&l=as2&o=1&a=1568812361″ width=”1″ height=”1″ border=”0″ alt=”" style=”border:none !important; margin:0px !important;” />

June 13, 2008

Python mysql access

Filed under: Python, mysql — Tags: , , , — developerresources @ 11:55 pm

Here is how you can access mysql using python:

#!/usr/bin/python

import MySQLdb
import sys
import time

db = MySQLdb.connect(host="reportsdb",user="root", db="tool_metrics")
dbc = db.cursor()
str = "select '1','2','3'";
dbc.execute(str)
records = dbc.fetchall()

for r in records:
    print r[0]+","+r[1]+","+r[2]

For more information on Python and databases I recommend the following book for futher reading: <a href=”http://www.amazon.com/gp/product/0596009402?ie=UTF8&tag=develoresour-20&linkCode=as2&camp=1789&creative=9325&creativeASIN=0596009402″>Python Pocket Reference (Pocket Reference (O’Reilly))</a><img src=”http://www.assoc-amazon.com/e/ir?t=develoresour-20&l=as2&o=1&a=0596009402″ width=”1″ height=”1″ border=”0″ alt=”" style=”border:none !important; margin:0px !important;” />

Ruby database access using mysql dbd.

Filed under: Ruby, mysql — Tags: , , — developerresources @ 10:58 pm

Step 1: Install mysql driver for ruby.

You can download package from here

Execute: ruby extconf.rb –with-mysql-config

Then make;make install

Step 2: Write simple ruby client.

  require "mysql"
  my = Mysql.connect("localhost", "", "", "test")
  puts my
  res = my.query("select * from tblMovies")
  res.each do |row|
    puts row[0]+row[1]
  end

That’s it! Now you can access a database with Ruby.

undefined symbol: __pure_virtual

Filed under: Ruby — Tags: , , , — developerresources @ 10:37 pm

While trying to install mysql dbd module for ruby I encountered the following error:

/usr/local/ruby/lib/ruby/site_ruby/1.8/i686-linux/mysql.so: /usr/local/ruby/lib/                                           ruby/site_ruby/1.8/i686-linux/mysql.so: undefined symbol: __pure_virtual - /usr/                                           local/ruby/lib/ruby/site_ruby/1.8/i686-linux/mysql.so (LoadError)        from ruby-db-client.rb:4

After some googling I found this is a problem with mysql 5.0.22. I was able to fix it by modyfing /usr/local/mysql/bin/mysql_config and adding -lgcc to the libs argument.

After reinstalling ruby everything worked fine.

VB/VBA Call WebService

Filed under: Web Services — Tags: , , — developerresources @ 8:11 pm

You can call a webservice from VBA simply by posting the soap envelope. Here is an example:

    Dim http As New WinHttp.WinHttpRequest
    Dim URL As String
    Dim envelope As String
    URL = "http://notificationserver/NotificationServer/NotifyService"
    envelope = "<?xml version=""1.0"" encoding=""UTF-8""?><soap:Envelope soap:encodingStyle=""http://schemas.xmlsoap.org/soap/encoding/"" xmlns:soap=""http://schemas.xmlsoap.org/soap/envelope/"" xmlns:soapenc=""http://schemas.xmlsoap.org/soap/encoding/"" xmlns:xsi=""http://www.w3.org/2001/XMLSchema-instance"" xmlns:xsd=""http://www.w3.org/2001/XMLSchema"" xmlns:tns=""http://NotificationServer.nvidia.com/""><soap:Body><tns:notify><id xsi:type=""xsd:string"">blah</id><from xsi:type=""xsd:string"">from </from><subject xsi:type=""xsd:string"">subject</subject><details xsi:type=""xsd:string"">details</details></tns:notify></soap:Body></soap:Envelope>"

    Call http.Open("POST", URL)

    http.SetRequestHeader "Content-Type", "text/xml"
    http.SetRequestHeader "SOAPAction", " "

    http.Send envelope
    MsgBox http.ResponseText

June 12, 2008

Got error 28 from storage engine query

Filed under: mysql — Tags: — developerresources @ 11:50 pm

I ran into this problem today with mysql. Took me a while to figure out that it was caused by my /tmp drive being full.

Clearing the /tmp drive solved the issue.

C++ How to trim a string

Filed under: c++ — Tags: , , , — developerresources @ 8:11 am

To trim a string in C++ you should use the Boost library. Writing C++ without Boost is no fun. Boost provides great libraries for almost any c++ work and its a proving ground for future revisions of the c++ standard library.

So how do you trim a string in c++ using boost?

#include <iostream>
#include <boost/algorithm/string/trim.hpp>
int main(int argc, char * argv[]) {
  cout << trim(argv[1]);
}

For more information about boost I recommend this book: Beyond the C++ Standard Library: An Introduction to Boost

June 9, 2008

Python database access

Filed under: Python — Tags: , , , — developerresources @ 11:13 pm

Here is an example of how to connect to a MysqlDB using python.

#!/usr/bin/python

import MySQLdb
import sys

class Table:
    def __init__(self, db, name):
        self.db = db
        self.name = name
        self.dbc = self.db.cursor()

    def execute(self):
        query = "select 1,2 from you_table "
        self.dbc.execute(query)
        return self.dbc.fetchall()

    def next(self):
        return self.dbc.fetchone()

db = MySQLdb.connect(host="<your host>",user="<your username>", db="<your db>", passwd=<your passwd>)
event_table = Table(db, "event_table")

records = event_table.execute()
for r in records:
    print str(r[0])+","+str(r[1])

Ruby SOAP Client

Filed under: Examples, Ruby, Web Services — Tags: , , , — developerresources @ 8:20 am

Here is an example of how to call a web service using Ruby. This example requires no external libraries, everything is built into the language. However it does require version 1.8.5 early version don’t have the create_rpc_driver method.



#Requires Ruby version 1.8.5 or highet
require 'soap/wsdlDriver'
require 'pp'
wsdl = 'http://webservices.daehosting.com/services/isbnservice.wso?WSDL'
driver = SOAP::WSDLDriverFactory.new(wsdl).create_rpc_driver

# Log SOAP request and response
driver.wiredump_file_base = "soap-log.txt"

response =  driver.IsValidISBN13(:sISBN => '0000000000000')
#pp(response)
puts response.isValidISBN13Result

For more information about Ruby and Web Services I recommend Ruby Cookbook (Cookbooks (O’Reilly)).

Java Soap Client using axis library

Filed under: Examples, Java, Web Services — Tags: , , — developerresources @ 6:55 am

This post is an example Java Soap client call using the apache axis library.

First you need to download axis from ws.apache.org. This library provides tools to convert a wsdl into a Java stub class that you can then use.

Once you’ve extracted the axis directory run the following command on the wsdl that you want to consume.

java -cp axis-1_4/lib/wsdl4j-1.5.1.jar:axis-1_4/lib/saaj.jar:axis-1_4/lib/jaxrpc.jar:axis-1_4/lib/axis.jar:axis-1_4/lib/commons-logging-1.0.4.jar:axis-1_4/lib/commons-discovery-0.2.jar:. org.apache.axis.wsdl.WSDL2Java http://webservices.daehosting.com/services/isbnservice.wso?WSDL

If you have your classpath set up you probably don’t need as much but for simplicity I included all the jars in the axis distribution.

Now you can use the stubs that were generated



import com.daehosting.webservices.ISBN.*;

class client {

  public static void main(String argv[]) throws Exception {

    ISBNServiceSoapType service = new ISBNServiceLocator().getISBNServiceSoap();

    System.out.println(service.isValidISBN13("0000000000000"));

  }

}

For further reading I recommend the following book Java Web Services.

Older Posts »

Blog at WordPress.com.